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