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
26 typedef struct token_s token;
52 void token_delete(token*);
53 token* token_copy(const token *cp);
54 void token_delete_all(token *t);
55 token* token_copy_all(const token *cp);
62 /* Other tokens which we can return: */
72 TOKEN_KEYWORD, /* loop */
74 TOKEN_DOTS, /* 3 dots, ... */
76 TOKEN_ATTRIBUTE_OPEN, /* [[ */
77 TOKEN_ATTRIBUTE_CLOSE, /* ]] */
79 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
90 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
91 * other error related tokens as well
94 TOKEN_FATAL /* internal error, eg out of memory */
104 const char *open_string;
105 size_t open_string_length;
106 size_t open_string_pos;
110 size_t sline; /* line at the start of a token */
117 token tok; /* not a pointer anymore */
121 bool nodigraphs; /* used when lexing string constants */
122 bool preprocessing; /* whitespace and EOLs become actual tokens */
123 bool mergelines; /* backslash at the end of a line escapes the newline */
133 lex_file* lex_open (const char *file);
134 lex_file* lex_open_string(const char *str, size_t len, const char *name);
135 void lex_close(lex_file *lex);
136 int lex_do (lex_file *lex);
137 void lex_cleanup(void);
153 unsigned int operands;
161 #define opid2(a,b) ((a<<8)|b)
162 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
164 static const oper_info c_operators[] = {
165 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
167 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 15, OP_SUFFIX},
168 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 15, OP_SUFFIX},
169 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
170 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
171 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
173 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
174 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
175 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
176 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
177 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
178 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
179 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
181 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
182 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
183 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
185 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
186 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
188 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
189 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
191 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
192 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
193 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
194 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
196 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
197 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
199 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
201 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
203 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
205 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
207 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
209 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
211 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
212 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
213 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0 },
214 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0 },
215 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0 },
216 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0 },
217 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
218 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
219 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
220 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
221 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
223 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0 },
225 { ",", 2, opid1(','), ASSOC_LEFT, 0, 0 }
227 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
229 static const oper_info fte_operators[] = {
230 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
232 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 15, OP_SUFFIX},
233 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 15, OP_SUFFIX},
234 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
235 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
236 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
238 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
239 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
240 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
241 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
242 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
244 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
245 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
246 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
247 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
249 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
250 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
252 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
253 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
254 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
255 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
256 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
257 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
259 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 9, 0 },
261 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
262 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
263 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
264 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
265 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
266 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
267 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
268 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
269 { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8, 0 },
271 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
272 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
274 /* Leave precedence 3 for : with -fcorrect-ternary */
275 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0 },
276 { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0 }
278 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
280 static const oper_info qcc_operators[] = {
281 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
283 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
284 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
285 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
287 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
288 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
289 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
291 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
292 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
293 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
294 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
296 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
297 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
299 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
300 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
301 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
302 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
303 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
304 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
306 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
307 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
308 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
309 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
310 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
311 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
312 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
313 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
315 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
316 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
318 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0 },
320 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
322 extern const oper_info *operators;
323 extern size_t operator_count;
324 void lexerror(lex_file*, const char *fmt, ...);