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",
40 struct lex_file *lex_open(FILE *fp) {
41 struct lex_file *lex = mem_a(sizeof(struct lex_file));
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);
53 memset(lex->peek, 0, sizeof(lex->peek));
57 void lex_close(struct lex_file *file) {
60 fclose(file->file); /* may already be closed */
64 static void lex_addch(int ch, struct lex_file *file) {
65 if (file->current < sizeof(file->lastok)-1)
66 file->lastok[file->current++] = (char)ch;
67 if (file->current == sizeof(file->lastok)-1)
68 file->lastok[file->current] = (char)'\0';
70 static inline void lex_clear(struct lex_file *file) {
75 * read in inget/unget character from a lexer stream.
76 * This doesn't play with file streams, the lexer has
77 * it's own internal state for this.
79 static int lex_inget(struct lex_file *file) {
82 return file->peek[--file->last];
83 return fgetc(file->file);
85 static void lex_unget(int ch, struct lex_file *file) {
86 if (file->last < sizeof(file->peek))
87 file->peek[file->last++] = ch;
92 * This is trigraph and digraph support, a feature not qc compiler
93 * supports. Moving up in this world!
95 static int lex_trigraph(struct lex_file *file) {
97 if ((ch = lex_inget(file)) != '?') {
102 ch = lex_inget(file);
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 case '=' : return '#' ;
114 lex_unget('?', file);
115 lex_unget(ch , file);
120 static int lex_digraph(struct lex_file *file, int first) {
121 int ch = lex_inget(file);
124 if (ch == '%') return '{';
125 if (ch == ':') return '[';
128 if (ch == '>') return '}';
129 if (ch == ':') return '#';
132 if (ch == '>') return ']';
140 static int lex_getch(struct lex_file *file) {
141 int ch = lex_inget(file);
146 return lex_trigraph(file);
150 case '"' : str = !str; if (str) { file->line ++; }
151 return lex_digraph(file, ch);
161 static int lex_get(struct lex_file *file) {
163 if (!isspace(ch = lex_getch(file)))
166 /* skip over all spaces */
167 while (isspace(ch) && ch != '\n')
168 ch = lex_getch(file);
176 static int lex_skipchr(struct lex_file *file) {
181 lex_addch('\'', file);
183 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
189 lex_addch(lex_getch(file), file);
191 lex_addch('\'', file);
192 lex_addch('\0', file);
200 static int lex_skipstr(struct lex_file *file) {
203 lex_addch('"', file);
205 while ((ch = lex_getch(file)) != '"') {
206 if (ch == '\n' || ch == EOF)
211 lex_addch(lex_inget(file), file);
214 lex_addch('"', file);
215 lex_addch('\0', file);
219 static int lex_skipcmt(struct lex_file *file) {
222 ch = lex_getch(file);
225 lex_addch('/', file);
226 lex_addch('/', file);
228 while ((ch = lex_getch(file)) != '\n') {
231 lex_addch(lex_getch(file), file);
236 lex_addch('\0', file);
245 lex_addch('/', file);
250 while ((ch = lex_getch(file)) != '*') {
252 return error(ERROR_LEX, "malformatted comment at line", "");
257 } while ((ch = lex_getch(file)) != '/');
259 lex_addch('/', file);
260 lex_addch('\0', file);
265 static int lex_getsource(struct lex_file *file) {
266 int ch = lex_get(file);
268 /* skip char/string/comment */
270 case '\'': return lex_skipchr(file);
271 case '"': return lex_skipstr(file);
272 case '/': return lex_skipcmt(file);
278 int lex_token(struct lex_file *file) {
279 int ch = lex_getsource(file);
282 /* valid identifier */
283 if (ch > 0 && (ch == '_' || isalpha(ch))) {
287 * Yes this is dirty, but there is no other _sane_ easy
288 * way to do it, this is what I call defensive programming
289 * if something breaks, add more defense :-)
291 while (ch > 0 && ch != ' ' && ch != '(' &&
292 ch != '\n' && ch != ';' && ch != ')') {
294 ch = lex_getsource(file);
297 lex_addch('\0', file);
299 /* look inside the table for a keyword .. */
300 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
301 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
305 #define TEST_TYPE(X) \
307 if (!strncmp(X, "float", sizeof("float"))) \
308 return TOKEN_FLOAT; \
309 if (!strncmp(X, "vector", sizeof("vector"))) \
310 return TOKEN_VECTOR; \
311 if (!strncmp(X, "string", sizeof("string"))) \
312 return TOKEN_STRING; \
313 if (!strncmp(X, "entity", sizeof("entity"))) \
314 return TOKEN_ENTITY; \
315 if (!strncmp(X, "void" , sizeof("void"))) \
319 TEST_TYPE(file->lastok);
321 /* try the hashtable for typedefs? */
322 if (typedef_find(file->lastok))
323 TEST_TYPE(typedef_find(file->lastok)->name);
331 void lex_reset(struct lex_file *file) {
334 file->length = file->size;
335 fseek(file->file, 0, SEEK_SET);
337 memset(file->peek, 0, sizeof(file->peek ));
338 memset(file->lastok, 0, sizeof(file->lastok));
342 * Include a file into the lexer / parsing process: This really
343 * should check if names are the same to prevent endless include
346 struct lex_file *lex_include(struct lex_file *lex, char *file) {
347 char *find = (char*)file;
349 * strip for "" (quotes) .. they might be part of the current
350 * thing. Or possibly not.
355 while (*find != '"' && *find != '\0')
358 /* strip end "" (quotes) .. if they're actually there */
364 * Dissallow recrusive include: this could easily cause some breakage
367 if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
368 error(ERROR_LEX, "%s:%d Source file cannot include itself\n", lex->name, lex->line-1);
372 FILE *fp = fopen(file, "r");
374 error(ERROR_LEX, "%s:%d Include file `%s` doesn't exist\n", lex->name, lex->line, file);
378 /* must free strdup */