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",
53 "__builtin_debug_printtype"
55 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
63 void lexerror(lex_file *lex, const char *fmt, ...)
69 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
71 con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
75 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
78 int lvl = LVL_WARNING;
80 if (!OPTS_WARN(warntype))
87 con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
97 token *tok = (token*)mem_a(sizeof(token));
100 memset(tok, 0, sizeof(*tok));
104 void token_delete(token *self)
106 if (self->next && self->next->prev == self)
107 self->next->prev = self->prev;
108 if (self->prev && self->prev->next == self)
109 self->prev->next = self->next;
110 MEM_VECTOR_CLEAR(self, value);
114 token* token_copy(const token *cp)
116 token* self = token_new();
120 self->value_alloc = cp->value_count + 1;
121 self->value_count = cp->value_count;
122 self->value = (char*)mem_a(self->value_alloc);
127 memcpy(self->value, cp->value, cp->value_count);
128 self->value[self->value_alloc-1] = 0;
132 self->ttype = cp->ttype;
133 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
137 void token_delete_all(token *t)
148 token* token_copy_all(const token *cp)
153 out = cur = token_copy(cp);
159 cur->next = token_copy(cp);
161 token_delete_all(out);
164 cur->next->prev = cur;
171 static void lex_token_new(lex_file *lex)
175 token_delete(lex->tok);
176 lex->tok = token_new();
179 vec_shrinkto(lex->tok.value, 0);
180 lex->tok.constval.t = 0;
181 lex->tok.ctx.line = lex->sline;
182 lex->tok.ctx.file = lex->name;
187 lex_file* lex_open(const char *file)
190 FILE *in = util_fopen(file, "rb");
193 lexerror(NULL, "open failed: '%s'\n", file);
197 lex = (lex_file*)mem_a(sizeof(*lex));
200 lexerror(NULL, "out of memory\n");
204 memset(lex, 0, sizeof(*lex));
207 lex->name = util_strdup(file);
208 lex->line = 1; /* we start counting at 1 */
213 vec_push(lex_filenames, lex->name);
217 lex_file* lex_open_string(const char *str, size_t len, const char *name)
221 lex = (lex_file*)mem_a(sizeof(*lex));
223 lexerror(NULL, "out of memory\n");
227 memset(lex, 0, sizeof(*lex));
230 lex->open_string = str;
231 lex->open_string_length = len;
232 lex->open_string_pos = 0;
234 lex->name = util_strdup(name ? name : "<string-source>");
235 lex->line = 1; /* we start counting at 1 */
240 vec_push(lex_filenames, lex->name);
245 void lex_cleanup(void)
248 for (i = 0; i < vec_size(lex_filenames); ++i)
249 mem_d(lex_filenames[i]);
250 vec_free(lex_filenames);
253 void lex_close(lex_file *lex)
256 for (i = 0; i < vec_size(lex->frames); ++i)
257 mem_d(lex->frames[i].name);
258 vec_free(lex->frames);
261 vec_free(lex->modelname);
267 token_delete(lex->tok);
269 vec_free(lex->tok.value);
271 /* mem_d(lex->name); collected in lex_filenames */
275 static int lex_fgetc(lex_file *lex)
278 return fgetc(lex->file);
279 if (lex->open_string) {
280 if (lex->open_string_pos >= lex->open_string_length)
282 return lex->open_string[lex->open_string_pos++];
287 /* Get or put-back data
288 * The following to functions do NOT understand what kind of data they
290 * The are merely wrapping get/put in order to count line numbers.
292 static void lex_ungetch(lex_file *lex, int ch);
293 static int lex_try_trigraph(lex_file *lex, int old)
298 lex_ungetch(lex, c2);
304 case '=': return '#';
305 case '/': return '\\';
306 case '\'': return '^';
307 case '(': return '[';
308 case ')': return ']';
309 case '!': return '|';
310 case '<': return '{';
311 case '>': return '}';
312 case '-': return '~';
314 lex_ungetch(lex, c3);
315 lex_ungetch(lex, c2);
320 static int lex_try_digraph(lex_file *lex, int ch)
324 /* we just used fgetc() so count lines
325 * need to offset a \n the ungetch would recognize
327 if (!lex->push_line && c2 == '\n')
329 if (ch == '<' && c2 == ':')
331 else if (ch == ':' && c2 == '>')
333 else if (ch == '<' && c2 == '%')
335 else if (ch == '%' && c2 == '>')
337 else if (ch == '%' && c2 == ':')
339 lex_ungetch(lex, c2);
343 static int lex_getch(lex_file *lex)
349 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
351 return lex->peek[lex->peekpos];
355 if (!lex->push_line && ch == '\n')
358 return lex_try_trigraph(lex, ch);
359 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
360 return lex_try_digraph(lex, ch);
364 static void lex_ungetch(lex_file *lex, int ch)
366 lex->peek[lex->peekpos++] = ch;
367 if (!lex->push_line && ch == '\n')
371 /* classify characters
372 * some additions to the is*() functions of ctype.h
375 /* Idents are alphanumberic, but they start with alpha or _ */
376 static bool isident_start(int ch)
378 return isalpha(ch) || ch == '_';
381 static bool isident(int ch)
383 return isident_start(ch) || isdigit(ch);
386 /* isxdigit_only is used when we already know it's not a digit
387 * and want to see if it's a hex digit anyway.
389 static bool isxdigit_only(int ch)
391 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
394 /* Append a character to the token buffer */
395 static void lex_tokench(lex_file *lex, int ch)
397 vec_push(lex->tok.value, ch);
400 /* Append a trailing null-byte */
401 static void lex_endtoken(lex_file *lex)
403 vec_push(lex->tok.value, 0);
404 vec_shrinkby(lex->tok.value, 1);
407 static bool lex_try_pragma(lex_file *lex)
411 char *command = NULL;
415 if (lex->flags.preprocessing)
422 lex_ungetch(lex, ch);
426 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
427 vec_push(pragma, ch);
430 if (ch != ' ' || strcmp(pragma, "pragma")) {
431 lex_ungetch(lex, ch);
435 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
436 vec_push(command, ch);
437 vec_push(command, 0);
440 lex_ungetch(lex, ch);
444 for (ch = lex_getch(lex); vec_size(param) < 32 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
449 lex_ungetch(lex, ch);
453 if (!strcmp(command, "push")) {
454 if (!strcmp(param, "line")) {
456 if (lex->push_line == 1)
462 else if (!strcmp(command, "pop")) {
463 if (!strcmp(param, "line")) {
466 if (lex->push_line == 0)
472 else if (!strcmp(command, "file")) {
473 lex->name = util_strdup(param);
474 vec_push(lex_filenames, lex->name);
476 else if (!strcmp(command, "line")) {
477 line = strtol(param, NULL, 0)-1;
483 while (ch != '\n' && ch != EOF)
490 while (vec_size(command)) {
491 lex_ungetch(lex, vec_last(command));
498 while (vec_size(command)) {
499 lex_ungetch(lex, vec_last(command));
506 while (vec_size(pragma)) {
507 lex_ungetch(lex, vec_last(pragma));
512 lex_ungetch(lex, '#');
518 /* Skip whitespace and comments and return the first
519 * non-white character.
520 * As this makes use of the above getch() ungetch() functions,
521 * we don't need to care at all about line numbering anymore.
523 * In theory, this function should only be used at the beginning
524 * of lexing, or when we *know* the next character is part of the token.
525 * Otherwise, if the parser throws an error, the linenumber may not be
526 * the line of the error, but the line of the next token AFTER the error.
528 * This is currently only problematic when using c-like string-continuation,
529 * since comments and whitespaces are allowed between 2 such strings.
533 "A continuation of the previous string"
534 // This line is skipped
537 * In this case, if the parse decides it didn't actually want a string,
538 * and uses lex->line to print an error, it will show the ', foo);' line's
541 * On the other hand, the parser is supposed to remember the line of the next
542 * token's beginning. In this case we would want skipwhite() to be called
543 * AFTER reading a token, so that the parser, before reading the NEXT token,
544 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
547 * here is to store the line of the first character after skipping
548 * the initial whitespace in lex->sline, this happens in lex_do.
550 static int lex_skipwhite(lex_file *lex)
553 bool haswhite = false;
558 while (ch != EOF && isspace(ch)) {
560 if (lex_try_pragma(lex))
563 if (lex->flags.preprocessing) {
566 /* see if there was whitespace first */
567 if (haswhite) { /* (vec_size(lex->tok.value)) { */
568 lex_ungetch(lex, ch);
572 /* otherwise return EOL */
576 lex_tokench(lex, ch);
585 /* one line comment */
588 if (lex->flags.preprocessing) {
591 lex_tokench(lex, '/');
592 lex_tokench(lex, '/');
594 lex_tokench(lex, ' ');
595 lex_tokench(lex, ' ');
598 while (ch != EOF && ch != '\n') {
599 if (lex->flags.preprocessing)
600 lex_tokench(lex, ' '); /* ch); */
603 if (lex->flags.preprocessing) {
604 lex_ungetch(lex, '\n');
612 /* multiline comment */
613 if (lex->flags.preprocessing) {
616 lex_tokench(lex, '/');
617 lex_tokench(lex, '*');
619 lex_tokench(lex, ' ');
620 lex_tokench(lex, ' ');
629 if (lex->flags.preprocessing) {
631 lex_tokench(lex, '*');
632 lex_tokench(lex, '/');
634 lex_tokench(lex, ' ');
635 lex_tokench(lex, ' ');
639 lex_ungetch(lex, ch);
641 if (lex->flags.preprocessing) {
643 lex_tokench(lex, '\n');
645 lex_tokench(lex, ' '); /* ch); */
648 ch = ' '; /* cause TRUE in the isspace check */
651 /* Otherwise roll back to the slash and break out of the loop */
652 lex_ungetch(lex, ch);
656 } while (ch != EOF && isspace(ch));
660 lex_ungetch(lex, ch);
667 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
672 while (ch != EOF && isident(ch))
674 lex_tokench(lex, ch);
678 /* last ch was not an ident ch: */
679 lex_ungetch(lex, ch);
684 /* read one ident for the frame list */
685 static int lex_parse_frame(lex_file *lex)
692 while (ch != EOF && ch != '\n' && isspace(ch))
698 if (!isident_start(ch)) {
699 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
703 lex_tokench(lex, ch);
704 if (!lex_finish_ident(lex))
710 /* read a list of $frames */
711 static bool lex_finish_frames(lex_file *lex)
718 rc = lex_parse_frame(lex);
719 if (rc > 0) /* end of line */
721 if (rc < 0) /* error */
724 for (i = 0; i < vec_size(lex->frames); ++i) {
725 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
726 lex->frames[i].value = lex->framevalue++;
727 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
732 if (i < vec_size(lex->frames))
735 m.value = lex->framevalue++;
736 m.name = util_strdup(lex->tok.value);
737 vec_shrinkto(lex->tok.value, 0);
738 vec_push(lex->frames, m);
742 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
751 return TOKEN_STRINGCONST;
753 if (lex->flags.preprocessing && ch == '\\') {
754 lex_tokench(lex, ch);
757 lexerror(lex, "unexpected end of file");
758 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
759 return (lex->tok.ttype = TOKEN_ERROR);
761 lex_tokench(lex, ch);
763 else if (ch == '\\') {
766 lexerror(lex, "unexpected end of file");
767 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
768 return (lex->tok.ttype = TOKEN_ERROR);
775 case 'a': ch = '\a'; break;
776 case 'b': ch = '\b'; break;
777 case 'r': ch = '\r'; break;
778 case 'n': ch = '\n'; break;
779 case 't': ch = '\t'; break;
780 case 'f': ch = '\f'; break;
781 case 'v': ch = '\v'; break;
784 /* same procedure as in fteqcc */
786 nextch = lex_getch(lex);
787 if (nextch >= '0' && nextch <= '9')
789 else if (nextch >= 'a' && nextch <= 'f')
790 ch += nextch - 'a' + 10;
791 else if (nextch >= 'A' && nextch <= 'F')
792 ch += nextch - 'A' + 10;
794 lexerror(lex, "bad character code");
795 lex_ungetch(lex, nextch);
796 return (lex->tok.ttype = TOKEN_ERROR);
800 nextch = lex_getch(lex);
801 if (nextch >= '0' && nextch <= '9')
803 else if (nextch >= 'a' && nextch <= 'f')
804 ch += nextch - 'a' + 10;
805 else if (nextch >= 'A' && nextch <= 'F')
806 ch += nextch - 'A' + 10;
808 lexerror(lex, "bad character code");
809 lex_ungetch(lex, nextch);
810 return (lex->tok.ttype = TOKEN_ERROR);
815 case '0': case '1': case '2': case '3':
816 case '4': case '5': case '6': case '7':
820 case '<': ch = 29; break;
821 case '-': ch = 30; break;
822 case '>': ch = 31; break;
823 case '[': ch = 16; break;
824 case ']': ch = 17; break;
827 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
828 ch = ch * 10 + nextch - '0';
829 if (nextch < '0' || nextch > '9' || ch > 255) {
830 lexerror(lex, "bad character code");
831 return (lex->tok.ttype = TOKEN_ERROR);
835 case '\n': ch = '\n'; break;
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);
846 lex_tokench(lex, ch);
848 lexerror(lex, "unexpected end of file within string constant");
849 lex_ungetch(lex, 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 != '.' && !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 (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 */
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;
941 ch = lex_skipwhite(lex);
942 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);
968 return (lex->tok.ttype = TOKEN_EOF);
971 /* modelgen / spiritgen commands */
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 != EOF && isspace(ch) && ch != '\n')
1004 ch = lex_getch(lex);
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);
1064 if (lex->modelname) {
1066 m.value = lex->framevalue;
1067 m.name = lex->modelname;
1068 lex->modelname = NULL;
1069 vec_push(lex->frames, m);
1071 lex->modelname = lex->tok.value;
1072 lex->tok.value = NULL;
1076 if (!strcmp(v, "flush"))
1079 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1080 mem_d(lex->frames[fi].name);
1081 vec_free(lex->frames);
1082 /* skip line (fteqcc does it too) */
1083 ch = lex_getch(lex);
1084 while (ch != EOF && ch != '\n')
1085 ch = lex_getch(lex);
1089 if (!strcmp(v, "cd") ||
1090 !strcmp(v, "origin") ||
1091 !strcmp(v, "base") ||
1092 !strcmp(v, "flags") ||
1093 !strcmp(v, "scale") ||
1097 ch = lex_getch(lex);
1098 while (ch != EOF && ch != '\n')
1099 ch = lex_getch(lex);
1103 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1104 if (!strcmp(v, lex->frames[frame].name)) {
1105 lex->tok.constval.i = lex->frames[frame].value;
1106 return (lex->tok.ttype = TOKEN_INTCONST);
1110 lexerror(lex, "invalid frame macro");
1114 /* single-character tokens */
1121 lex_tokench(lex, ch);
1123 if (lex->flags.noops)
1124 return (lex->tok.ttype = ch);
1126 return (lex->tok.ttype = TOKEN_OPERATOR);
1134 lex_tokench(lex, ch);
1136 return (lex->tok.ttype = ch);
1142 nextch = lex_getch(lex);
1143 /* digits starting with a dot */
1144 if (isdigit(nextch)) {
1145 lex_ungetch(lex, nextch);
1146 lex->tok.ttype = lex_finish_digit(lex, ch);
1148 return lex->tok.ttype;
1150 lex_ungetch(lex, nextch);
1153 if (lex->flags.noops)
1155 /* Detect characters early which are normally
1156 * operators OR PART of an operator.
1175 lex_tokench(lex, ch);
1177 return (lex->tok.ttype = ch);
1184 lex_tokench(lex, ch);
1185 /* peak ahead once */
1186 nextch = lex_getch(lex);
1187 if (nextch != '.') {
1188 lex_ungetch(lex, nextch);
1190 return (lex->tok.ttype = ch);
1192 /* peak ahead again */
1193 nextch = lex_getch(lex);
1194 if (nextch != '.') {
1195 lex_ungetch(lex, nextch);
1196 lex_ungetch(lex, '.');
1198 return (lex->tok.ttype = ch);
1200 /* fill the token to be "..." */
1201 lex_tokench(lex, ch);
1202 lex_tokench(lex, ch);
1204 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 == '<' || /* <<, >>, <=, >= */
1216 ch == '=' || ch == '!' || /* ==, != */
1217 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1219 lex_tokench(lex, ch);
1221 nextch = lex_getch(lex);
1222 if (nextch == '=' || (nextch == ch && ch != '!')) {
1223 lex_tokench(lex, nextch);
1224 } else if (ch == '-' && nextch == '>') {
1225 lex_tokench(lex, nextch);
1226 } else if (ch == '&' && nextch == '~') {
1227 thirdch = lex_getch(lex);
1228 if (thirdch != '=') {
1229 lex_ungetch(lex, thirdch);
1230 lex_ungetch(lex, nextch);
1233 lex_tokench(lex, nextch);
1234 lex_tokench(lex, thirdch);
1237 lex_ungetch(lex, nextch);
1240 return (lex->tok.ttype = TOKEN_OPERATOR);
1244 if (ch == '^' || ch == '~' || ch == '!')
1246 lex_tokench(lex, ch);
1248 return (lex->tok.ttype = TOKEN_OPERATOR);
1252 if (ch == '*' || ch == '/') /* *=, /= */
1254 lex_tokench(lex, ch);
1256 nextch = lex_getch(lex);
1257 if (nextch == '=') {
1258 lex_tokench(lex, nextch);
1260 lex_ungetch(lex, nextch);
1263 return (lex->tok.ttype = TOKEN_OPERATOR);
1266 if (isident_start(ch))
1270 lex_tokench(lex, ch);
1271 if (!lex_finish_ident(lex)) {
1273 return (lex->tok.ttype = TOKEN_ERROR);
1276 lex->tok.ttype = TOKEN_IDENT;
1279 if (!strcmp(v, "void")) {
1280 lex->tok.ttype = TOKEN_TYPENAME;
1281 lex->tok.constval.t = TYPE_VOID;
1282 } else if (!strcmp(v, "int")) {
1283 lex->tok.ttype = TOKEN_TYPENAME;
1284 lex->tok.constval.t = TYPE_INTEGER;
1285 } else if (!strcmp(v, "float")) {
1286 lex->tok.ttype = TOKEN_TYPENAME;
1287 lex->tok.constval.t = TYPE_FLOAT;
1288 } else if (!strcmp(v, "string")) {
1289 lex->tok.ttype = TOKEN_TYPENAME;
1290 lex->tok.constval.t = TYPE_STRING;
1291 } else if (!strcmp(v, "entity")) {
1292 lex->tok.ttype = TOKEN_TYPENAME;
1293 lex->tok.constval.t = TYPE_ENTITY;
1294 } else if (!strcmp(v, "vector")) {
1295 lex->tok.ttype = TOKEN_TYPENAME;
1296 lex->tok.constval.t = TYPE_VECTOR;
1299 for (kw = 0; kw < num_keywords_qc; ++kw) {
1300 if (!strcmp(v, keywords_qc[kw]))
1301 return (lex->tok.ttype = TOKEN_KEYWORD);
1303 if (opts_standard != COMPILER_QCC) {
1304 for (kw = 0; kw < num_keywords_fg; ++kw) {
1305 if (!strcmp(v, keywords_fg[kw]))
1306 return (lex->tok.ttype = TOKEN_KEYWORD);
1311 return lex->tok.ttype;
1316 lex->flags.nodigraphs = true;
1317 if (lex->flags.preprocessing)
1318 lex_tokench(lex, ch);
1319 lex->tok.ttype = lex_finish_string(lex, '"');
1320 if (lex->flags.preprocessing)
1321 lex_tokench(lex, ch);
1322 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1324 /* Allow c style "string" "continuation" */
1325 ch = lex_skipwhite(lex);
1327 lex_ungetch(lex, ch);
1331 lex->tok.ttype = lex_finish_string(lex, '"');
1333 lex->flags.nodigraphs = false;
1335 return lex->tok.ttype;
1340 /* we parse character constants like string,
1341 * but return TOKEN_CHARCONST, or a vector type if it fits...
1342 * Likewise actual unescaping has to be done by the parser.
1343 * The difference is we don't allow 'char' 'continuation'.
1345 if (lex->flags.preprocessing)
1346 lex_tokench(lex, ch);
1347 lex->tok.ttype = lex_finish_string(lex, '\'');
1348 if (lex->flags.preprocessing)
1349 lex_tokench(lex, ch);
1352 lex->tok.ttype = TOKEN_CHARCONST;
1353 /* It's a vector if we can successfully scan 3 floats */
1355 if (sscanf_s(lex->tok.value, " %f %f %f ",
1356 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1358 if (sscanf(lex->tok.value, " %f %f %f ",
1359 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1363 lex->tok.ttype = TOKEN_VECTORCONST;
1367 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1368 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER, "multibyte character: `%s`", lex->tok.value))
1369 return (lex->tok.ttype = TOKEN_ERROR);
1371 lex->tok.constval.i = lex->tok.value[0];
1374 return lex->tok.ttype;
1379 lex->tok.ttype = lex_finish_digit(lex, ch);
1381 return lex->tok.ttype;
1384 if (lex->flags.preprocessing) {
1385 lex_tokench(lex, ch);
1387 return (lex->tok.ttype = ch);
1390 lexerror(lex, "unknown token");
1391 return (lex->tok.ttype = TOKEN_ERROR);