]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Implement subscripting for __VA_ARGS__, and added test-case. This only works on...
[xonotic/gmqcc.git] / lexer.h
1 /*
2  * Copyright (C) 2012, 2013
3  *     Wolfgang Bumiller
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #ifndef GMQCC_LEXER_HDR
24 #define GMQCC_LEXER_HDR
25
26 typedef struct token_s token;
27
28 struct token_s {
29     int ttype;
30
31     char *value;
32
33     union {
34         vector v;
35         int    i;
36         double f;
37         int    t; /* type */
38     } constval;
39
40 #if 0
41     struct token_s *next;
42     struct token_s *prev;
43 #endif
44
45     lex_ctx ctx;
46 };
47
48 #if 0
49 token* token_new();
50 void   token_delete(token*);
51 token* token_copy(const token *cp);
52 void   token_delete_all(token *t);
53 token* token_copy_all(const token *cp);
54 #endif
55
56 /* Lexer
57  *
58  */
59 enum {
60     /* Other tokens which we can return: */
61     TOKEN_NONE = 0,
62     TOKEN_START = 128,
63
64     TOKEN_IDENT,
65
66     TOKEN_TYPENAME,
67
68     TOKEN_OPERATOR,
69
70     TOKEN_KEYWORD, /* loop */
71
72     TOKEN_DOTS, /* 3 dots, ... */
73
74     TOKEN_ATTRIBUTE_OPEN,  /* [[ */
75     TOKEN_ATTRIBUTE_CLOSE, /* ]] */
76
77     TOKEN_VA_ARGS, /* for the ftepp only */
78     TOKEN_VA_ARGS_ARRAY, /* for the ftepp only */
79
80     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
81     TOKEN_CHARCONST,
82     TOKEN_VECTORCONST,
83     TOKEN_INTCONST,
84     TOKEN_FLOATCONST,
85
86     TOKEN_WHITE,
87     TOKEN_EOL,
88
89     TOKEN_EOF,
90
91     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
92      * other error related tokens as well
93      */
94     TOKEN_ERROR,
95     TOKEN_FATAL /* internal error, eg out of memory */
96 };
97
98 typedef struct {
99     char *name;
100     int   value;
101 } frame_macro;
102
103 typedef struct lex_file_s {
104     FILE   *file;
105     const char *open_string;
106     size_t      open_string_length;
107     size_t      open_string_pos;
108
109     char   *name;
110     size_t  line;
111     size_t  sline; /* line at the start of a token */
112
113     int     peek[256];
114     size_t  peekpos;
115
116     bool    eof;
117
118     token   tok; /* not a pointer anymore */
119
120     struct {
121         bool noops;
122         bool nodigraphs; /* used when lexing string constants */
123         bool preprocessing; /* whitespace and EOLs become actual tokens */
124         bool mergelines; /* backslash at the end of a line escapes the newline */
125     } flags;
126
127     int framevalue;
128     frame_macro *frames;
129     char *modelname;
130
131     size_t push_line;
132 } lex_file;
133
134 lex_file* lex_open (const char *file);
135 lex_file* lex_open_string(const char *str, size_t len, const char *name);
136 void      lex_close(lex_file   *lex);
137 int       lex_do   (lex_file   *lex);
138 void      lex_cleanup(void);
139
140 /* Parser
141  *
142  */
143
144 enum {
145     ASSOC_LEFT,
146     ASSOC_RIGHT
147 };
148
149 #define OP_SUFFIX 1
150 #define OP_PREFIX 2
151
152 typedef struct {
153     const char   *op;
154     unsigned int operands;
155     unsigned int id;
156     unsigned int assoc;
157     signed int   prec;
158     unsigned int flags;
159 } oper_info;
160
161 #define opid1(a) (a)
162 #define opid2(a,b) ((a<<8)|b)
163 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
164
165 static const oper_info c_operators[] = {
166     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
167
168     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  15, OP_SUFFIX},
169     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  15, OP_SUFFIX},
170     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
171     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
172     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
173
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, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
178     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
179     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
180 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
181
182     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
183     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
184     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
185
186     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
187     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
188
189     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
190     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
191
192     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
193     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
194     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
195     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
196
197     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
198     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
199
200     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
201
202     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
203
204     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
205
206     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
207
208     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
209
210     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
211
212     { "=",   2, opid1('='),         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, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
218     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
219     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
220     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0 },
221     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0 },
222     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0 },
223     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 2,  0 },
224
225     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0 },
226
227     { ",",   2, opid1(','),         ASSOC_LEFT,  0,  0 }
228 };
229 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
230
231 static const oper_info fte_operators[] = {
232     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
233
234     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  15, OP_SUFFIX},
235     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  15, OP_SUFFIX},
236     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
237     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
238     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
239
240     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
241     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
242     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
243     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
244     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
245
246     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
247     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
248     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0 },
249     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0 },
250
251     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
252     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
253
254     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
255     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
256
257     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
258     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
259     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
260     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
261     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10,  0 },
262     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
263
264     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0 },
265
266     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
267     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
268     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0 },
269     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0 },
270     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0 },
271     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0 },
272     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0 },
273     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0 },
274     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8,  0 },
275
276     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
277     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
278
279     /* Leave precedence 3 for : with -fcorrect-ternary */
280     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0 },
281     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0 }
282 };
283 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
284
285 static const oper_info qcc_operators[] = {
286     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
287
288     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
289     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
290     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
291
292     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
293     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
294     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
295
296     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
297     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
298     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0 },
299     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0 },
300
301     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
302     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
303
304     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
305     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
306     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
307     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
308     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10,  0 },
309     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
310
311     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
312     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
313     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0 },
314     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0 },
315     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0 },
316     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0 },
317     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0 },
318     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0 },
319
320     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
321     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
322
323     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0 },
324 };
325 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
326
327 extern const oper_info *operators;
328 extern size_t           operator_count;
329 void lexerror(lex_file*, const char *fmt, ...);
330
331 #endif