]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
lexer now turns '(' into an operator if noops=false
[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         struct token_s *next;
21         struct token_s *prev;
22
23         lex_ctx ctx;
24 };
25
26 token* token_new();
27 void   token_delete(token*);
28 token* token_copy(const token *cp);
29 void   token_delete_all(token *t);
30 token* token_copy_all(const token *cp);
31
32 /* Lexer
33  *
34  */
35 enum {
36     /* Other tokens which we can return: */
37     TOKEN_NONE = 0,
38     TOKEN_START = 128,
39
40     TOKEN_IDENT,
41
42     TOKEN_TYPENAME,
43
44     TOKEN_OPERATOR,
45
46     TOKEN_KEYWORD, /* loop */
47
48     TOKEN_STRINGCONST, /* not the typename but an actual "string" */
49     TOKEN_CHARCONST,
50     TOKEN_VECTORCONST,
51     TOKEN_INTCONST,
52     TOKEN_FLOATCONST,
53
54     TOKEN_EOF,
55
56     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
57      * other error related tokens as well
58      */
59     TOKEN_ERROR,
60     TOKEN_FATAL /* internal error, eg out of memory */
61 };
62
63 static const char *_tokennames[] = {
64     "TOKEN_START",
65     "TOKEN_IDENT",
66     "TOKEN_TYPENAME",
67     "TOKEN_OPERATOR",
68     "TOKEN_KEYWORD",
69     "TOKEN_STRINGCONST",
70     "TOKEN_CHARCONST",
71     "TOKEN_VECTORCONST",
72     "TOKEN_INTCONST",
73     "TOKEN_FLOATCONST",
74     "TOKEN_EOF",
75     "TOKEN_ERROR",
76     "TOKEN_FATAL",
77 };
78 typedef int
79 _all_tokennames_added_[
80         ((TOKEN_FATAL - TOKEN_START + 1) ==
81          (sizeof(_tokennames)/sizeof(_tokennames[0])))
82         ? 1 : -1];
83
84 typedef struct {
85     char *name;
86     int   value;
87 } frame_macro;
88
89 typedef struct {
90         FILE   *file;
91         char   *name;
92         size_t  line;
93         size_t  sline; /* line at the start of a token */
94
95         char    peek[256];
96         size_t  peekpos;
97
98         token  *tok;
99
100         struct {
101             bool noops;
102         } flags;
103
104     int framevalue;
105         MEM_VECTOR_MAKE(frame_macro, frames);
106         char *modelname;
107 } lex_file;
108
109 MEM_VECTOR_PROTO(lex_file, char, token);
110
111 lex_file* lex_open (const char *file);
112 void      lex_close(lex_file   *lex);
113 int       lex_do   (lex_file   *lex);
114
115 /* Parser
116  *
117  */
118
119 enum {
120     ASSOC_LEFT,
121     ASSOC_RIGHT
122 };
123
124 #define OP_SUFFIX 1
125 #define OP_PREFIX 2
126
127 typedef struct {
128     const char   *op;
129     unsigned int operands;
130     unsigned int id;
131     unsigned int assoc;
132     unsigned int prec;
133     unsigned int flags;
134 } oper_info;
135
136 #define opid1(a) (a)
137 #define opid2(a,b) ((a<<8)|b)
138 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
139
140 static const oper_info operators[] = {
141     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
142     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
143
144     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
145     { "(",   0, opid1('('),         ASSOC_LEFT,  15, OP_SUFFIX },
146
147     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
148     { "~",   1, opid2('~', 'P'),    ASSOC_RIGHT, 14, OP_PREFIX },
149     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
150     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
151     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
152     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
153 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
154
155     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
156     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
157     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
158
159     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
160     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
161
162     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
163     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
164
165     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
166     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
167     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
168     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
169
170     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
171     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
172
173     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
174
175     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
176
177     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
178
179     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
180
181     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
182
183     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
184
185     { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0 },
186     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 2,  0 },
187     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 2,  0 },
188     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 2,  0 },
189     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 2,  0 },
190     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
191     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
192     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
193     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0 },
194     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0 },
195     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0 },
196
197     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
198 };
199 static const size_t operator_count = (sizeof(operators) / sizeof(operators[0]));
200
201 typedef struct
202 {
203         lex_file *lex;
204         int      error;
205         lex_ctx  ctx;
206
207         token    *tokens;
208         token    *lastok;
209
210         token    *tok; /* current token */
211
212         MEM_VECTOR_MAKE(ast_value*, globals);
213 } parse_file;
214
215 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
216
217 parse_file* parse_open(const char *file);
218 void        parse_file_close(parse_file*);
219
220 bool        parse(parse_file*);
221
222 bool        parse_iskey(parse_file *self, const char *ident);
223
224 void lexerror(lex_file*, const char *fmt, ...);
225
226 #endif