2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 static const char *keywords_qc[] = {
41 static const char *keywords_fg[] = {
42 "switch", "case", "default",
48 "__builtin_debug_printtype"
54 static char* *lex_filenames;
56 static void lexerror(lex_file *lex, const char *fmt, ...)
62 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
64 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
68 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
75 ctx.line = lex->sline;
76 ctx.column = lex->column;
79 r = vcompile_warning(ctx, warntype, fmt, ap);
88 token *tok = (token*)mem_a(sizeof(token));
91 memset(tok, 0, sizeof(*tok));
95 void token_delete(token *self)
97 if (self->next && self->next->prev == self)
98 self->next->prev = self->prev;
99 if (self->prev && self->prev->next == self)
100 self->prev->next = self->next;
101 MEM_VECTOR_CLEAR(self, value);
105 token* token_copy(const token *cp)
107 token* self = token_new();
111 self->value_alloc = cp->value_count + 1;
112 self->value_count = cp->value_count;
113 self->value = (char*)mem_a(self->value_alloc);
118 memcpy(self->value, cp->value, cp->value_count);
119 self->value[self->value_alloc-1] = 0;
123 self->ttype = cp->ttype;
124 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
128 void token_delete_all(token *t)
139 token* token_copy_all(const token *cp)
144 out = cur = token_copy(cp);
150 cur->next = token_copy(cp);
152 token_delete_all(out);
155 cur->next->prev = cur;
162 static void lex_token_new(lex_file *lex)
166 token_delete(lex->tok);
167 lex->tok = token_new();
170 vec_shrinkto(lex->tok.value, 0);
172 lex->tok.constval.t = 0;
173 lex->tok.ctx.line = lex->sline;
174 lex->tok.ctx.file = lex->name;
175 lex->tok.ctx.column = lex->column;
180 lex_file* lex_open(const char *file)
183 FILE *in = fs_file_open(file, "rb");
186 lexerror(NULL, "open failed: '%s'\n", file);
190 lex = (lex_file*)mem_a(sizeof(*lex));
193 lexerror(NULL, "out of memory\n");
197 memset(lex, 0, sizeof(*lex));
200 lex->name = util_strdup(file);
201 lex->line = 1; /* we start counting at 1 */
206 vec_push(lex_filenames, lex->name);
210 lex_file* lex_open_string(const char *str, size_t len, const char *name)
214 lex = (lex_file*)mem_a(sizeof(*lex));
216 lexerror(NULL, "out of memory\n");
220 memset(lex, 0, sizeof(*lex));
223 lex->open_string = str;
224 lex->open_string_length = len;
225 lex->open_string_pos = 0;
227 lex->name = util_strdup(name ? name : "<string-source>");
228 lex->line = 1; /* we start counting at 1 */
233 vec_push(lex_filenames, lex->name);
238 void lex_cleanup(void)
241 for (i = 0; i < vec_size(lex_filenames); ++i)
242 mem_d(lex_filenames[i]);
243 vec_free(lex_filenames);
246 void lex_close(lex_file *lex)
249 for (i = 0; i < vec_size(lex->frames); ++i)
250 mem_d(lex->frames[i].name);
251 vec_free(lex->frames);
254 vec_free(lex->modelname);
257 fs_file_close(lex->file);
260 token_delete(lex->tok);
262 vec_free(lex->tok.value);
264 /* mem_d(lex->name); collected in lex_filenames */
268 static int lex_fgetc(lex_file *lex)
272 return fs_file_getc(lex->file);
274 if (lex->open_string) {
275 if (lex->open_string_pos >= lex->open_string_length)
278 return lex->open_string[lex->open_string_pos++];
283 /* Get or put-back data
284 * The following to functions do NOT understand what kind of data they
286 * The are merely wrapping get/put in order to count line numbers.
288 static void lex_ungetch(lex_file *lex, int ch);
289 static int lex_try_trigraph(lex_file *lex, int old)
293 if (!lex->push_line && c2 == '\n') {
299 lex_ungetch(lex, c2);
304 if (!lex->push_line && c3 == '\n') {
310 case '=': return '#';
311 case '/': return '\\';
312 case '\'': return '^';
313 case '(': return '[';
314 case ')': return ']';
315 case '!': return '|';
316 case '<': return '{';
317 case '>': return '}';
318 case '-': return '~';
320 lex_ungetch(lex, c3);
321 lex_ungetch(lex, c2);
326 static int lex_try_digraph(lex_file *lex, int ch)
330 /* we just used fgetc() so count lines
331 * need to offset a \n the ungetch would recognize
333 if (!lex->push_line && c2 == '\n')
335 if (ch == '<' && c2 == ':')
337 else if (ch == ':' && c2 == '>')
339 else if (ch == '<' && c2 == '%')
341 else if (ch == '%' && c2 == '>')
343 else if (ch == '%' && c2 == ':')
345 lex_ungetch(lex, c2);
349 static int lex_getch(lex_file *lex)
355 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
357 return lex->peek[lex->peekpos];
361 if (!lex->push_line && ch == '\n')
364 return lex_try_trigraph(lex, ch);
365 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
366 return lex_try_digraph(lex, ch);
370 static void lex_ungetch(lex_file *lex, int ch)
372 lex->peek[lex->peekpos++] = ch;
374 if (!lex->push_line && ch == '\n') {
380 /* classify characters
381 * some additions to the is*() functions of ctype.h
384 /* Idents are alphanumberic, but they start with alpha or _ */
385 static bool isident_start(int ch)
387 return util_isalpha(ch) || ch == '_';
390 static bool isident(int ch)
392 return isident_start(ch) || util_isdigit(ch);
395 /* isxdigit_only is used when we already know it's not a digit
396 * and want to see if it's a hex digit anyway.
398 static bool isxdigit_only(int ch)
400 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
403 /* Append a character to the token buffer */
404 static void lex_tokench(lex_file *lex, int ch)
406 vec_push(lex->tok.value, ch);
409 /* Append a trailing null-byte */
410 static void lex_endtoken(lex_file *lex)
412 vec_push(lex->tok.value, 0);
413 vec_shrinkby(lex->tok.value, 1);
416 static bool lex_try_pragma(lex_file *lex)
420 char *command = NULL;
424 if (lex->flags.preprocessing)
431 lex_ungetch(lex, ch);
435 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
436 vec_push(pragma, ch);
439 if (ch != ' ' || strcmp(pragma, "pragma")) {
440 lex_ungetch(lex, ch);
444 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
445 vec_push(command, ch);
446 vec_push(command, 0);
449 lex_ungetch(lex, ch);
453 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
458 lex_ungetch(lex, ch);
462 if (!strcmp(command, "push")) {
463 if (!strcmp(param, "line")) {
465 if (lex->push_line == 1)
471 else if (!strcmp(command, "pop")) {
472 if (!strcmp(param, "line")) {
475 if (lex->push_line == 0)
481 else if (!strcmp(command, "file")) {
482 lex->name = util_strdup(param);
483 vec_push(lex_filenames, lex->name);
485 else if (!strcmp(command, "line")) {
486 line = strtol(param, NULL, 0)-1;
492 while (ch != '\n' && ch != EOF)
502 while (vec_size(command)) {
503 lex_ungetch(lex, (unsigned char)vec_last(command));
507 lex_ungetch(lex, ' ');
511 while (vec_size(param)) {
512 lex_ungetch(lex, (unsigned char)vec_last(param));
516 lex_ungetch(lex, ' ');
520 while (vec_size(pragma)) {
521 lex_ungetch(lex, (unsigned char)vec_last(pragma));
526 lex_ungetch(lex, '#');
532 /* Skip whitespace and comments and return the first
533 * non-white character.
534 * As this makes use of the above getch() ungetch() functions,
535 * we don't need to care at all about line numbering anymore.
537 * In theory, this function should only be used at the beginning
538 * of lexing, or when we *know* the next character is part of the token.
539 * Otherwise, if the parser throws an error, the linenumber may not be
540 * the line of the error, but the line of the next token AFTER the error.
542 * This is currently only problematic when using c-like string-continuation,
543 * since comments and whitespaces are allowed between 2 such strings.
547 "A continuation of the previous string"
548 // This line is skipped
551 * In this case, if the parse decides it didn't actually want a string,
552 * and uses lex->line to print an error, it will show the ', foo);' line's
555 * On the other hand, the parser is supposed to remember the line of the next
556 * token's beginning. In this case we would want skipwhite() to be called
557 * AFTER reading a token, so that the parser, before reading the NEXT token,
558 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
561 * here is to store the line of the first character after skipping
562 * the initial whitespace in lex->sline, this happens in lex_do.
564 static int lex_skipwhite(lex_file *lex, bool hadwhite)
567 bool haswhite = hadwhite;
572 while (ch != EOF && util_isspace(ch)) {
574 if (lex_try_pragma(lex))
577 if (lex->flags.preprocessing) {
580 /* see if there was whitespace first */
581 if (haswhite) { /* (vec_size(lex->tok.value)) { */
582 lex_ungetch(lex, ch);
586 /* otherwise return EOL */
590 lex_tokench(lex, ch);
599 /* one line comment */
602 if (lex->flags.preprocessing) {
605 lex_tokench(lex, '/');
606 lex_tokench(lex, '/');
608 lex_tokench(lex, ' ');
609 lex_tokench(lex, ' ');
612 while (ch != EOF && ch != '\n') {
613 if (lex->flags.preprocessing)
614 lex_tokench(lex, ' '); /* ch); */
617 if (lex->flags.preprocessing) {
618 lex_ungetch(lex, '\n');
626 /* multiline comment */
627 if (lex->flags.preprocessing) {
630 lex_tokench(lex, '/');
631 lex_tokench(lex, '*');
633 lex_tokench(lex, ' ');
634 lex_tokench(lex, ' ');
643 if (lex->flags.preprocessing) {
645 lex_tokench(lex, '*');
646 lex_tokench(lex, '/');
648 lex_tokench(lex, ' ');
649 lex_tokench(lex, ' ');
653 lex_ungetch(lex, ch);
655 if (lex->flags.preprocessing) {
657 lex_tokench(lex, '\n');
659 lex_tokench(lex, ' '); /* ch); */
662 ch = ' '; /* cause TRUE in the isspace check */
665 /* Otherwise roll back to the slash and break out of the loop */
666 lex_ungetch(lex, ch);
670 } while (ch != EOF && util_isspace(ch));
674 lex_ungetch(lex, ch);
681 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
686 while (ch != EOF && isident(ch))
688 lex_tokench(lex, ch);
692 /* last ch was not an ident ch: */
693 lex_ungetch(lex, ch);
698 /* read one ident for the frame list */
699 static int lex_parse_frame(lex_file *lex)
706 while (ch != EOF && ch != '\n' && util_isspace(ch))
712 if (!isident_start(ch)) {
713 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
717 lex_tokench(lex, ch);
718 if (!lex_finish_ident(lex))
724 /* read a list of $frames */
725 static bool lex_finish_frames(lex_file *lex)
732 rc = lex_parse_frame(lex);
733 if (rc > 0) /* end of line */
735 if (rc < 0) /* error */
738 for (i = 0; i < vec_size(lex->frames); ++i) {
739 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
740 lex->frames[i].value = lex->framevalue++;
741 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
746 if (i < vec_size(lex->frames))
749 m.value = lex->framevalue++;
750 m.name = util_strdup(lex->tok.value);
751 vec_shrinkto(lex->tok.value, 0);
752 vec_push(lex->frames, m);
758 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
764 char u8buf[8]; /* way more than enough */
771 return TOKEN_STRINGCONST;
773 if (lex->flags.preprocessing && ch == '\\') {
774 lex_tokench(lex, ch);
777 lexerror(lex, "unexpected end of file");
778 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
779 return (lex->tok.ttype = TOKEN_ERROR);
781 lex_tokench(lex, ch);
783 else if (ch == '\\') {
786 lexerror(lex, "unexpected end of file");
787 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
788 return (lex->tok.ttype = TOKEN_ERROR);
795 case 'a': ch = '\a'; break;
796 case 'b': ch = '\b'; break;
797 case 'r': ch = '\r'; break;
798 case 'n': ch = '\n'; break;
799 case 't': ch = '\t'; break;
800 case 'f': ch = '\f'; break;
801 case 'v': ch = '\v'; break;
804 /* same procedure as in fteqcc */
806 nextch = lex_getch(lex);
807 if (nextch >= '0' && nextch <= '9')
809 else if (nextch >= 'a' && nextch <= 'f')
810 ch += nextch - 'a' + 10;
811 else if (nextch >= 'A' && nextch <= 'F')
812 ch += nextch - 'A' + 10;
814 lexerror(lex, "bad character code");
815 lex_ungetch(lex, nextch);
816 return (lex->tok.ttype = TOKEN_ERROR);
820 nextch = lex_getch(lex);
821 if (nextch >= '0' && nextch <= '9')
823 else if (nextch >= 'a' && nextch <= 'f')
824 ch += nextch - 'a' + 10;
825 else if (nextch >= 'A' && nextch <= 'F')
826 ch += nextch - 'A' + 10;
828 lexerror(lex, "bad character code");
829 lex_ungetch(lex, nextch);
830 return (lex->tok.ttype = TOKEN_ERROR);
835 case '0': case '1': case '2': case '3':
836 case '4': case '5': case '6': case '7':
840 case '<': ch = 29; break;
841 case '-': ch = 30; break;
842 case '>': ch = 31; break;
843 case '[': ch = 16; break;
844 case ']': ch = 17; break;
847 nextch = lex_getch(lex);
848 hex = (nextch == 'x');
850 lex_ungetch(lex, nextch);
851 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
853 if (nextch >= '0' && nextch <= '9')
854 chr = chr * 10 + nextch - '0';
856 lexerror(lex, "bad character code");
857 return (lex->tok.ttype = TOKEN_ERROR);
860 if (nextch >= '0' && nextch <= '9')
861 chr = chr * 0x10 + nextch - '0';
862 else if (nextch >= 'a' && nextch <= 'f')
863 chr = chr * 0x10 + nextch - 'a' + 10;
864 else if (nextch >= 'A' && nextch <= 'F')
865 chr = chr * 0x10 + nextch - 'A' + 10;
867 lexerror(lex, "bad character code");
868 return (lex->tok.ttype = TOKEN_ERROR);
871 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
873 lexerror(lex, "character code out of range");
874 return (lex->tok.ttype = TOKEN_ERROR);
877 if (OPTS_FLAG(UTF8) && chr >= 128) {
878 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
883 lex->column += u8len;
884 for (uc = 0; uc < u8len; ++uc)
885 lex_tokench(lex, u8buf[uc]);
886 /* the last character will be inserted with the tokench() call
895 case '\n': ch = '\n'; break;
898 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
899 /* so we just add the character plus backslash no matter what it actually is */
900 lex_tokench(lex, '\\');
902 /* add the character finally */
903 lex_tokench(lex, ch);
906 lex_tokench(lex, ch);
908 lexerror(lex, "unexpected end of file within string constant");
909 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
910 return (lex->tok.ttype = TOKEN_ERROR);
913 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
919 /* parse a number... */
921 lex->tok.ttype = TOKEN_FLOATCONST;
923 lex->tok.ttype = TOKEN_INTCONST;
925 lex_tokench(lex, ch);
928 if (ch != '.' && !util_isdigit(ch))
930 if (lastch != '0' || ch != 'x')
932 /* end of the number or EOF */
933 lex_ungetch(lex, ch);
936 lex->tok.constval.i = lastch - '0';
937 return lex->tok.ttype;
943 /* EOF would have been caught above */
947 lex_tokench(lex, ch);
949 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
951 lex_tokench(lex, ch);
955 /* NOT else, '.' can come from above as well */
956 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
958 /* Allow floating comma in non-hex mode */
959 lex->tok.ttype = TOKEN_FLOATCONST;
960 lex_tokench(lex, ch);
962 /* continue digits-only */
964 while (util_isdigit(ch))
966 lex_tokench(lex, ch);
970 /* put back the last character */
971 /* but do not put back the trailing 'f' or a float */
972 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
975 /* generally we don't want words to follow numbers: */
977 lexerror(lex, "unexpected trailing characters after number");
978 return (lex->tok.ttype = TOKEN_ERROR);
980 lex_ungetch(lex, ch);
983 if (lex->tok.ttype == TOKEN_FLOATCONST)
984 lex->tok.constval.f = strtod(lex->tok.value, NULL);
986 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
987 return lex->tok.ttype;
990 int lex_do(lex_file *lex)
992 int ch, nextch, thirdch;
993 bool hadwhite = false;
1002 ch = lex_skipwhite(lex, hadwhite);
1004 if (!lex->flags.mergelines || ch != '\\')
1006 ch = lex_getch(lex);
1008 ch = lex_getch(lex);
1010 lex_ungetch(lex, ch);
1014 /* we reached a linemerge */
1015 lex_tokench(lex, '\n');
1019 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1020 return (lex->tok.ttype = ch);
1023 lex->sline = lex->line;
1024 lex->tok.ctx.line = lex->sline;
1025 lex->tok.ctx.file = lex->name;
1028 return (lex->tok.ttype = TOKEN_FATAL);
1032 return (lex->tok.ttype = TOKEN_EOF);
1035 /* modelgen / spiritgen commands */
1036 if (ch == '$' && !lex->flags.preprocessing) {
1040 ch = lex_getch(lex);
1041 if (!isident_start(ch)) {
1042 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1045 lex_tokench(lex, ch);
1046 if (!lex_finish_ident(lex))
1047 return (lex->tok.ttype = TOKEN_ERROR);
1049 /* skip the known commands */
1052 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1054 /* frame/framesave command works like an enum
1055 * similar to fteqcc we handle this in the lexer.
1056 * The reason for this is that it is sensitive to newlines,
1057 * which the parser is unaware of
1059 if (!lex_finish_frames(lex))
1060 return (lex->tok.ttype = TOKEN_ERROR);
1064 if (!strcmp(v, "framevalue"))
1066 ch = lex_getch(lex);
1067 while (ch != EOF && util_isspace(ch) && ch != '\n')
1068 ch = lex_getch(lex);
1070 if (!util_isdigit(ch)) {
1071 lexerror(lex, "$framevalue requires an integer parameter");
1076 lex->tok.ttype = lex_finish_digit(lex, ch);
1078 if (lex->tok.ttype != TOKEN_INTCONST) {
1079 lexerror(lex, "$framevalue requires an integer parameter");
1082 lex->framevalue = lex->tok.constval.i;
1086 if (!strcmp(v, "framerestore"))
1092 rc = lex_parse_frame(lex);
1095 lexerror(lex, "$framerestore requires a framename parameter");
1099 return (lex->tok.ttype = TOKEN_FATAL);
1102 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1103 if (!strcmp(v, lex->frames[frame].name)) {
1104 lex->framevalue = lex->frames[frame].value;
1108 lexerror(lex, "unknown framename `%s`", v);
1112 if (!strcmp(v, "modelname"))
1118 rc = lex_parse_frame(lex);
1121 lexerror(lex, "$modelname requires a parameter");
1125 return (lex->tok.ttype = TOKEN_FATAL);
1127 if (lex->modelname) {
1129 m.value = lex->framevalue;
1130 m.name = lex->modelname;
1131 lex->modelname = NULL;
1132 vec_push(lex->frames, m);
1134 lex->modelname = lex->tok.value;
1135 lex->tok.value = NULL;
1139 if (!strcmp(v, "flush"))
1142 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1143 mem_d(lex->frames[fi].name);
1144 vec_free(lex->frames);
1145 /* skip line (fteqcc does it too) */
1146 ch = lex_getch(lex);
1147 while (ch != EOF && ch != '\n')
1148 ch = lex_getch(lex);
1152 if (!strcmp(v, "cd") ||
1153 !strcmp(v, "origin") ||
1154 !strcmp(v, "base") ||
1155 !strcmp(v, "flags") ||
1156 !strcmp(v, "scale") ||
1160 ch = lex_getch(lex);
1161 while (ch != EOF && ch != '\n')
1162 ch = lex_getch(lex);
1166 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1167 if (!strcmp(v, lex->frames[frame].name)) {
1168 lex->tok.constval.i = lex->frames[frame].value;
1169 return (lex->tok.ttype = TOKEN_INTCONST);
1173 lexerror(lex, "invalid frame macro");
1177 /* single-character tokens */
1181 nextch = lex_getch(lex);
1182 if (nextch == '[') {
1183 lex_tokench(lex, ch);
1184 lex_tokench(lex, nextch);
1186 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1188 lex_ungetch(lex, nextch);
1193 lex_tokench(lex, ch);
1195 if (lex->flags.noops)
1196 return (lex->tok.ttype = ch);
1198 return (lex->tok.ttype = TOKEN_OPERATOR);
1201 if (lex->flags.noops) {
1202 nextch = lex_getch(lex);
1203 if (nextch == ']') {
1204 lex_tokench(lex, ch);
1205 lex_tokench(lex, nextch);
1207 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1209 lex_ungetch(lex, nextch);
1218 lex_tokench(lex, ch);
1220 return (lex->tok.ttype = ch);
1226 nextch = lex_getch(lex);
1227 /* digits starting with a dot */
1228 if (util_isdigit(nextch)) {
1229 lex_ungetch(lex, nextch);
1230 lex->tok.ttype = lex_finish_digit(lex, ch);
1232 return lex->tok.ttype;
1234 lex_ungetch(lex, nextch);
1237 if (lex->flags.noops)
1239 /* Detect characters early which are normally
1240 * operators OR PART of an operator.
1259 lex_tokench(lex, ch);
1261 return (lex->tok.ttype = ch);
1269 lex_tokench(lex, ch);
1270 /* peak ahead once */
1271 nextch = lex_getch(lex);
1272 if (nextch != '.') {
1273 lex_ungetch(lex, nextch);
1275 if (lex->flags.noops)
1276 return (lex->tok.ttype = ch);
1278 return (lex->tok.ttype = TOKEN_OPERATOR);
1280 /* peak ahead again */
1281 nextch = lex_getch(lex);
1282 if (nextch != '.') {
1283 lex_ungetch(lex, nextch);
1284 lex_ungetch(lex, '.');
1286 if (lex->flags.noops)
1287 return (lex->tok.ttype = ch);
1289 return (lex->tok.ttype = TOKEN_OPERATOR);
1291 /* fill the token to be "..." */
1292 lex_tokench(lex, ch);
1293 lex_tokench(lex, ch);
1295 return (lex->tok.ttype = TOKEN_DOTS);
1298 if (ch == ',' || ch == '.') {
1299 lex_tokench(lex, ch);
1301 return (lex->tok.ttype = TOKEN_OPERATOR);
1304 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1305 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1306 ch == '=' || ch == '!' || /* <=>, ==, != */
1307 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1308 ch == '~' || ch == '^' /* ~=, ~, ^ */
1310 lex_tokench(lex, ch);
1312 nextch = lex_getch(lex);
1313 if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
1314 lex_tokench(lex, nextch);
1315 } else if (ch == '<' && nextch == '=') {
1316 lex_tokench(lex, nextch);
1317 if ((thirdch = lex_getch(lex)) == '>')
1318 lex_tokench(lex, thirdch);
1320 lex_ungetch(lex, thirdch);
1322 } else if (ch == '-' && nextch == '>') {
1323 lex_tokench(lex, nextch);
1324 } else if (ch == '&' && nextch == '~') {
1325 thirdch = lex_getch(lex);
1326 if (thirdch != '=') {
1327 lex_ungetch(lex, thirdch);
1328 lex_ungetch(lex, nextch);
1331 lex_tokench(lex, nextch);
1332 lex_tokench(lex, thirdch);
1335 else if (lex->flags.preprocessing &&
1336 ch == '-' && util_isdigit(nextch))
1338 lex->tok.ttype = lex_finish_digit(lex, nextch);
1339 if (lex->tok.ttype == TOKEN_INTCONST)
1340 lex->tok.constval.i = -lex->tok.constval.i;
1342 lex->tok.constval.f = -lex->tok.constval.f;
1344 return lex->tok.ttype;
1346 lex_ungetch(lex, nextch);
1350 return (lex->tok.ttype = TOKEN_OPERATOR);
1354 if (ch == '^' || ch == '~' || ch == '!')
1356 lex_tokench(lex, ch);
1358 return (lex->tok.ttype = TOKEN_OPERATOR);
1362 if (ch == '*' || ch == '/') /* *=, /= */
1364 lex_tokench(lex, ch);
1366 nextch = lex_getch(lex);
1367 if (nextch == '=' || nextch == '*') {
1368 lex_tokench(lex, nextch);
1370 lex_ungetch(lex, nextch);
1373 return (lex->tok.ttype = TOKEN_OPERATOR);
1377 lex_tokench(lex, ch);
1379 return (lex->tok.ttype = TOKEN_OPERATOR);
1382 if (isident_start(ch))
1386 lex_tokench(lex, ch);
1387 if (!lex_finish_ident(lex)) {
1389 return (lex->tok.ttype = TOKEN_ERROR);
1392 lex->tok.ttype = TOKEN_IDENT;
1395 if (!strcmp(v, "void")) {
1396 lex->tok.ttype = TOKEN_TYPENAME;
1397 lex->tok.constval.t = TYPE_VOID;
1398 } else if (!strcmp(v, "int")) {
1399 lex->tok.ttype = TOKEN_TYPENAME;
1400 lex->tok.constval.t = TYPE_INTEGER;
1401 } else if (!strcmp(v, "float")) {
1402 lex->tok.ttype = TOKEN_TYPENAME;
1403 lex->tok.constval.t = TYPE_FLOAT;
1404 } else if (!strcmp(v, "string")) {
1405 lex->tok.ttype = TOKEN_TYPENAME;
1406 lex->tok.constval.t = TYPE_STRING;
1407 } else if (!strcmp(v, "entity")) {
1408 lex->tok.ttype = TOKEN_TYPENAME;
1409 lex->tok.constval.t = TYPE_ENTITY;
1410 } else if (!strcmp(v, "vector")) {
1411 lex->tok.ttype = TOKEN_TYPENAME;
1412 lex->tok.constval.t = TYPE_VECTOR;
1415 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1416 if (!strcmp(v, keywords_qc[kw]))
1417 return (lex->tok.ttype = TOKEN_KEYWORD);
1419 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1420 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1421 if (!strcmp(v, keywords_fg[kw]))
1422 return (lex->tok.ttype = TOKEN_KEYWORD);
1427 return lex->tok.ttype;
1432 lex->flags.nodigraphs = true;
1433 if (lex->flags.preprocessing)
1434 lex_tokench(lex, ch);
1435 lex->tok.ttype = lex_finish_string(lex, '"');
1436 if (lex->flags.preprocessing)
1437 lex_tokench(lex, ch);
1438 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1440 /* Allow c style "string" "continuation" */
1441 ch = lex_skipwhite(lex, false);
1443 lex_ungetch(lex, ch);
1447 lex->tok.ttype = lex_finish_string(lex, '"');
1449 lex->flags.nodigraphs = false;
1451 return lex->tok.ttype;
1456 /* we parse character constants like string,
1457 * but return TOKEN_CHARCONST, or a vector type if it fits...
1458 * Likewise actual unescaping has to be done by the parser.
1459 * The difference is we don't allow 'char' 'continuation'.
1461 if (lex->flags.preprocessing)
1462 lex_tokench(lex, ch);
1463 lex->tok.ttype = lex_finish_string(lex, '\'');
1464 if (lex->flags.preprocessing)
1465 lex_tokench(lex, ch);
1468 lex->tok.ttype = TOKEN_CHARCONST;
1469 /* It's a vector if we can successfully scan 3 floats */
1471 if (sscanf_s(lex->tok.value, " %f %f %f ",
1472 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1474 if (sscanf(lex->tok.value, " %f %f %f ",
1475 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1479 lex->tok.ttype = TOKEN_VECTORCONST;
1483 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1485 /* check for a valid utf8 character */
1486 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1487 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1488 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1489 : "multibyte character: `%s`" ),
1491 return (lex->tok.ttype = TOKEN_ERROR);
1494 lex->tok.constval.i = u8char;
1497 lex->tok.constval.i = lex->tok.value[0];
1500 return lex->tok.ttype;
1503 if (util_isdigit(ch))
1505 lex->tok.ttype = lex_finish_digit(lex, ch);
1507 return lex->tok.ttype;
1510 if (lex->flags.preprocessing) {
1511 lex_tokench(lex, ch);
1513 return (lex->tok.ttype = ch);
1516 lexerror(lex, "unknown token: `%s`", lex->tok.value);
1517 return (lex->tok.ttype = TOKEN_ERROR);