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
26 * Keywords are multichar, punctuation lexing is a bit more complicated
27 * than keyword lexing.
29 static const char *const lex_keywords[] = {
30 "do", "else", "if", "while",
31 "break", "continue", "return", "goto",
35 struct lex_file *lex_open(FILE *fp) {
36 struct lex_file *lex = mem_a(sizeof(struct lex_file));
41 fseek(lex->file, 0, SEEK_END);
42 lex->length = ftell(lex->file);
43 lex->size = lex->length; /* copy, this is never changed */
44 fseek(lex->file, 0, SEEK_SET);
48 memset(lex->peek, 0, sizeof(lex->peek));
52 void lex_close(struct lex_file *file) {
55 fclose(file->file); /* may already be closed */
59 static void lex_addch(int ch, struct lex_file *file) {
60 if (file->current < sizeof(file->lastok)-1)
61 file->lastok[file->current++] = (char)ch;
62 if (file->current == sizeof(file->lastok)-1)
63 file->lastok[file->current] = (char)'\0';
65 static inline void lex_clear(struct lex_file *file) {
70 * read in inget/unget character from a lexer stream.
71 * This doesn't play with file streams, the lexer has
72 * it's own internal state for this.
74 static int lex_inget(struct lex_file *file) {
77 return file->peek[--file->last];
78 return fgetc(file->file);
80 static void lex_unget(int ch, struct lex_file *file) {
81 if (file->last < sizeof(file->peek))
82 file->peek[file->last++] = ch;
87 * This is trigraph and digraph support, a feature not qc compiler
88 * supports. Moving up in this world!
90 static int lex_trigraph(struct lex_file *file) {
92 if ((ch = lex_inget(file)) != '?') {
99 case '(' : return '[' ;
100 case ')' : return ']' ;
101 case '/' : return '\\';
102 case '\'': return '^' ;
103 case '<' : return '{' ;
104 case '>' : return '}' ;
105 case '!' : return '|' ;
106 case '-' : return '~' ;
107 case '=' : return '#' ;
109 lex_unget('?', file);
110 lex_unget(ch , file);
115 static int lex_digraph(struct lex_file *file, int first) {
116 int ch = lex_inget(file);
119 if (ch == '%') return '{';
120 if (ch == ':') return '[';
123 if (ch == '>') return '}';
124 if (ch == ':') return '#';
127 if (ch == '>') return ']';
135 static int lex_getch(struct lex_file *file) {
136 int ch = lex_inget(file);
141 return lex_trigraph(file);
145 case '"' : str = !str; if (str) { file->line ++; }
146 return lex_digraph(file, ch);
156 static int lex_get(struct lex_file *file) {
158 if (!isspace(ch = lex_getch(file)))
161 /* skip over all spaces */
162 while (isspace(ch) && ch != '\n')
163 ch = lex_getch(file);
171 static int lex_skipchr(struct lex_file *file) {
176 lex_addch('\'', file);
178 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
184 lex_addch(lex_getch(file), file);
186 lex_addch('\'', file);
187 lex_addch('\0', file);
195 static int lex_skipstr(struct lex_file *file) {
198 lex_addch('"', file);
200 while ((ch = lex_getch(file)) != '"') {
201 if (ch == '\n' || ch == EOF)
206 lex_addch(lex_inget(file), file);
209 lex_addch('"', file);
210 lex_addch('\0', file);
214 static int lex_skipcmt(struct lex_file *file) {
217 ch = lex_getch(file);
220 lex_addch('/', file);
221 lex_addch('/', file);
223 while ((ch = lex_getch(file)) != '\n') {
226 lex_addch(lex_getch(file), file);
231 lex_addch('\0', file);
240 lex_addch('/', file);
245 while ((ch = lex_getch(file)) != '*') {
247 return error(file, ERROR_LEX, "malformatted comment");
252 } while ((ch = lex_getch(file)) != '/');
254 lex_addch('/', file);
255 lex_addch('\0', file);
260 static int lex_getsource(struct lex_file *file) {
261 int ch = lex_get(file);
263 /* skip char/string/comment */
265 case '\'': return lex_skipchr(file);
266 case '"': return lex_skipstr(file);
267 case '/': return lex_skipcmt(file);
273 int lex_token(struct lex_file *file) {
274 int ch = lex_getsource(file);
277 /* valid identifier */
278 if (ch > 0 && (ch == '_' || isalpha(ch))) {
282 * Yes this is dirty, but there is no other _sane_ easy
283 * way to do it, this is what I call defensive programming
284 * if something breaks, add more defense :-)
286 while (ch > 0 && ch != ' ' && ch != '(' &&
287 ch != '\n' && ch != ';' && ch != ')') {
289 ch = lex_getsource(file);
292 lex_addch('\0', file);
294 /* look inside the table for a keyword .. */
295 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
296 if (!strncmp(file->lastok, lex_keywords[it], strlen(lex_keywords[it])))
300 #define TEST_TYPE(X) \
302 if (!strncmp(X, "float", sizeof("float"))) \
303 return TOKEN_FLOAT; \
304 if (!strncmp(X, "vector", sizeof("vector"))) \
305 return TOKEN_VECTOR; \
306 if (!strncmp(X, "string", sizeof("string"))) \
307 return TOKEN_STRING; \
308 if (!strncmp(X, "entity", sizeof("entity"))) \
309 return TOKEN_ENTITY; \
310 if (!strncmp(X, "void" , sizeof("void"))) \
314 TEST_TYPE(file->lastok);
316 /* try the hashtable for typedefs? */
317 if (typedef_find(file->lastok))
318 TEST_TYPE(typedef_find(file->lastok)->name);
326 void lex_reset(struct lex_file *file) {
329 file->length = file->size;
330 fseek(file->file, 0, SEEK_SET);
332 memset(file->peek, 0, sizeof(file->peek ));
333 memset(file->lastok, 0, sizeof(file->lastok));
337 * Include a file into the lexer / parsing process: This really
338 * should check if names are the same to prevent endless include
341 struct lex_file *lex_include(struct lex_file *lex, char *file) {
343 if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
344 error(lex, ERROR_LEX, "Source file cannot include itself\n");
348 FILE *fp = fopen(file, "r");
350 error(lex, ERROR_LEX, "Include file `%s` doesn't exist\n", file);