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 void lex_init(const char *file, lex_file **set) {
36 lex_file *lex = mem_a(sizeof(lex_file));
40 lex->file = fopen(file, "r");
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(lex_file *file) {
60 fclose(file->file); /* may already be closed */
64 static void lex_addch(int ch, 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 GMQCC_INLINE void lex_clear(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(lex_file *file) {
84 if ((get = file->peek[--file->last]) == '\n')
88 if ((get = fgetc(file->file)) == '\n')
93 static void lex_unget(int ch, lex_file *file) {
94 if (file->last < sizeof(file->peek)) {
97 file->peek[file->last++] = ch;
103 * This is trigraph and digraph support, a feature not qc compiler
104 * supports. Moving up in this world!
106 static int lex_trigraph(lex_file *file) {
108 if ((ch = lex_inget(file)) != '?') {
113 ch = lex_inget(file);
115 case '(' : return '[' ;
116 case ')' : return ']' ;
117 case '/' : return '\\';
118 case '\'': return '^' ;
119 case '<' : return '{' ;
120 case '>' : return '}' ;
121 case '!' : return '|' ;
122 case '-' : return '~' ;
123 case '=' : return '#' ;
125 lex_unget('?', file);
126 lex_unget(ch , file);
130 static int lex_digraph(lex_file *file, int first) {
131 int ch = lex_inget(file);
134 if (ch == '%') return '{';
135 if (ch == ':') return '[';
138 if (ch == '>') return '}';
139 if (ch == ':') return '#';
142 if (ch == '>') return ']';
150 static int lex_getch(lex_file *file) {
151 int ch = lex_inget(file);
153 return lex_trigraph(file);
154 if (ch == '<' || ch == ':' || ch == '%')
155 return lex_digraph(file, ch);
159 static int lex_get(lex_file *file) {
161 if (!isspace(ch = lex_getch(file)))
164 /* skip over all spaces */
165 while (isspace(ch) && ch != '\n')
166 ch = lex_getch(file);
174 static int lex_skipchr(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(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(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(file, ERROR_LEX, "malformatted comment");
255 } while ((ch = lex_getch(file)) != '/');
257 lex_addch('/', file);
258 lex_addch('\0', file);
263 static int lex_getsource(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);
276 int lex_token(lex_file *file) {
277 int ch = lex_getsource(file);
280 /* valid identifier */
281 if (ch > 0 && (ch == '_' || isalpha(ch))) {
284 while (ch > 0 && (ch == '_' || isalpha(ch))) {
286 ch = lex_getsource(file);
289 lex_addch('\0', file);
291 /* look inside the table for a keyword .. */
292 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
293 if (!strncmp(file->lastok, lex_keywords[it], strlen(lex_keywords[it])))
297 #define TEST_TYPE(X) \
299 if (!strncmp(X, "float", sizeof("float"))) \
300 return TOKEN_FLOAT; \
301 if (!strncmp(X, "vector", sizeof("vector"))) \
302 return TOKEN_VECTOR; \
303 if (!strncmp(X, "string", sizeof("string"))) \
304 return TOKEN_STRING; \
305 if (!strncmp(X, "entity", sizeof("entity"))) \
306 return TOKEN_ENTITY; \
307 if (!strncmp(X, "void" , sizeof("void"))) \
311 TEST_TYPE(file->lastok);
313 /* try the hashtable for typedefs? */
314 if (typedef_find(file->lastok))
315 TEST_TYPE(typedef_find(file->lastok)->name);
320 return (ch != ' ') ? ch : lex_token(file);
323 void lex_reset(lex_file *file) {
326 file->length = file->size;
327 fseek(file->file, 0, SEEK_SET);
329 memset(file->peek, 0, sizeof(file->peek ));
330 memset(file->lastok, 0, sizeof(file->lastok));
333 void lex_parse(lex_file *file) {
335 parse_gen(file); /* run parser */
339 * Include a file into the lexer / parsing process: This really
340 * should check if names are the same to prevent endless include
343 lex_file *lex_include(lex_file *lex, const char *file) {
344 lex_file *set = NULL;
347 if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
348 error(lex, ERROR_LEX, "Source file cannot include itself\n");
351 lex_init(file, &set);