]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.h
Copying my old lexer
[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 assoc;
121     unsigned int prec;
122     unsigned int flags;
123 } oper_info;
124
125 static const oper_info operators[] = {
126     { "++",  ASSOC_LEFT,  16, OP_SUFFIX},
127     { "--",  ASSOC_LEFT,  16, OP_SUFFIX},
128
129     { ".",   ASSOC_LEFT,  15, 0 },
130
131     { "!",   ASSOC_RIGHT, 14, 0 },
132     { "~",   ASSOC_RIGHT, 14, 0 },
133     { "+",   ASSOC_RIGHT, 14, OP_PREFIX },
134     { "-",   ASSOC_RIGHT, 14, OP_PREFIX },
135     { "++",  ASSOC_RIGHT, 14, OP_PREFIX },
136     { "--",  ASSOC_RIGHT, 14, OP_PREFIX },
137 /*  { "&",   ASSOC_RIGHT, 14, OP_PREFIX }, */
138
139     { "*",   ASSOC_LEFT,  13, 0 },
140     { "/",   ASSOC_LEFT,  13, 0 },
141     { "%",   ASSOC_LEFT,  13, 0 },
142
143     { "+",   ASSOC_LEFT,  12, 0 },
144     { "-",   ASSOC_LEFT,  12, 0 },
145
146     { "<<",  ASSOC_LEFT,  11, 0 },
147     { ">>",  ASSOC_LEFT,  11, 0 },
148
149     { "<",   ASSOC_LEFT,  10, 0 },
150     { ">",   ASSOC_LEFT,  10, 0 },
151     { "<=",  ASSOC_LEFT,  10, 0 },
152     { ">=",  ASSOC_LEFT,  10, 0 },
153
154     { "==",  ASSOC_LEFT,  9,  0 },
155     { "!=",  ASSOC_LEFT,  9,  0 },
156
157     { "&",   ASSOC_LEFT,  8,  0 },
158
159     { "^",   ASSOC_LEFT,  7,  0 },
160
161     { "|",   ASSOC_LEFT,  6,  0 },
162
163     { "&&",  ASSOC_LEFT,  5,  0 },
164
165     { "||",  ASSOC_LEFT,  4,  0 },
166
167     { "?",   ASSOC_RIGHT, 3,  0 },
168
169     { "=",   ASSOC_RIGHT, 2,  0 },
170     { "+=",  ASSOC_RIGHT, 2,  0 },
171     { "-=",  ASSOC_RIGHT, 2,  0 },
172     { "*=",  ASSOC_RIGHT, 2,  0 },
173     { "/=",  ASSOC_RIGHT, 2,  0 },
174     { "%=",  ASSOC_RIGHT, 2,  0 },
175     { ">>=", ASSOC_RIGHT, 2,  0 },
176     { "<<=", ASSOC_RIGHT, 2,  0 },
177     { "&=",  ASSOC_RIGHT, 2,  0 },
178     { "^=",  ASSOC_RIGHT, 2,  0 },
179     { "|=",  ASSOC_RIGHT, 2,  0 },
180 };
181
182 typedef struct
183 {
184         lex_file *lex;
185         int      error;
186         lex_ctx  ctx;
187
188         token    *tokens;
189         token    *lastok;
190
191         token    *tok; /* current token */
192
193         MEM_VECTOR_MAKE(ast_value*, globals);
194 } parse_file;
195
196 MEM_VECTOR_PROTO(parse_file, ast_value*, globals);
197
198 parse_file* parse_open(const char *file);
199 void        parse_file_close(parse_file*);
200
201 bool        parse(parse_file*);
202
203 bool        parse_iskey(parse_file *self, const char *ident);
204
205 void lexerror(lex_file*, const char *fmt, ...);
206
207 #endif