]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Only increment the buffer location for macro output whitespace stripping if the situa...
[xonotic/gmqcc.git] / lexer.h
1 /*
2  * Copyright (C) 2012, 2013, 2014
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 #if 0
42     struct token_s *next;
43     struct token_s *prev;
44 #endif
45
46     lex_ctx_t ctx;
47 };
48
49 #if 0
50 token* token_new();
51 void   token_delete(token*);
52 token* token_copy(const token *cp);
53 void   token_delete_all(token *t);
54 token* token_copy_all(const token *cp);
55 #endif
56
57 /* Lexer
58  *
59  */
60 enum {
61     /* Other tokens which we can return: */
62     TOKEN_NONE = 0,
63     TOKEN_START = 128,
64
65     TOKEN_IDENT,
66
67     TOKEN_TYPENAME,
68
69     TOKEN_OPERATOR,
70
71     TOKEN_KEYWORD, /* loop */
72
73     TOKEN_DOTS, /* 3 dots, ... */
74
75     TOKEN_ATTRIBUTE_OPEN,  /* [[ */
76     TOKEN_ATTRIBUTE_CLOSE, /* ]] */
77
78     TOKEN_VA_ARGS, /* for the ftepp only */
79     TOKEN_VA_ARGS_ARRAY, /* for the ftepp only */
80     TOKEN_VA_COUNT,     /* to get the count of vaargs */
81
82     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
83     TOKEN_CHARCONST,
84     TOKEN_VECTORCONST,
85     TOKEN_INTCONST,
86     TOKEN_FLOATCONST,
87
88     TOKEN_WHITE,
89     TOKEN_EOL,
90
91     /* if we add additional tokens before this, the exposed API
92      * should not be broken anyway, but EOF/ERROR/... should
93      * still be at the bottom
94      */
95     TOKEN_EOF = 1024,
96
97     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
98      * other error related tokens as well
99      */
100     TOKEN_ERROR,
101     TOKEN_FATAL /* internal error, eg out of memory */
102 };
103
104 typedef struct {
105     char *name;
106     int   value;
107 } frame_macro;
108
109 typedef struct lex_file_s {
110     fs_file_t  *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     int     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 } lex_file;
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 int       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 typedef struct {
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 } oper_info;
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
181     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  17, OP_SUFFIX, false},
182     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  17, OP_SUFFIX, false},
183     { ".",   2, opid1('.'),         ASSOC_LEFT,  17, 0,         false},
184     { "(",   0, opid1('('),         ASSOC_LEFT,  17, 0,         false}, /* function call */
185     { "[",   2, opid1('['),         ASSOC_LEFT,  17, 0,         false}, /* array subscript */
186
187     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
188     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
189
190     { "**",  2, opid2('*','*'),     ASSOC_RIGHT, 14, 0,         true},
191     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
192     { "~",   1, opid2('~','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
193     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
194     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
195 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false}, */
196
197     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0,         true},
198     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0,         true},
199     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0,         true},
200     { "><",  2, opid2('>','<'),     ASSOC_LEFT,  13, 0,         true},
201
202     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
203     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
204
205     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0,         true},
206     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0,         true},
207
208     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
209     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
210     { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT,  10, 0,         true},
211     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
212     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
213
214     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0,         true},
215     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0,         true},
216
217     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0,         true},
218
219     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0,         true},
220
221     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0,         true},
222
223     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
224
225     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0,         true},
226
227     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0,         true},
228
229     { "=",   2, opid1('='),         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, opid2('%','='),     ASSOC_RIGHT, 2,  0,         false},
235     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0,         false},
236     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0,         false},
237     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0,         false},
238     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0,         false},
239     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0,         false},
240
241     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0,         false},
242
243     { ",",   2, opid1(','),         ASSOC_LEFT,  0,  0,         false}
244 };
245
246 static const oper_info fte_operators[] = {
247     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
248
249     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  15, OP_SUFFIX, false},
250     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  15, OP_SUFFIX, false},
251     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0,         false},
252     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0,         false}, /* function call */
253     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0,         false}, /* array subscript */
254
255     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
256     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
257     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
258     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
259     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
260
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     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0,         true},
265
266     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
267     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
268
269     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0,         true},
270     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0,         true},
271
272     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
273     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
274     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
275     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
276     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10, 0,         true},
277     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10, 0,         true},
278
279     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0,         true},
280
281     { "=",   2, opid1('='),         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, opid2('|','='),     ASSOC_RIGHT, 8,  0,         false},
289     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8,  0,         false},
290
291     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
292     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0,         true},
293
294     /* Leave precedence 3 for : with -fcorrect-ternary */
295     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0,         false},
296     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0,         false}
297 };
298
299 static const oper_info qcc_operators[] = {
300     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX, false}, /* paren expression - non function call */
301
302     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0,         false},
303     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0,         false}, /* function call */
304     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0,         false}, /* array subscript */
305
306     { "!",   1, opid2('!','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
307     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX, false},
308     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX, true},
309
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     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0,         true},
314
315     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0,         true},
316     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0,         true},
317
318     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0,         false},
319     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0,         false},
320     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0,         false},
321     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0,         false},
322     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10, 0,         true},
323     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10, 0,         true},
324
325     { "=",   2, opid1('='),         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     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0,         false},
333
334     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0,         true},
335     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0,         true},
336
337     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0,         false},
338 };
339 extern const oper_info *operators;
340 extern size_t           operator_count;
341
342 #endif