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, "parse error", fmt, ap);
71 con_vprintmsg(LVL_ERROR, "", 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;
185 lex_file* lex_open(const char *file)
188 FILE *in = fs_file_open(file, "rb");
191 lexerror(NULL, "open failed: '%s'\n", file);
195 lex = (lex_file*)mem_a(sizeof(*lex));
198 lexerror(NULL, "out of memory\n");
202 memset(lex, 0, sizeof(*lex));
205 lex->name = util_strdup(file);
206 lex->line = 1; /* we start counting at 1 */
211 vec_push(lex_filenames, lex->name);
215 lex_file* lex_open_string(const char *str, size_t len, const char *name)
219 lex = (lex_file*)mem_a(sizeof(*lex));
221 lexerror(NULL, "out of memory\n");
225 memset(lex, 0, sizeof(*lex));
228 lex->open_string = str;
229 lex->open_string_length = len;
230 lex->open_string_pos = 0;
232 lex->name = util_strdup(name ? name : "<string-source>");
233 lex->line = 1; /* we start counting at 1 */
238 vec_push(lex_filenames, lex->name);
243 void lex_cleanup(void)
246 for (i = 0; i < vec_size(lex_filenames); ++i)
247 mem_d(lex_filenames[i]);
248 vec_free(lex_filenames);
251 void lex_close(lex_file *lex)
254 for (i = 0; i < vec_size(lex->frames); ++i)
255 mem_d(lex->frames[i].name);
256 vec_free(lex->frames);
259 vec_free(lex->modelname);
262 fs_file_close(lex->file);
265 token_delete(lex->tok);
267 vec_free(lex->tok.value);
269 /* mem_d(lex->name); collected in lex_filenames */
273 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)
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')
299 lex_ungetch(lex, c2);
304 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;
370 if (!lex->push_line && ch == '\n')
374 /* classify characters
375 * some additions to the is*() functions of ctype.h
378 /* Idents are alphanumberic, but they start with alpha or _ */
379 static bool isident_start(int ch)
381 return isalpha(ch) || ch == '_';
384 static bool isident(int ch)
386 return isident_start(ch) || isdigit(ch);
389 /* isxdigit_only is used when we already know it's not a digit
390 * and want to see if it's a hex digit anyway.
392 static bool isxdigit_only(int ch)
394 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
397 /* Append a character to the token buffer */
398 static void lex_tokench(lex_file *lex, int ch)
400 vec_push(lex->tok.value, ch);
403 /* Append a trailing null-byte */
404 static void lex_endtoken(lex_file *lex)
406 vec_push(lex->tok.value, 0);
407 vec_shrinkby(lex->tok.value, 1);
410 static bool lex_try_pragma(lex_file *lex)
414 char *command = NULL;
418 if (lex->flags.preprocessing)
425 lex_ungetch(lex, ch);
429 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
430 vec_push(pragma, ch);
433 if (ch != ' ' || strcmp(pragma, "pragma")) {
434 lex_ungetch(lex, ch);
438 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
439 vec_push(command, ch);
440 vec_push(command, 0);
443 lex_ungetch(lex, ch);
447 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
452 lex_ungetch(lex, ch);
456 if (!strcmp(command, "push")) {
457 if (!strcmp(param, "line")) {
459 if (lex->push_line == 1)
465 else if (!strcmp(command, "pop")) {
466 if (!strcmp(param, "line")) {
469 if (lex->push_line == 0)
475 else if (!strcmp(command, "file")) {
476 lex->name = util_strdup(param);
477 vec_push(lex_filenames, lex->name);
479 else if (!strcmp(command, "line")) {
480 line = strtol(param, NULL, 0)-1;
486 while (ch != '\n' && ch != EOF)
496 while (vec_size(command)) {
497 lex_ungetch(lex, (unsigned char)vec_last(command));
501 lex_ungetch(lex, ' ');
505 while (vec_size(param)) {
506 lex_ungetch(lex, (unsigned char)vec_last(param));
510 lex_ungetch(lex, ' ');
514 while (vec_size(pragma)) {
515 lex_ungetch(lex, (unsigned char)vec_last(pragma));
520 lex_ungetch(lex, '#');
526 /* Skip whitespace and comments and return the first
527 * non-white character.
528 * As this makes use of the above getch() ungetch() functions,
529 * we don't need to care at all about line numbering anymore.
531 * In theory, this function should only be used at the beginning
532 * of lexing, or when we *know* the next character is part of the token.
533 * Otherwise, if the parser throws an error, the linenumber may not be
534 * the line of the error, but the line of the next token AFTER the error.
536 * This is currently only problematic when using c-like string-continuation,
537 * since comments and whitespaces are allowed between 2 such strings.
541 "A continuation of the previous string"
542 // This line is skipped
545 * In this case, if the parse decides it didn't actually want a string,
546 * and uses lex->line to print an error, it will show the ', foo);' line's
549 * On the other hand, the parser is supposed to remember the line of the next
550 * token's beginning. In this case we would want skipwhite() to be called
551 * AFTER reading a token, so that the parser, before reading the NEXT token,
552 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
555 * here is to store the line of the first character after skipping
556 * the initial whitespace in lex->sline, this happens in lex_do.
558 static int lex_skipwhite(lex_file *lex, bool hadwhite)
561 bool haswhite = hadwhite;
566 while (ch != EOF && isspace(ch)) {
568 if (lex_try_pragma(lex))
571 if (lex->flags.preprocessing) {
574 /* see if there was whitespace first */
575 if (haswhite) { /* (vec_size(lex->tok.value)) { */
576 lex_ungetch(lex, ch);
580 /* otherwise return EOL */
584 lex_tokench(lex, ch);
593 /* one line comment */
596 if (lex->flags.preprocessing) {
599 lex_tokench(lex, '/');
600 lex_tokench(lex, '/');
602 lex_tokench(lex, ' ');
603 lex_tokench(lex, ' ');
606 while (ch != EOF && ch != '\n') {
607 if (lex->flags.preprocessing)
608 lex_tokench(lex, ' '); /* ch); */
611 if (lex->flags.preprocessing) {
612 lex_ungetch(lex, '\n');
620 /* multiline comment */
621 if (lex->flags.preprocessing) {
624 lex_tokench(lex, '/');
625 lex_tokench(lex, '*');
627 lex_tokench(lex, ' ');
628 lex_tokench(lex, ' ');
637 if (lex->flags.preprocessing) {
639 lex_tokench(lex, '*');
640 lex_tokench(lex, '/');
642 lex_tokench(lex, ' ');
643 lex_tokench(lex, ' ');
647 lex_ungetch(lex, ch);
649 if (lex->flags.preprocessing) {
651 lex_tokench(lex, '\n');
653 lex_tokench(lex, ' '); /* ch); */
656 ch = ' '; /* cause TRUE in the isspace check */
659 /* Otherwise roll back to the slash and break out of the loop */
660 lex_ungetch(lex, ch);
664 } while (ch != EOF && isspace(ch));
668 lex_ungetch(lex, ch);
675 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
680 while (ch != EOF && isident(ch))
682 lex_tokench(lex, ch);
686 /* last ch was not an ident ch: */
687 lex_ungetch(lex, ch);
692 /* read one ident for the frame list */
693 static int lex_parse_frame(lex_file *lex)
700 while (ch != EOF && ch != '\n' && isspace(ch))
706 if (!isident_start(ch)) {
707 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
711 lex_tokench(lex, ch);
712 if (!lex_finish_ident(lex))
718 /* read a list of $frames */
719 static bool lex_finish_frames(lex_file *lex)
726 rc = lex_parse_frame(lex);
727 if (rc > 0) /* end of line */
729 if (rc < 0) /* error */
732 for (i = 0; i < vec_size(lex->frames); ++i) {
733 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
734 lex->frames[i].value = lex->framevalue++;
735 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
740 if (i < vec_size(lex->frames))
743 m.value = lex->framevalue++;
744 m.name = util_strdup(lex->tok.value);
745 vec_shrinkto(lex->tok.value, 0);
746 vec_push(lex->frames, m);
752 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
758 char u8buf[8]; /* way more than enough */
765 return TOKEN_STRINGCONST;
767 if (lex->flags.preprocessing && ch == '\\') {
768 lex_tokench(lex, ch);
771 lexerror(lex, "unexpected end of file");
772 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
773 return (lex->tok.ttype = TOKEN_ERROR);
775 lex_tokench(lex, ch);
777 else if (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);
789 case 'a': ch = '\a'; break;
790 case 'b': ch = '\b'; break;
791 case 'r': ch = '\r'; break;
792 case 'n': ch = '\n'; break;
793 case 't': ch = '\t'; break;
794 case 'f': ch = '\f'; break;
795 case 'v': ch = '\v'; break;
798 /* same procedure as in fteqcc */
800 nextch = lex_getch(lex);
801 if (nextch >= '0' && nextch <= '9')
803 else if (nextch >= 'a' && nextch <= 'f')
804 ch += nextch - 'a' + 10;
805 else if (nextch >= 'A' && nextch <= 'F')
806 ch += nextch - 'A' + 10;
808 lexerror(lex, "bad character code");
809 lex_ungetch(lex, nextch);
810 return (lex->tok.ttype = TOKEN_ERROR);
814 nextch = lex_getch(lex);
815 if (nextch >= '0' && nextch <= '9')
817 else if (nextch >= 'a' && nextch <= 'f')
818 ch += nextch - 'a' + 10;
819 else if (nextch >= 'A' && nextch <= 'F')
820 ch += nextch - 'A' + 10;
822 lexerror(lex, "bad character code");
823 lex_ungetch(lex, nextch);
824 return (lex->tok.ttype = TOKEN_ERROR);
829 case '0': case '1': case '2': case '3':
830 case '4': case '5': case '6': case '7':
834 case '<': ch = 29; break;
835 case '-': ch = 30; break;
836 case '>': ch = 31; break;
837 case '[': ch = 16; break;
838 case ']': ch = 17; break;
841 nextch = lex_getch(lex);
842 hex = (nextch == 'x');
844 lex_ungetch(lex, nextch);
845 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
847 if (nextch >= '0' && nextch <= '9')
848 chr = chr * 10 + nextch - '0';
850 lexerror(lex, "bad character code");
851 return (lex->tok.ttype = TOKEN_ERROR);
854 if (nextch >= '0' && nextch <= '9')
855 chr = chr * 0x10 + nextch - '0';
856 else if (nextch >= 'a' && nextch <= 'f')
857 chr = chr * 0x10 + nextch - 'a' + 10;
858 else if (nextch >= 'A' && nextch <= 'F')
859 chr = chr * 0x10 + nextch - 'A' + 10;
861 lexerror(lex, "bad character code");
862 return (lex->tok.ttype = TOKEN_ERROR);
865 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
867 lexerror(lex, "character code out of range");
868 return (lex->tok.ttype = TOKEN_ERROR);
871 if (OPTS_FLAG(UTF8) && chr >= 128) {
872 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
877 for (uc = 0; uc < u8len; ++uc)
878 lex_tokench(lex, u8buf[uc]);
879 /* the last character will be inserted with the tokench() call
888 case '\n': ch = '\n'; break;
891 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
892 /* so we just add the character plus backslash no matter what it actually is */
893 lex_tokench(lex, '\\');
895 /* add the character finally */
896 lex_tokench(lex, ch);
899 lex_tokench(lex, ch);
901 lexerror(lex, "unexpected end of file within string constant");
902 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
903 return (lex->tok.ttype = TOKEN_ERROR);
906 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
912 /* parse a number... */
914 lex->tok.ttype = TOKEN_FLOATCONST;
916 lex->tok.ttype = TOKEN_INTCONST;
918 lex_tokench(lex, ch);
921 if (ch != '.' && !isdigit(ch))
923 if (lastch != '0' || ch != 'x')
925 /* end of the number or EOF */
926 lex_ungetch(lex, ch);
929 lex->tok.constval.i = lastch - '0';
930 return lex->tok.ttype;
936 /* EOF would have been caught above */
940 lex_tokench(lex, ch);
942 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
944 lex_tokench(lex, ch);
948 /* NOT else, '.' can come from above as well */
949 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
951 /* Allow floating comma in non-hex mode */
952 lex->tok.ttype = TOKEN_FLOATCONST;
953 lex_tokench(lex, ch);
955 /* continue digits-only */
959 lex_tokench(lex, ch);
963 /* put back the last character */
964 /* but do not put back the trailing 'f' or a float */
965 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
968 /* generally we don't want words to follow numbers: */
970 lexerror(lex, "unexpected trailing characters after number");
971 return (lex->tok.ttype = TOKEN_ERROR);
973 lex_ungetch(lex, ch);
976 if (lex->tok.ttype == TOKEN_FLOATCONST)
977 lex->tok.constval.f = strtod(lex->tok.value, NULL);
979 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
980 return lex->tok.ttype;
983 int lex_do(lex_file *lex)
985 int ch, nextch, thirdch;
986 bool hadwhite = false;
995 ch = lex_skipwhite(lex, hadwhite);
997 if (!lex->flags.mergelines || ch != '\\')
1001 ch = lex_getch(lex);
1003 lex_ungetch(lex, ch);
1007 /* we reached a linemerge */
1008 lex_tokench(lex, '\n');
1012 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1013 return (lex->tok.ttype = ch);
1016 lex->sline = lex->line;
1017 lex->tok.ctx.line = lex->sline;
1018 lex->tok.ctx.file = lex->name;
1021 return (lex->tok.ttype = TOKEN_FATAL);
1025 return (lex->tok.ttype = TOKEN_EOF);
1028 /* modelgen / spiritgen commands */
1029 if (ch == '$' && !lex->flags.preprocessing) {
1033 ch = lex_getch(lex);
1034 if (!isident_start(ch)) {
1035 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1038 lex_tokench(lex, ch);
1039 if (!lex_finish_ident(lex))
1040 return (lex->tok.ttype = TOKEN_ERROR);
1042 /* skip the known commands */
1045 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1047 /* frame/framesave command works like an enum
1048 * similar to fteqcc we handle this in the lexer.
1049 * The reason for this is that it is sensitive to newlines,
1050 * which the parser is unaware of
1052 if (!lex_finish_frames(lex))
1053 return (lex->tok.ttype = TOKEN_ERROR);
1057 if (!strcmp(v, "framevalue"))
1059 ch = lex_getch(lex);
1060 while (ch != EOF && isspace(ch) && ch != '\n')
1061 ch = lex_getch(lex);
1064 lexerror(lex, "$framevalue requires an integer parameter");
1069 lex->tok.ttype = lex_finish_digit(lex, ch);
1071 if (lex->tok.ttype != TOKEN_INTCONST) {
1072 lexerror(lex, "$framevalue requires an integer parameter");
1075 lex->framevalue = lex->tok.constval.i;
1079 if (!strcmp(v, "framerestore"))
1085 rc = lex_parse_frame(lex);
1088 lexerror(lex, "$framerestore requires a framename parameter");
1092 return (lex->tok.ttype = TOKEN_FATAL);
1095 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1096 if (!strcmp(v, lex->frames[frame].name)) {
1097 lex->framevalue = lex->frames[frame].value;
1101 lexerror(lex, "unknown framename `%s`", v);
1105 if (!strcmp(v, "modelname"))
1111 rc = lex_parse_frame(lex);
1114 lexerror(lex, "$modelname requires a parameter");
1118 return (lex->tok.ttype = TOKEN_FATAL);
1120 if (lex->modelname) {
1122 m.value = lex->framevalue;
1123 m.name = lex->modelname;
1124 lex->modelname = NULL;
1125 vec_push(lex->frames, m);
1127 lex->modelname = lex->tok.value;
1128 lex->tok.value = NULL;
1132 if (!strcmp(v, "flush"))
1135 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1136 mem_d(lex->frames[fi].name);
1137 vec_free(lex->frames);
1138 /* skip line (fteqcc does it too) */
1139 ch = lex_getch(lex);
1140 while (ch != EOF && ch != '\n')
1141 ch = lex_getch(lex);
1145 if (!strcmp(v, "cd") ||
1146 !strcmp(v, "origin") ||
1147 !strcmp(v, "base") ||
1148 !strcmp(v, "flags") ||
1149 !strcmp(v, "scale") ||
1153 ch = lex_getch(lex);
1154 while (ch != EOF && ch != '\n')
1155 ch = lex_getch(lex);
1159 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1160 if (!strcmp(v, lex->frames[frame].name)) {
1161 lex->tok.constval.i = lex->frames[frame].value;
1162 return (lex->tok.ttype = TOKEN_INTCONST);
1166 lexerror(lex, "invalid frame macro");
1170 /* single-character tokens */
1174 nextch = lex_getch(lex);
1175 if (nextch == '[') {
1176 lex_tokench(lex, ch);
1177 lex_tokench(lex, nextch);
1179 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1181 lex_ungetch(lex, nextch);
1186 lex_tokench(lex, ch);
1188 if (lex->flags.noops)
1189 return (lex->tok.ttype = ch);
1191 return (lex->tok.ttype = TOKEN_OPERATOR);
1194 if (lex->flags.noops) {
1195 nextch = lex_getch(lex);
1196 if (nextch == ']') {
1197 lex_tokench(lex, ch);
1198 lex_tokench(lex, nextch);
1200 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1202 lex_ungetch(lex, nextch);
1211 lex_tokench(lex, ch);
1213 return (lex->tok.ttype = ch);
1219 nextch = lex_getch(lex);
1220 /* digits starting with a dot */
1221 if (isdigit(nextch)) {
1222 lex_ungetch(lex, nextch);
1223 lex->tok.ttype = lex_finish_digit(lex, ch);
1225 return lex->tok.ttype;
1227 lex_ungetch(lex, nextch);
1230 if (lex->flags.noops)
1232 /* Detect characters early which are normally
1233 * operators OR PART of an operator.
1252 lex_tokench(lex, ch);
1254 return (lex->tok.ttype = ch);
1262 lex_tokench(lex, ch);
1263 /* peak ahead once */
1264 nextch = lex_getch(lex);
1265 if (nextch != '.') {
1266 lex_ungetch(lex, nextch);
1268 if (lex->flags.noops)
1269 return (lex->tok.ttype = ch);
1271 return (lex->tok.ttype = TOKEN_OPERATOR);
1273 /* peak ahead again */
1274 nextch = lex_getch(lex);
1275 if (nextch != '.') {
1276 lex_ungetch(lex, nextch);
1277 lex_ungetch(lex, '.');
1279 if (lex->flags.noops)
1280 return (lex->tok.ttype = ch);
1282 return (lex->tok.ttype = TOKEN_OPERATOR);
1284 /* fill the token to be "..." */
1285 lex_tokench(lex, ch);
1286 lex_tokench(lex, ch);
1288 return (lex->tok.ttype = TOKEN_DOTS);
1291 if (ch == ',' || ch == '.') {
1292 lex_tokench(lex, ch);
1294 return (lex->tok.ttype = TOKEN_OPERATOR);
1297 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1298 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1299 ch == '=' || ch == '!' || /* <=>, ==, != */
1300 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1301 ch == '~' /* ~=, ~ */
1303 lex_tokench(lex, ch);
1305 nextch = lex_getch(lex);
1306 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1307 lex_tokench(lex, nextch);
1308 } else if (ch == '<' && nextch == '=') {
1309 lex_tokench(lex, nextch);
1310 if ((thirdch = lex_getch(lex)) == '>')
1311 lex_tokench(lex, thirdch);
1313 lex_ungetch(lex, thirdch);
1315 } else if (ch == '-' && nextch == '>') {
1316 lex_tokench(lex, nextch);
1317 } else if (ch == '&' && nextch == '~') {
1318 thirdch = lex_getch(lex);
1319 if (thirdch != '=') {
1320 lex_ungetch(lex, thirdch);
1321 lex_ungetch(lex, nextch);
1324 lex_tokench(lex, nextch);
1325 lex_tokench(lex, thirdch);
1328 else if (lex->flags.preprocessing &&
1329 ch == '-' && isdigit(nextch))
1331 lex->tok.ttype = lex_finish_digit(lex, nextch);
1332 if (lex->tok.ttype == TOKEN_INTCONST)
1333 lex->tok.constval.i = -lex->tok.constval.i;
1335 lex->tok.constval.f = -lex->tok.constval.f;
1337 return lex->tok.ttype;
1339 lex_ungetch(lex, nextch);
1343 return (lex->tok.ttype = TOKEN_OPERATOR);
1347 if (ch == '^' || ch == '~' || ch == '!')
1349 lex_tokench(lex, ch);
1351 return (lex->tok.ttype = TOKEN_OPERATOR);
1355 if (ch == '*' || ch == '/') /* *=, /= */
1357 lex_tokench(lex, ch);
1359 nextch = lex_getch(lex);
1360 if (nextch == '=' || nextch == '*') {
1361 lex_tokench(lex, nextch);
1363 lex_ungetch(lex, nextch);
1366 return (lex->tok.ttype = TOKEN_OPERATOR);
1370 lex_tokench(lex, ch);
1372 return (lex->tok.ttype = TOKEN_OPERATOR);
1375 if (isident_start(ch))
1379 lex_tokench(lex, ch);
1380 if (!lex_finish_ident(lex)) {
1382 return (lex->tok.ttype = TOKEN_ERROR);
1385 lex->tok.ttype = TOKEN_IDENT;
1388 if (!strcmp(v, "void")) {
1389 lex->tok.ttype = TOKEN_TYPENAME;
1390 lex->tok.constval.t = TYPE_VOID;
1391 } else if (!strcmp(v, "int")) {
1392 lex->tok.ttype = TOKEN_TYPENAME;
1393 lex->tok.constval.t = TYPE_INTEGER;
1394 } else if (!strcmp(v, "float")) {
1395 lex->tok.ttype = TOKEN_TYPENAME;
1396 lex->tok.constval.t = TYPE_FLOAT;
1397 } else if (!strcmp(v, "string")) {
1398 lex->tok.ttype = TOKEN_TYPENAME;
1399 lex->tok.constval.t = TYPE_STRING;
1400 } else if (!strcmp(v, "entity")) {
1401 lex->tok.ttype = TOKEN_TYPENAME;
1402 lex->tok.constval.t = TYPE_ENTITY;
1403 } else if (!strcmp(v, "vector")) {
1404 lex->tok.ttype = TOKEN_TYPENAME;
1405 lex->tok.constval.t = TYPE_VECTOR;
1408 for (kw = 0; kw < num_keywords_qc; ++kw) {
1409 if (!strcmp(v, keywords_qc[kw]))
1410 return (lex->tok.ttype = TOKEN_KEYWORD);
1412 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1413 for (kw = 0; kw < num_keywords_fg; ++kw) {
1414 if (!strcmp(v, keywords_fg[kw]))
1415 return (lex->tok.ttype = TOKEN_KEYWORD);
1420 return lex->tok.ttype;
1425 lex->flags.nodigraphs = true;
1426 if (lex->flags.preprocessing)
1427 lex_tokench(lex, ch);
1428 lex->tok.ttype = lex_finish_string(lex, '"');
1429 if (lex->flags.preprocessing)
1430 lex_tokench(lex, ch);
1431 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1433 /* Allow c style "string" "continuation" */
1434 ch = lex_skipwhite(lex, false);
1436 lex_ungetch(lex, ch);
1440 lex->tok.ttype = lex_finish_string(lex, '"');
1442 lex->flags.nodigraphs = false;
1444 return lex->tok.ttype;
1449 /* we parse character constants like string,
1450 * but return TOKEN_CHARCONST, or a vector type if it fits...
1451 * Likewise actual unescaping has to be done by the parser.
1452 * The difference is we don't allow 'char' 'continuation'.
1454 if (lex->flags.preprocessing)
1455 lex_tokench(lex, ch);
1456 lex->tok.ttype = lex_finish_string(lex, '\'');
1457 if (lex->flags.preprocessing)
1458 lex_tokench(lex, ch);
1461 lex->tok.ttype = TOKEN_CHARCONST;
1462 /* It's a vector if we can successfully scan 3 floats */
1464 if (sscanf_s(lex->tok.value, " %f %f %f ",
1465 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1467 if (sscanf(lex->tok.value, " %f %f %f ",
1468 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1472 lex->tok.ttype = TOKEN_VECTORCONST;
1476 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1478 /* check for a valid utf8 character */
1479 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1480 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1481 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1482 : "multibyte character: `%s`" ),
1484 return (lex->tok.ttype = TOKEN_ERROR);
1487 lex->tok.constval.i = u8char;
1490 lex->tok.constval.i = lex->tok.value[0];
1493 return lex->tok.ttype;
1498 lex->tok.ttype = lex_finish_digit(lex, ch);
1500 return lex->tok.ttype;
1503 if (lex->flags.preprocessing) {
1504 lex_tokench(lex, ch);
1506 return (lex->tok.ttype = ch);
1509 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1510 return (lex->tok.ttype = TOKEN_ERROR);