]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Merge branch 'master' into blub/bc3
[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         FILE   *file;
86         char   *name;
87         size_t  line;
88         size_t  sline; /* line at the start of a token */
89
90         char    peek[256];
91         size_t  peekpos;
92
93         token  *tok;
94
95         struct {
96             bool noops;
97         } flags;
98 } lex_file;
99
100 MEM_VECTOR_PROTO(lex_file, char, token);
101
102 lex_file* lex_open (const char *file);
103 void      lex_close(lex_file   *lex);
104 int       lex_do   (lex_file   *lex);
105
106 /* Parser
107  *
108  */
109
110 enum {
111     ASSOC_LEFT,
112     ASSOC_RIGHT
113 };
114
115 #define OP_SUFFIX 1
116 #define OP_PREFIX 2
117
118 typedef struct {
119     const char   *op;
120     unsigned int operands;
121     unsigned int id;
122     unsigned int assoc;
123     unsigned int prec;
124     unsigned int flags;
125 } oper_info;
126
127 #define opid1(a) (a)
128 #define opid2(a,b) ((a<<8)|b)
129 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
130
131 static const oper_info operators[] = {
132     { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
133     { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
134
135     { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
136
137     { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, 0 },
138     { "~",   1, opid2('~', 'P'),    ASSOC_RIGHT, 14, 0 },
139     { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
140     { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
141     { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
142     { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
143 /*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
144
145     { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
146     { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
147     { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
148
149     { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
150     { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
151
152     { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
153     { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
154
155     { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
156     { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
157     { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
158     { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
159
160     { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
161     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
162
163     { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
164
165     { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
166
167     { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
168
169     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
170
171     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
172
173     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
174
175     { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0 },
176     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 2,  0 },
177     { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 2,  0 },
178     { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 2,  0 },
179     { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 2,  0 },
180     { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
181     { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
182     { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
183     { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0 },
184     { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0 },
185     { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0 },
186
187     { ",",   2, opid1(','),         ASSOC_LEFT,  1,  0 }
188 };
189 static const size_t operator_count = (sizeof(operators) / sizeof(operators[0]));
190
191 typedef struct
192 {
193         lex_file *lex;
194         int      error;
195         lex_ctx  ctx;
196
197         token    *tokens;
198         token    *lastok;
199
200         token    *tok; /* current token */
201
202         MEM_VECTOR_MAKE(ast_value*, globals);
203 } parse_file;
204
205 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
206
207 parse_file* parse_open(const char *file);
208 void        parse_file_close(parse_file*);
209
210 bool        parse(parse_file*);
211
212 bool        parse_iskey(parse_file *self, const char *ident);
213
214 void lexerror(lex_file*, const char *fmt, ...);
215
216 #endif