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
31 * Keywords are multichar, punctuation lexing is a bit more complicated
32 * than keyword lexing.
34 static const char *const lex_keywords[] = {
35 "do", "else", "if", "while",
36 "break", "continue", "return", "goto",
48 struct lex_file *lex_open(FILE *fp) {
49 struct lex_file *lex = mem_a(sizeof(struct lex_file));
52 fseek(lex->file, 0, SEEK_END);
53 lex->length = ftell(lex->file);
54 lex->size = lex->length; /* copy, this is never changed */
55 fseek(lex->file, 0, SEEK_SET);
58 memset(lex->peek, 0, sizeof(lex->peek));
63 int lex_close(struct lex_file *file) {
66 ret = fclose(file->file);
72 static void lex_addch(int ch, struct lex_file *file) {
73 if (file->current < sizeof(file->lastok)-1)
74 file->lastok[file->current++] = (char)ch;
75 if (file->current == sizeof(file->lastok)-1)
76 file->lastok[file->current] = (char)'\0';
78 static inline void lex_clear(struct lex_file *file) {
83 * read in inget/unget character from a lexer stream.
84 * This doesn't play with file streams, the lexer has
85 * it's own internal state for this.
87 static int lex_inget(struct lex_file *file) {
90 return file->peek[--file->last];
91 return fgetc(file->file);
93 static void lex_unget(int ch, struct lex_file *file) {
94 if (file->last < sizeof(file->peek))
95 file->peek[file->last++] = ch;
100 * This is trigraph and digraph support, a feature not qc compiler
101 * supports. Moving up in this world!
103 static int lex_trigraph(struct lex_file *file) {
105 if ((ch = lex_inget(file)) != '?') {
110 ch = lex_inget(file);
112 case '(' : return '[' ;
113 case ')' : return ']' ;
114 case '/' : return '\\';
115 case '\'': return '^' ;
116 case '<' : return '{' ;
117 case '>' : return '}' ;
118 case '!' : return '|' ;
119 case '-' : return '~' ;
120 case '=' : return '#' ;
122 lex_unget('?', file);
123 lex_unget(ch , file);
128 static int lex_digraph(struct lex_file *file, int first) {
129 int ch = lex_inget(file);
132 if (ch == '%') return '{';
133 if (ch == ':') return '[';
136 if (ch == '>') return '}';
137 if (ch == ':') return '#';
140 if (ch == '>') return ']';
148 static int lex_getch(struct lex_file *file) {
149 int ch = lex_inget(file);
151 return lex_trigraph(file);
152 if (ch == '<' || ch == ':' || ch == '%')
153 return lex_digraph (file, ch);
158 static int lex_get(struct lex_file *file) {
160 if (!isspace(ch = lex_getch(file)))
163 /* skip over all spaces */
164 while (isspace(ch) && ch != '\n')
165 ch = lex_getch(file);
174 static int lex_skipchr(struct lex_file *file) {
179 lex_addch('\'', file);
181 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
187 lex_addch(lex_getch(file), file);
189 lex_addch('\'', file);
190 lex_addch('\0', file);
198 static int lex_skipstr(struct lex_file *file) {
201 lex_addch('"', file);
203 while ((ch = lex_getch(file)) != '"') {
204 if (ch == '\n' || ch == EOF)
209 lex_addch(lex_inget(file), file);
212 lex_addch('"', file);
213 lex_addch('\0', file);
217 static int lex_skipcmt(struct lex_file *file) {
220 ch = lex_getch(file);
223 lex_addch('/', file);
224 lex_addch('/', file);
226 while ((ch = lex_getch(file)) != '\n') {
229 lex_addch(lex_getch(file), file);
234 lex_addch('\0', file);
243 lex_addch('/', file);
248 while ((ch = lex_getch(file)) != '*') {
250 return error(ERROR_LEX, "malformatted comment", " ");
255 } while ((ch = lex_getch(file)) != '/');
257 lex_addch('/', file);
258 lex_addch('\0', file);
263 static int lex_getsource(struct lex_file *file) {
264 int ch = lex_get(file);
266 /* skip char/string/comment */
268 case '\'': return lex_skipchr(file);
269 case '"': return lex_skipstr(file);
270 case '/': return lex_skipcmt(file);
275 int lex_token(struct lex_file *file) {
276 int ch = lex_getsource(file);
279 /* valid identifier */
280 if (ch > 0 && (ch == '_' || isalpha(ch))) {
282 while (ch > 0 && (isalpha(ch) || ch == '_')) {
284 ch = lex_getsource(file);
287 lex_addch('\0', file);
289 /* look inside the table for a keyword .. */
290 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
291 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
299 void lex_reset(struct lex_file *file) {
302 file->length = file->size;
303 fseek(file->file, 0, SEEK_SET);
305 memset(file->peek, 0, sizeof(file->peek ));
306 memset(file->lastok, 0, sizeof(file->lastok));
309 int lex_debug(struct lex_file *file) {
315 int list_continue = 0;
320 printf("===========================\nTOKENS: \n===========================\n");
321 while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
324 case 0: list_do ++; break;
325 case 1: list_else ++; break;
326 case 2: list_if ++; break;
327 case 3: list_while ++; break;
328 case 4: list_break ++; break;
329 case 5: list_continue++; break;
330 case 6: list_return ++; break;
331 case 7: list_goto ++; break;
332 case 8: list_for ++; break;
335 if (token >= 33 && token <= 126)
338 printf("\n===========================\nBRANCHES \n===========================\n");
339 printf("\t if % 8d\n", list_if);
340 printf("\t else % 8d\n", list_else);
341 printf("===========================\nLOOPS \n===========================\n");
342 printf("\t for % 8d\n", list_for);
343 printf("\t while % 8d\n", list_while);
344 printf("\t do % 8d\n", list_do);
345 printf("===========================\nSTATEMENTS \n===========================\n");
346 printf("\t break % 8d\n", list_break);
347 printf("\t continue % 8d\n", list_continue);
348 printf("\t return % 8d\n", list_return);
349 printf("\t goto % 8d\n", list_goto);
350 printf("===========================\nIDENTIFIERS\n===========================\n");
352 while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0)
353 if (token == LEX_IDENT)
354 printf("%s ", file->lastok);