]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.c
fixup line counting in #pragmas
[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         }
409         else
410             goto unroll;
411     }
412     else
413         goto unroll;
414
415     while (ch != '\n')
416         ch = lex_getch(lex);
417     lex->line = line;
418     return true;
419
420 unroll:
421     if (command) {
422         vec_pop(command);
423         while (vec_size(command)) {
424             lex_ungetch(lex, vec_last(command));
425             vec_pop(command);
426         }
427         vec_free(command);
428     }
429     if (command) {
430         vec_pop(command);
431         while (vec_size(command)) {
432             lex_ungetch(lex, vec_last(command));
433             vec_pop(command);
434         }
435         vec_free(command);
436     }
437     if (pragma) {
438         vec_pop(pragma);
439         while (vec_size(pragma)) {
440             lex_ungetch(lex, vec_last(pragma));
441             vec_pop(pragma);
442         }
443         vec_free(pragma);
444     }
445     lex_ungetch(lex, '#');
446
447     lex->line = line;
448     return false;
449 }
450
451 /* Skip whitespace and comments and return the first
452  * non-white character.
453  * As this makes use of the above getch() ungetch() functions,
454  * we don't need to care at all about line numbering anymore.
455  *
456  * In theory, this function should only be used at the beginning
457  * of lexing, or when we *know* the next character is part of the token.
458  * Otherwise, if the parser throws an error, the linenumber may not be
459  * the line of the error, but the line of the next token AFTER the error.
460  *
461  * This is currently only problematic when using c-like string-continuation,
462  * since comments and whitespaces are allowed between 2 such strings.
463  * Example:
464 printf(   "line one\n"
465 // A comment
466           "A continuation of the previous string"
467 // This line is skipped
468       , foo);
469
470  * In this case, if the parse decides it didn't actually want a string,
471  * and uses lex->line to print an error, it will show the ', foo);' line's
472  * linenumber.
473  *
474  * On the other hand, the parser is supposed to remember the line of the next
475  * token's beginning. In this case we would want skipwhite() to be called
476  * AFTER reading a token, so that the parser, before reading the NEXT token,
477  * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
478  *
479  * THIS SOLUTION
480  *    here is to store the line of the first character after skipping
481  *    the initial whitespace in lex->sline, this happens in lex_do.
482  */
483 static int lex_skipwhite(lex_file *lex)
484 {
485     int ch = 0;
486     bool haswhite = false;
487
488     do
489     {
490         ch = lex_getch(lex);
491         while (ch != EOF && isspace(ch)) {
492             if (ch == '\n') {
493                 if (lex_try_pragma(lex)) {
494                     ch = lex_getch(lex);
495                     continue;
496                 }
497             }
498             if (lex->flags.preprocessing) {
499                 if (ch == '\n') {
500                     /* end-of-line */
501                     /* see if there was whitespace first */
502                     if (haswhite) { /* (vec_size(lex->tok.value)) { */
503                         lex_ungetch(lex, ch);
504                         lex_endtoken(lex);
505                         return TOKEN_WHITE;
506                     }
507                     /* otherwise return EOL */
508                     return TOKEN_EOL;
509                 }
510                 haswhite = true;
511                 lex_tokench(lex, ch);
512             }
513             ch = lex_getch(lex);
514         }
515
516         if (ch == '/') {
517             ch = lex_getch(lex);
518             if (ch == '/')
519             {
520                 /* one line comment */
521                 ch = lex_getch(lex);
522
523                 if (lex->flags.preprocessing) {
524                     haswhite = true;
525                     /*
526                     lex_tokench(lex, '/');
527                     lex_tokench(lex, '/');
528                     */
529                     lex_tokench(lex, ' ');
530                     lex_tokench(lex, ' ');
531                 }
532
533                 while (ch != EOF && ch != '\n') {
534                     if (lex->flags.preprocessing)
535                         lex_tokench(lex, ' '); /* ch); */
536                     ch = lex_getch(lex);
537                 }
538                 if (lex->flags.preprocessing) {
539                     lex_ungetch(lex, '\n');
540                     lex_endtoken(lex);
541                     return TOKEN_WHITE;
542                 }
543                 continue;
544             }
545             if (ch == '*')
546             {
547                 /* multiline comment */
548                 if (lex->flags.preprocessing) {
549                     haswhite = true;
550                     /*
551                     lex_tokench(lex, '/');
552                     lex_tokench(lex, '*');
553                     */
554                     lex_tokench(lex, ' ');
555                     lex_tokench(lex, ' ');
556                 }
557
558                 while (ch != EOF)
559                 {
560                     ch = lex_getch(lex);
561                     if (ch == '*') {
562                         ch = lex_getch(lex);
563                         if (ch == '/') {
564                             if (lex->flags.preprocessing) {
565                                 /*
566                                 lex_tokench(lex, '*');
567                                 lex_tokench(lex, '/');
568                                 */
569                                 lex_tokench(lex, ' ');
570                                 lex_tokench(lex, ' ');
571                             }
572                             break;
573                         }
574                     }
575                     if (lex->flags.preprocessing) {
576                         lex_tokench(lex, ' '); /* ch); */
577                     }
578                 }
579                 ch = ' '; /* cause TRUE in the isspace check */
580                 continue;
581             }
582             /* Otherwise roll back to the slash and break out of the loop */
583             lex_ungetch(lex, ch);
584             ch = '/';
585             break;
586         }
587     } while (ch != EOF && isspace(ch));
588
589     if (haswhite) {
590         lex_endtoken(lex);
591         lex_ungetch(lex, ch);
592         return TOKEN_WHITE;
593     }
594     return ch;
595 }
596
597 /* Get a token */
598 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
599 {
600     int ch;
601
602     ch = lex_getch(lex);
603     while (ch != EOF && isident(ch))
604     {
605         lex_tokench(lex, ch);
606         ch = lex_getch(lex);
607     }
608
609     /* last ch was not an ident ch: */
610     lex_ungetch(lex, ch);
611
612     return true;
613 }
614
615 /* read one ident for the frame list */
616 static int lex_parse_frame(lex_file *lex)
617 {
618     int ch;
619
620     lex_token_new(lex);
621
622     ch = lex_getch(lex);
623     while (ch != EOF && ch != '\n' && isspace(ch))
624         ch = lex_getch(lex);
625
626     if (ch == '\n')
627         return 1;
628
629     if (!isident_start(ch)) {
630         lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
631         return -1;
632     }
633
634     lex_tokench(lex, ch);
635     if (!lex_finish_ident(lex))
636         return -1;
637     lex_endtoken(lex);
638     return 0;
639 }
640
641 /* read a list of $frames */
642 static bool lex_finish_frames(lex_file *lex)
643 {
644     do {
645         size_t i;
646         int    rc;
647         frame_macro m;
648
649         rc = lex_parse_frame(lex);
650         if (rc > 0) /* end of line */
651             return true;
652         if (rc < 0) /* error */
653             return false;
654
655         for (i = 0; i < vec_size(lex->frames); ++i) {
656             if (!strcmp(lex->tok.value, lex->frames[i].name)) {
657                 lex->frames[i].value = lex->framevalue++;
658                 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
659                     return false;
660                 break;
661             }
662         }
663         if (i < vec_size(lex->frames))
664             continue;
665
666         m.value = lex->framevalue++;
667         m.name = util_strdup(lex->tok.value);
668         vec_shrinkto(lex->tok.value, 0);
669         vec_push(lex->frames, m);
670     } while (true);
671 }
672
673 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
674 {
675     int ch = 0;
676
677     while (ch != EOF)
678     {
679         ch = lex_getch(lex);
680         if (ch == quote)
681             return TOKEN_STRINGCONST;
682
683         if (lex->flags.preprocessing && ch == '\\') {
684             lex_tokench(lex, ch);
685             ch = lex_getch(lex);
686             if (ch == EOF) {
687                 lexerror(lex, "unexpected end of file");
688                 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
689                 return (lex->tok.ttype = TOKEN_ERROR);
690             }
691             lex_tokench(lex, ch);
692         }
693         else if (ch == '\\') {
694             ch = lex_getch(lex);
695             if (ch == EOF) {
696                 lexerror(lex, "unexpected end of file");
697                 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
698                 return (lex->tok.ttype = TOKEN_ERROR);
699             }
700
701             switch (ch) {
702             case '\\': break;
703             case '\'': break;
704             case '"':  break;
705             case 'a':  ch = '\a'; break;
706             case 'b':  ch = '\b'; break;
707             case 'r':  ch = '\r'; break;
708             case 'n':  ch = '\n'; break;
709             case 't':  ch = '\t'; break;
710             case 'f':  ch = '\f'; break;
711             case 'v':  ch = '\v'; break;
712             default:
713                 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
714                 /* so we just add the character plus backslash no matter what it actually is */
715                 lex_tokench(lex, '\\');
716             }
717             /* add the character finally */
718             lex_tokench(lex, ch);
719         }
720         else
721             lex_tokench(lex, ch);
722     }
723     lexerror(lex, "unexpected end of file within string constant");
724     lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
725     return (lex->tok.ttype = TOKEN_ERROR);
726 }
727
728 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
729 {
730     bool ishex = false;
731
732     int  ch = lastch;
733
734     /* parse a number... */
735     lex->tok.ttype = TOKEN_INTCONST;
736
737     lex_tokench(lex, ch);
738
739     ch = lex_getch(lex);
740     if (ch != '.' && !isdigit(ch))
741     {
742         if (lastch != '0' || ch != 'x')
743         {
744             /* end of the number or EOF */
745             lex_ungetch(lex, ch);
746             lex_endtoken(lex);
747
748             lex->tok.constval.i = lastch - '0';
749             return lex->tok.ttype;
750         }
751
752         ishex = true;
753     }
754
755     /* EOF would have been caught above */
756
757     if (ch != '.')
758     {
759         lex_tokench(lex, ch);
760         ch = lex_getch(lex);
761         while (isdigit(ch) || (ishex && isxdigit_only(ch)))
762         {
763             lex_tokench(lex, ch);
764             ch = lex_getch(lex);
765         }
766     }
767     /* NOT else, '.' can come from above as well */
768     if (ch == '.' && !ishex)
769     {
770         /* Allow floating comma in non-hex mode */
771         lex->tok.ttype = TOKEN_FLOATCONST;
772         lex_tokench(lex, ch);
773
774         /* continue digits-only */
775         ch = lex_getch(lex);
776         while (isdigit(ch))
777         {
778             lex_tokench(lex, ch);
779             ch = lex_getch(lex);
780         }
781     }
782     /* put back the last character */
783     /* but do not put back the trailing 'f' or a float */
784     if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
785         ch = lex_getch(lex);
786
787     /* generally we don't want words to follow numbers: */
788     if (isident(ch)) {
789         lexerror(lex, "unexpected trailing characters after number");
790         return (lex->tok.ttype = TOKEN_ERROR);
791     }
792     lex_ungetch(lex, ch);
793
794     lex_endtoken(lex);
795     if (lex->tok.ttype == TOKEN_FLOATCONST)
796         lex->tok.constval.f = strtod(lex->tok.value, NULL);
797     else
798         lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
799     return lex->tok.ttype;
800 }
801
802 int lex_do(lex_file *lex)
803 {
804     int ch, nextch;
805
806     lex_token_new(lex);
807 #if 0
808     if (!lex->tok)
809         return TOKEN_FATAL;
810 #endif
811
812     while (true) {
813         ch = lex_skipwhite(lex);
814         if (!lex->flags.mergelines || ch != '\\')
815             break;
816         ch = lex_getch(lex);
817         if (ch != '\n') {
818             lex_ungetch(lex, ch);
819             ch = '\\';
820             break;
821         }
822         /* we reached a linemerge */
823         lex_tokench(lex, '\n');
824         continue;
825     }
826
827     lex->sline = lex->line;
828     lex->tok.ctx.line = lex->sline;
829     lex->tok.ctx.file = lex->name;
830
831     if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
832         return (lex->tok.ttype = ch);
833     }
834
835     if (lex->eof)
836         return (lex->tok.ttype = TOKEN_FATAL);
837
838     if (ch == EOF) {
839         lex->eof = true;
840         return (lex->tok.ttype = TOKEN_EOF);
841     }
842
843     /* modelgen / spiritgen commands */
844     if (ch == '$') {
845         const char *v;
846         size_t frame;
847
848         ch = lex_getch(lex);
849         if (!isident_start(ch)) {
850             lexerror(lex, "hanging '$' modelgen/spritegen command line");
851             return lex_do(lex);
852         }
853         lex_tokench(lex, ch);
854         if (!lex_finish_ident(lex))
855             return (lex->tok.ttype = TOKEN_ERROR);
856         lex_endtoken(lex);
857         /* skip the known commands */
858         v = lex->tok.value;
859
860         if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
861         {
862             /* frame/framesave command works like an enum
863              * similar to fteqcc we handle this in the lexer.
864              * The reason for this is that it is sensitive to newlines,
865              * which the parser is unaware of
866              */
867             if (!lex_finish_frames(lex))
868                  return (lex->tok.ttype = TOKEN_ERROR);
869             return lex_do(lex);
870         }
871
872         if (!strcmp(v, "framevalue"))
873         {
874             ch = lex_getch(lex);
875             while (ch != EOF && isspace(ch) && ch != '\n')
876                 ch = lex_getch(lex);
877
878             if (!isdigit(ch)) {
879                 lexerror(lex, "$framevalue requires an integer parameter");
880                 return lex_do(lex);
881             }
882
883             lex_token_new(lex);
884             lex->tok.ttype = lex_finish_digit(lex, ch);
885             lex_endtoken(lex);
886             if (lex->tok.ttype != TOKEN_INTCONST) {
887                 lexerror(lex, "$framevalue requires an integer parameter");
888                 return lex_do(lex);
889             }
890             lex->framevalue = lex->tok.constval.i;
891             return lex_do(lex);
892         }
893
894         if (!strcmp(v, "framerestore"))
895         {
896             int rc;
897
898             lex_token_new(lex);
899
900             rc = lex_parse_frame(lex);
901
902             if (rc > 0) {
903                 lexerror(lex, "$framerestore requires a framename parameter");
904                 return lex_do(lex);
905             }
906             if (rc < 0)
907                 return (lex->tok.ttype = TOKEN_FATAL);
908
909             v = lex->tok.value;
910             for (frame = 0; frame < vec_size(lex->frames); ++frame) {
911                 if (!strcmp(v, lex->frames[frame].name)) {
912                     lex->framevalue = lex->frames[frame].value;
913                     return lex_do(lex);
914                 }
915             }
916             lexerror(lex, "unknown framename `%s`", v);
917             return lex_do(lex);
918         }
919
920         if (!strcmp(v, "modelname"))
921         {
922             int rc;
923
924             lex_token_new(lex);
925
926             rc = lex_parse_frame(lex);
927
928             if (rc > 0) {
929                 lexerror(lex, "$modelname requires a parameter");
930                 return lex_do(lex);
931             }
932             if (rc < 0)
933                 return (lex->tok.ttype = TOKEN_FATAL);
934
935             v = lex->tok.value;
936             if (lex->modelname) {
937                 frame_macro m;
938                 m.value = lex->framevalue;
939                 m.name = lex->modelname;
940                 lex->modelname = NULL;
941                 vec_push(lex->frames, m);
942             }
943             lex->modelname = lex->tok.value;
944             lex->tok.value = NULL;
945             return lex_do(lex);
946         }
947
948         if (!strcmp(v, "flush"))
949         {
950             size_t frame;
951             for (frame = 0; frame < vec_size(lex->frames); ++frame)
952                 mem_d(lex->frames[frame].name);
953             vec_free(lex->frames);
954             /* skip line (fteqcc does it too) */
955             ch = lex_getch(lex);
956             while (ch != EOF && ch != '\n')
957                 ch = lex_getch(lex);
958             return lex_do(lex);
959         }
960
961         if (!strcmp(v, "cd") ||
962             !strcmp(v, "origin") ||
963             !strcmp(v, "base") ||
964             !strcmp(v, "flags") ||
965             !strcmp(v, "scale") ||
966             !strcmp(v, "skin"))
967         {
968             /* skip line */
969             ch = lex_getch(lex);
970             while (ch != EOF && ch != '\n')
971                 ch = lex_getch(lex);
972             return lex_do(lex);
973         }
974
975         for (frame = 0; frame < vec_size(lex->frames); ++frame) {
976             if (!strcmp(v, lex->frames[frame].name)) {
977                 lex->tok.constval.i = lex->frames[frame].value;
978                 return (lex->tok.ttype = TOKEN_INTCONST);
979             }
980         }
981
982         lexerror(lex, "invalid frame macro");
983         return lex_do(lex);
984     }
985
986     /* single-character tokens */
987     switch (ch)
988     {
989         case '[':
990         case '(':
991             lex_tokench(lex, ch);
992             lex_endtoken(lex);
993             if (lex->flags.noops)
994                 return (lex->tok.ttype = ch);
995             else
996                 return (lex->tok.ttype = TOKEN_OPERATOR);
997         case ')':
998         case ';':
999         case ':':
1000         case '{':
1001         case '}':
1002         case ']':
1003
1004         case '#':
1005             lex_tokench(lex, ch);
1006             lex_endtoken(lex);
1007             return (lex->tok.ttype = ch);
1008         default:
1009             break;
1010     }
1011
1012     if (lex->flags.noops)
1013     {
1014         /* Detect characters early which are normally
1015          * operators OR PART of an operator.
1016          */
1017         switch (ch)
1018         {
1019             case '+':
1020             case '-':
1021             case '*':
1022             case '/':
1023             case '<':
1024             case '>':
1025             case '=':
1026             case '&':
1027             case '|':
1028             case '^':
1029             case '~':
1030             case ',':
1031             case '!':
1032                 lex_tokench(lex, ch);
1033                 lex_endtoken(lex);
1034                 return (lex->tok.ttype = ch);
1035             default:
1036                 break;
1037         }
1038
1039         if (ch == '.')
1040         {
1041             lex_tokench(lex, ch);
1042             /* peak ahead once */
1043             nextch = lex_getch(lex);
1044             if (nextch != '.') {
1045                 lex_ungetch(lex, nextch);
1046                 lex_endtoken(lex);
1047                 return (lex->tok.ttype = ch);
1048             }
1049             /* peak ahead again */
1050             nextch = lex_getch(lex);
1051             if (nextch != '.') {
1052                 lex_ungetch(lex, nextch);
1053                 lex_ungetch(lex, nextch);
1054                 lex_endtoken(lex);
1055                 return (lex->tok.ttype = ch);
1056             }
1057             /* fill the token to be "..." */
1058             lex_tokench(lex, ch);
1059             lex_tokench(lex, ch);
1060             lex_endtoken(lex);
1061             return (lex->tok.ttype = TOKEN_DOTS);
1062         }
1063     }
1064
1065     if (ch == ',' || ch == '.') {
1066         lex_tokench(lex, ch);
1067         lex_endtoken(lex);
1068         return (lex->tok.ttype = TOKEN_OPERATOR);
1069     }
1070
1071     if (ch == '+' || ch == '-' || /* ++, --, +=, -=  and -> as well! */
1072         ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1073         ch == '=' || ch == '!' || /* ==, != */
1074         ch == '&' || ch == '|')   /* &&, ||, &=, |= */
1075     {
1076         lex_tokench(lex, ch);
1077
1078         nextch = lex_getch(lex);
1079         if (nextch == ch || nextch == '=') {
1080             lex_tokench(lex, nextch);
1081         } else if (ch == '-' && nextch == '>') {
1082             lex_tokench(lex, nextch);
1083         } else
1084             lex_ungetch(lex, nextch);
1085
1086         lex_endtoken(lex);
1087         return (lex->tok.ttype = TOKEN_OPERATOR);
1088     }
1089
1090     /*
1091     if (ch == '^' || ch == '~' || ch == '!')
1092     {
1093         lex_tokench(lex, ch);
1094         lex_endtoken(lex);
1095         return (lex->tok.ttype = TOKEN_OPERATOR);
1096     }
1097     */
1098
1099     if (ch == '*' || ch == '/') /* *=, /= */
1100     {
1101         lex_tokench(lex, ch);
1102
1103         nextch = lex_getch(lex);
1104         if (nextch == '=') {
1105             lex_tokench(lex, nextch);
1106         } else
1107             lex_ungetch(lex, nextch);
1108
1109         lex_endtoken(lex);
1110         return (lex->tok.ttype = TOKEN_OPERATOR);
1111     }
1112
1113     if (isident_start(ch))
1114     {
1115         const char *v;
1116
1117         lex_tokench(lex, ch);
1118         if (!lex_finish_ident(lex)) {
1119             /* error? */
1120             return (lex->tok.ttype = TOKEN_ERROR);
1121         }
1122         lex_endtoken(lex);
1123         lex->tok.ttype = TOKEN_IDENT;
1124
1125         v = lex->tok.value;
1126         if (!strcmp(v, "void")) {
1127             lex->tok.ttype = TOKEN_TYPENAME;
1128             lex->tok.constval.t = TYPE_VOID;
1129         } else if (!strcmp(v, "int")) {
1130             lex->tok.ttype = TOKEN_TYPENAME;
1131             lex->tok.constval.t = TYPE_INTEGER;
1132         } else if (!strcmp(v, "float")) {
1133             lex->tok.ttype = TOKEN_TYPENAME;
1134             lex->tok.constval.t = TYPE_FLOAT;
1135         } else if (!strcmp(v, "string")) {
1136             lex->tok.ttype = TOKEN_TYPENAME;
1137             lex->tok.constval.t = TYPE_STRING;
1138         } else if (!strcmp(v, "entity")) {
1139             lex->tok.ttype = TOKEN_TYPENAME;
1140             lex->tok.constval.t = TYPE_ENTITY;
1141         } else if (!strcmp(v, "vector")) {
1142             lex->tok.ttype = TOKEN_TYPENAME;
1143             lex->tok.constval.t = TYPE_VECTOR;
1144         } else if (!strcmp(v, "for")  ||
1145                  !strcmp(v, "while")  ||
1146                  !strcmp(v, "do")     ||
1147                  !strcmp(v, "if")     ||
1148                  !strcmp(v, "else")   ||
1149                  !strcmp(v, "local")  ||
1150                  !strcmp(v, "return") ||
1151                  !strcmp(v, "not")    ||
1152                  !strcmp(v, "const"))
1153         {
1154             lex->tok.ttype = TOKEN_KEYWORD;
1155         }
1156         else if (opts_standard != COMPILER_QCC)
1157         {
1158             /* other standards reserve these keywords */
1159             if (!strcmp(v, "switch") ||
1160                 !strcmp(v, "struct") ||
1161                 !strcmp(v, "union")  ||
1162                 !strcmp(v, "break")  ||
1163                 !strcmp(v, "continue") ||
1164                 !strcmp(v, "var"))
1165             {
1166                 lex->tok.ttype = TOKEN_KEYWORD;
1167             }
1168         }
1169
1170         return lex->tok.ttype;
1171     }
1172
1173     if (ch == '"')
1174     {
1175         lex->flags.nodigraphs = true;
1176         if (lex->flags.preprocessing)
1177             lex_tokench(lex, ch);
1178         lex->tok.ttype = lex_finish_string(lex, '"');
1179         if (lex->flags.preprocessing)
1180             lex_tokench(lex, ch);
1181         while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1182         {
1183             /* Allow c style "string" "continuation" */
1184             ch = lex_skipwhite(lex);
1185             if (ch != '"') {
1186                 lex_ungetch(lex, ch);
1187                 break;
1188             }
1189
1190             lex->tok.ttype = lex_finish_string(lex, '"');
1191         }
1192         lex->flags.nodigraphs = false;
1193         lex_endtoken(lex);
1194         return lex->tok.ttype;
1195     }
1196
1197     if (ch == '\'')
1198     {
1199         /* we parse character constants like string,
1200          * but return TOKEN_CHARCONST, or a vector type if it fits...
1201          * Likewise actual unescaping has to be done by the parser.
1202          * The difference is we don't allow 'char' 'continuation'.
1203          */
1204         if (lex->flags.preprocessing)
1205             lex_tokench(lex, ch);
1206         lex->tok.ttype = lex_finish_string(lex, '\'');
1207         if (lex->flags.preprocessing)
1208             lex_tokench(lex, ch);
1209         lex_endtoken(lex);
1210
1211          /* It's a vector if we can successfully scan 3 floats */
1212 #ifdef WIN32
1213         if (sscanf_s(lex->tok.value, " %f %f %f ",
1214                    &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1215 #else
1216         if (sscanf(lex->tok.value, " %f %f %f ",
1217                    &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1218 #endif
1219
1220         {
1221              lex->tok.ttype = TOKEN_VECTORCONST;
1222         }
1223
1224         return lex->tok.ttype;
1225     }
1226
1227     if (isdigit(ch))
1228     {
1229         lex->tok.ttype = lex_finish_digit(lex, ch);
1230         lex_endtoken(lex);
1231         return lex->tok.ttype;
1232     }
1233
1234     lexerror(lex, "unknown token");
1235     return (lex->tok.ttype = TOKEN_ERROR);
1236 }