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);
142 return lex_trigraph(file);
143 if (ch == '<' || ch == ':' || ch == '%')
144 return lex_digraph (file, ch);
149 static int lex_get(struct lex_file *file) {
151 if (!isspace(ch = lex_getch(file)))
154 /* skip over all spaces */
155 while (isspace(ch) && ch != '\n')
156 ch = lex_getch(file);
164 static int lex_skipchr(struct lex_file *file) {
169 lex_addch('\'', file);
171 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
177 lex_addch(lex_getch(file), file);
179 lex_addch('\'', file);
180 lex_addch('\0', file);
188 static int lex_skipstr(struct lex_file *file) {
191 lex_addch('"', file);
193 while ((ch = lex_getch(file)) != '"') {
194 if (ch == '\n' || ch == EOF)
199 lex_addch(lex_inget(file), file);
202 lex_addch('"', file);
203 lex_addch('\0', file);
207 static int lex_skipcmt(struct lex_file *file) {
210 ch = lex_getch(file);
213 lex_addch('/', file);
214 lex_addch('/', file);
216 while ((ch = lex_getch(file)) != '\n') {
219 lex_addch(lex_getch(file), file);
224 lex_addch('\0', file);
233 lex_addch('/', file);
238 while ((ch = lex_getch(file)) != '*') {
240 return error(ERROR_LEX, "malformatted comment at line", "");
245 } while ((ch = lex_getch(file)) != '/');
247 lex_addch('/', file);
248 lex_addch('\0', file);
253 static int lex_getsource(struct lex_file *file) {
254 int ch = lex_get(file);
256 /* skip char/string/comment */
258 case '\'': return lex_skipchr(file);
259 case '"': return lex_skipstr(file);
260 case '/': return lex_skipcmt(file);
265 int lex_token(struct lex_file *file) {
266 int ch = lex_getsource(file);
269 /* valid identifier */
270 if (ch > 0 && (ch == '_' || isalpha(ch))) {
272 while (ch > 0 && (isalpha(ch) || ch == '_')) {
274 ch = lex_getsource(file);
277 lex_addch('\0', file);
279 /* look inside the table for a keyword .. */
280 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
281 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
285 #define TEST_TYPE(X) \
287 if (!strncmp(X, "float", sizeof("float"))) \
288 return TOKEN_FLOAT; \
289 if (!strncmp(X, "vector", sizeof("vector"))) \
290 return TOKEN_VECTOR; \
291 if (!strncmp(X, "string", sizeof("string"))) \
292 return TOKEN_STRING; \
293 if (!strncmp(X, "entity", sizeof("entity"))) \
294 return TOKEN_ENTITY; \
295 if (!strncmp(X, "void" , sizeof("void"))) \
299 TEST_TYPE(file->lastok);
301 /* try the hashtable for typedefs? */
302 if (typedef_find(file->lastok))
303 TEST_TYPE(typedef_find(file->lastok)->name);
310 void lex_reset(struct lex_file *file) {
313 file->length = file->size;
314 fseek(file->file, 0, SEEK_SET);
316 memset(file->peek, 0, sizeof(file->peek ));
317 memset(file->lastok, 0, sizeof(file->lastok));