2 * Copyright (C) 2012, 2013
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
25 typedef struct token_s token;
49 void token_delete(token*);
50 token* token_copy(const token *cp);
51 void token_delete_all(token *t);
52 token* token_copy_all(const token *cp);
59 /* Other tokens which we can return: */
69 TOKEN_KEYWORD, /* loop */
71 TOKEN_DOTS, /* 3 dots, ... */
73 TOKEN_ATTRIBUTE_OPEN, /* [[ */
74 TOKEN_ATTRIBUTE_CLOSE, /* ]] */
76 TOKEN_VA_ARGS, /* for the ftepp only */
77 TOKEN_VA_ARGS_ARRAY, /* for the ftepp only */
78 TOKEN_VA_COUNT, /* to get the count of vaargs */
80 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
89 /* if we add additional tokens before this, the exposed API
90 * should not be broken anyway, but EOF/ERROR/... should
91 * still be at the bottom
95 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
96 * other error related tokens as well
99 TOKEN_FATAL /* internal error, eg out of memory */
107 typedef struct lex_file_s {
109 const char *open_string;
110 size_t open_string_length;
111 size_t open_string_pos;
113 size_t sline; /* line at the start of a token */
116 * no more 'shallow' copies, instead all new instances
117 * of a lex_ctx will just point to this lex_file::ctx.
118 * i.e 4/8 byte pointer. Instead of every token getting
119 * a 'shallow' 12/24 byte structure.
128 token tok; /* not a pointer anymore */
132 bool nodigraphs; /* used when lexing string constants */
133 bool preprocessing; /* whitespace and EOLs become actual tokens */
134 bool mergelines; /* backslash at the end of a line escapes the newline */
144 lex_file* lex_open (const char *file);
145 lex_file* lex_open_string(const char *str, size_t len, const char *name);
146 void lex_close(lex_file *lex);
147 int lex_do (lex_file *lex);
148 void lex_cleanup(void);
164 unsigned int operands;
172 * Explicit uint8_t casts since the left operand of shift operator cannot
173 * be negative, even though it won't happen, this supresses the future
176 #define opid1(a) ((uint8_t)a)
177 #define opid2(a,b) (((uint8_t)a<<8) |(uint8_t)b)
178 #define opid3(a,b,c) (((uint8_t)a<<16)|((uint8_t)b<<8)|(uint8_t)c)
180 static const oper_info c_operators[] = {
181 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
183 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 17, OP_SUFFIX},
184 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 17, OP_SUFFIX},
185 { ".", 2, opid1('.'), ASSOC_LEFT, 17, 0 },
186 { "(", 0, opid1('('), ASSOC_LEFT, 17, 0 }, /* function call */
187 { "[", 2, opid1('['), ASSOC_LEFT, 17, 0 }, /* array subscript */
189 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX },
190 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX },
192 { "**", 2, opid2('*', '*'), ASSOC_RIGHT, 15, 0 },
194 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
195 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
196 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
197 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
198 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
200 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
201 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
202 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
204 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
205 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
207 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
208 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
210 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
211 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
212 { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT, 10, 0 },
213 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
214 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
216 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
217 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
219 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
221 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
223 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
225 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
227 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
229 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
231 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
232 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
233 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0 },
234 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0 },
235 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0 },
236 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0 },
237 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
238 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
239 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
240 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
241 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
242 { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 2, 0 },
244 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0 },
246 { ",", 2, opid1(','), ASSOC_LEFT, 0, 0 }
248 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
250 static const oper_info fte_operators[] = {
251 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
253 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 15, OP_SUFFIX},
254 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 15, OP_SUFFIX},
255 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
256 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
257 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
259 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
260 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
261 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
262 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
263 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
265 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
266 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
267 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
268 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
270 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
271 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
273 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
274 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
276 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
277 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
278 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
279 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
280 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
281 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
283 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 9, 0 },
285 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
286 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
287 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
288 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
289 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
290 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
291 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
292 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
293 { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8, 0 },
295 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
296 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
298 /* Leave precedence 3 for : with -fcorrect-ternary */
299 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0 },
300 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0 }
302 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
304 static const oper_info qcc_operators[] = {
305 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
307 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
308 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
309 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
311 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
312 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
313 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
315 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
316 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
317 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
318 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
320 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
321 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
323 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
324 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
325 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
326 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
327 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
328 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
330 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
331 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
332 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
333 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
334 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
335 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
336 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
337 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
339 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
340 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
342 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0 },
344 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
346 extern const oper_info *operators;
347 extern size_t operator_count;
348 /*void lexerror(lex_file*, const char *fmt, ...);*/