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);
52 memset(lex->peek, 0, sizeof(lex->peek));
56 void lex_close(struct lex_file *file) {
59 fclose(file->file); /* may already be closed */
63 static void lex_addch(int ch, struct lex_file *file) {
64 if (file->current < sizeof(file->lastok)-1)
65 file->lastok[file->current++] = (char)ch;
66 if (file->current == sizeof(file->lastok)-1)
67 file->lastok[file->current] = (char)'\0';
69 static inline void lex_clear(struct lex_file *file) {
74 * read in inget/unget character from a lexer stream.
75 * This doesn't play with file streams, the lexer has
76 * it's own internal state for this.
78 static int lex_inget(struct lex_file *file) {
81 return file->peek[--file->last];
82 return fgetc(file->file);
84 static void lex_unget(int ch, struct lex_file *file) {
85 if (file->last < sizeof(file->peek))
86 file->peek[file->last++] = ch;
91 * This is trigraph and digraph support, a feature not qc compiler
92 * supports. Moving up in this world!
94 static int lex_trigraph(struct lex_file *file) {
96 if ((ch = lex_inget(file)) != '?') {
101 ch = lex_inget(file);
103 case '(' : return '[' ;
104 case ')' : return ']' ;
105 case '/' : return '\\';
106 case '\'': return '^' ;
107 case '<' : return '{' ;
108 case '>' : return '}' ;
109 case '!' : return '|' ;
110 case '-' : return '~' ;
111 case '=' : return '#' ;
113 lex_unget('?', file);
114 lex_unget(ch , file);
119 static int lex_digraph(struct lex_file *file, int first) {
120 int ch = lex_inget(file);
123 if (ch == '%') return '{';
124 if (ch == ':') return '[';
127 if (ch == '>') return '}';
128 if (ch == ':') return '#';
131 if (ch == '>') return ']';
139 static int lex_getch(struct lex_file *file) {
140 int ch = lex_inget(file);
144 return lex_trigraph(file);
148 return lex_digraph (file, ch);
149 case '\n': file->line ++;
155 static int lex_get(struct lex_file *file) {
157 if (!isspace(ch = lex_getch(file)))
160 /* skip over all spaces */
161 while (isspace(ch) && ch != '\n')
162 ch = lex_getch(file);
170 static int lex_skipchr(struct lex_file *file) {
175 lex_addch('\'', file);
177 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
183 lex_addch(lex_getch(file), file);
185 lex_addch('\'', file);
186 lex_addch('\0', file);
194 static int lex_skipstr(struct lex_file *file) {
197 lex_addch('"', file);
199 while ((ch = lex_getch(file)) != '"') {
200 if (ch == '\n' || ch == EOF)
205 lex_addch(lex_inget(file), file);
208 lex_addch('"', file);
209 lex_addch('\0', file);
213 static int lex_skipcmt(struct lex_file *file) {
216 ch = lex_getch(file);
219 lex_addch('/', file);
220 lex_addch('/', file);
222 while ((ch = lex_getch(file)) != '\n') {
225 lex_addch(lex_getch(file), file);
230 lex_addch('\0', file);
239 lex_addch('/', file);
244 while ((ch = lex_getch(file)) != '*') {
246 return error(ERROR_LEX, "malformatted comment at line", "");
251 } while ((ch = lex_getch(file)) != '/');
253 lex_addch('/', file);
254 lex_addch('\0', file);
259 static int lex_getsource(struct lex_file *file) {
260 int ch = lex_get(file);
262 /* skip char/string/comment */
264 case '\'': return lex_skipchr(file);
265 case '"': return lex_skipstr(file);
266 case '/': return lex_skipcmt(file);
272 int lex_token(struct lex_file *file) {
273 int ch = lex_getsource(file);
276 /* valid identifier */
277 if (ch > 0 && (ch == '_' || isalpha(ch))) {
279 while (ch > 0 && (isalpha(ch) || ch == '_')) {
281 ch = lex_getsource(file);
284 lex_addch('\0', file);
286 /* look inside the table for a keyword .. */
287 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
288 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
292 #define TEST_TYPE(X) \
294 if (!strncmp(X, "float", sizeof("float"))) \
295 return TOKEN_FLOAT; \
296 if (!strncmp(X, "vector", sizeof("vector"))) \
297 return TOKEN_VECTOR; \
298 if (!strncmp(X, "string", sizeof("string"))) \
299 return TOKEN_STRING; \
300 if (!strncmp(X, "entity", sizeof("entity"))) \
301 return TOKEN_ENTITY; \
302 if (!strncmp(X, "void" , sizeof("void"))) \
306 TEST_TYPE(file->lastok);
308 /* try the hashtable for typedefs? */
309 if (typedef_find(file->lastok))
310 TEST_TYPE(typedef_find(file->lastok)->name);
318 void lex_reset(struct lex_file *file) {
321 file->length = file->size;
322 fseek(file->file, 0, SEEK_SET);
324 memset(file->peek, 0, sizeof(file->peek ));
325 memset(file->lastok, 0, sizeof(file->lastok));