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
34 static const char *keywords_qc[] = {
42 static const char *keywords_fg[] = {
43 "switch", "case", "default",
49 "__builtin_debug_printtype"
55 static char* *lex_filenames;
57 static void lexerror(lex_file *lex, const char *fmt, ...)
63 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
65 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
69 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
76 ctx.line = lex->sline;
77 ctx.column = lex->column;
80 r = vcompile_warning(ctx, warntype, fmt, ap);
89 token *tok = (token*)mem_a(sizeof(token));
92 memset(tok, 0, sizeof(*tok));
96 void token_delete(token *self)
98 if (self->next && self->next->prev == self)
99 self->next->prev = self->prev;
100 if (self->prev && self->prev->next == self)
101 self->prev->next = self->next;
102 MEM_VECTOR_CLEAR(self, value);
106 token* token_copy(const token *cp)
108 token* self = token_new();
112 self->value_alloc = cp->value_count + 1;
113 self->value_count = cp->value_count;
114 self->value = (char*)mem_a(self->value_alloc);
119 memcpy(self->value, cp->value, cp->value_count);
120 self->value[self->value_alloc-1] = 0;
124 self->ttype = cp->ttype;
125 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
129 void token_delete_all(token *t)
140 token* token_copy_all(const token *cp)
145 out = cur = token_copy(cp);
151 cur->next = token_copy(cp);
153 token_delete_all(out);
156 cur->next->prev = cur;
163 static void lex_token_new(lex_file *lex)
167 token_delete(lex->tok);
168 lex->tok = token_new();
171 vec_shrinkto(lex->tok.value, 0);
173 lex->tok.constval.t = 0;
174 lex->tok.ctx.line = lex->sline;
175 lex->tok.ctx.file = lex->name;
176 lex->tok.ctx.column = lex->column;
181 lex_file* lex_open(const char *file)
184 fs_file_t *in = fs_file_open(file, "rb");
187 lexerror(NULL, "open failed: '%s'\n", file);
191 lex = (lex_file*)mem_a(sizeof(*lex));
194 lexerror(NULL, "out of memory\n");
198 memset(lex, 0, sizeof(*lex));
201 lex->name = util_strdup(file);
202 lex->line = 1; /* we start counting at 1 */
207 vec_push(lex_filenames, lex->name);
211 lex_file* lex_open_string(const char *str, size_t len, const char *name)
215 lex = (lex_file*)mem_a(sizeof(*lex));
217 lexerror(NULL, "out of memory\n");
221 memset(lex, 0, sizeof(*lex));
224 lex->open_string = str;
225 lex->open_string_length = len;
226 lex->open_string_pos = 0;
228 lex->name = util_strdup(name ? name : "<string-source>");
229 lex->line = 1; /* we start counting at 1 */
234 vec_push(lex_filenames, lex->name);
239 void lex_cleanup(void)
242 for (i = 0; i < vec_size(lex_filenames); ++i)
243 mem_d(lex_filenames[i]);
244 vec_free(lex_filenames);
247 void lex_close(lex_file *lex)
250 for (i = 0; i < vec_size(lex->frames); ++i)
251 mem_d(lex->frames[i].name);
252 vec_free(lex->frames);
255 vec_free(lex->modelname);
258 fs_file_close(lex->file);
261 token_delete(lex->tok);
263 vec_free(lex->tok.value);
265 /* mem_d(lex->name); collected in lex_filenames */
269 static int lex_fgetc(lex_file *lex)
273 return fs_file_getc(lex->file);
275 if (lex->open_string) {
276 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') {
300 lex_ungetch(lex, c2);
305 if (!lex->push_line && c3 == '\n') {
311 case '=': return '#';
312 case '/': return '\\';
313 case '\'': return '^';
314 case '(': return '[';
315 case ')': return ']';
316 case '!': return '|';
317 case '<': return '{';
318 case '>': return '}';
319 case '-': return '~';
321 lex_ungetch(lex, c3);
322 lex_ungetch(lex, c2);
327 static int lex_try_digraph(lex_file *lex, int ch)
331 /* we just used fgetc() so count lines
332 * need to offset a \n the ungetch would recognize
334 if (!lex->push_line && c2 == '\n')
336 if (ch == '<' && c2 == ':')
338 else if (ch == ':' && c2 == '>')
340 else if (ch == '<' && c2 == '%')
342 else if (ch == '%' && c2 == '>')
344 else if (ch == '%' && c2 == ':')
346 lex_ungetch(lex, c2);
350 static int lex_getch(lex_file *lex)
356 if (!lex->push_line && lex->peek[lex->peekpos] == '\n') {
360 return lex->peek[lex->peekpos];
364 if (!lex->push_line && ch == '\n') {
369 return lex_try_trigraph(lex, ch);
370 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
371 return lex_try_digraph(lex, ch);
375 static void lex_ungetch(lex_file *lex, int ch)
377 lex->peek[lex->peekpos++] = ch;
379 if (!lex->push_line && ch == '\n') {
385 /* classify characters
386 * some additions to the is*() functions of ctype.h
389 /* Idents are alphanumberic, but they start with alpha or _ */
390 static bool isident_start(int ch)
392 return util_isalpha(ch) || ch == '_';
395 static bool isident(int ch)
397 return isident_start(ch) || util_isdigit(ch);
400 /* isxdigit_only is used when we already know it's not a digit
401 * and want to see if it's a hex digit anyway.
403 static bool isxdigit_only(int ch)
405 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
408 /* Append a character to the token buffer */
409 static void lex_tokench(lex_file *lex, int ch)
411 vec_push(lex->tok.value, ch);
414 /* Append a trailing null-byte */
415 static void lex_endtoken(lex_file *lex)
417 vec_push(lex->tok.value, 0);
418 vec_shrinkby(lex->tok.value, 1);
421 static bool lex_try_pragma(lex_file *lex)
425 char *command = NULL;
429 if (lex->flags.preprocessing)
436 lex_ungetch(lex, ch);
440 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
441 vec_push(pragma, ch);
444 if (ch != ' ' || strcmp(pragma, "pragma")) {
445 lex_ungetch(lex, ch);
449 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
450 vec_push(command, ch);
451 vec_push(command, 0);
454 lex_ungetch(lex, ch);
458 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
463 lex_ungetch(lex, ch);
467 if (!strcmp(command, "push")) {
468 if (!strcmp(param, "line")) {
470 if (lex->push_line == 1)
476 else if (!strcmp(command, "pop")) {
477 if (!strcmp(param, "line")) {
480 if (lex->push_line == 0)
486 else if (!strcmp(command, "file")) {
487 lex->name = util_strdup(param);
488 vec_push(lex_filenames, lex->name);
490 else if (!strcmp(command, "line")) {
491 line = strtol(param, NULL, 0)-1;
497 while (ch != '\n' && ch != FS_FILE_EOF)
507 while (vec_size(command)) {
508 lex_ungetch(lex, (unsigned char)vec_last(command));
512 lex_ungetch(lex, ' ');
516 while (vec_size(param)) {
517 lex_ungetch(lex, (unsigned char)vec_last(param));
521 lex_ungetch(lex, ' ');
525 while (vec_size(pragma)) {
526 lex_ungetch(lex, (unsigned char)vec_last(pragma));
531 lex_ungetch(lex, '#');
537 /* Skip whitespace and comments and return the first
538 * non-white character.
539 * As this makes use of the above getch() ungetch() functions,
540 * we don't need to care at all about line numbering anymore.
542 * In theory, this function should only be used at the beginning
543 * of lexing, or when we *know* the next character is part of the token.
544 * Otherwise, if the parser throws an error, the linenumber may not be
545 * the line of the error, but the line of the next token AFTER the error.
547 * This is currently only problematic when using c-like string-continuation,
548 * since comments and whitespaces are allowed between 2 such strings.
552 "A continuation of the previous string"
553 // This line is skipped
556 * In this case, if the parse decides it didn't actually want a string,
557 * and uses lex->line to print an error, it will show the ', foo);' line's
560 * On the other hand, the parser is supposed to remember the line of the next
561 * token's beginning. In this case we would want skipwhite() to be called
562 * AFTER reading a token, so that the parser, before reading the NEXT token,
563 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
566 * here is to store the line of the first character after skipping
567 * the initial whitespace in lex->sline, this happens in lex_do.
569 static int lex_skipwhite(lex_file *lex, bool hadwhite)
572 bool haswhite = hadwhite;
577 while (ch != FS_FILE_EOF && util_isspace(ch)) {
579 if (lex_try_pragma(lex))
582 if (lex->flags.preprocessing) {
585 /* see if there was whitespace first */
586 if (haswhite) { /* (vec_size(lex->tok.value)) { */
587 lex_ungetch(lex, ch);
591 /* otherwise return EOL */
595 lex_tokench(lex, ch);
604 /* one line comment */
607 if (lex->flags.preprocessing) {
610 lex_tokench(lex, '/');
611 lex_tokench(lex, '/');
613 lex_tokench(lex, ' ');
614 lex_tokench(lex, ' ');
617 while (ch != FS_FILE_EOF && ch != '\n') {
618 if (lex->flags.preprocessing)
619 lex_tokench(lex, ' '); /* ch); */
622 if (lex->flags.preprocessing) {
623 lex_ungetch(lex, '\n');
631 /* multiline comment */
632 if (lex->flags.preprocessing) {
635 lex_tokench(lex, '/');
636 lex_tokench(lex, '*');
638 lex_tokench(lex, ' ');
639 lex_tokench(lex, ' ');
642 while (ch != FS_FILE_EOF)
648 if (lex->flags.preprocessing) {
650 lex_tokench(lex, '*');
651 lex_tokench(lex, '/');
653 lex_tokench(lex, ' ');
654 lex_tokench(lex, ' ');
658 lex_ungetch(lex, ch);
660 if (lex->flags.preprocessing) {
662 lex_tokench(lex, '\n');
664 lex_tokench(lex, ' '); /* ch); */
667 ch = ' '; /* cause TRUE in the isspace check */
670 /* Otherwise roll back to the slash and break out of the loop */
671 lex_ungetch(lex, ch);
675 } while (ch != FS_FILE_EOF && util_isspace(ch));
679 lex_ungetch(lex, ch);
686 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
691 while (ch != FS_FILE_EOF && isident(ch))
693 lex_tokench(lex, ch);
697 /* last ch was not an ident ch: */
698 lex_ungetch(lex, ch);
703 /* read one ident for the frame list */
704 static int lex_parse_frame(lex_file *lex)
711 while (ch != FS_FILE_EOF && ch != '\n' && util_isspace(ch))
717 if (!isident_start(ch)) {
718 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
722 lex_tokench(lex, ch);
723 if (!lex_finish_ident(lex))
729 /* read a list of $frames */
730 static bool lex_finish_frames(lex_file *lex)
737 rc = lex_parse_frame(lex);
738 if (rc > 0) /* end of line */
740 if (rc < 0) /* error */
743 for (i = 0; i < vec_size(lex->frames); ++i) {
744 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
745 lex->frames[i].value = lex->framevalue++;
746 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
751 if (i < vec_size(lex->frames))
754 m.value = lex->framevalue++;
755 m.name = util_strdup(lex->tok.value);
756 vec_shrinkto(lex->tok.value, 0);
757 vec_push(lex->frames, m);
763 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
770 char u8buf[8]; /* way more than enough */
773 while (ch != FS_FILE_EOF)
777 return TOKEN_STRINGCONST;
779 if (lex->flags.preprocessing && ch == '\\') {
780 lex_tokench(lex, ch);
782 if (ch == FS_FILE_EOF) {
783 lexerror(lex, "unexpected end of file");
784 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
785 return (lex->tok.ttype = TOKEN_ERROR);
787 lex_tokench(lex, ch);
789 else if (ch == '\\') {
791 if (ch == FS_FILE_EOF) {
792 lexerror(lex, "unexpected end of file");
793 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
794 return (lex->tok.ttype = TOKEN_ERROR);
801 case 'a': ch = '\a'; break;
802 case 'b': ch = '\b'; break;
803 case 'r': ch = '\r'; break;
804 case 'n': ch = '\n'; break;
805 case 't': ch = '\t'; break;
806 case 'f': ch = '\f'; break;
807 case 'v': ch = '\v'; break;
810 /* same procedure as in fteqcc */
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);
826 nextch = lex_getch(lex);
827 if (nextch >= '0' && nextch <= '9')
829 else if (nextch >= 'a' && nextch <= 'f')
830 ch += nextch - 'a' + 10;
831 else if (nextch >= 'A' && nextch <= 'F')
832 ch += nextch - 'A' + 10;
834 lexerror(lex, "bad character code");
835 lex_ungetch(lex, nextch);
836 return (lex->tok.ttype = TOKEN_ERROR);
841 case '0': case '1': case '2': case '3':
842 case '4': case '5': case '6': case '7':
846 case '<': ch = 29; break;
847 case '-': ch = 30; break;
848 case '>': ch = 31; break;
849 case '[': ch = 16; break;
850 case ']': ch = 17; break;
853 nextch = lex_getch(lex);
854 hex = (nextch == 'x');
855 oct = (nextch == '0');
857 lex_ungetch(lex, nextch);
858 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
860 if (nextch >= '0' && nextch <= '9')
861 chr = chr * 10 + nextch - '0';
863 lexerror(lex, "bad character code");
864 return (lex->tok.ttype = TOKEN_ERROR);
867 if (nextch >= '0' && nextch <= '9')
868 chr = chr * 0x10 + nextch - '0';
869 else if (nextch >= 'a' && nextch <= 'f')
870 chr = chr * 0x10 + nextch - 'a' + 10;
871 else if (nextch >= 'A' && nextch <= 'F')
872 chr = chr * 0x10 + nextch - 'A' + 10;
874 lexerror(lex, "bad character code");
875 return (lex->tok.ttype = TOKEN_ERROR);
878 if (nextch >= '0' && nextch <= '9')
879 chr = chr * 8 + chr - '0';
881 lexerror(lex, "bad character code");
882 return (lex->tok.ttype = TOKEN_ERROR);
885 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
887 lexerror(lex, "character code out of range");
888 return (lex->tok.ttype = TOKEN_ERROR);
891 if (OPTS_FLAG(UTF8) && chr >= 128) {
892 u8len = utf8_from(u8buf, chr);
897 lex->column += u8len;
898 for (uc = 0; uc < u8len; ++uc)
899 lex_tokench(lex, u8buf[uc]);
901 * the last character will be inserted with the tokench() call
910 case '\n': ch = '\n'; break;
913 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
914 /* so we just add the character plus backslash no matter what it actually is */
915 lex_tokench(lex, '\\');
917 /* add the character finally */
918 lex_tokench(lex, ch);
921 lex_tokench(lex, ch);
923 lexerror(lex, "unexpected end of file within string constant");
924 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
925 return (lex->tok.ttype = TOKEN_ERROR);
928 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
934 /* parse a number... */
936 lex->tok.ttype = TOKEN_FLOATCONST;
938 lex->tok.ttype = TOKEN_INTCONST;
940 lex_tokench(lex, ch);
943 if (ch != '.' && !util_isdigit(ch))
945 if (lastch != '0' || ch != 'x')
947 /* end of the number or EOF */
948 lex_ungetch(lex, ch);
951 lex->tok.constval.i = lastch - '0';
952 return lex->tok.ttype;
958 /* EOF would have been caught above */
962 lex_tokench(lex, ch);
964 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
966 lex_tokench(lex, ch);
970 /* NOT else, '.' can come from above as well */
971 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
973 /* Allow floating comma in non-hex mode */
974 lex->tok.ttype = TOKEN_FLOATCONST;
975 lex_tokench(lex, ch);
977 /* continue digits-only */
979 while (util_isdigit(ch))
981 lex_tokench(lex, ch);
985 /* put back the last character */
986 /* but do not put back the trailing 'f' or a float */
987 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
990 /* generally we don't want words to follow numbers: */
992 lexerror(lex, "unexpected trailing characters after number");
993 return (lex->tok.ttype = TOKEN_ERROR);
995 lex_ungetch(lex, ch);
998 if (lex->tok.ttype == TOKEN_FLOATCONST)
999 lex->tok.constval.f = strtod(lex->tok.value, NULL);
1001 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
1002 return lex->tok.ttype;
1005 int lex_do(lex_file *lex)
1007 int ch, nextch, thirdch;
1008 bool hadwhite = false;
1017 ch = lex_skipwhite(lex, hadwhite);
1019 if (!lex->flags.mergelines || ch != '\\')
1021 ch = lex_getch(lex);
1023 ch = lex_getch(lex);
1025 lex_ungetch(lex, ch);
1029 /* we reached a linemerge */
1030 lex_tokench(lex, '\n');
1034 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1035 return (lex->tok.ttype = ch);
1038 lex->sline = lex->line;
1039 lex->tok.ctx.line = lex->sline;
1040 lex->tok.ctx.file = lex->name;
1043 return (lex->tok.ttype = TOKEN_FATAL);
1045 if (ch == FS_FILE_EOF) {
1047 return (lex->tok.ttype = TOKEN_EOF);
1050 /* modelgen / spiritgen commands */
1051 if (ch == '$' && !lex->flags.preprocessing) {
1055 ch = lex_getch(lex);
1056 if (!isident_start(ch)) {
1057 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1060 lex_tokench(lex, ch);
1061 if (!lex_finish_ident(lex))
1062 return (lex->tok.ttype = TOKEN_ERROR);
1064 /* skip the known commands */
1067 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1069 /* frame/framesave command works like an enum
1070 * similar to fteqcc we handle this in the lexer.
1071 * The reason for this is that it is sensitive to newlines,
1072 * which the parser is unaware of
1074 if (!lex_finish_frames(lex))
1075 return (lex->tok.ttype = TOKEN_ERROR);
1079 if (!strcmp(v, "framevalue"))
1081 ch = lex_getch(lex);
1082 while (ch != FS_FILE_EOF && util_isspace(ch) && ch != '\n')
1083 ch = lex_getch(lex);
1085 if (!util_isdigit(ch)) {
1086 lexerror(lex, "$framevalue requires an integer parameter");
1091 lex->tok.ttype = lex_finish_digit(lex, ch);
1093 if (lex->tok.ttype != TOKEN_INTCONST) {
1094 lexerror(lex, "$framevalue requires an integer parameter");
1097 lex->framevalue = lex->tok.constval.i;
1101 if (!strcmp(v, "framerestore"))
1107 rc = lex_parse_frame(lex);
1110 lexerror(lex, "$framerestore requires a framename parameter");
1114 return (lex->tok.ttype = TOKEN_FATAL);
1117 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1118 if (!strcmp(v, lex->frames[frame].name)) {
1119 lex->framevalue = lex->frames[frame].value;
1123 lexerror(lex, "unknown framename `%s`", v);
1127 if (!strcmp(v, "modelname"))
1133 rc = lex_parse_frame(lex);
1136 lexerror(lex, "$modelname requires a parameter");
1140 return (lex->tok.ttype = TOKEN_FATAL);
1142 if (lex->modelname) {
1144 m.value = lex->framevalue;
1145 m.name = lex->modelname;
1146 lex->modelname = NULL;
1147 vec_push(lex->frames, m);
1149 lex->modelname = lex->tok.value;
1150 lex->tok.value = NULL;
1154 if (!strcmp(v, "flush"))
1157 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1158 mem_d(lex->frames[fi].name);
1159 vec_free(lex->frames);
1160 /* skip line (fteqcc does it too) */
1161 ch = lex_getch(lex);
1162 while (ch != FS_FILE_EOF && ch != '\n')
1163 ch = lex_getch(lex);
1167 if (!strcmp(v, "cd") ||
1168 !strcmp(v, "origin") ||
1169 !strcmp(v, "base") ||
1170 !strcmp(v, "flags") ||
1171 !strcmp(v, "scale") ||
1175 ch = lex_getch(lex);
1176 while (ch != FS_FILE_EOF && ch != '\n')
1177 ch = lex_getch(lex);
1181 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1182 if (!strcmp(v, lex->frames[frame].name)) {
1183 lex->tok.constval.i = lex->frames[frame].value;
1184 return (lex->tok.ttype = TOKEN_INTCONST);
1188 lexerror(lex, "invalid frame macro");
1192 /* single-character tokens */
1196 nextch = lex_getch(lex);
1197 if (nextch == '[') {
1198 lex_tokench(lex, ch);
1199 lex_tokench(lex, nextch);
1201 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1203 lex_ungetch(lex, nextch);
1208 lex_tokench(lex, ch);
1210 if (lex->flags.noops)
1211 return (lex->tok.ttype = ch);
1213 return (lex->tok.ttype = TOKEN_OPERATOR);
1216 if (lex->flags.noops) {
1217 nextch = lex_getch(lex);
1218 if (nextch == ']') {
1219 lex_tokench(lex, ch);
1220 lex_tokench(lex, nextch);
1222 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1224 lex_ungetch(lex, nextch);
1233 lex_tokench(lex, ch);
1235 return (lex->tok.ttype = ch);
1241 nextch = lex_getch(lex);
1242 /* digits starting with a dot */
1243 if (util_isdigit(nextch)) {
1244 lex_ungetch(lex, nextch);
1245 lex->tok.ttype = lex_finish_digit(lex, ch);
1247 return lex->tok.ttype;
1249 lex_ungetch(lex, nextch);
1252 if (lex->flags.noops)
1254 /* Detect characters early which are normally
1255 * operators OR PART of an operator.
1274 lex_tokench(lex, ch);
1276 return (lex->tok.ttype = ch);
1284 lex_tokench(lex, ch);
1285 /* peak ahead once */
1286 nextch = lex_getch(lex);
1287 if (nextch != '.') {
1288 lex_ungetch(lex, nextch);
1290 if (lex->flags.noops)
1291 return (lex->tok.ttype = ch);
1293 return (lex->tok.ttype = TOKEN_OPERATOR);
1295 /* peak ahead again */
1296 nextch = lex_getch(lex);
1297 if (nextch != '.') {
1298 lex_ungetch(lex, nextch);
1299 lex_ungetch(lex, '.');
1301 if (lex->flags.noops)
1302 return (lex->tok.ttype = ch);
1304 return (lex->tok.ttype = TOKEN_OPERATOR);
1306 /* fill the token to be "..." */
1307 lex_tokench(lex, ch);
1308 lex_tokench(lex, ch);
1310 return (lex->tok.ttype = TOKEN_DOTS);
1313 if (ch == ',' || ch == '.') {
1314 lex_tokench(lex, ch);
1316 return (lex->tok.ttype = TOKEN_OPERATOR);
1319 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1320 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1321 ch == '=' || ch == '!' || /* <=>, ==, != */
1322 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1323 ch == '~' || ch == '^' /* ~=, ~, ^ */
1325 lex_tokench(lex, ch);
1327 nextch = lex_getch(lex);
1328 if ((nextch == '=' && ch != '<') ||
1329 (nextch == ch && ch != '!') ||
1330 (nextch == '<' && ch == '>')) {
1331 lex_tokench(lex, nextch);
1332 } else if (ch == '<' && nextch == '=') {
1333 lex_tokench(lex, nextch);
1334 if ((thirdch = lex_getch(lex)) == '>')
1335 lex_tokench(lex, thirdch);
1337 lex_ungetch(lex, thirdch);
1339 } else if (ch == '-' && nextch == '>') {
1340 lex_tokench(lex, nextch);
1341 } else if (ch == '&' && nextch == '~') {
1342 thirdch = lex_getch(lex);
1343 if (thirdch != '=') {
1344 lex_ungetch(lex, thirdch);
1345 lex_ungetch(lex, nextch);
1348 lex_tokench(lex, nextch);
1349 lex_tokench(lex, thirdch);
1352 else if (lex->flags.preprocessing &&
1353 ch == '-' && util_isdigit(nextch))
1355 lex->tok.ttype = lex_finish_digit(lex, nextch);
1356 if (lex->tok.ttype == TOKEN_INTCONST)
1357 lex->tok.constval.i = -lex->tok.constval.i;
1359 lex->tok.constval.f = -lex->tok.constval.f;
1361 return lex->tok.ttype;
1363 lex_ungetch(lex, nextch);
1367 return (lex->tok.ttype = TOKEN_OPERATOR);
1371 if (ch == '^' || ch == '~' || ch == '!')
1373 lex_tokench(lex, ch);
1375 return (lex->tok.ttype = TOKEN_OPERATOR);
1379 if (ch == '*' || ch == '/') /* *=, /= */
1381 lex_tokench(lex, ch);
1383 nextch = lex_getch(lex);
1384 if (nextch == '=' || nextch == '*') {
1385 lex_tokench(lex, nextch);
1387 lex_ungetch(lex, nextch);
1390 return (lex->tok.ttype = TOKEN_OPERATOR);
1394 lex_tokench(lex, ch);
1396 return (lex->tok.ttype = TOKEN_OPERATOR);
1399 if (isident_start(ch))
1403 lex_tokench(lex, ch);
1404 if (!lex_finish_ident(lex)) {
1406 return (lex->tok.ttype = TOKEN_ERROR);
1409 lex->tok.ttype = TOKEN_IDENT;
1412 if (!strcmp(v, "void")) {
1413 lex->tok.ttype = TOKEN_TYPENAME;
1414 lex->tok.constval.t = TYPE_VOID;
1415 } else if (!strcmp(v, "int")) {
1416 lex->tok.ttype = TOKEN_TYPENAME;
1417 lex->tok.constval.t = TYPE_INTEGER;
1418 } else if (!strcmp(v, "float")) {
1419 lex->tok.ttype = TOKEN_TYPENAME;
1420 lex->tok.constval.t = TYPE_FLOAT;
1421 } else if (!strcmp(v, "string")) {
1422 lex->tok.ttype = TOKEN_TYPENAME;
1423 lex->tok.constval.t = TYPE_STRING;
1424 } else if (!strcmp(v, "entity")) {
1425 lex->tok.ttype = TOKEN_TYPENAME;
1426 lex->tok.constval.t = TYPE_ENTITY;
1427 } else if (!strcmp(v, "vector")) {
1428 lex->tok.ttype = TOKEN_TYPENAME;
1429 lex->tok.constval.t = TYPE_VECTOR;
1432 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1433 if (!strcmp(v, keywords_qc[kw]))
1434 return (lex->tok.ttype = TOKEN_KEYWORD);
1436 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1437 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1438 if (!strcmp(v, keywords_fg[kw]))
1439 return (lex->tok.ttype = TOKEN_KEYWORD);
1444 return lex->tok.ttype;
1449 lex->flags.nodigraphs = true;
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);
1455 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1457 /* Allow c style "string" "continuation" */
1458 ch = lex_skipwhite(lex, false);
1460 lex_ungetch(lex, ch);
1464 lex->tok.ttype = lex_finish_string(lex, '"');
1466 lex->flags.nodigraphs = false;
1468 return lex->tok.ttype;
1473 /* we parse character constants like string,
1474 * but return TOKEN_CHARCONST, or a vector type if it fits...
1475 * Likewise actual unescaping has to be done by the parser.
1476 * The difference is we don't allow 'char' 'continuation'.
1478 if (lex->flags.preprocessing)
1479 lex_tokench(lex, ch);
1480 lex->tok.ttype = lex_finish_string(lex, '\'');
1481 if (lex->flags.preprocessing)
1482 lex_tokench(lex, ch);
1485 lex->tok.ttype = TOKEN_CHARCONST;
1487 /* It's a vector if we can successfully scan 3 floats */
1488 if (util_sscanf(lex->tok.value, " %f %f %f ",
1489 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1492 lex->tok.ttype = TOKEN_VECTORCONST;
1496 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1498 /* check for a valid utf8 character */
1499 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1500 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1501 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1502 : "multibyte character: `%s`" ),
1504 return (lex->tok.ttype = TOKEN_ERROR);
1507 lex->tok.constval.i = u8char;
1510 lex->tok.constval.i = lex->tok.value[0];
1513 return lex->tok.ttype;
1516 if (util_isdigit(ch))
1518 lex->tok.ttype = lex_finish_digit(lex, ch);
1520 return lex->tok.ttype;
1523 if (lex->flags.preprocessing) {
1524 lex_tokench(lex, ch);
1526 return (lex->tok.ttype = ch);
1529 lexerror(lex, "unknown token: `%c`", ch);
1530 return (lex->tok.ttype = TOKEN_ERROR);