]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Revert that trans stuff
[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     TOKEN_VA_COUNT,     /* to get the count of vaargs */
80
81     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
82     TOKEN_CHARCONST,
83     TOKEN_VECTORCONST,
84     TOKEN_INTCONST,
85     TOKEN_FLOATCONST,
86
87     TOKEN_WHITE,
88     TOKEN_EOL,
89
90     TOKEN_EOF,
91
92     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
93      * other error related tokens as well
94      */
95     TOKEN_ERROR,
96     TOKEN_FATAL /* internal error, eg out of memory */
97 };
98
99 typedef struct {
100     char *name;
101     int   value;
102 } frame_macro;
103
104 typedef struct lex_file_s {
105     FILE   *file;
106     const char *open_string;
107     size_t      open_string_length;
108     size_t      open_string_pos;
109
110     char   *name;
111     size_t  line;
112     size_t  sline; /* line at the start of a token */
113
114     int     peek[256];
115     size_t  peekpos;
116
117     bool    eof;
118
119     token   tok; /* not a pointer anymore */
120
121     struct {
122         bool noops;
123         bool nodigraphs; /* used when lexing string constants */
124         bool preprocessing; /* whitespace and EOLs become actual tokens */
125         bool mergelines; /* backslash at the end of a line escapes the newline */
126     } flags;
127
128     int framevalue;
129     frame_macro *frames;
130     char *modelname;
131
132     size_t push_line;
133 } lex_file;
134
135 lex_file* lex_open (const char *file);
136 lex_file* lex_open_string(const char *str, size_t len, const char *name);
137 void      lex_close(lex_file   *lex);
138 int       lex_do   (lex_file   *lex);
139 void      lex_cleanup(void);
140
141 /* Parser
142  *
143  */
144
145 enum {
146     ASSOC_LEFT,
147     ASSOC_RIGHT
148 };
149
150 #define OP_SUFFIX 1
151 #define OP_PREFIX 2
152
153 typedef struct {
154     const char   *op;
155     unsigned int operands;
156     unsigned int id;
157     unsigned int assoc;
158     signed int   prec;
159     unsigned int flags;
160 } oper_info;
161
162 #define opid1(a) (a)
163 #define opid2(a,b) ((a<<8)|b)
164 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
165
166 static const oper_info c_operators[] = {
167     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
168
169     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  17, OP_SUFFIX},
170     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  17, OP_SUFFIX},
171     { ".",   2, opid1('.'),         ASSOC_LEFT,  17, 0 },
172     { "(",   0, opid1('('),         ASSOC_LEFT,  17, 0 }, /* function call */
173     { "[",   2, opid1('['),         ASSOC_LEFT,  17, 0 }, /* array subscript */
174
175     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX },
176     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX },
177
178     { "**",  2, opid2('*', '*'),    ASSOC_RIGHT, 15, 0 },
179
180     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
181     { "~",   1, opid2('~', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
182     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
183     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
184 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
185
186     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
187     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
188     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
189
190     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
191     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
192
193     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
194     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
195
196     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
197     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
198     { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT,  10, 0 },
199     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
200     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
201
202     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
203     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
204
205     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
206
207     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
208
209     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
210
211     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
212
213     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
214
215     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
216
217     { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0 },
218     { "+=",  2, opid2('+','='),     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 },
222     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
223     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
224     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
225     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0 },
226     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0 },
227     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0 },
228     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 2,  0 },
229
230     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0 },
231
232     { ",",   2, opid1(','),         ASSOC_LEFT,  0,  0 }
233 };
234 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
235
236 static const oper_info fte_operators[] = {
237     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
238
239     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  15, OP_SUFFIX},
240     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  15, OP_SUFFIX},
241     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
242     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
243     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
244
245     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
246     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
247     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
248     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
249     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
250
251     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
252     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
253     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0 },
254     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0 },
255
256     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
257     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
258
259     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
260     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
261
262     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
263     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
264     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
265     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
266     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10,  0 },
267     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
268
269     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0 },
270
271     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
272     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
273     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0 },
274     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0 },
275     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0 },
276     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0 },
277     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0 },
278     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0 },
279     { "&~=", 2, opid3('&','~','='), ASSOC_RIGHT, 8,  0 },
280
281     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
282     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
283
284     /* Leave precedence 3 for : with -fcorrect-ternary */
285     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0 },
286     { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 1,  0 }
287 };
288 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
289
290 static const oper_info qcc_operators[] = {
291     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
292
293     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
294     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
295     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
296
297     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
298     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
299     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
300
301     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
302     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
303     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0 },
304     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0 },
305
306     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
307     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
308
309     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
310     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
311     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
312     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
313     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10,  0 },
314     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
315
316     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
317     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
318     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0 },
319     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0 },
320     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0 },
321     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0 },
322     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0 },
323     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0 },
324
325     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
326     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
327
328     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0 },
329 };
330 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
331
332 extern const oper_info *operators;
333 extern size_t           operator_count;
334 void lexerror(lex_file*, const char *fmt, ...);
335
336 #endif