]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Merging in new-syntax
[xonotic/gmqcc.git] / lexer.h
1 #ifndef GMQCC_LEXER_HDR_
2 #define GMQCC_LEXER_HDR_
3
4 typedef struct token_s token;
5
6 #include "ast.h"
7
8 struct token_s {
9         int ttype;
10
11         MEM_VECTOR_MAKE(char, value);
12
13         union {
14                 vector v;
15                 int    i;
16                 double f;
17                 int    t; /* type */
18         } constval;
19
20 #if 0
21         struct token_s *next;
22         struct token_s *prev;
23 #endif
24
25         lex_ctx ctx;
26 };
27
28 #if 0
29 token* token_new();
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);
34 #endif
35
36 /* Lexer
37  *
38  */
39 enum {
40     /* Other tokens which we can return: */
41     TOKEN_NONE = 0,
42     TOKEN_START = 128,
43
44     TOKEN_IDENT,
45
46     TOKEN_TYPENAME,
47
48     TOKEN_OPERATOR,
49
50     TOKEN_KEYWORD, /* loop */
51
52     TOKEN_DOTS, /* 3 dots, ... */
53
54     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
55     TOKEN_CHARCONST,
56     TOKEN_VECTORCONST,
57     TOKEN_INTCONST,
58     TOKEN_FLOATCONST,
59
60     TOKEN_WHITE,
61     TOKEN_EOL,
62
63     TOKEN_EOF,
64
65     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
66      * other error related tokens as well
67      */
68     TOKEN_ERROR,
69     TOKEN_FATAL /* internal error, eg out of memory */
70 };
71
72 static const char *_tokennames[] = {
73     "TOKEN_START",
74     "TOKEN_IDENT",
75     "TOKEN_TYPENAME",
76     "TOKEN_OPERATOR",
77     "TOKEN_KEYWORD",
78     "TOKEN_DOTS",
79     "TOKEN_STRINGCONST",
80     "TOKEN_CHARCONST",
81     "TOKEN_VECTORCONST",
82     "TOKEN_INTCONST",
83     "TOKEN_FLOATCONST",
84     "TOKEN_WHITE",
85     "TOKEN_EOL",
86     "TOKEN_EOF",
87     "TOKEN_ERROR",
88     "TOKEN_FATAL",
89 };
90 typedef int
91 _all_tokennames_added_[
92         ((TOKEN_FATAL - TOKEN_START + 1) ==
93          (sizeof(_tokennames)/sizeof(_tokennames[0])))
94         ? 1 : -1];
95
96 typedef struct {
97     char *name;
98     int   value;
99 } frame_macro;
100
101 typedef struct {
102         FILE   *file;
103         const char *open_string;
104         size_t      open_string_length;
105         size_t      open_string_pos;
106
107         char   *name;
108         size_t  line;
109         size_t  sline; /* line at the start of a token */
110
111         char    peek[256];
112         size_t  peekpos;
113
114         bool    eof;
115
116         token   tok; /* not a pointer anymore */
117
118         struct {
119             bool noops;
120             bool nodigraphs; /* used when lexing string constants */
121             bool preprocessing; /* whitespace and EOLs become actual tokens */
122         } flags;
123
124     int framevalue;
125         MEM_VECTOR_MAKE(frame_macro, frames);
126         char *modelname;
127 } lex_file;
128
129 MEM_VECTOR_PROTO(lex_file, char, token);
130
131 lex_file* lex_open (const char *file);
132 lex_file* lex_open_string(const char *str, size_t len, const char *name);
133 void      lex_close(lex_file   *lex);
134 int       lex_do   (lex_file   *lex);
135 void      lex_cleanup(void);
136
137 /* Parser
138  *
139  */
140
141 enum {
142     ASSOC_LEFT,
143     ASSOC_RIGHT
144 };
145
146 #define OP_SUFFIX 1
147 #define OP_PREFIX 2
148
149 typedef struct {
150     const char   *op;
151     unsigned int operands;
152     unsigned int id;
153     unsigned int assoc;
154     unsigned int prec;
155     unsigned int flags;
156 } oper_info;
157
158 #define opid1(a) (a)
159 #define opid2(a,b) ((a<<8)|b)
160 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
161
162 static const oper_info c_operators[] = {
163     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
164
165     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
166     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
167
168     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
169     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
170     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
171
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, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
176     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
177     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
178 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
179
180     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
181     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
182     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
183
184     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
185     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
186
187     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
188     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
189
190     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
191     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
192     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
193     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
194
195     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
196     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
197
198     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
199
200     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
201
202     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
203
204     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
205
206     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
207
208     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
209
210     { "=",   2, opid1('='),         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, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
216     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
217     { "<<=", 2, opid3('<','<','='), 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
222     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
223 };
224 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
225
226 static const oper_info qcc_operators[] = {
227     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
228
229     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
230     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
231     { "[",   2, opid1('['),         ASSOC_LEFT,  15, 0 }, /* array subscript */
232
233     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
234     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
235     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
236
237     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
238     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
239     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0 },
240     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0 },
241
242     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
243     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
244
245     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
246     { ">",   2, opid1('>'),         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 },
250     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
251
252     { "=",   2, opid1('='),         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 },
259     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0 },
260
261     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
262     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
263
264     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
265 };
266 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
267
268 extern const oper_info *operators;
269 extern size_t           operator_count;
270 void lexerror(lex_file*, const char *fmt, ...);
271
272 #endif