]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lex.c
More parsing stuff (still totally broken)
[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 || !fp)
43                 return NULL;
44                 
45         lex->file = fp;
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         return lex;
55 }
56
57 void lex_close(struct 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, 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         
143         switch (ch) {
144                 case '?' :
145                         return lex_trigraph(file);
146                 case '<' :
147                 case ':' :
148                 case '%' :
149                         return lex_digraph (file, ch);
150                 case '\n': file->line ++;
151         }
152                 
153         return ch;
154 }
155
156 static int lex_get(struct lex_file *file) {
157         int ch;
158         if (!isspace(ch = lex_getch(file)))
159                 return ch;
160                 
161         /* skip over all spaces */
162         while (isspace(ch) && ch != '\n')
163                 ch = lex_getch(file);
164                 
165         if (ch == '\n')
166                 return ch;
167         lex_unget(ch, file);
168         return ' ';
169 }
170
171 static int lex_skipchr(struct lex_file *file) {
172         int ch;
173         int it;
174         
175         lex_clear(file);
176         lex_addch('\'', file);
177         
178         for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
179                 lex_addch(ch, file);
180                 
181                 if (ch == '\n')
182                         return ERROR_LEX;
183                 if (ch == '\\')
184                         lex_addch(lex_getch(file), file);
185         }
186         lex_addch('\'', file);
187         lex_addch('\0', file);
188         
189         if (it > 2)
190                 return ERROR_LEX;
191                 
192         return LEX_CHRLIT;
193 }
194
195 static int lex_skipstr(struct lex_file *file) {
196         int ch;
197         lex_clear(file);
198         lex_addch('"', file);
199         
200         while ((ch = lex_getch(file)) != '"') {
201                 if (ch == '\n' || ch == EOF)
202                         return ERROR_LEX;
203                         
204                 lex_addch(ch, file);
205                 if (ch == '\\')
206                         lex_addch(lex_inget(file), file);
207         }
208         
209         lex_addch('"', file);
210         lex_addch('\0', file);
211         
212         return LEX_STRLIT;
213 }
214 static int lex_skipcmt(struct lex_file *file) {
215         int ch;
216         lex_clear(file);
217         ch = lex_getch(file);
218         
219         if (ch == '/') {
220                 lex_addch('/', file);
221                 lex_addch('/', file);
222                 
223                 while ((ch = lex_getch(file)) != '\n') {
224                         if (ch == '\\') {
225                                 lex_addch(ch, file);
226                                 lex_addch(lex_getch(file), file);
227                         } else {
228                                 lex_addch(ch, file);
229                         }
230                 }
231                 lex_addch('\0', file);
232                 return LEX_COMMENT;
233         }
234         
235         if (ch != '*') {
236                 lex_unget(ch, file);
237                 return '/';
238         }
239         
240         lex_addch('/', file);
241         
242         /* hate this */
243         do {
244                 lex_addch(ch, file);
245                 while ((ch = lex_getch(file)) != '*') {
246                         if (ch == EOF)
247                                 return error(ERROR_LEX, "malformatted comment at line", "");
248                         else
249                                 lex_addch(ch, file);
250                 }
251                 lex_addch(ch, file);
252         } while ((ch = lex_getch(file)) != '/');
253         
254         lex_addch('/',  file);
255         lex_addch('\0', file);
256         
257         return LEX_COMMENT;
258 }
259
260 static int lex_getsource(struct lex_file *file) {
261         int ch = lex_get(file);
262         
263         /* skip char/string/comment */
264         switch (ch) {
265                 case '\'': return lex_skipchr(file);
266                 case '"':  return lex_skipstr(file);
267                 case '/':  return lex_skipcmt(file);
268                 default:
269                         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 && ch != ' ' && ch != '(' && ch != '\n') {
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 a type? */
293                 #define TEST_TYPE(X)                                 \
294                     do {                                             \
295                         if (!strncmp(X, "float",  sizeof("float")))  \
296                             return TOKEN_FLOAT;                      \
297                         if (!strncmp(X, "vector", sizeof("vector"))) \
298                             return TOKEN_VECTOR;                     \
299                         if (!strncmp(X, "string", sizeof("string"))) \
300                             return TOKEN_STRING;                     \
301                         if (!strncmp(X, "entity", sizeof("entity"))) \
302                             return TOKEN_ENTITY;                     \
303                         if (!strncmp(X, "void"  , sizeof("void")))   \
304                             return TOKEN_VOID;                       \
305                     } while(0)
306                 
307                 TEST_TYPE(file->lastok);
308                 
309                 /* try the hashtable for typedefs? */
310                 if (typedef_find(file->lastok))
311                         TEST_TYPE(typedef_find(file->lastok)->name);
312                         
313                 #undef TEST_TYPE
314                 return LEX_IDENT;
315         }
316         return ch;
317 }
318
319 void lex_reset(struct lex_file *file) {
320         file->current = 0;
321         file->last    = 0;
322         file->length  = file->size;
323         fseek(file->file, 0, SEEK_SET);
324         
325         memset(file->peek,   0, sizeof(file->peek  ));
326         memset(file->lastok, 0, sizeof(file->lastok));
327 }