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