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]);
63 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 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);
177 lex->tok.constval.t = 0;
178 lex->tok.ctx.line = lex->sline;
179 lex->tok.ctx.file = lex->name;
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)
275 return fs_file_getc(lex->file);
276 if (lex->open_string) {
277 if (lex->open_string_pos >= lex->open_string_length)
279 return lex->open_string[lex->open_string_pos++];
284 /* Get or put-back data
285 * The following to functions do NOT understand what kind of data they
287 * The are merely wrapping get/put in order to count line numbers.
289 static void lex_ungetch(lex_file *lex, int ch);
290 static int lex_try_trigraph(lex_file *lex, int old)
294 if (!lex->push_line && c2 == '\n')
297 lex_ungetch(lex, c2);
302 if (!lex->push_line && c3 == '\n')
305 case '=': return '#';
306 case '/': return '\\';
307 case '\'': return '^';
308 case '(': return '[';
309 case ')': return ']';
310 case '!': return '|';
311 case '<': return '{';
312 case '>': return '}';
313 case '-': return '~';
315 lex_ungetch(lex, c3);
316 lex_ungetch(lex, c2);
321 static int lex_try_digraph(lex_file *lex, int ch)
325 /* we just used fgetc() so count lines
326 * need to offset a \n the ungetch would recognize
328 if (!lex->push_line && c2 == '\n')
330 if (ch == '<' && c2 == ':')
332 else if (ch == ':' && c2 == '>')
334 else if (ch == '<' && c2 == '%')
336 else if (ch == '%' && c2 == '>')
338 else if (ch == '%' && c2 == ':')
340 lex_ungetch(lex, c2);
344 static int lex_getch(lex_file *lex)
350 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
352 return lex->peek[lex->peekpos];
356 if (!lex->push_line && ch == '\n')
359 return lex_try_trigraph(lex, ch);
360 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
361 return lex_try_digraph(lex, ch);
365 static void lex_ungetch(lex_file *lex, int ch)
367 lex->peek[lex->peekpos++] = ch;
368 if (!lex->push_line && ch == '\n')
372 /* classify characters
373 * some additions to the is*() functions of ctype.h
376 /* Idents are alphanumberic, but they start with alpha or _ */
377 static bool isident_start(int ch)
379 return isalpha(ch) || ch == '_';
382 static bool isident(int ch)
384 return isident_start(ch) || isdigit(ch);
387 /* isxdigit_only is used when we already know it's not a digit
388 * and want to see if it's a hex digit anyway.
390 static bool isxdigit_only(int ch)
392 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
395 /* Append a character to the token buffer */
396 static void lex_tokench(lex_file *lex, int ch)
398 vec_push(lex->tok.value, ch);
401 /* Append a trailing null-byte */
402 static void lex_endtoken(lex_file *lex)
404 vec_push(lex->tok.value, 0);
405 vec_shrinkby(lex->tok.value, 1);
408 static bool lex_try_pragma(lex_file *lex)
412 char *command = NULL;
416 if (lex->flags.preprocessing)
423 lex_ungetch(lex, ch);
427 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
428 vec_push(pragma, ch);
431 if (ch != ' ' || strcmp(pragma, "pragma")) {
432 lex_ungetch(lex, ch);
436 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
437 vec_push(command, ch);
438 vec_push(command, 0);
441 lex_ungetch(lex, ch);
445 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
450 lex_ungetch(lex, ch);
454 if (!strcmp(command, "push")) {
455 if (!strcmp(param, "line")) {
457 if (lex->push_line == 1)
463 else if (!strcmp(command, "pop")) {
464 if (!strcmp(param, "line")) {
467 if (lex->push_line == 0)
473 else if (!strcmp(command, "file")) {
474 lex->name = util_strdup(param);
475 vec_push(lex_filenames, lex->name);
477 else if (!strcmp(command, "line")) {
478 line = strtol(param, NULL, 0)-1;
484 while (ch != '\n' && ch != EOF)
494 while (vec_size(command)) {
495 lex_ungetch(lex, (unsigned char)vec_last(command));
499 lex_ungetch(lex, ' ');
503 while (vec_size(param)) {
504 lex_ungetch(lex, (unsigned char)vec_last(param));
508 lex_ungetch(lex, ' ');
512 while (vec_size(pragma)) {
513 lex_ungetch(lex, (unsigned char)vec_last(pragma));
518 lex_ungetch(lex, '#');
524 /* Skip whitespace and comments and return the first
525 * non-white character.
526 * As this makes use of the above getch() ungetch() functions,
527 * we don't need to care at all about line numbering anymore.
529 * In theory, this function should only be used at the beginning
530 * of lexing, or when we *know* the next character is part of the token.
531 * Otherwise, if the parser throws an error, the linenumber may not be
532 * the line of the error, but the line of the next token AFTER the error.
534 * This is currently only problematic when using c-like string-continuation,
535 * since comments and whitespaces are allowed between 2 such strings.
539 "A continuation of the previous string"
540 // This line is skipped
543 * In this case, if the parse decides it didn't actually want a string,
544 * and uses lex->line to print an error, it will show the ', foo);' line's
547 * On the other hand, the parser is supposed to remember the line of the next
548 * token's beginning. In this case we would want skipwhite() to be called
549 * AFTER reading a token, so that the parser, before reading the NEXT token,
550 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
553 * here is to store the line of the first character after skipping
554 * the initial whitespace in lex->sline, this happens in lex_do.
556 static int lex_skipwhite(lex_file *lex, bool hadwhite)
559 bool haswhite = hadwhite;
564 while (ch != EOF && isspace(ch)) {
566 if (lex_try_pragma(lex))
569 if (lex->flags.preprocessing) {
572 /* see if there was whitespace first */
573 if (haswhite) { /* (vec_size(lex->tok.value)) { */
574 lex_ungetch(lex, ch);
578 /* otherwise return EOL */
582 lex_tokench(lex, ch);
591 /* one line comment */
594 if (lex->flags.preprocessing) {
597 lex_tokench(lex, '/');
598 lex_tokench(lex, '/');
600 lex_tokench(lex, ' ');
601 lex_tokench(lex, ' ');
604 while (ch != EOF && ch != '\n') {
605 if (lex->flags.preprocessing)
606 lex_tokench(lex, ' '); /* ch); */
609 if (lex->flags.preprocessing) {
610 lex_ungetch(lex, '\n');
618 /* multiline comment */
619 if (lex->flags.preprocessing) {
622 lex_tokench(lex, '/');
623 lex_tokench(lex, '*');
625 lex_tokench(lex, ' ');
626 lex_tokench(lex, ' ');
635 if (lex->flags.preprocessing) {
637 lex_tokench(lex, '*');
638 lex_tokench(lex, '/');
640 lex_tokench(lex, ' ');
641 lex_tokench(lex, ' ');
645 lex_ungetch(lex, ch);
647 if (lex->flags.preprocessing) {
649 lex_tokench(lex, '\n');
651 lex_tokench(lex, ' '); /* ch); */
654 ch = ' '; /* cause TRUE in the isspace check */
657 /* Otherwise roll back to the slash and break out of the loop */
658 lex_ungetch(lex, ch);
662 } while (ch != EOF && isspace(ch));
666 lex_ungetch(lex, ch);
673 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
678 while (ch != EOF && isident(ch))
680 lex_tokench(lex, ch);
684 /* last ch was not an ident ch: */
685 lex_ungetch(lex, ch);
690 /* read one ident for the frame list */
691 static int lex_parse_frame(lex_file *lex)
698 while (ch != EOF && ch != '\n' && isspace(ch))
704 if (!isident_start(ch)) {
705 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
709 lex_tokench(lex, ch);
710 if (!lex_finish_ident(lex))
716 /* read a list of $frames */
717 static bool lex_finish_frames(lex_file *lex)
724 rc = lex_parse_frame(lex);
725 if (rc > 0) /* end of line */
727 if (rc < 0) /* error */
730 for (i = 0; i < vec_size(lex->frames); ++i) {
731 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
732 lex->frames[i].value = lex->framevalue++;
733 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
738 if (i < vec_size(lex->frames))
741 m.value = lex->framevalue++;
742 m.name = util_strdup(lex->tok.value);
743 vec_shrinkto(lex->tok.value, 0);
744 vec_push(lex->frames, m);
750 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
756 char u8buf[8]; /* way more than enough */
763 return TOKEN_STRINGCONST;
765 if (lex->flags.preprocessing && ch == '\\') {
766 lex_tokench(lex, ch);
769 lexerror(lex, "unexpected end of file");
770 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
771 return (lex->tok.ttype = TOKEN_ERROR);
773 lex_tokench(lex, ch);
775 else if (ch == '\\') {
778 lexerror(lex, "unexpected end of file");
779 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
780 return (lex->tok.ttype = TOKEN_ERROR);
787 case 'a': ch = '\a'; break;
788 case 'b': ch = '\b'; break;
789 case 'r': ch = '\r'; break;
790 case 'n': ch = '\n'; break;
791 case 't': ch = '\t'; break;
792 case 'f': ch = '\f'; break;
793 case 'v': ch = '\v'; break;
796 /* same procedure as in fteqcc */
798 nextch = lex_getch(lex);
799 if (nextch >= '0' && nextch <= '9')
801 else if (nextch >= 'a' && nextch <= 'f')
802 ch += nextch - 'a' + 10;
803 else if (nextch >= 'A' && nextch <= 'F')
804 ch += nextch - 'A' + 10;
806 lexerror(lex, "bad character code");
807 lex_ungetch(lex, nextch);
808 return (lex->tok.ttype = TOKEN_ERROR);
812 nextch = lex_getch(lex);
813 if (nextch >= '0' && nextch <= '9')
815 else if (nextch >= 'a' && nextch <= 'f')
816 ch += nextch - 'a' + 10;
817 else if (nextch >= 'A' && nextch <= 'F')
818 ch += nextch - 'A' + 10;
820 lexerror(lex, "bad character code");
821 lex_ungetch(lex, nextch);
822 return (lex->tok.ttype = TOKEN_ERROR);
827 case '0': case '1': case '2': case '3':
828 case '4': case '5': case '6': case '7':
832 case '<': ch = 29; break;
833 case '-': ch = 30; break;
834 case '>': ch = 31; break;
835 case '[': ch = 16; break;
836 case ']': ch = 17; break;
839 nextch = lex_getch(lex);
840 hex = (nextch == 'x');
842 lex_ungetch(lex, nextch);
843 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
845 if (nextch >= '0' && nextch <= '9')
846 chr = chr * 10 + nextch - '0';
848 lexerror(lex, "bad character code");
849 return (lex->tok.ttype = TOKEN_ERROR);
852 if (nextch >= '0' && nextch <= '9')
853 chr = chr * 0x10 + nextch - '0';
854 else if (nextch >= 'a' && nextch <= 'f')
855 chr = chr * 0x10 + nextch - 'a' + 10;
856 else if (nextch >= 'A' && nextch <= 'F')
857 chr = chr * 0x10 + nextch - 'A' + 10;
859 lexerror(lex, "bad character code");
860 return (lex->tok.ttype = TOKEN_ERROR);
863 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
865 lexerror(lex, "character code out of range");
866 return (lex->tok.ttype = TOKEN_ERROR);
869 if (OPTS_FLAG(UTF8) && chr >= 128) {
870 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
875 for (uc = 0; uc < u8len; ++uc)
876 lex_tokench(lex, u8buf[uc]);
877 /* the last character will be inserted with the tokench() call
886 case '\n': ch = '\n'; break;
889 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
890 /* so we just add the character plus backslash no matter what it actually is */
891 lex_tokench(lex, '\\');
893 /* add the character finally */
894 lex_tokench(lex, ch);
897 lex_tokench(lex, ch);
899 lexerror(lex, "unexpected end of file within string constant");
900 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
901 return (lex->tok.ttype = TOKEN_ERROR);
904 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
910 /* parse a number... */
912 lex->tok.ttype = TOKEN_FLOATCONST;
914 lex->tok.ttype = TOKEN_INTCONST;
916 lex_tokench(lex, ch);
919 if (ch != '.' && !isdigit(ch))
921 if (lastch != '0' || ch != 'x')
923 /* end of the number or EOF */
924 lex_ungetch(lex, ch);
927 lex->tok.constval.i = lastch - '0';
928 return lex->tok.ttype;
934 /* EOF would have been caught above */
938 lex_tokench(lex, ch);
940 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
942 lex_tokench(lex, ch);
946 /* NOT else, '.' can come from above as well */
947 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
949 /* Allow floating comma in non-hex mode */
950 lex->tok.ttype = TOKEN_FLOATCONST;
951 lex_tokench(lex, ch);
953 /* continue digits-only */
957 lex_tokench(lex, ch);
961 /* put back the last character */
962 /* but do not put back the trailing 'f' or a float */
963 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
966 /* generally we don't want words to follow numbers: */
968 lexerror(lex, "unexpected trailing characters after number");
969 return (lex->tok.ttype = TOKEN_ERROR);
971 lex_ungetch(lex, ch);
974 if (lex->tok.ttype == TOKEN_FLOATCONST)
975 lex->tok.constval.f = strtod(lex->tok.value, NULL);
977 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
978 return lex->tok.ttype;
981 int lex_do(lex_file *lex)
983 int ch, nextch, thirdch;
984 bool hadwhite = false;
993 ch = lex_skipwhite(lex, hadwhite);
995 if (!lex->flags.mergelines || ch != '\\')
1001 lex_ungetch(lex, ch);
1005 /* we reached a linemerge */
1006 lex_tokench(lex, '\n');
1010 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1011 return (lex->tok.ttype = ch);
1014 lex->sline = lex->line;
1015 lex->tok.ctx.line = lex->sline;
1016 lex->tok.ctx.file = lex->name;
1019 return (lex->tok.ttype = TOKEN_FATAL);
1023 return (lex->tok.ttype = TOKEN_EOF);
1026 /* modelgen / spiritgen commands */
1027 if (ch == '$' && !lex->flags.preprocessing) {
1031 ch = lex_getch(lex);
1032 if (!isident_start(ch)) {
1033 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1036 lex_tokench(lex, ch);
1037 if (!lex_finish_ident(lex))
1038 return (lex->tok.ttype = TOKEN_ERROR);
1040 /* skip the known commands */
1043 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1045 /* frame/framesave command works like an enum
1046 * similar to fteqcc we handle this in the lexer.
1047 * The reason for this is that it is sensitive to newlines,
1048 * which the parser is unaware of
1050 if (!lex_finish_frames(lex))
1051 return (lex->tok.ttype = TOKEN_ERROR);
1055 if (!strcmp(v, "framevalue"))
1057 ch = lex_getch(lex);
1058 while (ch != EOF && isspace(ch) && ch != '\n')
1059 ch = lex_getch(lex);
1062 lexerror(lex, "$framevalue requires an integer parameter");
1067 lex->tok.ttype = lex_finish_digit(lex, ch);
1069 if (lex->tok.ttype != TOKEN_INTCONST) {
1070 lexerror(lex, "$framevalue requires an integer parameter");
1073 lex->framevalue = lex->tok.constval.i;
1077 if (!strcmp(v, "framerestore"))
1083 rc = lex_parse_frame(lex);
1086 lexerror(lex, "$framerestore requires a framename parameter");
1090 return (lex->tok.ttype = TOKEN_FATAL);
1093 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1094 if (!strcmp(v, lex->frames[frame].name)) {
1095 lex->framevalue = lex->frames[frame].value;
1099 lexerror(lex, "unknown framename `%s`", v);
1103 if (!strcmp(v, "modelname"))
1109 rc = lex_parse_frame(lex);
1112 lexerror(lex, "$modelname requires a parameter");
1116 return (lex->tok.ttype = TOKEN_FATAL);
1118 if (lex->modelname) {
1120 m.value = lex->framevalue;
1121 m.name = lex->modelname;
1122 lex->modelname = NULL;
1123 vec_push(lex->frames, m);
1125 lex->modelname = lex->tok.value;
1126 lex->tok.value = NULL;
1130 if (!strcmp(v, "flush"))
1133 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1134 mem_d(lex->frames[fi].name);
1135 vec_free(lex->frames);
1136 /* skip line (fteqcc does it too) */
1137 ch = lex_getch(lex);
1138 while (ch != EOF && ch != '\n')
1139 ch = lex_getch(lex);
1143 if (!strcmp(v, "cd") ||
1144 !strcmp(v, "origin") ||
1145 !strcmp(v, "base") ||
1146 !strcmp(v, "flags") ||
1147 !strcmp(v, "scale") ||
1151 ch = lex_getch(lex);
1152 while (ch != EOF && ch != '\n')
1153 ch = lex_getch(lex);
1157 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1158 if (!strcmp(v, lex->frames[frame].name)) {
1159 lex->tok.constval.i = lex->frames[frame].value;
1160 return (lex->tok.ttype = TOKEN_INTCONST);
1164 lexerror(lex, "invalid frame macro");
1168 /* single-character tokens */
1172 nextch = lex_getch(lex);
1173 if (nextch == '[') {
1174 lex_tokench(lex, ch);
1175 lex_tokench(lex, nextch);
1177 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1179 lex_ungetch(lex, nextch);
1184 lex_tokench(lex, ch);
1186 if (lex->flags.noops)
1187 return (lex->tok.ttype = ch);
1189 return (lex->tok.ttype = TOKEN_OPERATOR);
1192 if (lex->flags.noops) {
1193 nextch = lex_getch(lex);
1194 if (nextch == ']') {
1195 lex_tokench(lex, ch);
1196 lex_tokench(lex, nextch);
1198 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1200 lex_ungetch(lex, nextch);
1209 lex_tokench(lex, ch);
1211 return (lex->tok.ttype = ch);
1217 nextch = lex_getch(lex);
1218 /* digits starting with a dot */
1219 if (isdigit(nextch)) {
1220 lex_ungetch(lex, nextch);
1221 lex->tok.ttype = lex_finish_digit(lex, ch);
1223 return lex->tok.ttype;
1225 lex_ungetch(lex, nextch);
1228 if (lex->flags.noops)
1230 /* Detect characters early which are normally
1231 * operators OR PART of an operator.
1250 lex_tokench(lex, ch);
1252 return (lex->tok.ttype = ch);
1260 lex_tokench(lex, ch);
1261 /* peak ahead once */
1262 nextch = lex_getch(lex);
1263 if (nextch != '.') {
1264 lex_ungetch(lex, nextch);
1266 if (lex->flags.noops)
1267 return (lex->tok.ttype = ch);
1269 return (lex->tok.ttype = TOKEN_OPERATOR);
1271 /* peak ahead again */
1272 nextch = lex_getch(lex);
1273 if (nextch != '.') {
1274 lex_ungetch(lex, nextch);
1275 lex_ungetch(lex, '.');
1277 if (lex->flags.noops)
1278 return (lex->tok.ttype = ch);
1280 return (lex->tok.ttype = TOKEN_OPERATOR);
1282 /* fill the token to be "..." */
1283 lex_tokench(lex, ch);
1284 lex_tokench(lex, ch);
1286 return (lex->tok.ttype = TOKEN_DOTS);
1289 if (ch == ',' || ch == '.') {
1290 lex_tokench(lex, ch);
1292 return (lex->tok.ttype = TOKEN_OPERATOR);
1295 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1296 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1297 ch == '=' || ch == '!' || /* <=>, ==, != */
1298 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1299 ch == '~' /* ~=, ~ */
1301 lex_tokench(lex, ch);
1303 nextch = lex_getch(lex);
1304 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1305 lex_tokench(lex, nextch);
1306 } else if (ch == '<' && nextch == '=') {
1307 lex_tokench(lex, nextch);
1308 if ((thirdch = lex_getch(lex)) == '>')
1309 lex_tokench(lex, thirdch);
1311 lex_ungetch(lex, thirdch);
1313 } else if (ch == '-' && nextch == '>') {
1314 lex_tokench(lex, nextch);
1315 } else if (ch == '&' && nextch == '~') {
1316 thirdch = lex_getch(lex);
1317 if (thirdch != '=') {
1318 lex_ungetch(lex, thirdch);
1319 lex_ungetch(lex, nextch);
1322 lex_tokench(lex, nextch);
1323 lex_tokench(lex, thirdch);
1326 else if (lex->flags.preprocessing &&
1327 ch == '-' && isdigit(nextch))
1329 lex->tok.ttype = lex_finish_digit(lex, nextch);
1330 if (lex->tok.ttype == TOKEN_INTCONST)
1331 lex->tok.constval.i = -lex->tok.constval.i;
1333 lex->tok.constval.f = -lex->tok.constval.f;
1335 return lex->tok.ttype;
1337 lex_ungetch(lex, nextch);
1341 return (lex->tok.ttype = TOKEN_OPERATOR);
1345 if (ch == '^' || ch == '~' || ch == '!')
1347 lex_tokench(lex, ch);
1349 return (lex->tok.ttype = TOKEN_OPERATOR);
1353 if (ch == '*' || ch == '/') /* *=, /= */
1355 lex_tokench(lex, ch);
1357 nextch = lex_getch(lex);
1358 if (nextch == '=' || nextch == '*') {
1359 lex_tokench(lex, nextch);
1361 lex_ungetch(lex, nextch);
1364 return (lex->tok.ttype = TOKEN_OPERATOR);
1368 lex_tokench(lex, ch);
1370 return (lex->tok.ttype = TOKEN_OPERATOR);
1373 if (isident_start(ch))
1377 lex_tokench(lex, ch);
1378 if (!lex_finish_ident(lex)) {
1380 return (lex->tok.ttype = TOKEN_ERROR);
1383 lex->tok.ttype = TOKEN_IDENT;
1386 if (!strcmp(v, "void")) {
1387 lex->tok.ttype = TOKEN_TYPENAME;
1388 lex->tok.constval.t = TYPE_VOID;
1389 } else if (!strcmp(v, "int")) {
1390 lex->tok.ttype = TOKEN_TYPENAME;
1391 lex->tok.constval.t = TYPE_INTEGER;
1392 } else if (!strcmp(v, "float")) {
1393 lex->tok.ttype = TOKEN_TYPENAME;
1394 lex->tok.constval.t = TYPE_FLOAT;
1395 } else if (!strcmp(v, "string")) {
1396 lex->tok.ttype = TOKEN_TYPENAME;
1397 lex->tok.constval.t = TYPE_STRING;
1398 } else if (!strcmp(v, "entity")) {
1399 lex->tok.ttype = TOKEN_TYPENAME;
1400 lex->tok.constval.t = TYPE_ENTITY;
1401 } else if (!strcmp(v, "vector")) {
1402 lex->tok.ttype = TOKEN_TYPENAME;
1403 lex->tok.constval.t = TYPE_VECTOR;
1406 for (kw = 0; kw < num_keywords_qc; ++kw) {
1407 if (!strcmp(v, keywords_qc[kw]))
1408 return (lex->tok.ttype = TOKEN_KEYWORD);
1410 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1411 for (kw = 0; kw < num_keywords_fg; ++kw) {
1412 if (!strcmp(v, keywords_fg[kw]))
1413 return (lex->tok.ttype = TOKEN_KEYWORD);
1418 return lex->tok.ttype;
1423 lex->flags.nodigraphs = true;
1424 if (lex->flags.preprocessing)
1425 lex_tokench(lex, ch);
1426 lex->tok.ttype = lex_finish_string(lex, '"');
1427 if (lex->flags.preprocessing)
1428 lex_tokench(lex, ch);
1429 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1431 /* Allow c style "string" "continuation" */
1432 ch = lex_skipwhite(lex, false);
1434 lex_ungetch(lex, ch);
1438 lex->tok.ttype = lex_finish_string(lex, '"');
1440 lex->flags.nodigraphs = false;
1442 return lex->tok.ttype;
1447 /* we parse character constants like string,
1448 * but return TOKEN_CHARCONST, or a vector type if it fits...
1449 * Likewise actual unescaping has to be done by the parser.
1450 * The difference is we don't allow 'char' 'continuation'.
1452 if (lex->flags.preprocessing)
1453 lex_tokench(lex, ch);
1454 lex->tok.ttype = lex_finish_string(lex, '\'');
1455 if (lex->flags.preprocessing)
1456 lex_tokench(lex, ch);
1459 lex->tok.ttype = TOKEN_CHARCONST;
1460 /* It's a vector if we can successfully scan 3 floats */
1462 if (sscanf_s(lex->tok.value, " %f %f %f ",
1463 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1465 if (sscanf(lex->tok.value, " %f %f %f ",
1466 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1470 lex->tok.ttype = TOKEN_VECTORCONST;
1474 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1476 /* check for a valid utf8 character */
1477 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1478 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1479 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1480 : "multibyte character: `%s`" ),
1482 return (lex->tok.ttype = TOKEN_ERROR);
1485 lex->tok.constval.i = u8char;
1488 lex->tok.constval.i = lex->tok.value[0];
1491 return lex->tok.ttype;
1496 lex->tok.ttype = lex_finish_digit(lex, ch);
1498 return lex->tok.ttype;
1501 if (lex->flags.preprocessing) {
1502 lex_tokench(lex, ch);
1504 return (lex->tok.ttype = ch);
1507 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1508 return (lex->tok.ttype = TOKEN_ERROR);