]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Merge branch 'cooking' of github.com:graphitemaster/gmqcc into cooking
[xonotic/gmqcc.git] / lexer.h
1 /*
2  * Copyright (C) 2012, 2013
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef GMQCC_LEXER_HDR
24 #define GMQCC_LEXER_HDR
25 typedef struct token_s token;
26
27 struct token_s {
28     int ttype;
29
30     char *value;
31
32     union {
33         vec3_t v;
34         int    i;
35         double f;
36         int    t; /* type */
37     } constval;
38
39 #if 0
40     struct token_s *next;
41     struct token_s *prev;
42 #endif
43
44     lex_ctx_t ctx;
45 };
46
47 #if 0
48 token* token_new();
49 void   token_delete(token*);
50 token* token_copy(const token *cp);
51 void   token_delete_all(token *t);
52 token* token_copy_all(const token *cp);
53 #endif
54
55 /* Lexer
56  *
57  */
58 enum {
59     /* Other tokens which we can return: */
60     TOKEN_NONE = 0,
61     TOKEN_START = 128,
62
63     TOKEN_IDENT,
64
65     TOKEN_TYPENAME,
66
67     TOKEN_OPERATOR,
68
69     TOKEN_KEYWORD, /* loop */
70
71     TOKEN_DOTS, /* 3 dots, ... */
72
73     TOKEN_ATTRIBUTE_OPEN,  /* [[ */
74     TOKEN_ATTRIBUTE_CLOSE, /* ]] */
75
76     TOKEN_VA_ARGS, /* for the ftepp only */
77     TOKEN_VA_ARGS_ARRAY, /* for the ftepp only */
78     TOKEN_VA_COUNT,     /* to get the count of vaargs */
79
80     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
81     TOKEN_CHARCONST,
82     TOKEN_VECTORCONST,
83     TOKEN_INTCONST,
84     TOKEN_FLOATCONST,
85
86     TOKEN_WHITE,
87     TOKEN_EOL,
88
89     /* if we add additional tokens before this, the exposed API
90      * should not be broken anyway, but EOF/ERROR/... should
91      * still be at the bottom
92      */
93     TOKEN_EOF = 1024,
94
95     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
96      * other error related tokens as well
97      */
98     TOKEN_ERROR,
99     TOKEN_FATAL /* internal error, eg out of memory */
100 };
101
102 typedef struct {
103     char *name;
104     int   value;
105 } frame_macro;
106
107 typedef struct lex_file_s {
108     FILE   *file;
109     const char *open_string;
110     size_t      open_string_length;
111     size_t      open_string_pos;
112
113     char   *name;
114     size_t  line;
115     size_t  sline; /* line at the start of a token */
116     size_t  column;
117
118     int     peek[256];
119     size_t  peekpos;
120
121     bool    eof;
122
123     token   tok; /* not a pointer anymore */
124
125     struct {
126         unsigned noops:1;
127         unsigned nodigraphs:1; /* used when lexing string constants */
128         unsigned preprocessing:1; /* whitespace and EOLs become actual tokens */
129         unsigned mergelines:1; /* backslash at the end of a line escapes the newline */
130     } flags; /* sizeof == 1 */
131
132     int framevalue;
133     frame_macro *frames;
134     char *modelname;
135
136     size_t push_line;
137 } lex_file;
138
139 lex_file* lex_open (const char *file);
140 lex_file* lex_open_string(const char *str, size_t len, const char *name);
141 void      lex_close(lex_file   *lex);
142 int       lex_do   (lex_file   *lex);
143 void      lex_cleanup(void);
144
145 /* Parser
146  *
147  */
148
149 enum {
150     ASSOC_LEFT,
151     ASSOC_RIGHT
152 };
153
154 #define OP_SUFFIX 1
155 #define OP_PREFIX 2
156
157 typedef struct {
158     const char   *op;
159     unsigned int operands;
160     unsigned int id;
161     unsigned int assoc;
162     signed int   prec;
163     unsigned int flags;
164     bool         folds;
165 } oper_info;
166
167 /*
168  * Explicit uint8_t casts since the left operand of shift operator cannot
169  * be negative, even though it won't happen, this supresses the future
170  * possibility.
171  */
172 #define opid1(a)     ((uint8_t)a)
173 #define opid2(a,b)   (((uint8_t)a<<8) |(uint8_t)b)
174 #define opid3(a,b,c) (((uint8_t)a<<16)|((uint8_t)b<<8)|(uint8_t)c)
175
176 static const oper_info c_operators[] = {
177     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
178
179     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  17, OP_SUFFIX, false},
180     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  17, OP_SUFFIX, false},
181     { ".",   2, opid1('.'),         ASSOC_LEFT,  17, 0,         false},
182     { "(",   0, opid1('('),         ASSOC_LEFT,  17, 0,         false}, /* function call */
183     { "[",   2, opid1('['),         ASSOC_LEFT,  17, 0,         false}, /* array subscript */
184
185     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
186     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
187
188     { "**",  2, opid2('*','*'),     ASSOC_RIGHT, 15, 0,         true},
189
190     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
191     { "~",   1, opid2('~','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
192     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
193     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
194 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false}, */
195
196     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
197     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
198     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0,         true},
199     { "><",  2, opid2('>','<'),     ASSOC_LEFT,  13, 0,         true},
200
201     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
202     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
203
204     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0,         true},
205     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0,         true},
206
207     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
208     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
209     { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT,  10, 0,         true},
210     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
211     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
212
213     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0,         true},
214     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0,         true},
215
216     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0,         true},
217
218     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0,         true},
219
220     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0,         true},
221
222     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
223
224     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0,         true},
225
226     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0,         true},
227
228     { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0,         false},
229     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 2,  0,         false},
230     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 2,  0,         false},
231     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 2,  0,         false},
232     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 2,  0,         false},
233     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 2,  0,         false},
234     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0,         false},
235     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0,         false},
236     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0,         false},
237     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0,         false},
238     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0,         false},
239
240     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0,         false},
241
242     { ",",   2, opid1(','),         ASSOC_LEFT,  0,  0,         false}
243 };
244
245 static const oper_info fte_operators[] = {
246     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
247
248     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  15, OP_SUFFIX, false},
249     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  15, OP_SUFFIX, false},
250     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0,         false},
251     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0,         false}, /* function call */
252     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0,         false}, /* array subscript */
253
254     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
255     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
256     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
257     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
258     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
259
260     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
261     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
262     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0,         true},
263     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0,         true},
264
265     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
266     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
267
268     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0,         true},
269     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0,         true},
270
271     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
272     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
273     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
274     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
275     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10, 0,         true},
276     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10, 0,         true},
277
278     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0,         true},
279
280     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0,         false},
281     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0,         false},
282     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0,         false},
283     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0,         false},
284     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0,         false},
285     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0,         false},
286     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0,         false},
287     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0,         false},
288     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8,  0,         false},
289
290     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
291     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0,         true},
292
293     /* Leave precedence 3 for : with -fcorrect-ternary */
294     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0,         false},
295     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0,         false}
296 };
297
298 static const oper_info qcc_operators[] = {
299     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
300
301     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0,         false},
302     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0,         false}, /* function call */
303     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0,         false}, /* array subscript */
304
305     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
306     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
307     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
308
309     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
310     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
311     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0,         true},
312     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0,         true},
313
314     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
315     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
316
317     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
318     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
319     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
320     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
321     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10, 0,         true},
322     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10, 0,         true},
323
324     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0,         false},
325     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0,         false},
326     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0,         false},
327     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0,         false},
328     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0,         false},
329     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0,         false},
330     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0,         false},
331     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0,         false},
332
333     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
334     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0,         true},
335
336     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0,         false},
337 };
338 extern const oper_info *operators;
339 extern size_t           operator_count;
340
341 #endif