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);
145 return lex_trigraph(file);
149 return lex_digraph (file, ch);
150 case '\n': file->line ++;
156 static int lex_get(struct lex_file *file) {
158 if (!isspace(ch = lex_getch(file)))
161 /* skip over all spaces */
162 while (isspace(ch) && ch != '\n')
163 ch = lex_getch(file);
171 static int lex_skipchr(struct lex_file *file) {
176 lex_addch('\'', file);
178 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
184 lex_addch(lex_getch(file), file);
186 lex_addch('\'', file);
187 lex_addch('\0', file);
195 static int lex_skipstr(struct lex_file *file) {
198 lex_addch('"', file);
200 while ((ch = lex_getch(file)) != '"') {
201 if (ch == '\n' || ch == EOF)
206 lex_addch(lex_inget(file), file);
209 lex_addch('"', file);
210 lex_addch('\0', file);
214 static int lex_skipcmt(struct lex_file *file) {
217 ch = lex_getch(file);
220 lex_addch('/', file);
221 lex_addch('/', file);
223 while ((ch = lex_getch(file)) != '\n') {
226 lex_addch(lex_getch(file), file);
231 lex_addch('\0', file);
240 lex_addch('/', file);
245 while ((ch = lex_getch(file)) != '*') {
247 return error(ERROR_LEX, "malformatted comment at line", "");
252 } while ((ch = lex_getch(file)) != '/');
254 lex_addch('/', file);
255 lex_addch('\0', file);
260 static int lex_getsource(struct lex_file *file) {
261 int ch = lex_get(file);
263 /* skip char/string/comment */
265 case '\'': return lex_skipchr(file);
266 case '"': return lex_skipstr(file);
267 case '/': return lex_skipcmt(file);
273 int lex_token(struct lex_file *file) {
274 int ch = lex_getsource(file);
277 /* valid identifier */
278 if (ch > 0 && (ch == '_' || isalpha(ch))) {
280 while (ch > 0 && ch != ' ' && ch != '(' && ch != '\n' && ch != ';') {
282 ch = lex_getsource(file);
285 lex_addch('\0', file);
287 /* look inside the table for a keyword .. */
288 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
289 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
293 #define TEST_TYPE(X) \
295 if (!strncmp(X, "float", sizeof("float"))) \
296 return TOKEN_FLOAT; \
297 if (!strncmp(X, "vector", sizeof("vector"))) \
298 return TOKEN_VECTOR; \
299 if (!strncmp(X, "string", sizeof("string"))) \
300 return TOKEN_STRING; \
301 if (!strncmp(X, "entity", sizeof("entity"))) \
302 return TOKEN_ENTITY; \
303 if (!strncmp(X, "void" , sizeof("void"))) \
307 TEST_TYPE(file->lastok);
309 /* try the hashtable for typedefs? */
310 if (typedef_find(file->lastok))
311 TEST_TYPE(typedef_find(file->lastok)->name);
319 void lex_reset(struct lex_file *file) {
322 file->length = file->size;
323 fseek(file->file, 0, SEEK_SET);
325 memset(file->peek, 0, sizeof(file->peek ));
326 memset(file->lastok, 0, sizeof(file->lastok));