]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lex.c
Lexer fixes
[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 = 0;
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     file->length --;
81     if (file->last > 0)
82         return file->peek[--file->last];
83     return fgetc(file->file);
84 }
85 static void lex_unget(int ch, lex_file *file) {
86     if (file->last < sizeof(file->peek))
87         file->peek[file->last++] = ch;
88     file->length ++;
89 }
90
91 /*
92  * This is trigraph and digraph support, a feature not qc compiler
93  * supports.  Moving up in this world!
94  */
95 static int lex_trigraph(lex_file *file) {
96     int  ch;
97     if ((ch = lex_inget(file)) != '?') {
98         lex_unget(ch, file);
99         return '?';
100     }
101     
102     ch = lex_inget(file);
103     switch (ch) {
104         case '(' : return '[' ;
105         case ')' : return ']' ;
106         case '/' : return '\\';
107         case '\'': return '^' ;
108         case '<' : return '{' ;
109         case '>' : return '}' ;
110         case '!' : return '|' ;
111         case '-' : return '~' ;
112         case '=' : return '#' ;
113         default:
114             lex_unget('?', file);
115             lex_unget(ch , file);
116             return '?';
117     }
118     return '?';
119 }
120 static int lex_digraph(lex_file *file, int first) {
121     int ch = lex_inget(file);
122     switch (first) {
123         case '<':
124             if (ch == '%') return '{';
125             if (ch == ':') return '[';
126             break;
127         case '%':
128             if (ch == '>') return '}';
129             if (ch == ':') return '#';
130             break;
131         case ':':
132             if (ch == '>') return ']';
133             break;
134     }
135     
136     lex_unget(ch, file);
137     return first;
138 }
139
140 static int lex_getch(lex_file *file) {
141     int ch = lex_inget(file);
142     if (ch == '?')
143         return lex_trigraph(file);
144     if (ch == '<' || ch == ':' || ch == '%')
145         return lex_digraph(file, ch);
146     return ch;
147 }
148
149 static int lex_get(lex_file *file) {
150     int ch;
151     if (!isspace(ch = lex_getch(file)))
152         return ch;
153         
154     /* skip over all spaces */
155     while (isspace(ch) && ch != '\n')
156         ch = lex_getch(file);
157         
158     if (ch == '\n')
159         return ch;
160     lex_unget(ch, file);
161     return ' ';
162 }
163
164 static int lex_skipchr(lex_file *file) {
165     int ch;
166     int it;
167     
168     lex_clear(file);
169     lex_addch('\'', file);
170     
171     for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
172         lex_addch(ch, file);
173         
174         if (ch == '\n')
175             return ERROR_LEX;
176         if (ch == '\\')
177             lex_addch(lex_getch(file), file);
178     }
179     lex_addch('\'', file);
180     lex_addch('\0', file);
181     
182     if (it > 2)
183         return ERROR_LEX;
184         
185     return LEX_CHRLIT;
186 }
187
188 static int lex_skipstr(lex_file *file) {
189     int ch;
190     lex_clear(file);
191     lex_addch('"', file);
192     
193     while ((ch = lex_getch(file)) != '"') {
194         if (ch == '\n' || ch == EOF)
195             return ERROR_LEX;
196             
197         lex_addch(ch, file);
198         if (ch == '\\')
199             lex_addch(lex_inget(file), file);
200     }
201     
202     lex_addch('"', file);
203     lex_addch('\0', file);
204     
205     return LEX_STRLIT;
206 }
207 static int lex_skipcmt(lex_file *file) {
208     int ch;
209     lex_clear(file);
210     ch = lex_getch(file);
211     
212     if (ch == '/') {
213         lex_addch('/', file);
214         lex_addch('/', file);
215         
216         while ((ch = lex_getch(file)) != '\n') {
217             if (ch == '\\') {
218                 lex_addch(ch, file);
219                 lex_addch(lex_getch(file), file);
220             } else {
221                 lex_addch(ch, file);
222             }
223         }
224         lex_addch('\0', file);
225         return LEX_COMMENT;
226     }
227     
228     if (ch != '*') {
229         lex_unget(ch, file);
230         return '/';
231     }
232     
233     lex_addch('/', file);
234     
235     /* hate this */
236     do {
237         lex_addch(ch, file);
238         while ((ch = lex_getch(file)) != '*') {
239             if (ch == EOF)
240                 return error(file, ERROR_LEX, "malformatted comment");
241             else
242                 lex_addch(ch, file);
243         }
244         lex_addch(ch, file);
245     } while ((ch = lex_getch(file)) != '/');
246     
247     lex_addch('/',  file);
248     lex_addch('\0', file);
249     
250     return LEX_COMMENT;
251 }
252
253 static int lex_getsource(lex_file *file) {
254     int ch = lex_get(file);
255     
256     /* skip char/string/comment */
257     switch (ch) {
258         case '\'': return lex_skipchr(file);
259         case '"':  return lex_skipstr(file);
260         case '/':  return lex_skipcmt(file);
261         default:
262             return ch;
263     }
264 }
265
266 int lex_token(lex_file *file) {
267     int ch = lex_getsource(file);
268     int it;
269     
270     /* valid identifier */
271     if (ch > 0 && (ch == '_' || isalpha(ch))) {
272         lex_clear(file);
273         
274         while (ch > 0 && (ch == '_' || isalpha(ch))) {
275             lex_addch(ch, file);
276             ch = lex_getsource(file);
277         }
278         lex_unget(ch,   file);
279         lex_addch('\0', file);
280         
281         /* look inside the table for a keyword .. */
282         for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
283             if (!strncmp(file->lastok, lex_keywords[it], strlen(lex_keywords[it])))
284                 return it;
285                 
286         /* try a type? */
287         #define TEST_TYPE(X)                                 \
288             do {                                             \
289                 if (!strncmp(X, "float",  sizeof("float")))  \
290                     return TOKEN_FLOAT;                      \
291                 if (!strncmp(X, "vector", sizeof("vector"))) \
292                     return TOKEN_VECTOR;                     \
293                 if (!strncmp(X, "string", sizeof("string"))) \
294                     return TOKEN_STRING;                     \
295                 if (!strncmp(X, "entity", sizeof("entity"))) \
296                     return TOKEN_ENTITY;                     \
297                 if (!strncmp(X, "void"  , sizeof("void")))   \
298                     return TOKEN_VOID;                       \
299             } while(0)
300         
301         TEST_TYPE(file->lastok);
302         
303         /* try the hashtable for typedefs? */
304         if (typedef_find(file->lastok))
305             TEST_TYPE(typedef_find(file->lastok)->name);
306             
307         #undef TEST_TYPE
308         return LEX_IDENT;
309     }
310     return ch;
311 }
312
313 void lex_reset(lex_file *file) {
314     file->current = 0;
315     file->last    = 0;
316     file->length  = file->size;
317     fseek(file->file, 0, SEEK_SET);
318     
319     memset(file->peek,   0, sizeof(file->peek  ));
320     memset(file->lastok, 0, sizeof(file->lastok));
321 }
322
323 void lex_parse(lex_file *file) {
324     if (!file) return;
325     parse_gen(file); /* run parser */
326 }
327
328 /*
329  * Include a file into the lexer / parsing process:  This really
330  * should check if names are the same to prevent endless include
331  * recrusion.
332  */
333 lex_file *lex_include(lex_file *lex, const char *file) {
334     util_strrq(file);
335     if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
336         error(lex, ERROR_LEX, "Source file cannot include itself\n");
337         exit (-1);
338     }
339
340     lex_file *set = NULL;
341     lex_init(file, &set);
342
343     return set;
344 }