]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lex.c
Fixed parsing issues, added some parser tests.
[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
40 struct lex_file *lex_open(FILE *fp) {
41         struct lex_file *lex = mem_a(sizeof(struct lex_file));
42         if (lex) {
43                 lex->file = fp;
44                 fseek(lex->file, 0, SEEK_END);
45                 lex->length = ftell(lex->file);
46                 lex->size   = lex->length; /* copy, this is never changed */
47                 fseek(lex->file, 0, SEEK_SET);
48                 lex->last = 0;
49                 
50                 memset(lex->peek, 0, sizeof(lex->peek));
51         }
52         return lex;
53 }
54
55 int lex_close(struct lex_file *file) {
56         int ret = -1;
57         if (file) {
58                 ret = fclose(file->file);
59                 mem_d(file);
60         }
61         return ret;
62 }
63
64 static void lex_addch(int ch, struct 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(struct 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(struct 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, struct 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(struct 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(struct 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(struct 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                 
147         return ch;
148 }
149
150 static int lex_get(struct lex_file *file) {
151         int ch;
152         if (!isspace(ch = lex_getch(file)))
153                 return ch;
154         
155         /* skip over all spaces */
156         while (isspace(ch) && ch != '\n')
157                 ch = lex_getch(file);
158                 
159         if (ch == '\n')
160                 return ch;
161         lex_unget(ch, file);
162         return ' ';
163 }
164
165 static int lex_skipchr(struct lex_file *file) {
166         int ch;
167         int it;
168         
169         lex_clear(file);
170         lex_addch('\'', file);
171         
172         for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
173                 lex_addch(ch, file);
174                 
175                 if (ch == '\n')
176                         return ERROR_LEX;
177                 if (ch == '\\')
178                         lex_addch(lex_getch(file), file);
179         }
180         lex_addch('\'', file);
181         lex_addch('\0', file);
182         
183         if (it > 2)
184                 return ERROR_LEX;
185                 
186         return LEX_CHRLIT;
187 }
188
189 static int lex_skipstr(struct lex_file *file) {
190         int ch;
191         lex_clear(file);
192         lex_addch('"', file);
193         
194         while ((ch = lex_getch(file)) != '"') {
195                 if (ch == '\n' || ch == EOF)
196                         return ERROR_LEX;
197                         
198                 lex_addch(ch, file);
199                 if (ch == '\\')
200                         lex_addch(lex_inget(file), file);
201         }
202         
203         lex_addch('"', file);
204         lex_addch('\0', file);
205         
206         return LEX_STRLIT;
207 }
208 static int lex_skipcmt(struct lex_file *file) {
209         int ch;
210         lex_clear(file);
211         ch = lex_getch(file);
212         
213         if (ch == '/') {
214                 lex_addch('/', file);
215                 lex_addch('/', file);
216                 
217                 while ((ch = lex_getch(file)) != '\n') {
218                         if (ch == '\\') {
219                                 lex_addch(ch, file);
220                                 lex_addch(lex_getch(file), file);
221                         } else {
222                                 lex_addch(ch, file);
223                         }
224                 }
225                 lex_addch('\0', file);
226                 return LEX_COMMENT;
227         }
228         
229         if (ch != '*') {
230                 lex_unget(ch, file);
231                 return '/';
232         }
233         
234         lex_addch('/', file);
235         
236         /* hate this */
237         do {
238                 lex_addch(ch, file);
239                 while ((ch = lex_getch(file)) != '*') {
240                         if (ch == EOF)
241                                 return error(ERROR_LEX, "malformatted comment at line %d", file->line);
242                         else
243                                 lex_addch(ch, file);
244                 }
245                 lex_addch(ch, file);
246         } while ((ch = lex_getch(file)) != '/');
247         
248         lex_addch('/',  file);
249         lex_addch('\0', file);
250         
251         return LEX_COMMENT;
252 }
253
254 static int lex_getsource(struct lex_file *file) {
255         int ch = lex_get(file);
256         
257         /* skip char/string/comment */
258         switch (ch) {
259                 case '\'': return lex_skipchr(file);
260                 case '"':  return lex_skipstr(file);
261                 case '/':  return lex_skipcmt(file);
262                 default:   return ch;
263         }
264 }
265
266 int lex_token(struct 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                 while (ch > 0 && (isalpha(ch) || ch == '_')) {
274                         lex_addch(ch, file);
275                         ch = lex_getsource(file);
276                 }
277                 lex_unget(ch,   file);
278                 lex_addch('\0', file);
279                 
280                 /* look inside the table for a keyword .. */
281                 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
282                         if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
283                                 return it;
284                                 
285                 /* try a type? */
286                 #define TEST_TYPE(X)                                 \
287                     do {                                             \
288                         if (!strncmp(X, "float",  sizeof("float")))  \
289                                 return TOKEN_FLOAT;                      \
290                         if (!strncmp(X, "vector", sizeof("vector"))) \
291                                 return TOKEN_VECTOR;                     \
292                         if (!strncmp(X, "string", sizeof("string"))) \
293                             return TOKEN_STRING;                     \
294                         if (!strncmp(X, "entity", sizeof("entity"))) \
295                             return TOKEN_ENTITY;                     \
296                         if (!strncmp(X, "void"  , sizeof("void")))   \
297                                 return TOKEN_VOID;                       \
298                     } while(0)
299                 
300                 TEST_TYPE(file->lastok);
301                 
302                 /* try the hashtable for typedefs? */
303                 if (typedef_find(file->lastok))
304                         TEST_TYPE(typedef_find(file->lastok)->name);
305                         
306                 return LEX_IDENT;
307         }
308         return ch;
309 }
310
311 void lex_reset(struct lex_file *file) {
312         file->current = 0;
313         file->last    = 0;
314         file->length  = file->size;
315         fseek(file->file, 0, SEEK_SET);
316         
317         memset(file->peek,   0, sizeof(file->peek  ));
318         memset(file->lastok, 0, sizeof(file->lastok));
319 }