2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 static const char *keywords_qc[] = {
42 static const char *keywords_fg[] = {
43 "switch", "case", "default",
49 "__builtin_debug_printtype"
55 static char* *lex_filenames;
57 static void lexerror(lex_file *lex, const char *fmt, ...)
63 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
65 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
69 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
76 ctx.line = lex->sline;
77 ctx.column = lex->column;
80 r = vcompile_warning(ctx, warntype, fmt, ap);
85 static void lex_token_new(lex_file *lex)
88 vec_shrinkto(lex->tok.value, 0);
90 lex->tok.constval.t = 0;
91 lex->tok.ctx.line = lex->sline;
92 lex->tok.ctx.file = lex->name;
93 lex->tok.ctx.column = lex->column;
96 static void lex_ungetch(lex_file *lex, int ch);
97 static int lex_getch(lex_file *lex);
99 lex_file* lex_open(const char *file)
102 fs_file_t *in = fs_file_open(file, "rb");
106 lexerror(NULL, "open failed: '%s'\n", file);
110 lex = (lex_file*)mem_a(sizeof(*lex));
113 lexerror(NULL, "out of memory\n");
117 memset(lex, 0, sizeof(*lex));
120 lex->name = util_strdup(file);
121 lex->line = 1; /* we start counting at 1 */
127 if ((read = (lex_getch(lex) << 16) | (lex_getch(lex) << 8) | lex_getch(lex)) != 0xEFBBBF) {
128 lex_ungetch(lex, (read & 0x0000FF));
129 lex_ungetch(lex, (read & 0x00FF00) >> 8);
130 lex_ungetch(lex, (read & 0xFF0000) >> 16);
133 * otherwise the lexer has advanced 3 bytes for the BOM, we need
134 * to set the column back to 0
139 vec_push(lex_filenames, lex->name);
143 lex_file* lex_open_string(const char *str, size_t len, const char *name)
147 lex = (lex_file*)mem_a(sizeof(*lex));
149 lexerror(NULL, "out of memory\n");
153 memset(lex, 0, sizeof(*lex));
156 lex->open_string = str;
157 lex->open_string_length = len;
158 lex->open_string_pos = 0;
160 lex->name = util_strdup(name ? name : "<string-source>");
161 lex->line = 1; /* we start counting at 1 */
166 vec_push(lex_filenames, lex->name);
171 void lex_cleanup(void)
174 for (i = 0; i < vec_size(lex_filenames); ++i)
175 mem_d(lex_filenames[i]);
176 vec_free(lex_filenames);
179 void lex_close(lex_file *lex)
182 for (i = 0; i < vec_size(lex->frames); ++i)
183 mem_d(lex->frames[i].name);
184 vec_free(lex->frames);
187 vec_free(lex->modelname);
190 fs_file_close(lex->file);
192 vec_free(lex->tok.value);
194 /* mem_d(lex->name); collected in lex_filenames */
200 static int lex_fgetc(lex_file *lex)
204 return fs_file_getc(lex->file);
206 if (lex->open_string) {
207 if (lex->open_string_pos >= lex->open_string_length)
210 return lex->open_string[lex->open_string_pos++];
215 /* Get or put-back data
216 * The following to functions do NOT understand what kind of data they
218 * The are merely wrapping get/put in order to count line numbers.
220 static int lex_try_trigraph(lex_file *lex, int old)
224 if (!lex->push_line && c2 == '\n') {
230 lex_ungetch(lex, c2);
235 if (!lex->push_line && c3 == '\n') {
241 case '=': return '#';
242 case '/': return '\\';
243 case '\'': return '^';
244 case '(': return '[';
245 case ')': return ']';
246 case '!': return '|';
247 case '<': return '{';
248 case '>': return '}';
249 case '-': return '~';
251 lex_ungetch(lex, c3);
252 lex_ungetch(lex, c2);
257 static int lex_try_digraph(lex_file *lex, int ch)
261 /* we just used fgetc() so count lines
262 * need to offset a \n the ungetch would recognize
264 if (!lex->push_line && c2 == '\n')
266 if (ch == '<' && c2 == ':')
268 else if (ch == ':' && c2 == '>')
270 else if (ch == '<' && c2 == '%')
272 else if (ch == '%' && c2 == '>')
274 else if (ch == '%' && c2 == ':')
276 lex_ungetch(lex, c2);
280 static int lex_getch(lex_file *lex)
286 if (!lex->push_line && lex->peek[lex->peekpos] == '\n') {
290 return lex->peek[lex->peekpos];
294 if (!lex->push_line && ch == '\n') {
299 return lex_try_trigraph(lex, ch);
300 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
301 return lex_try_digraph(lex, ch);
305 static void lex_ungetch(lex_file *lex, int ch)
307 lex->peek[lex->peekpos++] = ch;
309 if (!lex->push_line && ch == '\n') {
315 /* classify characters
316 * some additions to the is*() functions of ctype.h
319 /* Idents are alphanumberic, but they start with alpha or _ */
320 static bool isident_start(int ch)
322 return util_isalpha(ch) || ch == '_';
325 static bool isident(int ch)
327 return isident_start(ch) || util_isdigit(ch);
330 /* isxdigit_only is used when we already know it's not a digit
331 * and want to see if it's a hex digit anyway.
333 static bool isxdigit_only(int ch)
335 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
338 /* Append a character to the token buffer */
339 static void lex_tokench(lex_file *lex, int ch)
341 vec_push(lex->tok.value, ch);
344 /* Append a trailing null-byte */
345 static void lex_endtoken(lex_file *lex)
347 vec_push(lex->tok.value, 0);
348 vec_shrinkby(lex->tok.value, 1);
351 static bool lex_try_pragma(lex_file *lex)
355 char *command = NULL;
359 if (lex->flags.preprocessing)
366 lex_ungetch(lex, ch);
370 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
371 vec_push(pragma, ch);
374 if (ch != ' ' || strcmp(pragma, "pragma")) {
375 lex_ungetch(lex, ch);
379 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
380 vec_push(command, ch);
381 vec_push(command, 0);
384 lex_ungetch(lex, ch);
388 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
393 lex_ungetch(lex, ch);
397 if (!strcmp(command, "push")) {
398 if (!strcmp(param, "line")) {
400 if (lex->push_line == 1)
406 else if (!strcmp(command, "pop")) {
407 if (!strcmp(param, "line")) {
410 if (lex->push_line == 0)
416 else if (!strcmp(command, "file")) {
417 lex->name = util_strdup(param);
418 vec_push(lex_filenames, lex->name);
420 else if (!strcmp(command, "line")) {
421 line = strtol(param, NULL, 0)-1;
427 while (ch != '\n' && ch != FS_FILE_EOF)
437 while (vec_size(command)) {
438 lex_ungetch(lex, (unsigned char)vec_last(command));
442 lex_ungetch(lex, ' ');
446 while (vec_size(param)) {
447 lex_ungetch(lex, (unsigned char)vec_last(param));
451 lex_ungetch(lex, ' ');
455 while (vec_size(pragma)) {
456 lex_ungetch(lex, (unsigned char)vec_last(pragma));
461 lex_ungetch(lex, '#');
467 /* Skip whitespace and comments and return the first
468 * non-white character.
469 * As this makes use of the above getch() ungetch() functions,
470 * we don't need to care at all about line numbering anymore.
472 * In theory, this function should only be used at the beginning
473 * of lexing, or when we *know* the next character is part of the token.
474 * Otherwise, if the parser throws an error, the linenumber may not be
475 * the line of the error, but the line of the next token AFTER the error.
477 * This is currently only problematic when using c-like string-continuation,
478 * since comments and whitespaces are allowed between 2 such strings.
482 "A continuation of the previous string"
483 // This line is skipped
486 * In this case, if the parse decides it didn't actually want a string,
487 * and uses lex->line to print an error, it will show the ', foo);' line's
490 * On the other hand, the parser is supposed to remember the line of the next
491 * token's beginning. In this case we would want skipwhite() to be called
492 * AFTER reading a token, so that the parser, before reading the NEXT token,
493 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
496 * here is to store the line of the first character after skipping
497 * the initial whitespace in lex->sline, this happens in lex_do.
499 static int lex_skipwhite(lex_file *lex, bool hadwhite)
502 bool haswhite = hadwhite;
507 while (ch != FS_FILE_EOF && util_isspace(ch)) {
509 if (lex_try_pragma(lex))
512 if (lex->flags.preprocessing) {
515 /* see if there was whitespace first */
516 if (haswhite) { /* (vec_size(lex->tok.value)) { */
517 lex_ungetch(lex, ch);
521 /* otherwise return EOL */
525 lex_tokench(lex, ch);
534 /* one line comment */
537 if (lex->flags.preprocessing) {
539 lex_tokench(lex, ' ');
540 lex_tokench(lex, ' ');
543 while (ch != FS_FILE_EOF && ch != '\n') {
544 if (lex->flags.preprocessing)
545 lex_tokench(lex, ' '); /* ch); */
548 if (lex->flags.preprocessing) {
549 lex_ungetch(lex, '\n');
557 /* multiline comment */
558 if (lex->flags.preprocessing) {
560 lex_tokench(lex, ' ');
561 lex_tokench(lex, ' ');
564 while (ch != FS_FILE_EOF)
570 if (lex->flags.preprocessing) {
571 lex_tokench(lex, ' ');
572 lex_tokench(lex, ' ');
576 lex_ungetch(lex, ch);
578 if (lex->flags.preprocessing) {
580 lex_tokench(lex, '\n');
582 lex_tokench(lex, ' '); /* ch); */
585 ch = ' '; /* cause TRUE in the isspace check */
588 /* Otherwise roll back to the slash and break out of the loop */
589 lex_ungetch(lex, ch);
593 } while (ch != FS_FILE_EOF && util_isspace(ch));
597 lex_ungetch(lex, ch);
604 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
609 while (ch != FS_FILE_EOF && isident(ch))
611 lex_tokench(lex, ch);
615 /* last ch was not an ident ch: */
616 lex_ungetch(lex, ch);
621 /* read one ident for the frame list */
622 static int lex_parse_frame(lex_file *lex)
629 while (ch != FS_FILE_EOF && ch != '\n' && util_isspace(ch))
635 if (!isident_start(ch)) {
636 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
640 lex_tokench(lex, ch);
641 if (!lex_finish_ident(lex))
647 /* read a list of $frames */
648 static bool lex_finish_frames(lex_file *lex)
655 rc = lex_parse_frame(lex);
656 if (rc > 0) /* end of line */
658 if (rc < 0) /* error */
661 for (i = 0; i < vec_size(lex->frames); ++i) {
662 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
663 lex->frames[i].value = lex->framevalue++;
664 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
669 if (i < vec_size(lex->frames))
672 m.value = lex->framevalue++;
673 m.name = util_strdup(lex->tok.value);
674 vec_shrinkto(lex->tok.value, 0);
675 vec_push(lex->frames, m);
681 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
688 char u8buf[8]; /* way more than enough */
691 while (ch != FS_FILE_EOF)
695 return TOKEN_STRINGCONST;
697 if (lex->flags.preprocessing && ch == '\\') {
698 lex_tokench(lex, ch);
700 if (ch == FS_FILE_EOF) {
701 lexerror(lex, "unexpected end of file");
702 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
703 return (lex->tok.ttype = TOKEN_ERROR);
705 lex_tokench(lex, ch);
707 else if (ch == '\\') {
709 if (ch == FS_FILE_EOF) {
710 lexerror(lex, "unexpected end of file");
711 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
712 return (lex->tok.ttype = TOKEN_ERROR);
719 case 'a': ch = '\a'; break;
720 case 'b': ch = '\b'; break;
721 case 'r': ch = '\r'; break;
722 case 'n': ch = '\n'; break;
723 case 't': ch = '\t'; break;
724 case 'f': ch = '\f'; break;
725 case 'v': ch = '\v'; break;
728 /* same procedure as in fteqcc */
730 nextch = lex_getch(lex);
731 if (nextch >= '0' && nextch <= '9')
733 else if (nextch >= 'a' && nextch <= 'f')
734 ch += nextch - 'a' + 10;
735 else if (nextch >= 'A' && nextch <= 'F')
736 ch += nextch - 'A' + 10;
738 lexerror(lex, "bad character code");
739 lex_ungetch(lex, nextch);
740 return (lex->tok.ttype = TOKEN_ERROR);
744 nextch = lex_getch(lex);
745 if (nextch >= '0' && nextch <= '9')
747 else if (nextch >= 'a' && nextch <= 'f')
748 ch += nextch - 'a' + 10;
749 else if (nextch >= 'A' && nextch <= 'F')
750 ch += nextch - 'A' + 10;
752 lexerror(lex, "bad character code");
753 lex_ungetch(lex, nextch);
754 return (lex->tok.ttype = TOKEN_ERROR);
759 case '0': case '1': case '2': case '3':
760 case '4': case '5': case '6': case '7':
764 case '<': ch = 29; break;
765 case '-': ch = 30; break;
766 case '>': ch = 31; break;
767 case '[': ch = 16; break;
768 case ']': ch = 17; break;
771 nextch = lex_getch(lex);
772 hex = (nextch == 'x');
773 oct = (nextch == '0');
775 lex_ungetch(lex, nextch);
776 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
778 if (nextch >= '0' && nextch <= '9')
779 chr = chr * 10 + nextch - '0';
781 lexerror(lex, "bad character code");
782 return (lex->tok.ttype = TOKEN_ERROR);
785 if (nextch >= '0' && nextch <= '9')
786 chr = chr * 0x10 + nextch - '0';
787 else if (nextch >= 'a' && nextch <= 'f')
788 chr = chr * 0x10 + nextch - 'a' + 10;
789 else if (nextch >= 'A' && nextch <= 'F')
790 chr = chr * 0x10 + nextch - 'A' + 10;
792 lexerror(lex, "bad character code");
793 return (lex->tok.ttype = TOKEN_ERROR);
796 if (nextch >= '0' && nextch <= '9')
797 chr = chr * 8 + chr - '0';
799 lexerror(lex, "bad character code");
800 return (lex->tok.ttype = TOKEN_ERROR);
803 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
805 lexerror(lex, "character code out of range");
806 return (lex->tok.ttype = TOKEN_ERROR);
809 if (OPTS_FLAG(UTF8) && chr >= 128) {
810 u8len = utf8_from(u8buf, chr);
815 lex->column += u8len;
816 for (uc = 0; uc < u8len; ++uc)
817 lex_tokench(lex, u8buf[uc]);
819 * the last character will be inserted with the tokench() call
828 case '\n': ch = '\n'; break;
831 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
832 /* so we just add the character plus backslash no matter what it actually is */
833 lex_tokench(lex, '\\');
835 /* add the character finally */
836 lex_tokench(lex, ch);
839 lex_tokench(lex, ch);
841 lexerror(lex, "unexpected end of file within string constant");
842 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
843 return (lex->tok.ttype = TOKEN_ERROR);
846 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
852 /* parse a number... */
854 lex->tok.ttype = TOKEN_FLOATCONST;
856 lex->tok.ttype = TOKEN_INTCONST;
858 lex_tokench(lex, ch);
861 if (ch != '.' && !util_isdigit(ch))
863 if (lastch != '0' || ch != 'x')
865 /* end of the number or EOF */
866 lex_ungetch(lex, ch);
869 lex->tok.constval.i = lastch - '0';
870 return lex->tok.ttype;
876 /* EOF would have been caught above */
880 lex_tokench(lex, ch);
882 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
884 lex_tokench(lex, ch);
888 /* NOT else, '.' can come from above as well */
889 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
891 /* Allow floating comma in non-hex mode */
892 lex->tok.ttype = TOKEN_FLOATCONST;
893 lex_tokench(lex, ch);
895 /* continue digits-only */
897 while (util_isdigit(ch))
899 lex_tokench(lex, ch);
903 /* put back the last character */
904 /* but do not put back the trailing 'f' or a float */
905 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
908 /* generally we don't want words to follow numbers: */
910 lexerror(lex, "unexpected trailing characters after number");
911 return (lex->tok.ttype = TOKEN_ERROR);
913 lex_ungetch(lex, ch);
916 if (lex->tok.ttype == TOKEN_FLOATCONST)
917 lex->tok.constval.f = strtod(lex->tok.value, NULL);
919 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
920 return lex->tok.ttype;
923 int lex_do(lex_file *lex)
925 int ch, nextch, thirdch;
926 bool hadwhite = false;
931 ch = lex_skipwhite(lex, hadwhite);
933 if (!lex->flags.mergelines || ch != '\\')
939 lex_ungetch(lex, ch);
943 /* we reached a linemerge */
944 lex_tokench(lex, '\n');
948 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
949 return (lex->tok.ttype = ch);
952 lex->sline = lex->line;
953 lex->tok.ctx.line = lex->sline;
954 lex->tok.ctx.file = lex->name;
957 return (lex->tok.ttype = TOKEN_FATAL);
959 if (ch == FS_FILE_EOF) {
961 return (lex->tok.ttype = TOKEN_EOF);
964 /* modelgen / spiritgen commands */
965 if (ch == '$' && !lex->flags.preprocessing) {
970 if (!isident_start(ch)) {
971 lexerror(lex, "hanging '$' modelgen/spritegen command line");
974 lex_tokench(lex, ch);
975 if (!lex_finish_ident(lex))
976 return (lex->tok.ttype = TOKEN_ERROR);
978 /* skip the known commands */
981 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
983 /* frame/framesave command works like an enum
984 * similar to fteqcc we handle this in the lexer.
985 * The reason for this is that it is sensitive to newlines,
986 * which the parser is unaware of
988 if (!lex_finish_frames(lex))
989 return (lex->tok.ttype = TOKEN_ERROR);
993 if (!strcmp(v, "framevalue"))
996 while (ch != FS_FILE_EOF && util_isspace(ch) && ch != '\n')
999 if (!util_isdigit(ch)) {
1000 lexerror(lex, "$framevalue requires an integer parameter");
1005 lex->tok.ttype = lex_finish_digit(lex, ch);
1007 if (lex->tok.ttype != TOKEN_INTCONST) {
1008 lexerror(lex, "$framevalue requires an integer parameter");
1011 lex->framevalue = lex->tok.constval.i;
1015 if (!strcmp(v, "framerestore"))
1021 rc = lex_parse_frame(lex);
1024 lexerror(lex, "$framerestore requires a framename parameter");
1028 return (lex->tok.ttype = TOKEN_FATAL);
1031 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1032 if (!strcmp(v, lex->frames[frame].name)) {
1033 lex->framevalue = lex->frames[frame].value;
1037 lexerror(lex, "unknown framename `%s`", v);
1041 if (!strcmp(v, "modelname"))
1047 rc = lex_parse_frame(lex);
1050 lexerror(lex, "$modelname requires a parameter");
1054 return (lex->tok.ttype = TOKEN_FATAL);
1056 if (lex->modelname) {
1058 m.value = lex->framevalue;
1059 m.name = lex->modelname;
1060 lex->modelname = NULL;
1061 vec_push(lex->frames, m);
1063 lex->modelname = lex->tok.value;
1064 lex->tok.value = NULL;
1068 if (!strcmp(v, "flush"))
1071 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1072 mem_d(lex->frames[fi].name);
1073 vec_free(lex->frames);
1074 /* skip line (fteqcc does it too) */
1075 ch = lex_getch(lex);
1076 while (ch != FS_FILE_EOF && ch != '\n')
1077 ch = lex_getch(lex);
1081 if (!strcmp(v, "cd") ||
1082 !strcmp(v, "origin") ||
1083 !strcmp(v, "base") ||
1084 !strcmp(v, "flags") ||
1085 !strcmp(v, "scale") ||
1089 ch = lex_getch(lex);
1090 while (ch != FS_FILE_EOF && ch != '\n')
1091 ch = lex_getch(lex);
1095 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1096 if (!strcmp(v, lex->frames[frame].name)) {
1097 lex->tok.constval.i = lex->frames[frame].value;
1098 return (lex->tok.ttype = TOKEN_INTCONST);
1102 lexerror(lex, "invalid frame macro");
1106 /* single-character tokens */
1110 nextch = lex_getch(lex);
1111 if (nextch == '[') {
1112 lex_tokench(lex, ch);
1113 lex_tokench(lex, nextch);
1115 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1117 lex_ungetch(lex, nextch);
1122 lex_tokench(lex, ch);
1124 if (lex->flags.noops)
1125 return (lex->tok.ttype = ch);
1127 return (lex->tok.ttype = TOKEN_OPERATOR);
1130 if (lex->flags.noops) {
1131 nextch = lex_getch(lex);
1132 if (nextch == ']') {
1133 lex_tokench(lex, ch);
1134 lex_tokench(lex, nextch);
1136 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1138 lex_ungetch(lex, nextch);
1147 lex_tokench(lex, ch);
1149 return (lex->tok.ttype = ch);
1155 nextch = lex_getch(lex);
1156 /* digits starting with a dot */
1157 if (util_isdigit(nextch)) {
1158 lex_ungetch(lex, nextch);
1159 lex->tok.ttype = lex_finish_digit(lex, ch);
1161 return lex->tok.ttype;
1163 lex_ungetch(lex, nextch);
1166 if (lex->flags.noops)
1168 /* Detect characters early which are normally
1169 * operators OR PART of an operator.
1184 lex_tokench(lex, ch);
1186 return (lex->tok.ttype = ch);
1194 lex_tokench(lex, ch);
1195 /* peak ahead once */
1196 nextch = lex_getch(lex);
1197 if (nextch != '.') {
1198 lex_ungetch(lex, nextch);
1200 if (lex->flags.noops)
1201 return (lex->tok.ttype = ch);
1203 return (lex->tok.ttype = TOKEN_OPERATOR);
1205 /* peak ahead again */
1206 nextch = lex_getch(lex);
1207 if (nextch != '.') {
1208 lex_ungetch(lex, nextch);
1209 lex_ungetch(lex, '.');
1211 if (lex->flags.noops)
1212 return (lex->tok.ttype = ch);
1214 return (lex->tok.ttype = TOKEN_OPERATOR);
1216 /* fill the token to be "..." */
1217 lex_tokench(lex, ch);
1218 lex_tokench(lex, ch);
1220 return (lex->tok.ttype = TOKEN_DOTS);
1223 if (ch == ',' || ch == '.') {
1224 lex_tokench(lex, ch);
1226 return (lex->tok.ttype = TOKEN_OPERATOR);
1229 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1230 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1231 ch == '=' || ch == '!' || /* <=>, ==, != */
1232 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1233 ch == '~' || ch == '^' /* ~=, ~, ^ */
1235 lex_tokench(lex, ch);
1237 nextch = lex_getch(lex);
1238 if ((nextch == '=' && ch != '<') ||
1239 (nextch == ch && ch != '!') ||
1240 (nextch == '<' && ch == '>')) {
1241 lex_tokench(lex, nextch);
1242 } else if (ch == '<' && nextch == '=') {
1243 lex_tokench(lex, nextch);
1244 if ((thirdch = lex_getch(lex)) == '>')
1245 lex_tokench(lex, thirdch);
1247 lex_ungetch(lex, thirdch);
1249 } else if (ch == '-' && nextch == '>') {
1250 lex_tokench(lex, nextch);
1251 } else if (ch == '&' && nextch == '~') {
1252 thirdch = lex_getch(lex);
1253 if (thirdch != '=') {
1254 lex_ungetch(lex, thirdch);
1255 lex_ungetch(lex, nextch);
1258 lex_tokench(lex, nextch);
1259 lex_tokench(lex, thirdch);
1262 else if (lex->flags.preprocessing &&
1263 ch == '-' && util_isdigit(nextch))
1265 lex->tok.ttype = lex_finish_digit(lex, nextch);
1266 if (lex->tok.ttype == TOKEN_INTCONST)
1267 lex->tok.constval.i = -lex->tok.constval.i;
1269 lex->tok.constval.f = -lex->tok.constval.f;
1271 return lex->tok.ttype;
1273 lex_ungetch(lex, nextch);
1277 return (lex->tok.ttype = TOKEN_OPERATOR);
1280 if (ch == '*' || ch == '/') /* *=, /= */
1282 lex_tokench(lex, ch);
1284 nextch = lex_getch(lex);
1285 if (nextch == '=' || nextch == '*') {
1286 lex_tokench(lex, nextch);
1288 lex_ungetch(lex, nextch);
1291 return (lex->tok.ttype = TOKEN_OPERATOR);
1295 lex_tokench(lex, ch);
1297 return (lex->tok.ttype = TOKEN_OPERATOR);
1300 if (isident_start(ch))
1304 lex_tokench(lex, ch);
1305 if (!lex_finish_ident(lex)) {
1307 return (lex->tok.ttype = TOKEN_ERROR);
1310 lex->tok.ttype = TOKEN_IDENT;
1313 if (!strcmp(v, "void")) {
1314 lex->tok.ttype = TOKEN_TYPENAME;
1315 lex->tok.constval.t = TYPE_VOID;
1316 } else if (!strcmp(v, "int")) {
1317 lex->tok.ttype = TOKEN_TYPENAME;
1318 lex->tok.constval.t = TYPE_INTEGER;
1319 } else if (!strcmp(v, "float")) {
1320 lex->tok.ttype = TOKEN_TYPENAME;
1321 lex->tok.constval.t = TYPE_FLOAT;
1322 } else if (!strcmp(v, "string")) {
1323 lex->tok.ttype = TOKEN_TYPENAME;
1324 lex->tok.constval.t = TYPE_STRING;
1325 } else if (!strcmp(v, "entity")) {
1326 lex->tok.ttype = TOKEN_TYPENAME;
1327 lex->tok.constval.t = TYPE_ENTITY;
1328 } else if (!strcmp(v, "vector")) {
1329 lex->tok.ttype = TOKEN_TYPENAME;
1330 lex->tok.constval.t = TYPE_VECTOR;
1333 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1334 if (!strcmp(v, keywords_qc[kw]))
1335 return (lex->tok.ttype = TOKEN_KEYWORD);
1337 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1338 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1339 if (!strcmp(v, keywords_fg[kw]))
1340 return (lex->tok.ttype = TOKEN_KEYWORD);
1345 return lex->tok.ttype;
1350 lex->flags.nodigraphs = true;
1351 if (lex->flags.preprocessing)
1352 lex_tokench(lex, ch);
1353 lex->tok.ttype = lex_finish_string(lex, '"');
1354 if (lex->flags.preprocessing)
1355 lex_tokench(lex, ch);
1356 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1358 /* Allow c style "string" "continuation" */
1359 ch = lex_skipwhite(lex, false);
1361 lex_ungetch(lex, ch);
1365 lex->tok.ttype = lex_finish_string(lex, '"');
1367 lex->flags.nodigraphs = false;
1369 return lex->tok.ttype;
1374 /* we parse character constants like string,
1375 * but return TOKEN_CHARCONST, or a vector type if it fits...
1376 * Likewise actual unescaping has to be done by the parser.
1377 * The difference is we don't allow 'char' 'continuation'.
1379 if (lex->flags.preprocessing)
1380 lex_tokench(lex, ch);
1381 lex->tok.ttype = lex_finish_string(lex, '\'');
1382 if (lex->flags.preprocessing)
1383 lex_tokench(lex, ch);
1386 lex->tok.ttype = TOKEN_CHARCONST;
1388 /* It's a vector if we can successfully scan 3 floats */
1389 if (util_sscanf(lex->tok.value, " %f %f %f ",
1390 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1393 lex->tok.ttype = TOKEN_VECTORCONST;
1397 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1399 /* check for a valid utf8 character */
1400 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1401 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1402 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1403 : "multibyte character: `%s`" ),
1405 return (lex->tok.ttype = TOKEN_ERROR);
1408 lex->tok.constval.i = u8char;
1411 lex->tok.constval.i = lex->tok.value[0];
1414 return lex->tok.ttype;
1417 if (util_isdigit(ch))
1419 lex->tok.ttype = lex_finish_digit(lex, ch);
1421 return lex->tok.ttype;
1424 if (lex->flags.preprocessing) {
1425 lex_tokench(lex, ch);
1427 return (lex->tok.ttype = ch);
1430 lexerror(lex, "unknown token: `%c`", ch);
1431 return (lex->tok.ttype = TOKEN_ERROR);