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)
750 return TOKEN_STRINGCONST;
752 if (lex->flags.preprocessing && ch == '\\') {
753 lex_tokench(lex, ch);
756 lexerror(lex, "unexpected end of file");
757 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
758 return (lex->tok.ttype = TOKEN_ERROR);
760 lex_tokench(lex, ch);
762 else if (ch == '\\') {
765 lexerror(lex, "unexpected end of file");
766 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
767 return (lex->tok.ttype = TOKEN_ERROR);
774 case 'a': ch = '\a'; break;
775 case 'b': ch = '\b'; break;
776 case 'r': ch = '\r'; break;
777 case 'n': ch = '\n'; break;
778 case 't': ch = '\t'; break;
779 case 'f': ch = '\f'; break;
780 case 'v': ch = '\v'; break;
781 case '\n': ch = '\n'; break;
783 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
784 /* so we just add the character plus backslash no matter what it actually is */
785 lex_tokench(lex, '\\');
787 /* add the character finally */
788 lex_tokench(lex, ch);
791 lex_tokench(lex, ch);
793 lexerror(lex, "unexpected end of file within string constant");
794 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
795 return (lex->tok.ttype = TOKEN_ERROR);
798 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
804 /* parse a number... */
806 lex->tok.ttype = TOKEN_FLOATCONST;
808 lex->tok.ttype = TOKEN_INTCONST;
810 lex_tokench(lex, ch);
813 if (ch != '.' && !isdigit(ch))
815 if (lastch != '0' || ch != 'x')
817 /* end of the number or EOF */
818 lex_ungetch(lex, ch);
821 lex->tok.constval.i = lastch - '0';
822 return lex->tok.ttype;
828 /* EOF would have been caught above */
832 lex_tokench(lex, ch);
834 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
836 lex_tokench(lex, ch);
840 /* NOT else, '.' can come from above as well */
841 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
843 /* Allow floating comma in non-hex mode */
844 lex->tok.ttype = TOKEN_FLOATCONST;
845 lex_tokench(lex, ch);
847 /* continue digits-only */
851 lex_tokench(lex, ch);
855 /* put back the last character */
856 /* but do not put back the trailing 'f' or a float */
857 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
860 /* generally we don't want words to follow numbers: */
862 lexerror(lex, "unexpected trailing characters after number");
863 return (lex->tok.ttype = TOKEN_ERROR);
865 lex_ungetch(lex, ch);
868 if (lex->tok.ttype == TOKEN_FLOATCONST)
869 lex->tok.constval.f = strtod(lex->tok.value, NULL);
871 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
872 return lex->tok.ttype;
875 int lex_do(lex_file *lex)
877 int ch, nextch, thirdch;
886 ch = lex_skipwhite(lex);
887 if (!lex->flags.mergelines || ch != '\\')
891 lex_ungetch(lex, ch);
895 /* we reached a linemerge */
896 lex_tokench(lex, '\n');
900 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
901 return (lex->tok.ttype = ch);
904 lex->sline = lex->line;
905 lex->tok.ctx.line = lex->sline;
906 lex->tok.ctx.file = lex->name;
909 return (lex->tok.ttype = TOKEN_FATAL);
913 return (lex->tok.ttype = TOKEN_EOF);
916 /* modelgen / spiritgen commands */
922 if (!isident_start(ch)) {
923 lexerror(lex, "hanging '$' modelgen/spritegen command line");
926 lex_tokench(lex, ch);
927 if (!lex_finish_ident(lex))
928 return (lex->tok.ttype = TOKEN_ERROR);
930 /* skip the known commands */
933 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
935 /* frame/framesave command works like an enum
936 * similar to fteqcc we handle this in the lexer.
937 * The reason for this is that it is sensitive to newlines,
938 * which the parser is unaware of
940 if (!lex_finish_frames(lex))
941 return (lex->tok.ttype = TOKEN_ERROR);
945 if (!strcmp(v, "framevalue"))
948 while (ch != EOF && isspace(ch) && ch != '\n')
952 lexerror(lex, "$framevalue requires an integer parameter");
957 lex->tok.ttype = lex_finish_digit(lex, ch);
959 if (lex->tok.ttype != TOKEN_INTCONST) {
960 lexerror(lex, "$framevalue requires an integer parameter");
963 lex->framevalue = lex->tok.constval.i;
967 if (!strcmp(v, "framerestore"))
973 rc = lex_parse_frame(lex);
976 lexerror(lex, "$framerestore requires a framename parameter");
980 return (lex->tok.ttype = TOKEN_FATAL);
983 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
984 if (!strcmp(v, lex->frames[frame].name)) {
985 lex->framevalue = lex->frames[frame].value;
989 lexerror(lex, "unknown framename `%s`", v);
993 if (!strcmp(v, "modelname"))
999 rc = lex_parse_frame(lex);
1002 lexerror(lex, "$modelname requires a parameter");
1006 return (lex->tok.ttype = TOKEN_FATAL);
1009 if (lex->modelname) {
1011 m.value = lex->framevalue;
1012 m.name = lex->modelname;
1013 lex->modelname = NULL;
1014 vec_push(lex->frames, m);
1016 lex->modelname = lex->tok.value;
1017 lex->tok.value = NULL;
1021 if (!strcmp(v, "flush"))
1024 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1025 mem_d(lex->frames[fi].name);
1026 vec_free(lex->frames);
1027 /* skip line (fteqcc does it too) */
1028 ch = lex_getch(lex);
1029 while (ch != EOF && ch != '\n')
1030 ch = lex_getch(lex);
1034 if (!strcmp(v, "cd") ||
1035 !strcmp(v, "origin") ||
1036 !strcmp(v, "base") ||
1037 !strcmp(v, "flags") ||
1038 !strcmp(v, "scale") ||
1042 ch = lex_getch(lex);
1043 while (ch != EOF && ch != '\n')
1044 ch = lex_getch(lex);
1048 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1049 if (!strcmp(v, lex->frames[frame].name)) {
1050 lex->tok.constval.i = lex->frames[frame].value;
1051 return (lex->tok.ttype = TOKEN_INTCONST);
1055 lexerror(lex, "invalid frame macro");
1059 /* single-character tokens */
1066 lex_tokench(lex, ch);
1068 if (lex->flags.noops)
1069 return (lex->tok.ttype = ch);
1071 return (lex->tok.ttype = TOKEN_OPERATOR);
1079 lex_tokench(lex, ch);
1081 return (lex->tok.ttype = ch);
1087 nextch = lex_getch(lex);
1088 /* digits starting with a dot */
1089 if (isdigit(nextch)) {
1090 lex_ungetch(lex, nextch);
1091 lex->tok.ttype = lex_finish_digit(lex, ch);
1093 return lex->tok.ttype;
1095 lex_ungetch(lex, nextch);
1098 if (lex->flags.noops)
1100 /* Detect characters early which are normally
1101 * operators OR PART of an operator.
1120 lex_tokench(lex, ch);
1122 return (lex->tok.ttype = ch);
1129 lex_tokench(lex, ch);
1130 /* peak ahead once */
1131 nextch = lex_getch(lex);
1132 if (nextch != '.') {
1133 lex_ungetch(lex, nextch);
1135 return (lex->tok.ttype = ch);
1137 /* peak ahead again */
1138 nextch = lex_getch(lex);
1139 if (nextch != '.') {
1140 lex_ungetch(lex, nextch);
1141 lex_ungetch(lex, '.');
1143 return (lex->tok.ttype = ch);
1145 /* fill the token to be "..." */
1146 lex_tokench(lex, ch);
1147 lex_tokench(lex, ch);
1149 return (lex->tok.ttype = TOKEN_DOTS);
1153 if (ch == ',' || ch == '.') {
1154 lex_tokench(lex, ch);
1156 return (lex->tok.ttype = TOKEN_OPERATOR);
1159 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1160 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1161 ch == '=' || ch == '!' || /* ==, != */
1162 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1164 lex_tokench(lex, ch);
1166 nextch = lex_getch(lex);
1167 if (nextch == '=' || (nextch == ch && ch != '!')) {
1168 lex_tokench(lex, nextch);
1169 } else if (ch == '-' && nextch == '>') {
1170 lex_tokench(lex, nextch);
1171 } else if (ch == '&' && nextch == '~') {
1172 thirdch = lex_getch(lex);
1173 if (thirdch != '=') {
1174 lex_ungetch(lex, thirdch);
1175 lex_ungetch(lex, nextch);
1178 lex_tokench(lex, nextch);
1179 lex_tokench(lex, thirdch);
1182 lex_ungetch(lex, nextch);
1185 return (lex->tok.ttype = TOKEN_OPERATOR);
1189 if (ch == '^' || ch == '~' || ch == '!')
1191 lex_tokench(lex, ch);
1193 return (lex->tok.ttype = TOKEN_OPERATOR);
1197 if (ch == '*' || ch == '/') /* *=, /= */
1199 lex_tokench(lex, ch);
1201 nextch = lex_getch(lex);
1202 if (nextch == '=') {
1203 lex_tokench(lex, nextch);
1205 lex_ungetch(lex, nextch);
1208 return (lex->tok.ttype = TOKEN_OPERATOR);
1211 if (isident_start(ch))
1215 lex_tokench(lex, ch);
1216 if (!lex_finish_ident(lex)) {
1218 return (lex->tok.ttype = TOKEN_ERROR);
1221 lex->tok.ttype = TOKEN_IDENT;
1224 if (!strcmp(v, "void")) {
1225 lex->tok.ttype = TOKEN_TYPENAME;
1226 lex->tok.constval.t = TYPE_VOID;
1227 } else if (!strcmp(v, "int")) {
1228 lex->tok.ttype = TOKEN_TYPENAME;
1229 lex->tok.constval.t = TYPE_INTEGER;
1230 } else if (!strcmp(v, "float")) {
1231 lex->tok.ttype = TOKEN_TYPENAME;
1232 lex->tok.constval.t = TYPE_FLOAT;
1233 } else if (!strcmp(v, "string")) {
1234 lex->tok.ttype = TOKEN_TYPENAME;
1235 lex->tok.constval.t = TYPE_STRING;
1236 } else if (!strcmp(v, "entity")) {
1237 lex->tok.ttype = TOKEN_TYPENAME;
1238 lex->tok.constval.t = TYPE_ENTITY;
1239 } else if (!strcmp(v, "vector")) {
1240 lex->tok.ttype = TOKEN_TYPENAME;
1241 lex->tok.constval.t = TYPE_VECTOR;
1244 for (kw = 0; kw < num_keywords_qc; ++kw) {
1245 if (!strcmp(v, keywords_qc[kw]))
1246 return (lex->tok.ttype = TOKEN_KEYWORD);
1248 if (opts_standard != COMPILER_QCC) {
1249 for (kw = 0; kw < num_keywords_fg; ++kw) {
1250 if (!strcmp(v, keywords_fg[kw]))
1251 return (lex->tok.ttype = TOKEN_KEYWORD);
1256 return lex->tok.ttype;
1261 lex->flags.nodigraphs = true;
1262 if (lex->flags.preprocessing)
1263 lex_tokench(lex, ch);
1264 lex->tok.ttype = lex_finish_string(lex, '"');
1265 if (lex->flags.preprocessing)
1266 lex_tokench(lex, ch);
1267 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1269 /* Allow c style "string" "continuation" */
1270 ch = lex_skipwhite(lex);
1272 lex_ungetch(lex, ch);
1276 lex->tok.ttype = lex_finish_string(lex, '"');
1278 lex->flags.nodigraphs = false;
1280 return lex->tok.ttype;
1285 /* we parse character constants like string,
1286 * but return TOKEN_CHARCONST, or a vector type if it fits...
1287 * Likewise actual unescaping has to be done by the parser.
1288 * The difference is we don't allow 'char' 'continuation'.
1290 if (lex->flags.preprocessing)
1291 lex_tokench(lex, ch);
1292 lex->tok.ttype = lex_finish_string(lex, '\'');
1293 if (lex->flags.preprocessing)
1294 lex_tokench(lex, ch);
1297 lex->tok.ttype = TOKEN_CHARCONST;
1298 /* It's a vector if we can successfully scan 3 floats */
1300 if (sscanf_s(lex->tok.value, " %f %f %f ",
1301 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1303 if (sscanf(lex->tok.value, " %f %f %f ",
1304 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1308 lex->tok.ttype = TOKEN_VECTORCONST;
1312 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1313 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER, "multibyte character: `%s`", lex->tok.value))
1314 return (lex->tok.ttype = TOKEN_ERROR);
1316 lex->tok.constval.i = lex->tok.value[0];
1319 return lex->tok.ttype;
1324 lex->tok.ttype = lex_finish_digit(lex, ch);
1326 return lex->tok.ttype;
1329 lexerror(lex, "unknown token");
1330 return (lex->tok.ttype = TOKEN_ERROR);