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
36 static const char *keywords_qc[] = {
43 static size_t num_keywords_qc = sizeof(keywords_qc) / sizeof(keywords_qc[0]);
46 static const char *keywords_fg[] = {
47 "switch", "case", "default",
55 "__builtin_debug_printtype"
57 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
65 void lexerror(lex_file *lex, const char *fmt, ...)
71 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
73 con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
77 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
84 ctx.line = lex->sline;
87 r = vcompile_warning(ctx, warntype, fmt, ap);
96 token *tok = (token*)mem_a(sizeof(token));
99 memset(tok, 0, sizeof(*tok));
103 void token_delete(token *self)
105 if (self->next && self->next->prev == self)
106 self->next->prev = self->prev;
107 if (self->prev && self->prev->next == self)
108 self->prev->next = self->next;
109 MEM_VECTOR_CLEAR(self, value);
113 token* token_copy(const token *cp)
115 token* self = token_new();
119 self->value_alloc = cp->value_count + 1;
120 self->value_count = cp->value_count;
121 self->value = (char*)mem_a(self->value_alloc);
126 memcpy(self->value, cp->value, cp->value_count);
127 self->value[self->value_alloc-1] = 0;
131 self->ttype = cp->ttype;
132 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
136 void token_delete_all(token *t)
147 token* token_copy_all(const token *cp)
152 out = cur = token_copy(cp);
158 cur->next = token_copy(cp);
160 token_delete_all(out);
163 cur->next->prev = cur;
170 static void lex_token_new(lex_file *lex)
174 token_delete(lex->tok);
175 lex->tok = token_new();
178 vec_shrinkto(lex->tok.value, 0);
179 lex->tok.constval.t = 0;
180 lex->tok.ctx.line = lex->sline;
181 lex->tok.ctx.file = lex->name;
186 lex_file* lex_open(const char *file)
189 FILE *in = util_fopen(file, "rb");
192 lexerror(NULL, "open failed: '%s'\n", file);
196 lex = (lex_file*)mem_a(sizeof(*lex));
199 lexerror(NULL, "out of memory\n");
203 memset(lex, 0, sizeof(*lex));
206 lex->name = util_strdup(file);
207 lex->line = 1; /* we start counting at 1 */
212 vec_push(lex_filenames, lex->name);
216 lex_file* lex_open_string(const char *str, size_t len, const char *name)
220 lex = (lex_file*)mem_a(sizeof(*lex));
222 lexerror(NULL, "out of memory\n");
226 memset(lex, 0, sizeof(*lex));
229 lex->open_string = str;
230 lex->open_string_length = len;
231 lex->open_string_pos = 0;
233 lex->name = util_strdup(name ? name : "<string-source>");
234 lex->line = 1; /* we start counting at 1 */
239 vec_push(lex_filenames, lex->name);
244 void lex_cleanup(void)
247 for (i = 0; i < vec_size(lex_filenames); ++i)
248 mem_d(lex_filenames[i]);
249 vec_free(lex_filenames);
252 void lex_close(lex_file *lex)
255 for (i = 0; i < vec_size(lex->frames); ++i)
256 mem_d(lex->frames[i].name);
257 vec_free(lex->frames);
260 vec_free(lex->modelname);
266 token_delete(lex->tok);
268 vec_free(lex->tok.value);
270 /* mem_d(lex->name); collected in lex_filenames */
274 static int lex_fgetc(lex_file *lex)
277 return fgetc(lex->file);
278 if (lex->open_string) {
279 if (lex->open_string_pos >= lex->open_string_length)
281 return lex->open_string[lex->open_string_pos++];
286 /* Get or put-back data
287 * The following to functions do NOT understand what kind of data they
289 * The are merely wrapping get/put in order to count line numbers.
291 static void lex_ungetch(lex_file *lex, int ch);
292 static int lex_try_trigraph(lex_file *lex, int old)
297 lex_ungetch(lex, c2);
303 case '=': return '#';
304 case '/': return '\\';
305 case '\'': return '^';
306 case '(': return '[';
307 case ')': return ']';
308 case '!': return '|';
309 case '<': return '{';
310 case '>': return '}';
311 case '-': return '~';
313 lex_ungetch(lex, c3);
314 lex_ungetch(lex, c2);
319 static int lex_try_digraph(lex_file *lex, int ch)
323 /* we just used fgetc() so count lines
324 * need to offset a \n the ungetch would recognize
326 if (!lex->push_line && c2 == '\n')
328 if (ch == '<' && c2 == ':')
330 else if (ch == ':' && c2 == '>')
332 else if (ch == '<' && c2 == '%')
334 else if (ch == '%' && c2 == '>')
336 else if (ch == '%' && c2 == ':')
338 lex_ungetch(lex, c2);
342 static int lex_getch(lex_file *lex)
348 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
350 return lex->peek[lex->peekpos];
354 if (!lex->push_line && ch == '\n')
357 return lex_try_trigraph(lex, ch);
358 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
359 return lex_try_digraph(lex, ch);
363 static void lex_ungetch(lex_file *lex, int ch)
365 lex->peek[lex->peekpos++] = ch;
366 if (!lex->push_line && ch == '\n')
370 /* classify characters
371 * some additions to the is*() functions of ctype.h
374 /* Idents are alphanumberic, but they start with alpha or _ */
375 static bool isident_start(int ch)
377 return isalpha(ch) || ch == '_';
380 static bool isident(int ch)
382 return isident_start(ch) || isdigit(ch);
385 /* isxdigit_only is used when we already know it's not a digit
386 * and want to see if it's a hex digit anyway.
388 static bool isxdigit_only(int ch)
390 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
393 /* Append a character to the token buffer */
394 static void lex_tokench(lex_file *lex, int ch)
396 vec_push(lex->tok.value, ch);
399 /* Append a trailing null-byte */
400 static void lex_endtoken(lex_file *lex)
402 vec_push(lex->tok.value, 0);
403 vec_shrinkby(lex->tok.value, 1);
406 static bool lex_try_pragma(lex_file *lex)
410 char *command = NULL;
414 if (lex->flags.preprocessing)
421 lex_ungetch(lex, ch);
425 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
426 vec_push(pragma, ch);
429 if (ch != ' ' || strcmp(pragma, "pragma")) {
430 lex_ungetch(lex, ch);
434 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
435 vec_push(command, ch);
436 vec_push(command, 0);
439 lex_ungetch(lex, ch);
443 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
448 lex_ungetch(lex, ch);
452 if (!strcmp(command, "push")) {
453 if (!strcmp(param, "line")) {
455 if (lex->push_line == 1)
461 else if (!strcmp(command, "pop")) {
462 if (!strcmp(param, "line")) {
465 if (lex->push_line == 0)
471 else if (!strcmp(command, "file")) {
472 lex->name = util_strdup(param);
473 vec_push(lex_filenames, lex->name);
475 else if (!strcmp(command, "line")) {
476 line = strtol(param, NULL, 0)-1;
482 while (ch != '\n' && ch != EOF)
489 while (vec_size(command)) {
490 lex_ungetch(lex, vec_last(command));
494 lex_ungetch(lex, ' ');
498 while (vec_size(command)) {
499 lex_ungetch(lex, vec_last(command));
503 lex_ungetch(lex, ' ');
507 while (vec_size(pragma)) {
508 lex_ungetch(lex, vec_last(pragma));
513 lex_ungetch(lex, '#');
519 /* Skip whitespace and comments and return the first
520 * non-white character.
521 * As this makes use of the above getch() ungetch() functions,
522 * we don't need to care at all about line numbering anymore.
524 * In theory, this function should only be used at the beginning
525 * of lexing, or when we *know* the next character is part of the token.
526 * Otherwise, if the parser throws an error, the linenumber may not be
527 * the line of the error, but the line of the next token AFTER the error.
529 * This is currently only problematic when using c-like string-continuation,
530 * since comments and whitespaces are allowed between 2 such strings.
534 "A continuation of the previous string"
535 // This line is skipped
538 * In this case, if the parse decides it didn't actually want a string,
539 * and uses lex->line to print an error, it will show the ', foo);' line's
542 * On the other hand, the parser is supposed to remember the line of the next
543 * token's beginning. In this case we would want skipwhite() to be called
544 * AFTER reading a token, so that the parser, before reading the NEXT token,
545 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
548 * here is to store the line of the first character after skipping
549 * the initial whitespace in lex->sline, this happens in lex_do.
551 static int lex_skipwhite(lex_file *lex, bool hadwhite)
554 bool haswhite = hadwhite;
559 while (ch != EOF && isspace(ch)) {
561 if (lex_try_pragma(lex))
564 if (lex->flags.preprocessing) {
567 /* see if there was whitespace first */
568 if (haswhite) { /* (vec_size(lex->tok.value)) { */
569 lex_ungetch(lex, ch);
573 /* otherwise return EOL */
577 lex_tokench(lex, ch);
586 /* one line comment */
589 if (lex->flags.preprocessing) {
592 lex_tokench(lex, '/');
593 lex_tokench(lex, '/');
595 lex_tokench(lex, ' ');
596 lex_tokench(lex, ' ');
599 while (ch != EOF && ch != '\n') {
600 if (lex->flags.preprocessing)
601 lex_tokench(lex, ' '); /* ch); */
604 if (lex->flags.preprocessing) {
605 lex_ungetch(lex, '\n');
613 /* multiline comment */
614 if (lex->flags.preprocessing) {
617 lex_tokench(lex, '/');
618 lex_tokench(lex, '*');
620 lex_tokench(lex, ' ');
621 lex_tokench(lex, ' ');
630 if (lex->flags.preprocessing) {
632 lex_tokench(lex, '*');
633 lex_tokench(lex, '/');
635 lex_tokench(lex, ' ');
636 lex_tokench(lex, ' ');
640 lex_ungetch(lex, ch);
642 if (lex->flags.preprocessing) {
644 lex_tokench(lex, '\n');
646 lex_tokench(lex, ' '); /* ch); */
649 ch = ' '; /* cause TRUE in the isspace check */
652 /* Otherwise roll back to the slash and break out of the loop */
653 lex_ungetch(lex, ch);
657 } while (ch != EOF && isspace(ch));
661 lex_ungetch(lex, ch);
668 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
673 while (ch != EOF && isident(ch))
675 lex_tokench(lex, ch);
679 /* last ch was not an ident ch: */
680 lex_ungetch(lex, ch);
685 /* read one ident for the frame list */
686 static int lex_parse_frame(lex_file *lex)
693 while (ch != EOF && ch != '\n' && isspace(ch))
699 if (!isident_start(ch)) {
700 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
704 lex_tokench(lex, ch);
705 if (!lex_finish_ident(lex))
711 /* read a list of $frames */
712 static bool lex_finish_frames(lex_file *lex)
719 rc = lex_parse_frame(lex);
720 if (rc > 0) /* end of line */
722 if (rc < 0) /* error */
725 for (i = 0; i < vec_size(lex->frames); ++i) {
726 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
727 lex->frames[i].value = lex->framevalue++;
728 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
733 if (i < vec_size(lex->frames))
736 m.value = lex->framevalue++;
737 m.name = util_strdup(lex->tok.value);
738 vec_shrinkto(lex->tok.value, 0);
739 vec_push(lex->frames, m);
743 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
752 return TOKEN_STRINGCONST;
754 if (lex->flags.preprocessing && ch == '\\') {
755 lex_tokench(lex, ch);
758 lexerror(lex, "unexpected end of file");
759 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
760 return (lex->tok.ttype = TOKEN_ERROR);
762 lex_tokench(lex, ch);
764 else if (ch == '\\') {
767 lexerror(lex, "unexpected end of file");
768 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
769 return (lex->tok.ttype = TOKEN_ERROR);
776 case 'a': ch = '\a'; break;
777 case 'b': ch = '\b'; break;
778 case 'r': ch = '\r'; break;
779 case 'n': ch = '\n'; break;
780 case 't': ch = '\t'; break;
781 case 'f': ch = '\f'; break;
782 case 'v': ch = '\v'; break;
785 /* same procedure as in fteqcc */
787 nextch = lex_getch(lex);
788 if (nextch >= '0' && nextch <= '9')
790 else if (nextch >= 'a' && nextch <= 'f')
791 ch += nextch - 'a' + 10;
792 else if (nextch >= 'A' && nextch <= 'F')
793 ch += nextch - 'A' + 10;
795 lexerror(lex, "bad character code");
796 lex_ungetch(lex, nextch);
797 return (lex->tok.ttype = TOKEN_ERROR);
801 nextch = lex_getch(lex);
802 if (nextch >= '0' && nextch <= '9')
804 else if (nextch >= 'a' && nextch <= 'f')
805 ch += nextch - 'a' + 10;
806 else if (nextch >= 'A' && nextch <= 'F')
807 ch += nextch - 'A' + 10;
809 lexerror(lex, "bad character code");
810 lex_ungetch(lex, nextch);
811 return (lex->tok.ttype = TOKEN_ERROR);
816 case '0': case '1': case '2': case '3':
817 case '4': case '5': case '6': case '7':
821 case '<': ch = 29; break;
822 case '-': ch = 30; break;
823 case '>': ch = 31; break;
824 case '[': ch = 16; break;
825 case ']': ch = 17; break;
828 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
829 ch = ch * 10 + nextch - '0';
830 if (nextch < '0' || nextch > '9' || ch > 255) {
831 lexerror(lex, "bad character code");
832 return (lex->tok.ttype = TOKEN_ERROR);
836 case '\n': ch = '\n'; break;
839 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
840 /* so we just add the character plus backslash no matter what it actually is */
841 lex_tokench(lex, '\\');
843 /* add the character finally */
844 lex_tokench(lex, ch);
847 lex_tokench(lex, ch);
849 lexerror(lex, "unexpected end of file within string constant");
850 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
851 return (lex->tok.ttype = TOKEN_ERROR);
854 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
860 /* parse a number... */
862 lex->tok.ttype = TOKEN_FLOATCONST;
864 lex->tok.ttype = TOKEN_INTCONST;
866 lex_tokench(lex, ch);
869 if (ch != '.' && !isdigit(ch))
871 if (lastch != '0' || ch != 'x')
873 /* end of the number or EOF */
874 lex_ungetch(lex, ch);
877 lex->tok.constval.i = lastch - '0';
878 return lex->tok.ttype;
884 /* EOF would have been caught above */
888 lex_tokench(lex, ch);
890 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
892 lex_tokench(lex, ch);
896 /* NOT else, '.' can come from above as well */
897 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
899 /* Allow floating comma in non-hex mode */
900 lex->tok.ttype = TOKEN_FLOATCONST;
901 lex_tokench(lex, ch);
903 /* continue digits-only */
907 lex_tokench(lex, ch);
911 /* put back the last character */
912 /* but do not put back the trailing 'f' or a float */
913 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
916 /* generally we don't want words to follow numbers: */
918 lexerror(lex, "unexpected trailing characters after number");
919 return (lex->tok.ttype = TOKEN_ERROR);
921 lex_ungetch(lex, ch);
924 if (lex->tok.ttype == TOKEN_FLOATCONST)
925 lex->tok.constval.f = strtod(lex->tok.value, NULL);
927 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
928 return lex->tok.ttype;
931 int lex_do(lex_file *lex)
933 int ch, nextch, thirdch;
934 bool hadwhite = false;
943 ch = lex_skipwhite(lex, hadwhite);
945 if (!lex->flags.mergelines || ch != '\\')
949 lex_ungetch(lex, ch);
953 /* we reached a linemerge */
954 lex_tokench(lex, '\n');
958 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
959 return (lex->tok.ttype = ch);
962 lex->sline = lex->line;
963 lex->tok.ctx.line = lex->sline;
964 lex->tok.ctx.file = lex->name;
967 return (lex->tok.ttype = TOKEN_FATAL);
971 return (lex->tok.ttype = TOKEN_EOF);
974 /* modelgen / spiritgen commands */
975 if (ch == '$' && !lex->flags.preprocessing) {
980 if (!isident_start(ch)) {
981 lexerror(lex, "hanging '$' modelgen/spritegen command line");
984 lex_tokench(lex, ch);
985 if (!lex_finish_ident(lex))
986 return (lex->tok.ttype = TOKEN_ERROR);
988 /* skip the known commands */
991 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
993 /* frame/framesave command works like an enum
994 * similar to fteqcc we handle this in the lexer.
995 * The reason for this is that it is sensitive to newlines,
996 * which the parser is unaware of
998 if (!lex_finish_frames(lex))
999 return (lex->tok.ttype = TOKEN_ERROR);
1003 if (!strcmp(v, "framevalue"))
1005 ch = lex_getch(lex);
1006 while (ch != EOF && isspace(ch) && ch != '\n')
1007 ch = lex_getch(lex);
1010 lexerror(lex, "$framevalue requires an integer parameter");
1015 lex->tok.ttype = lex_finish_digit(lex, ch);
1017 if (lex->tok.ttype != TOKEN_INTCONST) {
1018 lexerror(lex, "$framevalue requires an integer parameter");
1021 lex->framevalue = lex->tok.constval.i;
1025 if (!strcmp(v, "framerestore"))
1031 rc = lex_parse_frame(lex);
1034 lexerror(lex, "$framerestore requires a framename parameter");
1038 return (lex->tok.ttype = TOKEN_FATAL);
1041 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1042 if (!strcmp(v, lex->frames[frame].name)) {
1043 lex->framevalue = lex->frames[frame].value;
1047 lexerror(lex, "unknown framename `%s`", v);
1051 if (!strcmp(v, "modelname"))
1057 rc = lex_parse_frame(lex);
1060 lexerror(lex, "$modelname requires a parameter");
1064 return (lex->tok.ttype = TOKEN_FATAL);
1066 if (lex->modelname) {
1068 m.value = lex->framevalue;
1069 m.name = lex->modelname;
1070 lex->modelname = NULL;
1071 vec_push(lex->frames, m);
1073 lex->modelname = lex->tok.value;
1074 lex->tok.value = NULL;
1078 if (!strcmp(v, "flush"))
1081 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1082 mem_d(lex->frames[fi].name);
1083 vec_free(lex->frames);
1084 /* skip line (fteqcc does it too) */
1085 ch = lex_getch(lex);
1086 while (ch != EOF && ch != '\n')
1087 ch = lex_getch(lex);
1091 if (!strcmp(v, "cd") ||
1092 !strcmp(v, "origin") ||
1093 !strcmp(v, "base") ||
1094 !strcmp(v, "flags") ||
1095 !strcmp(v, "scale") ||
1099 ch = lex_getch(lex);
1100 while (ch != EOF && ch != '\n')
1101 ch = lex_getch(lex);
1105 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1106 if (!strcmp(v, lex->frames[frame].name)) {
1107 lex->tok.constval.i = lex->frames[frame].value;
1108 return (lex->tok.ttype = TOKEN_INTCONST);
1112 lexerror(lex, "invalid frame macro");
1116 /* single-character tokens */
1123 lex_tokench(lex, ch);
1125 if (lex->flags.noops)
1126 return (lex->tok.ttype = ch);
1128 return (lex->tok.ttype = TOKEN_OPERATOR);
1136 lex_tokench(lex, ch);
1138 return (lex->tok.ttype = ch);
1144 nextch = lex_getch(lex);
1145 /* digits starting with a dot */
1146 if (isdigit(nextch)) {
1147 lex_ungetch(lex, nextch);
1148 lex->tok.ttype = lex_finish_digit(lex, ch);
1150 return lex->tok.ttype;
1152 lex_ungetch(lex, nextch);
1155 if (lex->flags.noops)
1157 /* Detect characters early which are normally
1158 * operators OR PART of an operator.
1177 lex_tokench(lex, ch);
1179 return (lex->tok.ttype = ch);
1186 lex_tokench(lex, ch);
1187 /* peak ahead once */
1188 nextch = lex_getch(lex);
1189 if (nextch != '.') {
1190 lex_ungetch(lex, nextch);
1192 return (lex->tok.ttype = ch);
1194 /* peak ahead again */
1195 nextch = lex_getch(lex);
1196 if (nextch != '.') {
1197 lex_ungetch(lex, nextch);
1198 lex_ungetch(lex, '.');
1200 return (lex->tok.ttype = ch);
1202 /* fill the token to be "..." */
1203 lex_tokench(lex, ch);
1204 lex_tokench(lex, ch);
1206 return (lex->tok.ttype = TOKEN_DOTS);
1210 if (ch == ',' || ch == '.') {
1211 lex_tokench(lex, ch);
1213 return (lex->tok.ttype = TOKEN_OPERATOR);
1216 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1217 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1218 ch == '=' || ch == '!' || /* ==, != */
1219 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1221 lex_tokench(lex, ch);
1223 nextch = lex_getch(lex);
1224 if (nextch == '=' || (nextch == ch && ch != '!')) {
1225 lex_tokench(lex, nextch);
1226 } else if (ch == '-' && nextch == '>') {
1227 lex_tokench(lex, nextch);
1228 } else if (ch == '&' && nextch == '~') {
1229 thirdch = lex_getch(lex);
1230 if (thirdch != '=') {
1231 lex_ungetch(lex, thirdch);
1232 lex_ungetch(lex, nextch);
1235 lex_tokench(lex, nextch);
1236 lex_tokench(lex, thirdch);
1239 lex_ungetch(lex, nextch);
1242 return (lex->tok.ttype = TOKEN_OPERATOR);
1246 if (ch == '^' || ch == '~' || ch == '!')
1248 lex_tokench(lex, ch);
1250 return (lex->tok.ttype = TOKEN_OPERATOR);
1254 if (ch == '*' || ch == '/') /* *=, /= */
1256 lex_tokench(lex, ch);
1258 nextch = lex_getch(lex);
1259 if (nextch == '=') {
1260 lex_tokench(lex, nextch);
1262 lex_ungetch(lex, nextch);
1265 return (lex->tok.ttype = TOKEN_OPERATOR);
1268 if (isident_start(ch))
1272 lex_tokench(lex, ch);
1273 if (!lex_finish_ident(lex)) {
1275 return (lex->tok.ttype = TOKEN_ERROR);
1278 lex->tok.ttype = TOKEN_IDENT;
1281 if (!strcmp(v, "void")) {
1282 lex->tok.ttype = TOKEN_TYPENAME;
1283 lex->tok.constval.t = TYPE_VOID;
1284 } else if (!strcmp(v, "int")) {
1285 lex->tok.ttype = TOKEN_TYPENAME;
1286 lex->tok.constval.t = TYPE_INTEGER;
1287 } else if (!strcmp(v, "float")) {
1288 lex->tok.ttype = TOKEN_TYPENAME;
1289 lex->tok.constval.t = TYPE_FLOAT;
1290 } else if (!strcmp(v, "string")) {
1291 lex->tok.ttype = TOKEN_TYPENAME;
1292 lex->tok.constval.t = TYPE_STRING;
1293 } else if (!strcmp(v, "entity")) {
1294 lex->tok.ttype = TOKEN_TYPENAME;
1295 lex->tok.constval.t = TYPE_ENTITY;
1296 } else if (!strcmp(v, "vector")) {
1297 lex->tok.ttype = TOKEN_TYPENAME;
1298 lex->tok.constval.t = TYPE_VECTOR;
1301 for (kw = 0; kw < num_keywords_qc; ++kw) {
1302 if (!strcmp(v, keywords_qc[kw]))
1303 return (lex->tok.ttype = TOKEN_KEYWORD);
1305 if (opts.standard != COMPILER_QCC) {
1306 for (kw = 0; kw < num_keywords_fg; ++kw) {
1307 if (!strcmp(v, keywords_fg[kw]))
1308 return (lex->tok.ttype = TOKEN_KEYWORD);
1313 return lex->tok.ttype;
1318 lex->flags.nodigraphs = true;
1319 if (lex->flags.preprocessing)
1320 lex_tokench(lex, ch);
1321 lex->tok.ttype = lex_finish_string(lex, '"');
1322 if (lex->flags.preprocessing)
1323 lex_tokench(lex, ch);
1324 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1326 /* Allow c style "string" "continuation" */
1327 ch = lex_skipwhite(lex, false);
1329 lex_ungetch(lex, ch);
1333 lex->tok.ttype = lex_finish_string(lex, '"');
1335 lex->flags.nodigraphs = false;
1337 return lex->tok.ttype;
1342 /* we parse character constants like string,
1343 * but return TOKEN_CHARCONST, or a vector type if it fits...
1344 * Likewise actual unescaping has to be done by the parser.
1345 * The difference is we don't allow 'char' 'continuation'.
1347 if (lex->flags.preprocessing)
1348 lex_tokench(lex, ch);
1349 lex->tok.ttype = lex_finish_string(lex, '\'');
1350 if (lex->flags.preprocessing)
1351 lex_tokench(lex, ch);
1354 lex->tok.ttype = TOKEN_CHARCONST;
1355 /* It's a vector if we can successfully scan 3 floats */
1357 if (sscanf_s(lex->tok.value, " %f %f %f ",
1358 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1360 if (sscanf(lex->tok.value, " %f %f %f ",
1361 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1365 lex->tok.ttype = TOKEN_VECTORCONST;
1369 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1370 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER, "multibyte character: `%s`", lex->tok.value))
1371 return (lex->tok.ttype = TOKEN_ERROR);
1373 lex->tok.constval.i = lex->tok.value[0];
1376 return lex->tok.ttype;
1381 lex->tok.ttype = lex_finish_digit(lex, ch);
1383 return lex->tok.ttype;
1386 if (lex->flags.preprocessing) {
1387 lex_tokench(lex, ch);
1389 return (lex->tok.ttype = ch);
1392 lexerror(lex, "unknown token");
1393 return (lex->tok.ttype = TOKEN_ERROR);