]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lex.c
Remove trailing whitespace from everything
[xonotic/gmqcc.git] / lex.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24
25 /*
26  * Keywords are multichar, punctuation lexing is a bit more complicated
27  * than keyword lexing.
28  */
29 static const char *const lex_keywords[] = {
30     "do",    "else",     "if",     "while",
31     "break", "continue", "return", "goto",
32     "for",   "typedef"
33 };
34
35 void lex_init(const char *file, lex_file **set) {
36     lex_file *lex = mem_a(sizeof(lex_file));
37     if (!lex)
38         return;
39
40     lex->file = fopen(file, "r");
41     if (!lex->file) {
42         mem_d(lex);
43         return;
44     }
45
46     fseek(lex->file, 0, SEEK_END);
47     lex->length = ftell(lex->file);
48     lex->size   = lex->length; /* copy, this is never changed */
49     fseek(lex->file, 0, SEEK_SET);
50     lex->last = 0;
51     lex->line = 1;
52
53     memset(lex->peek, 0, sizeof(lex->peek));
54     *set = lex;
55 }
56
57 void lex_close(lex_file *file) {
58     if (!file) return;
59
60     fclose(file->file); /* may already be closed */
61     mem_d (file);
62 }
63
64 static void lex_addch(int ch, lex_file *file) {
65     if (file->current <  sizeof(file->lastok)-1)
66         file->lastok[file->current++] = (char)ch;
67     if (file->current == sizeof(file->lastok)-1)
68         file->lastok[file->current]   = (char)'\0';
69 }
70 static inline void lex_clear(lex_file *file) {
71     file->current = 0;
72 }
73
74 /*
75  * read in inget/unget character from a lexer stream.
76  * This doesn't play with file streams, the lexer has
77  * it's own internal state for this.
78  */
79 static int lex_inget(lex_file *file) {
80     char  get;
81     file->length --;
82
83     if (file->last > 0) {
84         if ((get = file->peek[--file->last]) == '\n')
85             file->line ++;
86         return get;
87     }
88     if ((get = fgetc(file->file)) == '\n')
89         file->line++;
90
91     return get;
92 }
93 static void lex_unget(int ch, lex_file *file) {
94     if (file->last < sizeof(file->peek)) {
95         if (ch == '\n')
96             file->line --;
97         file->peek[file->last++] = ch;
98     }
99     file->length ++;
100 }
101
102 /*
103  * This is trigraph and digraph support, a feature not qc compiler
104  * supports.  Moving up in this world!
105  */
106 static int lex_trigraph(lex_file *file) {
107     int  ch;
108     if ((ch = lex_inget(file)) != '?') {
109         lex_unget(ch, file);
110         return '?';
111     }
112
113     ch = lex_inget(file);
114     switch (ch) {
115         case '(' : return '[' ;
116         case ')' : return ']' ;
117         case '/' : return '\\';
118         case '\'': return '^' ;
119         case '<' : return '{' ;
120         case '>' : return '}' ;
121         case '!' : return '|' ;
122         case '-' : return '~' ;
123         case '=' : return '#' ;
124         default:
125             lex_unget('?', file);
126             lex_unget(ch , file);
127             return '?';
128     }
129     return '?';
130 }
131 static int lex_digraph(lex_file *file, int first) {
132     int ch = lex_inget(file);
133     switch (first) {
134         case '<':
135             if (ch == '%') return '{';
136             if (ch == ':') return '[';
137             break;
138         case '%':
139             if (ch == '>') return '}';
140             if (ch == ':') return '#';
141             break;
142         case ':':
143             if (ch == '>') return ']';
144             break;
145     }
146
147     lex_unget(ch, file);
148     return first;
149 }
150
151 static int lex_getch(lex_file *file) {
152     int ch = lex_inget(file);
153     if (ch == '?')
154         return lex_trigraph(file);
155     if (ch == '<' || ch == ':' || ch == '%')
156         return lex_digraph(file, ch);
157     return ch;
158 }
159
160 static int lex_get(lex_file *file) {
161     int ch;
162     if (!isspace(ch = lex_getch(file)))
163         return ch;
164
165     /* skip over all spaces */
166     while (isspace(ch) && ch != '\n')
167         ch = lex_getch(file);
168
169     if (ch == '\n')
170         return ch;
171     lex_unget(ch, file);
172     return ' ';
173 }
174
175 static int lex_skipchr(lex_file *file) {
176     int ch;
177     int it;
178
179     lex_clear(file);
180     lex_addch('\'', file);
181
182     for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
183         lex_addch(ch, file);
184
185         if (ch == '\n')
186             return ERROR_LEX;
187         if (ch == '\\')
188             lex_addch(lex_getch(file), file);
189     }
190     lex_addch('\'', file);
191     lex_addch('\0', file);
192
193     if (it > 2)
194         return ERROR_LEX;
195
196     return LEX_CHRLIT;
197 }
198
199 static int lex_skipstr(lex_file *file) {
200     int ch;
201     lex_clear(file);
202     lex_addch('"', file);
203
204     while ((ch = lex_getch(file)) != '"') {
205         if (ch == '\n' || ch == EOF)
206             return ERROR_LEX;
207
208         lex_addch(ch, file);
209         if (ch == '\\')
210             lex_addch(lex_inget(file), file);
211     }
212
213     lex_addch('"', file);
214     lex_addch('\0', file);
215
216     return LEX_STRLIT;
217 }
218 static int lex_skipcmt(lex_file *file) {
219     int ch;
220     lex_clear(file);
221     ch = lex_getch(file);
222
223     if (ch == '/') {
224         lex_addch('/', file);
225         lex_addch('/', file);
226
227         while ((ch = lex_getch(file)) != '\n') {
228             if (ch == '\\') {
229                 lex_addch(ch, file);
230                 lex_addch(lex_getch(file), file);
231             } else {
232                 lex_addch(ch, file);
233             }
234         }
235         lex_addch('\0', file);
236         return LEX_COMMENT;
237     }
238
239     if (ch != '*') {
240         lex_unget(ch, file);
241         return '/';
242     }
243
244     lex_addch('/', file);
245
246     /* hate this */
247     do {
248         lex_addch(ch, file);
249         while ((ch = lex_getch(file)) != '*') {
250             if (ch == EOF)
251                 return error(file, ERROR_LEX, "malformatted comment");
252             else
253                 lex_addch(ch, file);
254         }
255         lex_addch(ch, file);
256     } while ((ch = lex_getch(file)) != '/');
257
258     lex_addch('/',  file);
259     lex_addch('\0', file);
260
261     return LEX_COMMENT;
262 }
263
264 static int lex_getsource(lex_file *file) {
265     int ch = lex_get(file);
266
267     /* skip char/string/comment */
268     switch (ch) {
269         case '\'': return lex_skipchr(file);
270         case '"':  return lex_skipstr(file);
271         case '/':  return lex_skipcmt(file);
272         default:
273             return ch;
274     }
275 }
276
277 int lex_token(lex_file *file) {
278     int ch = lex_getsource(file);
279     int it;
280
281     /* valid identifier */
282     if (ch > 0 && (ch == '_' || isalpha(ch))) {
283         lex_clear(file);
284
285         while (ch > 0 && (ch == '_' || isalpha(ch))) {
286             lex_addch(ch, file);
287             ch = lex_getsource(file);
288         }
289         lex_unget(ch,   file);
290         lex_addch('\0', file);
291
292         /* look inside the table for a keyword .. */
293         for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
294             if (!strncmp(file->lastok, lex_keywords[it], strlen(lex_keywords[it])))
295                 return it;
296
297         /* try a type? */
298         #define TEST_TYPE(X)                                 \
299             do {                                             \
300                 if (!strncmp(X, "float",  sizeof("float")))  \
301                     return TOKEN_FLOAT;                      \
302                 if (!strncmp(X, "vector", sizeof("vector"))) \
303                     return TOKEN_VECTOR;                     \
304                 if (!strncmp(X, "string", sizeof("string"))) \
305                     return TOKEN_STRING;                     \
306                 if (!strncmp(X, "entity", sizeof("entity"))) \
307                     return TOKEN_ENTITY;                     \
308                 if (!strncmp(X, "void"  , sizeof("void")))   \
309                     return TOKEN_VOID;                       \
310             } while(0)
311
312         TEST_TYPE(file->lastok);
313
314         /* try the hashtable for typedefs? */
315         if (typedef_find(file->lastok))
316             TEST_TYPE(typedef_find(file->lastok)->name);
317
318         #undef TEST_TYPE
319         return LEX_IDENT;
320     }
321     return ch;
322 }
323
324 void lex_reset(lex_file *file) {
325     file->current = 0;
326     file->last    = 0;
327     file->length  = file->size;
328     fseek(file->file, 0, SEEK_SET);
329
330     memset(file->peek,   0, sizeof(file->peek  ));
331     memset(file->lastok, 0, sizeof(file->lastok));
332 }
333
334 void lex_parse(lex_file *file) {
335     if (!file) return;
336     parse_gen(file); /* run parser */
337 }
338
339 /*
340  * Include a file into the lexer / parsing process:  This really
341  * should check if names are the same to prevent endless include
342  * recrusion.
343  */
344 lex_file *lex_include(lex_file *lex, const char *file) {
345     util_strrq(file);
346     if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
347         error(lex, ERROR_LEX, "Source file cannot include itself\n");
348         exit (-1);
349     }
350
351     lex_file *set = NULL;
352     lex_init(file, &set);
353
354     return set;
355 }