]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.c
Make the lexer use the correct error-printing mechanism
[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 MEM_VEC_FUNCTIONS(token, char, value)
10 MEM_VEC_FUNCTIONS(lex_file, frame_macro, frames)
11
12 VECTOR_MAKE(char*, lex_filenames);
13
14 void lexerror(lex_file *lex, const char *fmt, ...)
15 {
16         va_list ap;
17
18         parser->errors++;
19
20         va_start(ap, fmt);
21     vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
22         va_end(ap);
23 }
24
25 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
26 {
27         va_list ap;
28         int lvl = LVL_WARNING;
29
30     if (!OPTS_WARN(warntype))
31         return false;
32
33     if (opts_werror)
34             lvl = LVL_ERROR;
35
36         va_start(ap, fmt);
37     vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
38         va_end(ap);
39
40         return opts_werror;
41 }
42
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
119 lex_file* lex_open(const char *file)
120 {
121     lex_file *lex;
122     FILE *in = util_fopen(file, "rb");
123
124     if (!in) {
125         lexerror(NULL, "open failed: '%s'\n", file);
126         return NULL;
127     }
128
129     lex = (lex_file*)mem_a(sizeof(*lex));
130     if (!lex) {
131         fclose(in);
132         lexerror(NULL, "out of memory\n");
133         return NULL;
134     }
135
136     memset(lex, 0, sizeof(*lex));
137
138     lex->file = in;
139     lex->name = util_strdup(file);
140     lex->line = 1; /* we start counting at 1 */
141
142     lex->peekpos = 0;
143     lex->eof = false;
144
145     lex_filenames_add(lex->name);
146
147     return lex;
148 }
149
150 void lex_cleanup(void)
151 {
152     size_t i;
153     for (i = 0; i < lex_filenames_elements; ++i)
154         mem_d(lex_filenames_data[i]);
155     mem_d(lex_filenames_data);
156 }
157
158 void lex_close(lex_file *lex)
159 {
160     size_t i;
161     for (i = 0; i < lex->frames_count; ++i)
162         mem_d(lex->frames[i].name);
163     MEM_VECTOR_CLEAR(lex, frames);
164
165     if (lex->modelname)
166         mem_d(lex->modelname);
167
168     if (lex->file)
169         fclose(lex->file);
170     if (lex->tok)
171         token_delete(lex->tok);
172     /* mem_d(lex->name); collected in lex_filenames */
173     mem_d(lex);
174 }
175
176 /* Get or put-back data
177  * The following to functions do NOT understand what kind of data they
178  * are working on.
179  * The are merely wrapping get/put in order to count line numbers.
180  */
181 static int lex_getch(lex_file *lex)
182 {
183     int ch;
184
185     if (lex->peekpos) {
186         lex->peekpos--;
187         if (lex->peek[lex->peekpos] == '\n')
188             lex->line++;
189         return lex->peek[lex->peekpos];
190     }
191
192     ch = fgetc(lex->file);
193     if (ch == '\n')
194         lex->line++;
195     return ch;
196 }
197
198 static void lex_ungetch(lex_file *lex, int ch)
199 {
200     lex->peek[lex->peekpos++] = ch;
201     if (ch == '\n')
202         lex->line--;
203 }
204
205 /* classify characters
206  * some additions to the is*() functions of ctype.h
207  */
208
209 /* Idents are alphanumberic, but they start with alpha or _ */
210 static bool isident_start(int ch)
211 {
212     return isalpha(ch) || ch == '_';
213 }
214
215 static bool isident(int ch)
216 {
217     return isident_start(ch) || isdigit(ch);
218 }
219
220 /* isxdigit_only is used when we already know it's not a digit
221  * and want to see if it's a hex digit anyway.
222  */
223 static bool isxdigit_only(int ch)
224 {
225     return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
226 }
227
228 /* Skip whitespace and comments and return the first
229  * non-white character.
230  * As this makes use of the above getch() ungetch() functions,
231  * we don't need to care at all about line numbering anymore.
232  *
233  * In theory, this function should only be used at the beginning
234  * of lexing, or when we *know* the next character is part of the token.
235  * Otherwise, if the parser throws an error, the linenumber may not be
236  * the line of the error, but the line of the next token AFTER the error.
237  *
238  * This is currently only problematic when using c-like string-continuation,
239  * since comments and whitespaces are allowed between 2 such strings.
240  * Example:
241 printf(   "line one\n"
242 // A comment
243           "A continuation of the previous string"
244 // This line is skipped
245       , foo);
246
247  * In this case, if the parse decides it didn't actually want a string,
248  * and uses lex->line to print an error, it will show the ', foo);' line's
249  * linenumber.
250  *
251  * On the other hand, the parser is supposed to remember the line of the next
252  * token's beginning. In this case we would want skipwhite() to be called
253  * AFTER reading a token, so that the parser, before reading the NEXT token,
254  * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
255  *
256  * THIS SOLUTION
257  *    here is to store the line of the first character after skipping
258  *    the initial whitespace in lex->sline, this happens in lex_do.
259  */
260 static int lex_skipwhite(lex_file *lex)
261 {
262     int ch = 0;
263
264     do
265     {
266         ch = lex_getch(lex);
267         while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
268
269         if (ch == '/') {
270             ch = lex_getch(lex);
271             if (ch == '/')
272             {
273                 /* one line comment */
274                 ch = lex_getch(lex);
275
276                 /* check for special: '/', '/', '*', '/' */
277                 if (ch == '*') {
278                     ch = lex_getch(lex);
279                     if (ch == '/') {
280                         ch = ' ';
281                         continue;
282                     }
283                 }
284
285                 while (ch != EOF && ch != '\n') {
286                     ch = lex_getch(lex);
287                 }
288                 continue;
289             }
290             if (ch == '*')
291             {
292                 /* multiline comment */
293                 while (ch != EOF)
294                 {
295                     ch = lex_getch(lex);
296                     if (ch == '*') {
297                         ch = lex_getch(lex);
298                         if (ch == '/') {
299                             ch = lex_getch(lex);
300                             break;
301                         }
302                     }
303                 }
304                 if (ch == '/') /* allow *//* direct following comment */
305                 {
306                     lex_ungetch(lex, ch);
307                     ch = ' '; /* cause TRUE in the isspace check */
308                 }
309                 continue;
310             }
311             /* Otherwise roll back to the slash and break out of the loop */
312             lex_ungetch(lex, ch);
313             ch = '/';
314             break;
315         }
316     } while (ch != EOF && isspace(ch));
317
318     return ch;
319 }
320
321 /* Append a character to the token buffer */
322 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
323 {
324     if (!token_value_add(lex->tok, ch)) {
325         lexerror(lex, "out of memory");
326         return false;
327     }
328     return true;
329 }
330
331 /* Append a trailing null-byte */
332 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
333 {
334     if (!token_value_add(lex->tok, 0)) {
335         lexerror(lex, "out of memory");
336         return false;
337     }
338     lex->tok->value_count--;
339     return true;
340 }
341
342 /* Get a token */
343 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
344 {
345     int ch;
346
347     ch = lex_getch(lex);
348     while (ch != EOF && isident(ch))
349     {
350         if (!lex_tokench(lex, ch))
351             return (lex->tok->ttype = TOKEN_FATAL);
352         ch = lex_getch(lex);
353     }
354
355     /* last ch was not an ident ch: */
356     lex_ungetch(lex, ch);
357
358     return true;
359 }
360
361 /* read one ident for the frame list */
362 static int lex_parse_frame(lex_file *lex)
363 {
364     int ch;
365
366     if (lex->tok)
367         token_delete(lex->tok);
368     lex->tok = token_new();
369
370     ch = lex_getch(lex);
371     while (ch != EOF && ch != '\n' && isspace(ch))
372         ch = lex_getch(lex);
373
374     if (ch == '\n')
375         return 1;
376
377     if (!isident_start(ch)) {
378         lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
379         return -1;
380     }
381
382     if (!lex_tokench(lex, ch))
383         return -1;
384     if (!lex_finish_ident(lex))
385         return -1;
386     if (!lex_endtoken(lex))
387         return -1;
388     return 0;
389 }
390
391 /* read a list of $frames */
392 static bool lex_finish_frames(lex_file *lex)
393 {
394     do {
395         size_t i;
396         int    rc;
397         frame_macro m;
398
399         rc = lex_parse_frame(lex);
400         if (rc > 0) /* end of line */
401             return true;
402         if (rc < 0) /* error */
403             return false;
404
405         for (i = 0; i < lex->frames_count; ++i) {
406             if (!strcmp(lex->tok->value, lex->frames[i].name)) {
407                 lex->frames[i].value = lex->framevalue++;
408                 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok->value))
409                     return false;
410                 continue;
411             }
412         }
413
414         m.value = lex->framevalue++;
415         m.name = lex->tok->value;
416         lex->tok->value = NULL;
417         if (!lex_file_frames_add(lex, m)) {
418             lexerror(lex, "out of memory");
419             return false;
420         }
421     } while (true);
422 }
423
424 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
425 {
426     int ch = 0;
427
428     while (ch != EOF)
429     {
430         ch = lex_getch(lex);
431         if (ch == quote)
432             return TOKEN_STRINGCONST;
433
434         if (ch == '\\') {
435             ch = lex_getch(lex);
436             if (ch == EOF) {
437                 lexerror(lex, "unexpected end of file");
438                 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
439                 return (lex->tok->ttype = TOKEN_ERROR);
440             }
441
442             switch (ch) {
443             case '\\': break;
444             case 'a':  ch = '\a'; break;
445             case 'b':  ch = '\b'; break;
446             case 'r':  ch = '\r'; break;
447             case 'n':  ch = '\n'; break;
448             case 't':  ch = '\t'; break;
449             case 'f':  ch = '\f'; break;
450             case 'v':  ch = '\v'; break;
451             default:
452                 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
453                 /* so we just add the character plus backslash no matter what it actually is */
454                 if (!lex_tokench(lex, '\\'))
455                     return (lex->tok->ttype = TOKEN_FATAL);
456             }
457             /* add the character finally */
458             if (!lex_tokench(lex, ch))
459                 return (lex->tok->ttype = TOKEN_FATAL);
460         }
461         else if (!lex_tokench(lex, ch))
462             return (lex->tok->ttype = TOKEN_FATAL);
463     }
464     lexerror(lex, "unexpected end of file within string constant");
465     lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
466     return (lex->tok->ttype = TOKEN_ERROR);
467 }
468
469 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
470 {
471     bool ishex = false;
472
473     int  ch = lastch;
474
475     /* parse a number... */
476     lex->tok->ttype = TOKEN_INTCONST;
477
478     if (!lex_tokench(lex, ch))
479         return (lex->tok->ttype = TOKEN_FATAL);
480
481     ch = lex_getch(lex);
482     if (ch != '.' && !isdigit(ch))
483     {
484         if (lastch != '0' || ch != 'x')
485         {
486             /* end of the number or EOF */
487             lex_ungetch(lex, ch);
488             if (!lex_endtoken(lex))
489                 return (lex->tok->ttype = TOKEN_FATAL);
490
491             lex->tok->constval.i = lastch - '0';
492             return lex->tok->ttype;
493         }
494
495         ishex = true;
496     }
497
498     /* EOF would have been caught above */
499
500     if (ch != '.')
501     {
502         if (!lex_tokench(lex, ch))
503             return (lex->tok->ttype = TOKEN_FATAL);
504         ch = lex_getch(lex);
505         while (isdigit(ch) || (ishex && isxdigit_only(ch)))
506         {
507             if (!lex_tokench(lex, ch))
508                 return (lex->tok->ttype = TOKEN_FATAL);
509             ch = lex_getch(lex);
510         }
511     }
512     /* NOT else, '.' can come from above as well */
513     if (ch == '.' && !ishex)
514     {
515         /* Allow floating comma in non-hex mode */
516         lex->tok->ttype = TOKEN_FLOATCONST;
517         if (!lex_tokench(lex, ch))
518             return (lex->tok->ttype = TOKEN_FATAL);
519
520         /* continue digits-only */
521         ch = lex_getch(lex);
522         while (isdigit(ch))
523         {
524             if (!lex_tokench(lex, ch))
525                 return (lex->tok->ttype = TOKEN_FATAL);
526             ch = lex_getch(lex);
527         }
528     }
529     /* put back the last character */
530     /* but do not put back the trailing 'f' or a float */
531     if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
532         ch = lex_getch(lex);
533
534     /* generally we don't want words to follow numbers: */
535     if (isident(ch)) {
536         lexerror(lex, "unexpected trailing characters after number");
537         return (lex->tok->ttype = TOKEN_ERROR);
538     }
539     lex_ungetch(lex, ch);
540
541     if (!lex_endtoken(lex))
542         return (lex->tok->ttype = TOKEN_FATAL);
543     if (lex->tok->ttype == TOKEN_FLOATCONST)
544         lex->tok->constval.f = strtod(lex->tok->value, NULL);
545     else
546         lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
547     return lex->tok->ttype;
548 }
549
550 int lex_do(lex_file *lex)
551 {
552     int ch, nextch;
553
554     if (lex->tok)
555         token_delete(lex->tok);
556     lex->tok = token_new();
557     if (!lex->tok)
558         return TOKEN_FATAL;
559
560     ch = lex_skipwhite(lex);
561     lex->sline = lex->line;
562     lex->tok->ctx.line = lex->sline;
563     lex->tok->ctx.file = lex->name;
564
565     if (lex->eof)
566         return (lex->tok->ttype = TOKEN_FATAL);
567
568     if (ch == EOF) {
569         lex->eof = true;
570         return (lex->tok->ttype = TOKEN_EOF);
571     }
572
573     /* modelgen / spiritgen commands */
574     if (ch == '$') {
575         const char *v;
576         size_t frame;
577
578         ch = lex_getch(lex);
579         if (!isident_start(ch)) {
580             lexerror(lex, "hanging '$' modelgen/spritegen command line");
581             return lex_do(lex);
582         }
583         if (!lex_tokench(lex, ch))
584             return (lex->tok->ttype = TOKEN_FATAL);
585         if (!lex_finish_ident(lex))
586             return (lex->tok->ttype = TOKEN_ERROR);
587         if (!lex_endtoken(lex))
588             return (lex->tok->ttype = TOKEN_FATAL);
589         /* skip the known commands */
590         v = lex->tok->value;
591
592         if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
593         {
594             /* frame/framesave command works like an enum
595              * similar to fteqcc we handle this in the lexer.
596              * The reason for this is that it is sensitive to newlines,
597              * which the parser is unaware of
598              */
599             if (!lex_finish_frames(lex))
600                  return (lex->tok->ttype = TOKEN_ERROR);
601             return lex_do(lex);
602         }
603
604         if (!strcmp(v, "framevalue"))
605         {
606             ch = lex_getch(lex);
607             while (ch != EOF && isspace(ch) && ch != '\n')
608                 ch = lex_getch(lex);
609
610             if (!isdigit(ch)) {
611                 lexerror(lex, "$framevalue requires an integer parameter");
612                 return lex_do(lex);
613             }
614
615             token_delete(lex->tok);
616             lex->tok = token_new();
617             lex->tok->ttype = lex_finish_digit(lex, ch);
618             if (!lex_endtoken(lex))
619                 return (lex->tok->ttype = TOKEN_FATAL);
620             if (lex->tok->ttype != TOKEN_INTCONST) {
621                 lexerror(lex, "$framevalue requires an integer parameter");
622                 return lex_do(lex);
623             }
624             lex->framevalue = lex->tok->constval.i;
625             return lex_do(lex);
626         }
627
628         if (!strcmp(v, "framerestore"))
629         {
630             int rc;
631
632             token_delete(lex->tok);
633             lex->tok = token_new();
634
635             rc = lex_parse_frame(lex);
636
637             if (rc > 0) {
638                 lexerror(lex, "$framerestore requires a framename parameter");
639                 return lex_do(lex);
640             }
641             if (rc < 0)
642                 return (lex->tok->ttype = TOKEN_FATAL);
643
644             v = lex->tok->value;
645             for (frame = 0; frame < lex->frames_count; ++frame) {
646                 if (!strcmp(v, lex->frames[frame].name)) {
647                     lex->framevalue = lex->frames[frame].value;
648                     return lex_do(lex);
649                 }
650             }
651             lexerror(lex, "unknown framename `%s`", v);
652             return lex_do(lex);
653         }
654
655         if (!strcmp(v, "modelname"))
656         {
657             int rc;
658
659             token_delete(lex->tok);
660             lex->tok = token_new();
661
662             rc = lex_parse_frame(lex);
663
664             if (rc > 0) {
665                 lexerror(lex, "$framerestore requires a framename parameter");
666                 return lex_do(lex);
667             }
668             if (rc < 0)
669                 return (lex->tok->ttype = TOKEN_FATAL);
670
671             v = lex->tok->value;
672             if (lex->modelname) {
673                 frame_macro m;
674                 m.value = lex->framevalue;
675                 m.name = lex->modelname;
676                 lex->modelname = NULL;
677                 if (!lex_file_frames_add(lex, m)) {
678                     lexerror(lex, "out of memory");
679                     return (lex->tok->ttype = TOKEN_FATAL);
680                 }
681             }
682             lex->modelname = lex->tok->value;
683             lex->tok->value = NULL;
684             for (frame = 0; frame < lex->frames_count; ++frame) {
685                 if (!strcmp(v, lex->frames[frame].name)) {
686                     lex->framevalue = lex->frames[frame].value;
687                     break;
688                 }
689             }
690             return lex_do(lex);
691         }
692
693         if (!strcmp(v, "flush"))
694         {
695             size_t frame;
696             for (frame = 0; frame < lex->frames_count; ++frame)
697                 mem_d(lex->frames[frame].name);
698             MEM_VECTOR_CLEAR(lex, frames);
699             /* skip line (fteqcc does it too) */
700             ch = lex_getch(lex);
701             while (ch != EOF && ch != '\n')
702                 ch = lex_getch(lex);
703             return lex_do(lex);
704         }
705
706         if (!strcmp(v, "cd") ||
707             !strcmp(v, "origin") ||
708             !strcmp(v, "base") ||
709             !strcmp(v, "flags") ||
710             !strcmp(v, "scale") ||
711             !strcmp(v, "skin"))
712         {
713             /* skip line */
714             ch = lex_getch(lex);
715             while (ch != EOF && ch != '\n')
716                 ch = lex_getch(lex);
717             return lex_do(lex);
718         }
719
720         for (frame = 0; frame < lex->frames_count; ++frame) {
721             if (!strcmp(v, lex->frames[frame].name)) {
722                 lex->tok->constval.i = lex->frames[frame].value;
723                 return (lex->tok->ttype = TOKEN_INTCONST);
724             }
725         }
726
727         lexerror(lex, "invalid frame macro");
728         return lex_do(lex);
729     }
730
731     /* single-character tokens */
732     switch (ch)
733     {
734         case '(':
735             if (!lex_tokench(lex, ch) ||
736                 !lex_endtoken(lex))
737             {
738                 return (lex->tok->ttype = TOKEN_FATAL);
739             }
740             if (lex->flags.noops)
741                 return (lex->tok->ttype = ch);
742             else
743                 return (lex->tok->ttype = TOKEN_OPERATOR);
744         case ')':
745         case ';':
746         case '{':
747         case '}':
748         case '[':
749         case ']':
750
751         case '#':
752             if (!lex_tokench(lex, ch) ||
753                 !lex_endtoken(lex))
754             {
755                 return (lex->tok->ttype = TOKEN_FATAL);
756             }
757             return (lex->tok->ttype = ch);
758         default:
759             break;
760     }
761
762     if (lex->flags.noops)
763     {
764         /* Detect characters early which are normally
765          * operators OR PART of an operator.
766          */
767         switch (ch)
768         {
769             case '+':
770             case '-':
771             case '*':
772             case '/':
773             case '<':
774             case '>':
775             case '=':
776             case '&':
777             case '|':
778             case '^':
779             case '~':
780             case ',':
781             case '!':
782                 if (!lex_tokench(lex, ch) ||
783                     !lex_endtoken(lex))
784                 {
785                     return (lex->tok->ttype = TOKEN_FATAL);
786                 }
787                 return (lex->tok->ttype = ch);
788             default:
789                 break;
790         }
791
792         if (ch == '.')
793         {
794             if (!lex_tokench(lex, ch))
795                 return (lex->tok->ttype = TOKEN_FATAL);
796             /* peak ahead once */
797             nextch = lex_getch(lex);
798             if (nextch != '.') {
799                 lex_ungetch(lex, nextch);
800                 if (!lex_endtoken(lex))
801                     return (lex->tok->ttype = TOKEN_FATAL);
802                 return (lex->tok->ttype = ch);
803             }
804             /* peak ahead again */
805             nextch = lex_getch(lex);
806             if (nextch != '.') {
807                 lex_ungetch(lex, nextch);
808                 lex_ungetch(lex, nextch);
809                 if (!lex_endtoken(lex))
810                     return (lex->tok->ttype = TOKEN_FATAL);
811                 return (lex->tok->ttype = ch);
812             }
813             /* fill the token to be "..." */
814             if (!lex_tokench(lex, ch) ||
815                 !lex_tokench(lex, ch) ||
816                 !lex_endtoken(lex))
817             {
818                 return (lex->tok->ttype = TOKEN_FATAL);
819             }
820             return (lex->tok->ttype = TOKEN_DOTS);
821         }
822     }
823
824     if (ch == ',' || ch == '.') {
825         if (!lex_tokench(lex, ch) ||
826             !lex_endtoken(lex))
827         {
828             return (lex->tok->ttype = TOKEN_FATAL);
829         }
830         return (lex->tok->ttype = TOKEN_OPERATOR);
831     }
832
833     if (ch == '+' || ch == '-' || /* ++, --, +=, -=  and -> as well! */
834         ch == '>' || ch == '<' || /* <<, >>, <=, >= */
835         ch == '=' || ch == '!' || /* ==, != */
836         ch == '&' || ch == '|')   /* &&, ||, &=, |= */
837     {
838         if (!lex_tokench(lex, ch))
839             return (lex->tok->ttype = TOKEN_FATAL);
840
841         nextch = lex_getch(lex);
842         if (nextch == ch || nextch == '=') {
843             if (!lex_tokench(lex, nextch))
844                 return (lex->tok->ttype = TOKEN_FATAL);
845         } else if (ch == '-' && nextch == '>') {
846             if (!lex_tokench(lex, nextch))
847                 return (lex->tok->ttype = TOKEN_FATAL);
848         } else
849             lex_ungetch(lex, nextch);
850
851         if (!lex_endtoken(lex))
852             return (lex->tok->ttype = TOKEN_FATAL);
853         return (lex->tok->ttype = TOKEN_OPERATOR);
854     }
855
856     /*
857     if (ch == '^' || ch == '~' || ch == '!')
858     {
859         if (!lex_tokench(lex, ch) ||
860             !lex_endtoken(lex))
861         {
862             return (lex->tok->ttype = TOKEN_FATAL);
863         }
864         return (lex->tok->ttype = TOKEN_OPERATOR);
865     }
866     */
867
868     if (ch == '*' || ch == '/') /* *=, /= */
869     {
870         if (!lex_tokench(lex, ch))
871             return (lex->tok->ttype = TOKEN_FATAL);
872
873         nextch = lex_getch(lex);
874         if (nextch == '=') {
875             if (!lex_tokench(lex, nextch))
876                 return (lex->tok->ttype = TOKEN_FATAL);
877         } else
878             lex_ungetch(lex, nextch);
879
880         if (!lex_endtoken(lex))
881             return (lex->tok->ttype = TOKEN_FATAL);
882         return (lex->tok->ttype = TOKEN_OPERATOR);
883     }
884
885     if (isident_start(ch))
886     {
887         const char *v;
888
889         if (!lex_tokench(lex, ch))
890             return (lex->tok->ttype = TOKEN_FATAL);
891         if (!lex_finish_ident(lex)) {
892             /* error? */
893             return (lex->tok->ttype = TOKEN_ERROR);
894         }
895         if (!lex_endtoken(lex))
896             return (lex->tok->ttype = TOKEN_FATAL);
897         lex->tok->ttype = TOKEN_IDENT;
898
899         v = lex->tok->value;
900         if (!strcmp(v, "void")) {
901             lex->tok->ttype = TOKEN_TYPENAME;
902             lex->tok->constval.t = TYPE_VOID;
903         } else if (!strcmp(v, "int")) {
904             lex->tok->ttype = TOKEN_TYPENAME;
905             lex->tok->constval.t = TYPE_INTEGER;
906         } else if (!strcmp(v, "float")) {
907             lex->tok->ttype = TOKEN_TYPENAME;
908             lex->tok->constval.t = TYPE_FLOAT;
909         } else if (!strcmp(v, "string")) {
910             lex->tok->ttype = TOKEN_TYPENAME;
911             lex->tok->constval.t = TYPE_STRING;
912         } else if (!strcmp(v, "entity")) {
913             lex->tok->ttype = TOKEN_TYPENAME;
914             lex->tok->constval.t = TYPE_ENTITY;
915         } else if (!strcmp(v, "vector")) {
916             lex->tok->ttype = TOKEN_TYPENAME;
917             lex->tok->constval.t = TYPE_VECTOR;
918         } else if (!strcmp(v, "for")  ||
919                  !strcmp(v, "while")  ||
920                  !strcmp(v, "do")     ||
921                  !strcmp(v, "if")     ||
922                  !strcmp(v, "else")   ||
923                  !strcmp(v, "local")  ||
924                  !strcmp(v, "return") ||
925                  !strcmp(v, "const"))
926             lex->tok->ttype = TOKEN_KEYWORD;
927
928         return lex->tok->ttype;
929     }
930
931     if (ch == '"')
932     {
933         lex->tok->ttype = lex_finish_string(lex, '"');
934         while (lex->tok->ttype == TOKEN_STRINGCONST)
935         {
936             /* Allow c style "string" "continuation" */
937             ch = lex_skipwhite(lex);
938             if (ch != '"') {
939                 lex_ungetch(lex, ch);
940                 break;
941             }
942
943             lex->tok->ttype = lex_finish_string(lex, '"');
944         }
945         if (!lex_endtoken(lex))
946             return (lex->tok->ttype = TOKEN_FATAL);
947         return lex->tok->ttype;
948     }
949
950     if (ch == '\'')
951     {
952         /* we parse character constants like string,
953          * but return TOKEN_CHARCONST, or a vector type if it fits...
954          * Likewise actual unescaping has to be done by the parser.
955          * The difference is we don't allow 'char' 'continuation'.
956          */
957          lex->tok->ttype = lex_finish_string(lex, '\'');
958          if (!lex_endtoken(lex))
959               return (lex->tok->ttype = TOKEN_FATAL);
960
961          /* It's a vector if we can successfully scan 3 floats */
962 #ifdef WIN32
963          if (sscanf_s(lex->tok->value, " %f %f %f ",
964                     &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
965 #else
966          if (sscanf(lex->tok->value, " %f %f %f ",
967                     &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
968 #endif
969          {
970               lex->tok->ttype = TOKEN_VECTORCONST;
971          }
972
973          return lex->tok->ttype;
974     }
975
976     if (isdigit(ch))
977     {
978         lex->tok->ttype = lex_finish_digit(lex, ch);
979         if (!lex_endtoken(lex))
980             return (lex->tok->ttype = TOKEN_FATAL);
981         return lex->tok->ttype;
982     }
983
984     lexerror(lex, "unknown token");
985     return (lex->tok->ttype = TOKEN_ERROR);
986 }