]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.c
Better linecounting for pragmas; support for prgama file(filename)
[xonotic/gmqcc.git] / lexer.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5
6 #include "gmqcc.h"
7 #include "lexer.h"
8
9 char* *lex_filenames;
10
11 void lexerror(lex_file *lex, const char *fmt, ...)
12 {
13         va_list ap;
14
15         va_start(ap, fmt);
16         if (lex)
17         con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
18     else
19         con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
20         va_end(ap);
21 }
22
23 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
24 {
25         va_list ap;
26         int lvl = LVL_WARNING;
27
28     if (!OPTS_WARN(warntype))
29         return false;
30
31     if (opts_werror)
32             lvl = LVL_ERROR;
33
34         va_start(ap, fmt);
35     con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
36         va_end(ap);
37
38         return opts_werror;
39 }
40
41
42 #if 0
43 token* token_new()
44 {
45     token *tok = (token*)mem_a(sizeof(token));
46     if (!tok)
47         return NULL;
48     memset(tok, 0, sizeof(*tok));
49     return tok;
50 }
51
52 void token_delete(token *self)
53 {
54     if (self->next && self->next->prev == self)
55         self->next->prev = self->prev;
56     if (self->prev && self->prev->next == self)
57         self->prev->next = self->next;
58     MEM_VECTOR_CLEAR(self, value);
59     mem_d(self);
60 }
61
62 token* token_copy(const token *cp)
63 {
64     token* self = token_new();
65     if (!self)
66         return NULL;
67     /* copy the value */
68     self->value_alloc = cp->value_count + 1;
69     self->value_count = cp->value_count;
70     self->value = (char*)mem_a(self->value_alloc);
71     if (!self->value) {
72         mem_d(self);
73         return NULL;
74     }
75     memcpy(self->value, cp->value, cp->value_count);
76     self->value[self->value_alloc-1] = 0;
77
78     /* rest */
79     self->ctx = cp->ctx;
80     self->ttype = cp->ttype;
81     memcpy(&self->constval, &cp->constval, sizeof(self->constval));
82     return self;
83 }
84
85 void token_delete_all(token *t)
86 {
87     token *n;
88
89     do {
90         n = t->next;
91         token_delete(t);
92         t = n;
93     } while(t);
94 }
95
96 token* token_copy_all(const token *cp)
97 {
98     token *cur;
99     token *out;
100
101     out = cur = token_copy(cp);
102     if (!out)
103         return NULL;
104
105     while (cp->next) {
106         cp = cp->next;
107         cur->next = token_copy(cp);
108         if (!cur->next) {
109             token_delete_all(out);
110             return NULL;
111         }
112         cur->next->prev = cur;
113         cur = cur->next;
114     }
115
116     return out;
117 }
118 #else
119 static void lex_token_new(lex_file *lex)
120 {
121 #if 0
122     if (lex->tok)
123         token_delete(lex->tok);
124     lex->tok = token_new();
125 #else
126     if (lex->tok.value)
127         vec_shrinkto(lex->tok.value, 0);
128     lex->tok.constval.t  = 0;
129     lex->tok.ctx.line = lex->sline;
130     lex->tok.ctx.file = lex->name;
131 #endif
132 }
133 #endif
134
135 lex_file* lex_open(const char *file)
136 {
137     lex_file *lex;
138     FILE *in = util_fopen(file, "rb");
139
140     if (!in) {
141         lexerror(NULL, "open failed: '%s'\n", file);
142         return NULL;
143     }
144
145     lex = (lex_file*)mem_a(sizeof(*lex));
146     if (!lex) {
147         fclose(in);
148         lexerror(NULL, "out of memory\n");
149         return NULL;
150     }
151
152     memset(lex, 0, sizeof(*lex));
153
154     lex->file = in;
155     lex->name = util_strdup(file);
156     lex->line = 1; /* we start counting at 1 */
157
158     lex->peekpos = 0;
159     lex->eof = false;
160
161     vec_push(lex_filenames, lex->name);
162     return lex;
163 }
164
165 lex_file* lex_open_string(const char *str, size_t len, const char *name)
166 {
167     lex_file *lex;
168
169     lex = (lex_file*)mem_a(sizeof(*lex));
170     if (!lex) {
171         lexerror(NULL, "out of memory\n");
172         return NULL;
173     }
174
175     memset(lex, 0, sizeof(*lex));
176
177     lex->file = NULL;
178     lex->open_string        = str;
179     lex->open_string_length = len;
180     lex->open_string_pos    = 0;
181
182     lex->name = util_strdup(name ? name : "<string-source>");
183     lex->line = 1; /* we start counting at 1 */
184
185     lex->peekpos = 0;
186     lex->eof = false;
187
188     vec_push(lex_filenames, lex->name);
189
190     return lex;
191 }
192
193 void lex_cleanup(void)
194 {
195     size_t i;
196     for (i = 0; i < vec_size(lex_filenames); ++i)
197         mem_d(lex_filenames[i]);
198     vec_free(lex_filenames);
199 }
200
201 void lex_close(lex_file *lex)
202 {
203     size_t i;
204     for (i = 0; i < vec_size(lex->frames); ++i)
205         mem_d(lex->frames[i].name);
206     vec_free(lex->frames);
207
208     if (lex->modelname)
209         vec_free(lex->modelname);
210
211     if (lex->file)
212         fclose(lex->file);
213 #if 0
214     if (lex->tok)
215         token_delete(lex->tok);
216 #else
217     vec_free(lex->tok.value);
218 #endif
219     /* mem_d(lex->name); collected in lex_filenames */
220     mem_d(lex);
221 }
222
223 static int lex_fgetc(lex_file *lex)
224 {
225     if (lex->file)
226         return fgetc(lex->file);
227     if (lex->open_string) {
228         if (lex->open_string_pos >= lex->open_string_length)
229             return EOF;
230         return lex->open_string[lex->open_string_pos++];
231     }
232     return EOF;
233 }
234
235 /* Get or put-back data
236  * The following to functions do NOT understand what kind of data they
237  * are working on.
238  * The are merely wrapping get/put in order to count line numbers.
239  */
240 static void lex_ungetch(lex_file *lex, int ch);
241 static int lex_try_trigraph(lex_file *lex, int old)
242 {
243     int c2, c3;
244     c2 = lex_fgetc(lex);
245     if (c2 != '?') {
246         lex_ungetch(lex, c2);
247         return old;
248     }
249
250     c3 = lex_fgetc(lex);
251     switch (c3) {
252         case '=': return '#';
253         case '/': return '\\';
254         case '\'': return '^';
255         case '(': return '[';
256         case ')': return ']';
257         case '!': return '|';
258         case '<': return '{';
259         case '>': return '}';
260         case '-': return '~';
261         default:
262             lex_ungetch(lex, c3);
263             lex_ungetch(lex, c2);
264             return old;
265     }
266 }
267
268 static int lex_try_digraph(lex_file *lex, int ch)
269 {
270     int c2;
271     c2 = lex_fgetc(lex);
272     if      (ch == '<' && c2 == ':')
273         return '[';
274     else if (ch == ':' && c2 == '>')
275         return ']';
276     else if (ch == '<' && c2 == '%')
277         return '{';
278     else if (ch == '%' && c2 == '>')
279         return '}';
280     else if (ch == '%' && c2 == ':')
281         return '#';
282     lex_ungetch(lex, c2);
283     return ch;
284 }
285
286 static int lex_getch(lex_file *lex)
287 {
288     int ch;
289
290     if (lex->peekpos) {
291         lex->peekpos--;
292         if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
293             lex->line++;
294         return lex->peek[lex->peekpos];
295     }
296
297     ch = lex_fgetc(lex);
298     if (!lex->push_line && ch == '\n')
299         lex->line++;
300     else if (ch == '?')
301         return lex_try_trigraph(lex, ch);
302     else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
303         return lex_try_digraph(lex, ch);
304     return ch;
305 }
306
307 static void lex_ungetch(lex_file *lex, int ch)
308 {
309     lex->peek[lex->peekpos++] = ch;
310     if (!lex->push_line && ch == '\n')
311         lex->line--;
312 }
313
314 /* classify characters
315  * some additions to the is*() functions of ctype.h
316  */
317
318 /* Idents are alphanumberic, but they start with alpha or _ */
319 static bool isident_start(int ch)
320 {
321     return isalpha(ch) || ch == '_';
322 }
323
324 static bool isident(int ch)
325 {
326     return isident_start(ch) || isdigit(ch);
327 }
328
329 /* isxdigit_only is used when we already know it's not a digit
330  * and want to see if it's a hex digit anyway.
331  */
332 static bool isxdigit_only(int ch)
333 {
334     return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
335 }
336
337 /* Append a character to the token buffer */
338 static void lex_tokench(lex_file *lex, int ch)
339 {
340     vec_push(lex->tok.value, ch);
341 }
342
343 /* Append a trailing null-byte */
344 static void lex_endtoken(lex_file *lex)
345 {
346     vec_push(lex->tok.value, 0);
347     vec_shrinkby(lex->tok.value, 1);
348 }
349
350 static bool lex_try_pragma(lex_file *lex)
351 {
352     int ch;
353     char *pragma  = NULL;
354     char *command = NULL;
355     char *param   = NULL;
356     size_t line;
357
358     if (lex->flags.preprocessing)
359         return false;
360
361     line = lex->line;
362
363     ch = lex_getch(lex);
364     if (ch != '#') {
365         lex_ungetch(lex, ch);
366         return false;
367     }
368
369     for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
370         vec_push(pragma, ch);
371     vec_push(pragma, 0);
372
373     if (ch != ' ' || strcmp(pragma, "pragma")) {
374         lex_ungetch(lex, ch);
375         goto unroll;
376     }
377
378     for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
379         vec_push(command, ch);
380     vec_push(command, 0);
381
382     if (ch != '(') {
383         lex_ungetch(lex, ch);
384         goto unroll;
385     }
386
387     for (ch = lex_getch(lex); vec_size(param) < 32 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
388         vec_push(param, ch);
389     vec_push(param, 0);
390
391     if (ch != ')') {
392         lex_ungetch(lex, ch);
393         goto unroll;
394     }
395
396     if (!strcmp(command, "push")) {
397         if (!strcmp(param, "line")) {
398             lex->push_line++;
399             --line;
400         }
401         else
402             goto unroll;
403     }
404     else if (!strcmp(command, "pop")) {
405         if (!strcmp(param, "line")) {
406             if (lex->push_line)
407                 lex->push_line--;
408             --line;
409         }
410         else
411             goto unroll;
412     }
413     else if (!strcmp(command, "file")) {
414         lex->name = util_strdup(param);
415         vec_push(lex_filenames, lex->name);
416     }
417     else
418         goto unroll;
419
420     lex->line = line;
421     while (ch != '\n')
422         ch = lex_getch(lex);
423     return true;
424
425 unroll:
426     if (command) {
427         vec_pop(command);
428         while (vec_size(command)) {
429             lex_ungetch(lex, vec_last(command));
430             vec_pop(command);
431         }
432         vec_free(command);
433     }
434     if (command) {
435         vec_pop(command);
436         while (vec_size(command)) {
437             lex_ungetch(lex, vec_last(command));
438             vec_pop(command);
439         }
440         vec_free(command);
441     }
442     if (pragma) {
443         vec_pop(pragma);
444         while (vec_size(pragma)) {
445             lex_ungetch(lex, vec_last(pragma));
446             vec_pop(pragma);
447         }
448         vec_free(pragma);
449     }
450     lex_ungetch(lex, '#');
451
452     lex->line = line;
453     return false;
454 }
455
456 /* Skip whitespace and comments and return the first
457  * non-white character.
458  * As this makes use of the above getch() ungetch() functions,
459  * we don't need to care at all about line numbering anymore.
460  *
461  * In theory, this function should only be used at the beginning
462  * of lexing, or when we *know* the next character is part of the token.
463  * Otherwise, if the parser throws an error, the linenumber may not be
464  * the line of the error, but the line of the next token AFTER the error.
465  *
466  * This is currently only problematic when using c-like string-continuation,
467  * since comments and whitespaces are allowed between 2 such strings.
468  * Example:
469 printf(   "line one\n"
470 // A comment
471           "A continuation of the previous string"
472 // This line is skipped
473       , foo);
474
475  * In this case, if the parse decides it didn't actually want a string,
476  * and uses lex->line to print an error, it will show the ', foo);' line's
477  * linenumber.
478  *
479  * On the other hand, the parser is supposed to remember the line of the next
480  * token's beginning. In this case we would want skipwhite() to be called
481  * AFTER reading a token, so that the parser, before reading the NEXT token,
482  * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
483  *
484  * THIS SOLUTION
485  *    here is to store the line of the first character after skipping
486  *    the initial whitespace in lex->sline, this happens in lex_do.
487  */
488 static int lex_skipwhite(lex_file *lex)
489 {
490     int ch = 0;
491     bool haswhite = false;
492
493     do
494     {
495         ch = lex_getch(lex);
496         while (ch != EOF && isspace(ch)) {
497             if (ch == '\n') {
498                 if (lex_try_pragma(lex)) {
499                     ch = lex_getch(lex);
500                     continue;
501                 }
502             }
503             if (lex->flags.preprocessing) {
504                 if (ch == '\n') {
505                     /* end-of-line */
506                     /* see if there was whitespace first */
507                     if (haswhite) { /* (vec_size(lex->tok.value)) { */
508                         lex_ungetch(lex, ch);
509                         lex_endtoken(lex);
510                         return TOKEN_WHITE;
511                     }
512                     /* otherwise return EOL */
513                     return TOKEN_EOL;
514                 }
515                 haswhite = true;
516                 lex_tokench(lex, ch);
517             }
518             ch = lex_getch(lex);
519         }
520
521         if (ch == '/') {
522             ch = lex_getch(lex);
523             if (ch == '/')
524             {
525                 /* one line comment */
526                 ch = lex_getch(lex);
527
528                 if (lex->flags.preprocessing) {
529                     haswhite = true;
530                     /*
531                     lex_tokench(lex, '/');
532                     lex_tokench(lex, '/');
533                     */
534                     lex_tokench(lex, ' ');
535                     lex_tokench(lex, ' ');
536                 }
537
538                 while (ch != EOF && ch != '\n') {
539                     if (lex->flags.preprocessing)
540                         lex_tokench(lex, ' '); /* ch); */
541                     ch = lex_getch(lex);
542                 }
543                 if (lex->flags.preprocessing) {
544                     lex_ungetch(lex, '\n');
545                     lex_endtoken(lex);
546                     return TOKEN_WHITE;
547                 }
548                 continue;
549             }
550             if (ch == '*')
551             {
552                 /* multiline comment */
553                 if (lex->flags.preprocessing) {
554                     haswhite = true;
555                     /*
556                     lex_tokench(lex, '/');
557                     lex_tokench(lex, '*');
558                     */
559                     lex_tokench(lex, ' ');
560                     lex_tokench(lex, ' ');
561                 }
562
563                 while (ch != EOF)
564                 {
565                     ch = lex_getch(lex);
566                     if (ch == '*') {
567                         ch = lex_getch(lex);
568                         if (ch == '/') {
569                             if (lex->flags.preprocessing) {
570                                 /*
571                                 lex_tokench(lex, '*');
572                                 lex_tokench(lex, '/');
573                                 */
574                                 lex_tokench(lex, ' ');
575                                 lex_tokench(lex, ' ');
576                             }
577                             break;
578                         }
579                     }
580                     if (lex->flags.preprocessing) {
581                         lex_tokench(lex, ' '); /* ch); */
582                     }
583                 }
584                 ch = ' '; /* cause TRUE in the isspace check */
585                 continue;
586             }
587             /* Otherwise roll back to the slash and break out of the loop */
588             lex_ungetch(lex, ch);
589             ch = '/';
590             break;
591         }
592     } while (ch != EOF && isspace(ch));
593
594     if (haswhite) {
595         lex_endtoken(lex);
596         lex_ungetch(lex, ch);
597         return TOKEN_WHITE;
598     }
599     return ch;
600 }
601
602 /* Get a token */
603 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
604 {
605     int ch;
606
607     ch = lex_getch(lex);
608     while (ch != EOF && isident(ch))
609     {
610         lex_tokench(lex, ch);
611         ch = lex_getch(lex);
612     }
613
614     /* last ch was not an ident ch: */
615     lex_ungetch(lex, ch);
616
617     return true;
618 }
619
620 /* read one ident for the frame list */
621 static int lex_parse_frame(lex_file *lex)
622 {
623     int ch;
624
625     lex_token_new(lex);
626
627     ch = lex_getch(lex);
628     while (ch != EOF && ch != '\n' && isspace(ch))
629         ch = lex_getch(lex);
630
631     if (ch == '\n')
632         return 1;
633
634     if (!isident_start(ch)) {
635         lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
636         return -1;
637     }
638
639     lex_tokench(lex, ch);
640     if (!lex_finish_ident(lex))
641         return -1;
642     lex_endtoken(lex);
643     return 0;
644 }
645
646 /* read a list of $frames */
647 static bool lex_finish_frames(lex_file *lex)
648 {
649     do {
650         size_t i;
651         int    rc;
652         frame_macro m;
653
654         rc = lex_parse_frame(lex);
655         if (rc > 0) /* end of line */
656             return true;
657         if (rc < 0) /* error */
658             return false;
659
660         for (i = 0; i < vec_size(lex->frames); ++i) {
661             if (!strcmp(lex->tok.value, lex->frames[i].name)) {
662                 lex->frames[i].value = lex->framevalue++;
663                 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
664                     return false;
665                 break;
666             }
667         }
668         if (i < vec_size(lex->frames))
669             continue;
670
671         m.value = lex->framevalue++;
672         m.name = util_strdup(lex->tok.value);
673         vec_shrinkto(lex->tok.value, 0);
674         vec_push(lex->frames, m);
675     } while (true);
676 }
677
678 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
679 {
680     int ch = 0;
681
682     while (ch != EOF)
683     {
684         ch = lex_getch(lex);
685         if (ch == quote)
686             return TOKEN_STRINGCONST;
687
688         if (lex->flags.preprocessing && ch == '\\') {
689             lex_tokench(lex, ch);
690             ch = lex_getch(lex);
691             if (ch == EOF) {
692                 lexerror(lex, "unexpected end of file");
693                 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
694                 return (lex->tok.ttype = TOKEN_ERROR);
695             }
696             lex_tokench(lex, ch);
697         }
698         else if (ch == '\\') {
699             ch = lex_getch(lex);
700             if (ch == EOF) {
701                 lexerror(lex, "unexpected end of file");
702                 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
703                 return (lex->tok.ttype = TOKEN_ERROR);
704             }
705
706             switch (ch) {
707             case '\\': break;
708             case '\'': break;
709             case '"':  break;
710             case 'a':  ch = '\a'; break;
711             case 'b':  ch = '\b'; break;
712             case 'r':  ch = '\r'; break;
713             case 'n':  ch = '\n'; break;
714             case 't':  ch = '\t'; break;
715             case 'f':  ch = '\f'; break;
716             case 'v':  ch = '\v'; break;
717             default:
718                 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
719                 /* so we just add the character plus backslash no matter what it actually is */
720                 lex_tokench(lex, '\\');
721             }
722             /* add the character finally */
723             lex_tokench(lex, ch);
724         }
725         else
726             lex_tokench(lex, ch);
727     }
728     lexerror(lex, "unexpected end of file within string constant");
729     lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
730     return (lex->tok.ttype = TOKEN_ERROR);
731 }
732
733 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
734 {
735     bool ishex = false;
736
737     int  ch = lastch;
738
739     /* parse a number... */
740     lex->tok.ttype = TOKEN_INTCONST;
741
742     lex_tokench(lex, ch);
743
744     ch = lex_getch(lex);
745     if (ch != '.' && !isdigit(ch))
746     {
747         if (lastch != '0' || ch != 'x')
748         {
749             /* end of the number or EOF */
750             lex_ungetch(lex, ch);
751             lex_endtoken(lex);
752
753             lex->tok.constval.i = lastch - '0';
754             return lex->tok.ttype;
755         }
756
757         ishex = true;
758     }
759
760     /* EOF would have been caught above */
761
762     if (ch != '.')
763     {
764         lex_tokench(lex, ch);
765         ch = lex_getch(lex);
766         while (isdigit(ch) || (ishex && isxdigit_only(ch)))
767         {
768             lex_tokench(lex, ch);
769             ch = lex_getch(lex);
770         }
771     }
772     /* NOT else, '.' can come from above as well */
773     if (ch == '.' && !ishex)
774     {
775         /* Allow floating comma in non-hex mode */
776         lex->tok.ttype = TOKEN_FLOATCONST;
777         lex_tokench(lex, ch);
778
779         /* continue digits-only */
780         ch = lex_getch(lex);
781         while (isdigit(ch))
782         {
783             lex_tokench(lex, ch);
784             ch = lex_getch(lex);
785         }
786     }
787     /* put back the last character */
788     /* but do not put back the trailing 'f' or a float */
789     if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
790         ch = lex_getch(lex);
791
792     /* generally we don't want words to follow numbers: */
793     if (isident(ch)) {
794         lexerror(lex, "unexpected trailing characters after number");
795         return (lex->tok.ttype = TOKEN_ERROR);
796     }
797     lex_ungetch(lex, ch);
798
799     lex_endtoken(lex);
800     if (lex->tok.ttype == TOKEN_FLOATCONST)
801         lex->tok.constval.f = strtod(lex->tok.value, NULL);
802     else
803         lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
804     return lex->tok.ttype;
805 }
806
807 int lex_do(lex_file *lex)
808 {
809     int ch, nextch;
810
811     lex_token_new(lex);
812 #if 0
813     if (!lex->tok)
814         return TOKEN_FATAL;
815 #endif
816
817     while (true) {
818         ch = lex_skipwhite(lex);
819         if (!lex->flags.mergelines || ch != '\\')
820             break;
821         ch = lex_getch(lex);
822         if (ch != '\n') {
823             lex_ungetch(lex, ch);
824             ch = '\\';
825             break;
826         }
827         /* we reached a linemerge */
828         lex_tokench(lex, '\n');
829         continue;
830     }
831
832     lex->sline = lex->line;
833     lex->tok.ctx.line = lex->sline;
834     lex->tok.ctx.file = lex->name;
835
836     if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
837         return (lex->tok.ttype = ch);
838     }
839
840     if (lex->eof)
841         return (lex->tok.ttype = TOKEN_FATAL);
842
843     if (ch == EOF) {
844         lex->eof = true;
845         return (lex->tok.ttype = TOKEN_EOF);
846     }
847
848     /* modelgen / spiritgen commands */
849     if (ch == '$') {
850         const char *v;
851         size_t frame;
852
853         ch = lex_getch(lex);
854         if (!isident_start(ch)) {
855             lexerror(lex, "hanging '$' modelgen/spritegen command line");
856             return lex_do(lex);
857         }
858         lex_tokench(lex, ch);
859         if (!lex_finish_ident(lex))
860             return (lex->tok.ttype = TOKEN_ERROR);
861         lex_endtoken(lex);
862         /* skip the known commands */
863         v = lex->tok.value;
864
865         if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
866         {
867             /* frame/framesave command works like an enum
868              * similar to fteqcc we handle this in the lexer.
869              * The reason for this is that it is sensitive to newlines,
870              * which the parser is unaware of
871              */
872             if (!lex_finish_frames(lex))
873                  return (lex->tok.ttype = TOKEN_ERROR);
874             return lex_do(lex);
875         }
876
877         if (!strcmp(v, "framevalue"))
878         {
879             ch = lex_getch(lex);
880             while (ch != EOF && isspace(ch) && ch != '\n')
881                 ch = lex_getch(lex);
882
883             if (!isdigit(ch)) {
884                 lexerror(lex, "$framevalue requires an integer parameter");
885                 return lex_do(lex);
886             }
887
888             lex_token_new(lex);
889             lex->tok.ttype = lex_finish_digit(lex, ch);
890             lex_endtoken(lex);
891             if (lex->tok.ttype != TOKEN_INTCONST) {
892                 lexerror(lex, "$framevalue requires an integer parameter");
893                 return lex_do(lex);
894             }
895             lex->framevalue = lex->tok.constval.i;
896             return lex_do(lex);
897         }
898
899         if (!strcmp(v, "framerestore"))
900         {
901             int rc;
902
903             lex_token_new(lex);
904
905             rc = lex_parse_frame(lex);
906
907             if (rc > 0) {
908                 lexerror(lex, "$framerestore requires a framename parameter");
909                 return lex_do(lex);
910             }
911             if (rc < 0)
912                 return (lex->tok.ttype = TOKEN_FATAL);
913
914             v = lex->tok.value;
915             for (frame = 0; frame < vec_size(lex->frames); ++frame) {
916                 if (!strcmp(v, lex->frames[frame].name)) {
917                     lex->framevalue = lex->frames[frame].value;
918                     return lex_do(lex);
919                 }
920             }
921             lexerror(lex, "unknown framename `%s`", v);
922             return lex_do(lex);
923         }
924
925         if (!strcmp(v, "modelname"))
926         {
927             int rc;
928
929             lex_token_new(lex);
930
931             rc = lex_parse_frame(lex);
932
933             if (rc > 0) {
934                 lexerror(lex, "$modelname requires a parameter");
935                 return lex_do(lex);
936             }
937             if (rc < 0)
938                 return (lex->tok.ttype = TOKEN_FATAL);
939
940             v = lex->tok.value;
941             if (lex->modelname) {
942                 frame_macro m;
943                 m.value = lex->framevalue;
944                 m.name = lex->modelname;
945                 lex->modelname = NULL;
946                 vec_push(lex->frames, m);
947             }
948             lex->modelname = lex->tok.value;
949             lex->tok.value = NULL;
950             return lex_do(lex);
951         }
952
953         if (!strcmp(v, "flush"))
954         {
955             size_t frame;
956             for (frame = 0; frame < vec_size(lex->frames); ++frame)
957                 mem_d(lex->frames[frame].name);
958             vec_free(lex->frames);
959             /* skip line (fteqcc does it too) */
960             ch = lex_getch(lex);
961             while (ch != EOF && ch != '\n')
962                 ch = lex_getch(lex);
963             return lex_do(lex);
964         }
965
966         if (!strcmp(v, "cd") ||
967             !strcmp(v, "origin") ||
968             !strcmp(v, "base") ||
969             !strcmp(v, "flags") ||
970             !strcmp(v, "scale") ||
971             !strcmp(v, "skin"))
972         {
973             /* skip line */
974             ch = lex_getch(lex);
975             while (ch != EOF && ch != '\n')
976                 ch = lex_getch(lex);
977             return lex_do(lex);
978         }
979
980         for (frame = 0; frame < vec_size(lex->frames); ++frame) {
981             if (!strcmp(v, lex->frames[frame].name)) {
982                 lex->tok.constval.i = lex->frames[frame].value;
983                 return (lex->tok.ttype = TOKEN_INTCONST);
984             }
985         }
986
987         lexerror(lex, "invalid frame macro");
988         return lex_do(lex);
989     }
990
991     /* single-character tokens */
992     switch (ch)
993     {
994         case '[':
995         case '(':
996             lex_tokench(lex, ch);
997             lex_endtoken(lex);
998             if (lex->flags.noops)
999                 return (lex->tok.ttype = ch);
1000             else
1001                 return (lex->tok.ttype = TOKEN_OPERATOR);
1002         case ')':
1003         case ';':
1004         case ':':
1005         case '{':
1006         case '}':
1007         case ']':
1008
1009         case '#':
1010             lex_tokench(lex, ch);
1011             lex_endtoken(lex);
1012             return (lex->tok.ttype = ch);
1013         default:
1014             break;
1015     }
1016
1017     if (lex->flags.noops)
1018     {
1019         /* Detect characters early which are normally
1020          * operators OR PART of an operator.
1021          */
1022         switch (ch)
1023         {
1024             case '+':
1025             case '-':
1026             case '*':
1027             case '/':
1028             case '<':
1029             case '>':
1030             case '=':
1031             case '&':
1032             case '|':
1033             case '^':
1034             case '~':
1035             case ',':
1036             case '!':
1037                 lex_tokench(lex, ch);
1038                 lex_endtoken(lex);
1039                 return (lex->tok.ttype = ch);
1040             default:
1041                 break;
1042         }
1043
1044         if (ch == '.')
1045         {
1046             lex_tokench(lex, ch);
1047             /* peak ahead once */
1048             nextch = lex_getch(lex);
1049             if (nextch != '.') {
1050                 lex_ungetch(lex, nextch);
1051                 lex_endtoken(lex);
1052                 return (lex->tok.ttype = ch);
1053             }
1054             /* peak ahead again */
1055             nextch = lex_getch(lex);
1056             if (nextch != '.') {
1057                 lex_ungetch(lex, nextch);
1058                 lex_ungetch(lex, nextch);
1059                 lex_endtoken(lex);
1060                 return (lex->tok.ttype = ch);
1061             }
1062             /* fill the token to be "..." */
1063             lex_tokench(lex, ch);
1064             lex_tokench(lex, ch);
1065             lex_endtoken(lex);
1066             return (lex->tok.ttype = TOKEN_DOTS);
1067         }
1068     }
1069
1070     if (ch == ',' || ch == '.') {
1071         lex_tokench(lex, ch);
1072         lex_endtoken(lex);
1073         return (lex->tok.ttype = TOKEN_OPERATOR);
1074     }
1075
1076     if (ch == '+' || ch == '-' || /* ++, --, +=, -=  and -> as well! */
1077         ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1078         ch == '=' || ch == '!' || /* ==, != */
1079         ch == '&' || ch == '|')   /* &&, ||, &=, |= */
1080     {
1081         lex_tokench(lex, ch);
1082
1083         nextch = lex_getch(lex);
1084         if (nextch == ch || nextch == '=') {
1085             lex_tokench(lex, nextch);
1086         } else if (ch == '-' && nextch == '>') {
1087             lex_tokench(lex, nextch);
1088         } else
1089             lex_ungetch(lex, nextch);
1090
1091         lex_endtoken(lex);
1092         return (lex->tok.ttype = TOKEN_OPERATOR);
1093     }
1094
1095     /*
1096     if (ch == '^' || ch == '~' || ch == '!')
1097     {
1098         lex_tokench(lex, ch);
1099         lex_endtoken(lex);
1100         return (lex->tok.ttype = TOKEN_OPERATOR);
1101     }
1102     */
1103
1104     if (ch == '*' || ch == '/') /* *=, /= */
1105     {
1106         lex_tokench(lex, ch);
1107
1108         nextch = lex_getch(lex);
1109         if (nextch == '=') {
1110             lex_tokench(lex, nextch);
1111         } else
1112             lex_ungetch(lex, nextch);
1113
1114         lex_endtoken(lex);
1115         return (lex->tok.ttype = TOKEN_OPERATOR);
1116     }
1117
1118     if (isident_start(ch))
1119     {
1120         const char *v;
1121
1122         lex_tokench(lex, ch);
1123         if (!lex_finish_ident(lex)) {
1124             /* error? */
1125             return (lex->tok.ttype = TOKEN_ERROR);
1126         }
1127         lex_endtoken(lex);
1128         lex->tok.ttype = TOKEN_IDENT;
1129
1130         v = lex->tok.value;
1131         if (!strcmp(v, "void")) {
1132             lex->tok.ttype = TOKEN_TYPENAME;
1133             lex->tok.constval.t = TYPE_VOID;
1134         } else if (!strcmp(v, "int")) {
1135             lex->tok.ttype = TOKEN_TYPENAME;
1136             lex->tok.constval.t = TYPE_INTEGER;
1137         } else if (!strcmp(v, "float")) {
1138             lex->tok.ttype = TOKEN_TYPENAME;
1139             lex->tok.constval.t = TYPE_FLOAT;
1140         } else if (!strcmp(v, "string")) {
1141             lex->tok.ttype = TOKEN_TYPENAME;
1142             lex->tok.constval.t = TYPE_STRING;
1143         } else if (!strcmp(v, "entity")) {
1144             lex->tok.ttype = TOKEN_TYPENAME;
1145             lex->tok.constval.t = TYPE_ENTITY;
1146         } else if (!strcmp(v, "vector")) {
1147             lex->tok.ttype = TOKEN_TYPENAME;
1148             lex->tok.constval.t = TYPE_VECTOR;
1149         } else if (!strcmp(v, "for")  ||
1150                  !strcmp(v, "while")  ||
1151                  !strcmp(v, "do")     ||
1152                  !strcmp(v, "if")     ||
1153                  !strcmp(v, "else")   ||
1154                  !strcmp(v, "local")  ||
1155                  !strcmp(v, "return") ||
1156                  !strcmp(v, "not")    ||
1157                  !strcmp(v, "const"))
1158         {
1159             lex->tok.ttype = TOKEN_KEYWORD;
1160         }
1161         else if (opts_standard != COMPILER_QCC)
1162         {
1163             /* other standards reserve these keywords */
1164             if (!strcmp(v, "switch") ||
1165                 !strcmp(v, "struct") ||
1166                 !strcmp(v, "union")  ||
1167                 !strcmp(v, "break")  ||
1168                 !strcmp(v, "continue") ||
1169                 !strcmp(v, "var"))
1170             {
1171                 lex->tok.ttype = TOKEN_KEYWORD;
1172             }
1173         }
1174
1175         return lex->tok.ttype;
1176     }
1177
1178     if (ch == '"')
1179     {
1180         lex->flags.nodigraphs = true;
1181         if (lex->flags.preprocessing)
1182             lex_tokench(lex, ch);
1183         lex->tok.ttype = lex_finish_string(lex, '"');
1184         if (lex->flags.preprocessing)
1185             lex_tokench(lex, ch);
1186         while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1187         {
1188             /* Allow c style "string" "continuation" */
1189             ch = lex_skipwhite(lex);
1190             if (ch != '"') {
1191                 lex_ungetch(lex, ch);
1192                 break;
1193             }
1194
1195             lex->tok.ttype = lex_finish_string(lex, '"');
1196         }
1197         lex->flags.nodigraphs = false;
1198         lex_endtoken(lex);
1199         return lex->tok.ttype;
1200     }
1201
1202     if (ch == '\'')
1203     {
1204         /* we parse character constants like string,
1205          * but return TOKEN_CHARCONST, or a vector type if it fits...
1206          * Likewise actual unescaping has to be done by the parser.
1207          * The difference is we don't allow 'char' 'continuation'.
1208          */
1209         if (lex->flags.preprocessing)
1210             lex_tokench(lex, ch);
1211         lex->tok.ttype = lex_finish_string(lex, '\'');
1212         if (lex->flags.preprocessing)
1213             lex_tokench(lex, ch);
1214         lex_endtoken(lex);
1215
1216          /* It's a vector if we can successfully scan 3 floats */
1217 #ifdef WIN32
1218         if (sscanf_s(lex->tok.value, " %f %f %f ",
1219                    &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1220 #else
1221         if (sscanf(lex->tok.value, " %f %f %f ",
1222                    &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1223 #endif
1224
1225         {
1226              lex->tok.ttype = TOKEN_VECTORCONST;
1227         }
1228
1229         return lex->tok.ttype;
1230     }
1231
1232     if (isdigit(ch))
1233     {
1234         lex->tok.ttype = lex_finish_digit(lex, ch);
1235         lex_endtoken(lex);
1236         return lex->tok.ttype;
1237     }
1238
1239     lexerror(lex, "unknown token");
1240     return (lex->tok.ttype = TOKEN_ERROR);
1241 }