2 * Copyright (C) 2012, 2013
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]);
61 static char* *lex_filenames;
63 static void lexerror(lex_file *lex, const char *fmt, ...)
69 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
71 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
75 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
82 ctx.line = lex->sline;
85 r = vcompile_warning(ctx, warntype, 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);
178 lex->tok.constval.t = 0;
179 lex->tok.ctx.line = lex->sline;
180 lex->tok.ctx.file = lex->name;
181 lex->tok.ctx.column = lex->column;
186 lex_file* lex_open(const char *file)
189 FILE *in = fs_file_open(file, "rb");
192 lexerror(NULL, "open failed: '%s'\n", file);
196 lex = (lex_file*)mem_a(sizeof(*lex));
199 lexerror(NULL, "out of memory\n");
203 memset(lex, 0, sizeof(*lex));
206 lex->name = util_strdup(file);
207 lex->line = 1; /* we start counting at 1 */
212 vec_push(lex_filenames, lex->name);
216 lex_file* lex_open_string(const char *str, size_t len, const char *name)
220 lex = (lex_file*)mem_a(sizeof(*lex));
222 lexerror(NULL, "out of memory\n");
226 memset(lex, 0, sizeof(*lex));
229 lex->open_string = str;
230 lex->open_string_length = len;
231 lex->open_string_pos = 0;
233 lex->name = util_strdup(name ? name : "<string-source>");
234 lex->line = 1; /* we start counting at 1 */
239 vec_push(lex_filenames, lex->name);
244 void lex_cleanup(void)
247 for (i = 0; i < vec_size(lex_filenames); ++i)
248 mem_d(lex_filenames[i]);
249 vec_free(lex_filenames);
252 void lex_close(lex_file *lex)
255 for (i = 0; i < vec_size(lex->frames); ++i)
256 mem_d(lex->frames[i].name);
257 vec_free(lex->frames);
260 vec_free(lex->modelname);
263 fs_file_close(lex->file);
266 token_delete(lex->tok);
268 vec_free(lex->tok.value);
270 /* mem_d(lex->name); collected in lex_filenames */
274 static int lex_fgetc(lex_file *lex)
278 return fs_file_getc(lex->file);
280 if (lex->open_string) {
281 if (lex->open_string_pos >= lex->open_string_length)
284 return lex->open_string[lex->open_string_pos++];
289 /* Get or put-back data
290 * The following to functions do NOT understand what kind of data they
292 * The are merely wrapping get/put in order to count line numbers.
294 static void lex_ungetch(lex_file *lex, int ch);
295 static int lex_try_trigraph(lex_file *lex, int old)
299 if (!lex->push_line && c2 == '\n') {
305 lex_ungetch(lex, c2);
310 if (!lex->push_line && c3 == '\n') {
316 case '=': return '#';
317 case '/': return '\\';
318 case '\'': return '^';
319 case '(': return '[';
320 case ')': return ']';
321 case '!': return '|';
322 case '<': return '{';
323 case '>': return '}';
324 case '-': return '~';
326 lex_ungetch(lex, c3);
327 lex_ungetch(lex, c2);
332 static int lex_try_digraph(lex_file *lex, int ch)
336 /* we just used fgetc() so count lines
337 * need to offset a \n the ungetch would recognize
339 if (!lex->push_line && c2 == '\n')
341 if (ch == '<' && c2 == ':')
343 else if (ch == ':' && c2 == '>')
345 else if (ch == '<' && c2 == '%')
347 else if (ch == '%' && c2 == '>')
349 else if (ch == '%' && c2 == ':')
351 lex_ungetch(lex, c2);
355 static int lex_getch(lex_file *lex)
361 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
363 return lex->peek[lex->peekpos];
367 if (!lex->push_line && ch == '\n')
370 return lex_try_trigraph(lex, ch);
371 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
372 return lex_try_digraph(lex, ch);
376 static void lex_ungetch(lex_file *lex, int ch)
378 lex->peek[lex->peekpos++] = ch;
380 if (!lex->push_line && ch == '\n') {
386 /* classify characters
387 * some additions to the is*() functions of ctype.h
390 /* Idents are alphanumberic, but they start with alpha or _ */
391 static bool isident_start(int ch)
393 return isalpha(ch) || ch == '_';
396 static bool isident(int ch)
398 return isident_start(ch) || isdigit(ch);
401 /* isxdigit_only is used when we already know it's not a digit
402 * and want to see if it's a hex digit anyway.
404 static bool isxdigit_only(int ch)
406 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
409 /* Append a character to the token buffer */
410 static void lex_tokench(lex_file *lex, int ch)
412 vec_push(lex->tok.value, ch);
415 /* Append a trailing null-byte */
416 static void lex_endtoken(lex_file *lex)
418 vec_push(lex->tok.value, 0);
419 vec_shrinkby(lex->tok.value, 1);
422 static bool lex_try_pragma(lex_file *lex)
426 char *command = NULL;
430 if (lex->flags.preprocessing)
437 lex_ungetch(lex, ch);
441 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
442 vec_push(pragma, ch);
445 if (ch != ' ' || strcmp(pragma, "pragma")) {
446 lex_ungetch(lex, ch);
450 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
451 vec_push(command, ch);
452 vec_push(command, 0);
455 lex_ungetch(lex, ch);
459 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
464 lex_ungetch(lex, ch);
468 if (!strcmp(command, "push")) {
469 if (!strcmp(param, "line")) {
471 if (lex->push_line == 1)
477 else if (!strcmp(command, "pop")) {
478 if (!strcmp(param, "line")) {
481 if (lex->push_line == 0)
487 else if (!strcmp(command, "file")) {
488 lex->name = util_strdup(param);
489 vec_push(lex_filenames, lex->name);
491 else if (!strcmp(command, "line")) {
492 line = strtol(param, NULL, 0)-1;
498 while (ch != '\n' && ch != EOF)
508 while (vec_size(command)) {
509 lex_ungetch(lex, (unsigned char)vec_last(command));
513 lex_ungetch(lex, ' ');
517 while (vec_size(param)) {
518 lex_ungetch(lex, (unsigned char)vec_last(param));
522 lex_ungetch(lex, ' ');
526 while (vec_size(pragma)) {
527 lex_ungetch(lex, (unsigned char)vec_last(pragma));
532 lex_ungetch(lex, '#');
538 /* Skip whitespace and comments and return the first
539 * non-white character.
540 * As this makes use of the above getch() ungetch() functions,
541 * we don't need to care at all about line numbering anymore.
543 * In theory, this function should only be used at the beginning
544 * of lexing, or when we *know* the next character is part of the token.
545 * Otherwise, if the parser throws an error, the linenumber may not be
546 * the line of the error, but the line of the next token AFTER the error.
548 * This is currently only problematic when using c-like string-continuation,
549 * since comments and whitespaces are allowed between 2 such strings.
553 "A continuation of the previous string"
554 // This line is skipped
557 * In this case, if the parse decides it didn't actually want a string,
558 * and uses lex->line to print an error, it will show the ', foo);' line's
561 * On the other hand, the parser is supposed to remember the line of the next
562 * token's beginning. In this case we would want skipwhite() to be called
563 * AFTER reading a token, so that the parser, before reading the NEXT token,
564 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
567 * here is to store the line of the first character after skipping
568 * the initial whitespace in lex->sline, this happens in lex_do.
570 static int lex_skipwhite(lex_file *lex, bool hadwhite)
573 bool haswhite = hadwhite;
578 while (ch != EOF && isspace(ch)) {
580 if (lex_try_pragma(lex))
583 if (lex->flags.preprocessing) {
586 /* see if there was whitespace first */
587 if (haswhite) { /* (vec_size(lex->tok.value)) { */
588 lex_ungetch(lex, ch);
592 /* otherwise return EOL */
596 lex_tokench(lex, ch);
605 /* one line comment */
608 if (lex->flags.preprocessing) {
611 lex_tokench(lex, '/');
612 lex_tokench(lex, '/');
614 lex_tokench(lex, ' ');
615 lex_tokench(lex, ' ');
618 while (ch != EOF && ch != '\n') {
619 if (lex->flags.preprocessing)
620 lex_tokench(lex, ' '); /* ch); */
623 if (lex->flags.preprocessing) {
624 lex_ungetch(lex, '\n');
632 /* multiline comment */
633 if (lex->flags.preprocessing) {
636 lex_tokench(lex, '/');
637 lex_tokench(lex, '*');
639 lex_tokench(lex, ' ');
640 lex_tokench(lex, ' ');
649 if (lex->flags.preprocessing) {
651 lex_tokench(lex, '*');
652 lex_tokench(lex, '/');
654 lex_tokench(lex, ' ');
655 lex_tokench(lex, ' ');
659 lex_ungetch(lex, ch);
661 if (lex->flags.preprocessing) {
663 lex_tokench(lex, '\n');
665 lex_tokench(lex, ' '); /* ch); */
668 ch = ' '; /* cause TRUE in the isspace check */
671 /* Otherwise roll back to the slash and break out of the loop */
672 lex_ungetch(lex, ch);
676 } while (ch != EOF && isspace(ch));
680 lex_ungetch(lex, ch);
687 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
692 while (ch != EOF && isident(ch))
694 lex_tokench(lex, ch);
698 /* last ch was not an ident ch: */
699 lex_ungetch(lex, ch);
704 /* read one ident for the frame list */
705 static int lex_parse_frame(lex_file *lex)
712 while (ch != EOF && ch != '\n' && isspace(ch))
718 if (!isident_start(ch)) {
719 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
723 lex_tokench(lex, ch);
724 if (!lex_finish_ident(lex))
730 /* read a list of $frames */
731 static bool lex_finish_frames(lex_file *lex)
738 rc = lex_parse_frame(lex);
739 if (rc > 0) /* end of line */
741 if (rc < 0) /* error */
744 for (i = 0; i < vec_size(lex->frames); ++i) {
745 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
746 lex->frames[i].value = lex->framevalue++;
747 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
752 if (i < vec_size(lex->frames))
755 m.value = lex->framevalue++;
756 m.name = util_strdup(lex->tok.value);
757 vec_shrinkto(lex->tok.value, 0);
758 vec_push(lex->frames, m);
764 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
770 char u8buf[8]; /* way more than enough */
777 return TOKEN_STRINGCONST;
779 if (lex->flags.preprocessing && ch == '\\') {
780 lex_tokench(lex, ch);
783 lexerror(lex, "unexpected end of file");
784 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
785 return (lex->tok.ttype = TOKEN_ERROR);
787 lex_tokench(lex, ch);
789 else if (ch == '\\') {
792 lexerror(lex, "unexpected end of file");
793 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
794 return (lex->tok.ttype = TOKEN_ERROR);
801 case 'a': ch = '\a'; break;
802 case 'b': ch = '\b'; break;
803 case 'r': ch = '\r'; break;
804 case 'n': ch = '\n'; break;
805 case 't': ch = '\t'; break;
806 case 'f': ch = '\f'; break;
807 case 'v': ch = '\v'; break;
810 /* same procedure as in fteqcc */
812 nextch = lex_getch(lex);
813 if (nextch >= '0' && nextch <= '9')
815 else if (nextch >= 'a' && nextch <= 'f')
816 ch += nextch - 'a' + 10;
817 else if (nextch >= 'A' && nextch <= 'F')
818 ch += nextch - 'A' + 10;
820 lexerror(lex, "bad character code");
821 lex_ungetch(lex, nextch);
822 return (lex->tok.ttype = TOKEN_ERROR);
826 nextch = lex_getch(lex);
827 if (nextch >= '0' && nextch <= '9')
829 else if (nextch >= 'a' && nextch <= 'f')
830 ch += nextch - 'a' + 10;
831 else if (nextch >= 'A' && nextch <= 'F')
832 ch += nextch - 'A' + 10;
834 lexerror(lex, "bad character code");
835 lex_ungetch(lex, nextch);
836 return (lex->tok.ttype = TOKEN_ERROR);
841 case '0': case '1': case '2': case '3':
842 case '4': case '5': case '6': case '7':
846 case '<': ch = 29; break;
847 case '-': ch = 30; break;
848 case '>': ch = 31; break;
849 case '[': ch = 16; break;
850 case ']': ch = 17; break;
853 nextch = lex_getch(lex);
854 hex = (nextch == 'x');
856 lex_ungetch(lex, nextch);
857 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
859 if (nextch >= '0' && nextch <= '9')
860 chr = chr * 10 + nextch - '0';
862 lexerror(lex, "bad character code");
863 return (lex->tok.ttype = TOKEN_ERROR);
866 if (nextch >= '0' && nextch <= '9')
867 chr = chr * 0x10 + nextch - '0';
868 else if (nextch >= 'a' && nextch <= 'f')
869 chr = chr * 0x10 + nextch - 'a' + 10;
870 else if (nextch >= 'A' && nextch <= 'F')
871 chr = chr * 0x10 + nextch - 'A' + 10;
873 lexerror(lex, "bad character code");
874 return (lex->tok.ttype = TOKEN_ERROR);
877 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
879 lexerror(lex, "character code out of range");
880 return (lex->tok.ttype = TOKEN_ERROR);
883 if (OPTS_FLAG(UTF8) && chr >= 128) {
884 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
889 lex->column += u8len;
890 for (uc = 0; uc < u8len; ++uc)
891 lex_tokench(lex, u8buf[uc]);
892 /* the last character will be inserted with the tokench() call
901 case '\n': ch = '\n'; break;
904 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
905 /* so we just add the character plus backslash no matter what it actually is */
906 lex_tokench(lex, '\\');
908 /* add the character finally */
909 lex_tokench(lex, ch);
912 lex_tokench(lex, ch);
914 lexerror(lex, "unexpected end of file within string constant");
915 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
916 return (lex->tok.ttype = TOKEN_ERROR);
919 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
925 /* parse a number... */
927 lex->tok.ttype = TOKEN_FLOATCONST;
929 lex->tok.ttype = TOKEN_INTCONST;
931 lex_tokench(lex, ch);
934 if (ch != '.' && !isdigit(ch))
936 if (lastch != '0' || ch != 'x')
938 /* end of the number or EOF */
939 lex_ungetch(lex, ch);
942 lex->tok.constval.i = lastch - '0';
943 return lex->tok.ttype;
949 /* EOF would have been caught above */
953 lex_tokench(lex, ch);
955 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
957 lex_tokench(lex, ch);
961 /* NOT else, '.' can come from above as well */
962 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
964 /* Allow floating comma in non-hex mode */
965 lex->tok.ttype = TOKEN_FLOATCONST;
966 lex_tokench(lex, ch);
968 /* continue digits-only */
972 lex_tokench(lex, ch);
976 /* put back the last character */
977 /* but do not put back the trailing 'f' or a float */
978 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
981 /* generally we don't want words to follow numbers: */
983 lexerror(lex, "unexpected trailing characters after number");
984 return (lex->tok.ttype = TOKEN_ERROR);
986 lex_ungetch(lex, ch);
989 if (lex->tok.ttype == TOKEN_FLOATCONST)
990 lex->tok.constval.f = strtod(lex->tok.value, NULL);
992 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
993 return lex->tok.ttype;
996 int lex_do(lex_file *lex)
998 int ch, nextch, thirdch;
999 bool hadwhite = false;
1008 ch = lex_skipwhite(lex, hadwhite);
1010 if (!lex->flags.mergelines || ch != '\\')
1012 ch = lex_getch(lex);
1014 ch = lex_getch(lex);
1016 lex_ungetch(lex, ch);
1020 /* we reached a linemerge */
1021 lex_tokench(lex, '\n');
1025 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1026 return (lex->tok.ttype = ch);
1029 lex->sline = lex->line;
1030 lex->tok.ctx.line = lex->sline;
1031 lex->tok.ctx.file = lex->name;
1034 return (lex->tok.ttype = TOKEN_FATAL);
1038 return (lex->tok.ttype = TOKEN_EOF);
1041 /* modelgen / spiritgen commands */
1042 if (ch == '$' && !lex->flags.preprocessing) {
1046 ch = lex_getch(lex);
1047 if (!isident_start(ch)) {
1048 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1051 lex_tokench(lex, ch);
1052 if (!lex_finish_ident(lex))
1053 return (lex->tok.ttype = TOKEN_ERROR);
1055 /* skip the known commands */
1058 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1060 /* frame/framesave command works like an enum
1061 * similar to fteqcc we handle this in the lexer.
1062 * The reason for this is that it is sensitive to newlines,
1063 * which the parser is unaware of
1065 if (!lex_finish_frames(lex))
1066 return (lex->tok.ttype = TOKEN_ERROR);
1070 if (!strcmp(v, "framevalue"))
1072 ch = lex_getch(lex);
1073 while (ch != EOF && isspace(ch) && ch != '\n')
1074 ch = lex_getch(lex);
1077 lexerror(lex, "$framevalue requires an integer parameter");
1082 lex->tok.ttype = lex_finish_digit(lex, ch);
1084 if (lex->tok.ttype != TOKEN_INTCONST) {
1085 lexerror(lex, "$framevalue requires an integer parameter");
1088 lex->framevalue = lex->tok.constval.i;
1092 if (!strcmp(v, "framerestore"))
1098 rc = lex_parse_frame(lex);
1101 lexerror(lex, "$framerestore requires a framename parameter");
1105 return (lex->tok.ttype = TOKEN_FATAL);
1108 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1109 if (!strcmp(v, lex->frames[frame].name)) {
1110 lex->framevalue = lex->frames[frame].value;
1114 lexerror(lex, "unknown framename `%s`", v);
1118 if (!strcmp(v, "modelname"))
1124 rc = lex_parse_frame(lex);
1127 lexerror(lex, "$modelname requires a parameter");
1131 return (lex->tok.ttype = TOKEN_FATAL);
1133 if (lex->modelname) {
1135 m.value = lex->framevalue;
1136 m.name = lex->modelname;
1137 lex->modelname = NULL;
1138 vec_push(lex->frames, m);
1140 lex->modelname = lex->tok.value;
1141 lex->tok.value = NULL;
1145 if (!strcmp(v, "flush"))
1148 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1149 mem_d(lex->frames[fi].name);
1150 vec_free(lex->frames);
1151 /* skip line (fteqcc does it too) */
1152 ch = lex_getch(lex);
1153 while (ch != EOF && ch != '\n')
1154 ch = lex_getch(lex);
1158 if (!strcmp(v, "cd") ||
1159 !strcmp(v, "origin") ||
1160 !strcmp(v, "base") ||
1161 !strcmp(v, "flags") ||
1162 !strcmp(v, "scale") ||
1166 ch = lex_getch(lex);
1167 while (ch != EOF && ch != '\n')
1168 ch = lex_getch(lex);
1172 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1173 if (!strcmp(v, lex->frames[frame].name)) {
1174 lex->tok.constval.i = lex->frames[frame].value;
1175 return (lex->tok.ttype = TOKEN_INTCONST);
1179 lexerror(lex, "invalid frame macro");
1183 /* single-character tokens */
1187 nextch = lex_getch(lex);
1188 if (nextch == '[') {
1189 lex_tokench(lex, ch);
1190 lex_tokench(lex, nextch);
1192 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1194 lex_ungetch(lex, nextch);
1199 lex_tokench(lex, ch);
1201 if (lex->flags.noops)
1202 return (lex->tok.ttype = ch);
1204 return (lex->tok.ttype = TOKEN_OPERATOR);
1207 if (lex->flags.noops) {
1208 nextch = lex_getch(lex);
1209 if (nextch == ']') {
1210 lex_tokench(lex, ch);
1211 lex_tokench(lex, nextch);
1213 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1215 lex_ungetch(lex, nextch);
1224 lex_tokench(lex, ch);
1226 return (lex->tok.ttype = ch);
1232 nextch = lex_getch(lex);
1233 /* digits starting with a dot */
1234 if (isdigit(nextch)) {
1235 lex_ungetch(lex, nextch);
1236 lex->tok.ttype = lex_finish_digit(lex, ch);
1238 return lex->tok.ttype;
1240 lex_ungetch(lex, nextch);
1243 if (lex->flags.noops)
1245 /* Detect characters early which are normally
1246 * operators OR PART of an operator.
1265 lex_tokench(lex, ch);
1267 return (lex->tok.ttype = ch);
1275 lex_tokench(lex, ch);
1276 /* peak ahead once */
1277 nextch = lex_getch(lex);
1278 if (nextch != '.') {
1279 lex_ungetch(lex, nextch);
1281 if (lex->flags.noops)
1282 return (lex->tok.ttype = ch);
1284 return (lex->tok.ttype = TOKEN_OPERATOR);
1286 /* peak ahead again */
1287 nextch = lex_getch(lex);
1288 if (nextch != '.') {
1289 lex_ungetch(lex, nextch);
1290 lex_ungetch(lex, '.');
1292 if (lex->flags.noops)
1293 return (lex->tok.ttype = ch);
1295 return (lex->tok.ttype = TOKEN_OPERATOR);
1297 /* fill the token to be "..." */
1298 lex_tokench(lex, ch);
1299 lex_tokench(lex, ch);
1301 return (lex->tok.ttype = TOKEN_DOTS);
1304 if (ch == ',' || ch == '.') {
1305 lex_tokench(lex, ch);
1307 return (lex->tok.ttype = TOKEN_OPERATOR);
1310 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1311 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1312 ch == '=' || ch == '!' || /* <=>, ==, != */
1313 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1314 ch == '~' /* ~=, ~ */
1316 lex_tokench(lex, ch);
1318 nextch = lex_getch(lex);
1319 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1320 lex_tokench(lex, nextch);
1321 } else if (ch == '<' && nextch == '=') {
1322 lex_tokench(lex, nextch);
1323 if ((thirdch = lex_getch(lex)) == '>')
1324 lex_tokench(lex, thirdch);
1326 lex_ungetch(lex, thirdch);
1328 } else if (ch == '-' && nextch == '>') {
1329 lex_tokench(lex, nextch);
1330 } else if (ch == '&' && nextch == '~') {
1331 thirdch = lex_getch(lex);
1332 if (thirdch != '=') {
1333 lex_ungetch(lex, thirdch);
1334 lex_ungetch(lex, nextch);
1337 lex_tokench(lex, nextch);
1338 lex_tokench(lex, thirdch);
1341 else if (lex->flags.preprocessing &&
1342 ch == '-' && isdigit(nextch))
1344 lex->tok.ttype = lex_finish_digit(lex, nextch);
1345 if (lex->tok.ttype == TOKEN_INTCONST)
1346 lex->tok.constval.i = -lex->tok.constval.i;
1348 lex->tok.constval.f = -lex->tok.constval.f;
1350 return lex->tok.ttype;
1352 lex_ungetch(lex, nextch);
1356 return (lex->tok.ttype = TOKEN_OPERATOR);
1360 if (ch == '^' || ch == '~' || ch == '!')
1362 lex_tokench(lex, ch);
1364 return (lex->tok.ttype = TOKEN_OPERATOR);
1368 if (ch == '*' || ch == '/') /* *=, /= */
1370 lex_tokench(lex, ch);
1372 nextch = lex_getch(lex);
1373 if (nextch == '=' || nextch == '*') {
1374 lex_tokench(lex, nextch);
1376 lex_ungetch(lex, nextch);
1379 return (lex->tok.ttype = TOKEN_OPERATOR);
1383 lex_tokench(lex, ch);
1385 return (lex->tok.ttype = TOKEN_OPERATOR);
1388 if (isident_start(ch))
1392 lex_tokench(lex, ch);
1393 if (!lex_finish_ident(lex)) {
1395 return (lex->tok.ttype = TOKEN_ERROR);
1398 lex->tok.ttype = TOKEN_IDENT;
1401 if (!strcmp(v, "void")) {
1402 lex->tok.ttype = TOKEN_TYPENAME;
1403 lex->tok.constval.t = TYPE_VOID;
1404 } else if (!strcmp(v, "int")) {
1405 lex->tok.ttype = TOKEN_TYPENAME;
1406 lex->tok.constval.t = TYPE_INTEGER;
1407 } else if (!strcmp(v, "float")) {
1408 lex->tok.ttype = TOKEN_TYPENAME;
1409 lex->tok.constval.t = TYPE_FLOAT;
1410 } else if (!strcmp(v, "string")) {
1411 lex->tok.ttype = TOKEN_TYPENAME;
1412 lex->tok.constval.t = TYPE_STRING;
1413 } else if (!strcmp(v, "entity")) {
1414 lex->tok.ttype = TOKEN_TYPENAME;
1415 lex->tok.constval.t = TYPE_ENTITY;
1416 } else if (!strcmp(v, "vector")) {
1417 lex->tok.ttype = TOKEN_TYPENAME;
1418 lex->tok.constval.t = TYPE_VECTOR;
1421 for (kw = 0; kw < num_keywords_qc; ++kw) {
1422 if (!strcmp(v, keywords_qc[kw]))
1423 return (lex->tok.ttype = TOKEN_KEYWORD);
1425 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1426 for (kw = 0; kw < num_keywords_fg; ++kw) {
1427 if (!strcmp(v, keywords_fg[kw]))
1428 return (lex->tok.ttype = TOKEN_KEYWORD);
1433 return lex->tok.ttype;
1438 lex->flags.nodigraphs = true;
1439 if (lex->flags.preprocessing)
1440 lex_tokench(lex, ch);
1441 lex->tok.ttype = lex_finish_string(lex, '"');
1442 if (lex->flags.preprocessing)
1443 lex_tokench(lex, ch);
1444 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1446 /* Allow c style "string" "continuation" */
1447 ch = lex_skipwhite(lex, false);
1449 lex_ungetch(lex, ch);
1453 lex->tok.ttype = lex_finish_string(lex, '"');
1455 lex->flags.nodigraphs = false;
1457 return lex->tok.ttype;
1462 /* we parse character constants like string,
1463 * but return TOKEN_CHARCONST, or a vector type if it fits...
1464 * Likewise actual unescaping has to be done by the parser.
1465 * The difference is we don't allow 'char' 'continuation'.
1467 if (lex->flags.preprocessing)
1468 lex_tokench(lex, ch);
1469 lex->tok.ttype = lex_finish_string(lex, '\'');
1470 if (lex->flags.preprocessing)
1471 lex_tokench(lex, ch);
1474 lex->tok.ttype = TOKEN_CHARCONST;
1475 /* It's a vector if we can successfully scan 3 floats */
1477 if (sscanf_s(lex->tok.value, " %f %f %f ",
1478 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1480 if (sscanf(lex->tok.value, " %f %f %f ",
1481 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1485 lex->tok.ttype = TOKEN_VECTORCONST;
1489 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1491 /* check for a valid utf8 character */
1492 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1493 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1494 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1495 : "multibyte character: `%s`" ),
1497 return (lex->tok.ttype = TOKEN_ERROR);
1500 lex->tok.constval.i = u8char;
1503 lex->tok.constval.i = lex->tok.value[0];
1506 return lex->tok.ttype;
1511 lex->tok.ttype = lex_finish_digit(lex, ch);
1513 return lex->tok.ttype;
1516 if (lex->flags.preprocessing) {
1517 lex_tokench(lex, ch);
1519 return (lex->tok.ttype = ch);
1522 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1523 return (lex->tok.ttype = TOKEN_ERROR);