1 #ifndef GMQCC_LEXER_HDR_
2 #define GMQCC_LEXER_HDR_
4 typedef struct token_s token;
11 MEM_VECTOR_MAKE(char, value);
27 void token_delete(token*);
28 token* token_copy(const token *cp);
29 void token_delete_all(token *t);
30 token* token_copy_all(const token *cp);
36 /* Other tokens which we can return: */
46 TOKEN_KEYWORD, /* loop */
48 TOKEN_STRINGCONST, /* not the typename but an actual "string" */
56 /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
57 * other error related tokens as well
60 TOKEN_FATAL /* internal error, eg out of memory */
63 static const char *_tokennames[] = {
79 _all_tokennames_added_[
80 ((TOKEN_FATAL - TOKEN_START + 1) ==
81 (sizeof(_tokennames)/sizeof(_tokennames[0])))
88 size_t sline; /* line at the start of a token */
100 MEM_VECTOR_PROTO(lex_file, char, token);
102 lex_file* lex_open (const char *file);
103 void lex_close(lex_file *lex);
104 int lex_do (lex_file *lex);
120 unsigned int operands;
128 #define opid2(a,b) ((a<<8)|b)
129 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
131 static const oper_info operators[] = {
132 { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 16, OP_SUFFIX},
133 { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 16, OP_SUFFIX},
135 { ".", 2, opid1('.'), ASSOC_LEFT, 15, 0 },
137 { "!", 1, opid2('!', 'P'), ASSOC_RIGHT, 14, 0 },
138 { "~", 1, opid2('~', 'P'), ASSOC_RIGHT, 14, 0 },
139 { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
140 { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
141 { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
142 { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
143 /* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX }, */
145 { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0 },
146 { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0 },
147 { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0 },
149 { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0 },
150 { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0 },
152 { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0 },
153 { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0 },
155 { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0 },
156 { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0 },
157 { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0 },
158 { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0 },
160 { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0 },
161 { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0 },
163 { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0 },
165 { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0 },
167 { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0 },
169 { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0 },
171 { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0 },
173 { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0 },
175 { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0 },
176 { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0 },
177 { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0 },
178 { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0 },
179 { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0 },
180 { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0 },
181 { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0 },
182 { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0 },
183 { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0 },
184 { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0 },
185 { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0 },
187 { ",", 2, opid1(','), ASSOC_LEFT, 1, 0 }
189 static const size_t operator_count = (sizeof(operators) / sizeof(operators[0]));
200 token *tok; /* current token */
202 MEM_VECTOR_MAKE(ast_value*, globals);
205 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
207 parse_file* parse_open(const char *file);
208 void parse_file_close(parse_file*);
210 bool parse(parse_file*);
212 bool parse_iskey(parse_file *self, const char *ident);
214 void lexerror(lex_file*, const char *fmt, ...);