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, (opts.werror ? "error" : "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) < 1024 && 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));
495 lex_ungetch(lex, ' ');
499 while (vec_size(command)) {
500 lex_ungetch(lex, vec_last(command));
504 lex_ungetch(lex, ' ');
508 while (vec_size(pragma)) {
509 lex_ungetch(lex, vec_last(pragma));
514 lex_ungetch(lex, '#');
520 /* Skip whitespace and comments and return the first
521 * non-white character.
522 * As this makes use of the above getch() ungetch() functions,
523 * we don't need to care at all about line numbering anymore.
525 * In theory, this function should only be used at the beginning
526 * of lexing, or when we *know* the next character is part of the token.
527 * Otherwise, if the parser throws an error, the linenumber may not be
528 * the line of the error, but the line of the next token AFTER the error.
530 * This is currently only problematic when using c-like string-continuation,
531 * since comments and whitespaces are allowed between 2 such strings.
535 "A continuation of the previous string"
536 // This line is skipped
539 * In this case, if the parse decides it didn't actually want a string,
540 * and uses lex->line to print an error, it will show the ', foo);' line's
543 * On the other hand, the parser is supposed to remember the line of the next
544 * token's beginning. In this case we would want skipwhite() to be called
545 * AFTER reading a token, so that the parser, before reading the NEXT token,
546 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
549 * here is to store the line of the first character after skipping
550 * the initial whitespace in lex->sline, this happens in lex_do.
552 static int lex_skipwhite(lex_file *lex, bool hadwhite)
555 bool haswhite = hadwhite;
560 while (ch != EOF && isspace(ch)) {
562 if (lex_try_pragma(lex))
565 if (lex->flags.preprocessing) {
568 /* see if there was whitespace first */
569 if (haswhite) { /* (vec_size(lex->tok.value)) { */
570 lex_ungetch(lex, ch);
574 /* otherwise return EOL */
578 lex_tokench(lex, ch);
587 /* one line comment */
590 if (lex->flags.preprocessing) {
593 lex_tokench(lex, '/');
594 lex_tokench(lex, '/');
596 lex_tokench(lex, ' ');
597 lex_tokench(lex, ' ');
600 while (ch != EOF && ch != '\n') {
601 if (lex->flags.preprocessing)
602 lex_tokench(lex, ' '); /* ch); */
605 if (lex->flags.preprocessing) {
606 lex_ungetch(lex, '\n');
614 /* multiline comment */
615 if (lex->flags.preprocessing) {
618 lex_tokench(lex, '/');
619 lex_tokench(lex, '*');
621 lex_tokench(lex, ' ');
622 lex_tokench(lex, ' ');
631 if (lex->flags.preprocessing) {
633 lex_tokench(lex, '*');
634 lex_tokench(lex, '/');
636 lex_tokench(lex, ' ');
637 lex_tokench(lex, ' ');
641 lex_ungetch(lex, ch);
643 if (lex->flags.preprocessing) {
645 lex_tokench(lex, '\n');
647 lex_tokench(lex, ' '); /* ch); */
650 ch = ' '; /* cause TRUE in the isspace check */
653 /* Otherwise roll back to the slash and break out of the loop */
654 lex_ungetch(lex, ch);
658 } while (ch != EOF && isspace(ch));
662 lex_ungetch(lex, ch);
669 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
674 while (ch != EOF && isident(ch))
676 lex_tokench(lex, ch);
680 /* last ch was not an ident ch: */
681 lex_ungetch(lex, ch);
686 /* read one ident for the frame list */
687 static int lex_parse_frame(lex_file *lex)
694 while (ch != EOF && ch != '\n' && isspace(ch))
700 if (!isident_start(ch)) {
701 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
705 lex_tokench(lex, ch);
706 if (!lex_finish_ident(lex))
712 /* read a list of $frames */
713 static bool lex_finish_frames(lex_file *lex)
720 rc = lex_parse_frame(lex);
721 if (rc > 0) /* end of line */
723 if (rc < 0) /* error */
726 for (i = 0; i < vec_size(lex->frames); ++i) {
727 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
728 lex->frames[i].value = lex->framevalue++;
729 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
734 if (i < vec_size(lex->frames))
737 m.value = lex->framevalue++;
738 m.name = util_strdup(lex->tok.value);
739 vec_shrinkto(lex->tok.value, 0);
740 vec_push(lex->frames, m);
744 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
753 return TOKEN_STRINGCONST;
755 if (lex->flags.preprocessing && ch == '\\') {
756 lex_tokench(lex, ch);
759 lexerror(lex, "unexpected end of file");
760 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
761 return (lex->tok.ttype = TOKEN_ERROR);
763 lex_tokench(lex, ch);
765 else if (ch == '\\') {
768 lexerror(lex, "unexpected end of file");
769 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
770 return (lex->tok.ttype = TOKEN_ERROR);
777 case 'a': ch = '\a'; break;
778 case 'b': ch = '\b'; break;
779 case 'r': ch = '\r'; break;
780 case 'n': ch = '\n'; break;
781 case 't': ch = '\t'; break;
782 case 'f': ch = '\f'; break;
783 case 'v': ch = '\v'; break;
786 /* same procedure as in fteqcc */
788 nextch = lex_getch(lex);
789 if (nextch >= '0' && nextch <= '9')
791 else if (nextch >= 'a' && nextch <= 'f')
792 ch += nextch - 'a' + 10;
793 else if (nextch >= 'A' && nextch <= 'F')
794 ch += nextch - 'A' + 10;
796 lexerror(lex, "bad character code");
797 lex_ungetch(lex, nextch);
798 return (lex->tok.ttype = TOKEN_ERROR);
802 nextch = lex_getch(lex);
803 if (nextch >= '0' && nextch <= '9')
805 else if (nextch >= 'a' && nextch <= 'f')
806 ch += nextch - 'a' + 10;
807 else if (nextch >= 'A' && nextch <= 'F')
808 ch += nextch - 'A' + 10;
810 lexerror(lex, "bad character code");
811 lex_ungetch(lex, nextch);
812 return (lex->tok.ttype = TOKEN_ERROR);
817 case '0': case '1': case '2': case '3':
818 case '4': case '5': case '6': case '7':
822 case '<': ch = 29; break;
823 case '-': ch = 30; break;
824 case '>': ch = 31; break;
825 case '[': ch = 16; break;
826 case ']': ch = 17; break;
829 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
830 ch = ch * 10 + nextch - '0';
831 if (nextch < '0' || nextch > '9' || ch > 255) {
832 lexerror(lex, "bad character code");
833 return (lex->tok.ttype = TOKEN_ERROR);
837 case '\n': ch = '\n'; break;
840 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
841 /* so we just add the character plus backslash no matter what it actually is */
842 lex_tokench(lex, '\\');
844 /* add the character finally */
845 lex_tokench(lex, ch);
848 lex_tokench(lex, ch);
850 lexerror(lex, "unexpected end of file within string constant");
851 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
852 return (lex->tok.ttype = TOKEN_ERROR);
855 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
861 /* parse a number... */
863 lex->tok.ttype = TOKEN_FLOATCONST;
865 lex->tok.ttype = TOKEN_INTCONST;
867 lex_tokench(lex, ch);
870 if (ch != '.' && !isdigit(ch))
872 if (lastch != '0' || ch != 'x')
874 /* end of the number or EOF */
875 lex_ungetch(lex, ch);
878 lex->tok.constval.i = lastch - '0';
879 return lex->tok.ttype;
885 /* EOF would have been caught above */
889 lex_tokench(lex, ch);
891 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
893 lex_tokench(lex, ch);
897 /* NOT else, '.' can come from above as well */
898 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
900 /* Allow floating comma in non-hex mode */
901 lex->tok.ttype = TOKEN_FLOATCONST;
902 lex_tokench(lex, ch);
904 /* continue digits-only */
908 lex_tokench(lex, ch);
912 /* put back the last character */
913 /* but do not put back the trailing 'f' or a float */
914 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
917 /* generally we don't want words to follow numbers: */
919 lexerror(lex, "unexpected trailing characters after number");
920 return (lex->tok.ttype = TOKEN_ERROR);
922 lex_ungetch(lex, ch);
925 if (lex->tok.ttype == TOKEN_FLOATCONST)
926 lex->tok.constval.f = strtod(lex->tok.value, NULL);
928 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
929 return lex->tok.ttype;
932 int lex_do(lex_file *lex)
934 int ch, nextch, thirdch;
935 bool hadwhite = false;
944 ch = lex_skipwhite(lex, hadwhite);
946 if (!lex->flags.mergelines || ch != '\\')
950 lex_ungetch(lex, ch);
954 /* we reached a linemerge */
955 lex_tokench(lex, '\n');
959 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
960 return (lex->tok.ttype = ch);
963 lex->sline = lex->line;
964 lex->tok.ctx.line = lex->sline;
965 lex->tok.ctx.file = lex->name;
968 return (lex->tok.ttype = TOKEN_FATAL);
972 return (lex->tok.ttype = TOKEN_EOF);
975 /* modelgen / spiritgen commands */
976 if (ch == '$' && !lex->flags.preprocessing) {
981 if (!isident_start(ch)) {
982 lexerror(lex, "hanging '$' modelgen/spritegen command line");
985 lex_tokench(lex, ch);
986 if (!lex_finish_ident(lex))
987 return (lex->tok.ttype = TOKEN_ERROR);
989 /* skip the known commands */
992 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
994 /* frame/framesave command works like an enum
995 * similar to fteqcc we handle this in the lexer.
996 * The reason for this is that it is sensitive to newlines,
997 * which the parser is unaware of
999 if (!lex_finish_frames(lex))
1000 return (lex->tok.ttype = TOKEN_ERROR);
1004 if (!strcmp(v, "framevalue"))
1006 ch = lex_getch(lex);
1007 while (ch != EOF && isspace(ch) && ch != '\n')
1008 ch = lex_getch(lex);
1011 lexerror(lex, "$framevalue requires an integer parameter");
1016 lex->tok.ttype = lex_finish_digit(lex, ch);
1018 if (lex->tok.ttype != TOKEN_INTCONST) {
1019 lexerror(lex, "$framevalue requires an integer parameter");
1022 lex->framevalue = lex->tok.constval.i;
1026 if (!strcmp(v, "framerestore"))
1032 rc = lex_parse_frame(lex);
1035 lexerror(lex, "$framerestore requires a framename parameter");
1039 return (lex->tok.ttype = TOKEN_FATAL);
1042 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1043 if (!strcmp(v, lex->frames[frame].name)) {
1044 lex->framevalue = lex->frames[frame].value;
1048 lexerror(lex, "unknown framename `%s`", v);
1052 if (!strcmp(v, "modelname"))
1058 rc = lex_parse_frame(lex);
1061 lexerror(lex, "$modelname requires a parameter");
1065 return (lex->tok.ttype = TOKEN_FATAL);
1068 if (lex->modelname) {
1070 m.value = lex->framevalue;
1071 m.name = lex->modelname;
1072 lex->modelname = NULL;
1073 vec_push(lex->frames, m);
1075 lex->modelname = lex->tok.value;
1076 lex->tok.value = NULL;
1080 if (!strcmp(v, "flush"))
1083 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1084 mem_d(lex->frames[fi].name);
1085 vec_free(lex->frames);
1086 /* skip line (fteqcc does it too) */
1087 ch = lex_getch(lex);
1088 while (ch != EOF && ch != '\n')
1089 ch = lex_getch(lex);
1093 if (!strcmp(v, "cd") ||
1094 !strcmp(v, "origin") ||
1095 !strcmp(v, "base") ||
1096 !strcmp(v, "flags") ||
1097 !strcmp(v, "scale") ||
1101 ch = lex_getch(lex);
1102 while (ch != EOF && ch != '\n')
1103 ch = lex_getch(lex);
1107 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1108 if (!strcmp(v, lex->frames[frame].name)) {
1109 lex->tok.constval.i = lex->frames[frame].value;
1110 return (lex->tok.ttype = TOKEN_INTCONST);
1114 lexerror(lex, "invalid frame macro");
1118 /* single-character tokens */
1125 lex_tokench(lex, ch);
1127 if (lex->flags.noops)
1128 return (lex->tok.ttype = ch);
1130 return (lex->tok.ttype = TOKEN_OPERATOR);
1138 lex_tokench(lex, ch);
1140 return (lex->tok.ttype = ch);
1146 nextch = lex_getch(lex);
1147 /* digits starting with a dot */
1148 if (isdigit(nextch)) {
1149 lex_ungetch(lex, nextch);
1150 lex->tok.ttype = lex_finish_digit(lex, ch);
1152 return lex->tok.ttype;
1154 lex_ungetch(lex, nextch);
1157 if (lex->flags.noops)
1159 /* Detect characters early which are normally
1160 * operators OR PART of an operator.
1179 lex_tokench(lex, ch);
1181 return (lex->tok.ttype = ch);
1188 lex_tokench(lex, ch);
1189 /* peak ahead once */
1190 nextch = lex_getch(lex);
1191 if (nextch != '.') {
1192 lex_ungetch(lex, nextch);
1194 return (lex->tok.ttype = ch);
1196 /* peak ahead again */
1197 nextch = lex_getch(lex);
1198 if (nextch != '.') {
1199 lex_ungetch(lex, nextch);
1200 lex_ungetch(lex, '.');
1202 return (lex->tok.ttype = ch);
1204 /* fill the token to be "..." */
1205 lex_tokench(lex, ch);
1206 lex_tokench(lex, ch);
1208 return (lex->tok.ttype = TOKEN_DOTS);
1212 if (ch == ',' || ch == '.') {
1213 lex_tokench(lex, ch);
1215 return (lex->tok.ttype = TOKEN_OPERATOR);
1218 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1219 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1220 ch == '=' || ch == '!' || /* ==, != */
1221 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1223 lex_tokench(lex, ch);
1225 nextch = lex_getch(lex);
1226 if (nextch == '=' || (nextch == ch && ch != '!')) {
1227 lex_tokench(lex, nextch);
1228 } else if (ch == '-' && nextch == '>') {
1229 lex_tokench(lex, nextch);
1230 } else if (ch == '&' && nextch == '~') {
1231 thirdch = lex_getch(lex);
1232 if (thirdch != '=') {
1233 lex_ungetch(lex, thirdch);
1234 lex_ungetch(lex, nextch);
1237 lex_tokench(lex, nextch);
1238 lex_tokench(lex, thirdch);
1241 lex_ungetch(lex, nextch);
1244 return (lex->tok.ttype = TOKEN_OPERATOR);
1248 if (ch == '^' || ch == '~' || ch == '!')
1250 lex_tokench(lex, ch);
1252 return (lex->tok.ttype = TOKEN_OPERATOR);
1256 if (ch == '*' || ch == '/') /* *=, /= */
1258 lex_tokench(lex, ch);
1260 nextch = lex_getch(lex);
1261 if (nextch == '=') {
1262 lex_tokench(lex, nextch);
1264 lex_ungetch(lex, nextch);
1267 return (lex->tok.ttype = TOKEN_OPERATOR);
1270 if (isident_start(ch))
1274 lex_tokench(lex, ch);
1275 if (!lex_finish_ident(lex)) {
1277 return (lex->tok.ttype = TOKEN_ERROR);
1280 lex->tok.ttype = TOKEN_IDENT;
1283 if (!strcmp(v, "void")) {
1284 lex->tok.ttype = TOKEN_TYPENAME;
1285 lex->tok.constval.t = TYPE_VOID;
1286 } else if (!strcmp(v, "int")) {
1287 lex->tok.ttype = TOKEN_TYPENAME;
1288 lex->tok.constval.t = TYPE_INTEGER;
1289 } else if (!strcmp(v, "float")) {
1290 lex->tok.ttype = TOKEN_TYPENAME;
1291 lex->tok.constval.t = TYPE_FLOAT;
1292 } else if (!strcmp(v, "string")) {
1293 lex->tok.ttype = TOKEN_TYPENAME;
1294 lex->tok.constval.t = TYPE_STRING;
1295 } else if (!strcmp(v, "entity")) {
1296 lex->tok.ttype = TOKEN_TYPENAME;
1297 lex->tok.constval.t = TYPE_ENTITY;
1298 } else if (!strcmp(v, "vector")) {
1299 lex->tok.ttype = TOKEN_TYPENAME;
1300 lex->tok.constval.t = TYPE_VECTOR;
1303 for (kw = 0; kw < num_keywords_qc; ++kw) {
1304 if (!strcmp(v, keywords_qc[kw]))
1305 return (lex->tok.ttype = TOKEN_KEYWORD);
1307 if (opts.standard != COMPILER_QCC) {
1308 for (kw = 0; kw < num_keywords_fg; ++kw) {
1309 if (!strcmp(v, keywords_fg[kw]))
1310 return (lex->tok.ttype = TOKEN_KEYWORD);
1315 return lex->tok.ttype;
1320 lex->flags.nodigraphs = true;
1321 if (lex->flags.preprocessing)
1322 lex_tokench(lex, ch);
1323 lex->tok.ttype = lex_finish_string(lex, '"');
1324 if (lex->flags.preprocessing)
1325 lex_tokench(lex, ch);
1326 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1328 /* Allow c style "string" "continuation" */
1329 ch = lex_skipwhite(lex, false);
1331 lex_ungetch(lex, ch);
1335 lex->tok.ttype = lex_finish_string(lex, '"');
1337 lex->flags.nodigraphs = false;
1339 return lex->tok.ttype;
1344 /* we parse character constants like string,
1345 * but return TOKEN_CHARCONST, or a vector type if it fits...
1346 * Likewise actual unescaping has to be done by the parser.
1347 * The difference is we don't allow 'char' 'continuation'.
1349 if (lex->flags.preprocessing)
1350 lex_tokench(lex, ch);
1351 lex->tok.ttype = lex_finish_string(lex, '\'');
1352 if (lex->flags.preprocessing)
1353 lex_tokench(lex, ch);
1356 lex->tok.ttype = TOKEN_CHARCONST;
1357 /* It's a vector if we can successfully scan 3 floats */
1359 if (sscanf_s(lex->tok.value, " %f %f %f ",
1360 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1362 if (sscanf(lex->tok.value, " %f %f %f ",
1363 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1367 lex->tok.ttype = TOKEN_VECTORCONST;
1371 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1372 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER, "multibyte character: `%s`", lex->tok.value))
1373 return (lex->tok.ttype = TOKEN_ERROR);
1375 lex->tok.constval.i = lex->tok.value[0];
1378 return lex->tok.ttype;
1383 lex->tok.ttype = lex_finish_digit(lex, ch);
1385 return lex->tok.ttype;
1388 if (lex->flags.preprocessing) {
1389 lex_tokench(lex, ch);
1391 return (lex->tok.ttype = ch);
1394 lexerror(lex, "unknown token");
1395 return (lex->tok.ttype = TOKEN_ERROR);