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... */
804 lex->tok.ttype = TOKEN_FLOATCONST;
806 lex->tok.ttype = TOKEN_INTCONST;
808 lex_tokench(lex, ch);
811 if (ch != '.' && !isdigit(ch))
813 if (lastch != '0' || ch != 'x')
815 /* end of the number or EOF */
816 lex_ungetch(lex, ch);
819 lex->tok.constval.i = lastch - '0';
820 return lex->tok.ttype;
826 /* EOF would have been caught above */
830 lex_tokench(lex, ch);
832 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
834 lex_tokench(lex, ch);
838 /* NOT else, '.' can come from above as well */
839 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
841 /* Allow floating comma in non-hex mode */
842 lex->tok.ttype = TOKEN_FLOATCONST;
843 lex_tokench(lex, ch);
845 /* continue digits-only */
849 lex_tokench(lex, ch);
853 /* put back the last character */
854 /* but do not put back the trailing 'f' or a float */
855 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
858 /* generally we don't want words to follow numbers: */
860 lexerror(lex, "unexpected trailing characters after number");
861 return (lex->tok.ttype = TOKEN_ERROR);
863 lex_ungetch(lex, ch);
866 if (lex->tok.ttype == TOKEN_FLOATCONST)
867 lex->tok.constval.f = strtod(lex->tok.value, NULL);
869 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
870 return lex->tok.ttype;
873 int lex_do(lex_file *lex)
875 int ch, nextch, thirdch;
884 ch = lex_skipwhite(lex);
885 if (!lex->flags.mergelines || ch != '\\')
889 lex_ungetch(lex, ch);
893 /* we reached a linemerge */
894 lex_tokench(lex, '\n');
898 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
899 return (lex->tok.ttype = ch);
902 lex->sline = lex->line;
903 lex->tok.ctx.line = lex->sline;
904 lex->tok.ctx.file = lex->name;
907 return (lex->tok.ttype = TOKEN_FATAL);
911 return (lex->tok.ttype = TOKEN_EOF);
914 /* modelgen / spiritgen commands */
920 if (!isident_start(ch)) {
921 lexerror(lex, "hanging '$' modelgen/spritegen command line");
924 lex_tokench(lex, ch);
925 if (!lex_finish_ident(lex))
926 return (lex->tok.ttype = TOKEN_ERROR);
928 /* skip the known commands */
931 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
933 /* frame/framesave command works like an enum
934 * similar to fteqcc we handle this in the lexer.
935 * The reason for this is that it is sensitive to newlines,
936 * which the parser is unaware of
938 if (!lex_finish_frames(lex))
939 return (lex->tok.ttype = TOKEN_ERROR);
943 if (!strcmp(v, "framevalue"))
946 while (ch != EOF && isspace(ch) && ch != '\n')
950 lexerror(lex, "$framevalue requires an integer parameter");
955 lex->tok.ttype = lex_finish_digit(lex, ch);
957 if (lex->tok.ttype != TOKEN_INTCONST) {
958 lexerror(lex, "$framevalue requires an integer parameter");
961 lex->framevalue = lex->tok.constval.i;
965 if (!strcmp(v, "framerestore"))
971 rc = lex_parse_frame(lex);
974 lexerror(lex, "$framerestore requires a framename parameter");
978 return (lex->tok.ttype = TOKEN_FATAL);
981 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
982 if (!strcmp(v, lex->frames[frame].name)) {
983 lex->framevalue = lex->frames[frame].value;
987 lexerror(lex, "unknown framename `%s`", v);
991 if (!strcmp(v, "modelname"))
997 rc = lex_parse_frame(lex);
1000 lexerror(lex, "$modelname requires a parameter");
1004 return (lex->tok.ttype = TOKEN_FATAL);
1007 if (lex->modelname) {
1009 m.value = lex->framevalue;
1010 m.name = lex->modelname;
1011 lex->modelname = NULL;
1012 vec_push(lex->frames, m);
1014 lex->modelname = lex->tok.value;
1015 lex->tok.value = NULL;
1019 if (!strcmp(v, "flush"))
1022 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1023 mem_d(lex->frames[fi].name);
1024 vec_free(lex->frames);
1025 /* skip line (fteqcc does it too) */
1026 ch = lex_getch(lex);
1027 while (ch != EOF && ch != '\n')
1028 ch = lex_getch(lex);
1032 if (!strcmp(v, "cd") ||
1033 !strcmp(v, "origin") ||
1034 !strcmp(v, "base") ||
1035 !strcmp(v, "flags") ||
1036 !strcmp(v, "scale") ||
1040 ch = lex_getch(lex);
1041 while (ch != EOF && ch != '\n')
1042 ch = lex_getch(lex);
1046 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1047 if (!strcmp(v, lex->frames[frame].name)) {
1048 lex->tok.constval.i = lex->frames[frame].value;
1049 return (lex->tok.ttype = TOKEN_INTCONST);
1053 lexerror(lex, "invalid frame macro");
1057 /* single-character tokens */
1064 lex_tokench(lex, ch);
1066 if (lex->flags.noops)
1067 return (lex->tok.ttype = ch);
1069 return (lex->tok.ttype = TOKEN_OPERATOR);
1077 lex_tokench(lex, ch);
1079 return (lex->tok.ttype = ch);
1085 nextch = lex_getch(lex);
1086 /* digits starting with a dot */
1087 if (isdigit(nextch)) {
1088 lex_ungetch(lex, nextch);
1089 lex->tok.ttype = lex_finish_digit(lex, ch);
1091 return lex->tok.ttype;
1093 lex_ungetch(lex, nextch);
1096 if (lex->flags.noops)
1098 /* Detect characters early which are normally
1099 * operators OR PART of an operator.
1118 lex_tokench(lex, ch);
1120 return (lex->tok.ttype = ch);
1127 lex_tokench(lex, ch);
1128 /* peak ahead once */
1129 nextch = lex_getch(lex);
1130 if (nextch != '.') {
1131 lex_ungetch(lex, nextch);
1133 return (lex->tok.ttype = ch);
1135 /* peak ahead again */
1136 nextch = lex_getch(lex);
1137 if (nextch != '.') {
1138 lex_ungetch(lex, nextch);
1139 lex_ungetch(lex, '.');
1141 return (lex->tok.ttype = ch);
1143 /* fill the token to be "..." */
1144 lex_tokench(lex, ch);
1145 lex_tokench(lex, ch);
1147 return (lex->tok.ttype = TOKEN_DOTS);
1151 if (ch == ',' || ch == '.') {
1152 lex_tokench(lex, ch);
1154 return (lex->tok.ttype = TOKEN_OPERATOR);
1157 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1158 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1159 ch == '=' || ch == '!' || /* ==, != */
1160 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1162 lex_tokench(lex, ch);
1164 nextch = lex_getch(lex);
1165 if (nextch == ch || nextch == '=') {
1166 lex_tokench(lex, nextch);
1167 } else if (ch == '-' && nextch == '>') {
1168 lex_tokench(lex, nextch);
1169 } else if (ch == '&' && nextch == '~') {
1170 thirdch = lex_getch(lex);
1171 if (thirdch != '=') {
1172 lex_ungetch(lex, thirdch);
1173 lex_ungetch(lex, nextch);
1176 lex_tokench(lex, nextch);
1177 lex_tokench(lex, thirdch);
1180 lex_ungetch(lex, nextch);
1183 return (lex->tok.ttype = TOKEN_OPERATOR);
1187 if (ch == '^' || ch == '~' || ch == '!')
1189 lex_tokench(lex, ch);
1191 return (lex->tok.ttype = TOKEN_OPERATOR);
1195 if (ch == '*' || ch == '/') /* *=, /= */
1197 lex_tokench(lex, ch);
1199 nextch = lex_getch(lex);
1200 if (nextch == '=') {
1201 lex_tokench(lex, nextch);
1203 lex_ungetch(lex, nextch);
1206 return (lex->tok.ttype = TOKEN_OPERATOR);
1209 if (isident_start(ch))
1213 lex_tokench(lex, ch);
1214 if (!lex_finish_ident(lex)) {
1216 return (lex->tok.ttype = TOKEN_ERROR);
1219 lex->tok.ttype = TOKEN_IDENT;
1222 if (!strcmp(v, "void")) {
1223 lex->tok.ttype = TOKEN_TYPENAME;
1224 lex->tok.constval.t = TYPE_VOID;
1225 } else if (!strcmp(v, "int")) {
1226 lex->tok.ttype = TOKEN_TYPENAME;
1227 lex->tok.constval.t = TYPE_INTEGER;
1228 } else if (!strcmp(v, "float")) {
1229 lex->tok.ttype = TOKEN_TYPENAME;
1230 lex->tok.constval.t = TYPE_FLOAT;
1231 } else if (!strcmp(v, "string")) {
1232 lex->tok.ttype = TOKEN_TYPENAME;
1233 lex->tok.constval.t = TYPE_STRING;
1234 } else if (!strcmp(v, "entity")) {
1235 lex->tok.ttype = TOKEN_TYPENAME;
1236 lex->tok.constval.t = TYPE_ENTITY;
1237 } else if (!strcmp(v, "vector")) {
1238 lex->tok.ttype = TOKEN_TYPENAME;
1239 lex->tok.constval.t = TYPE_VECTOR;
1242 for (kw = 0; kw < num_keywords_qc; ++kw) {
1243 if (!strcmp(v, keywords_qc[kw]))
1244 return (lex->tok.ttype = TOKEN_KEYWORD);
1246 if (opts_standard != COMPILER_QCC) {
1247 for (kw = 0; kw < num_keywords_fg; ++kw) {
1248 if (!strcmp(v, keywords_fg[kw]))
1249 return (lex->tok.ttype = TOKEN_KEYWORD);
1254 return lex->tok.ttype;
1259 lex->flags.nodigraphs = true;
1260 if (lex->flags.preprocessing)
1261 lex_tokench(lex, ch);
1262 lex->tok.ttype = lex_finish_string(lex, '"');
1263 if (lex->flags.preprocessing)
1264 lex_tokench(lex, ch);
1265 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1267 /* Allow c style "string" "continuation" */
1268 ch = lex_skipwhite(lex);
1270 lex_ungetch(lex, ch);
1274 lex->tok.ttype = lex_finish_string(lex, '"');
1276 lex->flags.nodigraphs = false;
1278 return lex->tok.ttype;
1283 /* we parse character constants like string,
1284 * but return TOKEN_CHARCONST, or a vector type if it fits...
1285 * Likewise actual unescaping has to be done by the parser.
1286 * The difference is we don't allow 'char' 'continuation'.
1288 if (lex->flags.preprocessing)
1289 lex_tokench(lex, ch);
1290 lex->tok.ttype = lex_finish_string(lex, '\'');
1291 if (lex->flags.preprocessing)
1292 lex_tokench(lex, ch);
1295 lex->tok.ttype = TOKEN_CHARCONST;
1296 /* It's a vector if we can successfully scan 3 floats */
1298 if (sscanf_s(lex->tok.value, " %f %f %f ",
1299 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1301 if (sscanf(lex->tok.value, " %f %f %f ",
1302 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1306 lex->tok.ttype = TOKEN_VECTORCONST;
1310 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1311 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER, "multibyte character: `%s`", lex->tok.value))
1312 return (lex->tok.ttype = TOKEN_ERROR);
1314 lex->tok.constval.i = lex->tok.value[0];
1317 return lex->tok.ttype;
1322 lex->tok.ttype = lex_finish_digit(lex, ch);
1324 return lex->tok.ttype;
1327 lexerror(lex, "unknown token");
1328 return (lex->tok.ttype = TOKEN_ERROR);