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));
44 fseek(lex->file, 0, SEEK_END);
45 lex->length = ftell(lex->file);
46 lex->size = lex->length; /* copy, this is never changed */
47 fseek(lex->file, 0, SEEK_SET);
50 memset(lex->peek, 0, sizeof(lex->peek));
55 void lex_close(struct lex_file *file) {
58 fclose(file->file); /* may already be closed */
62 static void lex_addch(int ch, struct lex_file *file) {
63 if (file->current < sizeof(file->lastok)-1)
64 file->lastok[file->current++] = (char)ch;
65 if (file->current == sizeof(file->lastok)-1)
66 file->lastok[file->current] = (char)'\0';
68 static inline void lex_clear(struct lex_file *file) {
73 * read in inget/unget character from a lexer stream.
74 * This doesn't play with file streams, the lexer has
75 * it's own internal state for this.
77 static int lex_inget(struct lex_file *file) {
80 return file->peek[--file->last];
81 return fgetc(file->file);
83 static void lex_unget(int ch, struct lex_file *file) {
84 if (file->last < sizeof(file->peek))
85 file->peek[file->last++] = ch;
90 * This is trigraph and digraph support, a feature not qc compiler
91 * supports. Moving up in this world!
93 static int lex_trigraph(struct lex_file *file) {
95 if ((ch = lex_inget(file)) != '?') {
100 ch = lex_inget(file);
102 case '(' : return '[' ;
103 case ')' : return ']' ;
104 case '/' : return '\\';
105 case '\'': return '^' ;
106 case '<' : return '{' ;
107 case '>' : return '}' ;
108 case '!' : return '|' ;
109 case '-' : return '~' ;
110 case '=' : return '#' ;
112 lex_unget('?', file);
113 lex_unget(ch , file);
118 static int lex_digraph(struct lex_file *file, int first) {
119 int ch = lex_inget(file);
122 if (ch == '%') return '{';
123 if (ch == ':') return '[';
126 if (ch == '>') return '}';
127 if (ch == ':') return '#';
130 if (ch == '>') return ']';
138 static int lex_getch(struct lex_file *file) {
139 int ch = lex_inget(file);
141 return lex_trigraph(file);
142 if (ch == '<' || ch == ':' || ch == '%')
143 return lex_digraph (file, ch);
148 static int lex_get(struct lex_file *file) {
150 if (!isspace(ch = lex_getch(file)))
153 /* skip over all spaces */
154 while (isspace(ch) && ch != '\n')
155 ch = lex_getch(file);
163 static int lex_skipchr(struct lex_file *file) {
168 lex_addch('\'', file);
170 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
176 lex_addch(lex_getch(file), file);
178 lex_addch('\'', file);
179 lex_addch('\0', file);
187 static int lex_skipstr(struct lex_file *file) {
190 lex_addch('"', file);
192 while ((ch = lex_getch(file)) != '"') {
193 if (ch == '\n' || ch == EOF)
198 lex_addch(lex_inget(file), file);
201 lex_addch('"', file);
202 lex_addch('\0', file);
206 static int lex_skipcmt(struct lex_file *file) {
209 ch = lex_getch(file);
212 lex_addch('/', file);
213 lex_addch('/', file);
215 while ((ch = lex_getch(file)) != '\n') {
218 lex_addch(lex_getch(file), file);
223 lex_addch('\0', file);
232 lex_addch('/', file);
237 while ((ch = lex_getch(file)) != '*') {
239 return error(ERROR_LEX, "malformatted comment at line %d", file->line);
244 } while ((ch = lex_getch(file)) != '/');
246 lex_addch('/', file);
247 lex_addch('\0', file);
252 static int lex_getsource(struct lex_file *file) {
253 int ch = lex_get(file);
255 /* skip char/string/comment */
257 case '\'': return lex_skipchr(file);
258 case '"': return lex_skipstr(file);
259 case '/': return lex_skipcmt(file);
264 int lex_token(struct lex_file *file) {
265 int ch = lex_getsource(file);
268 /* valid identifier */
269 if (ch > 0 && (ch == '_' || isalpha(ch))) {
271 while (ch > 0 && (isalpha(ch) || ch == '_')) {
273 ch = lex_getsource(file);
276 lex_addch('\0', file);
278 /* look inside the table for a keyword .. */
279 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
280 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
284 #define TEST_TYPE(X) \
286 if (!strncmp(X, "float", sizeof("float"))) \
287 return TOKEN_FLOAT; \
288 if (!strncmp(X, "vector", sizeof("vector"))) \
289 return TOKEN_VECTOR; \
290 if (!strncmp(X, "string", sizeof("string"))) \
291 return TOKEN_STRING; \
292 if (!strncmp(X, "entity", sizeof("entity"))) \
293 return TOKEN_ENTITY; \
294 if (!strncmp(X, "void" , sizeof("void"))) \
298 TEST_TYPE(file->lastok);
300 /* try the hashtable for typedefs? */
301 if (typedef_find(file->lastok))
302 TEST_TYPE(typedef_find(file->lastok)->name);
309 void lex_reset(struct lex_file *file) {
312 file->length = file->size;
313 fseek(file->file, 0, SEEK_SET);
315 memset(file->peek, 0, sizeof(file->peek ));
316 memset(file->lastok, 0, sizeof(file->lastok));