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