switch -O2 to -O1 for zetacom compilation
[xoc.git] / zeta / grammar.h
blob846136453bf1e8a1f72ca1b583fcc60eca33ad9e
1 // Run-time for grammars
3 typedef struct ParseValue ParseValue;
4 typedef struct Gram Gram;
5 typedef struct GramPrec GramPrec;
6 typedef struct Ast Ast;
7 typedef struct Line Line;
9 // CFG values
10 typedef ParseValue CfgValue;
11 typedef void SlrLexerArg;
13 #include "cfg.h"
14 #include "slr.h"
15 #ifdef GLR
16 #include "glr.h"
17 #define RgTokenL GlrTokenL
18 #define RgToken GlrToken
19 #define RgParse GlrParse
20 #define mkRgTokenL mkGlrTokenL
21 #define revRgTokenL revGlrTokenL
22 #define RgTokenLL GlrTokenLL
23 #define mkRgTokenLL mkGlrTokenLL
24 #define revRgTokenLL revGlrTokenLL
25 #define rgnewparse glrnewparse
26 #define rgfreeparse (void)
27 #define rgnewtoken glrnewtoken
28 #define rgparse glrparse
29 #else
30 #include "rnglr.h"
31 #endif
33 LIST(CfgSym)
34 LIST(Gram)
35 LIST(Ast)
37 // DFA values
38 typedef CfgSym DKey;
39 typedef CfgSymL DKeyL;
41 #include "nfa.h"
42 #include "dfa.h"
44 struct Gram
46 Name name;
47 Type *type;
48 CfgGram *cfg;
49 Hash *syms;
50 DFA *dfa;
51 int dfaprec;
52 Hash *precs;
53 Hash *ignores;
54 Hash *rules;
55 CfgSym *ignore;
56 CfgSym *any;
57 TypeL *gramx1s;
60 struct GramPrec
62 CfgPrec *cfgprec;
65 Gram* mkGram(Name name);
66 void gramaddrule(Type*, GramPrec*, CfgSym*, CfgSymL*);
67 CfgSym* gramsym(Type*, Name);
68 CfgSym* gramliteral(Type*, Name);
69 CfgSym* gramregexp(Type*, Name, int regexpflags, int canfail);
70 GramPrec* gramprec(Gram*, Name, CfgAssoc, CfgPrecKind, int creat);
71 CfgSym* gramlooksym(Gram*, Name);
72 CfgRule* gramlookrule(Gram*, Name);
73 CfgSym* gramunlistsym(Gram*, CfgSym*);
75 int gramignoring(Gram*, CfgSym*);
76 CfgSym* gramignore(Gram*, Name, int, int);
77 int grampriority(Gram*, Name, Name, int);
79 // Parsing
81 struct Line
83 Name file;
84 int line;
87 // TODO: Replace ParseValue with Ast.
88 struct ParseValue
90 int ref; // reference count during parsing
91 int refcheck;
92 Ast *ast;
93 Line line;
95 ParseValue *mkParseValue(Ast*);
96 void pvfree(CfgSym*, CfgValue*);
97 void pvdup(CfgSym*, CfgValue**, CfgValue*);
99 Ast *parsetext(Type*, Name);
100 int astfmt(Fmt*);
102 // Abstract syntax trees
103 #define NoParentYet ((Ast*)-1)
105 typedef struct Attr Attr;
106 typedef enum AstOp {
107 AstString = 1,
108 AstRule,
109 AstMerge,
110 AstSlot,
111 } AstOp;
112 struct Ast
114 uchar op;
115 uchar slotnum;
116 uchar ref;
117 uchar nright;
118 // int refcheck;
119 Ast *canonical;
120 Ast *copiedfrom;
121 Type *tag;
122 Line line;
123 Ast *parent;
124 Map attrmap;
126 CfgSym *sym;
127 CfgRule *rule;
128 Name text;
129 Ast **right;
131 Ast *mkastrule(CfgRule*, Ast**);
132 Ast *mkaststring(CfgSym*, Name);
133 Ast *mkastmerge(Ast*, Ast*);
134 Ast *mkastslot(Type*, CfgSym*, int);
135 Ast *mkAst(AstOp, CfgSym*);
137 Ast *asttoquestion(Ast*, CfgSym*); /* sym is ? type */
138 Ast *astfromquestion(Ast*, CfgSym*); /* sym is ? type */
139 Ast *astlisttostar(Zlist*, CfgSym*); /* sym is * type */
140 Ast *astlisttoplus(Zlist*, CfgSym*); /* sym is + type */
141 int aststartolist(Ast*, CfgSym*, Zlist**); /* sym is * type */
142 int astplustolist(Ast*, CfgSym*, Zlist**); /* sym is + type */
143 Zlist *asttolist(Ast*);
144 Zarray *astsplit(Ast*);
145 void astremovecopiedfrom(Ast*);
146 Ast *astjoin(Ast*, Zarray*);
147 Ast *astcopy(Ast*);
148 Ast *astlastkid(Ast*);
149 Name astlisp(Ast*);
150 Name astlong(Ast*);
151 Name asttostring(Ast*);
152 int astmerged(Ast*);
153 int astslotted(Ast*);
154 Ast* astdup(Ast*);
155 void astfree(Ast*);
156 void astrefcheck(Ast*, int);
157 Ast *astleak(Ast*);
158 Ast *astright(Ast*, int);
159 Zpoly* astline(Ast*);
160 Ast *astcanonicalize(Ast*);
162 Ast *astparent(Ast*, Type*);
163 Ast *astcvtslot(Ast*, Type*);
165 void astswap(Ast*, Ast*);
167 int astsame(Ast*, Ast*);
169 #define astparent_attr(a) astparent(a, 0)
170 Ast *astprev_attr(Ast*);
171 Ast *astnext_attr(Ast*);
172 Ast *astcopiedfrom_attr(Ast*);
173 int parseaction(CfgGram*, CfgRule*, ParseValue**, ParseValue**);
175 // Attributes on ASTs
177 typedef struct AttrInfo AttrInfo;
179 struct AttrInfo
181 Name name;
182 Type *type;
183 Zfn **fn;
184 Name pos;
185 int registered;
186 int ncompute;
189 typedef enum AttrState
191 AttrUninit = 0,
192 AttrComputing,
193 AttrInit
194 } AttrState;
195 struct Attr
197 AttrState state;
198 Zpoly data;
199 MapElem me;
201 void regattr(AttrInfo*);
202 void attrstats(void);
203 void attrloop(AttrInfo*);
205 void set_attribute(Ast*, AttrInfo*, Zpoly);
206 Attr *get_attribute(Ast*, AttrInfo*);
207 //Zpoly attribute(Ast*, AttrInfo*);
209 typedef struct AttrLink AttrLink;
210 struct AttrLink
212 AttrInfo *info;
213 Ast *ast;
214 AttrLink *next;
217 extern AttrLink *attrlinks;
220 extern int nmkast;
221 extern int nfreeast;
222 extern int nleakast;
224 extern int nattr;
225 extern AttrInfo **attrinfo;
227 #define GS(_name_) \
228 ({ \
229 static CfgSym *sym; \
230 if(!sym){ \
231 Name x = _name_; \
232 sym = gramlooksym(__tgram__->gram, x); \
233 if(sym == nil) \
234 panic("no symbol %s in %s", x, __tgram__->name); \
236 sym; \
239 #define GR(_name_) \
240 ({ \
241 static CfgRule *rule; \
242 if(!rule){ \
243 Name x = _name_; \
244 rule = gramlookrule(__tgram__->gram, x); \
245 if(rule == nil) \
246 panic("no rule %s in %s", x, __tgram__->name); \
248 rule; \
252 enum
254 LeftRecursive = 1