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->ctx.file, lex->sline, lex->ctx.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, ...)
78 r = vcompile_warning(lex->ctx, warntype, fmt, ap);
87 token *tok = (token*)mem_a(sizeof(token));
90 memset(tok, 0, sizeof(*tok));
94 void token_delete(token *self)
96 if (self->next && self->next->prev == self)
97 self->next->prev = self->prev;
98 if (self->prev && self->prev->next == self)
99 self->prev->next = self->next;
100 MEM_VECTOR_CLEAR(self, value);
104 token* token_copy(const token *cp)
106 token* self = token_new();
110 self->value_alloc = cp->value_count + 1;
111 self->value_count = cp->value_count;
112 self->value = (char*)mem_a(self->value_alloc);
117 memcpy(self->value, cp->value, cp->value_count);
118 self->value[self->value_alloc-1] = 0;
122 self->ttype = cp->ttype;
123 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
127 void token_delete_all(token *t)
138 token* token_copy_all(const token *cp)
143 out = cur = token_copy(cp);
149 cur->next = token_copy(cp);
151 token_delete_all(out);
154 cur->next->prev = cur;
161 static void lex_token_new(lex_file *lex)
165 token_delete(lex->tok);
166 lex->tok = token_new();
169 vec_shrinkto(lex->tok.value, 0);
171 lex->tok.constval.t = 0;
172 lex->tok.ctx = &lex->ctx; /* this is 'shallow' */
177 lex_file* lex_open(const char *file)
180 FILE *in = fs_file_open(file, "rb");
183 lexerror(NULL, "open failed: '%s'\n", file);
187 lex = (lex_file*)mem_a(sizeof(*lex));
190 lexerror(NULL, "out of memory\n");
194 memset(lex, 0, sizeof(*lex));
197 lex->ctx.file = util_strdup(file);
198 lex->ctx.line = 1; /* we start counting at 1 */
203 vec_push(lex_filenames, (char *)lex->ctx.file);
207 lex_file* lex_open_string(const char *str, size_t len, const char *name)
211 lex = (lex_file*)mem_a(sizeof(*lex));
213 lexerror(NULL, "out of memory\n");
217 memset(lex, 0, sizeof(*lex));
220 lex->open_string = str;
221 lex->open_string_length = len;
222 lex->open_string_pos = 0;
224 lex->ctx.file = util_strdup(name ? name : "<string-source>");
225 lex->ctx.line = 1; /* we start counting at 1 */
230 vec_push(lex_filenames, (char*)lex->ctx.file);
235 void lex_cleanup(void)
238 for (i = 0; i < vec_size(lex_filenames); ++i)
239 mem_d(lex_filenames[i]);
240 vec_free(lex_filenames);
243 void lex_close(lex_file *lex)
246 for (i = 0; i < vec_size(lex->frames); ++i)
247 mem_d(lex->frames[i].name);
248 vec_free(lex->frames);
251 vec_free(lex->modelname);
254 fs_file_close(lex->file);
257 token_delete(lex->tok);
259 vec_free(lex->tok.value);
261 /* mem_d(lex->name); collected in lex_filenames */
265 static int lex_fgetc(lex_file *lex)
269 return fs_file_getc(lex->file);
271 if (lex->open_string) {
272 if (lex->open_string_pos >= lex->open_string_length)
275 return lex->open_string[lex->open_string_pos++];
280 /* Get or put-back data
281 * The following to functions do NOT understand what kind of data they
283 * The are merely wrapping get/put in order to count line numbers.
285 static void lex_ungetch(lex_file *lex, int ch);
286 static int lex_try_trigraph(lex_file *lex, int old)
290 if (!lex->push_line && c2 == '\n') {
296 lex_ungetch(lex, c2);
301 if (!lex->push_line && c3 == '\n') {
307 case '=': return '#';
308 case '/': return '\\';
309 case '\'': return '^';
310 case '(': return '[';
311 case ')': return ']';
312 case '!': return '|';
313 case '<': return '{';
314 case '>': return '}';
315 case '-': return '~';
317 lex_ungetch(lex, c3);
318 lex_ungetch(lex, c2);
323 static int lex_try_digraph(lex_file *lex, int ch)
327 /* we just used fgetc() so count lines
328 * need to offset a \n the ungetch would recognize
330 if (!lex->push_line && c2 == '\n')
332 if (ch == '<' && c2 == ':')
334 else if (ch == ':' && c2 == '>')
336 else if (ch == '<' && c2 == '%')
338 else if (ch == '%' && c2 == '>')
340 else if (ch == '%' && c2 == ':')
342 lex_ungetch(lex, c2);
346 static int lex_getch(lex_file *lex)
352 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
354 return lex->peek[lex->peekpos];
358 if (!lex->push_line && ch == '\n')
361 return lex_try_trigraph(lex, ch);
362 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
363 return lex_try_digraph(lex, ch);
367 static void lex_ungetch(lex_file *lex, int ch)
369 lex->peek[lex->peekpos++] = ch;
371 if (!lex->push_line && ch == '\n') {
377 /* classify characters
378 * some additions to the is*() functions of ctype.h
381 /* Idents are alphanumberic, but they start with alpha or _ */
382 static bool isident_start(int ch)
384 return util_isalpha(ch) || ch == '_';
387 static bool isident(int ch)
389 return isident_start(ch) || util_isdigit(ch);
392 /* isxdigit_only is used when we already know it's not a digit
393 * and want to see if it's a hex digit anyway.
395 static bool isxdigit_only(int ch)
397 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
400 /* Append a character to the token buffer */
401 static void lex_tokench(lex_file *lex, int ch)
403 vec_push(lex->tok.value, ch);
406 /* Append a trailing null-byte */
407 static void lex_endtoken(lex_file *lex)
409 vec_push(lex->tok.value, 0);
410 vec_shrinkby(lex->tok.value, 1);
413 static bool lex_try_pragma(lex_file *lex)
417 char *command = NULL;
421 if (lex->flags.preprocessing)
424 line = lex->ctx.line;
428 lex_ungetch(lex, ch);
432 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
433 vec_push(pragma, ch);
436 if (ch != ' ' || strcmp(pragma, "pragma")) {
437 lex_ungetch(lex, ch);
441 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
442 vec_push(command, ch);
443 vec_push(command, 0);
446 lex_ungetch(lex, ch);
450 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
455 lex_ungetch(lex, ch);
459 if (!strcmp(command, "push")) {
460 if (!strcmp(param, "line")) {
462 if (lex->push_line == 1)
468 else if (!strcmp(command, "pop")) {
469 if (!strcmp(param, "line")) {
472 if (lex->push_line == 0)
478 else if (!strcmp(command, "file")) {
479 lex->ctx.file = util_strdup(param);
480 vec_push(lex_filenames, (char*)lex->ctx.file);
482 else if (!strcmp(command, "line")) {
483 line = strtol(param, NULL, 0)-1;
488 lex->ctx.line = line;
489 while (ch != '\n' && ch != EOF)
499 while (vec_size(command)) {
500 lex_ungetch(lex, (unsigned char)vec_last(command));
504 lex_ungetch(lex, ' ');
508 while (vec_size(param)) {
509 lex_ungetch(lex, (unsigned char)vec_last(param));
513 lex_ungetch(lex, ' ');
517 while (vec_size(pragma)) {
518 lex_ungetch(lex, (unsigned char)vec_last(pragma));
523 lex_ungetch(lex, '#');
525 lex->ctx.line = line;
529 /* Skip whitespace and comments and return the first
530 * non-white character.
531 * As this makes use of the above getch() ungetch() functions,
532 * we don't need to care at all about line numbering anymore.
534 * In theory, this function should only be used at the beginning
535 * of lexing, or when we *know* the next character is part of the token.
536 * Otherwise, if the parser throws an error, the linenumber may not be
537 * the line of the error, but the line of the next token AFTER the error.
539 * This is currently only problematic when using c-like string-continuation,
540 * since comments and whitespaces are allowed between 2 such strings.
544 "A continuation of the previous string"
545 // This line is skipped
548 * In this case, if the parse decides it didn't actually want a string,
549 * and uses lex->line to print an error, it will show the ', foo);' line's
552 * On the other hand, the parser is supposed to remember the line of the next
553 * token's beginning. In this case we would want skipwhite() to be called
554 * AFTER reading a token, so that the parser, before reading the NEXT token,
555 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
558 * here is to store the line of the first character after skipping
559 * the initial whitespace in lex->sline, this happens in lex_do.
561 static int lex_skipwhite(lex_file *lex, bool hadwhite)
564 bool haswhite = hadwhite;
569 while (ch != EOF && util_isspace(ch)) {
571 if (lex_try_pragma(lex))
574 if (lex->flags.preprocessing) {
577 /* see if there was whitespace first */
578 if (haswhite) { /* (vec_size(lex->tok.value)) { */
579 lex_ungetch(lex, ch);
583 /* otherwise return EOL */
587 lex_tokench(lex, ch);
596 /* one line comment */
599 if (lex->flags.preprocessing) {
602 lex_tokench(lex, '/');
603 lex_tokench(lex, '/');
605 lex_tokench(lex, ' ');
606 lex_tokench(lex, ' ');
609 while (ch != EOF && ch != '\n') {
610 if (lex->flags.preprocessing)
611 lex_tokench(lex, ' '); /* ch); */
614 if (lex->flags.preprocessing) {
615 lex_ungetch(lex, '\n');
623 /* multiline comment */
624 if (lex->flags.preprocessing) {
627 lex_tokench(lex, '/');
628 lex_tokench(lex, '*');
630 lex_tokench(lex, ' ');
631 lex_tokench(lex, ' ');
640 if (lex->flags.preprocessing) {
642 lex_tokench(lex, '*');
643 lex_tokench(lex, '/');
645 lex_tokench(lex, ' ');
646 lex_tokench(lex, ' ');
650 lex_ungetch(lex, ch);
652 if (lex->flags.preprocessing) {
654 lex_tokench(lex, '\n');
656 lex_tokench(lex, ' '); /* ch); */
659 ch = ' '; /* cause TRUE in the isspace check */
662 /* Otherwise roll back to the slash and break out of the loop */
663 lex_ungetch(lex, ch);
667 } while (ch != EOF && util_isspace(ch));
671 lex_ungetch(lex, ch);
678 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
683 while (ch != EOF && isident(ch))
685 lex_tokench(lex, ch);
689 /* last ch was not an ident ch: */
690 lex_ungetch(lex, ch);
695 /* read one ident for the frame list */
696 static int lex_parse_frame(lex_file *lex)
703 while (ch != EOF && ch != '\n' && util_isspace(ch))
709 if (!isident_start(ch)) {
710 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
714 lex_tokench(lex, ch);
715 if (!lex_finish_ident(lex))
721 /* read a list of $frames */
722 static bool lex_finish_frames(lex_file *lex)
729 rc = lex_parse_frame(lex);
730 if (rc > 0) /* end of line */
732 if (rc < 0) /* error */
735 for (i = 0; i < vec_size(lex->frames); ++i) {
736 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
737 lex->frames[i].value = lex->framevalue++;
738 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
743 if (i < vec_size(lex->frames))
746 m.value = lex->framevalue++;
747 m.name = util_strdup(lex->tok.value);
748 vec_shrinkto(lex->tok.value, 0);
749 vec_push(lex->frames, m);
755 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
761 char u8buf[8]; /* way more than enough */
768 return TOKEN_STRINGCONST;
770 if (lex->flags.preprocessing && ch == '\\') {
771 lex_tokench(lex, ch);
774 lexerror(lex, "unexpected end of file");
775 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
776 return (lex->tok.ttype = TOKEN_ERROR);
778 lex_tokench(lex, ch);
780 else if (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);
792 case 'a': ch = '\a'; break;
793 case 'b': ch = '\b'; break;
794 case 'r': ch = '\r'; break;
795 case 'n': ch = '\n'; break;
796 case 't': ch = '\t'; break;
797 case 'f': ch = '\f'; break;
798 case 'v': ch = '\v'; break;
801 /* same procedure as in fteqcc */
803 nextch = lex_getch(lex);
804 if (nextch >= '0' && nextch <= '9')
806 else if (nextch >= 'a' && nextch <= 'f')
807 ch += nextch - 'a' + 10;
808 else if (nextch >= 'A' && nextch <= 'F')
809 ch += nextch - 'A' + 10;
811 lexerror(lex, "bad character code");
812 lex_ungetch(lex, nextch);
813 return (lex->tok.ttype = TOKEN_ERROR);
817 nextch = lex_getch(lex);
818 if (nextch >= '0' && nextch <= '9')
820 else if (nextch >= 'a' && nextch <= 'f')
821 ch += nextch - 'a' + 10;
822 else if (nextch >= 'A' && nextch <= 'F')
823 ch += nextch - 'A' + 10;
825 lexerror(lex, "bad character code");
826 lex_ungetch(lex, nextch);
827 return (lex->tok.ttype = TOKEN_ERROR);
832 case '0': case '1': case '2': case '3':
833 case '4': case '5': case '6': case '7':
837 case '<': ch = 29; break;
838 case '-': ch = 30; break;
839 case '>': ch = 31; break;
840 case '[': ch = 16; break;
841 case ']': ch = 17; break;
844 nextch = lex_getch(lex);
845 hex = (nextch == 'x');
847 lex_ungetch(lex, nextch);
848 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
850 if (nextch >= '0' && nextch <= '9')
851 chr = chr * 10 + nextch - '0';
853 lexerror(lex, "bad character code");
854 return (lex->tok.ttype = TOKEN_ERROR);
857 if (nextch >= '0' && nextch <= '9')
858 chr = chr * 0x10 + nextch - '0';
859 else if (nextch >= 'a' && nextch <= 'f')
860 chr = chr * 0x10 + nextch - 'a' + 10;
861 else if (nextch >= 'A' && nextch <= 'F')
862 chr = chr * 0x10 + nextch - 'A' + 10;
864 lexerror(lex, "bad character code");
865 return (lex->tok.ttype = TOKEN_ERROR);
868 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
870 lexerror(lex, "character code out of range");
871 return (lex->tok.ttype = TOKEN_ERROR);
874 if (OPTS_FLAG(UTF8) && chr >= 128) {
875 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
880 lex->ctx.column += u8len;
881 for (uc = 0; uc < u8len; ++uc)
882 lex_tokench(lex, u8buf[uc]);
883 /* the last character will be inserted with the tokench() call
892 case '\n': ch = '\n'; break;
895 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
896 /* so we just add the character plus backslash no matter what it actually is */
897 lex_tokench(lex, '\\');
899 /* add the character finally */
900 lex_tokench(lex, ch);
903 lex_tokench(lex, ch);
905 lexerror(lex, "unexpected end of file within string constant");
906 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
907 return (lex->tok.ttype = TOKEN_ERROR);
910 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
916 /* parse a number... */
918 lex->tok.ttype = TOKEN_FLOATCONST;
920 lex->tok.ttype = TOKEN_INTCONST;
922 lex_tokench(lex, ch);
925 if (ch != '.' && !util_isdigit(ch))
927 if (lastch != '0' || ch != 'x')
929 /* end of the number or EOF */
930 lex_ungetch(lex, ch);
933 lex->tok.constval.i = lastch - '0';
934 return lex->tok.ttype;
940 /* EOF would have been caught above */
944 lex_tokench(lex, ch);
946 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
948 lex_tokench(lex, ch);
952 /* NOT else, '.' can come from above as well */
953 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
955 /* Allow floating comma in non-hex mode */
956 lex->tok.ttype = TOKEN_FLOATCONST;
957 lex_tokench(lex, ch);
959 /* continue digits-only */
961 while (util_isdigit(ch))
963 lex_tokench(lex, ch);
967 /* put back the last character */
968 /* but do not put back the trailing 'f' or a float */
969 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
972 /* generally we don't want words to follow numbers: */
974 lexerror(lex, "unexpected trailing characters after number");
975 return (lex->tok.ttype = TOKEN_ERROR);
977 lex_ungetch(lex, ch);
980 if (lex->tok.ttype == TOKEN_FLOATCONST)
981 lex->tok.constval.f = strtod(lex->tok.value, NULL);
983 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
984 return lex->tok.ttype;
987 int lex_do(lex_file *lex)
989 int ch, nextch, thirdch;
990 bool hadwhite = false;
999 ch = lex_skipwhite(lex, hadwhite);
1001 if (!lex->flags.mergelines || ch != '\\')
1003 ch = lex_getch(lex);
1005 ch = lex_getch(lex);
1007 lex_ungetch(lex, ch);
1011 /* we reached a linemerge */
1012 lex_tokench(lex, '\n');
1016 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1017 return (lex->tok.ttype = ch);
1020 lex->sline = lex->ctx.line;
1021 lex->tok.ctx->line = lex->sline;
1022 lex->tok.ctx->file = lex->ctx.file;
1025 return (lex->tok.ttype = TOKEN_FATAL);
1029 return (lex->tok.ttype = TOKEN_EOF);
1032 /* modelgen / spiritgen commands */
1033 if (ch == '$' && !lex->flags.preprocessing) {
1037 ch = lex_getch(lex);
1038 if (!isident_start(ch)) {
1039 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1042 lex_tokench(lex, ch);
1043 if (!lex_finish_ident(lex))
1044 return (lex->tok.ttype = TOKEN_ERROR);
1046 /* skip the known commands */
1049 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1051 /* frame/framesave command works like an enum
1052 * similar to fteqcc we handle this in the lexer.
1053 * The reason for this is that it is sensitive to newlines,
1054 * which the parser is unaware of
1056 if (!lex_finish_frames(lex))
1057 return (lex->tok.ttype = TOKEN_ERROR);
1061 if (!strcmp(v, "framevalue"))
1063 ch = lex_getch(lex);
1064 while (ch != EOF && util_isspace(ch) && ch != '\n')
1065 ch = lex_getch(lex);
1067 if (!util_isdigit(ch)) {
1068 lexerror(lex, "$framevalue requires an integer parameter");
1073 lex->tok.ttype = lex_finish_digit(lex, ch);
1075 if (lex->tok.ttype != TOKEN_INTCONST) {
1076 lexerror(lex, "$framevalue requires an integer parameter");
1079 lex->framevalue = lex->tok.constval.i;
1083 if (!strcmp(v, "framerestore"))
1089 rc = lex_parse_frame(lex);
1092 lexerror(lex, "$framerestore requires a framename parameter");
1096 return (lex->tok.ttype = TOKEN_FATAL);
1099 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1100 if (!strcmp(v, lex->frames[frame].name)) {
1101 lex->framevalue = lex->frames[frame].value;
1105 lexerror(lex, "unknown framename `%s`", v);
1109 if (!strcmp(v, "modelname"))
1115 rc = lex_parse_frame(lex);
1118 lexerror(lex, "$modelname requires a parameter");
1122 return (lex->tok.ttype = TOKEN_FATAL);
1124 if (lex->modelname) {
1126 m.value = lex->framevalue;
1127 m.name = lex->modelname;
1128 lex->modelname = NULL;
1129 vec_push(lex->frames, m);
1131 lex->modelname = lex->tok.value;
1132 lex->tok.value = NULL;
1136 if (!strcmp(v, "flush"))
1139 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1140 mem_d(lex->frames[fi].name);
1141 vec_free(lex->frames);
1142 /* skip line (fteqcc does it too) */
1143 ch = lex_getch(lex);
1144 while (ch != EOF && ch != '\n')
1145 ch = lex_getch(lex);
1149 if (!strcmp(v, "cd") ||
1150 !strcmp(v, "origin") ||
1151 !strcmp(v, "base") ||
1152 !strcmp(v, "flags") ||
1153 !strcmp(v, "scale") ||
1157 ch = lex_getch(lex);
1158 while (ch != EOF && ch != '\n')
1159 ch = lex_getch(lex);
1163 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1164 if (!strcmp(v, lex->frames[frame].name)) {
1165 lex->tok.constval.i = lex->frames[frame].value;
1166 return (lex->tok.ttype = TOKEN_INTCONST);
1170 lexerror(lex, "invalid frame macro");
1174 /* single-character tokens */
1178 nextch = lex_getch(lex);
1179 if (nextch == '[') {
1180 lex_tokench(lex, ch);
1181 lex_tokench(lex, nextch);
1183 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1185 lex_ungetch(lex, nextch);
1190 lex_tokench(lex, ch);
1192 if (lex->flags.noops)
1193 return (lex->tok.ttype = ch);
1195 return (lex->tok.ttype = TOKEN_OPERATOR);
1198 if (lex->flags.noops) {
1199 nextch = lex_getch(lex);
1200 if (nextch == ']') {
1201 lex_tokench(lex, ch);
1202 lex_tokench(lex, nextch);
1204 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1206 lex_ungetch(lex, nextch);
1215 lex_tokench(lex, ch);
1217 return (lex->tok.ttype = ch);
1223 nextch = lex_getch(lex);
1224 /* digits starting with a dot */
1225 if (util_isdigit(nextch)) {
1226 lex_ungetch(lex, nextch);
1227 lex->tok.ttype = lex_finish_digit(lex, ch);
1229 return lex->tok.ttype;
1231 lex_ungetch(lex, nextch);
1234 if (lex->flags.noops)
1236 /* Detect characters early which are normally
1237 * operators OR PART of an operator.
1256 lex_tokench(lex, ch);
1258 return (lex->tok.ttype = ch);
1266 lex_tokench(lex, ch);
1267 /* peak ahead once */
1268 nextch = lex_getch(lex);
1269 if (nextch != '.') {
1270 lex_ungetch(lex, nextch);
1272 if (lex->flags.noops)
1273 return (lex->tok.ttype = ch);
1275 return (lex->tok.ttype = TOKEN_OPERATOR);
1277 /* peak ahead again */
1278 nextch = lex_getch(lex);
1279 if (nextch != '.') {
1280 lex_ungetch(lex, nextch);
1281 lex_ungetch(lex, '.');
1283 if (lex->flags.noops)
1284 return (lex->tok.ttype = ch);
1286 return (lex->tok.ttype = TOKEN_OPERATOR);
1288 /* fill the token to be "..." */
1289 lex_tokench(lex, ch);
1290 lex_tokench(lex, ch);
1292 return (lex->tok.ttype = TOKEN_DOTS);
1295 if (ch == ',' || ch == '.') {
1296 lex_tokench(lex, ch);
1298 return (lex->tok.ttype = TOKEN_OPERATOR);
1301 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1302 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1303 ch == '=' || ch == '!' || /* <=>, ==, != */
1304 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1305 ch == '~' || ch == '^' /* ~=, ~, ^ */
1307 lex_tokench(lex, ch);
1309 nextch = lex_getch(lex);
1310 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1311 lex_tokench(lex, nextch);
1312 } else if (ch == '<' && nextch == '=') {
1313 lex_tokench(lex, nextch);
1314 if ((thirdch = lex_getch(lex)) == '>')
1315 lex_tokench(lex, thirdch);
1317 lex_ungetch(lex, thirdch);
1319 } else if (ch == '-' && nextch == '>') {
1320 lex_tokench(lex, nextch);
1321 } else if (ch == '&' && nextch == '~') {
1322 thirdch = lex_getch(lex);
1323 if (thirdch != '=') {
1324 lex_ungetch(lex, thirdch);
1325 lex_ungetch(lex, nextch);
1328 lex_tokench(lex, nextch);
1329 lex_tokench(lex, thirdch);
1332 else if (lex->flags.preprocessing &&
1333 ch == '-' && util_isdigit(nextch))
1335 lex->tok.ttype = lex_finish_digit(lex, nextch);
1336 if (lex->tok.ttype == TOKEN_INTCONST)
1337 lex->tok.constval.i = -lex->tok.constval.i;
1339 lex->tok.constval.f = -lex->tok.constval.f;
1341 return lex->tok.ttype;
1343 lex_ungetch(lex, nextch);
1347 return (lex->tok.ttype = TOKEN_OPERATOR);
1351 if (ch == '^' || ch == '~' || ch == '!')
1353 lex_tokench(lex, ch);
1355 return (lex->tok.ttype = TOKEN_OPERATOR);
1359 if (ch == '*' || ch == '/') /* *=, /= */
1361 lex_tokench(lex, ch);
1363 nextch = lex_getch(lex);
1364 if (nextch == '=' || nextch == '*') {
1365 lex_tokench(lex, nextch);
1367 lex_ungetch(lex, nextch);
1370 return (lex->tok.ttype = TOKEN_OPERATOR);
1374 lex_tokench(lex, ch);
1376 return (lex->tok.ttype = TOKEN_OPERATOR);
1379 if (isident_start(ch))
1383 lex_tokench(lex, ch);
1384 if (!lex_finish_ident(lex)) {
1386 return (lex->tok.ttype = TOKEN_ERROR);
1389 lex->tok.ttype = TOKEN_IDENT;
1392 if (!strcmp(v, "void")) {
1393 lex->tok.ttype = TOKEN_TYPENAME;
1394 lex->tok.constval.t = TYPE_VOID;
1395 } else if (!strcmp(v, "int")) {
1396 lex->tok.ttype = TOKEN_TYPENAME;
1397 lex->tok.constval.t = TYPE_INTEGER;
1398 } else if (!strcmp(v, "float")) {
1399 lex->tok.ttype = TOKEN_TYPENAME;
1400 lex->tok.constval.t = TYPE_FLOAT;
1401 } else if (!strcmp(v, "string")) {
1402 lex->tok.ttype = TOKEN_TYPENAME;
1403 lex->tok.constval.t = TYPE_STRING;
1404 } else if (!strcmp(v, "entity")) {
1405 lex->tok.ttype = TOKEN_TYPENAME;
1406 lex->tok.constval.t = TYPE_ENTITY;
1407 } else if (!strcmp(v, "vector")) {
1408 lex->tok.ttype = TOKEN_TYPENAME;
1409 lex->tok.constval.t = TYPE_VECTOR;
1412 for (kw = 0; kw < num_keywords_qc; ++kw) {
1413 if (!strcmp(v, keywords_qc[kw]))
1414 return (lex->tok.ttype = TOKEN_KEYWORD);
1416 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1417 for (kw = 0; kw < num_keywords_fg; ++kw) {
1418 if (!strcmp(v, keywords_fg[kw]))
1419 return (lex->tok.ttype = TOKEN_KEYWORD);
1424 return lex->tok.ttype;
1429 lex->flags.nodigraphs = true;
1430 if (lex->flags.preprocessing)
1431 lex_tokench(lex, ch);
1432 lex->tok.ttype = lex_finish_string(lex, '"');
1433 if (lex->flags.preprocessing)
1434 lex_tokench(lex, ch);
1435 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1437 /* Allow c style "string" "continuation" */
1438 ch = lex_skipwhite(lex, false);
1440 lex_ungetch(lex, ch);
1444 lex->tok.ttype = lex_finish_string(lex, '"');
1446 lex->flags.nodigraphs = false;
1448 return lex->tok.ttype;
1453 /* we parse character constants like string,
1454 * but return TOKEN_CHARCONST, or a vector type if it fits...
1455 * Likewise actual unescaping has to be done by the parser.
1456 * The difference is we don't allow 'char' 'continuation'.
1458 if (lex->flags.preprocessing)
1459 lex_tokench(lex, ch);
1460 lex->tok.ttype = lex_finish_string(lex, '\'');
1461 if (lex->flags.preprocessing)
1462 lex_tokench(lex, ch);
1465 lex->tok.ttype = TOKEN_CHARCONST;
1466 /* It's a vector if we can successfully scan 3 floats */
1468 if (sscanf_s(lex->tok.value, " %f %f %f ",
1469 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1471 if (sscanf(lex->tok.value, " %f %f %f ",
1472 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1476 lex->tok.ttype = TOKEN_VECTORCONST;
1480 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1482 /* check for a valid utf8 character */
1483 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1484 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1485 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1486 : "multibyte character: `%s`" ),
1488 return (lex->tok.ttype = TOKEN_ERROR);
1491 lex->tok.constval.i = u8char;
1494 lex->tok.constval.i = lex->tok.value[0];
1497 return lex->tok.ttype;
1500 if (util_isdigit(ch))
1502 lex->tok.ttype = lex_finish_digit(lex, ch);
1504 return lex->tok.ttype;
1507 if (lex->flags.preprocessing) {
1508 lex_tokench(lex, ch);
1510 return (lex->tok.ttype = ch);
1513 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1514 return (lex->tok.ttype = TOKEN_ERROR);