]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Classy Tokens
[xonotic/gmqcc.git] / lexer.h
1 #ifndef GMQCC_LEXER_HDR
2 #define GMQCC_LEXER_HDR
3 #include "gmqcc.h"
4
5 /* Lexer
6  *
7  */
8 enum Token : int { // todo: enum class
9     /* Other tokens which we can return: */
10     NONE = 0,
11
12     CR = '\r',
13     LF = '\n',
14     WS = ' ',
15     BACKSLASH = '\\',
16
17     HASH = '#',
18     DOLLAR = '$',
19
20     DOT = '.',
21     COMMA = ',',
22     COLON = ':',
23     SEMICOLON = ';',
24
25     AND = '&',
26     OR = '|',
27     XOR = '^',
28     BITNOT = '~',
29     NOT = '!',
30
31     LT = '<',
32     GT = '>',
33     EQ = '=',
34
35     MUL = '*',
36     DIV = '/',
37     MOD = '%',
38
39     ADD = '+',
40     SUB = '-',
41
42     QUOT_SINGLE = '\'',
43     QUOT_DOUBLE = '"',
44
45     QUESTION = '?',
46
47     BRACE_OPEN = '{', BRACE_CLOSE = '}',
48     BRACKET_OPEN = '[', BRACKET_CLOSE = ']',
49     PAREN_OPEN = '(', PAREN_CLOSE = ')',
50
51     START = 128,
52
53     IDENT,
54
55     TYPENAME,
56
57     OPERATOR,
58
59     KEYWORD, /* loop */
60
61     DOTS, /* 3 dots, ... */
62
63     ATTRIBUTE_OPEN,  /* [[ */
64     ATTRIBUTE_CLOSE, /* ]] */
65
66     VA_ARGS, /* for the ftepp only */
67     VA_ARGS_ARRAY, /* for the ftepp only */
68     VA_COUNT,     /* to get the count of vaargs */
69
70     STRINGCONST, /* not the typename but an actual "string" */
71     CHARCONST,
72     VECTORCONST,
73     INTCONST,
74     FLOATCONST,
75
76     WHITE,
77     EOL,
78
79     /* if we add additional tokens before this, the exposed API
80      * should not be broken anyway, but EOF/ERROR/... should
81      * still be at the bottom
82      */
83     END = 1024,
84
85     /* We use '< ERROR', so FATAL must come after it and any
86      * other error related tokens as well
87      */
88     ERROR,
89     FATAL /* internal error, eg out of memory */
90 };
91
92 struct token {
93     Token ttype;
94     char *value;
95     union {
96         vec3_t v;
97         int i;
98         qcfloat_t f;
99         qc_type t; /* type */
100     } constval;
101     lex_ctx_t ctx;
102 };
103
104 struct frame_macro {
105     char *name;
106     int value;
107 };
108
109 struct lex_file {
110     FILE  *file;
111     const char *open_string;
112     size_t      open_string_length;
113     size_t      open_string_pos;
114
115     char   *name;
116     size_t  line;
117     size_t  sline; /* line at the start of a token */
118     size_t  column;
119
120     Token   peek[256];
121     size_t  peekpos;
122
123     bool    eof;
124
125     token   tok; /* not a pointer anymore */
126
127     struct {
128         unsigned noops:1;
129         unsigned nodigraphs:1; /* used when lexing string constants */
130         unsigned preprocessing:1; /* whitespace and EOLs become actual tokens */
131         unsigned mergelines:1; /* backslash at the end of a line escapes the newline */
132     } flags; /* sizeof == 1 */
133
134     int framevalue;
135     frame_macro *frames;
136     char *modelname;
137
138     size_t push_line;
139 };
140
141 lex_file* lex_open (const char *file);
142 lex_file* lex_open_string(const char *str, size_t len, const char *name);
143 void      lex_close(lex_file   *lex);
144 Token     lex_do   (lex_file   *lex);
145 void      lex_cleanup(void);
146
147 /* Parser
148  *
149  */
150
151 enum {
152     ASSOC_LEFT,
153     ASSOC_RIGHT
154 };
155
156 #define OP_SUFFIX 1
157 #define OP_PREFIX 2
158
159 struct oper_info {
160     const char   *op;
161     unsigned int operands;
162     unsigned int id;
163     unsigned int assoc;
164     signed int   prec;
165     unsigned int flags;
166     bool         folds;
167 };
168
169 /*
170  * Explicit uint8_t casts since the left operand of shift operator cannot
171  * be negative, even though it won't happen, this supresses the future
172  * possibility.
173  */
174 #define opid1(a)     ((uint8_t)a)
175 #define opid2(a,b)   (((uint8_t)a<<8) |(uint8_t)b)
176 #define opid3(a,b,c) (((uint8_t)a<<16)|((uint8_t)b<<8)|(uint8_t)c)
177
178 static const oper_info c_operators[] = {
179     { "(",       0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
180     { "_length", 1, opid3('l','e','n'), ASSOC_RIGHT, 98, OP_PREFIX, true},
181
182     { "++",     1, opid3('S','+','+'), ASSOC_LEFT,  17, OP_SUFFIX, false},
183     { "--",     1, opid3('S','-','-'), ASSOC_LEFT,  17, OP_SUFFIX, false},
184     { ".",      2, opid1('.'),         ASSOC_LEFT,  17, 0,         false},
185     { "(",      0, opid1('('),         ASSOC_LEFT,  17, 0,         false}, /* function call */
186     { "[",      2, opid1('['),         ASSOC_LEFT,  17, 0,         false}, /* array subscript */
187
188     { "++",     1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
189     { "--",     1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
190
191     { "**",     2, opid2('*','*'),     ASSOC_RIGHT, 14, 0,         true},
192     { "!",      1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
193     { "~",      1, opid2('~','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
194     { "+",      1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
195     { "-",      1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
196 /*  { "&",      1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false}, */
197
198     { "*",      2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
199     { "/",      2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
200     { "%",      2, opid1('%'),         ASSOC_LEFT,  13, 0,         true},
201     { "><",     2, opid2('>','<'),     ASSOC_LEFT,  13, 0,         true},
202
203     { "+",      2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
204     { "-",      2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
205
206     { "<<",     2, opid2('<','<'),     ASSOC_LEFT,  11, 0,         true},
207     { ">>",     2, opid2('>','>'),     ASSOC_LEFT,  11, 0,         true},
208
209     { "<",      2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
210     { ">",      2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
211     { "<=>",    2, opid3('<','=','>'), ASSOC_LEFT,  10, 0,         true},
212     { "<=",     2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
213     { ">=",     2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
214
215     { "==",     2, opid2('=','='),     ASSOC_LEFT,  9,  0,         true},
216     { "!=",     2, opid2('!','='),     ASSOC_LEFT,  9,  0,         true},
217
218     { "&",      2, opid1('&'),         ASSOC_LEFT,  8,  0,         true},
219
220     { "^",      2, opid1('^'),         ASSOC_LEFT,  7,  0,         true},
221
222     { "|",      2, opid1('|'),         ASSOC_LEFT,  6,  0,         true},
223
224     { "&&",     2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
225
226     { "||",     2, opid2('|','|'),     ASSOC_LEFT,  4,  0,         true},
227
228     { "?",      3, opid2('?',':'),     ASSOC_RIGHT, 3,  0,         true},
229
230     { "=",      2, opid1('='),         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, opid2('/','='),     ASSOC_RIGHT, 2,  0,         false},
235     { "%=",     2, opid2('%','='),     ASSOC_RIGHT, 2,  0,         false},
236     { ">>=",    2, opid3('>','>','='), ASSOC_RIGHT, 2,  0,         false},
237     { "<<=",    2, opid3('<','<','='), ASSOC_RIGHT, 2,  0,         false},
238     { "&=",     2, opid2('&','='),     ASSOC_RIGHT, 2,  0,         false},
239     { "^=",     2, opid2('^','='),     ASSOC_RIGHT, 2,  0,         false},
240     { "|=",     2, opid2('|','='),     ASSOC_RIGHT, 2,  0,         false},
241
242     { ":",      0, opid2(':','?'),     ASSOC_RIGHT, 1,  0,         false},
243
244     { ",",      2, opid1(','),         ASSOC_LEFT,  0,  0,         false}
245 };
246
247 static const oper_info fte_operators[] = {
248     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
249
250     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  15, OP_SUFFIX, false},
251     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  15, OP_SUFFIX, false},
252     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0,         false},
253     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0,         false}, /* function call */
254     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0,         false}, /* array subscript */
255
256     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
257     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
258     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
259     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
260     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
261
262     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
263     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
264     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0,         true},
265     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0,         true},
266
267     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
268     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
269
270     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0,         true},
271     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0,         true},
272
273     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
274     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
275     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
276     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
277     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10, 0,         true},
278     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10, 0,         true},
279
280     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0,         true},
281
282     { "=",   2, opid1('='),         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, opid2('&','='),     ASSOC_RIGHT, 8,  0,         false},
289     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0,         false},
290     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8,  0,         false},
291
292     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
293     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0,         true},
294
295     /* Leave precedence 3 for : with -fcorrect-ternary */
296     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0,         false},
297     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0,         false}
298 };
299
300 static const oper_info qcc_operators[] = {
301     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
302
303     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0,         false},
304     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0,         false}, /* function call */
305     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0,         false}, /* array subscript */
306
307     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
308     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
309     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
310
311     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
312     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
313     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0,         true},
314     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0,         true},
315
316     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
317     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
318
319     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
320     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
321     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
322     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
323     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10, 0,         true},
324     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10, 0,         true},
325
326     { "=",   2, opid1('='),         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     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0,         false},
333     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0,         false},
334
335     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
336     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0,         true},
337
338     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0,         false},
339 };
340 extern const oper_info *operators;
341 extern size_t           operator_count;
342
343 #endif