]> git.xonotic.org Git - xonotic/gmqcc.git/blob - lexer.c
Fixing handling of duplicate frame macros: 'continue' would continue the inner for...
[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         va_start(ap, fmt);
19     vprintmsg(LVL_ERROR, lex->name, lex->sline, "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     vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
36         va_end(ap);
37
38         return opts_werror;
39 }
40
41 token* token_new()
42 {
43     token *tok = (token*)mem_a(sizeof(token));
44     if (!tok)
45         return NULL;
46     memset(tok, 0, sizeof(*tok));
47     return tok;
48 }
49
50 void token_delete(token *self)
51 {
52     if (self->next && self->next->prev == self)
53         self->next->prev = self->prev;
54     if (self->prev && self->prev->next == self)
55         self->prev->next = self->next;
56     MEM_VECTOR_CLEAR(self, value);
57     mem_d(self);
58 }
59
60 token* token_copy(const token *cp)
61 {
62     token* self = token_new();
63     if (!self)
64         return NULL;
65     /* copy the value */
66     self->value_alloc = cp->value_count + 1;
67     self->value_count = cp->value_count;
68     self->value = (char*)mem_a(self->value_alloc);
69     if (!self->value) {
70         mem_d(self);
71         return NULL;
72     }
73     memcpy(self->value, cp->value, cp->value_count);
74     self->value[self->value_alloc-1] = 0;
75
76     /* rest */
77     self->ctx = cp->ctx;
78     self->ttype = cp->ttype;
79     memcpy(&self->constval, &cp->constval, sizeof(self->constval));
80     return self;
81 }
82
83 void token_delete_all(token *t)
84 {
85     token *n;
86
87     do {
88         n = t->next;
89         token_delete(t);
90         t = n;
91     } while(t);
92 }
93
94 token* token_copy_all(const token *cp)
95 {
96     token *cur;
97     token *out;
98
99     out = cur = token_copy(cp);
100     if (!out)
101         return NULL;
102
103     while (cp->next) {
104         cp = cp->next;
105         cur->next = token_copy(cp);
106         if (!cur->next) {
107             token_delete_all(out);
108             return NULL;
109         }
110         cur->next->prev = cur;
111         cur = cur->next;
112     }
113
114     return out;
115 }
116
117 lex_file* lex_open(const char *file)
118 {
119     lex_file *lex;
120     FILE *in = util_fopen(file, "rb");
121
122     if (!in) {
123         lexerror(NULL, "open failed: '%s'\n", file);
124         return NULL;
125     }
126
127     lex = (lex_file*)mem_a(sizeof(*lex));
128     if (!lex) {
129         fclose(in);
130         lexerror(NULL, "out of memory\n");
131         return NULL;
132     }
133
134     memset(lex, 0, sizeof(*lex));
135
136     lex->file = in;
137     lex->name = util_strdup(file);
138     lex->line = 1; /* we start counting at 1 */
139
140     lex->peekpos = 0;
141     lex->eof = false;
142
143     lex_filenames_add(lex->name);
144
145     return lex;
146 }
147
148 void lex_cleanup(void)
149 {
150     size_t i;
151     for (i = 0; i < lex_filenames_elements; ++i)
152         mem_d(lex_filenames_data[i]);
153     mem_d(lex_filenames_data);
154 }
155
156 void lex_close(lex_file *lex)
157 {
158     size_t i;
159     for (i = 0; i < lex->frames_count; ++i)
160         mem_d(lex->frames[i].name);
161     MEM_VECTOR_CLEAR(lex, frames);
162
163     if (lex->modelname)
164         mem_d(lex->modelname);
165
166     if (lex->file)
167         fclose(lex->file);
168     if (lex->tok)
169         token_delete(lex->tok);
170     /* mem_d(lex->name); collected in lex_filenames */
171     mem_d(lex);
172 }
173
174 /* Get or put-back data
175  * The following to functions do NOT understand what kind of data they
176  * are working on.
177  * The are merely wrapping get/put in order to count line numbers.
178  */
179 static int lex_getch(lex_file *lex)
180 {
181     int ch;
182
183     if (lex->peekpos) {
184         lex->peekpos--;
185         if (lex->peek[lex->peekpos] == '\n')
186             lex->line++;
187         return lex->peek[lex->peekpos];
188     }
189
190     ch = fgetc(lex->file);
191     if (ch == '\n')
192         lex->line++;
193     return ch;
194 }
195
196 static void lex_ungetch(lex_file *lex, int ch)
197 {
198     lex->peek[lex->peekpos++] = ch;
199     if (ch == '\n')
200         lex->line--;
201 }
202
203 /* classify characters
204  * some additions to the is*() functions of ctype.h
205  */
206
207 /* Idents are alphanumberic, but they start with alpha or _ */
208 static bool isident_start(int ch)
209 {
210     return isalpha(ch) || ch == '_';
211 }
212
213 static bool isident(int ch)
214 {
215     return isident_start(ch) || isdigit(ch);
216 }
217
218 /* isxdigit_only is used when we already know it's not a digit
219  * and want to see if it's a hex digit anyway.
220  */
221 static bool isxdigit_only(int ch)
222 {
223     return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
224 }
225
226 /* Skip whitespace and comments and return the first
227  * non-white character.
228  * As this makes use of the above getch() ungetch() functions,
229  * we don't need to care at all about line numbering anymore.
230  *
231  * In theory, this function should only be used at the beginning
232  * of lexing, or when we *know* the next character is part of the token.
233  * Otherwise, if the parser throws an error, the linenumber may not be
234  * the line of the error, but the line of the next token AFTER the error.
235  *
236  * This is currently only problematic when using c-like string-continuation,
237  * since comments and whitespaces are allowed between 2 such strings.
238  * Example:
239 printf(   "line one\n"
240 // A comment
241           "A continuation of the previous string"
242 // This line is skipped
243       , foo);
244
245  * In this case, if the parse decides it didn't actually want a string,
246  * and uses lex->line to print an error, it will show the ', foo);' line's
247  * linenumber.
248  *
249  * On the other hand, the parser is supposed to remember the line of the next
250  * token's beginning. In this case we would want skipwhite() to be called
251  * AFTER reading a token, so that the parser, before reading the NEXT token,
252  * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
253  *
254  * THIS SOLUTION
255  *    here is to store the line of the first character after skipping
256  *    the initial whitespace in lex->sline, this happens in lex_do.
257  */
258 static int lex_skipwhite(lex_file *lex)
259 {
260     int ch = 0;
261
262     do
263     {
264         ch = lex_getch(lex);
265         while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
266
267         if (ch == '/') {
268             ch = lex_getch(lex);
269             if (ch == '/')
270             {
271                 /* one line comment */
272                 ch = lex_getch(lex);
273
274                 /* check for special: '/', '/', '*', '/' */
275                 if (ch == '*') {
276                     ch = lex_getch(lex);
277                     if (ch == '/') {
278                         ch = ' ';
279                         continue;
280                     }
281                 }
282
283                 while (ch != EOF && ch != '\n') {
284                     ch = lex_getch(lex);
285                 }
286                 continue;
287             }
288             if (ch == '*')
289             {
290                 /* multiline comment */
291                 while (ch != EOF)
292                 {
293                     ch = lex_getch(lex);
294                     if (ch == '*') {
295                         ch = lex_getch(lex);
296                         if (ch == '/') {
297                             ch = lex_getch(lex);
298                             break;
299                         }
300                     }
301                 }
302                 if (ch == '/') /* allow *//* direct following comment */
303                 {
304                     lex_ungetch(lex, ch);
305                     ch = ' '; /* cause TRUE in the isspace check */
306                 }
307                 continue;
308             }
309             /* Otherwise roll back to the slash and break out of the loop */
310             lex_ungetch(lex, ch);
311             ch = '/';
312             break;
313         }
314     } while (ch != EOF && isspace(ch));
315
316     return ch;
317 }
318
319 /* Append a character to the token buffer */
320 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
321 {
322     if (!token_value_add(lex->tok, ch)) {
323         lexerror(lex, "out of memory");
324         return false;
325     }
326     return true;
327 }
328
329 /* Append a trailing null-byte */
330 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
331 {
332     if (!token_value_add(lex->tok, 0)) {
333         lexerror(lex, "out of memory");
334         return false;
335     }
336     lex->tok->value_count--;
337     return true;
338 }
339
340 /* Get a token */
341 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
342 {
343     int ch;
344
345     ch = lex_getch(lex);
346     while (ch != EOF && isident(ch))
347     {
348         if (!lex_tokench(lex, ch))
349             return (lex->tok->ttype = TOKEN_FATAL);
350         ch = lex_getch(lex);
351     }
352
353     /* last ch was not an ident ch: */
354     lex_ungetch(lex, ch);
355
356     return true;
357 }
358
359 /* read one ident for the frame list */
360 static int lex_parse_frame(lex_file *lex)
361 {
362     int ch;
363
364     if (lex->tok)
365         token_delete(lex->tok);
366     lex->tok = token_new();
367
368     ch = lex_getch(lex);
369     while (ch != EOF && ch != '\n' && isspace(ch))
370         ch = lex_getch(lex);
371
372     if (ch == '\n')
373         return 1;
374
375     if (!isident_start(ch)) {
376         lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
377         return -1;
378     }
379
380     if (!lex_tokench(lex, ch))
381         return -1;
382     if (!lex_finish_ident(lex))
383         return -1;
384     if (!lex_endtoken(lex))
385         return -1;
386     return 0;
387 }
388
389 /* read a list of $frames */
390 static bool lex_finish_frames(lex_file *lex)
391 {
392     do {
393         size_t i;
394         int    rc;
395         frame_macro m;
396
397         rc = lex_parse_frame(lex);
398         if (rc > 0) /* end of line */
399             return true;
400         if (rc < 0) /* error */
401             return false;
402
403         for (i = 0; i < lex->frames_count; ++i) {
404             if (!strcmp(lex->tok->value, lex->frames[i].name)) {
405                 lex->frames[i].value = lex->framevalue++;
406                 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok->value))
407                     return false;
408                 break;
409             }
410         }
411         if (i < lex->frames_count)
412             continue;
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 }