1 #ifndef GMQCC_LEXER_HDR_
2 #define GMQCC_LEXER_HDR_
4 typedef struct token_s token;
30 void token_delete(token*);
31 token* token_copy(const token *cp);
32 void token_delete_all(token *t);
33 token* token_copy_all(const token *cp);
40 /* Other tokens which we can return: */
50 TOKEN_KEYWORD, /* loop */
52 TOKEN_DOTS, /* 3 dots, ... */
54 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
65 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
66 * other error related tokens as well
69 TOKEN_FATAL /* internal error, eg out of memory */
79 const char *open_string;
80 size_t open_string_length;
81 size_t open_string_pos;
85 size_t sline; /* line at the start of a token */
92 token tok; /* not a pointer anymore */
96 bool nodigraphs; /* used when lexing string constants */
97 bool preprocessing; /* whitespace and EOLs become actual tokens */
98 bool mergelines; /* backslash at the end of a line escapes the newline */
108 lex_file* lex_open (const char *file);
109 lex_file* lex_open_string(const char *str, size_t len, const char *name);
110 void lex_close(lex_file *lex);
111 int lex_do (lex_file *lex);
112 void lex_cleanup(void);
128 unsigned int operands;
136 #define opid2(a,b) ((a<<8)|b)
137 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
139 static const oper_info c_operators[] = {
140 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
142 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 15, OP_SUFFIX},
143 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 15, OP_SUFFIX},
144 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
145 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
146 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
148 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
149 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
150 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
151 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
152 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
153 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
154 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
156 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
157 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
158 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
160 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
161 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
163 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
164 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
166 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
167 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
168 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
169 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
171 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
172 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
174 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
176 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
178 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
180 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
182 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
184 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
185 { ":", 3, opid2(':','?'), ASSOC_RIGHT, 3, 0 },
187 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
188 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
189 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0 },
190 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0 },
191 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0 },
192 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0 },
193 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
194 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
195 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
196 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
197 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
199 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
201 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
203 static const oper_info fte_operators[] = {
204 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
206 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 15, OP_SUFFIX},
207 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 15, OP_SUFFIX},
208 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
209 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
210 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
212 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
213 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
214 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
215 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
216 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
218 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
219 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
220 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
221 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
223 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
224 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
226 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
227 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
228 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
229 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
230 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
231 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
233 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
234 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
235 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
236 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
237 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
238 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
239 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
240 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
241 { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8, 0 },
243 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
244 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
246 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0 },
248 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 1, 0 },
249 { ":", 3, opid2(':','?'), ASSOC_RIGHT, 1, 0 }
251 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
253 static const oper_info qcc_operators[] = {
254 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
256 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
257 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
258 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
260 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
261 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
262 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
264 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
265 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
266 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
267 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
269 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
270 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
272 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
273 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
274 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
275 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
276 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
277 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
279 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
280 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
281 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
282 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
283 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
284 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
285 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
286 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
288 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
289 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
291 { ",", 2, opid1(','), ASSOC_LEFT, 2, 0 },
293 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 1, 0 },
294 { ":", 3, opid2(':','?'), ASSOC_RIGHT, 1, 0 }
296 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
298 extern const oper_info *operators;
299 extern size_t operator_count;
300 void lexerror(lex_file*, const char *fmt, ...);