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;
80 ctx.column = lex->column;
83 r = vcompile_warning(ctx, warntype, fmt, ap);
92 token *tok = (token*)mem_a(sizeof(token));
95 memset(tok, 0, sizeof(*tok));
99 void token_delete(token *self)
101 if (self->next && self->next->prev == self)
102 self->next->prev = self->prev;
103 if (self->prev && self->prev->next == self)
104 self->prev->next = self->next;
105 MEM_VECTOR_CLEAR(self, value);
109 token* token_copy(const token *cp)
111 token* self = token_new();
115 self->value_alloc = cp->value_count + 1;
116 self->value_count = cp->value_count;
117 self->value = (char*)mem_a(self->value_alloc);
122 memcpy(self->value, cp->value, cp->value_count);
123 self->value[self->value_alloc-1] = 0;
127 self->ttype = cp->ttype;
128 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
132 void token_delete_all(token *t)
143 token* token_copy_all(const token *cp)
148 out = cur = token_copy(cp);
154 cur->next = token_copy(cp);
156 token_delete_all(out);
159 cur->next->prev = cur;
166 static void lex_token_new(lex_file *lex)
170 token_delete(lex->tok);
171 lex->tok = token_new();
174 vec_shrinkto(lex->tok.value, 0);
176 lex->tok.constval.t = 0;
177 lex->tok.ctx.line = lex->sline;
178 lex->tok.ctx.file = lex->name;
179 lex->tok.ctx.column = lex->column;
184 lex_file* lex_open(const char *file)
187 FILE *in = fs_file_open(file, "rb");
190 lexerror(NULL, "open failed: '%s'\n", file);
194 lex = (lex_file*)mem_a(sizeof(*lex));
197 lexerror(NULL, "out of memory\n");
201 memset(lex, 0, sizeof(*lex));
204 lex->name = util_strdup(file);
205 lex->line = 1; /* we start counting at 1 */
210 vec_push(lex_filenames, lex->name);
214 lex_file* lex_open_string(const char *str, size_t len, const char *name)
218 lex = (lex_file*)mem_a(sizeof(*lex));
220 lexerror(NULL, "out of memory\n");
224 memset(lex, 0, sizeof(*lex));
227 lex->open_string = str;
228 lex->open_string_length = len;
229 lex->open_string_pos = 0;
231 lex->name = util_strdup(name ? name : "<string-source>");
232 lex->line = 1; /* we start counting at 1 */
237 vec_push(lex_filenames, lex->name);
242 void lex_cleanup(void)
245 for (i = 0; i < vec_size(lex_filenames); ++i)
246 mem_d(lex_filenames[i]);
247 vec_free(lex_filenames);
250 void lex_close(lex_file *lex)
253 for (i = 0; i < vec_size(lex->frames); ++i)
254 mem_d(lex->frames[i].name);
255 vec_free(lex->frames);
258 vec_free(lex->modelname);
261 fs_file_close(lex->file);
264 token_delete(lex->tok);
266 vec_free(lex->tok.value);
268 /* mem_d(lex->name); collected in lex_filenames */
272 static int lex_fgetc(lex_file *lex)
276 return fs_file_getc(lex->file);
278 if (lex->open_string) {
279 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)
297 if (!lex->push_line && c2 == '\n') {
303 lex_ungetch(lex, c2);
308 if (!lex->push_line && c3 == '\n') {
314 case '=': return '#';
315 case '/': return '\\';
316 case '\'': return '^';
317 case '(': return '[';
318 case ')': return ']';
319 case '!': return '|';
320 case '<': return '{';
321 case '>': return '}';
322 case '-': return '~';
324 lex_ungetch(lex, c3);
325 lex_ungetch(lex, c2);
330 static int lex_try_digraph(lex_file *lex, int ch)
334 /* we just used fgetc() so count lines
335 * need to offset a \n the ungetch would recognize
337 if (!lex->push_line && c2 == '\n')
339 if (ch == '<' && c2 == ':')
341 else if (ch == ':' && c2 == '>')
343 else if (ch == '<' && c2 == '%')
345 else if (ch == '%' && c2 == '>')
347 else if (ch == '%' && c2 == ':')
349 lex_ungetch(lex, c2);
353 static int lex_getch(lex_file *lex)
359 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
361 return lex->peek[lex->peekpos];
365 if (!lex->push_line && ch == '\n')
368 return lex_try_trigraph(lex, ch);
369 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
370 return lex_try_digraph(lex, ch);
374 static void lex_ungetch(lex_file *lex, int ch)
376 lex->peek[lex->peekpos++] = ch;
378 if (!lex->push_line && ch == '\n') {
384 /* classify characters
385 * some additions to the is*() functions of ctype.h
388 /* Idents are alphanumberic, but they start with alpha or _ */
389 static bool isident_start(int ch)
391 return util_isalpha(ch) || ch == '_';
394 static bool isident(int ch)
396 return isident_start(ch) || util_isdigit(ch);
399 /* isxdigit_only is used when we already know it's not a digit
400 * and want to see if it's a hex digit anyway.
402 static bool isxdigit_only(int ch)
404 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
407 /* Append a character to the token buffer */
408 static void lex_tokench(lex_file *lex, int ch)
410 vec_push(lex->tok.value, ch);
413 /* Append a trailing null-byte */
414 static void lex_endtoken(lex_file *lex)
416 vec_push(lex->tok.value, 0);
417 vec_shrinkby(lex->tok.value, 1);
420 static bool lex_try_pragma(lex_file *lex)
424 char *command = NULL;
428 if (lex->flags.preprocessing)
435 lex_ungetch(lex, ch);
439 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
440 vec_push(pragma, ch);
443 if (ch != ' ' || strcmp(pragma, "pragma")) {
444 lex_ungetch(lex, ch);
448 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
449 vec_push(command, ch);
450 vec_push(command, 0);
453 lex_ungetch(lex, ch);
457 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
462 lex_ungetch(lex, ch);
466 if (!strcmp(command, "push")) {
467 if (!strcmp(param, "line")) {
469 if (lex->push_line == 1)
475 else if (!strcmp(command, "pop")) {
476 if (!strcmp(param, "line")) {
479 if (lex->push_line == 0)
485 else if (!strcmp(command, "file")) {
486 lex->name = util_strdup(param);
487 vec_push(lex_filenames, lex->name);
489 else if (!strcmp(command, "line")) {
490 line = strtol(param, NULL, 0)-1;
496 while (ch != '\n' && ch != EOF)
506 while (vec_size(command)) {
507 lex_ungetch(lex, (unsigned char)vec_last(command));
511 lex_ungetch(lex, ' ');
515 while (vec_size(param)) {
516 lex_ungetch(lex, (unsigned char)vec_last(param));
520 lex_ungetch(lex, ' ');
524 while (vec_size(pragma)) {
525 lex_ungetch(lex, (unsigned char)vec_last(pragma));
530 lex_ungetch(lex, '#');
536 /* Skip whitespace and comments and return the first
537 * non-white character.
538 * As this makes use of the above getch() ungetch() functions,
539 * we don't need to care at all about line numbering anymore.
541 * In theory, this function should only be used at the beginning
542 * of lexing, or when we *know* the next character is part of the token.
543 * Otherwise, if the parser throws an error, the linenumber may not be
544 * the line of the error, but the line of the next token AFTER the error.
546 * This is currently only problematic when using c-like string-continuation,
547 * since comments and whitespaces are allowed between 2 such strings.
551 "A continuation of the previous string"
552 // This line is skipped
555 * In this case, if the parse decides it didn't actually want a string,
556 * and uses lex->line to print an error, it will show the ', foo);' line's
559 * On the other hand, the parser is supposed to remember the line of the next
560 * token's beginning. In this case we would want skipwhite() to be called
561 * AFTER reading a token, so that the parser, before reading the NEXT token,
562 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
565 * here is to store the line of the first character after skipping
566 * the initial whitespace in lex->sline, this happens in lex_do.
568 static int lex_skipwhite(lex_file *lex, bool hadwhite)
571 bool haswhite = hadwhite;
576 while (ch != EOF && util_isspace(ch)) {
578 if (lex_try_pragma(lex))
581 if (lex->flags.preprocessing) {
584 /* see if there was whitespace first */
585 if (haswhite) { /* (vec_size(lex->tok.value)) { */
586 lex_ungetch(lex, ch);
590 /* otherwise return EOL */
594 lex_tokench(lex, ch);
603 /* one line comment */
606 if (lex->flags.preprocessing) {
609 lex_tokench(lex, '/');
610 lex_tokench(lex, '/');
612 lex_tokench(lex, ' ');
613 lex_tokench(lex, ' ');
616 while (ch != EOF && ch != '\n') {
617 if (lex->flags.preprocessing)
618 lex_tokench(lex, ' '); /* ch); */
621 if (lex->flags.preprocessing) {
622 lex_ungetch(lex, '\n');
630 /* multiline comment */
631 if (lex->flags.preprocessing) {
634 lex_tokench(lex, '/');
635 lex_tokench(lex, '*');
637 lex_tokench(lex, ' ');
638 lex_tokench(lex, ' ');
647 if (lex->flags.preprocessing) {
649 lex_tokench(lex, '*');
650 lex_tokench(lex, '/');
652 lex_tokench(lex, ' ');
653 lex_tokench(lex, ' ');
657 lex_ungetch(lex, ch);
659 if (lex->flags.preprocessing) {
661 lex_tokench(lex, '\n');
663 lex_tokench(lex, ' '); /* ch); */
666 ch = ' '; /* cause TRUE in the isspace check */
669 /* Otherwise roll back to the slash and break out of the loop */
670 lex_ungetch(lex, ch);
674 } while (ch != EOF && util_isspace(ch));
678 lex_ungetch(lex, ch);
685 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
690 while (ch != EOF && isident(ch))
692 lex_tokench(lex, ch);
696 /* last ch was not an ident ch: */
697 lex_ungetch(lex, ch);
702 /* read one ident for the frame list */
703 static int lex_parse_frame(lex_file *lex)
710 while (ch != EOF && ch != '\n' && util_isspace(ch))
716 if (!isident_start(ch)) {
717 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
721 lex_tokench(lex, ch);
722 if (!lex_finish_ident(lex))
728 /* read a list of $frames */
729 static bool lex_finish_frames(lex_file *lex)
736 rc = lex_parse_frame(lex);
737 if (rc > 0) /* end of line */
739 if (rc < 0) /* error */
742 for (i = 0; i < vec_size(lex->frames); ++i) {
743 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
744 lex->frames[i].value = lex->framevalue++;
745 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
750 if (i < vec_size(lex->frames))
753 m.value = lex->framevalue++;
754 m.name = util_strdup(lex->tok.value);
755 vec_shrinkto(lex->tok.value, 0);
756 vec_push(lex->frames, m);
762 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
768 char u8buf[8]; /* way more than enough */
775 return TOKEN_STRINGCONST;
777 if (lex->flags.preprocessing && ch == '\\') {
778 lex_tokench(lex, ch);
781 lexerror(lex, "unexpected end of file");
782 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
783 return (lex->tok.ttype = TOKEN_ERROR);
785 lex_tokench(lex, ch);
787 else if (ch == '\\') {
790 lexerror(lex, "unexpected end of file");
791 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
792 return (lex->tok.ttype = TOKEN_ERROR);
799 case 'a': ch = '\a'; break;
800 case 'b': ch = '\b'; break;
801 case 'r': ch = '\r'; break;
802 case 'n': ch = '\n'; break;
803 case 't': ch = '\t'; break;
804 case 'f': ch = '\f'; break;
805 case 'v': ch = '\v'; break;
808 /* same procedure as in fteqcc */
810 nextch = lex_getch(lex);
811 if (nextch >= '0' && nextch <= '9')
813 else if (nextch >= 'a' && nextch <= 'f')
814 ch += nextch - 'a' + 10;
815 else if (nextch >= 'A' && nextch <= 'F')
816 ch += nextch - 'A' + 10;
818 lexerror(lex, "bad character code");
819 lex_ungetch(lex, nextch);
820 return (lex->tok.ttype = TOKEN_ERROR);
824 nextch = lex_getch(lex);
825 if (nextch >= '0' && nextch <= '9')
827 else if (nextch >= 'a' && nextch <= 'f')
828 ch += nextch - 'a' + 10;
829 else if (nextch >= 'A' && nextch <= 'F')
830 ch += nextch - 'A' + 10;
832 lexerror(lex, "bad character code");
833 lex_ungetch(lex, nextch);
834 return (lex->tok.ttype = TOKEN_ERROR);
839 case '0': case '1': case '2': case '3':
840 case '4': case '5': case '6': case '7':
844 case '<': ch = 29; break;
845 case '-': ch = 30; break;
846 case '>': ch = 31; break;
847 case '[': ch = 16; break;
848 case ']': ch = 17; break;
851 nextch = lex_getch(lex);
852 hex = (nextch == 'x');
854 lex_ungetch(lex, nextch);
855 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
857 if (nextch >= '0' && nextch <= '9')
858 chr = chr * 10 + nextch - '0';
860 lexerror(lex, "bad character code");
861 return (lex->tok.ttype = TOKEN_ERROR);
864 if (nextch >= '0' && nextch <= '9')
865 chr = chr * 0x10 + nextch - '0';
866 else if (nextch >= 'a' && nextch <= 'f')
867 chr = chr * 0x10 + nextch - 'a' + 10;
868 else if (nextch >= 'A' && nextch <= 'F')
869 chr = chr * 0x10 + nextch - 'A' + 10;
871 lexerror(lex, "bad character code");
872 return (lex->tok.ttype = TOKEN_ERROR);
875 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
877 lexerror(lex, "character code out of range");
878 return (lex->tok.ttype = TOKEN_ERROR);
881 if (OPTS_FLAG(UTF8) && chr >= 128) {
882 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
887 lex->column += u8len;
888 for (uc = 0; uc < u8len; ++uc)
889 lex_tokench(lex, u8buf[uc]);
890 /* the last character will be inserted with the tokench() call
899 case '\n': ch = '\n'; break;
902 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
903 /* so we just add the character plus backslash no matter what it actually is */
904 lex_tokench(lex, '\\');
906 /* add the character finally */
907 lex_tokench(lex, ch);
910 lex_tokench(lex, ch);
912 lexerror(lex, "unexpected end of file within string constant");
913 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
914 return (lex->tok.ttype = TOKEN_ERROR);
917 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
923 /* parse a number... */
925 lex->tok.ttype = TOKEN_FLOATCONST;
927 lex->tok.ttype = TOKEN_INTCONST;
929 lex_tokench(lex, ch);
932 if (ch != '.' && !util_isdigit(ch))
934 if (lastch != '0' || ch != 'x')
936 /* end of the number or EOF */
937 lex_ungetch(lex, ch);
940 lex->tok.constval.i = lastch - '0';
941 return lex->tok.ttype;
947 /* EOF would have been caught above */
951 lex_tokench(lex, ch);
953 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
955 lex_tokench(lex, ch);
959 /* NOT else, '.' can come from above as well */
960 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
962 /* Allow floating comma in non-hex mode */
963 lex->tok.ttype = TOKEN_FLOATCONST;
964 lex_tokench(lex, ch);
966 /* continue digits-only */
968 while (util_isdigit(ch))
970 lex_tokench(lex, ch);
974 /* put back the last character */
975 /* but do not put back the trailing 'f' or a float */
976 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
979 /* generally we don't want words to follow numbers: */
981 lexerror(lex, "unexpected trailing characters after number");
982 return (lex->tok.ttype = TOKEN_ERROR);
984 lex_ungetch(lex, ch);
987 if (lex->tok.ttype == TOKEN_FLOATCONST)
988 lex->tok.constval.f = strtod(lex->tok.value, NULL);
990 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
991 return lex->tok.ttype;
994 int lex_do(lex_file *lex)
996 int ch, nextch, thirdch;
997 bool hadwhite = false;
1006 ch = lex_skipwhite(lex, hadwhite);
1008 if (!lex->flags.mergelines || ch != '\\')
1010 ch = lex_getch(lex);
1012 ch = lex_getch(lex);
1014 lex_ungetch(lex, ch);
1018 /* we reached a linemerge */
1019 lex_tokench(lex, '\n');
1023 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1024 return (lex->tok.ttype = ch);
1027 lex->sline = lex->line;
1028 lex->tok.ctx.line = lex->sline;
1029 lex->tok.ctx.file = lex->name;
1032 return (lex->tok.ttype = TOKEN_FATAL);
1036 return (lex->tok.ttype = TOKEN_EOF);
1039 /* modelgen / spiritgen commands */
1040 if (ch == '$' && !lex->flags.preprocessing) {
1044 ch = lex_getch(lex);
1045 if (!isident_start(ch)) {
1046 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1049 lex_tokench(lex, ch);
1050 if (!lex_finish_ident(lex))
1051 return (lex->tok.ttype = TOKEN_ERROR);
1053 /* skip the known commands */
1056 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1058 /* frame/framesave command works like an enum
1059 * similar to fteqcc we handle this in the lexer.
1060 * The reason for this is that it is sensitive to newlines,
1061 * which the parser is unaware of
1063 if (!lex_finish_frames(lex))
1064 return (lex->tok.ttype = TOKEN_ERROR);
1068 if (!strcmp(v, "framevalue"))
1070 ch = lex_getch(lex);
1071 while (ch != EOF && util_isspace(ch) && ch != '\n')
1072 ch = lex_getch(lex);
1074 if (!util_isdigit(ch)) {
1075 lexerror(lex, "$framevalue requires an integer parameter");
1080 lex->tok.ttype = lex_finish_digit(lex, ch);
1082 if (lex->tok.ttype != TOKEN_INTCONST) {
1083 lexerror(lex, "$framevalue requires an integer parameter");
1086 lex->framevalue = lex->tok.constval.i;
1090 if (!strcmp(v, "framerestore"))
1096 rc = lex_parse_frame(lex);
1099 lexerror(lex, "$framerestore requires a framename parameter");
1103 return (lex->tok.ttype = TOKEN_FATAL);
1106 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1107 if (!strcmp(v, lex->frames[frame].name)) {
1108 lex->framevalue = lex->frames[frame].value;
1112 lexerror(lex, "unknown framename `%s`", v);
1116 if (!strcmp(v, "modelname"))
1122 rc = lex_parse_frame(lex);
1125 lexerror(lex, "$modelname requires a parameter");
1129 return (lex->tok.ttype = TOKEN_FATAL);
1131 if (lex->modelname) {
1133 m.value = lex->framevalue;
1134 m.name = lex->modelname;
1135 lex->modelname = NULL;
1136 vec_push(lex->frames, m);
1138 lex->modelname = lex->tok.value;
1139 lex->tok.value = NULL;
1143 if (!strcmp(v, "flush"))
1146 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1147 mem_d(lex->frames[fi].name);
1148 vec_free(lex->frames);
1149 /* skip line (fteqcc does it too) */
1150 ch = lex_getch(lex);
1151 while (ch != EOF && ch != '\n')
1152 ch = lex_getch(lex);
1156 if (!strcmp(v, "cd") ||
1157 !strcmp(v, "origin") ||
1158 !strcmp(v, "base") ||
1159 !strcmp(v, "flags") ||
1160 !strcmp(v, "scale") ||
1164 ch = lex_getch(lex);
1165 while (ch != EOF && ch != '\n')
1166 ch = lex_getch(lex);
1170 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1171 if (!strcmp(v, lex->frames[frame].name)) {
1172 lex->tok.constval.i = lex->frames[frame].value;
1173 return (lex->tok.ttype = TOKEN_INTCONST);
1177 lexerror(lex, "invalid frame macro");
1181 /* single-character tokens */
1185 nextch = lex_getch(lex);
1186 if (nextch == '[') {
1187 lex_tokench(lex, ch);
1188 lex_tokench(lex, nextch);
1190 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1192 lex_ungetch(lex, nextch);
1197 lex_tokench(lex, ch);
1199 if (lex->flags.noops)
1200 return (lex->tok.ttype = ch);
1202 return (lex->tok.ttype = TOKEN_OPERATOR);
1205 if (lex->flags.noops) {
1206 nextch = lex_getch(lex);
1207 if (nextch == ']') {
1208 lex_tokench(lex, ch);
1209 lex_tokench(lex, nextch);
1211 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1213 lex_ungetch(lex, nextch);
1222 lex_tokench(lex, ch);
1224 return (lex->tok.ttype = ch);
1230 nextch = lex_getch(lex);
1231 /* digits starting with a dot */
1232 if (util_isdigit(nextch)) {
1233 lex_ungetch(lex, nextch);
1234 lex->tok.ttype = lex_finish_digit(lex, ch);
1236 return lex->tok.ttype;
1238 lex_ungetch(lex, nextch);
1241 if (lex->flags.noops)
1243 /* Detect characters early which are normally
1244 * operators OR PART of an operator.
1263 lex_tokench(lex, ch);
1265 return (lex->tok.ttype = ch);
1273 lex_tokench(lex, ch);
1274 /* peak ahead once */
1275 nextch = lex_getch(lex);
1276 if (nextch != '.') {
1277 lex_ungetch(lex, nextch);
1279 if (lex->flags.noops)
1280 return (lex->tok.ttype = ch);
1282 return (lex->tok.ttype = TOKEN_OPERATOR);
1284 /* peak ahead again */
1285 nextch = lex_getch(lex);
1286 if (nextch != '.') {
1287 lex_ungetch(lex, nextch);
1288 lex_ungetch(lex, '.');
1290 if (lex->flags.noops)
1291 return (lex->tok.ttype = ch);
1293 return (lex->tok.ttype = TOKEN_OPERATOR);
1295 /* fill the token to be "..." */
1296 lex_tokench(lex, ch);
1297 lex_tokench(lex, ch);
1299 return (lex->tok.ttype = TOKEN_DOTS);
1302 if (ch == ',' || ch == '.') {
1303 lex_tokench(lex, ch);
1305 return (lex->tok.ttype = TOKEN_OPERATOR);
1308 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1309 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1310 ch == '=' || ch == '!' || /* <=>, ==, != */
1311 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1312 ch == '~' || ch == '^' /* ~=, ~, ^ */
1314 lex_tokench(lex, ch);
1316 nextch = lex_getch(lex);
1317 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1318 lex_tokench(lex, nextch);
1319 } else if (ch == '<' && nextch == '=') {
1320 lex_tokench(lex, nextch);
1321 if ((thirdch = lex_getch(lex)) == '>')
1322 lex_tokench(lex, thirdch);
1324 lex_ungetch(lex, thirdch);
1326 } else if (ch == '-' && nextch == '>') {
1327 lex_tokench(lex, nextch);
1328 } else if (ch == '&' && nextch == '~') {
1329 thirdch = lex_getch(lex);
1330 if (thirdch != '=') {
1331 lex_ungetch(lex, thirdch);
1332 lex_ungetch(lex, nextch);
1335 lex_tokench(lex, nextch);
1336 lex_tokench(lex, thirdch);
1339 else if (lex->flags.preprocessing &&
1340 ch == '-' && util_isdigit(nextch))
1342 lex->tok.ttype = lex_finish_digit(lex, nextch);
1343 if (lex->tok.ttype == TOKEN_INTCONST)
1344 lex->tok.constval.i = -lex->tok.constval.i;
1346 lex->tok.constval.f = -lex->tok.constval.f;
1348 return lex->tok.ttype;
1350 lex_ungetch(lex, nextch);
1354 return (lex->tok.ttype = TOKEN_OPERATOR);
1358 if (ch == '^' || ch == '~' || ch == '!')
1360 lex_tokench(lex, ch);
1362 return (lex->tok.ttype = TOKEN_OPERATOR);
1366 if (ch == '*' || ch == '/') /* *=, /= */
1368 lex_tokench(lex, ch);
1370 nextch = lex_getch(lex);
1371 if (nextch == '=' || nextch == '*') {
1372 lex_tokench(lex, nextch);
1374 lex_ungetch(lex, nextch);
1377 return (lex->tok.ttype = TOKEN_OPERATOR);
1381 lex_tokench(lex, ch);
1383 return (lex->tok.ttype = TOKEN_OPERATOR);
1386 if (isident_start(ch))
1390 lex_tokench(lex, ch);
1391 if (!lex_finish_ident(lex)) {
1393 return (lex->tok.ttype = TOKEN_ERROR);
1396 lex->tok.ttype = TOKEN_IDENT;
1399 if (!strcmp(v, "void")) {
1400 lex->tok.ttype = TOKEN_TYPENAME;
1401 lex->tok.constval.t = TYPE_VOID;
1402 } else if (!strcmp(v, "int")) {
1403 lex->tok.ttype = TOKEN_TYPENAME;
1404 lex->tok.constval.t = TYPE_INTEGER;
1405 } else if (!strcmp(v, "float")) {
1406 lex->tok.ttype = TOKEN_TYPENAME;
1407 lex->tok.constval.t = TYPE_FLOAT;
1408 } else if (!strcmp(v, "string")) {
1409 lex->tok.ttype = TOKEN_TYPENAME;
1410 lex->tok.constval.t = TYPE_STRING;
1411 } else if (!strcmp(v, "entity")) {
1412 lex->tok.ttype = TOKEN_TYPENAME;
1413 lex->tok.constval.t = TYPE_ENTITY;
1414 } else if (!strcmp(v, "vector")) {
1415 lex->tok.ttype = TOKEN_TYPENAME;
1416 lex->tok.constval.t = TYPE_VECTOR;
1419 for (kw = 0; kw < num_keywords_qc; ++kw) {
1420 if (!strcmp(v, keywords_qc[kw]))
1421 return (lex->tok.ttype = TOKEN_KEYWORD);
1423 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1424 for (kw = 0; kw < num_keywords_fg; ++kw) {
1425 if (!strcmp(v, keywords_fg[kw]))
1426 return (lex->tok.ttype = TOKEN_KEYWORD);
1431 return lex->tok.ttype;
1436 lex->flags.nodigraphs = true;
1437 if (lex->flags.preprocessing)
1438 lex_tokench(lex, ch);
1439 lex->tok.ttype = lex_finish_string(lex, '"');
1440 if (lex->flags.preprocessing)
1441 lex_tokench(lex, ch);
1442 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1444 /* Allow c style "string" "continuation" */
1445 ch = lex_skipwhite(lex, false);
1447 lex_ungetch(lex, ch);
1451 lex->tok.ttype = lex_finish_string(lex, '"');
1453 lex->flags.nodigraphs = false;
1455 return lex->tok.ttype;
1460 /* we parse character constants like string,
1461 * but return TOKEN_CHARCONST, or a vector type if it fits...
1462 * Likewise actual unescaping has to be done by the parser.
1463 * The difference is we don't allow 'char' 'continuation'.
1465 if (lex->flags.preprocessing)
1466 lex_tokench(lex, ch);
1467 lex->tok.ttype = lex_finish_string(lex, '\'');
1468 if (lex->flags.preprocessing)
1469 lex_tokench(lex, ch);
1472 lex->tok.ttype = TOKEN_CHARCONST;
1473 /* It's a vector if we can successfully scan 3 floats */
1475 if (sscanf_s(lex->tok.value, " %f %f %f ",
1476 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1478 if (sscanf(lex->tok.value, " %f %f %f ",
1479 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1483 lex->tok.ttype = TOKEN_VECTORCONST;
1487 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1489 /* check for a valid utf8 character */
1490 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1491 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1492 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1493 : "multibyte character: `%s`" ),
1495 return (lex->tok.ttype = TOKEN_ERROR);
1498 lex->tok.constval.i = u8char;
1501 lex->tok.constval.i = lex->tok.value[0];
1504 return lex->tok.ttype;
1507 if (util_isdigit(ch))
1509 lex->tok.ttype = lex_finish_digit(lex, ch);
1511 return lex->tok.ttype;
1514 if (lex->flags.preprocessing) {
1515 lex_tokench(lex, ch);
1517 return (lex->tok.ttype = ch);
1520 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1521 return (lex->tok.ttype = TOKEN_ERROR);