]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
8d525a5db5e54eb507ba35a6def53ff162f365ad
[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_EOF,
61
62     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
63      * other error related tokens as well
64      */
65     TOKEN_ERROR,
66     TOKEN_FATAL /* internal error, eg out of memory */
67 };
68
69 static const char *_tokennames[] = {
70     "TOKEN_START",
71     "TOKEN_IDENT",
72     "TOKEN_TYPENAME",
73     "TOKEN_OPERATOR",
74     "TOKEN_KEYWORD",
75     "TOKEN_DOTS",
76     "TOKEN_STRINGCONST",
77     "TOKEN_CHARCONST",
78     "TOKEN_VECTORCONST",
79     "TOKEN_INTCONST",
80     "TOKEN_FLOATCONST",
81     "TOKEN_EOF",
82     "TOKEN_ERROR",
83     "TOKEN_FATAL",
84 };
85 typedef int
86 _all_tokennames_added_[
87         ((TOKEN_FATAL - TOKEN_START + 1) ==
88          (sizeof(_tokennames)/sizeof(_tokennames[0])))
89         ? 1 : -1];
90
91 typedef struct {
92     char *name;
93     int   value;
94 } frame_macro;
95
96 typedef struct {
97         FILE   *file;
98         char   *name;
99         size_t  line;
100         size_t  sline; /* line at the start of a token */
101
102         char    peek[256];
103         size_t  peekpos;
104
105         bool    eof;
106
107         token   tok; /* not a pointer anymore */
108
109         struct {
110             bool noops;
111             bool nodigraphs; /* used when lexing string constants */
112         } flags;
113
114     int framevalue;
115         MEM_VECTOR_MAKE(frame_macro, frames);
116         char *modelname;
117 } lex_file;
118
119 MEM_VECTOR_PROTO(lex_file, char, token);
120
121 lex_file* lex_open (const char *file);
122 void      lex_close(lex_file   *lex);
123 int       lex_do   (lex_file   *lex);
124 void      lex_cleanup(void);
125
126 /* Parser
127  *
128  */
129
130 enum {
131     ASSOC_LEFT,
132     ASSOC_RIGHT
133 };
134
135 #define OP_SUFFIX 1
136 #define OP_PREFIX 2
137
138 typedef struct {
139     const char   *op;
140     unsigned int operands;
141     unsigned int id;
142     unsigned int assoc;
143     unsigned int prec;
144     unsigned int flags;
145 } oper_info;
146
147 #define opid1(a) (a)
148 #define opid2(a,b) ((a<<8)|b)
149 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
150
151 static const oper_info c_operators[] = {
152     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
153
154     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
155     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
156
157     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
158     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
159
160     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
161     { "~",   1, opid2('~', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
162     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
163     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
164     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
165     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
166 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
167
168     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
169     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
170     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
171
172     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
173     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
174
175     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
176     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
177
178     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
179     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
180     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
181     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
182
183     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
184     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
185
186     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
187
188     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
189
190     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
191
192     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
193
194     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
195
196     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
197
198     { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0 },
199     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 2,  0 },
200     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 2,  0 },
201     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 2,  0 },
202     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 2,  0 },
203     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
204     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
205     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
206     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0 },
207     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0 },
208     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0 },
209
210     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
211 };
212 static const size_t c_operator_count = (sizeof(c_operators) / sizeof(c_operators[0]));
213
214 static const oper_info qcc_operators[] = {
215     { "(",   0, opid1('('),         ASSOC_LEFT,  99, OP_PREFIX}, /* paren expression - non function call */
216
217     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
218     { "(",   0, opid1('('),         ASSOC_LEFT,  15, 0 }, /* function call */
219
220     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
221     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
222     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
223
224     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
225     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
226     { "&",   2, opid1('&'),         ASSOC_LEFT,  13, 0 },
227     { "|",   2, opid1('|'),         ASSOC_LEFT,  13, 0 },
228
229     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
230     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
231
232     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
233     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
234     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
235     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
236     { "==",  2, opid2('=','='),     ASSOC_LEFT,  10,  0 },
237     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
238
239     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
240     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
241     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 8,  0 },
242     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 8,  0 },
243     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 8,  0 },
244     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 8,  0 },
245     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 8,  0 },
246     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 8,  0 },
247
248     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
249     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
250
251     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
252 };
253 static const size_t qcc_operator_count = (sizeof(qcc_operators) / sizeof(qcc_operators[0]));
254
255 extern const oper_info *operators;
256 extern size_t           operator_count;
257 void lexerror(lex_file*, const char *fmt, ...);
258
259 #endif