1 #ifndef GMQCC_LEXER_HDR
2 #define GMQCC_LEXER_HDR
21 /* Other tokens which we can return: */
31 TOKEN_KEYWORD, /* loop */
33 TOKEN_DOTS, /* 3 dots, ... */
35 TOKEN_ATTRIBUTE_OPEN, /* [[ */
36 TOKEN_ATTRIBUTE_CLOSE, /* ]] */
38 TOKEN_VA_ARGS, /* for the ftepp only */
39 TOKEN_VA_ARGS_ARRAY, /* for the ftepp only */
40 TOKEN_VA_COUNT, /* to get the count of vaargs */
42 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
51 /* if we add additional tokens before this, the exposed API
52 * should not be broken anyway, but EOF/ERROR/... should
53 * still be at the bottom
57 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
58 * other error related tokens as well
61 TOKEN_FATAL /* internal error, eg out of memory */
71 const char *open_string;
72 size_t open_string_length;
73 size_t open_string_pos;
77 size_t sline; /* line at the start of a token */
85 token tok; /* not a pointer anymore */
89 unsigned nodigraphs:1; /* used when lexing string constants */
90 unsigned preprocessing:1; /* whitespace and EOLs become actual tokens */
91 unsigned mergelines:1; /* backslash at the end of a line escapes the newline */
92 } flags; /* sizeof == 1 */
101 lex_file* lex_open (const char *file);
102 lex_file* lex_open_string(const char *str, size_t len, const char *name);
103 void lex_close(lex_file *lex);
104 int lex_do (lex_file *lex);
105 void lex_cleanup(void);
121 unsigned int operands;
130 * Explicit uint8_t casts since the left operand of shift operator cannot
131 * be negative, even though it won't happen, this supresses the future
134 #define opid1(a) ((uint8_t)a)
135 #define opid2(a,b) (((uint8_t)a<<8) |(uint8_t)b)
136 #define opid3(a,b,c) (((uint8_t)a<<16)|((uint8_t)b<<8)|(uint8_t)c)
138 static const oper_info c_operators[] = {
139 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
140 { "_length", 1, opid3('l','e','n'), ASSOC_RIGHT, 98, OP_PREFIX, true},
142 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 17, OP_SUFFIX, false},
143 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 17, OP_SUFFIX, false},
144 { ".", 2, opid1('.'), ASSOC_LEFT, 17, 0, false},
145 { "(", 0, opid1('('), ASSOC_LEFT, 17, 0, false}, /* function call */
146 { "[", 2, opid1('['), ASSOC_LEFT, 17, 0, false}, /* array subscript */
148 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
149 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
151 { "**", 2, opid2('*','*'), ASSOC_RIGHT, 14, 0, true},
152 { "!", 1, opid2('!','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
153 { "~", 1, opid2('~','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
154 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
155 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
156 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX, false}, */
158 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0, true},
159 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0, true},
160 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0, true},
161 { "><", 2, opid2('>','<'), ASSOC_LEFT, 13, 0, true},
163 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
164 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
166 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0, true},
167 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0, true},
169 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0, false},
170 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0, false},
171 { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT, 10, 0, true},
172 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0, false},
173 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0, false},
175 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0, true},
176 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0, true},
178 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0, true},
180 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0, true},
182 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0, true},
184 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
186 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0, true},
188 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0, true},
190 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0, false},
191 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0, false},
192 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0, false},
193 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0, false},
194 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0, false},
195 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0, false},
196 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0, false},
197 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0, false},
198 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0, false},
199 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0, false},
200 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0, false},
202 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0, false},
204 { ",", 2, opid1(','), ASSOC_LEFT, 0, 0, false}
207 static const oper_info fte_operators[] = {
208 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
210 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 15, OP_SUFFIX, false},
211 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 15, OP_SUFFIX, false},
212 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0, false},
213 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0, false}, /* function call */
214 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0, false}, /* array subscript */
216 { "!", 1, opid2('!','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
217 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
218 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
219 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
220 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
222 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0, true},
223 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0, true},
224 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0, true},
225 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0, true},
227 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
228 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
230 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0, true},
231 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0, true},
233 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0, false},
234 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0, false},
235 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0, false},
236 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0, false},
237 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0, true},
238 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0, true},
240 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 9, 0, true},
242 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0, false},
243 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0, false},
244 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0, false},
245 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0, false},
246 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0, false},
247 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0, false},
248 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0, false},
249 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0, false},
250 { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8, 0, false},
252 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
253 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0, true},
255 /* Leave precedence 3 for : with -fcorrect-ternary */
256 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0, false},
257 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0, false}
260 static const oper_info qcc_operators[] = {
261 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
263 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0, false},
264 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0, false}, /* function call */
265 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0, false}, /* array subscript */
267 { "!", 1, opid2('!','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
268 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
269 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
271 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0, true},
272 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0, true},
273 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0, true},
274 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0, true},
276 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
277 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
279 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0, false},
280 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0, false},
281 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0, false},
282 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0, false},
283 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0, true},
284 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0, true},
286 { "=", 2, opid1('='), 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, opid2('/','='), ASSOC_RIGHT, 8, 0, false},
291 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0, false},
292 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0, false},
293 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0, false},
295 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
296 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0, true},
298 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0, false},
300 extern const oper_info *operators;
301 extern size_t operator_count;