2 * Copyright (C) 2012, 2013, 2014, 2015
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
23 #ifndef GMQCC_LEXER_HDR
24 #define GMQCC_LEXER_HDR
27 typedef struct token_s token;
48 /* Other tokens which we can return: */
58 TOKEN_KEYWORD, /* loop */
60 TOKEN_DOTS, /* 3 dots, ... */
62 TOKEN_ATTRIBUTE_OPEN, /* [[ */
63 TOKEN_ATTRIBUTE_CLOSE, /* ]] */
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 */
69 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
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
84 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
85 * other error related tokens as well
88 TOKEN_FATAL /* internal error, eg out of memory */
96 typedef struct lex_file_s {
98 const char *open_string;
99 size_t open_string_length;
100 size_t open_string_pos;
104 size_t sline; /* line at the start of a token */
112 token tok; /* not a pointer anymore */
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 */
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);
148 unsigned int operands;
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
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)
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},
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 */
175 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
176 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
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}, */
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},
190 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
191 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
193 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0, true},
194 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0, true},
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},
202 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0, true},
203 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0, true},
205 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0, true},
207 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0, true},
209 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0, true},
211 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
213 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0, true},
215 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0, true},
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},
229 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0, false},
231 { ",", 2, opid1(','), ASSOC_LEFT, 0, 0, false}
234 static const oper_info fte_operators[] = {
235 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
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 */
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},
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},
254 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
255 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
257 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0, true},
258 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0, true},
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},
267 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 9, 0, true},
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},
279 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
280 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0, true},
282 /* Leave precedence 3 for : with -fcorrect-ternary */
283 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0, false},
284 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0, false}
287 static const oper_info qcc_operators[] = {
288 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
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 */
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},
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},
303 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
304 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
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},
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},
322 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
323 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0, true},
325 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0, false},
327 extern const oper_info *operators;
328 extern size_t operator_count;