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);
277 if (lex->open_string) {
278 if (lex->open_string_pos >= lex->open_string_length)
280 return lex->open_string[lex->open_string_pos++];
285 /* Get or put-back data
286 * The following to functions do NOT understand what kind of data they
288 * The are merely wrapping get/put in order to count line numbers.
290 static void lex_ungetch(lex_file *lex, int ch);
291 static int lex_try_trigraph(lex_file *lex, int old)
295 if (!lex->push_line && c2 == '\n')
298 lex_ungetch(lex, c2);
303 if (!lex->push_line && c3 == '\n')
306 case '=': return '#';
307 case '/': return '\\';
308 case '\'': return '^';
309 case '(': return '[';
310 case ')': return ']';
311 case '!': return '|';
312 case '<': return '{';
313 case '>': return '}';
314 case '-': return '~';
316 lex_ungetch(lex, c3);
317 lex_ungetch(lex, c2);
322 static int lex_try_digraph(lex_file *lex, int ch)
326 /* we just used fgetc() so count lines
327 * need to offset a \n the ungetch would recognize
329 if (!lex->push_line && c2 == '\n')
331 if (ch == '<' && c2 == ':')
333 else if (ch == ':' && c2 == '>')
335 else if (ch == '<' && c2 == '%')
337 else if (ch == '%' && c2 == '>')
339 else if (ch == '%' && c2 == ':')
341 lex_ungetch(lex, c2);
345 static int lex_getch(lex_file *lex)
351 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
353 return lex->peek[lex->peekpos];
357 if (!lex->push_line && ch == '\n')
360 return lex_try_trigraph(lex, ch);
361 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
362 return lex_try_digraph(lex, ch);
366 static void lex_ungetch(lex_file *lex, int ch)
368 lex->peek[lex->peekpos++] = ch;
369 if (!lex->push_line && ch == '\n')
373 /* classify characters
374 * some additions to the is*() functions of ctype.h
377 /* Idents are alphanumberic, but they start with alpha or _ */
378 static bool isident_start(int ch)
380 return isalpha(ch) || ch == '_';
383 static bool isident(int ch)
385 return isident_start(ch) || isdigit(ch);
388 /* isxdigit_only is used when we already know it's not a digit
389 * and want to see if it's a hex digit anyway.
391 static bool isxdigit_only(int ch)
393 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
396 /* Append a character to the token buffer */
397 static void lex_tokench(lex_file *lex, int ch)
399 vec_push(lex->tok.value, ch);
402 /* Append a trailing null-byte */
403 static void lex_endtoken(lex_file *lex)
405 vec_push(lex->tok.value, 0);
406 vec_shrinkby(lex->tok.value, 1);
409 static bool lex_try_pragma(lex_file *lex)
413 char *command = NULL;
417 if (lex->flags.preprocessing)
424 lex_ungetch(lex, ch);
428 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
429 vec_push(pragma, ch);
432 if (ch != ' ' || strcmp(pragma, "pragma")) {
433 lex_ungetch(lex, ch);
437 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
438 vec_push(command, ch);
439 vec_push(command, 0);
442 lex_ungetch(lex, ch);
446 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
451 lex_ungetch(lex, ch);
455 if (!strcmp(command, "push")) {
456 if (!strcmp(param, "line")) {
458 if (lex->push_line == 1)
464 else if (!strcmp(command, "pop")) {
465 if (!strcmp(param, "line")) {
468 if (lex->push_line == 0)
474 else if (!strcmp(command, "file")) {
475 lex->name = util_strdup(param);
476 vec_push(lex_filenames, lex->name);
478 else if (!strcmp(command, "line")) {
479 line = strtol(param, NULL, 0)-1;
485 while (ch != '\n' && ch != EOF)
492 while (vec_size(command)) {
493 lex_ungetch(lex, (unsigned char)vec_last(command));
497 lex_ungetch(lex, ' ');
501 while (vec_size(param)) {
502 lex_ungetch(lex, (unsigned char)vec_last(param));
506 lex_ungetch(lex, ' ');
510 while (vec_size(pragma)) {
511 lex_ungetch(lex, (unsigned char)vec_last(pragma));
516 lex_ungetch(lex, '#');
522 /* Skip whitespace and comments and return the first
523 * non-white character.
524 * As this makes use of the above getch() ungetch() functions,
525 * we don't need to care at all about line numbering anymore.
527 * In theory, this function should only be used at the beginning
528 * of lexing, or when we *know* the next character is part of the token.
529 * Otherwise, if the parser throws an error, the linenumber may not be
530 * the line of the error, but the line of the next token AFTER the error.
532 * This is currently only problematic when using c-like string-continuation,
533 * since comments and whitespaces are allowed between 2 such strings.
537 "A continuation of the previous string"
538 // This line is skipped
541 * In this case, if the parse decides it didn't actually want a string,
542 * and uses lex->line to print an error, it will show the ', foo);' line's
545 * On the other hand, the parser is supposed to remember the line of the next
546 * token's beginning. In this case we would want skipwhite() to be called
547 * AFTER reading a token, so that the parser, before reading the NEXT token,
548 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
551 * here is to store the line of the first character after skipping
552 * the initial whitespace in lex->sline, this happens in lex_do.
554 static int lex_skipwhite(lex_file *lex, bool hadwhite)
557 bool haswhite = hadwhite;
562 while (ch != EOF && isspace(ch)) {
564 if (lex_try_pragma(lex))
567 if (lex->flags.preprocessing) {
570 /* see if there was whitespace first */
571 if (haswhite) { /* (vec_size(lex->tok.value)) { */
572 lex_ungetch(lex, ch);
576 /* otherwise return EOL */
580 lex_tokench(lex, ch);
589 /* one line comment */
592 if (lex->flags.preprocessing) {
595 lex_tokench(lex, '/');
596 lex_tokench(lex, '/');
598 lex_tokench(lex, ' ');
599 lex_tokench(lex, ' ');
602 while (ch != EOF && ch != '\n') {
603 if (lex->flags.preprocessing)
604 lex_tokench(lex, ' '); /* ch); */
607 if (lex->flags.preprocessing) {
608 lex_ungetch(lex, '\n');
616 /* multiline comment */
617 if (lex->flags.preprocessing) {
620 lex_tokench(lex, '/');
621 lex_tokench(lex, '*');
623 lex_tokench(lex, ' ');
624 lex_tokench(lex, ' ');
633 if (lex->flags.preprocessing) {
635 lex_tokench(lex, '*');
636 lex_tokench(lex, '/');
638 lex_tokench(lex, ' ');
639 lex_tokench(lex, ' ');
643 lex_ungetch(lex, ch);
645 if (lex->flags.preprocessing) {
647 lex_tokench(lex, '\n');
649 lex_tokench(lex, ' '); /* ch); */
652 ch = ' '; /* cause TRUE in the isspace check */
655 /* Otherwise roll back to the slash and break out of the loop */
656 lex_ungetch(lex, ch);
660 } while (ch != EOF && isspace(ch));
664 lex_ungetch(lex, ch);
671 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
676 while (ch != EOF && isident(ch))
678 lex_tokench(lex, ch);
682 /* last ch was not an ident ch: */
683 lex_ungetch(lex, ch);
688 /* read one ident for the frame list */
689 static int lex_parse_frame(lex_file *lex)
696 while (ch != EOF && ch != '\n' && isspace(ch))
702 if (!isident_start(ch)) {
703 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
707 lex_tokench(lex, ch);
708 if (!lex_finish_ident(lex))
714 /* read a list of $frames */
715 static bool lex_finish_frames(lex_file *lex)
722 rc = lex_parse_frame(lex);
723 if (rc > 0) /* end of line */
725 if (rc < 0) /* error */
728 for (i = 0; i < vec_size(lex->frames); ++i) {
729 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
730 lex->frames[i].value = lex->framevalue++;
731 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
736 if (i < vec_size(lex->frames))
739 m.value = lex->framevalue++;
740 m.name = util_strdup(lex->tok.value);
741 vec_shrinkto(lex->tok.value, 0);
742 vec_push(lex->frames, m);
748 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
754 char u8buf[8]; /* way more than enough */
761 return TOKEN_STRINGCONST;
763 if (lex->flags.preprocessing && ch == '\\') {
764 lex_tokench(lex, ch);
767 lexerror(lex, "unexpected end of file");
768 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
769 return (lex->tok.ttype = TOKEN_ERROR);
771 lex_tokench(lex, ch);
773 else if (ch == '\\') {
776 lexerror(lex, "unexpected end of file");
777 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
778 return (lex->tok.ttype = TOKEN_ERROR);
785 case 'a': ch = '\a'; break;
786 case 'b': ch = '\b'; break;
787 case 'r': ch = '\r'; break;
788 case 'n': ch = '\n'; break;
789 case 't': ch = '\t'; break;
790 case 'f': ch = '\f'; break;
791 case 'v': ch = '\v'; break;
794 /* same procedure as in fteqcc */
796 nextch = lex_getch(lex);
797 if (nextch >= '0' && nextch <= '9')
799 else if (nextch >= 'a' && nextch <= 'f')
800 ch += nextch - 'a' + 10;
801 else if (nextch >= 'A' && nextch <= 'F')
802 ch += nextch - 'A' + 10;
804 lexerror(lex, "bad character code");
805 lex_ungetch(lex, nextch);
806 return (lex->tok.ttype = TOKEN_ERROR);
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);
825 case '0': case '1': case '2': case '3':
826 case '4': case '5': case '6': case '7':
830 case '<': ch = 29; break;
831 case '-': ch = 30; break;
832 case '>': ch = 31; break;
833 case '[': ch = 16; break;
834 case ']': ch = 17; break;
837 nextch = lex_getch(lex);
838 hex = (nextch == 'x');
840 lex_ungetch(lex, nextch);
841 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
843 if (nextch >= '0' && nextch <= '9')
844 chr = chr * 10 + nextch - '0';
846 lexerror(lex, "bad character code");
847 return (lex->tok.ttype = TOKEN_ERROR);
850 if (nextch >= '0' && nextch <= '9')
851 chr = chr * 0x10 + nextch - '0';
852 else if (nextch >= 'a' && nextch <= 'f')
853 chr = chr * 0x10 + nextch - 'a' + 10;
854 else if (nextch >= 'A' && nextch <= 'F')
855 chr = chr * 0x10 + nextch - 'A' + 10;
857 lexerror(lex, "bad character code");
858 return (lex->tok.ttype = TOKEN_ERROR);
861 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
863 lexerror(lex, "character code out of range");
864 return (lex->tok.ttype = TOKEN_ERROR);
867 if (OPTS_FLAG(UTF8) && chr >= 128) {
868 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
873 for (uc = 0; uc < u8len; ++uc)
874 lex_tokench(lex, u8buf[uc]);
875 /* the last character will be inserted with the tokench() call
884 case '\n': ch = '\n'; break;
887 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
888 /* so we just add the character plus backslash no matter what it actually is */
889 lex_tokench(lex, '\\');
891 /* add the character finally */
892 lex_tokench(lex, ch);
895 lex_tokench(lex, ch);
897 lexerror(lex, "unexpected end of file within string constant");
898 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
899 return (lex->tok.ttype = TOKEN_ERROR);
902 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
908 /* parse a number... */
910 lex->tok.ttype = TOKEN_FLOATCONST;
912 lex->tok.ttype = TOKEN_INTCONST;
914 lex_tokench(lex, ch);
917 if (ch != '.' && !isdigit(ch))
919 if (lastch != '0' || ch != 'x')
921 /* end of the number or EOF */
922 lex_ungetch(lex, ch);
925 lex->tok.constval.i = lastch - '0';
926 return lex->tok.ttype;
932 /* EOF would have been caught above */
936 lex_tokench(lex, ch);
938 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
940 lex_tokench(lex, ch);
944 /* NOT else, '.' can come from above as well */
945 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
947 /* Allow floating comma in non-hex mode */
948 lex->tok.ttype = TOKEN_FLOATCONST;
949 lex_tokench(lex, ch);
951 /* continue digits-only */
955 lex_tokench(lex, ch);
959 /* put back the last character */
960 /* but do not put back the trailing 'f' or a float */
961 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
964 /* generally we don't want words to follow numbers: */
966 lexerror(lex, "unexpected trailing characters after number");
967 return (lex->tok.ttype = TOKEN_ERROR);
969 lex_ungetch(lex, ch);
972 if (lex->tok.ttype == TOKEN_FLOATCONST)
973 lex->tok.constval.f = strtod(lex->tok.value, NULL);
975 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
976 return lex->tok.ttype;
979 int lex_do(lex_file *lex)
981 int ch, nextch, thirdch;
982 bool hadwhite = false;
991 ch = lex_skipwhite(lex, hadwhite);
993 if (!lex->flags.mergelines || ch != '\\')
999 lex_ungetch(lex, ch);
1003 /* we reached a linemerge */
1004 lex_tokench(lex, '\n');
1008 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1009 return (lex->tok.ttype = ch);
1012 lex->sline = lex->line;
1013 lex->tok.ctx.line = lex->sline;
1014 lex->tok.ctx.file = lex->name;
1017 return (lex->tok.ttype = TOKEN_FATAL);
1021 return (lex->tok.ttype = TOKEN_EOF);
1024 /* modelgen / spiritgen commands */
1025 if (ch == '$' && !lex->flags.preprocessing) {
1029 ch = lex_getch(lex);
1030 if (!isident_start(ch)) {
1031 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1034 lex_tokench(lex, ch);
1035 if (!lex_finish_ident(lex))
1036 return (lex->tok.ttype = TOKEN_ERROR);
1038 /* skip the known commands */
1041 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1043 /* frame/framesave command works like an enum
1044 * similar to fteqcc we handle this in the lexer.
1045 * The reason for this is that it is sensitive to newlines,
1046 * which the parser is unaware of
1048 if (!lex_finish_frames(lex))
1049 return (lex->tok.ttype = TOKEN_ERROR);
1053 if (!strcmp(v, "framevalue"))
1055 ch = lex_getch(lex);
1056 while (ch != EOF && isspace(ch) && ch != '\n')
1057 ch = lex_getch(lex);
1060 lexerror(lex, "$framevalue requires an integer parameter");
1065 lex->tok.ttype = lex_finish_digit(lex, ch);
1067 if (lex->tok.ttype != TOKEN_INTCONST) {
1068 lexerror(lex, "$framevalue requires an integer parameter");
1071 lex->framevalue = lex->tok.constval.i;
1075 if (!strcmp(v, "framerestore"))
1081 rc = lex_parse_frame(lex);
1084 lexerror(lex, "$framerestore requires a framename parameter");
1088 return (lex->tok.ttype = TOKEN_FATAL);
1091 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1092 if (!strcmp(v, lex->frames[frame].name)) {
1093 lex->framevalue = lex->frames[frame].value;
1097 lexerror(lex, "unknown framename `%s`", v);
1101 if (!strcmp(v, "modelname"))
1107 rc = lex_parse_frame(lex);
1110 lexerror(lex, "$modelname requires a parameter");
1114 return (lex->tok.ttype = TOKEN_FATAL);
1116 if (lex->modelname) {
1118 m.value = lex->framevalue;
1119 m.name = lex->modelname;
1120 lex->modelname = NULL;
1121 vec_push(lex->frames, m);
1123 lex->modelname = lex->tok.value;
1124 lex->tok.value = NULL;
1128 if (!strcmp(v, "flush"))
1131 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1132 mem_d(lex->frames[fi].name);
1133 vec_free(lex->frames);
1134 /* skip line (fteqcc does it too) */
1135 ch = lex_getch(lex);
1136 while (ch != EOF && ch != '\n')
1137 ch = lex_getch(lex);
1141 if (!strcmp(v, "cd") ||
1142 !strcmp(v, "origin") ||
1143 !strcmp(v, "base") ||
1144 !strcmp(v, "flags") ||
1145 !strcmp(v, "scale") ||
1149 ch = lex_getch(lex);
1150 while (ch != EOF && ch != '\n')
1151 ch = lex_getch(lex);
1155 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1156 if (!strcmp(v, lex->frames[frame].name)) {
1157 lex->tok.constval.i = lex->frames[frame].value;
1158 return (lex->tok.ttype = TOKEN_INTCONST);
1162 lexerror(lex, "invalid frame macro");
1166 /* single-character tokens */
1170 nextch = lex_getch(lex);
1171 if (nextch == '[') {
1172 lex_tokench(lex, ch);
1173 lex_tokench(lex, nextch);
1175 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1177 lex_ungetch(lex, nextch);
1182 lex_tokench(lex, ch);
1184 if (lex->flags.noops)
1185 return (lex->tok.ttype = ch);
1187 return (lex->tok.ttype = TOKEN_OPERATOR);
1190 if (lex->flags.noops) {
1191 nextch = lex_getch(lex);
1192 if (nextch == ']') {
1193 lex_tokench(lex, ch);
1194 lex_tokench(lex, nextch);
1196 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1198 lex_ungetch(lex, nextch);
1207 lex_tokench(lex, ch);
1209 return (lex->tok.ttype = ch);
1215 nextch = lex_getch(lex);
1216 /* digits starting with a dot */
1217 if (isdigit(nextch)) {
1218 lex_ungetch(lex, nextch);
1219 lex->tok.ttype = lex_finish_digit(lex, ch);
1221 return lex->tok.ttype;
1223 lex_ungetch(lex, nextch);
1226 if (lex->flags.noops)
1228 /* Detect characters early which are normally
1229 * operators OR PART of an operator.
1248 lex_tokench(lex, ch);
1250 return (lex->tok.ttype = ch);
1258 lex_tokench(lex, ch);
1259 /* peak ahead once */
1260 nextch = lex_getch(lex);
1261 if (nextch != '.') {
1262 lex_ungetch(lex, nextch);
1264 if (lex->flags.noops)
1265 return (lex->tok.ttype = ch);
1267 return (lex->tok.ttype = TOKEN_OPERATOR);
1269 /* peak ahead again */
1270 nextch = lex_getch(lex);
1271 if (nextch != '.') {
1272 lex_ungetch(lex, nextch);
1273 lex_ungetch(lex, '.');
1275 if (lex->flags.noops)
1276 return (lex->tok.ttype = ch);
1278 return (lex->tok.ttype = TOKEN_OPERATOR);
1280 /* fill the token to be "..." */
1281 lex_tokench(lex, ch);
1282 lex_tokench(lex, ch);
1284 return (lex->tok.ttype = TOKEN_DOTS);
1287 if (ch == ',' || ch == '.') {
1288 lex_tokench(lex, ch);
1290 return (lex->tok.ttype = TOKEN_OPERATOR);
1293 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1294 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1295 ch == '=' || ch == '!' || /* <=>, ==, != */
1296 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1297 ch == '~' /* ~=, ~ */
1299 lex_tokench(lex, ch);
1301 nextch = lex_getch(lex);
1302 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1303 lex_tokench(lex, nextch);
1304 } else if (ch == '<' && nextch == '=') {
1305 lex_tokench(lex, nextch);
1306 if ((thirdch = lex_getch(lex)) == '>')
1307 lex_tokench(lex, thirdch);
1309 lex_ungetch(lex, thirdch);
1311 } else if (ch == '-' && nextch == '>') {
1312 lex_tokench(lex, nextch);
1313 } else if (ch == '&' && nextch == '~') {
1314 thirdch = lex_getch(lex);
1315 if (thirdch != '=') {
1316 lex_ungetch(lex, thirdch);
1317 lex_ungetch(lex, nextch);
1320 lex_tokench(lex, nextch);
1321 lex_tokench(lex, thirdch);
1324 else if (lex->flags.preprocessing &&
1325 ch == '-' && isdigit(nextch))
1327 lex->tok.ttype = lex_finish_digit(lex, nextch);
1328 if (lex->tok.ttype == TOKEN_INTCONST)
1329 lex->tok.constval.i = -lex->tok.constval.i;
1331 lex->tok.constval.f = -lex->tok.constval.f;
1333 return lex->tok.ttype;
1335 lex_ungetch(lex, nextch);
1339 return (lex->tok.ttype = TOKEN_OPERATOR);
1343 if (ch == '^' || ch == '~' || ch == '!')
1345 lex_tokench(lex, ch);
1347 return (lex->tok.ttype = TOKEN_OPERATOR);
1351 if (ch == '*' || ch == '/') /* *=, /= */
1353 lex_tokench(lex, ch);
1355 nextch = lex_getch(lex);
1356 if (nextch == '=' || nextch == '*') {
1357 lex_tokench(lex, nextch);
1359 lex_ungetch(lex, nextch);
1362 return (lex->tok.ttype = TOKEN_OPERATOR);
1366 lex_tokench(lex, ch);
1368 return (lex->tok.ttype = TOKEN_OPERATOR);
1371 if (isident_start(ch))
1375 lex_tokench(lex, ch);
1376 if (!lex_finish_ident(lex)) {
1378 return (lex->tok.ttype = TOKEN_ERROR);
1381 lex->tok.ttype = TOKEN_IDENT;
1384 if (!strcmp(v, "void")) {
1385 lex->tok.ttype = TOKEN_TYPENAME;
1386 lex->tok.constval.t = TYPE_VOID;
1387 } else if (!strcmp(v, "int")) {
1388 lex->tok.ttype = TOKEN_TYPENAME;
1389 lex->tok.constval.t = TYPE_INTEGER;
1390 } else if (!strcmp(v, "float")) {
1391 lex->tok.ttype = TOKEN_TYPENAME;
1392 lex->tok.constval.t = TYPE_FLOAT;
1393 } else if (!strcmp(v, "string")) {
1394 lex->tok.ttype = TOKEN_TYPENAME;
1395 lex->tok.constval.t = TYPE_STRING;
1396 } else if (!strcmp(v, "entity")) {
1397 lex->tok.ttype = TOKEN_TYPENAME;
1398 lex->tok.constval.t = TYPE_ENTITY;
1399 } else if (!strcmp(v, "vector")) {
1400 lex->tok.ttype = TOKEN_TYPENAME;
1401 lex->tok.constval.t = TYPE_VECTOR;
1404 for (kw = 0; kw < num_keywords_qc; ++kw) {
1405 if (!strcmp(v, keywords_qc[kw]))
1406 return (lex->tok.ttype = TOKEN_KEYWORD);
1408 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1409 for (kw = 0; kw < num_keywords_fg; ++kw) {
1410 if (!strcmp(v, keywords_fg[kw]))
1411 return (lex->tok.ttype = TOKEN_KEYWORD);
1416 return lex->tok.ttype;
1421 lex->flags.nodigraphs = true;
1422 if (lex->flags.preprocessing)
1423 lex_tokench(lex, ch);
1424 lex->tok.ttype = lex_finish_string(lex, '"');
1425 if (lex->flags.preprocessing)
1426 lex_tokench(lex, ch);
1427 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1429 /* Allow c style "string" "continuation" */
1430 ch = lex_skipwhite(lex, false);
1432 lex_ungetch(lex, ch);
1436 lex->tok.ttype = lex_finish_string(lex, '"');
1438 lex->flags.nodigraphs = false;
1440 return lex->tok.ttype;
1445 /* we parse character constants like string,
1446 * but return TOKEN_CHARCONST, or a vector type if it fits...
1447 * Likewise actual unescaping has to be done by the parser.
1448 * The difference is we don't allow 'char' 'continuation'.
1450 if (lex->flags.preprocessing)
1451 lex_tokench(lex, ch);
1452 lex->tok.ttype = lex_finish_string(lex, '\'');
1453 if (lex->flags.preprocessing)
1454 lex_tokench(lex, ch);
1457 lex->tok.ttype = TOKEN_CHARCONST;
1458 /* It's a vector if we can successfully scan 3 floats */
1460 if (sscanf_s(lex->tok.value, " %f %f %f ",
1461 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1463 if (sscanf(lex->tok.value, " %f %f %f ",
1464 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1468 lex->tok.ttype = TOKEN_VECTORCONST;
1472 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1474 /* check for a valid utf8 character */
1475 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1476 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1477 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1478 : "multibyte character: `%s`" ),
1480 return (lex->tok.ttype = TOKEN_ERROR);
1483 lex->tok.constval.i = u8char;
1486 lex->tok.constval.i = lex->tok.value[0];
1489 return lex->tok.ttype;
1494 lex->tok.ttype = lex_finish_digit(lex, ch);
1496 return lex->tok.ttype;
1499 if (lex->flags.preprocessing) {
1500 lex_tokench(lex, ch);
1502 return (lex->tok.ttype = ch);
1505 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1506 return (lex->tok.ttype = TOKEN_ERROR);