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",
52 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
60 void lexerror(lex_file *lex, const char *fmt, ...)
66 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
68 con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
72 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
75 int lvl = LVL_WARNING;
77 if (!OPTS_WARN(warntype))
84 con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
94 token *tok = (token*)mem_a(sizeof(token));
97 memset(tok, 0, sizeof(*tok));
101 void token_delete(token *self)
103 if (self->next && self->next->prev == self)
104 self->next->prev = self->prev;
105 if (self->prev && self->prev->next == self)
106 self->prev->next = self->next;
107 MEM_VECTOR_CLEAR(self, value);
111 token* token_copy(const token *cp)
113 token* self = token_new();
117 self->value_alloc = cp->value_count + 1;
118 self->value_count = cp->value_count;
119 self->value = (char*)mem_a(self->value_alloc);
124 memcpy(self->value, cp->value, cp->value_count);
125 self->value[self->value_alloc-1] = 0;
129 self->ttype = cp->ttype;
130 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
134 void token_delete_all(token *t)
145 token* token_copy_all(const token *cp)
150 out = cur = token_copy(cp);
156 cur->next = token_copy(cp);
158 token_delete_all(out);
161 cur->next->prev = cur;
168 static void lex_token_new(lex_file *lex)
172 token_delete(lex->tok);
173 lex->tok = token_new();
176 vec_shrinkto(lex->tok.value, 0);
177 lex->tok.constval.t = 0;
178 lex->tok.ctx.line = lex->sline;
179 lex->tok.ctx.file = lex->name;
184 lex_file* lex_open(const char *file)
187 FILE *in = util_fopen(file, "rb");
190 lexerror(NULL, "open failed: '%s'\n", file);
194 lex = (lex_file*)mem_a(sizeof(*lex));
197 lexerror(NULL, "out of memory\n");
201 memset(lex, 0, sizeof(*lex));
204 lex->name = util_strdup(file);
205 lex->line = 1; /* we start counting at 1 */
210 vec_push(lex_filenames, lex->name);
214 lex_file* lex_open_string(const char *str, size_t len, const char *name)
218 lex = (lex_file*)mem_a(sizeof(*lex));
220 lexerror(NULL, "out of memory\n");
224 memset(lex, 0, sizeof(*lex));
227 lex->open_string = str;
228 lex->open_string_length = len;
229 lex->open_string_pos = 0;
231 lex->name = util_strdup(name ? name : "<string-source>");
232 lex->line = 1; /* we start counting at 1 */
237 vec_push(lex_filenames, lex->name);
242 void lex_cleanup(void)
245 for (i = 0; i < vec_size(lex_filenames); ++i)
246 mem_d(lex_filenames[i]);
247 vec_free(lex_filenames);
250 void lex_close(lex_file *lex)
253 for (i = 0; i < vec_size(lex->frames); ++i)
254 mem_d(lex->frames[i].name);
255 vec_free(lex->frames);
258 vec_free(lex->modelname);
264 token_delete(lex->tok);
266 vec_free(lex->tok.value);
268 /* mem_d(lex->name); collected in lex_filenames */
272 static int lex_fgetc(lex_file *lex)
275 return fgetc(lex->file);
276 if (lex->open_string) {
277 if (lex->open_string_pos >= lex->open_string_length)
279 return lex->open_string[lex->open_string_pos++];
284 /* Get or put-back data
285 * The following to functions do NOT understand what kind of data they
287 * The are merely wrapping get/put in order to count line numbers.
289 static void lex_ungetch(lex_file *lex, int ch);
290 static int lex_try_trigraph(lex_file *lex, int old)
295 lex_ungetch(lex, c2);
301 case '=': return '#';
302 case '/': return '\\';
303 case '\'': return '^';
304 case '(': return '[';
305 case ')': return ']';
306 case '!': return '|';
307 case '<': return '{';
308 case '>': return '}';
309 case '-': return '~';
311 lex_ungetch(lex, c3);
312 lex_ungetch(lex, c2);
317 static int lex_try_digraph(lex_file *lex, int ch)
321 if (ch == '<' && c2 == ':')
323 else if (ch == ':' && c2 == '>')
325 else if (ch == '<' && c2 == '%')
327 else if (ch == '%' && c2 == '>')
329 else if (ch == '%' && c2 == ':')
331 lex_ungetch(lex, c2);
335 static int lex_getch(lex_file *lex)
341 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
343 return lex->peek[lex->peekpos];
347 if (!lex->push_line && ch == '\n')
350 return lex_try_trigraph(lex, ch);
351 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
352 return lex_try_digraph(lex, ch);
356 static void lex_ungetch(lex_file *lex, int ch)
358 lex->peek[lex->peekpos++] = ch;
359 if (!lex->push_line && ch == '\n')
363 /* classify characters
364 * some additions to the is*() functions of ctype.h
367 /* Idents are alphanumberic, but they start with alpha or _ */
368 static bool isident_start(int ch)
370 return isalpha(ch) || ch == '_';
373 static bool isident(int ch)
375 return isident_start(ch) || isdigit(ch);
378 /* isxdigit_only is used when we already know it's not a digit
379 * and want to see if it's a hex digit anyway.
381 static bool isxdigit_only(int ch)
383 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
386 /* Append a character to the token buffer */
387 static void lex_tokench(lex_file *lex, int ch)
389 vec_push(lex->tok.value, ch);
392 /* Append a trailing null-byte */
393 static void lex_endtoken(lex_file *lex)
395 vec_push(lex->tok.value, 0);
396 vec_shrinkby(lex->tok.value, 1);
399 static bool lex_try_pragma(lex_file *lex)
403 char *command = NULL;
407 if (lex->flags.preprocessing)
414 lex_ungetch(lex, ch);
418 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
419 vec_push(pragma, ch);
422 if (ch != ' ' || strcmp(pragma, "pragma")) {
423 lex_ungetch(lex, ch);
427 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
428 vec_push(command, ch);
429 vec_push(command, 0);
432 lex_ungetch(lex, ch);
436 for (ch = lex_getch(lex); vec_size(param) < 32 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
441 lex_ungetch(lex, ch);
445 if (!strcmp(command, "push")) {
446 if (!strcmp(param, "line")) {
448 if (lex->push_line == 1)
454 else if (!strcmp(command, "pop")) {
455 if (!strcmp(param, "line")) {
458 if (lex->push_line == 0)
464 else if (!strcmp(command, "file")) {
465 lex->name = util_strdup(param);
466 vec_push(lex_filenames, lex->name);
468 else if (!strcmp(command, "line")) {
469 line = strtol(param, NULL, 0)-1;
475 while (ch != '\n' && ch != EOF)
482 while (vec_size(command)) {
483 lex_ungetch(lex, vec_last(command));
490 while (vec_size(command)) {
491 lex_ungetch(lex, vec_last(command));
498 while (vec_size(pragma)) {
499 lex_ungetch(lex, vec_last(pragma));
504 lex_ungetch(lex, '#');
510 /* Skip whitespace and comments and return the first
511 * non-white character.
512 * As this makes use of the above getch() ungetch() functions,
513 * we don't need to care at all about line numbering anymore.
515 * In theory, this function should only be used at the beginning
516 * of lexing, or when we *know* the next character is part of the token.
517 * Otherwise, if the parser throws an error, the linenumber may not be
518 * the line of the error, but the line of the next token AFTER the error.
520 * This is currently only problematic when using c-like string-continuation,
521 * since comments and whitespaces are allowed between 2 such strings.
525 "A continuation of the previous string"
526 // This line is skipped
529 * In this case, if the parse decides it didn't actually want a string,
530 * and uses lex->line to print an error, it will show the ', foo);' line's
533 * On the other hand, the parser is supposed to remember the line of the next
534 * token's beginning. In this case we would want skipwhite() to be called
535 * AFTER reading a token, so that the parser, before reading the NEXT token,
536 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
539 * here is to store the line of the first character after skipping
540 * the initial whitespace in lex->sline, this happens in lex_do.
542 static int lex_skipwhite(lex_file *lex)
545 bool haswhite = false;
550 while (ch != EOF && isspace(ch)) {
552 if (lex_try_pragma(lex))
555 if (lex->flags.preprocessing) {
558 /* see if there was whitespace first */
559 if (haswhite) { /* (vec_size(lex->tok.value)) { */
560 lex_ungetch(lex, ch);
564 /* otherwise return EOL */
568 lex_tokench(lex, ch);
577 /* one line comment */
580 if (lex->flags.preprocessing) {
583 lex_tokench(lex, '/');
584 lex_tokench(lex, '/');
586 lex_tokench(lex, ' ');
587 lex_tokench(lex, ' ');
590 while (ch != EOF && ch != '\n') {
591 if (lex->flags.preprocessing)
592 lex_tokench(lex, ' '); /* ch); */
595 if (lex->flags.preprocessing) {
596 lex_ungetch(lex, '\n');
604 /* multiline comment */
605 if (lex->flags.preprocessing) {
608 lex_tokench(lex, '/');
609 lex_tokench(lex, '*');
611 lex_tokench(lex, ' ');
612 lex_tokench(lex, ' ');
621 if (lex->flags.preprocessing) {
623 lex_tokench(lex, '*');
624 lex_tokench(lex, '/');
626 lex_tokench(lex, ' ');
627 lex_tokench(lex, ' ');
631 lex_ungetch(lex, ch);
633 if (lex->flags.preprocessing) {
635 lex_tokench(lex, '\n');
637 lex_tokench(lex, ' '); /* ch); */
640 ch = ' '; /* cause TRUE in the isspace check */
643 /* Otherwise roll back to the slash and break out of the loop */
644 lex_ungetch(lex, ch);
648 } while (ch != EOF && isspace(ch));
652 lex_ungetch(lex, ch);
659 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
664 while (ch != EOF && isident(ch))
666 lex_tokench(lex, ch);
670 /* last ch was not an ident ch: */
671 lex_ungetch(lex, ch);
676 /* read one ident for the frame list */
677 static int lex_parse_frame(lex_file *lex)
684 while (ch != EOF && ch != '\n' && isspace(ch))
690 if (!isident_start(ch)) {
691 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
695 lex_tokench(lex, ch);
696 if (!lex_finish_ident(lex))
702 /* read a list of $frames */
703 static bool lex_finish_frames(lex_file *lex)
710 rc = lex_parse_frame(lex);
711 if (rc > 0) /* end of line */
713 if (rc < 0) /* error */
716 for (i = 0; i < vec_size(lex->frames); ++i) {
717 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
718 lex->frames[i].value = lex->framevalue++;
719 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
724 if (i < vec_size(lex->frames))
727 m.value = lex->framevalue++;
728 m.name = util_strdup(lex->tok.value);
729 vec_shrinkto(lex->tok.value, 0);
730 vec_push(lex->frames, m);
734 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
742 return TOKEN_STRINGCONST;
744 if (lex->flags.preprocessing && ch == '\\') {
745 lex_tokench(lex, ch);
748 lexerror(lex, "unexpected end of file");
749 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
750 return (lex->tok.ttype = TOKEN_ERROR);
752 lex_tokench(lex, ch);
754 else if (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);
766 case 'a': ch = '\a'; break;
767 case 'b': ch = '\b'; break;
768 case 'r': ch = '\r'; break;
769 case 'n': ch = '\n'; break;
770 case 't': ch = '\t'; break;
771 case 'f': ch = '\f'; break;
772 case 'v': ch = '\v'; break;
774 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
775 /* so we just add the character plus backslash no matter what it actually is */
776 lex_tokench(lex, '\\');
778 /* add the character finally */
779 lex_tokench(lex, ch);
782 lex_tokench(lex, ch);
784 lexerror(lex, "unexpected end of file within string constant");
785 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
786 return (lex->tok.ttype = TOKEN_ERROR);
789 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
795 /* parse a number... */
796 lex->tok.ttype = TOKEN_INTCONST;
798 lex_tokench(lex, ch);
801 if (ch != '.' && !isdigit(ch))
803 if (lastch != '0' || ch != 'x')
805 /* end of the number or EOF */
806 lex_ungetch(lex, ch);
809 lex->tok.constval.i = lastch - '0';
810 return lex->tok.ttype;
816 /* EOF would have been caught above */
820 lex_tokench(lex, ch);
822 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
824 lex_tokench(lex, ch);
828 /* NOT else, '.' can come from above as well */
829 if (ch == '.' && !ishex)
831 /* Allow floating comma in non-hex mode */
832 lex->tok.ttype = TOKEN_FLOATCONST;
833 lex_tokench(lex, ch);
835 /* continue digits-only */
839 lex_tokench(lex, ch);
843 /* put back the last character */
844 /* but do not put back the trailing 'f' or a float */
845 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
848 /* generally we don't want words to follow numbers: */
850 lexerror(lex, "unexpected trailing characters after number");
851 return (lex->tok.ttype = TOKEN_ERROR);
853 lex_ungetch(lex, ch);
856 if (lex->tok.ttype == TOKEN_FLOATCONST)
857 lex->tok.constval.f = strtod(lex->tok.value, NULL);
859 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
860 return lex->tok.ttype;
863 int lex_do(lex_file *lex)
865 int ch, nextch, thirdch;
874 ch = lex_skipwhite(lex);
875 if (!lex->flags.mergelines || ch != '\\')
879 lex_ungetch(lex, ch);
883 /* we reached a linemerge */
884 lex_tokench(lex, '\n');
888 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
889 return (lex->tok.ttype = ch);
892 lex->sline = lex->line;
893 lex->tok.ctx.line = lex->sline;
894 lex->tok.ctx.file = lex->name;
897 return (lex->tok.ttype = TOKEN_FATAL);
901 return (lex->tok.ttype = TOKEN_EOF);
904 /* modelgen / spiritgen commands */
910 if (!isident_start(ch)) {
911 lexerror(lex, "hanging '$' modelgen/spritegen command line");
914 lex_tokench(lex, ch);
915 if (!lex_finish_ident(lex))
916 return (lex->tok.ttype = TOKEN_ERROR);
918 /* skip the known commands */
921 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
923 /* frame/framesave command works like an enum
924 * similar to fteqcc we handle this in the lexer.
925 * The reason for this is that it is sensitive to newlines,
926 * which the parser is unaware of
928 if (!lex_finish_frames(lex))
929 return (lex->tok.ttype = TOKEN_ERROR);
933 if (!strcmp(v, "framevalue"))
936 while (ch != EOF && isspace(ch) && ch != '\n')
940 lexerror(lex, "$framevalue requires an integer parameter");
945 lex->tok.ttype = lex_finish_digit(lex, ch);
947 if (lex->tok.ttype != TOKEN_INTCONST) {
948 lexerror(lex, "$framevalue requires an integer parameter");
951 lex->framevalue = lex->tok.constval.i;
955 if (!strcmp(v, "framerestore"))
961 rc = lex_parse_frame(lex);
964 lexerror(lex, "$framerestore requires a framename parameter");
968 return (lex->tok.ttype = TOKEN_FATAL);
971 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
972 if (!strcmp(v, lex->frames[frame].name)) {
973 lex->framevalue = lex->frames[frame].value;
977 lexerror(lex, "unknown framename `%s`", v);
981 if (!strcmp(v, "modelname"))
987 rc = lex_parse_frame(lex);
990 lexerror(lex, "$modelname requires a parameter");
994 return (lex->tok.ttype = TOKEN_FATAL);
997 if (lex->modelname) {
999 m.value = lex->framevalue;
1000 m.name = lex->modelname;
1001 lex->modelname = NULL;
1002 vec_push(lex->frames, m);
1004 lex->modelname = lex->tok.value;
1005 lex->tok.value = NULL;
1009 if (!strcmp(v, "flush"))
1012 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1013 mem_d(lex->frames[fi].name);
1014 vec_free(lex->frames);
1015 /* skip line (fteqcc does it too) */
1016 ch = lex_getch(lex);
1017 while (ch != EOF && ch != '\n')
1018 ch = lex_getch(lex);
1022 if (!strcmp(v, "cd") ||
1023 !strcmp(v, "origin") ||
1024 !strcmp(v, "base") ||
1025 !strcmp(v, "flags") ||
1026 !strcmp(v, "scale") ||
1030 ch = lex_getch(lex);
1031 while (ch != EOF && ch != '\n')
1032 ch = lex_getch(lex);
1036 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1037 if (!strcmp(v, lex->frames[frame].name)) {
1038 lex->tok.constval.i = lex->frames[frame].value;
1039 return (lex->tok.ttype = TOKEN_INTCONST);
1043 lexerror(lex, "invalid frame macro");
1047 /* single-character tokens */
1054 lex_tokench(lex, ch);
1056 if (lex->flags.noops)
1057 return (lex->tok.ttype = ch);
1059 return (lex->tok.ttype = TOKEN_OPERATOR);
1067 lex_tokench(lex, ch);
1069 return (lex->tok.ttype = ch);
1074 if (lex->flags.noops)
1076 /* Detect characters early which are normally
1077 * operators OR PART of an operator.
1096 lex_tokench(lex, ch);
1098 return (lex->tok.ttype = ch);
1105 lex_tokench(lex, ch);
1106 /* peak ahead once */
1107 nextch = lex_getch(lex);
1108 if (nextch != '.') {
1109 lex_ungetch(lex, nextch);
1111 return (lex->tok.ttype = ch);
1113 /* peak ahead again */
1114 nextch = lex_getch(lex);
1115 if (nextch != '.') {
1116 lex_ungetch(lex, nextch);
1117 lex_ungetch(lex, '.');
1119 return (lex->tok.ttype = ch);
1121 /* fill the token to be "..." */
1122 lex_tokench(lex, ch);
1123 lex_tokench(lex, ch);
1125 return (lex->tok.ttype = TOKEN_DOTS);
1129 if (ch == ',' || ch == '.') {
1130 lex_tokench(lex, ch);
1132 return (lex->tok.ttype = TOKEN_OPERATOR);
1135 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1136 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1137 ch == '=' || ch == '!' || /* ==, != */
1138 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1140 lex_tokench(lex, ch);
1142 nextch = lex_getch(lex);
1143 if (nextch == ch || nextch == '=') {
1144 lex_tokench(lex, nextch);
1145 } else if (ch == '-' && nextch == '>') {
1146 lex_tokench(lex, nextch);
1147 } else if (ch == '&' && nextch == '~') {
1148 thirdch = lex_getch(lex);
1149 if (thirdch != '=') {
1150 lex_ungetch(lex, thirdch);
1151 lex_ungetch(lex, nextch);
1154 lex_tokench(lex, nextch);
1155 lex_tokench(lex, thirdch);
1158 lex_ungetch(lex, nextch);
1161 return (lex->tok.ttype = TOKEN_OPERATOR);
1165 if (ch == '^' || ch == '~' || ch == '!')
1167 lex_tokench(lex, ch);
1169 return (lex->tok.ttype = TOKEN_OPERATOR);
1173 if (ch == '*' || ch == '/') /* *=, /= */
1175 lex_tokench(lex, ch);
1177 nextch = lex_getch(lex);
1178 if (nextch == '=') {
1179 lex_tokench(lex, nextch);
1181 lex_ungetch(lex, nextch);
1184 return (lex->tok.ttype = TOKEN_OPERATOR);
1187 if (isident_start(ch))
1191 lex_tokench(lex, ch);
1192 if (!lex_finish_ident(lex)) {
1194 return (lex->tok.ttype = TOKEN_ERROR);
1197 lex->tok.ttype = TOKEN_IDENT;
1200 if (!strcmp(v, "void")) {
1201 lex->tok.ttype = TOKEN_TYPENAME;
1202 lex->tok.constval.t = TYPE_VOID;
1203 } else if (!strcmp(v, "int")) {
1204 lex->tok.ttype = TOKEN_TYPENAME;
1205 lex->tok.constval.t = TYPE_INTEGER;
1206 } else if (!strcmp(v, "float")) {
1207 lex->tok.ttype = TOKEN_TYPENAME;
1208 lex->tok.constval.t = TYPE_FLOAT;
1209 } else if (!strcmp(v, "string")) {
1210 lex->tok.ttype = TOKEN_TYPENAME;
1211 lex->tok.constval.t = TYPE_STRING;
1212 } else if (!strcmp(v, "entity")) {
1213 lex->tok.ttype = TOKEN_TYPENAME;
1214 lex->tok.constval.t = TYPE_ENTITY;
1215 } else if (!strcmp(v, "vector")) {
1216 lex->tok.ttype = TOKEN_TYPENAME;
1217 lex->tok.constval.t = TYPE_VECTOR;
1220 for (kw = 0; kw < num_keywords_qc; ++kw) {
1221 if (!strcmp(v, keywords_qc[kw]))
1222 return (lex->tok.ttype = TOKEN_KEYWORD);
1224 if (opts_standard != COMPILER_QCC) {
1225 for (kw = 0; kw < num_keywords_fg; ++kw) {
1226 if (!strcmp(v, keywords_fg[kw]))
1227 return (lex->tok.ttype = TOKEN_KEYWORD);
1232 return lex->tok.ttype;
1237 lex->flags.nodigraphs = true;
1238 if (lex->flags.preprocessing)
1239 lex_tokench(lex, ch);
1240 lex->tok.ttype = lex_finish_string(lex, '"');
1241 if (lex->flags.preprocessing)
1242 lex_tokench(lex, ch);
1243 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1245 /* Allow c style "string" "continuation" */
1246 ch = lex_skipwhite(lex);
1248 lex_ungetch(lex, ch);
1252 lex->tok.ttype = lex_finish_string(lex, '"');
1254 lex->flags.nodigraphs = false;
1256 return lex->tok.ttype;
1261 /* we parse character constants like string,
1262 * but return TOKEN_CHARCONST, or a vector type if it fits...
1263 * Likewise actual unescaping has to be done by the parser.
1264 * The difference is we don't allow 'char' 'continuation'.
1266 if (lex->flags.preprocessing)
1267 lex_tokench(lex, ch);
1268 lex->tok.ttype = lex_finish_string(lex, '\'');
1269 if (lex->flags.preprocessing)
1270 lex_tokench(lex, ch);
1273 /* It's a vector if we can successfully scan 3 floats */
1275 if (sscanf_s(lex->tok.value, " %f %f %f ",
1276 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1278 if (sscanf(lex->tok.value, " %f %f %f ",
1279 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1283 lex->tok.ttype = TOKEN_VECTORCONST;
1286 return lex->tok.ttype;
1291 lex->tok.ttype = lex_finish_digit(lex, ch);
1293 return lex->tok.ttype;
1296 lexerror(lex, "unknown token");
1297 return (lex->tok.ttype = TOKEN_ERROR);