File: parser.h

package info (click to toggle)
yodl 4.04.00-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,720 kB
  • sloc: ansic: 7,803; perl: 683; cpp: 570; sh: 411; xml: 190; makefile: 164
file content (207 lines) | stat: -rw-r--r-- 9,471 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef INCLUDED_PARSER_H_
#define INCLUDED_PARSER_H_

#include <stdio.h>
#include <stdlib.h>

#include "../stack/stack.h"
#include "../hashmap/hashmap.h"
#include "../lexer/lexer.h"
#include "../ostream/ostream.h"
#include "../chartab/chartab.h"
#include "../subst/subst.h"

typedef enum
{
    COLLECT_SET,                /* retrieve the parameter list as it is */
                                /* encountered on the input             */

    DEFAULT_SET,                /* process macros, builtins etc.        */
                                /* By default phandle_insert() is       */
                                /* called                               */

    NOTRANS_SET,                /* insert the parameter list using the  */
                                /* currently active insertion function  */
                                /* otherwise identical to NOEXPAND_SET  */
                                /* The character translation table is   */
                                /* not used                             */

    IGNORE_SET,                 /* completely skip the parameter list   */

    NOEXPAND_SET,               /* do not expand the parameter list     */
                                /* but return the parameterlist         */
                                /* CHAR() is interpreted                */

    SKIPWS_SET,                 /* consume all ws characters            */
                                /* lexer_lex() returns the next non-ws  */
                                /* character                            */

    SIZEOF_HANDLER_SET_ELEMENTS
}
HANDLER_SET_ELEMENTS;
    /* see also phandler.c and pparse.c. Definitions of which functions */
    /* are called when is defined in p_setupHandlerSet                  */

typedef struct Parser
{
    bool        d_show_nomacros;        /* show non-macro calls             */
    size_t      d_max_macro_nesting;    /* max nesting level when calling   */
                                        /* (user defined) macros            */
    bool        d_keep_ws;              /* true: lexer keeps initial ws on  */
                                        /* lines                            */
    int         d_ws_level;             /* ws skiplevel                     */
    Stack       d_ws_level_st;          /* stacked wslevels when pushing    */
    size_t      d_parcount;             /* increments at each new paragraph */
                                        /* activation. Accessible as         */
                                        /* counter XXparcount               */
    size_t      d_parlist_lineno;       /* begin line and filename of       */
    char       *d_parlist_filename;     /* a parameter list                 */
                                        /* (used for handling premature EOF */
                                        /* when scanning parameter lists)   */
    size_t      d_paren;                /* counts parentheses of current    */
                                        /* parameter list                   */
    Stack       d_paren_st;             /* counts parentheses               */
                                        /* in open-/closeparen() functions  */
    Subst       d_subst;
    Lexer       d_lexer;
    Stack       d_atexit_st;            /* stores char *'s                  */
    Stack       d_string_st;            /* stores String *'s which are      */
                                        /* returned, and should not be      */
                                        /* destroyed by the stack           */
    String      *d_string_ptr;          /* output media for insert_string() */
    Ostream     *d_outs_ptr;            /* output media for insert_ostream()*/
    char        *d_indexName;           /* name of the index file (or 0)    */
    FILE        *d_indexFile;           /* file containing the index        */

                /* d_insert determines the target for the produced output   */
                /* this can be a String or an Ostream. It is set by         */
                /* various functions to obtain nested text. Used by         */
                /* the interrnal p_filter and p_dont_filter functions to    */
                /* find the destination of the produced characters          */
    void (*d_insert)(struct Parser *, char const *);
    Stack d_insert_st;                  /* stacked inserters                */


                /* d_handle is a pointer to an array of pointers to         */
                /* functions, having as element for each defined lexical    */
                /* scanner token, defining how to process that particular   */
                /* token in a given situation.                              */
                /* The functions are all called p_handle...., and return    */
                /* bool if processing should continue                       */
    bool (**d_handler)(struct Parser *);    /* active set of handlers       */
    Stack d_handler_st;                     /* stacked handler ptrs         */

    HashMap     *d_symtab_ptr;
    size_t       d_useSubstBits;
    bool         d_useSubst;
}
Parser;

