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 */
72 static const char *_tokennames[] = {
91 _all_tokennames_added_[
92 ((TOKEN_FATAL - TOKEN_START + 1) ==
93 (sizeof(_tokennames)/sizeof(_tokennames[0])))
103 const char *open_string;
104 size_t open_string_length;
105 size_t open_string_pos;
109 size_t sline; /* line at the start of a token */
116 token tok; /* not a pointer anymore */
120 bool nodigraphs; /* used when lexing string constants */
121 bool preprocessing; /* whitespace and EOLs become actual tokens */
122 bool mergelines; /* backslash at the end of a line escapes the newline */
130 lex_file* lex_open (const char *file);
131 lex_file* lex_open_string(const char *str, size_t len, const char *name);
132 void lex_close(lex_file *lex);
133 int lex_do (lex_file *lex);
134 void lex_cleanup(void);
150 unsigned int operands;
158 #define opid2(a,b) ((a<<8)|b)
159 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
161 static const oper_info c_operators[] = {
162 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
164 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 16, OP_SUFFIX},
165 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 16, OP_SUFFIX},
167 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
168 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
169 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
171 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
172 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
173 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
174 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
175 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
176 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
177 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
179 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
180 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
181 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
183 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
184 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
186 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
187 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
189 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
190 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
191 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
192 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
194 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
195 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
197 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
199 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
201 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
203 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
205 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
207 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
209 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
210 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
211 { "-=", 2, opid2('-','='), 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, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
216 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
217 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
218 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
219 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
221 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
223 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
225 static const oper_info qcc_operators[] = {
226 { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX}, /* paren expression - non function call */
228 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
229 { "(", 0, opid1('('), ASSOC_LEFT, 15, 0 }, /* function call */
230 { "[", 2, opid1('['), ASSOC_LEFT, 15, 0 }, /* array subscript */
232 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, OP_PREFIX },
233 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
234 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
236 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
237 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
238 { "&", 2, opid1('&'), ASSOC_LEFT, 13, 0 },
239 { "|", 2, opid1('|'), ASSOC_LEFT, 13, 0 },
241 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
242 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
244 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
245 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
246 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
247 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
248 { "==", 2, opid2('=','='), ASSOC_LEFT, 10, 0 },
249 { "!=", 2, opid2('!','='), ASSOC_LEFT, 10, 0 },
251 { "=", 2, opid1('='), ASSOC_RIGHT, 8, 0 },
252 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 8, 0 },
253 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 8, 0 },
254 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 8, 0 },
255 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 8, 0 },
256 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 8, 0 },
257 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 8, 0 },
258 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 8, 0 },
260 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
261 { "||", 2, opid2('|','|'), ASSOC_LEFT, 5, 0 },
263 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
265 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
267 extern const oper_info *operators;
268 extern size_t operator_count;
269 void lexerror(lex_file*, const char *fmt, ...);