]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lex.c
Fix parser bug
[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 <stdio.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include "gmqcc.h"
29
30 /*
31  * Keywords are multichar, punctuation lexing is a bit more complicated
32  * than keyword lexing.
33  */
34 static const char *const lex_keywords[] = {
35         "do",    "else",     "if",     "while",
36         "break", "continue", "return", "goto",
37         "for",   "typedef",
38         
39         /* types */
40         "void",
41         "string",
42         "float",
43         "vector",
44         "entity",
45 };
46
47 struct lex_file *lex_open(FILE *fp) {
48         struct lex_file *lex = mem_a(sizeof(struct lex_file));
49         if (lex) {
50                 lex->file = fp;
51                 fseek(lex->file, 0, SEEK_END);
52                 lex->length = ftell(lex->file);
53                 lex->size   = lex->length; /* copy, this is never changed */
54                 fseek(lex->file, 0, SEEK_SET);
55                 lex->last = 0;
56                 
57                 memset(lex->peek, 0, sizeof(lex->peek));
58         }
59         return lex;
60 }
61
62 int lex_close(struct lex_file *file) {
63         int ret = -1;
64         if (file) {
65                 ret = fclose(file->file);
66                 mem_d(file);
67         }
68         return ret;
69 }
70
71 static void lex_addch(int ch, struct lex_file *file) {
72         if (file->current <  sizeof(file->lastok)-1)
73                 file->lastok[file->current++] = (char)ch;
74         if (file->current == sizeof(file->lastok)-1)
75                 file->lastok[file->current]   = (char)'\0';
76 }
77 static inline void lex_clear(struct lex_file *file) {
78         file->current = 0;
79 }
80
81 /*
82  * read in inget/unget character from a lexer stream.
83  * This doesn't play with file streams, the lexer has
84  * it's own internal state for this.
85  */
86 static int lex_inget(struct lex_file *file) {
87         file->length --;
88         if (file->last > 0)
89                 return file->peek[--file->last];
90         return fgetc(file->file);
91 }
92 static void lex_unget(int ch, struct lex_file *file) {
93         if (file->last < sizeof(file->peek))
94                 file->peek[file->last++] = ch;
95         file->length ++;
96 }
97
98 /*
99  * This is trigraph and digraph support, a feature not qc compiler
100  * supports.  Moving up in this world!
101  */
102 static int lex_trigraph(struct lex_file *file) {
103         int  ch;
104         if ((ch = lex_inget(file)) != '?') {
105                 lex_unget(ch, file);
106                 return '?';
107         }
108         
109         ch = lex_inget(file);
110         switch (ch) {
111                 case '(' : return '[' ;
112                 case ')' : return ']' ;
113                 case '/' : return '\\';
114                 case '\'': return '^' ;
115                 case '<' : return '{' ;
116                 case '>' : return '}' ;
117                 case '!' : return '|' ;
118                 case '-' : return '~' ;
119                 case '=' : return '#' ;
120                 default:
121                         lex_unget('?', file);
122                         lex_unget(ch , file);
123                         return '?';
124         }
125         return '?';
126 }
127 static int lex_digraph(struct lex_file *file, int first) {
128         int ch = lex_inget(file);
129         switch (first) {
130                 case '<':
131                         if (ch == '%') return '{';
132                         if (ch == ':') return '[';
133                         break;
134                 case '%':
135                         if (ch == '>') return '}';
136                         if (ch == ':') return '#';
137                         break;
138                 case ':':
139                         if (ch == '>') return ']';
140                         break;
141         }
142         
143         lex_unget(ch, file);
144         return first;
145 }
146
147 static int lex_getch(struct lex_file *file) {
148         int ch = lex_inget(file);
149         if (ch == '?')
150                 return lex_trigraph(file);
151         if (ch == '<' || ch == ':' || ch == '%')
152                 return lex_digraph (file, ch);
153                 
154         return ch;
155 }
156
157 static int lex_get(struct lex_file *file) {
158         int ch;
159         if (!isspace(ch = lex_getch(file)))
160                 return ch;
161         
162         /* skip over all spaces */
163         while (isspace(ch) && ch != '\n')
164                 ch = lex_getch(file);
165                 
166         if (ch == '\n')
167                 return ch;
168         lex_unget(ch, file);
169         return ' ';
170 }
171
172 static int lex_skipchr(struct lex_file *file) {
173         int ch;
174         int it;
175         
176         lex_clear(file);
177         lex_addch('\'', file);
178         
179         for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
180                 lex_addch(ch, file);
181                 
182                 if (ch == '\n')
183                         return ERROR_LEX;
184                 if (ch == '\\')
185                         lex_addch(lex_getch(file), file);
186         }
187         lex_addch('\'', file);
188         lex_addch('\0', file);
189         
190         if (it > 2)
191                 return ERROR_LEX;
192                 
193         return LEX_CHRLIT;
194 }
195
196 static int lex_skipstr(struct lex_file *file) {
197         int ch;
198         lex_clear(file);
199         lex_addch('"', file);
200         
201         while ((ch = lex_getch(file)) != '"') {
202                 if (ch == '\n' || ch == EOF)
203                         return ERROR_LEX;
204                         
205                 lex_addch(ch, file);
206                 if (ch == '\\')
207                         lex_addch(lex_inget(file), file);
208         }
209         
210         lex_addch('"', file);
211         lex_addch('\0', file);
212         
213         return LEX_STRLIT;
214 }
215 static int lex_skipcmt(struct lex_file *file) {
216         int ch;
217         lex_clear(file);
218         ch = lex_getch(file);
219         
220         if (ch == '/') {
221                 lex_addch('/', file);
222                 lex_addch('/', file);
223                 
224                 while ((ch = lex_getch(file)) != '\n') {
225                         if (ch == '\\') {
226                                 lex_addch(ch, file);
227                                 lex_addch(lex_getch(file), file);
228                         } else {
229                                 lex_addch(ch, file);
230                         }
231                 }
232                 lex_addch('\0', file);
233                 return LEX_COMMENT;
234         }
235         
236         if (ch != '*') {
237                 lex_unget(ch, file);
238                 return '/';
239         }
240         
241         lex_addch('/', file);
242         
243         /* hate this */
244         do {
245                 lex_addch(ch, file);
246                 while ((ch = lex_getch(file)) != '*') {
247                         if (ch == EOF)
248                                 return error(ERROR_LEX, "malformatted comment at line %d", file->line);
249                         else
250                                 lex_addch(ch, file);
251                 }
252                 lex_addch(ch, file);
253         } while ((ch = lex_getch(file)) != '/');
254         
255         lex_addch('/',  file);
256         lex_addch('\0', file);
257         
258         return LEX_COMMENT;
259 }
260
261 static int lex_getsource(struct lex_file *file) {
262         int ch = lex_get(file);
263         
264         /* skip char/string/comment */
265         switch (ch) {
266                 case '\'': return lex_skipchr(file);
267                 case '"':  return lex_skipstr(file);
268                 case '/':  return lex_skipcmt(file);
269                 default:   return ch;
270         }
271 }
272
273 int lex_token(struct lex_file *file) {
274         int ch = lex_getsource(file);
275         int it;
276         
277         /* valid identifier */
278         if (ch > 0 && (ch == '_' || isalpha(ch))) {
279                 lex_clear(file);
280                 while (ch > 0 && (isalpha(ch) || ch == '_')) {
281                         lex_addch(ch, file);
282                         ch = lex_getsource(file);
283                 }
284                 lex_unget(ch,   file);
285                 lex_addch('\0', file);
286                 
287                 /* look inside the table for a keyword .. */
288                 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
289                         if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
290                                 return it;
291                                 
292                 /* try the hashtable for typedefs? */
293                 if (typedef_find(file->lastok))
294                         for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
295                                 if (!strncmp(typedef_find(file->lastok)->name, lex_keywords[it], sizeof(lex_keywords[it])))
296                                         return it;
297                 
298                         
299                 return LEX_IDENT;
300         }
301         return ch;
302 }
303
304 void lex_reset(struct lex_file *file) {
305         file->current = 0;
306         file->last    = 0;
307         file->length  = file->size;
308         fseek(file->file, 0, SEEK_SET);
309         
310         memset(file->peek,   0, sizeof(file->peek  ));
311         memset(file->lastok, 0, sizeof(file->lastok));
312 }