12 static const char *keywords_qc[] = {
20 static const char *keywords_fg[] = {
21 "switch", "case", "default",
27 "__builtin_debug_printtype"
33 static char* *lex_filenames;
35 static void lexerror(lex_file *lex, const char *fmt, ...)
41 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
43 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
47 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
54 ctx.line = lex->sline;
55 ctx.column = lex->column;
58 r = vcompile_warning(ctx, warntype, fmt, ap);
63 static void lex_token_new(lex_file *lex)
66 vec_shrinkto(lex->tok.value, 0);
68 lex->tok.constval.t = TYPE_VOID;
69 lex->tok.ctx.line = lex->sline;
70 lex->tok.ctx.file = lex->name;
71 lex->tok.ctx.column = lex->column;
74 static void lex_ungetch(lex_file *lex, int ch);
75 static int lex_getch(lex_file *lex);
77 lex_file* lex_open(const char *file)
80 FILE *in = fopen(file, "rb");
84 lexerror(nullptr, "open failed: '%s'\n", file);
88 lex = (lex_file*)mem_a(sizeof(*lex));
91 lexerror(nullptr, "out of memory\n");
95 memset(lex, 0, sizeof(*lex));
98 lex->name = util_strdup(file);
99 lex->line = 1; /* we start counting at 1 */
105 if ((read = (lex_getch(lex) << 16) | (lex_getch(lex) << 8) | lex_getch(lex)) != 0xEFBBBF) {
106 lex_ungetch(lex, (read & 0x0000FF));
107 lex_ungetch(lex, (read & 0x00FF00) >> 8);
108 lex_ungetch(lex, (read & 0xFF0000) >> 16);
111 * otherwise the lexer has advanced 3 bytes for the BOM, we need
112 * to set the column back to 0
117 vec_push(lex_filenames, lex->name);
121 lex_file* lex_open_string(const char *str, size_t len, const char *name)
125 lex = (lex_file*)mem_a(sizeof(*lex));
127 lexerror(nullptr, "out of memory\n");
131 memset(lex, 0, sizeof(*lex));
134 lex->open_string = str;
135 lex->open_string_length = len;
136 lex->open_string_pos = 0;
138 lex->name = util_strdup(name ? name : "<string-source>");
139 lex->line = 1; /* we start counting at 1 */
144 vec_push(lex_filenames, lex->name);
149 void lex_cleanup(void)
152 for (i = 0; i < vec_size(lex_filenames); ++i)
153 mem_d(lex_filenames[i]);
154 vec_free(lex_filenames);
157 void lex_close(lex_file *lex)
160 for (i = 0; i < vec_size(lex->frames); ++i)
161 mem_d(lex->frames[i].name);
162 vec_free(lex->frames);
165 vec_free(lex->modelname);
170 vec_free(lex->tok.value);
172 /* mem_d(lex->name); collected in lex_filenames */
178 static int lex_fgetc(lex_file *lex)
182 return fgetc(lex->file);
184 if (lex->open_string) {
185 if (lex->open_string_pos >= lex->open_string_length)
188 return lex->open_string[lex->open_string_pos++];
193 /* Get or put-back data
194 * The following to functions do NOT understand what kind of data they
196 * The are merely wrapping get/put in order to count line numbers.
198 static int lex_try_trigraph(lex_file *lex, int old)
202 if (!lex->push_line && c2 == '\n') {
208 lex_ungetch(lex, c2);
213 if (!lex->push_line && c3 == '\n') {
219 case '=': return '#';
220 case '/': return '\\';
221 case '\'': return '^';
222 case '(': return '[';
223 case ')': return ']';
224 case '!': return '|';
225 case '<': return '{';
226 case '>': return '}';
227 case '-': return '~';
229 lex_ungetch(lex, c3);
230 lex_ungetch(lex, c2);
235 static int lex_try_digraph(lex_file *lex, int ch)
239 /* we just used fgetc() so count lines
240 * need to offset a \n the ungetch would recognize
242 if (!lex->push_line && c2 == '\n')
244 if (ch == '<' && c2 == ':')
246 else if (ch == ':' && c2 == '>')
248 else if (ch == '<' && c2 == '%')
250 else if (ch == '%' && c2 == '>')
252 else if (ch == '%' && c2 == ':')
254 lex_ungetch(lex, c2);
258 static int lex_getch(lex_file *lex)
264 if (!lex->push_line && lex->peek[lex->peekpos] == '\n') {
268 return lex->peek[lex->peekpos];
272 if (!lex->push_line && ch == '\n') {
277 return lex_try_trigraph(lex, ch);
278 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
279 return lex_try_digraph(lex, ch);
283 static void lex_ungetch(lex_file *lex, int ch)
285 lex->peek[lex->peekpos++] = ch;
287 if (!lex->push_line && ch == '\n') {
293 /* classify characters
294 * some additions to the is*() functions of ctype.h
297 /* Idents are alphanumberic, but they start with alpha or _ */
298 static bool isident_start(int ch)
300 return util_isalpha(ch) || ch == '_';
303 static bool isident(int ch)
305 return isident_start(ch) || util_isdigit(ch);
308 /* isxdigit_only is used when we already know it's not a digit
309 * and want to see if it's a hex digit anyway.
311 static bool isxdigit_only(int ch)
313 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
316 /* Append a character to the token buffer */
317 static void lex_tokench(lex_file *lex, int ch)
319 vec_push(lex->tok.value, ch);
322 /* Append a trailing null-byte */
323 static void lex_endtoken(lex_file *lex)
325 vec_push(lex->tok.value, 0);
326 vec_shrinkby(lex->tok.value, 1);
329 static bool lex_try_pragma(lex_file *lex)
332 char *pragma = nullptr;
333 char *command = nullptr;
334 char *param = nullptr;
337 if (lex->flags.preprocessing)
344 lex_ungetch(lex, ch);
348 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
349 vec_push(pragma, ch);
352 if (ch != ' ' || strcmp(pragma, "pragma")) {
353 lex_ungetch(lex, ch);
357 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
358 vec_push(command, ch);
359 vec_push(command, 0);
362 lex_ungetch(lex, ch);
366 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
371 lex_ungetch(lex, ch);
375 if (!strcmp(command, "push")) {
376 if (!strcmp(param, "line")) {
378 if (lex->push_line == 1)
384 else if (!strcmp(command, "pop")) {
385 if (!strcmp(param, "line")) {
388 if (lex->push_line == 0)
394 else if (!strcmp(command, "file")) {
395 lex->name = util_strdup(param);
396 vec_push(lex_filenames, lex->name);
398 else if (!strcmp(command, "line")) {
399 line = strtol(param, nullptr, 0)-1;
405 while (ch != '\n' && ch != EOF)
415 while (vec_size(command)) {
416 lex_ungetch(lex, (unsigned char)vec_last(command));
420 lex_ungetch(lex, ' ');
424 while (vec_size(param)) {
425 lex_ungetch(lex, (unsigned char)vec_last(param));
429 lex_ungetch(lex, ' ');
433 while (vec_size(pragma)) {
434 lex_ungetch(lex, (unsigned char)vec_last(pragma));
439 lex_ungetch(lex, '#');
445 /* Skip whitespace and comments and return the first
446 * non-white character.
447 * As this makes use of the above getch() ungetch() functions,
448 * we don't need to care at all about line numbering anymore.
450 * In theory, this function should only be used at the beginning
451 * of lexing, or when we *know* the next character is part of the token.
452 * Otherwise, if the parser throws an error, the linenumber may not be
453 * the line of the error, but the line of the next token AFTER the error.
455 * This is currently only problematic when using c-like string-continuation,
456 * since comments and whitespaces are allowed between 2 such strings.
460 "A continuation of the previous string"
461 // This line is skipped
464 * In this case, if the parse decides it didn't actually want a string,
465 * and uses lex->line to print an error, it will show the ', foo);' line's
468 * On the other hand, the parser is supposed to remember the line of the next
469 * token's beginning. In this case we would want skipwhite() to be called
470 * AFTER reading a token, so that the parser, before reading the NEXT token,
471 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
474 * here is to store the line of the first character after skipping
475 * the initial whitespace in lex->sline, this happens in lex_do.
477 static int lex_skipwhite(lex_file *lex, bool hadwhite)
480 bool haswhite = hadwhite;
485 while (ch != EOF && util_isspace(ch)) {
487 if (lex_try_pragma(lex))
490 if (lex->flags.preprocessing) {
493 /* see if there was whitespace first */
494 if (haswhite) { /* (vec_size(lex->tok.value)) { */
495 lex_ungetch(lex, ch);
499 /* otherwise return EOL */
503 lex_tokench(lex, ch);
512 /* one line comment */
515 if (lex->flags.preprocessing) {
517 lex_tokench(lex, ' ');
518 lex_tokench(lex, ' ');
521 while (ch != EOF && ch != '\n') {
522 if (lex->flags.preprocessing)
523 lex_tokench(lex, ' '); /* ch); */
526 if (lex->flags.preprocessing) {
527 lex_ungetch(lex, '\n');
535 /* multiline comment */
536 if (lex->flags.preprocessing) {
538 lex_tokench(lex, ' ');
539 lex_tokench(lex, ' ');
548 if (lex->flags.preprocessing) {
549 lex_tokench(lex, ' ');
550 lex_tokench(lex, ' ');
554 lex_ungetch(lex, ch);
556 if (lex->flags.preprocessing) {
558 lex_tokench(lex, '\n');
560 lex_tokench(lex, ' ');
563 ch = ' '; /* cause TRUE in the isspace check */
566 /* Otherwise roll back to the slash and break out of the loop */
567 lex_ungetch(lex, ch);
571 } while (ch != EOF && util_isspace(ch));
575 lex_ungetch(lex, ch);
582 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
587 while (ch != EOF && isident(ch))
589 lex_tokench(lex, ch);
593 /* last ch was not an ident ch: */
594 lex_ungetch(lex, ch);
599 /* read one ident for the frame list */
600 static int lex_parse_frame(lex_file *lex)
607 while (ch != EOF && ch != '\n' && util_isspace(ch))
613 if (!isident_start(ch)) {
614 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
618 lex_tokench(lex, ch);
619 if (!lex_finish_ident(lex))
625 /* read a list of $frames */
626 static bool lex_finish_frames(lex_file *lex)
633 rc = lex_parse_frame(lex);
634 if (rc > 0) /* end of line */
636 if (rc < 0) /* error */
639 for (i = 0; i < vec_size(lex->frames); ++i) {
640 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
641 lex->frames[i].value = lex->framevalue++;
642 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
647 if (i < vec_size(lex->frames))
650 m.value = lex->framevalue++;
651 m.name = util_strdup(lex->tok.value);
652 vec_shrinkto(lex->tok.value, 0);
653 vec_push(lex->frames, m);
659 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
662 int ch = 0, texttype = 0;
666 char u8buf[8]; /* way more than enough */
673 return TOKEN_STRINGCONST;
675 if (lex->flags.preprocessing && ch == '\\') {
676 lex_tokench(lex, ch);
679 lexerror(lex, "unexpected end of file");
680 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
681 return (lex->tok.ttype = TOKEN_ERROR);
683 lex_tokench(lex, ch);
685 else if (ch == '\\') {
688 lexerror(lex, "unexpected end of file");
689 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
690 return (lex->tok.ttype = TOKEN_ERROR);
697 case 'a': ch = '\a'; break;
698 case 'r': ch = '\r'; break;
699 case 'n': ch = '\n'; break;
700 case 't': ch = '\t'; break;
701 case 'f': ch = '\f'; break;
702 case 'v': ch = '\v'; break;
705 /* same procedure as in fteqcc */
707 nextch = lex_getch(lex);
708 if (nextch >= '0' && nextch <= '9')
710 else if (nextch >= 'a' && nextch <= 'f')
711 ch += nextch - 'a' + 10;
712 else if (nextch >= 'A' && nextch <= 'F')
713 ch += nextch - 'A' + 10;
715 lexerror(lex, "bad character code");
716 lex_ungetch(lex, nextch);
717 return (lex->tok.ttype = TOKEN_ERROR);
721 nextch = lex_getch(lex);
722 if (nextch >= '0' && nextch <= '9')
724 else if (nextch >= 'a' && nextch <= 'f')
725 ch += nextch - 'a' + 10;
726 else if (nextch >= 'A' && nextch <= 'F')
727 ch += nextch - 'A' + 10;
729 lexerror(lex, "bad character code");
730 lex_ungetch(lex, nextch);
731 return (lex->tok.ttype = TOKEN_ERROR);
736 case '0': case '1': case '2': case '3':
737 case '4': case '5': case '6': case '7':
741 case '<': ch = 29; break;
742 case '-': ch = 30; break;
743 case '>': ch = 31; break;
744 case '[': ch = 16; break;
745 case ']': ch = 17; break;
748 nextch = lex_getch(lex);
749 hex = (nextch == 'x');
750 oct = (nextch == '0');
752 lex_ungetch(lex, nextch);
753 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
755 if (nextch >= '0' && nextch <= '9')
756 chr = chr * 10 + nextch - '0';
758 lexerror(lex, "bad character code");
759 return (lex->tok.ttype = TOKEN_ERROR);
762 if (nextch >= '0' && nextch <= '9')
763 chr = chr * 0x10 + nextch - '0';
764 else if (nextch >= 'a' && nextch <= 'f')
765 chr = chr * 0x10 + nextch - 'a' + 10;
766 else if (nextch >= 'A' && nextch <= 'F')
767 chr = chr * 0x10 + nextch - 'A' + 10;
769 lexerror(lex, "bad character code");
770 return (lex->tok.ttype = TOKEN_ERROR);
773 if (nextch >= '0' && nextch <= '9')
774 chr = chr * 8 + chr - '0';
776 lexerror(lex, "bad character code");
777 return (lex->tok.ttype = TOKEN_ERROR);
780 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
782 lexerror(lex, "character code out of range");
783 return (lex->tok.ttype = TOKEN_ERROR);
786 if (OPTS_FLAG(UTF8) && chr >= 128) {
787 u8len = utf8_from(u8buf, chr);
792 lex->column += u8len;
793 for (uc = 0; uc < u8len; ++uc)
794 lex_tokench(lex, u8buf[uc]);
796 * the last character will be inserted with the tokench() call
816 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
817 /* so we just add the character plus backslash no matter what it actually is */
818 lex_tokench(lex, '\\');
820 /* add the character finally */
821 lex_tokench(lex, ch | texttype);
824 lex_tokench(lex, ch);
826 lexerror(lex, "unexpected end of file within string constant");
827 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
828 return (lex->tok.ttype = TOKEN_ERROR);
831 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
837 /* parse a number... */
839 lex->tok.ttype = TOKEN_FLOATCONST;
841 lex->tok.ttype = TOKEN_INTCONST;
843 lex_tokench(lex, ch);
846 if (ch != '.' && !util_isdigit(ch))
848 if (lastch != '0' || ch != 'x')
850 /* end of the number or EOF */
851 lex_ungetch(lex, ch);
854 lex->tok.constval.i = lastch - '0';
855 return lex->tok.ttype;
861 /* EOF would have been caught above */
865 lex_tokench(lex, ch);
867 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
869 lex_tokench(lex, ch);
873 /* NOT else, '.' can come from above as well */
874 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
876 /* Allow floating comma in non-hex mode */
877 lex->tok.ttype = TOKEN_FLOATCONST;
878 lex_tokench(lex, ch);
880 /* continue digits-only */
882 while (util_isdigit(ch))
884 lex_tokench(lex, ch);
888 /* put back the last character */
889 /* but do not put back the trailing 'f' or a float */
890 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
893 /* generally we don't want words to follow numbers: */
895 lexerror(lex, "unexpected trailing characters after number");
896 return (lex->tok.ttype = TOKEN_ERROR);
898 lex_ungetch(lex, ch);
901 if (lex->tok.ttype == TOKEN_FLOATCONST)
902 lex->tok.constval.f = strtod(lex->tok.value, nullptr);
904 lex->tok.constval.i = strtol(lex->tok.value, nullptr, 0);
905 return lex->tok.ttype;
908 int lex_do(lex_file *lex)
910 int ch, nextch, thirdch;
911 bool hadwhite = false;
916 ch = lex_skipwhite(lex, hadwhite);
918 if (!lex->flags.mergelines || ch != '\\')
924 lex_ungetch(lex, ch);
928 /* we reached a linemerge */
929 lex_tokench(lex, '\n');
933 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
934 return (lex->tok.ttype = ch);
937 lex->sline = lex->line;
938 lex->tok.ctx.line = lex->sline;
939 lex->tok.ctx.file = lex->name;
942 return (lex->tok.ttype = TOKEN_FATAL);
946 return (lex->tok.ttype = TOKEN_EOF);
949 /* modelgen / spiritgen commands */
950 if (ch == '$' && !lex->flags.preprocessing) {
955 if (!isident_start(ch)) {
956 lexerror(lex, "hanging '$' modelgen/spritegen command line");
959 lex_tokench(lex, ch);
960 if (!lex_finish_ident(lex))
961 return (lex->tok.ttype = TOKEN_ERROR);
963 /* skip the known commands */
966 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
968 /* frame/framesave command works like an enum
969 * similar to fteqcc we handle this in the lexer.
970 * The reason for this is that it is sensitive to newlines,
971 * which the parser is unaware of
973 if (!lex_finish_frames(lex))
974 return (lex->tok.ttype = TOKEN_ERROR);
978 if (!strcmp(v, "framevalue"))
981 while (ch != EOF && util_isspace(ch) && ch != '\n')
984 if (!util_isdigit(ch)) {
985 lexerror(lex, "$framevalue requires an integer parameter");
990 lex->tok.ttype = lex_finish_digit(lex, ch);
992 if (lex->tok.ttype != TOKEN_INTCONST) {
993 lexerror(lex, "$framevalue requires an integer parameter");
996 lex->framevalue = lex->tok.constval.i;
1000 if (!strcmp(v, "framerestore"))
1006 rc = lex_parse_frame(lex);
1009 lexerror(lex, "$framerestore requires a framename parameter");
1013 return (lex->tok.ttype = TOKEN_FATAL);
1016 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1017 if (!strcmp(v, lex->frames[frame].name)) {
1018 lex->framevalue = lex->frames[frame].value;
1022 lexerror(lex, "unknown framename `%s`", v);
1026 if (!strcmp(v, "modelname"))
1032 rc = lex_parse_frame(lex);
1035 lexerror(lex, "$modelname requires a parameter");
1039 return (lex->tok.ttype = TOKEN_FATAL);
1041 if (lex->modelname) {
1043 m.value = lex->framevalue;
1044 m.name = lex->modelname;
1045 lex->modelname = nullptr;
1046 vec_push(lex->frames, m);
1048 lex->modelname = lex->tok.value;
1049 lex->tok.value = nullptr;
1053 if (!strcmp(v, "flush"))
1056 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1057 mem_d(lex->frames[fi].name);
1058 vec_free(lex->frames);
1059 /* skip line (fteqcc does it too) */
1060 ch = lex_getch(lex);
1061 while (ch != EOF && ch != '\n')
1062 ch = lex_getch(lex);
1066 if (!strcmp(v, "cd") ||
1067 !strcmp(v, "origin") ||
1068 !strcmp(v, "base") ||
1069 !strcmp(v, "flags") ||
1070 !strcmp(v, "scale") ||
1074 ch = lex_getch(lex);
1075 while (ch != EOF && ch != '\n')
1076 ch = lex_getch(lex);
1080 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1081 if (!strcmp(v, lex->frames[frame].name)) {
1082 lex->tok.constval.i = lex->frames[frame].value;
1083 return (lex->tok.ttype = TOKEN_INTCONST);
1087 lexerror(lex, "invalid frame macro");
1091 /* single-character tokens */
1095 nextch = lex_getch(lex);
1096 if (nextch == '[') {
1097 lex_tokench(lex, ch);
1098 lex_tokench(lex, nextch);
1100 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1102 lex_ungetch(lex, nextch);
1107 lex_tokench(lex, ch);
1109 if (lex->flags.noops)
1110 return (lex->tok.ttype = ch);
1112 return (lex->tok.ttype = TOKEN_OPERATOR);
1115 if (lex->flags.noops) {
1116 nextch = lex_getch(lex);
1117 if (nextch == ']') {
1118 lex_tokench(lex, ch);
1119 lex_tokench(lex, nextch);
1121 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1123 lex_ungetch(lex, nextch);
1132 lex_tokench(lex, ch);
1134 return (lex->tok.ttype = ch);
1140 nextch = lex_getch(lex);
1141 /* digits starting with a dot */
1142 if (util_isdigit(nextch)) {
1143 lex_ungetch(lex, nextch);
1144 lex->tok.ttype = lex_finish_digit(lex, ch);
1146 return lex->tok.ttype;
1148 lex_ungetch(lex, nextch);
1151 if (lex->flags.noops)
1153 /* Detect characters early which are normally
1154 * operators OR PART of an operator.
1169 lex_tokench(lex, ch);
1171 return (lex->tok.ttype = ch);
1179 lex_tokench(lex, ch);
1180 /* peak ahead once */
1181 nextch = lex_getch(lex);
1182 if (nextch != '.') {
1183 lex_ungetch(lex, nextch);
1185 if (lex->flags.noops)
1186 return (lex->tok.ttype = ch);
1188 return (lex->tok.ttype = TOKEN_OPERATOR);
1190 /* peak ahead again */
1191 nextch = lex_getch(lex);
1192 if (nextch != '.') {
1193 lex_ungetch(lex, nextch);
1194 lex_ungetch(lex, '.');
1196 if (lex->flags.noops)
1197 return (lex->tok.ttype = ch);
1199 return (lex->tok.ttype = TOKEN_OPERATOR);
1201 /* fill the token to be "..." */
1202 lex_tokench(lex, ch);
1203 lex_tokench(lex, ch);
1205 return (lex->tok.ttype = TOKEN_DOTS);
1208 if (ch == ',' || ch == '.') {
1209 lex_tokench(lex, ch);
1211 return (lex->tok.ttype = TOKEN_OPERATOR);
1214 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1215 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1216 ch == '=' || ch == '!' || /* <=>, ==, != */
1217 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1218 ch == '~' || ch == '^' /* ~=, ~, ^ */
1220 lex_tokench(lex, ch);
1221 nextch = lex_getch(lex);
1223 if ((nextch == '=' && ch != '<') || (nextch == '<' && ch == '>'))
1224 lex_tokench(lex, nextch);
1225 else if (nextch == ch && ch != '!') {
1226 lex_tokench(lex, nextch);
1227 if ((thirdch = lex_getch(lex)) == '=')
1228 lex_tokench(lex, thirdch);
1230 lex_ungetch(lex, thirdch);
1231 } else if (ch == '<' && nextch == '=') {
1232 lex_tokench(lex, nextch);
1233 if ((thirdch = lex_getch(lex)) == '>')
1234 lex_tokench(lex, thirdch);
1236 lex_ungetch(lex, thirdch);
1238 } else if (ch == '-' && nextch == '>') {
1239 lex_tokench(lex, nextch);
1240 } else if (ch == '&' && nextch == '~') {
1241 thirdch = lex_getch(lex);
1242 if (thirdch != '=') {
1243 lex_ungetch(lex, thirdch);
1244 lex_ungetch(lex, nextch);
1247 lex_tokench(lex, nextch);
1248 lex_tokench(lex, thirdch);
1251 else if (lex->flags.preprocessing &&
1252 ch == '-' && util_isdigit(nextch))
1254 lex->tok.ttype = lex_finish_digit(lex, nextch);
1255 if (lex->tok.ttype == TOKEN_INTCONST)
1256 lex->tok.constval.i = -lex->tok.constval.i;
1258 lex->tok.constval.f = -lex->tok.constval.f;
1260 return lex->tok.ttype;
1262 lex_ungetch(lex, nextch);
1266 return (lex->tok.ttype = TOKEN_OPERATOR);
1269 if (ch == '*' || ch == '/') /* *=, /= */
1271 lex_tokench(lex, ch);
1273 nextch = lex_getch(lex);
1274 if (nextch == '=' || nextch == '*') {
1275 lex_tokench(lex, nextch);
1277 lex_ungetch(lex, nextch);
1280 return (lex->tok.ttype = TOKEN_OPERATOR);
1284 lex_tokench(lex, ch);
1286 return (lex->tok.ttype = TOKEN_OPERATOR);
1289 if (isident_start(ch))
1293 lex_tokench(lex, ch);
1294 if (!lex_finish_ident(lex)) {
1296 return (lex->tok.ttype = TOKEN_ERROR);
1299 lex->tok.ttype = TOKEN_IDENT;
1302 if (!strcmp(v, "void")) {
1303 lex->tok.ttype = TOKEN_TYPENAME;
1304 lex->tok.constval.t = TYPE_VOID;
1305 } else if (!strcmp(v, "int")) {
1306 lex->tok.ttype = TOKEN_TYPENAME;
1307 lex->tok.constval.t = TYPE_INTEGER;
1308 } else if (!strcmp(v, "float")) {
1309 lex->tok.ttype = TOKEN_TYPENAME;
1310 lex->tok.constval.t = TYPE_FLOAT;
1311 } else if (!strcmp(v, "string")) {
1312 lex->tok.ttype = TOKEN_TYPENAME;
1313 lex->tok.constval.t = TYPE_STRING;
1314 } else if (!strcmp(v, "entity")) {
1315 lex->tok.ttype = TOKEN_TYPENAME;
1316 lex->tok.constval.t = TYPE_ENTITY;
1317 } else if (!strcmp(v, "vector")) {
1318 lex->tok.ttype = TOKEN_TYPENAME;
1319 lex->tok.constval.t = TYPE_VECTOR;
1320 } else if (!strcmp(v, "_length")) {
1321 lex->tok.ttype = TOKEN_OPERATOR;
1324 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1325 if (!strcmp(v, keywords_qc[kw]))
1326 return (lex->tok.ttype = TOKEN_KEYWORD);
1328 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1329 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1330 if (!strcmp(v, keywords_fg[kw]))
1331 return (lex->tok.ttype = TOKEN_KEYWORD);
1336 return lex->tok.ttype;
1341 lex->flags.nodigraphs = true;
1342 if (lex->flags.preprocessing)
1343 lex_tokench(lex, ch);
1344 lex->tok.ttype = lex_finish_string(lex, '"');
1345 if (lex->flags.preprocessing)
1346 lex_tokench(lex, ch);
1347 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1349 /* Allow c style "string" "continuation" */
1350 ch = lex_skipwhite(lex, false);
1352 lex_ungetch(lex, ch);
1356 lex->tok.ttype = lex_finish_string(lex, '"');
1358 lex->flags.nodigraphs = false;
1360 return lex->tok.ttype;
1365 /* we parse character constants like string,
1366 * but return TOKEN_CHARCONST, or a vector type if it fits...
1367 * Likewise actual unescaping has to be done by the parser.
1368 * The difference is we don't allow 'char' 'continuation'.
1370 if (lex->flags.preprocessing)
1371 lex_tokench(lex, ch);
1372 lex->tok.ttype = lex_finish_string(lex, '\'');
1373 if (lex->flags.preprocessing)
1374 lex_tokench(lex, ch);
1377 lex->tok.ttype = TOKEN_CHARCONST;
1379 /* It's a vector if we can successfully scan 3 floats */
1380 if (util_sscanf(lex->tok.value, " %f %f %f ",
1381 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1384 lex->tok.ttype = TOKEN_VECTORCONST;
1388 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1390 /* check for a valid utf8 character */
1391 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1392 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1393 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1394 : "multibyte character: `%s`" ),
1396 return (lex->tok.ttype = TOKEN_ERROR);
1399 lex->tok.constval.i = u8char;
1402 lex->tok.constval.i = lex->tok.value[0];
1405 return lex->tok.ttype;
1408 if (util_isdigit(ch))
1410 lex->tok.ttype = lex_finish_digit(lex, ch);
1412 return lex->tok.ttype;
1415 if (lex->flags.preprocessing) {
1416 lex_tokench(lex, ch);
1418 return (lex->tok.ttype = ch);
1421 lexerror(lex, "unknown token: `%c`", ch);
1422 return (lex->tok.ttype = TOKEN_ERROR);