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