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
33 static const char *keywords_qc[] = {
40 static size_t num_keywords_qc = sizeof(keywords_qc) / sizeof(keywords_qc[0]);
43 static const char *keywords_fg[] = {
44 "switch", "case", "default",
50 "__builtin_debug_printtype"
52 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
58 static char* *lex_filenames;
60 static void lexerror(lex_file *lex, const char *fmt, ...)
66 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
68 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
72 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
79 ctx.line = lex->sline;
82 r = vcompile_warning(ctx, warntype, fmt, ap);
91 token *tok = (token*)mem_a(sizeof(token));
94 memset(tok, 0, sizeof(*tok));
98 void token_delete(token *self)
100 if (self->next && self->next->prev == self)
101 self->next->prev = self->prev;
102 if (self->prev && self->prev->next == self)
103 self->prev->next = self->next;
104 MEM_VECTOR_CLEAR(self, value);
108 token* token_copy(const token *cp)
110 token* self = token_new();
114 self->value_alloc = cp->value_count + 1;
115 self->value_count = cp->value_count;
116 self->value = (char*)mem_a(self->value_alloc);
121 memcpy(self->value, cp->value, cp->value_count);
122 self->value[self->value_alloc-1] = 0;
126 self->ttype = cp->ttype;
127 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
131 void token_delete_all(token *t)
142 token* token_copy_all(const token *cp)
147 out = cur = token_copy(cp);
153 cur->next = token_copy(cp);
155 token_delete_all(out);
158 cur->next->prev = cur;
165 static void lex_token_new(lex_file *lex)
169 token_delete(lex->tok);
170 lex->tok = token_new();
173 vec_shrinkto(lex->tok.value, 0);
175 lex->tok.constval.t = 0;
176 lex->tok.ctx.line = lex->sline;
177 lex->tok.ctx.file = lex->name;
178 lex->tok.ctx.column = lex->column;
183 lex_file* lex_open(const char *file)
186 FILE *in = fs_file_open(file, "rb");
189 lexerror(NULL, "open failed: '%s'\n", file);
193 lex = (lex_file*)mem_a(sizeof(*lex));
196 lexerror(NULL, "out of memory\n");
200 memset(lex, 0, sizeof(*lex));
203 lex->name = util_strdup(file);
204 lex->line = 1; /* we start counting at 1 */
209 vec_push(lex_filenames, lex->name);
213 lex_file* lex_open_string(const char *str, size_t len, const char *name)
217 lex = (lex_file*)mem_a(sizeof(*lex));
219 lexerror(NULL, "out of memory\n");
223 memset(lex, 0, sizeof(*lex));
226 lex->open_string = str;
227 lex->open_string_length = len;
228 lex->open_string_pos = 0;
230 lex->name = util_strdup(name ? name : "<string-source>");
231 lex->line = 1; /* we start counting at 1 */
236 vec_push(lex_filenames, lex->name);
241 void lex_cleanup(void)
244 for (i = 0; i < vec_size(lex_filenames); ++i)
245 mem_d(lex_filenames[i]);
246 vec_free(lex_filenames);
249 void lex_close(lex_file *lex)
252 for (i = 0; i < vec_size(lex->frames); ++i)
253 mem_d(lex->frames[i].name);
254 vec_free(lex->frames);
257 vec_free(lex->modelname);
260 fs_file_close(lex->file);
263 token_delete(lex->tok);
265 vec_free(lex->tok.value);
267 /* mem_d(lex->name); collected in lex_filenames */
271 static int lex_fgetc(lex_file *lex)
275 return fs_file_getc(lex->file);
277 if (lex->open_string) {
278 if (lex->open_string_pos >= lex->open_string_length)
281 return lex->open_string[lex->open_string_pos++];
286 /* Get or put-back data
287 * The following to functions do NOT understand what kind of data they
289 * The are merely wrapping get/put in order to count line numbers.
291 static void lex_ungetch(lex_file *lex, int ch);
292 static int lex_try_trigraph(lex_file *lex, int old)
296 if (!lex->push_line && c2 == '\n') {
302 lex_ungetch(lex, c2);
307 if (!lex->push_line && c3 == '\n') {
313 case '=': return '#';
314 case '/': return '\\';
315 case '\'': return '^';
316 case '(': return '[';
317 case ')': return ']';
318 case '!': return '|';
319 case '<': return '{';
320 case '>': return '}';
321 case '-': return '~';
323 lex_ungetch(lex, c3);
324 lex_ungetch(lex, c2);
329 static int lex_try_digraph(lex_file *lex, int ch)
333 /* we just used fgetc() so count lines
334 * need to offset a \n the ungetch would recognize
336 if (!lex->push_line && c2 == '\n')
338 if (ch == '<' && c2 == ':')
340 else if (ch == ':' && c2 == '>')
342 else if (ch == '<' && c2 == '%')
344 else if (ch == '%' && c2 == '>')
346 else if (ch == '%' && c2 == ':')
348 lex_ungetch(lex, c2);
352 static int lex_getch(lex_file *lex)
358 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
360 return lex->peek[lex->peekpos];
364 if (!lex->push_line && ch == '\n')
367 return lex_try_trigraph(lex, ch);
368 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
369 return lex_try_digraph(lex, ch);
373 static void lex_ungetch(lex_file *lex, int ch)
375 lex->peek[lex->peekpos++] = ch;
377 if (!lex->push_line && ch == '\n') {
383 /* classify characters
384 * some additions to the is*() functions of ctype.h
387 /* Idents are alphanumberic, but they start with alpha or _ */
388 static bool isident_start(int ch)
390 return util_isalpha(ch) || ch == '_';
393 static bool isident(int ch)
395 return isident_start(ch) || util_isdigit(ch);
398 /* isxdigit_only is used when we already know it's not a digit
399 * and want to see if it's a hex digit anyway.
401 static bool isxdigit_only(int ch)
403 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
406 /* Append a character to the token buffer */
407 static void lex_tokench(lex_file *lex, int ch)
409 vec_push(lex->tok.value, ch);
412 /* Append a trailing null-byte */
413 static void lex_endtoken(lex_file *lex)
415 vec_push(lex->tok.value, 0);
416 vec_shrinkby(lex->tok.value, 1);
419 static bool lex_try_pragma(lex_file *lex)
423 char *command = NULL;
427 if (lex->flags.preprocessing)
434 lex_ungetch(lex, ch);
438 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
439 vec_push(pragma, ch);
442 if (ch != ' ' || strcmp(pragma, "pragma")) {
443 lex_ungetch(lex, ch);
447 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
448 vec_push(command, ch);
449 vec_push(command, 0);
452 lex_ungetch(lex, ch);
456 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
461 lex_ungetch(lex, ch);
465 if (!strcmp(command, "push")) {
466 if (!strcmp(param, "line")) {
468 if (lex->push_line == 1)
474 else if (!strcmp(command, "pop")) {
475 if (!strcmp(param, "line")) {
478 if (lex->push_line == 0)
484 else if (!strcmp(command, "file")) {
485 lex->name = util_strdup(param);
486 vec_push(lex_filenames, lex->name);
488 else if (!strcmp(command, "line")) {
489 line = strtol(param, NULL, 0)-1;
495 while (ch != '\n' && ch != EOF)
505 while (vec_size(command)) {
506 lex_ungetch(lex, (unsigned char)vec_last(command));
510 lex_ungetch(lex, ' ');
514 while (vec_size(param)) {
515 lex_ungetch(lex, (unsigned char)vec_last(param));
519 lex_ungetch(lex, ' ');
523 while (vec_size(pragma)) {
524 lex_ungetch(lex, (unsigned char)vec_last(pragma));
529 lex_ungetch(lex, '#');
535 /* Skip whitespace and comments and return the first
536 * non-white character.
537 * As this makes use of the above getch() ungetch() functions,
538 * we don't need to care at all about line numbering anymore.
540 * In theory, this function should only be used at the beginning
541 * of lexing, or when we *know* the next character is part of the token.
542 * Otherwise, if the parser throws an error, the linenumber may not be
543 * the line of the error, but the line of the next token AFTER the error.
545 * This is currently only problematic when using c-like string-continuation,
546 * since comments and whitespaces are allowed between 2 such strings.
550 "A continuation of the previous string"
551 // This line is skipped
554 * In this case, if the parse decides it didn't actually want a string,
555 * and uses lex->line to print an error, it will show the ', foo);' line's
558 * On the other hand, the parser is supposed to remember the line of the next
559 * token's beginning. In this case we would want skipwhite() to be called
560 * AFTER reading a token, so that the parser, before reading the NEXT token,
561 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
564 * here is to store the line of the first character after skipping
565 * the initial whitespace in lex->sline, this happens in lex_do.
567 static int lex_skipwhite(lex_file *lex, bool hadwhite)
570 bool haswhite = hadwhite;
575 while (ch != EOF && util_isspace(ch)) {
577 if (lex_try_pragma(lex))
580 if (lex->flags.preprocessing) {
583 /* see if there was whitespace first */
584 if (haswhite) { /* (vec_size(lex->tok.value)) { */
585 lex_ungetch(lex, ch);
589 /* otherwise return EOL */
593 lex_tokench(lex, ch);
602 /* one line comment */
605 if (lex->flags.preprocessing) {
608 lex_tokench(lex, '/');
609 lex_tokench(lex, '/');
611 lex_tokench(lex, ' ');
612 lex_tokench(lex, ' ');
615 while (ch != EOF && ch != '\n') {
616 if (lex->flags.preprocessing)
617 lex_tokench(lex, ' '); /* ch); */
620 if (lex->flags.preprocessing) {
621 lex_ungetch(lex, '\n');
629 /* multiline comment */
630 if (lex->flags.preprocessing) {
633 lex_tokench(lex, '/');
634 lex_tokench(lex, '*');
636 lex_tokench(lex, ' ');
637 lex_tokench(lex, ' ');
646 if (lex->flags.preprocessing) {
648 lex_tokench(lex, '*');
649 lex_tokench(lex, '/');
651 lex_tokench(lex, ' ');
652 lex_tokench(lex, ' ');
656 lex_ungetch(lex, ch);
658 if (lex->flags.preprocessing) {
660 lex_tokench(lex, '\n');
662 lex_tokench(lex, ' '); /* ch); */
665 ch = ' '; /* cause TRUE in the isspace check */
668 /* Otherwise roll back to the slash and break out of the loop */
669 lex_ungetch(lex, ch);
673 } while (ch != EOF && util_isspace(ch));
677 lex_ungetch(lex, ch);
684 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
689 while (ch != EOF && isident(ch))
691 lex_tokench(lex, ch);
695 /* last ch was not an ident ch: */
696 lex_ungetch(lex, ch);
701 /* read one ident for the frame list */
702 static int lex_parse_frame(lex_file *lex)
709 while (ch != EOF && ch != '\n' && util_isspace(ch))
715 if (!isident_start(ch)) {
716 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
720 lex_tokench(lex, ch);
721 if (!lex_finish_ident(lex))
727 /* read a list of $frames */
728 static bool lex_finish_frames(lex_file *lex)
735 rc = lex_parse_frame(lex);
736 if (rc > 0) /* end of line */
738 if (rc < 0) /* error */
741 for (i = 0; i < vec_size(lex->frames); ++i) {
742 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
743 lex->frames[i].value = lex->framevalue++;
744 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
749 if (i < vec_size(lex->frames))
752 m.value = lex->framevalue++;
753 m.name = util_strdup(lex->tok.value);
754 vec_shrinkto(lex->tok.value, 0);
755 vec_push(lex->frames, m);
761 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
767 char u8buf[8]; /* way more than enough */
774 return TOKEN_STRINGCONST;
776 if (lex->flags.preprocessing && ch == '\\') {
777 lex_tokench(lex, ch);
780 lexerror(lex, "unexpected end of file");
781 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
782 return (lex->tok.ttype = TOKEN_ERROR);
784 lex_tokench(lex, ch);
786 else if (ch == '\\') {
789 lexerror(lex, "unexpected end of file");
790 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
791 return (lex->tok.ttype = TOKEN_ERROR);
798 case 'a': ch = '\a'; break;
799 case 'b': ch = '\b'; break;
800 case 'r': ch = '\r'; break;
801 case 'n': ch = '\n'; break;
802 case 't': ch = '\t'; break;
803 case 'f': ch = '\f'; break;
804 case 'v': ch = '\v'; break;
807 /* same procedure as in fteqcc */
809 nextch = lex_getch(lex);
810 if (nextch >= '0' && nextch <= '9')
812 else if (nextch >= 'a' && nextch <= 'f')
813 ch += nextch - 'a' + 10;
814 else if (nextch >= 'A' && nextch <= 'F')
815 ch += nextch - 'A' + 10;
817 lexerror(lex, "bad character code");
818 lex_ungetch(lex, nextch);
819 return (lex->tok.ttype = TOKEN_ERROR);
823 nextch = lex_getch(lex);
824 if (nextch >= '0' && nextch <= '9')
826 else if (nextch >= 'a' && nextch <= 'f')
827 ch += nextch - 'a' + 10;
828 else if (nextch >= 'A' && nextch <= 'F')
829 ch += nextch - 'A' + 10;
831 lexerror(lex, "bad character code");
832 lex_ungetch(lex, nextch);
833 return (lex->tok.ttype = TOKEN_ERROR);
838 case '0': case '1': case '2': case '3':
839 case '4': case '5': case '6': case '7':
843 case '<': ch = 29; break;
844 case '-': ch = 30; break;
845 case '>': ch = 31; break;
846 case '[': ch = 16; break;
847 case ']': ch = 17; break;
850 nextch = lex_getch(lex);
851 hex = (nextch == 'x');
853 lex_ungetch(lex, nextch);
854 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
856 if (nextch >= '0' && nextch <= '9')
857 chr = chr * 10 + nextch - '0';
859 lexerror(lex, "bad character code");
860 return (lex->tok.ttype = TOKEN_ERROR);
863 if (nextch >= '0' && nextch <= '9')
864 chr = chr * 0x10 + nextch - '0';
865 else if (nextch >= 'a' && nextch <= 'f')
866 chr = chr * 0x10 + nextch - 'a' + 10;
867 else if (nextch >= 'A' && nextch <= 'F')
868 chr = chr * 0x10 + nextch - 'A' + 10;
870 lexerror(lex, "bad character code");
871 return (lex->tok.ttype = TOKEN_ERROR);
874 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
876 lexerror(lex, "character code out of range");
877 return (lex->tok.ttype = TOKEN_ERROR);
880 if (OPTS_FLAG(UTF8) && chr >= 128) {
881 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
886 lex->column += u8len;
887 for (uc = 0; uc < u8len; ++uc)
888 lex_tokench(lex, u8buf[uc]);
889 /* the last character will be inserted with the tokench() call
898 case '\n': ch = '\n'; break;
901 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
902 /* so we just add the character plus backslash no matter what it actually is */
903 lex_tokench(lex, '\\');
905 /* add the character finally */
906 lex_tokench(lex, ch);
909 lex_tokench(lex, ch);
911 lexerror(lex, "unexpected end of file within string constant");
912 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
913 return (lex->tok.ttype = TOKEN_ERROR);
916 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
922 /* parse a number... */
924 lex->tok.ttype = TOKEN_FLOATCONST;
926 lex->tok.ttype = TOKEN_INTCONST;
928 lex_tokench(lex, ch);
931 if (ch != '.' && !util_isdigit(ch))
933 if (lastch != '0' || ch != 'x')
935 /* end of the number or EOF */
936 lex_ungetch(lex, ch);
939 lex->tok.constval.i = lastch - '0';
940 return lex->tok.ttype;
946 /* EOF would have been caught above */
950 lex_tokench(lex, ch);
952 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
954 lex_tokench(lex, ch);
958 /* NOT else, '.' can come from above as well */
959 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
961 /* Allow floating comma in non-hex mode */
962 lex->tok.ttype = TOKEN_FLOATCONST;
963 lex_tokench(lex, ch);
965 /* continue digits-only */
967 while (util_isdigit(ch))
969 lex_tokench(lex, ch);
973 /* put back the last character */
974 /* but do not put back the trailing 'f' or a float */
975 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
978 /* generally we don't want words to follow numbers: */
980 lexerror(lex, "unexpected trailing characters after number");
981 return (lex->tok.ttype = TOKEN_ERROR);
983 lex_ungetch(lex, ch);
986 if (lex->tok.ttype == TOKEN_FLOATCONST)
987 lex->tok.constval.f = strtod(lex->tok.value, NULL);
989 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
990 return lex->tok.ttype;
993 int lex_do(lex_file *lex)
995 int ch, nextch, thirdch;
996 bool hadwhite = false;
1005 ch = lex_skipwhite(lex, hadwhite);
1007 if (!lex->flags.mergelines || ch != '\\')
1009 ch = lex_getch(lex);
1011 ch = lex_getch(lex);
1013 lex_ungetch(lex, ch);
1017 /* we reached a linemerge */
1018 lex_tokench(lex, '\n');
1022 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1023 return (lex->tok.ttype = ch);
1026 lex->sline = lex->line;
1027 lex->tok.ctx.line = lex->sline;
1028 lex->tok.ctx.file = lex->name;
1031 return (lex->tok.ttype = TOKEN_FATAL);
1035 return (lex->tok.ttype = TOKEN_EOF);
1038 /* modelgen / spiritgen commands */
1039 if (ch == '$' && !lex->flags.preprocessing) {
1043 ch = lex_getch(lex);
1044 if (!isident_start(ch)) {
1045 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1048 lex_tokench(lex, ch);
1049 if (!lex_finish_ident(lex))
1050 return (lex->tok.ttype = TOKEN_ERROR);
1052 /* skip the known commands */
1055 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1057 /* frame/framesave command works like an enum
1058 * similar to fteqcc we handle this in the lexer.
1059 * The reason for this is that it is sensitive to newlines,
1060 * which the parser is unaware of
1062 if (!lex_finish_frames(lex))
1063 return (lex->tok.ttype = TOKEN_ERROR);
1067 if (!strcmp(v, "framevalue"))
1069 ch = lex_getch(lex);
1070 while (ch != EOF && util_isspace(ch) && ch != '\n')
1071 ch = lex_getch(lex);
1073 if (!util_isdigit(ch)) {
1074 lexerror(lex, "$framevalue requires an integer parameter");
1079 lex->tok.ttype = lex_finish_digit(lex, ch);
1081 if (lex->tok.ttype != TOKEN_INTCONST) {
1082 lexerror(lex, "$framevalue requires an integer parameter");
1085 lex->framevalue = lex->tok.constval.i;
1089 if (!strcmp(v, "framerestore"))
1095 rc = lex_parse_frame(lex);
1098 lexerror(lex, "$framerestore requires a framename parameter");
1102 return (lex->tok.ttype = TOKEN_FATAL);
1105 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1106 if (!strcmp(v, lex->frames[frame].name)) {
1107 lex->framevalue = lex->frames[frame].value;
1111 lexerror(lex, "unknown framename `%s`", v);
1115 if (!strcmp(v, "modelname"))
1121 rc = lex_parse_frame(lex);
1124 lexerror(lex, "$modelname requires a parameter");
1128 return (lex->tok.ttype = TOKEN_FATAL);
1130 if (lex->modelname) {
1132 m.value = lex->framevalue;
1133 m.name = lex->modelname;
1134 lex->modelname = NULL;
1135 vec_push(lex->frames, m);
1137 lex->modelname = lex->tok.value;
1138 lex->tok.value = NULL;
1142 if (!strcmp(v, "flush"))
1145 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1146 mem_d(lex->frames[fi].name);
1147 vec_free(lex->frames);
1148 /* skip line (fteqcc does it too) */
1149 ch = lex_getch(lex);
1150 while (ch != EOF && ch != '\n')
1151 ch = lex_getch(lex);
1155 if (!strcmp(v, "cd") ||
1156 !strcmp(v, "origin") ||
1157 !strcmp(v, "base") ||
1158 !strcmp(v, "flags") ||
1159 !strcmp(v, "scale") ||
1163 ch = lex_getch(lex);
1164 while (ch != EOF && ch != '\n')
1165 ch = lex_getch(lex);
1169 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1170 if (!strcmp(v, lex->frames[frame].name)) {
1171 lex->tok.constval.i = lex->frames[frame].value;
1172 return (lex->tok.ttype = TOKEN_INTCONST);
1176 lexerror(lex, "invalid frame macro");
1180 /* single-character tokens */
1184 nextch = lex_getch(lex);
1185 if (nextch == '[') {
1186 lex_tokench(lex, ch);
1187 lex_tokench(lex, nextch);
1189 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1191 lex_ungetch(lex, nextch);
1196 lex_tokench(lex, ch);
1198 if (lex->flags.noops)
1199 return (lex->tok.ttype = ch);
1201 return (lex->tok.ttype = TOKEN_OPERATOR);
1204 if (lex->flags.noops) {
1205 nextch = lex_getch(lex);
1206 if (nextch == ']') {
1207 lex_tokench(lex, ch);
1208 lex_tokench(lex, nextch);
1210 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1212 lex_ungetch(lex, nextch);
1221 lex_tokench(lex, ch);
1223 return (lex->tok.ttype = ch);
1229 nextch = lex_getch(lex);
1230 /* digits starting with a dot */
1231 if (util_isdigit(nextch)) {
1232 lex_ungetch(lex, nextch);
1233 lex->tok.ttype = lex_finish_digit(lex, ch);
1235 return lex->tok.ttype;
1237 lex_ungetch(lex, nextch);
1240 if (lex->flags.noops)
1242 /* Detect characters early which are normally
1243 * operators OR PART of an operator.
1262 lex_tokench(lex, ch);
1264 return (lex->tok.ttype = ch);
1272 lex_tokench(lex, ch);
1273 /* peak ahead once */
1274 nextch = lex_getch(lex);
1275 if (nextch != '.') {
1276 lex_ungetch(lex, nextch);
1278 if (lex->flags.noops)
1279 return (lex->tok.ttype = ch);
1281 return (lex->tok.ttype = TOKEN_OPERATOR);
1283 /* peak ahead again */
1284 nextch = lex_getch(lex);
1285 if (nextch != '.') {
1286 lex_ungetch(lex, nextch);
1287 lex_ungetch(lex, '.');
1289 if (lex->flags.noops)
1290 return (lex->tok.ttype = ch);
1292 return (lex->tok.ttype = TOKEN_OPERATOR);
1294 /* fill the token to be "..." */
1295 lex_tokench(lex, ch);
1296 lex_tokench(lex, ch);
1298 return (lex->tok.ttype = TOKEN_DOTS);
1301 if (ch == ',' || ch == '.') {
1302 lex_tokench(lex, ch);
1304 return (lex->tok.ttype = TOKEN_OPERATOR);
1307 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1308 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1309 ch == '=' || ch == '!' || /* <=>, ==, != */
1310 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1311 ch == '~' || ch == '^' /* ~=, ~, ^ */
1313 lex_tokench(lex, ch);
1315 nextch = lex_getch(lex);
1316 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1317 lex_tokench(lex, nextch);
1318 } else if (ch == '<' && nextch == '=') {
1319 lex_tokench(lex, nextch);
1320 if ((thirdch = lex_getch(lex)) == '>')
1321 lex_tokench(lex, thirdch);
1323 lex_ungetch(lex, thirdch);
1325 } else if (ch == '-' && nextch == '>') {
1326 lex_tokench(lex, nextch);
1327 } else if (ch == '&' && nextch == '~') {
1328 thirdch = lex_getch(lex);
1329 if (thirdch != '=') {
1330 lex_ungetch(lex, thirdch);
1331 lex_ungetch(lex, nextch);
1334 lex_tokench(lex, nextch);
1335 lex_tokench(lex, thirdch);
1338 else if (lex->flags.preprocessing &&
1339 ch == '-' && util_isdigit(nextch))
1341 lex->tok.ttype = lex_finish_digit(lex, nextch);
1342 if (lex->tok.ttype == TOKEN_INTCONST)
1343 lex->tok.constval.i = -lex->tok.constval.i;
1345 lex->tok.constval.f = -lex->tok.constval.f;
1347 return lex->tok.ttype;
1349 lex_ungetch(lex, nextch);
1353 return (lex->tok.ttype = TOKEN_OPERATOR);
1357 if (ch == '^' || ch == '~' || ch == '!')
1359 lex_tokench(lex, ch);
1361 return (lex->tok.ttype = TOKEN_OPERATOR);
1365 if (ch == '*' || ch == '/') /* *=, /= */
1367 lex_tokench(lex, ch);
1369 nextch = lex_getch(lex);
1370 if (nextch == '=' || nextch == '*') {
1371 lex_tokench(lex, nextch);
1373 lex_ungetch(lex, nextch);
1376 return (lex->tok.ttype = TOKEN_OPERATOR);
1380 lex_tokench(lex, ch);
1382 return (lex->tok.ttype = TOKEN_OPERATOR);
1385 if (isident_start(ch))
1389 lex_tokench(lex, ch);
1390 if (!lex_finish_ident(lex)) {
1392 return (lex->tok.ttype = TOKEN_ERROR);
1395 lex->tok.ttype = TOKEN_IDENT;
1398 if (!strcmp(v, "void")) {
1399 lex->tok.ttype = TOKEN_TYPENAME;
1400 lex->tok.constval.t = TYPE_VOID;
1401 } else if (!strcmp(v, "int")) {
1402 lex->tok.ttype = TOKEN_TYPENAME;
1403 lex->tok.constval.t = TYPE_INTEGER;
1404 } else if (!strcmp(v, "float")) {
1405 lex->tok.ttype = TOKEN_TYPENAME;
1406 lex->tok.constval.t = TYPE_FLOAT;
1407 } else if (!strcmp(v, "string")) {
1408 lex->tok.ttype = TOKEN_TYPENAME;
1409 lex->tok.constval.t = TYPE_STRING;
1410 } else if (!strcmp(v, "entity")) {
1411 lex->tok.ttype = TOKEN_TYPENAME;
1412 lex->tok.constval.t = TYPE_ENTITY;
1413 } else if (!strcmp(v, "vector")) {
1414 lex->tok.ttype = TOKEN_TYPENAME;
1415 lex->tok.constval.t = TYPE_VECTOR;
1418 for (kw = 0; kw < num_keywords_qc; ++kw) {
1419 if (!strcmp(v, keywords_qc[kw]))
1420 return (lex->tok.ttype = TOKEN_KEYWORD);
1422 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1423 for (kw = 0; kw < num_keywords_fg; ++kw) {
1424 if (!strcmp(v, keywords_fg[kw]))
1425 return (lex->tok.ttype = TOKEN_KEYWORD);
1430 return lex->tok.ttype;
1435 lex->flags.nodigraphs = true;
1436 if (lex->flags.preprocessing)
1437 lex_tokench(lex, ch);
1438 lex->tok.ttype = lex_finish_string(lex, '"');
1439 if (lex->flags.preprocessing)
1440 lex_tokench(lex, ch);
1441 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1443 /* Allow c style "string" "continuation" */
1444 ch = lex_skipwhite(lex, false);
1446 lex_ungetch(lex, ch);
1450 lex->tok.ttype = lex_finish_string(lex, '"');
1452 lex->flags.nodigraphs = false;
1454 return lex->tok.ttype;
1459 /* we parse character constants like string,
1460 * but return TOKEN_CHARCONST, or a vector type if it fits...
1461 * Likewise actual unescaping has to be done by the parser.
1462 * The difference is we don't allow 'char' 'continuation'.
1464 if (lex->flags.preprocessing)
1465 lex_tokench(lex, ch);
1466 lex->tok.ttype = lex_finish_string(lex, '\'');
1467 if (lex->flags.preprocessing)
1468 lex_tokench(lex, ch);
1471 lex->tok.ttype = TOKEN_CHARCONST;
1472 /* It's a vector if we can successfully scan 3 floats */
1474 if (sscanf_s(lex->tok.value, " %f %f %f ",
1475 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1477 if (sscanf(lex->tok.value, " %f %f %f ",
1478 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1482 lex->tok.ttype = TOKEN_VECTORCONST;
1486 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1488 /* check for a valid utf8 character */
1489 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1490 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1491 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1492 : "multibyte character: `%s`" ),
1494 return (lex->tok.ttype = TOKEN_ERROR);
1497 lex->tok.constval.i = u8char;
1500 lex->tok.constval.i = lex->tok.value[0];
1503 return lex->tok.ttype;
1506 if (util_isdigit(ch))
1508 lex->tok.ttype = lex_finish_digit(lex, ch);
1510 return lex->tok.ttype;
1513 if (lex->flags.preprocessing) {
1514 lex_tokench(lex, ch);
1516 return (lex->tok.ttype = ch);
1519 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1520 return (lex->tok.ttype = TOKEN_ERROR);