extern int parser_data;

void        parser_construct(Parser *pp, HashMap *symtab, Ostream *outs);
void        parser_destruct(Parser *pp);

void        parser_allow_subst(register Parser *pp, bool onOff);
void        parser_apply_chartab(register Parser *pp);
void        parser_atexit(Parser *pp, char *text);
void        parser_discard(Parser *pp, SymbolType type,
                           char const *fun, char const *msg);
void        parser_empty_parlist(Parser *pp);
char       *parser_eval(register Parser *pp, register char *arg);
void        parser_if(Parser *pp, SymbolType type, char const *fun);
void        parser_if_cond(Parser *pp,
                            bool (*comparator)(Parser *pp, char **parlist),
                            char const *fun, size_t nparlists);
bool        parser_if_empty(Parser *pp, char **parlist);
bool        parser_if_equal(Parser *pp, char **parlist);
bool        parser_if_greater(Parser *pp, char **parlist);
bool        parser_if_smaller(Parser *pp, char **parlist);
bool        parser_if_strsub(Parser *pp, char **parlist);
bool        parser_if_zero(Parser *pp, char **parlist);
void        parser_includefile(Parser *pp, char const *filename);
void        parser_insertSubst(register Parser *pp, int index);
char       *parser_name_parlist(Parser *pp, bool skipws);
char       *parser_nochartab_eval(register Parser *pp, register char *arg);
void        parser_noexpand_include(Parser *pp,
                                    char const *filename, bool searchPath);
Result      parser_number_parlist(Parser *pp, int *value, bool skipws);
void        parser_push_fun(char const *name);
void        parser_push_ws_level(Parser *pp, int value);
void        parser_pop(Parser *pp, SymbolType type, char const *fun,
                                                            char const *msg);

            /* NULL with INSERT_SET, IGNORE_SET and NOEXPAND_SET            */
            /* never NULL with COLLECT_SET and DEFAULT_SET                  */
            /* never returns outer ()                                       */
            /* consumes the parlist including the outer ()                  */
void        parser_pop_ws_level(Parser *pp);
char       *parser_parlist(Parser *pp, HANDLER_SET_ELEMENTS newSet);

void        parser_process(Parser *pp);
void        parser_skipws(Parser *pp);
char const *parser_strvalue(register Parser *pp, char const *txt);

void      (*parser_suppress_chartab(Parser *pp))
                                            (struct Parser *, char const *);

Result      parser_value(Parser *pp, int *value, char const *text);
void        parser_pushSubst(register Parser *pp, bool value);
void        parser_popSubst(register Parser *pp);

static inline size_t            parser_ws_level(register Parser *pp);
static inline void              parser_pop_fun();
static inline void              parser_inc_ws_level(register Parser *pp);
static inline char const       *parser_fun();
static inline void              parser_dec_ws_level(register Parser *pp);
static inline bool              parser_if_strequal(register Parser *pp, 
                                               register char **parlist);
// static inline StrVector const  *parser_strVector(register Parser *pp);


/* == end of interface section ============================================ */


extern Stack ps_fun_st;              /* stores the names of functions    */

void    p_set_ws_level(Parser *pp, int value);


static inline size_t parser_ws_level(register Parser *pp)
{
    return pp->d_ws_level;
}

static inline void parser_pop_fun()
{
    stack_pop(&ps_fun_st);
}

static inline void parser_inc_ws_level(register Parser *pp)
{
    p_set_ws_level(pp, ++pp->d_ws_level);
}

static inline char const *parser_fun()
{
    return stack_tos(&ps_fun_st)->u_charCp;
}

static inline void parser_dec_ws_level(register Parser *pp)
{
    p_set_ws_level(pp, --pp->d_ws_level);
}

static inline bool parser_if_strequal(register Parser *pp, 
                                      register char **parlist)
{
    return !strcmp(parser_strvalue(pp, parlist[0]), 
            parser_strvalue(pp, parlist[1]));
}

//static inline StrVector const *parser_strVector(register Parser *pp)
//{
//    return subst_strVector(&pp->d_subst);
//}

#endif