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 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
61 void lexerror(lex_file *lex, const char *fmt, ...)
67 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
69 con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
73 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
76 int lvl = LVL_WARNING;
78 if (!OPTS_WARN(warntype))
85 con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
95 token *tok = (token*)mem_a(sizeof(token));
98 memset(tok, 0, sizeof(*tok));
102 void token_delete(token *self)
104 if (self->next && self->next->prev == self)
105 self->next->prev = self->prev;
106 if (self->prev && self->prev->next == self)
107 self->prev->next = self->next;
108 MEM_VECTOR_CLEAR(self, value);
112 token* token_copy(const token *cp)
114 token* self = token_new();
118 self->value_alloc = cp->value_count + 1;
119 self->value_count = cp->value_count;
120 self->value = (char*)mem_a(self->value_alloc);
125 memcpy(self->value, cp->value, cp->value_count);
126 self->value[self->value_alloc-1] = 0;
130 self->ttype = cp->ttype;
131 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
135 void token_delete_all(token *t)
146 token* token_copy_all(const token *cp)
151 out = cur = token_copy(cp);
157 cur->next = token_copy(cp);
159 token_delete_all(out);
162 cur->next->prev = cur;
169 static void lex_token_new(lex_file *lex)
173 token_delete(lex->tok);
174 lex->tok = token_new();
177 vec_shrinkto(lex->tok.value, 0);
178 lex->tok.constval.t = 0;
179 lex->tok.ctx.line = lex->sline;
180 lex->tok.ctx.file = lex->name;
185 lex_file* lex_open(const char *file)
188 FILE *in = util_fopen(file, "rb");
191 lexerror(NULL, "open failed: '%s'\n", file);
195 lex = (lex_file*)mem_a(sizeof(*lex));
198 lexerror(NULL, "out of memory\n");
202 memset(lex, 0, sizeof(*lex));
205 lex->name = util_strdup(file);
206 lex->line = 1; /* we start counting at 1 */
211 vec_push(lex_filenames, lex->name);
215 lex_file* lex_open_string(const char *str, size_t len, const char *name)
219 lex = (lex_file*)mem_a(sizeof(*lex));
221 lexerror(NULL, "out of memory\n");
225 memset(lex, 0, sizeof(*lex));
228 lex->open_string = str;
229 lex->open_string_length = len;
230 lex->open_string_pos = 0;
232 lex->name = util_strdup(name ? name : "<string-source>");
233 lex->line = 1; /* we start counting at 1 */
238 vec_push(lex_filenames, lex->name);
243 void lex_cleanup(void)
246 for (i = 0; i < vec_size(lex_filenames); ++i)
247 mem_d(lex_filenames[i]);
248 vec_free(lex_filenames);
251 void lex_close(lex_file *lex)
254 for (i = 0; i < vec_size(lex->frames); ++i)
255 mem_d(lex->frames[i].name);
256 vec_free(lex->frames);
259 vec_free(lex->modelname);
265 token_delete(lex->tok);
267 vec_free(lex->tok.value);
269 /* mem_d(lex->name); collected in lex_filenames */
273 static int lex_fgetc(lex_file *lex)
276 return fgetc(lex->file);
277 if (lex->open_string) {
278 if (lex->open_string_pos >= lex->open_string_length)
280 return lex->open_string[lex->open_string_pos++];
285 /* Get or put-back data
286 * The following to functions do NOT understand what kind of data they
288 * The are merely wrapping get/put in order to count line numbers.
290 static void lex_ungetch(lex_file *lex, int ch);
291 static int lex_try_trigraph(lex_file *lex, int old)
296 lex_ungetch(lex, c2);
302 case '=': return '#';
303 case '/': return '\\';
304 case '\'': return '^';
305 case '(': return '[';
306 case ')': return ']';
307 case '!': return '|';
308 case '<': return '{';
309 case '>': return '}';
310 case '-': return '~';
312 lex_ungetch(lex, c3);
313 lex_ungetch(lex, c2);
318 static int lex_try_digraph(lex_file *lex, int ch)
322 /* we just used fgetc() so count lines
323 * need to offset a \n the ungetch would recognize
325 if (!lex->push_line && c2 == '\n')
327 if (ch == '<' && c2 == ':')
329 else if (ch == ':' && c2 == '>')
331 else if (ch == '<' && c2 == '%')
333 else if (ch == '%' && c2 == '>')
335 else if (ch == '%' && c2 == ':')
337 lex_ungetch(lex, c2);
341 static int lex_getch(lex_file *lex)
347 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
349 return lex->peek[lex->peekpos];
353 if (!lex->push_line && ch == '\n')
356 return lex_try_trigraph(lex, ch);
357 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
358 return lex_try_digraph(lex, ch);
362 static void lex_ungetch(lex_file *lex, int ch)
364 lex->peek[lex->peekpos++] = ch;
365 if (!lex->push_line && ch == '\n')
369 /* classify characters
370 * some additions to the is*() functions of ctype.h
373 /* Idents are alphanumberic, but they start with alpha or _ */
374 static bool isident_start(int ch)
376 return isalpha(ch) || ch == '_';
379 static bool isident(int ch)
381 return isident_start(ch) || isdigit(ch);
384 /* isxdigit_only is used when we already know it's not a digit
385 * and want to see if it's a hex digit anyway.
387 static bool isxdigit_only(int ch)
389 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
392 /* Append a character to the token buffer */
393 static void lex_tokench(lex_file *lex, int ch)
395 vec_push(lex->tok.value, ch);
398 /* Append a trailing null-byte */
399 static void lex_endtoken(lex_file *lex)
401 vec_push(lex->tok.value, 0);
402 vec_shrinkby(lex->tok.value, 1);
405 static bool lex_try_pragma(lex_file *lex)
409 char *command = NULL;
413 if (lex->flags.preprocessing)
420 lex_ungetch(lex, ch);
424 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
425 vec_push(pragma, ch);
428 if (ch != ' ' || strcmp(pragma, "pragma")) {
429 lex_ungetch(lex, ch);
433 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
434 vec_push(command, ch);
435 vec_push(command, 0);
438 lex_ungetch(lex, ch);
442 for (ch = lex_getch(lex); vec_size(param) < 32 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
447 lex_ungetch(lex, ch);
451 if (!strcmp(command, "push")) {
452 if (!strcmp(param, "line")) {
454 if (lex->push_line == 1)
460 else if (!strcmp(command, "pop")) {
461 if (!strcmp(param, "line")) {
464 if (lex->push_line == 0)
470 else if (!strcmp(command, "file")) {
471 lex->name = util_strdup(param);
472 vec_push(lex_filenames, lex->name);
474 else if (!strcmp(command, "line")) {
475 line = strtol(param, NULL, 0)-1;
481 while (ch != '\n' && ch != EOF)
488 while (vec_size(command)) {
489 lex_ungetch(lex, vec_last(command));
496 while (vec_size(command)) {
497 lex_ungetch(lex, vec_last(command));
504 while (vec_size(pragma)) {
505 lex_ungetch(lex, vec_last(pragma));
510 lex_ungetch(lex, '#');
516 /* Skip whitespace and comments and return the first
517 * non-white character.
518 * As this makes use of the above getch() ungetch() functions,
519 * we don't need to care at all about line numbering anymore.
521 * In theory, this function should only be used at the beginning
522 * of lexing, or when we *know* the next character is part of the token.
523 * Otherwise, if the parser throws an error, the linenumber may not be
524 * the line of the error, but the line of the next token AFTER the error.
526 * This is currently only problematic when using c-like string-continuation,
527 * since comments and whitespaces are allowed between 2 such strings.
531 "A continuation of the previous string"
532 // This line is skipped
535 * In this case, if the parse decides it didn't actually want a string,
536 * and uses lex->line to print an error, it will show the ', foo);' line's
539 * On the other hand, the parser is supposed to remember the line of the next
540 * token's beginning. In this case we would want skipwhite() to be called
541 * AFTER reading a token, so that the parser, before reading the NEXT token,
542 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
545 * here is to store the line of the first character after skipping
546 * the initial whitespace in lex->sline, this happens in lex_do.
548 static int lex_skipwhite(lex_file *lex)
551 bool haswhite = false;
556 while (ch != EOF && isspace(ch)) {
558 if (lex_try_pragma(lex))
561 if (lex->flags.preprocessing) {
564 /* see if there was whitespace first */
565 if (haswhite) { /* (vec_size(lex->tok.value)) { */
566 lex_ungetch(lex, ch);
570 /* otherwise return EOL */
574 lex_tokench(lex, ch);
583 /* one line comment */
586 if (lex->flags.preprocessing) {
589 lex_tokench(lex, '/');
590 lex_tokench(lex, '/');
592 lex_tokench(lex, ' ');
593 lex_tokench(lex, ' ');
596 while (ch != EOF && ch != '\n') {
597 if (lex->flags.preprocessing)
598 lex_tokench(lex, ' '); /* ch); */
601 if (lex->flags.preprocessing) {
602 lex_ungetch(lex, '\n');
610 /* multiline comment */
611 if (lex->flags.preprocessing) {
614 lex_tokench(lex, '/');
615 lex_tokench(lex, '*');
617 lex_tokench(lex, ' ');
618 lex_tokench(lex, ' ');
627 if (lex->flags.preprocessing) {
629 lex_tokench(lex, '*');
630 lex_tokench(lex, '/');
632 lex_tokench(lex, ' ');
633 lex_tokench(lex, ' ');
637 lex_ungetch(lex, ch);
639 if (lex->flags.preprocessing) {
641 lex_tokench(lex, '\n');
643 lex_tokench(lex, ' '); /* ch); */
646 ch = ' '; /* cause TRUE in the isspace check */
649 /* Otherwise roll back to the slash and break out of the loop */
650 lex_ungetch(lex, ch);
654 } while (ch != EOF && isspace(ch));
658 lex_ungetch(lex, ch);
665 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
670 while (ch != EOF && isident(ch))
672 lex_tokench(lex, ch);
676 /* last ch was not an ident ch: */
677 lex_ungetch(lex, ch);
682 /* read one ident for the frame list */
683 static int lex_parse_frame(lex_file *lex)
690 while (ch != EOF && ch != '\n' && isspace(ch))
696 if (!isident_start(ch)) {
697 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
701 lex_tokench(lex, ch);
702 if (!lex_finish_ident(lex))
708 /* read a list of $frames */
709 static bool lex_finish_frames(lex_file *lex)
716 rc = lex_parse_frame(lex);
717 if (rc > 0) /* end of line */
719 if (rc < 0) /* error */
722 for (i = 0; i < vec_size(lex->frames); ++i) {
723 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
724 lex->frames[i].value = lex->framevalue++;
725 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
730 if (i < vec_size(lex->frames))
733 m.value = lex->framevalue++;
734 m.name = util_strdup(lex->tok.value);
735 vec_shrinkto(lex->tok.value, 0);
736 vec_push(lex->frames, m);
740 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
748 return TOKEN_STRINGCONST;
750 if (lex->flags.preprocessing && ch == '\\') {
751 lex_tokench(lex, ch);
754 lexerror(lex, "unexpected end of file");
755 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
756 return (lex->tok.ttype = TOKEN_ERROR);
758 lex_tokench(lex, ch);
760 else if (ch == '\\') {
763 lexerror(lex, "unexpected end of file");
764 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
765 return (lex->tok.ttype = TOKEN_ERROR);
772 case 'a': ch = '\a'; break;
773 case 'b': ch = '\b'; break;
774 case 'r': ch = '\r'; break;
775 case 'n': ch = '\n'; break;
776 case 't': ch = '\t'; break;
777 case 'f': ch = '\f'; break;
778 case 'v': ch = '\v'; break;
779 case '\n': ch = '\n'; break;
781 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
782 /* so we just add the character plus backslash no matter what it actually is */
783 lex_tokench(lex, '\\');
785 /* add the character finally */
786 lex_tokench(lex, ch);
789 lex_tokench(lex, ch);
791 lexerror(lex, "unexpected end of file within string constant");
792 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
793 return (lex->tok.ttype = TOKEN_ERROR);
796 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
802 /* parse a number... */
803 lex->tok.ttype = TOKEN_INTCONST;
805 lex_tokench(lex, ch);
808 if (ch != '.' && !isdigit(ch))
810 if (lastch != '0' || ch != 'x')
812 /* end of the number or EOF */
813 lex_ungetch(lex, ch);
816 lex->tok.constval.i = lastch - '0';
817 return lex->tok.ttype;
823 /* EOF would have been caught above */
827 lex_tokench(lex, ch);
829 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
831 lex_tokench(lex, ch);
835 /* NOT else, '.' can come from above as well */
836 if (ch == '.' && !ishex)
838 /* Allow floating comma in non-hex mode */
839 lex->tok.ttype = TOKEN_FLOATCONST;
840 lex_tokench(lex, ch);
842 /* continue digits-only */
846 lex_tokench(lex, ch);
850 /* put back the last character */
851 /* but do not put back the trailing 'f' or a float */
852 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
855 /* generally we don't want words to follow numbers: */
857 lexerror(lex, "unexpected trailing characters after number");
858 return (lex->tok.ttype = TOKEN_ERROR);
860 lex_ungetch(lex, ch);
863 if (lex->tok.ttype == TOKEN_FLOATCONST)
864 lex->tok.constval.f = strtod(lex->tok.value, NULL);
866 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
867 return lex->tok.ttype;
870 int lex_do(lex_file *lex)
872 int ch, nextch, thirdch;
881 ch = lex_skipwhite(lex);
882 if (!lex->flags.mergelines || ch != '\\')
886 lex_ungetch(lex, ch);
890 /* we reached a linemerge */
891 lex_tokench(lex, '\n');
895 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
896 return (lex->tok.ttype = ch);
899 lex->sline = lex->line;
900 lex->tok.ctx.line = lex->sline;
901 lex->tok.ctx.file = lex->name;
904 return (lex->tok.ttype = TOKEN_FATAL);
908 return (lex->tok.ttype = TOKEN_EOF);
911 /* modelgen / spiritgen commands */
917 if (!isident_start(ch)) {
918 lexerror(lex, "hanging '$' modelgen/spritegen command line");
921 lex_tokench(lex, ch);
922 if (!lex_finish_ident(lex))
923 return (lex->tok.ttype = TOKEN_ERROR);
925 /* skip the known commands */
928 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
930 /* frame/framesave command works like an enum
931 * similar to fteqcc we handle this in the lexer.
932 * The reason for this is that it is sensitive to newlines,
933 * which the parser is unaware of
935 if (!lex_finish_frames(lex))
936 return (lex->tok.ttype = TOKEN_ERROR);
940 if (!strcmp(v, "framevalue"))
943 while (ch != EOF && isspace(ch) && ch != '\n')
947 lexerror(lex, "$framevalue requires an integer parameter");
952 lex->tok.ttype = lex_finish_digit(lex, ch);
954 if (lex->tok.ttype != TOKEN_INTCONST) {
955 lexerror(lex, "$framevalue requires an integer parameter");
958 lex->framevalue = lex->tok.constval.i;
962 if (!strcmp(v, "framerestore"))
968 rc = lex_parse_frame(lex);
971 lexerror(lex, "$framerestore requires a framename parameter");
975 return (lex->tok.ttype = TOKEN_FATAL);
978 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
979 if (!strcmp(v, lex->frames[frame].name)) {
980 lex->framevalue = lex->frames[frame].value;
984 lexerror(lex, "unknown framename `%s`", v);
988 if (!strcmp(v, "modelname"))
994 rc = lex_parse_frame(lex);
997 lexerror(lex, "$modelname requires a parameter");
1001 return (lex->tok.ttype = TOKEN_FATAL);
1004 if (lex->modelname) {
1006 m.value = lex->framevalue;
1007 m.name = lex->modelname;
1008 lex->modelname = NULL;
1009 vec_push(lex->frames, m);
1011 lex->modelname = lex->tok.value;
1012 lex->tok.value = NULL;
1016 if (!strcmp(v, "flush"))
1019 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1020 mem_d(lex->frames[fi].name);
1021 vec_free(lex->frames);
1022 /* skip line (fteqcc does it too) */
1023 ch = lex_getch(lex);
1024 while (ch != EOF && ch != '\n')
1025 ch = lex_getch(lex);
1029 if (!strcmp(v, "cd") ||
1030 !strcmp(v, "origin") ||
1031 !strcmp(v, "base") ||
1032 !strcmp(v, "flags") ||
1033 !strcmp(v, "scale") ||
1037 ch = lex_getch(lex);
1038 while (ch != EOF && ch != '\n')
1039 ch = lex_getch(lex);
1043 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1044 if (!strcmp(v, lex->frames[frame].name)) {
1045 lex->tok.constval.i = lex->frames[frame].value;
1046 return (lex->tok.ttype = TOKEN_INTCONST);
1050 lexerror(lex, "invalid frame macro");
1054 /* single-character tokens */
1061 lex_tokench(lex, ch);
1063 if (lex->flags.noops)
1064 return (lex->tok.ttype = ch);
1066 return (lex->tok.ttype = TOKEN_OPERATOR);
1074 lex_tokench(lex, ch);
1076 return (lex->tok.ttype = ch);
1081 if (lex->flags.noops)
1083 /* Detect characters early which are normally
1084 * operators OR PART of an operator.
1103 lex_tokench(lex, ch);
1105 return (lex->tok.ttype = ch);
1112 lex_tokench(lex, ch);
1113 /* peak ahead once */
1114 nextch = lex_getch(lex);
1115 if (nextch != '.') {
1116 lex_ungetch(lex, nextch);
1118 return (lex->tok.ttype = ch);
1120 /* peak ahead again */
1121 nextch = lex_getch(lex);
1122 if (nextch != '.') {
1123 lex_ungetch(lex, nextch);
1124 lex_ungetch(lex, '.');
1126 return (lex->tok.ttype = ch);
1128 /* fill the token to be "..." */
1129 lex_tokench(lex, ch);
1130 lex_tokench(lex, ch);
1132 return (lex->tok.ttype = TOKEN_DOTS);
1136 if (ch == ',' || ch == '.') {
1137 lex_tokench(lex, ch);
1139 return (lex->tok.ttype = TOKEN_OPERATOR);
1142 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1143 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1144 ch == '=' || ch == '!' || /* ==, != */
1145 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1147 lex_tokench(lex, ch);
1149 nextch = lex_getch(lex);
1150 if (nextch == ch || nextch == '=') {
1151 lex_tokench(lex, nextch);
1152 } else if (ch == '-' && nextch == '>') {
1153 lex_tokench(lex, nextch);
1154 } else if (ch == '&' && nextch == '~') {
1155 thirdch = lex_getch(lex);
1156 if (thirdch != '=') {
1157 lex_ungetch(lex, thirdch);
1158 lex_ungetch(lex, nextch);
1161 lex_tokench(lex, nextch);
1162 lex_tokench(lex, thirdch);
1165 lex_ungetch(lex, nextch);
1168 return (lex->tok.ttype = TOKEN_OPERATOR);
1172 if (ch == '^' || ch == '~' || ch == '!')
1174 lex_tokench(lex, ch);
1176 return (lex->tok.ttype = TOKEN_OPERATOR);
1180 if (ch == '*' || ch == '/') /* *=, /= */
1182 lex_tokench(lex, ch);
1184 nextch = lex_getch(lex);
1185 if (nextch == '=') {
1186 lex_tokench(lex, nextch);
1188 lex_ungetch(lex, nextch);
1191 return (lex->tok.ttype = TOKEN_OPERATOR);
1194 if (isident_start(ch))
1198 lex_tokench(lex, ch);
1199 if (!lex_finish_ident(lex)) {
1201 return (lex->tok.ttype = TOKEN_ERROR);
1204 lex->tok.ttype = TOKEN_IDENT;
1207 if (!strcmp(v, "void")) {
1208 lex->tok.ttype = TOKEN_TYPENAME;
1209 lex->tok.constval.t = TYPE_VOID;
1210 } else if (!strcmp(v, "int")) {
1211 lex->tok.ttype = TOKEN_TYPENAME;
1212 lex->tok.constval.t = TYPE_INTEGER;
1213 } else if (!strcmp(v, "float")) {
1214 lex->tok.ttype = TOKEN_TYPENAME;
1215 lex->tok.constval.t = TYPE_FLOAT;
1216 } else if (!strcmp(v, "string")) {
1217 lex->tok.ttype = TOKEN_TYPENAME;
1218 lex->tok.constval.t = TYPE_STRING;
1219 } else if (!strcmp(v, "entity")) {
1220 lex->tok.ttype = TOKEN_TYPENAME;
1221 lex->tok.constval.t = TYPE_ENTITY;
1222 } else if (!strcmp(v, "vector")) {
1223 lex->tok.ttype = TOKEN_TYPENAME;
1224 lex->tok.constval.t = TYPE_VECTOR;
1227 for (kw = 0; kw < num_keywords_qc; ++kw) {
1228 if (!strcmp(v, keywords_qc[kw]))
1229 return (lex->tok.ttype = TOKEN_KEYWORD);
1231 if (opts_standard != COMPILER_QCC) {
1232 for (kw = 0; kw < num_keywords_fg; ++kw) {
1233 if (!strcmp(v, keywords_fg[kw]))
1234 return (lex->tok.ttype = TOKEN_KEYWORD);
1239 return lex->tok.ttype;
1244 lex->flags.nodigraphs = true;
1245 if (lex->flags.preprocessing)
1246 lex_tokench(lex, ch);
1247 lex->tok.ttype = lex_finish_string(lex, '"');
1248 if (lex->flags.preprocessing)
1249 lex_tokench(lex, ch);
1250 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1252 /* Allow c style "string" "continuation" */
1253 ch = lex_skipwhite(lex);
1255 lex_ungetch(lex, ch);
1259 lex->tok.ttype = lex_finish_string(lex, '"');
1261 lex->flags.nodigraphs = false;
1263 return lex->tok.ttype;
1268 /* we parse character constants like string,
1269 * but return TOKEN_CHARCONST, or a vector type if it fits...
1270 * Likewise actual unescaping has to be done by the parser.
1271 * The difference is we don't allow 'char' 'continuation'.
1273 if (lex->flags.preprocessing)
1274 lex_tokench(lex, ch);
1275 lex->tok.ttype = lex_finish_string(lex, '\'');
1276 if (lex->flags.preprocessing)
1277 lex_tokench(lex, ch);
1280 /* It's a vector if we can successfully scan 3 floats */
1282 if (sscanf_s(lex->tok.value, " %f %f %f ",
1283 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1285 if (sscanf(lex->tok.value, " %f %f %f ",
1286 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1290 lex->tok.ttype = TOKEN_VECTORCONST;
1293 return lex->tok.ttype;
1298 lex->tok.ttype = lex_finish_digit(lex, ch);
1300 return lex->tok.ttype;
1303 lexerror(lex, "unknown token");
1304 return (lex->tok.ttype = TOKEN_ERROR);