1 /*
   2  * Copyright (c) 2012-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
   3  *
   4  * Permission is hereby granted, free of charge, to any person obtaining a copy
   5  * of this software and associated documentation files (the "Software"), to deal
   6  * in the Software without restriction, including without limitation the rights
   7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8  * copies of the Software, and to permit persons to whom the Software is
   9  * furnished to do so, subject to the following conditions:
  10  *
  11  * The above copyright notice and this permission notice shall be included in
  12  * all copies or substantial portions of the Software.
  13  *
  14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20  * SOFTWARE.
  21  */
  22 
  23 #ifndef __PARSE_H
  24 #define __PARSE_H
  25 
  26 #include <jeffpc/error.h>
  27 #include <jeffpc/sexpr.h>
  28 
  29 #include "req.h"
  30 #include "post.h"
  31 
  32 struct parser_output {
  33         struct req *req;
  34         struct post *post;
  35 
  36         void *scanner;
  37         char *output;
  38         struct str *stroutput;
  39         struct val *valoutput;
  40 
  41         const char *input;
  42         size_t len;
  43         size_t pos;
  44         int lineno;
  45 
  46         /* template related */
  47         bool cond_stack[COND_STACK_DEPTH];
  48         int cond_stack_use;
  49 
  50         /* fmt3 */
  51         int table_nesting;
  52         int texttt_nesting;
  53 
  54         /* fmt3 special commands */
  55         struct str *sc_title;
  56         struct str *sc_pub;
  57         struct val *sc_tags;
  58         struct val *sc_cats;
  59         struct str *sc_twitter_img;
  60 };
  61 
  62 extern int fmt3_lex_destroy(void *yyscanner);
  63 extern int tmpl_lex_destroy(void *yyscanner);
  64 extern int fmt3_parse(struct parser_output *data);
  65 extern int tmpl_parse(struct parser_output *data);
  66 extern void fmt3_set_extra(void *user_defined, void *yyscanner);
  67 extern void tmpl_set_extra(void *user_defined, void *yyscanner);
  68 extern int fmt3_lex_init(void **scanner);
  69 extern int tmpl_lex_init(void **scanner);
  70 
  71 static inline bool cond_value(struct parser_output *data)
  72 {
  73         int i;
  74 
  75         for (i = 0; i <= data->cond_stack_use; i++)
  76                 if (!data->cond_stack[i])
  77                         return false;
  78 
  79         return true;
  80 }
  81 
  82 static inline void cond_if(struct parser_output *data, bool val)
  83 {
  84         ASSERT3S(data->cond_stack_use, <, COND_STACK_DEPTH);
  85 
  86         data->cond_stack[++data->cond_stack_use] = val;
  87 }
  88 
  89 static inline void cond_else(struct parser_output *data)
  90 {
  91         ASSERT3S(data->cond_stack_use, >=, 0);
  92 
  93         data->cond_stack[data->cond_stack_use] = !data->cond_stack[data->cond_stack_use];
  94 }
  95 
  96 static inline void cond_endif(struct parser_output *data)
  97 {
  98         ASSERT3S(data->cond_stack_use, >=, 0);
  99 
 100         data->cond_stack_use--;
 101 }
 102 
 103 /*
 104  * Format "4" (markdown) entry point:
 105  */
 106 extern struct str *fmt4_md_to_html(struct str *);
 107 #endif