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)
491 while (vec_size(command)) {
492 lex_ungetch(lex, (unsigned char)vec_last(command));
496 lex_ungetch(lex, ' ');
500 while (vec_size(param)) {
501 lex_ungetch(lex, (unsigned char)vec_last(param));
505 lex_ungetch(lex, ' ');
509 while (vec_size(pragma)) {
510 lex_ungetch(lex, (unsigned char)vec_last(pragma));
515 lex_ungetch(lex, '#');
521 /* Skip whitespace and comments and return the first
522 * non-white character.
523 * As this makes use of the above getch() ungetch() functions,
524 * we don't need to care at all about line numbering anymore.
526 * In theory, this function should only be used at the beginning
527 * of lexing, or when we *know* the next character is part of the token.
528 * Otherwise, if the parser throws an error, the linenumber may not be
529 * the line of the error, but the line of the next token AFTER the error.
531 * This is currently only problematic when using c-like string-continuation,
532 * since comments and whitespaces are allowed between 2 such strings.
536 "A continuation of the previous string"
537 // This line is skipped
540 * In this case, if the parse decides it didn't actually want a string,
541 * and uses lex->line to print an error, it will show the ', foo);' line's
544 * On the other hand, the parser is supposed to remember the line of the next
545 * token's beginning. In this case we would want skipwhite() to be called
546 * AFTER reading a token, so that the parser, before reading the NEXT token,
547 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
550 * here is to store the line of the first character after skipping
551 * the initial whitespace in lex->sline, this happens in lex_do.
553 static int lex_skipwhite(lex_file *lex, bool hadwhite)
556 bool haswhite = hadwhite;
561 while (ch != EOF && isspace(ch)) {
563 if (lex_try_pragma(lex))
566 if (lex->flags.preprocessing) {
569 /* see if there was whitespace first */
570 if (haswhite) { /* (vec_size(lex->tok.value)) { */
571 lex_ungetch(lex, ch);
575 /* otherwise return EOL */
579 lex_tokench(lex, ch);
588 /* one line comment */
591 if (lex->flags.preprocessing) {
594 lex_tokench(lex, '/');
595 lex_tokench(lex, '/');
597 lex_tokench(lex, ' ');
598 lex_tokench(lex, ' ');
601 while (ch != EOF && ch != '\n') {
602 if (lex->flags.preprocessing)
603 lex_tokench(lex, ' '); /* ch); */
606 if (lex->flags.preprocessing) {
607 lex_ungetch(lex, '\n');
615 /* multiline comment */
616 if (lex->flags.preprocessing) {
619 lex_tokench(lex, '/');
620 lex_tokench(lex, '*');
622 lex_tokench(lex, ' ');
623 lex_tokench(lex, ' ');
632 if (lex->flags.preprocessing) {
634 lex_tokench(lex, '*');
635 lex_tokench(lex, '/');
637 lex_tokench(lex, ' ');
638 lex_tokench(lex, ' ');
642 lex_ungetch(lex, ch);
644 if (lex->flags.preprocessing) {
646 lex_tokench(lex, '\n');
648 lex_tokench(lex, ' '); /* ch); */
651 ch = ' '; /* cause TRUE in the isspace check */
654 /* Otherwise roll back to the slash and break out of the loop */
655 lex_ungetch(lex, ch);
659 } while (ch != EOF && isspace(ch));
663 lex_ungetch(lex, ch);
670 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
675 while (ch != EOF && isident(ch))
677 lex_tokench(lex, ch);
681 /* last ch was not an ident ch: */
682 lex_ungetch(lex, ch);
687 /* read one ident for the frame list */
688 static int lex_parse_frame(lex_file *lex)
695 while (ch != EOF && ch != '\n' && isspace(ch))
701 if (!isident_start(ch)) {
702 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
706 lex_tokench(lex, ch);
707 if (!lex_finish_ident(lex))
713 /* read a list of $frames */
714 static bool lex_finish_frames(lex_file *lex)
721 rc = lex_parse_frame(lex);
722 if (rc > 0) /* end of line */
724 if (rc < 0) /* error */
727 for (i = 0; i < vec_size(lex->frames); ++i) {
728 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
729 lex->frames[i].value = lex->framevalue++;
730 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
735 if (i < vec_size(lex->frames))
738 m.value = lex->framevalue++;
739 m.name = util_strdup(lex->tok.value);
740 vec_shrinkto(lex->tok.value, 0);
741 vec_push(lex->frames, m);
747 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
753 char u8buf[8]; /* way more than enough */
760 return TOKEN_STRINGCONST;
762 if (lex->flags.preprocessing && ch == '\\') {
763 lex_tokench(lex, ch);
766 lexerror(lex, "unexpected end of file");
767 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
768 return (lex->tok.ttype = TOKEN_ERROR);
770 lex_tokench(lex, ch);
772 else if (ch == '\\') {
775 lexerror(lex, "unexpected end of file");
776 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
777 return (lex->tok.ttype = TOKEN_ERROR);
784 case 'a': ch = '\a'; break;
785 case 'b': ch = '\b'; break;
786 case 'r': ch = '\r'; break;
787 case 'n': ch = '\n'; break;
788 case 't': ch = '\t'; break;
789 case 'f': ch = '\f'; break;
790 case 'v': ch = '\v'; break;
793 /* same procedure as in fteqcc */
795 nextch = lex_getch(lex);
796 if (nextch >= '0' && nextch <= '9')
798 else if (nextch >= 'a' && nextch <= 'f')
799 ch += nextch - 'a' + 10;
800 else if (nextch >= 'A' && nextch <= 'F')
801 ch += nextch - 'A' + 10;
803 lexerror(lex, "bad character code");
804 lex_ungetch(lex, nextch);
805 return (lex->tok.ttype = TOKEN_ERROR);
809 nextch = lex_getch(lex);
810 if (nextch >= '0' && nextch <= '9')
812 else if (nextch >= 'a' && nextch <= 'f')
813 ch += nextch - 'a' + 10;
814 else if (nextch >= 'A' && nextch <= 'F')
815 ch += nextch - 'A' + 10;
817 lexerror(lex, "bad character code");
818 lex_ungetch(lex, nextch);
819 return (lex->tok.ttype = TOKEN_ERROR);
824 case '0': case '1': case '2': case '3':
825 case '4': case '5': case '6': case '7':
829 case '<': ch = 29; break;
830 case '-': ch = 30; break;
831 case '>': ch = 31; break;
832 case '[': ch = 16; break;
833 case ']': ch = 17; break;
836 nextch = lex_getch(lex);
837 hex = (nextch == 'x');
839 lex_ungetch(lex, nextch);
840 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
842 if (nextch >= '0' && nextch <= '9')
843 chr = chr * 10 + nextch - '0';
845 lexerror(lex, "bad character code");
846 return (lex->tok.ttype = TOKEN_ERROR);
849 if (nextch >= '0' && nextch <= '9')
850 chr = chr * 0x10 + nextch - '0';
851 else if (nextch >= 'a' && nextch <= 'f')
852 chr = chr * 0x10 + nextch - 'a' + 10;
853 else if (nextch >= 'A' && nextch <= 'F')
854 chr = chr * 0x10 + nextch - 'A' + 10;
856 lexerror(lex, "bad character code");
857 return (lex->tok.ttype = TOKEN_ERROR);
860 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
862 lexerror(lex, "character code out of range");
863 return (lex->tok.ttype = TOKEN_ERROR);
866 if (OPTS_FLAG(UTF8) && chr >= 128) {
867 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
872 for (uc = 0; uc < u8len; ++uc)
873 lex_tokench(lex, u8buf[uc]);
874 /* the last character will be inserted with the tokench() call
883 case '\n': ch = '\n'; break;
886 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
887 /* so we just add the character plus backslash no matter what it actually is */
888 lex_tokench(lex, '\\');
890 /* add the character finally */
891 lex_tokench(lex, ch);
894 lex_tokench(lex, ch);
896 lexerror(lex, "unexpected end of file within string constant");
897 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
898 return (lex->tok.ttype = TOKEN_ERROR);
901 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
907 /* parse a number... */
909 lex->tok.ttype = TOKEN_FLOATCONST;
911 lex->tok.ttype = TOKEN_INTCONST;
913 lex_tokench(lex, ch);
916 if (ch != '.' && !isdigit(ch))
918 if (lastch != '0' || ch != 'x')
920 /* end of the number or EOF */
921 lex_ungetch(lex, ch);
924 lex->tok.constval.i = lastch - '0';
925 return lex->tok.ttype;
931 /* EOF would have been caught above */
935 lex_tokench(lex, ch);
937 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
939 lex_tokench(lex, ch);
943 /* NOT else, '.' can come from above as well */
944 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
946 /* Allow floating comma in non-hex mode */
947 lex->tok.ttype = TOKEN_FLOATCONST;
948 lex_tokench(lex, ch);
950 /* continue digits-only */
954 lex_tokench(lex, ch);
958 /* put back the last character */
959 /* but do not put back the trailing 'f' or a float */
960 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
963 /* generally we don't want words to follow numbers: */
965 lexerror(lex, "unexpected trailing characters after number");
966 return (lex->tok.ttype = TOKEN_ERROR);
968 lex_ungetch(lex, ch);
971 if (lex->tok.ttype == TOKEN_FLOATCONST)
972 lex->tok.constval.f = strtod(lex->tok.value, NULL);
974 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
975 return lex->tok.ttype;
978 int lex_do(lex_file *lex)
980 int ch, nextch, thirdch;
981 bool hadwhite = false;
990 ch = lex_skipwhite(lex, hadwhite);
992 if (!lex->flags.mergelines || ch != '\\')
998 lex_ungetch(lex, ch);
1002 /* we reached a linemerge */
1003 lex_tokench(lex, '\n');
1007 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1008 return (lex->tok.ttype = ch);
1011 lex->sline = lex->line;
1012 lex->tok.ctx.line = lex->sline;
1013 lex->tok.ctx.file = lex->name;
1016 return (lex->tok.ttype = TOKEN_FATAL);
1020 return (lex->tok.ttype = TOKEN_EOF);
1023 /* modelgen / spiritgen commands */
1024 if (ch == '$' && !lex->flags.preprocessing) {
1028 ch = lex_getch(lex);
1029 if (!isident_start(ch)) {
1030 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1033 lex_tokench(lex, ch);
1034 if (!lex_finish_ident(lex))
1035 return (lex->tok.ttype = TOKEN_ERROR);
1037 /* skip the known commands */
1040 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1042 /* frame/framesave command works like an enum
1043 * similar to fteqcc we handle this in the lexer.
1044 * The reason for this is that it is sensitive to newlines,
1045 * which the parser is unaware of
1047 if (!lex_finish_frames(lex))
1048 return (lex->tok.ttype = TOKEN_ERROR);
1052 if (!strcmp(v, "framevalue"))
1054 ch = lex_getch(lex);
1055 while (ch != EOF && isspace(ch) && ch != '\n')
1056 ch = lex_getch(lex);
1059 lexerror(lex, "$framevalue requires an integer parameter");
1064 lex->tok.ttype = lex_finish_digit(lex, ch);
1066 if (lex->tok.ttype != TOKEN_INTCONST) {
1067 lexerror(lex, "$framevalue requires an integer parameter");
1070 lex->framevalue = lex->tok.constval.i;
1074 if (!strcmp(v, "framerestore"))
1080 rc = lex_parse_frame(lex);
1083 lexerror(lex, "$framerestore requires a framename parameter");
1087 return (lex->tok.ttype = TOKEN_FATAL);
1090 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1091 if (!strcmp(v, lex->frames[frame].name)) {
1092 lex->framevalue = lex->frames[frame].value;
1096 lexerror(lex, "unknown framename `%s`", v);
1100 if (!strcmp(v, "modelname"))
1106 rc = lex_parse_frame(lex);
1109 lexerror(lex, "$modelname requires a parameter");
1113 return (lex->tok.ttype = TOKEN_FATAL);
1115 if (lex->modelname) {
1117 m.value = lex->framevalue;
1118 m.name = lex->modelname;
1119 lex->modelname = NULL;
1120 vec_push(lex->frames, m);
1122 lex->modelname = lex->tok.value;
1123 lex->tok.value = NULL;
1127 if (!strcmp(v, "flush"))
1130 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1131 mem_d(lex->frames[fi].name);
1132 vec_free(lex->frames);
1133 /* skip line (fteqcc does it too) */
1134 ch = lex_getch(lex);
1135 while (ch != EOF && ch != '\n')
1136 ch = lex_getch(lex);
1140 if (!strcmp(v, "cd") ||
1141 !strcmp(v, "origin") ||
1142 !strcmp(v, "base") ||
1143 !strcmp(v, "flags") ||
1144 !strcmp(v, "scale") ||
1148 ch = lex_getch(lex);
1149 while (ch != EOF && ch != '\n')
1150 ch = lex_getch(lex);
1154 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1155 if (!strcmp(v, lex->frames[frame].name)) {
1156 lex->tok.constval.i = lex->frames[frame].value;
1157 return (lex->tok.ttype = TOKEN_INTCONST);
1161 lexerror(lex, "invalid frame macro");
1165 /* single-character tokens */
1169 nextch = lex_getch(lex);
1170 if (nextch == '[') {
1171 lex_tokench(lex, ch);
1172 lex_tokench(lex, nextch);
1174 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1176 lex_ungetch(lex, nextch);
1181 lex_tokench(lex, ch);
1183 if (lex->flags.noops)
1184 return (lex->tok.ttype = ch);
1186 return (lex->tok.ttype = TOKEN_OPERATOR);
1189 if (lex->flags.noops) {
1190 nextch = lex_getch(lex);
1191 if (nextch == ']') {
1192 lex_tokench(lex, ch);
1193 lex_tokench(lex, nextch);
1195 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1197 lex_ungetch(lex, nextch);
1206 lex_tokench(lex, ch);
1208 return (lex->tok.ttype = ch);
1214 nextch = lex_getch(lex);
1215 /* digits starting with a dot */
1216 if (isdigit(nextch)) {
1217 lex_ungetch(lex, nextch);
1218 lex->tok.ttype = lex_finish_digit(lex, ch);
1220 return lex->tok.ttype;
1222 lex_ungetch(lex, nextch);
1225 if (lex->flags.noops)
1227 /* Detect characters early which are normally
1228 * operators OR PART of an operator.
1247 lex_tokench(lex, ch);
1249 return (lex->tok.ttype = ch);
1257 lex_tokench(lex, ch);
1258 /* peak ahead once */
1259 nextch = lex_getch(lex);
1260 if (nextch != '.') {
1261 lex_ungetch(lex, nextch);
1263 if (lex->flags.noops)
1264 return (lex->tok.ttype = ch);
1266 return (lex->tok.ttype = TOKEN_OPERATOR);
1268 /* peak ahead again */
1269 nextch = lex_getch(lex);
1270 if (nextch != '.') {
1271 lex_ungetch(lex, nextch);
1272 lex_ungetch(lex, '.');
1274 if (lex->flags.noops)
1275 return (lex->tok.ttype = ch);
1277 return (lex->tok.ttype = TOKEN_OPERATOR);
1279 /* fill the token to be "..." */
1280 lex_tokench(lex, ch);
1281 lex_tokench(lex, ch);
1283 return (lex->tok.ttype = TOKEN_DOTS);
1286 if (ch == ',' || ch == '.') {
1287 lex_tokench(lex, ch);
1289 return (lex->tok.ttype = TOKEN_OPERATOR);
1292 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1293 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1294 ch == '=' || ch == '!' || /* <=>, ==, != */
1295 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1296 ch == '~' /* ~=, ~ */
1298 lex_tokench(lex, ch);
1300 nextch = lex_getch(lex);
1301 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1302 lex_tokench(lex, nextch);
1303 } else if (ch == '<' && nextch == '=') {
1304 lex_tokench(lex, nextch);
1305 if ((thirdch = lex_getch(lex)) == '>')
1306 lex_tokench(lex, thirdch);
1308 lex_ungetch(lex, thirdch);
1310 } else if (ch == '-' && nextch == '>') {
1311 lex_tokench(lex, nextch);
1312 } else if (ch == '&' && nextch == '~') {
1313 thirdch = lex_getch(lex);
1314 if (thirdch != '=') {
1315 lex_ungetch(lex, thirdch);
1316 lex_ungetch(lex, nextch);
1319 lex_tokench(lex, nextch);
1320 lex_tokench(lex, thirdch);
1323 else if (lex->flags.preprocessing &&
1324 ch == '-' && isdigit(nextch))
1326 lex->tok.ttype = lex_finish_digit(lex, nextch);
1327 if (lex->tok.ttype == TOKEN_INTCONST)
1328 lex->tok.constval.i = -lex->tok.constval.i;
1330 lex->tok.constval.f = -lex->tok.constval.f;
1332 return lex->tok.ttype;
1334 lex_ungetch(lex, nextch);
1338 return (lex->tok.ttype = TOKEN_OPERATOR);
1342 if (ch == '^' || ch == '~' || ch == '!')
1344 lex_tokench(lex, ch);
1346 return (lex->tok.ttype = TOKEN_OPERATOR);
1350 if (ch == '*' || ch == '/') /* *=, /= */
1352 lex_tokench(lex, ch);
1354 nextch = lex_getch(lex);
1355 if (nextch == '=' || nextch == '*') {
1356 lex_tokench(lex, nextch);
1358 lex_ungetch(lex, nextch);
1361 return (lex->tok.ttype = TOKEN_OPERATOR);
1365 lex_tokench(lex, ch);
1367 return (lex->tok.ttype = TOKEN_OPERATOR);
1370 if (isident_start(ch))
1374 lex_tokench(lex, ch);
1375 if (!lex_finish_ident(lex)) {
1377 return (lex->tok.ttype = TOKEN_ERROR);
1380 lex->tok.ttype = TOKEN_IDENT;
1383 if (!strcmp(v, "void")) {
1384 lex->tok.ttype = TOKEN_TYPENAME;
1385 lex->tok.constval.t = TYPE_VOID;
1386 } else if (!strcmp(v, "int")) {
1387 lex->tok.ttype = TOKEN_TYPENAME;
1388 lex->tok.constval.t = TYPE_INTEGER;
1389 } else if (!strcmp(v, "float")) {
1390 lex->tok.ttype = TOKEN_TYPENAME;
1391 lex->tok.constval.t = TYPE_FLOAT;
1392 } else if (!strcmp(v, "string")) {
1393 lex->tok.ttype = TOKEN_TYPENAME;
1394 lex->tok.constval.t = TYPE_STRING;
1395 } else if (!strcmp(v, "entity")) {
1396 lex->tok.ttype = TOKEN_TYPENAME;
1397 lex->tok.constval.t = TYPE_ENTITY;
1398 } else if (!strcmp(v, "vector")) {
1399 lex->tok.ttype = TOKEN_TYPENAME;
1400 lex->tok.constval.t = TYPE_VECTOR;
1403 for (kw = 0; kw < num_keywords_qc; ++kw) {
1404 if (!strcmp(v, keywords_qc[kw]))
1405 return (lex->tok.ttype = TOKEN_KEYWORD);
1407 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1408 for (kw = 0; kw < num_keywords_fg; ++kw) {
1409 if (!strcmp(v, keywords_fg[kw]))
1410 return (lex->tok.ttype = TOKEN_KEYWORD);
1415 return lex->tok.ttype;
1420 lex->flags.nodigraphs = true;
1421 if (lex->flags.preprocessing)
1422 lex_tokench(lex, ch);
1423 lex->tok.ttype = lex_finish_string(lex, '"');
1424 if (lex->flags.preprocessing)
1425 lex_tokench(lex, ch);
1426 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1428 /* Allow c style "string" "continuation" */
1429 ch = lex_skipwhite(lex, false);
1431 lex_ungetch(lex, ch);
1435 lex->tok.ttype = lex_finish_string(lex, '"');
1437 lex->flags.nodigraphs = false;
1439 return lex->tok.ttype;
1444 /* we parse character constants like string,
1445 * but return TOKEN_CHARCONST, or a vector type if it fits...
1446 * Likewise actual unescaping has to be done by the parser.
1447 * The difference is we don't allow 'char' 'continuation'.
1449 if (lex->flags.preprocessing)
1450 lex_tokench(lex, ch);
1451 lex->tok.ttype = lex_finish_string(lex, '\'');
1452 if (lex->flags.preprocessing)
1453 lex_tokench(lex, ch);
1456 lex->tok.ttype = TOKEN_CHARCONST;
1457 /* It's a vector if we can successfully scan 3 floats */
1459 if (sscanf_s(lex->tok.value, " %f %f %f ",
1460 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1462 if (sscanf(lex->tok.value, " %f %f %f ",
1463 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1467 lex->tok.ttype = TOKEN_VECTORCONST;
1471 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1473 /* check for a valid utf8 character */
1474 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1475 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1476 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1477 : "multibyte character: `%s`" ),
1479 return (lex->tok.ttype = TOKEN_ERROR);
1482 lex->tok.constval.i = u8char;
1485 lex->tok.constval.i = lex->tok.value[0];
1488 return lex->tok.ttype;
1493 lex->tok.ttype = lex_finish_digit(lex, ch);
1495 return lex->tok.ttype;
1498 if (lex->flags.preprocessing) {
1499 lex_tokench(lex, ch);
1501 return (lex->tok.ttype = ch);
1504 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1505 return (lex->tok.ttype = TOKEN_ERROR);