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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
30 static const char *const lex_keywords[] = {
31 "do", "else", "if", "while",
32 "break", "continue", "return", "goto",
36 struct lex_file *lex_open(const char *name) {
37 struct lex_file *lex = mem_a(sizeof(struct lex_file));
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);
46 memset(lex->peek, 0, sizeof(lex->peek));
51 int lex_close(struct lex_file *file) {
54 ret = fclose(file->file);
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';
66 static inline void lex_clear(struct lex_file *file) {
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.
75 static int lex_inget(struct lex_file *file) {
78 return file->peek[--file->last];
79 return fgetc(file->file);
81 static void lex_unget(int ch, struct lex_file *file) {
82 if (file->last < sizeof(file->peek))
83 file->peek[file->last++] = ch;
88 * This is trigraph and digraph support, a feature not qc compiler
89 * supports. Moving up in this world!
91 static int lex_trigraph(struct lex_file *file) {
93 if ((ch = lex_inget(file)) != '?') {
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 '#' ;
110 lex_unget('?', file);
111 lex_unget(ch , file);
116 static int lex_digraph(struct lex_file *file, int first) {
117 int ch = lex_inget(file);
120 if (ch == '%') return '{';
121 if (ch == ':') return '[';
124 if (ch == '>') return '}';
125 if (ch == ':') return '#';
128 if (ch == '>') return ']';
136 static int lex_getch(struct lex_file *file) {
137 int ch = lex_inget(file);
139 return lex_trigraph(file);
140 if (ch == '<' || ch == ':' || ch == '%')
141 return lex_digraph (file, ch);
146 static int lex_get(struct lex_file *file) {
148 if (!isspace(ch = lex_getch(file)))
151 /* skip over all spaces */
152 while (isspace(ch) && ch != '\n')
153 ch = lex_getch(file);
162 static int lex_skipchr(struct lex_file *file) {
167 lex_addch('\'', file);
169 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
175 lex_addch(lex_getch(file), file);
177 lex_addch('\'', file);
178 lex_addch('\0', file);
186 static int lex_skipstr(struct lex_file *file) {
189 lex_addch('"', file);
191 while ((ch = lex_getch(file)) != '"') {
192 if (ch == '\n' || ch == EOF)
197 lex_addch(lex_inget(file), file);
200 lex_addch('"', file);
201 lex_addch('\0', file);
205 static int lex_skipcmt(struct lex_file *file) {
208 ch = lex_getch(file);
211 lex_addch('/', file);
212 lex_addch('/', file);
214 while ((ch = lex_getch(file)) != '\n') {
217 lex_addch(lex_getch(file), file);
222 lex_addch('\0', file);
231 lex_addch('/', file);
236 while ((ch = lex_getch(file)) != '*') {
238 return error(ERROR_LEX, "malformatted comment"," ");
243 } while ((ch = lex_getch(file)) != '/');
245 lex_addch('/', file);
246 lex_addch('\0', file);
251 static int lex_getsource(struct lex_file *file) {
252 int ch = lex_get(file);
254 /* skip char/string/comment */
256 case '\'': return lex_skipchr(file);
257 case '"': return lex_skipstr(file);
258 case '/': return lex_skipcmt(file);
263 int lex_token(struct lex_file *file) {
264 int ch = lex_getsource(file);
267 /* valid identifier */
268 if (ch > 0 && (ch == '_' || isalpha(ch))) {
270 while (ch > 0 && (isalpha(ch) || isdigit(ch) || ch == '_')) {
272 ch = lex_getsource(file);
275 lex_addch('\0', file);
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])))
287 void lex_reset(struct lex_file *file) {
290 file->length = file->size;
291 fseek(file->file, 0, SEEK_SET);
293 memset(file->peek, 0, sizeof(file->peek ));
294 memset(file->lastok, 0, sizeof(file->lastok));
297 int lex_debug(struct lex_file *file) {
303 int list_continue = 0;
308 printf("===========================\nTOKENS: \n===========================\n");
309 while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
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;
323 if (token >= 33 && token <= 126)
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");
340 while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0)
341 if (token == LEX_IDENT)
342 printf("%s ", file->lastok);