2 * Copyright (C) 2012, 2013, 2014, 2015
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, ' ');
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)
684 int ch = 0, texttype = 0;
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 'r': ch = '\r'; break;
721 case 'n': ch = '\n'; break;
722 case 't': ch = '\t'; break;
723 case 'f': ch = '\f'; break;
724 case 'v': ch = '\v'; break;
727 /* same procedure as in fteqcc */
729 nextch = lex_getch(lex);
730 if (nextch >= '0' && nextch <= '9')
732 else if (nextch >= 'a' && nextch <= 'f')
733 ch += nextch - 'a' + 10;
734 else if (nextch >= 'A' && nextch <= 'F')
735 ch += nextch - 'A' + 10;
737 lexerror(lex, "bad character code");
738 lex_ungetch(lex, nextch);
739 return (lex->tok.ttype = TOKEN_ERROR);
743 nextch = lex_getch(lex);
744 if (nextch >= '0' && nextch <= '9')
746 else if (nextch >= 'a' && nextch <= 'f')
747 ch += nextch - 'a' + 10;
748 else if (nextch >= 'A' && nextch <= 'F')
749 ch += nextch - 'A' + 10;
751 lexerror(lex, "bad character code");
752 lex_ungetch(lex, nextch);
753 return (lex->tok.ttype = TOKEN_ERROR);
758 case '0': case '1': case '2': case '3':
759 case '4': case '5': case '6': case '7':
763 case '<': ch = 29; break;
764 case '-': ch = 30; break;
765 case '>': ch = 31; break;
766 case '[': ch = 16; break;
767 case ']': ch = 17; break;
770 nextch = lex_getch(lex);
771 hex = (nextch == 'x');
772 oct = (nextch == '0');
774 lex_ungetch(lex, nextch);
775 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
777 if (nextch >= '0' && nextch <= '9')
778 chr = chr * 10 + nextch - '0';
780 lexerror(lex, "bad character code");
781 return (lex->tok.ttype = TOKEN_ERROR);
784 if (nextch >= '0' && nextch <= '9')
785 chr = chr * 0x10 + nextch - '0';
786 else if (nextch >= 'a' && nextch <= 'f')
787 chr = chr * 0x10 + nextch - 'a' + 10;
788 else if (nextch >= 'A' && nextch <= 'F')
789 chr = chr * 0x10 + nextch - 'A' + 10;
791 lexerror(lex, "bad character code");
792 return (lex->tok.ttype = TOKEN_ERROR);
795 if (nextch >= '0' && nextch <= '9')
796 chr = chr * 8 + chr - '0';
798 lexerror(lex, "bad character code");
799 return (lex->tok.ttype = TOKEN_ERROR);
802 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
804 lexerror(lex, "character code out of range");
805 return (lex->tok.ttype = TOKEN_ERROR);
808 if (OPTS_FLAG(UTF8) && chr >= 128) {
809 u8len = utf8_from(u8buf, chr);
814 lex->column += u8len;
815 for (uc = 0; uc < u8len; ++uc)
816 lex_tokench(lex, u8buf[uc]);
818 * the last character will be inserted with the tokench() call
838 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
839 /* so we just add the character plus backslash no matter what it actually is */
840 lex_tokench(lex, '\\');
842 /* add the character finally */
843 lex_tokench(lex, ch | texttype);
846 lex_tokench(lex, ch);
848 lexerror(lex, "unexpected end of file within string constant");
849 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
850 return (lex->tok.ttype = TOKEN_ERROR);
853 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
859 /* parse a number... */
861 lex->tok.ttype = TOKEN_FLOATCONST;
863 lex->tok.ttype = TOKEN_INTCONST;
865 lex_tokench(lex, ch);
868 if (ch != '.' && !util_isdigit(ch))
870 if (lastch != '0' || ch != 'x')
872 /* end of the number or EOF */
873 lex_ungetch(lex, ch);
876 lex->tok.constval.i = lastch - '0';
877 return lex->tok.ttype;
883 /* EOF would have been caught above */
887 lex_tokench(lex, ch);
889 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
891 lex_tokench(lex, ch);
895 /* NOT else, '.' can come from above as well */
896 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
898 /* Allow floating comma in non-hex mode */
899 lex->tok.ttype = TOKEN_FLOATCONST;
900 lex_tokench(lex, ch);
902 /* continue digits-only */
904 while (util_isdigit(ch))
906 lex_tokench(lex, ch);
910 /* put back the last character */
911 /* but do not put back the trailing 'f' or a float */
912 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
915 /* generally we don't want words to follow numbers: */
917 lexerror(lex, "unexpected trailing characters after number");
918 return (lex->tok.ttype = TOKEN_ERROR);
920 lex_ungetch(lex, ch);
923 if (lex->tok.ttype == TOKEN_FLOATCONST)
924 lex->tok.constval.f = strtod(lex->tok.value, NULL);
926 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
927 return lex->tok.ttype;
930 int lex_do(lex_file *lex)
932 int ch, nextch, thirdch;
933 bool hadwhite = false;
938 ch = lex_skipwhite(lex, hadwhite);
940 if (!lex->flags.mergelines || ch != '\\')
946 lex_ungetch(lex, ch);
950 /* we reached a linemerge */
951 lex_tokench(lex, '\n');
955 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
956 return (lex->tok.ttype = ch);
959 lex->sline = lex->line;
960 lex->tok.ctx.line = lex->sline;
961 lex->tok.ctx.file = lex->name;
964 return (lex->tok.ttype = TOKEN_FATAL);
966 if (ch == FS_FILE_EOF) {
968 return (lex->tok.ttype = TOKEN_EOF);
971 /* modelgen / spiritgen commands */
972 if (ch == '$' && !lex->flags.preprocessing) {
977 if (!isident_start(ch)) {
978 lexerror(lex, "hanging '$' modelgen/spritegen command line");
981 lex_tokench(lex, ch);
982 if (!lex_finish_ident(lex))
983 return (lex->tok.ttype = TOKEN_ERROR);
985 /* skip the known commands */
988 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
990 /* frame/framesave command works like an enum
991 * similar to fteqcc we handle this in the lexer.
992 * The reason for this is that it is sensitive to newlines,
993 * which the parser is unaware of
995 if (!lex_finish_frames(lex))
996 return (lex->tok.ttype = TOKEN_ERROR);
1000 if (!strcmp(v, "framevalue"))
1002 ch = lex_getch(lex);
1003 while (ch != FS_FILE_EOF && util_isspace(ch) && ch != '\n')
1004 ch = lex_getch(lex);
1006 if (!util_isdigit(ch)) {
1007 lexerror(lex, "$framevalue requires an integer parameter");
1012 lex->tok.ttype = lex_finish_digit(lex, ch);
1014 if (lex->tok.ttype != TOKEN_INTCONST) {
1015 lexerror(lex, "$framevalue requires an integer parameter");
1018 lex->framevalue = lex->tok.constval.i;
1022 if (!strcmp(v, "framerestore"))
1028 rc = lex_parse_frame(lex);
1031 lexerror(lex, "$framerestore requires a framename parameter");
1035 return (lex->tok.ttype = TOKEN_FATAL);
1038 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1039 if (!strcmp(v, lex->frames[frame].name)) {
1040 lex->framevalue = lex->frames[frame].value;
1044 lexerror(lex, "unknown framename `%s`", v);
1048 if (!strcmp(v, "modelname"))
1054 rc = lex_parse_frame(lex);
1057 lexerror(lex, "$modelname requires a parameter");
1061 return (lex->tok.ttype = TOKEN_FATAL);
1063 if (lex->modelname) {
1065 m.value = lex->framevalue;
1066 m.name = lex->modelname;
1067 lex->modelname = NULL;
1068 vec_push(lex->frames, m);
1070 lex->modelname = lex->tok.value;
1071 lex->tok.value = NULL;
1075 if (!strcmp(v, "flush"))
1078 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1079 mem_d(lex->frames[fi].name);
1080 vec_free(lex->frames);
1081 /* skip line (fteqcc does it too) */
1082 ch = lex_getch(lex);
1083 while (ch != FS_FILE_EOF && ch != '\n')
1084 ch = lex_getch(lex);
1088 if (!strcmp(v, "cd") ||
1089 !strcmp(v, "origin") ||
1090 !strcmp(v, "base") ||
1091 !strcmp(v, "flags") ||
1092 !strcmp(v, "scale") ||
1096 ch = lex_getch(lex);
1097 while (ch != FS_FILE_EOF && ch != '\n')
1098 ch = lex_getch(lex);
1102 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1103 if (!strcmp(v, lex->frames[frame].name)) {
1104 lex->tok.constval.i = lex->frames[frame].value;
1105 return (lex->tok.ttype = TOKEN_INTCONST);
1109 lexerror(lex, "invalid frame macro");
1113 /* single-character tokens */
1117 nextch = lex_getch(lex);
1118 if (nextch == '[') {
1119 lex_tokench(lex, ch);
1120 lex_tokench(lex, nextch);
1122 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1124 lex_ungetch(lex, nextch);
1129 lex_tokench(lex, ch);
1131 if (lex->flags.noops)
1132 return (lex->tok.ttype = ch);
1134 return (lex->tok.ttype = TOKEN_OPERATOR);
1137 if (lex->flags.noops) {
1138 nextch = lex_getch(lex);
1139 if (nextch == ']') {
1140 lex_tokench(lex, ch);
1141 lex_tokench(lex, nextch);
1143 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1145 lex_ungetch(lex, nextch);
1154 lex_tokench(lex, ch);
1156 return (lex->tok.ttype = ch);
1162 nextch = lex_getch(lex);
1163 /* digits starting with a dot */
1164 if (util_isdigit(nextch)) {
1165 lex_ungetch(lex, nextch);
1166 lex->tok.ttype = lex_finish_digit(lex, ch);
1168 return lex->tok.ttype;
1170 lex_ungetch(lex, nextch);
1173 if (lex->flags.noops)
1175 /* Detect characters early which are normally
1176 * operators OR PART of an operator.
1191 lex_tokench(lex, ch);
1193 return (lex->tok.ttype = ch);
1201 lex_tokench(lex, ch);
1202 /* peak ahead once */
1203 nextch = lex_getch(lex);
1204 if (nextch != '.') {
1205 lex_ungetch(lex, nextch);
1207 if (lex->flags.noops)
1208 return (lex->tok.ttype = ch);
1210 return (lex->tok.ttype = TOKEN_OPERATOR);
1212 /* peak ahead again */
1213 nextch = lex_getch(lex);
1214 if (nextch != '.') {
1215 lex_ungetch(lex, nextch);
1216 lex_ungetch(lex, '.');
1218 if (lex->flags.noops)
1219 return (lex->tok.ttype = ch);
1221 return (lex->tok.ttype = TOKEN_OPERATOR);
1223 /* fill the token to be "..." */
1224 lex_tokench(lex, ch);
1225 lex_tokench(lex, ch);
1227 return (lex->tok.ttype = TOKEN_DOTS);
1230 if (ch == ',' || ch == '.') {
1231 lex_tokench(lex, ch);
1233 return (lex->tok.ttype = TOKEN_OPERATOR);
1236 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1237 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1238 ch == '=' || ch == '!' || /* <=>, ==, != */
1239 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1240 ch == '~' || ch == '^' /* ~=, ~, ^ */
1242 lex_tokench(lex, ch);
1243 nextch = lex_getch(lex);
1245 if ((nextch == '=' && ch != '<') || (nextch == '<' && ch == '>'))
1246 lex_tokench(lex, nextch);
1247 else if (nextch == ch && ch != '!') {
1248 lex_tokench(lex, nextch);
1249 if ((thirdch = lex_getch(lex)) == '=')
1250 lex_tokench(lex, thirdch);
1252 lex_ungetch(lex, thirdch);
1253 } else if (ch == '<' && nextch == '=') {
1254 lex_tokench(lex, nextch);
1255 if ((thirdch = lex_getch(lex)) == '>')
1256 lex_tokench(lex, thirdch);
1258 lex_ungetch(lex, thirdch);
1260 } else if (ch == '-' && nextch == '>') {
1261 lex_tokench(lex, nextch);
1262 } else if (ch == '&' && nextch == '~') {
1263 thirdch = lex_getch(lex);
1264 if (thirdch != '=') {
1265 lex_ungetch(lex, thirdch);
1266 lex_ungetch(lex, nextch);
1269 lex_tokench(lex, nextch);
1270 lex_tokench(lex, thirdch);
1273 else if (lex->flags.preprocessing &&
1274 ch == '-' && util_isdigit(nextch))
1276 lex->tok.ttype = lex_finish_digit(lex, nextch);
1277 if (lex->tok.ttype == TOKEN_INTCONST)
1278 lex->tok.constval.i = -lex->tok.constval.i;
1280 lex->tok.constval.f = -lex->tok.constval.f;
1282 return lex->tok.ttype;
1284 lex_ungetch(lex, nextch);
1288 return (lex->tok.ttype = TOKEN_OPERATOR);
1291 if (ch == '*' || ch == '/') /* *=, /= */
1293 lex_tokench(lex, ch);
1295 nextch = lex_getch(lex);
1296 if (nextch == '=' || nextch == '*') {
1297 lex_tokench(lex, nextch);
1299 lex_ungetch(lex, nextch);
1302 return (lex->tok.ttype = TOKEN_OPERATOR);
1306 lex_tokench(lex, ch);
1308 return (lex->tok.ttype = TOKEN_OPERATOR);
1311 if (isident_start(ch))
1315 lex_tokench(lex, ch);
1316 if (!lex_finish_ident(lex)) {
1318 return (lex->tok.ttype = TOKEN_ERROR);
1321 lex->tok.ttype = TOKEN_IDENT;
1324 if (!strcmp(v, "void")) {
1325 lex->tok.ttype = TOKEN_TYPENAME;
1326 lex->tok.constval.t = TYPE_VOID;
1327 } else if (!strcmp(v, "int")) {
1328 lex->tok.ttype = TOKEN_TYPENAME;
1329 lex->tok.constval.t = TYPE_INTEGER;
1330 } else if (!strcmp(v, "float")) {
1331 lex->tok.ttype = TOKEN_TYPENAME;
1332 lex->tok.constval.t = TYPE_FLOAT;
1333 } else if (!strcmp(v, "string")) {
1334 lex->tok.ttype = TOKEN_TYPENAME;
1335 lex->tok.constval.t = TYPE_STRING;
1336 } else if (!strcmp(v, "entity")) {
1337 lex->tok.ttype = TOKEN_TYPENAME;
1338 lex->tok.constval.t = TYPE_ENTITY;
1339 } else if (!strcmp(v, "vector")) {
1340 lex->tok.ttype = TOKEN_TYPENAME;
1341 lex->tok.constval.t = TYPE_VECTOR;
1342 } else if (!strcmp(v, "_length")) {
1343 lex->tok.ttype = TOKEN_OPERATOR;
1346 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1347 if (!strcmp(v, keywords_qc[kw]))
1348 return (lex->tok.ttype = TOKEN_KEYWORD);
1350 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1351 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1352 if (!strcmp(v, keywords_fg[kw]))
1353 return (lex->tok.ttype = TOKEN_KEYWORD);
1358 return lex->tok.ttype;
1363 lex->flags.nodigraphs = true;
1364 if (lex->flags.preprocessing)
1365 lex_tokench(lex, ch);
1366 lex->tok.ttype = lex_finish_string(lex, '"');
1367 if (lex->flags.preprocessing)
1368 lex_tokench(lex, ch);
1369 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1371 /* Allow c style "string" "continuation" */
1372 ch = lex_skipwhite(lex, false);
1374 lex_ungetch(lex, ch);
1378 lex->tok.ttype = lex_finish_string(lex, '"');
1380 lex->flags.nodigraphs = false;
1382 return lex->tok.ttype;
1387 /* we parse character constants like string,
1388 * but return TOKEN_CHARCONST, or a vector type if it fits...
1389 * Likewise actual unescaping has to be done by the parser.
1390 * The difference is we don't allow 'char' 'continuation'.
1392 if (lex->flags.preprocessing)
1393 lex_tokench(lex, ch);
1394 lex->tok.ttype = lex_finish_string(lex, '\'');
1395 if (lex->flags.preprocessing)
1396 lex_tokench(lex, ch);
1399 lex->tok.ttype = TOKEN_CHARCONST;
1401 /* It's a vector if we can successfully scan 3 floats */
1402 if (util_sscanf(lex->tok.value, " %f %f %f ",
1403 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1406 lex->tok.ttype = TOKEN_VECTORCONST;
1410 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1412 /* check for a valid utf8 character */
1413 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1414 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1415 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1416 : "multibyte character: `%s`" ),
1418 return (lex->tok.ttype = TOKEN_ERROR);
1421 lex->tok.constval.i = u8char;
1424 lex->tok.constval.i = lex->tok.value[0];
1427 return lex->tok.ttype;
1430 if (util_isdigit(ch))
1432 lex->tok.ttype = lex_finish_digit(lex, ch);
1434 return lex->tok.ttype;
1437 if (lex->flags.preprocessing) {
1438 lex_tokench(lex, ch);
1440 return (lex->tok.ttype = ch);
1443 lexerror(lex, "unknown token: `%c`", ch);
1444 return (lex->tok.ttype = TOKEN_ERROR);