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