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 static void lex_ungetch(lex_file *lex, int ch);
182 static int lex_getch(lex_file *lex);
184 lex_file* lex_open(const char *file)
187 fs_file_t *in = fs_file_open(file, "rb");
191 lexerror(NULL, "open failed: '%s'\n", file);
195 lex = (lex_file*)mem_a(sizeof(*lex));
198 lexerror(NULL, "out of memory\n");
202 memset(lex, 0, sizeof(*lex));
205 lex->name = util_strdup(file);
206 lex->line = 1; /* we start counting at 1 */
212 if ((read = (lex_getch(lex) << 16) | (lex_getch(lex) << 8) | lex_getch(lex)) != 0xEFBBBF) {
213 lex_ungetch(lex, (read & 0x0000FF));
214 lex_ungetch(lex, (read & 0x00FF00) >> 8);
215 lex_ungetch(lex, (read & 0xFF0000) >> 16);
218 * otherwise the lexer has advanced 3 bytes for the BOM, we need
219 * to set the column back to 0
224 vec_push(lex_filenames, lex->name);
228 lex_file* lex_open_string(const char *str, size_t len, const char *name)
232 lex = (lex_file*)mem_a(sizeof(*lex));
234 lexerror(NULL, "out of memory\n");
238 memset(lex, 0, sizeof(*lex));
241 lex->open_string = str;
242 lex->open_string_length = len;
243 lex->open_string_pos = 0;
245 lex->name = util_strdup(name ? name : "<string-source>");
246 lex->line = 1; /* we start counting at 1 */
251 vec_push(lex_filenames, lex->name);
256 void lex_cleanup(void)
259 for (i = 0; i < vec_size(lex_filenames); ++i)
260 mem_d(lex_filenames[i]);
261 vec_free(lex_filenames);
264 void lex_close(lex_file *lex)
267 for (i = 0; i < vec_size(lex->frames); ++i)
268 mem_d(lex->frames[i].name);
269 vec_free(lex->frames);
272 vec_free(lex->modelname);
275 fs_file_close(lex->file);
278 token_delete(lex->tok);
280 vec_free(lex->tok.value);
282 /* mem_d(lex->name); collected in lex_filenames */
288 static int lex_fgetc(lex_file *lex)
292 return fs_file_getc(lex->file);
294 if (lex->open_string) {
295 if (lex->open_string_pos >= lex->open_string_length)
298 return lex->open_string[lex->open_string_pos++];
303 /* Get or put-back data
304 * The following to functions do NOT understand what kind of data they
306 * The are merely wrapping get/put in order to count line numbers.
308 static int lex_try_trigraph(lex_file *lex, int old)
312 if (!lex->push_line && c2 == '\n') {
318 lex_ungetch(lex, c2);
323 if (!lex->push_line && c3 == '\n') {
329 case '=': return '#';
330 case '/': return '\\';
331 case '\'': return '^';
332 case '(': return '[';
333 case ')': return ']';
334 case '!': return '|';
335 case '<': return '{';
336 case '>': return '}';
337 case '-': return '~';
339 lex_ungetch(lex, c3);
340 lex_ungetch(lex, c2);
345 static int lex_try_digraph(lex_file *lex, int ch)
349 /* we just used fgetc() so count lines
350 * need to offset a \n the ungetch would recognize
352 if (!lex->push_line && c2 == '\n')
354 if (ch == '<' && c2 == ':')
356 else if (ch == ':' && c2 == '>')
358 else if (ch == '<' && c2 == '%')
360 else if (ch == '%' && c2 == '>')
362 else if (ch == '%' && c2 == ':')
364 lex_ungetch(lex, c2);
368 static int lex_getch(lex_file *lex)
374 if (!lex->push_line && lex->peek[lex->peekpos] == '\n') {
378 return lex->peek[lex->peekpos];
382 if (!lex->push_line && ch == '\n') {
387 return lex_try_trigraph(lex, ch);
388 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
389 return lex_try_digraph(lex, ch);
393 static void lex_ungetch(lex_file *lex, int ch)
395 lex->peek[lex->peekpos++] = ch;
397 if (!lex->push_line && ch == '\n') {
403 /* classify characters
404 * some additions to the is*() functions of ctype.h
407 /* Idents are alphanumberic, but they start with alpha or _ */
408 static bool isident_start(int ch)
410 return util_isalpha(ch) || ch == '_';
413 static bool isident(int ch)
415 return isident_start(ch) || util_isdigit(ch);
418 /* isxdigit_only is used when we already know it's not a digit
419 * and want to see if it's a hex digit anyway.
421 static bool isxdigit_only(int ch)
423 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
426 /* Append a character to the token buffer */
427 static void lex_tokench(lex_file *lex, int ch)
429 vec_push(lex->tok.value, ch);
432 /* Append a trailing null-byte */
433 static void lex_endtoken(lex_file *lex)
435 vec_push(lex->tok.value, 0);
436 vec_shrinkby(lex->tok.value, 1);
439 static bool lex_try_pragma(lex_file *lex)
443 char *command = NULL;
447 if (lex->flags.preprocessing)
454 lex_ungetch(lex, ch);
458 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
459 vec_push(pragma, ch);
462 if (ch != ' ' || strcmp(pragma, "pragma")) {
463 lex_ungetch(lex, ch);
467 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
468 vec_push(command, ch);
469 vec_push(command, 0);
472 lex_ungetch(lex, ch);
476 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
481 lex_ungetch(lex, ch);
485 if (!strcmp(command, "push")) {
486 if (!strcmp(param, "line")) {
488 if (lex->push_line == 1)
494 else if (!strcmp(command, "pop")) {
495 if (!strcmp(param, "line")) {
498 if (lex->push_line == 0)
504 else if (!strcmp(command, "file")) {
505 lex->name = util_strdup(param);
506 vec_push(lex_filenames, lex->name);
508 else if (!strcmp(command, "line")) {
509 line = strtol(param, NULL, 0)-1;
515 while (ch != '\n' && ch != FS_FILE_EOF)
525 while (vec_size(command)) {
526 lex_ungetch(lex, (unsigned char)vec_last(command));
530 lex_ungetch(lex, ' ');
534 while (vec_size(param)) {
535 lex_ungetch(lex, (unsigned char)vec_last(param));
539 lex_ungetch(lex, ' ');
543 while (vec_size(pragma)) {
544 lex_ungetch(lex, (unsigned char)vec_last(pragma));
549 lex_ungetch(lex, '#');
555 /* Skip whitespace and comments and return the first
556 * non-white character.
557 * As this makes use of the above getch() ungetch() functions,
558 * we don't need to care at all about line numbering anymore.
560 * In theory, this function should only be used at the beginning
561 * of lexing, or when we *know* the next character is part of the token.
562 * Otherwise, if the parser throws an error, the linenumber may not be
563 * the line of the error, but the line of the next token AFTER the error.
565 * This is currently only problematic when using c-like string-continuation,
566 * since comments and whitespaces are allowed between 2 such strings.
570 "A continuation of the previous string"
571 // This line is skipped
574 * In this case, if the parse decides it didn't actually want a string,
575 * and uses lex->line to print an error, it will show the ', foo);' line's
578 * On the other hand, the parser is supposed to remember the line of the next
579 * token's beginning. In this case we would want skipwhite() to be called
580 * AFTER reading a token, so that the parser, before reading the NEXT token,
581 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
584 * here is to store the line of the first character after skipping
585 * the initial whitespace in lex->sline, this happens in lex_do.
587 static int lex_skipwhite(lex_file *lex, bool hadwhite)
590 bool haswhite = hadwhite;
595 while (ch != FS_FILE_EOF && util_isspace(ch)) {
597 if (lex_try_pragma(lex))
600 if (lex->flags.preprocessing) {
603 /* see if there was whitespace first */
604 if (haswhite) { /* (vec_size(lex->tok.value)) { */
605 lex_ungetch(lex, ch);
609 /* otherwise return EOL */
613 lex_tokench(lex, ch);
622 /* one line comment */
625 if (lex->flags.preprocessing) {
628 lex_tokench(lex, '/');
629 lex_tokench(lex, '/');
631 lex_tokench(lex, ' ');
632 lex_tokench(lex, ' ');
635 while (ch != FS_FILE_EOF && ch != '\n') {
636 if (lex->flags.preprocessing)
637 lex_tokench(lex, ' '); /* ch); */
640 if (lex->flags.preprocessing) {
641 lex_ungetch(lex, '\n');
649 /* multiline comment */
650 if (lex->flags.preprocessing) {
653 lex_tokench(lex, '/');
654 lex_tokench(lex, '*');
656 lex_tokench(lex, ' ');
657 lex_tokench(lex, ' ');
660 while (ch != FS_FILE_EOF)
666 if (lex->flags.preprocessing) {
668 lex_tokench(lex, '*');
669 lex_tokench(lex, '/');
671 lex_tokench(lex, ' ');
672 lex_tokench(lex, ' ');
676 lex_ungetch(lex, ch);
678 if (lex->flags.preprocessing) {
680 lex_tokench(lex, '\n');
682 lex_tokench(lex, ' '); /* ch); */
685 ch = ' '; /* cause TRUE in the isspace check */
688 /* Otherwise roll back to the slash and break out of the loop */
689 lex_ungetch(lex, ch);
693 } while (ch != FS_FILE_EOF && util_isspace(ch));
697 lex_ungetch(lex, ch);
704 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
709 while (ch != FS_FILE_EOF && isident(ch))
711 lex_tokench(lex, ch);
715 /* last ch was not an ident ch: */
716 lex_ungetch(lex, ch);
721 /* read one ident for the frame list */
722 static int lex_parse_frame(lex_file *lex)
729 while (ch != FS_FILE_EOF && ch != '\n' && util_isspace(ch))
735 if (!isident_start(ch)) {
736 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
740 lex_tokench(lex, ch);
741 if (!lex_finish_ident(lex))
747 /* read a list of $frames */
748 static bool lex_finish_frames(lex_file *lex)
755 rc = lex_parse_frame(lex);
756 if (rc > 0) /* end of line */
758 if (rc < 0) /* error */
761 for (i = 0; i < vec_size(lex->frames); ++i) {
762 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
763 lex->frames[i].value = lex->framevalue++;
764 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
769 if (i < vec_size(lex->frames))
772 m.value = lex->framevalue++;
773 m.name = util_strdup(lex->tok.value);
774 vec_shrinkto(lex->tok.value, 0);
775 vec_push(lex->frames, m);
781 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
788 char u8buf[8]; /* way more than enough */
791 while (ch != FS_FILE_EOF)
795 return TOKEN_STRINGCONST;
797 if (lex->flags.preprocessing && ch == '\\') {
798 lex_tokench(lex, ch);
800 if (ch == FS_FILE_EOF) {
801 lexerror(lex, "unexpected end of file");
802 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
803 return (lex->tok.ttype = TOKEN_ERROR);
805 lex_tokench(lex, ch);
807 else if (ch == '\\') {
809 if (ch == FS_FILE_EOF) {
810 lexerror(lex, "unexpected end of file");
811 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
812 return (lex->tok.ttype = TOKEN_ERROR);
819 case 'a': ch = '\a'; break;
820 case 'b': ch = '\b'; break;
821 case 'r': ch = '\r'; break;
822 case 'n': ch = '\n'; break;
823 case 't': ch = '\t'; break;
824 case 'f': ch = '\f'; break;
825 case 'v': ch = '\v'; break;
828 /* same procedure as in fteqcc */
830 nextch = lex_getch(lex);
831 if (nextch >= '0' && nextch <= '9')
833 else if (nextch >= 'a' && nextch <= 'f')
834 ch += nextch - 'a' + 10;
835 else if (nextch >= 'A' && nextch <= 'F')
836 ch += nextch - 'A' + 10;
838 lexerror(lex, "bad character code");
839 lex_ungetch(lex, nextch);
840 return (lex->tok.ttype = TOKEN_ERROR);
844 nextch = lex_getch(lex);
845 if (nextch >= '0' && nextch <= '9')
847 else if (nextch >= 'a' && nextch <= 'f')
848 ch += nextch - 'a' + 10;
849 else if (nextch >= 'A' && nextch <= 'F')
850 ch += nextch - 'A' + 10;
852 lexerror(lex, "bad character code");
853 lex_ungetch(lex, nextch);
854 return (lex->tok.ttype = TOKEN_ERROR);
859 case '0': case '1': case '2': case '3':
860 case '4': case '5': case '6': case '7':
864 case '<': ch = 29; break;
865 case '-': ch = 30; break;
866 case '>': ch = 31; break;
867 case '[': ch = 16; break;
868 case ']': ch = 17; break;
871 nextch = lex_getch(lex);
872 hex = (nextch == 'x');
873 oct = (nextch == '0');
875 lex_ungetch(lex, nextch);
876 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
878 if (nextch >= '0' && nextch <= '9')
879 chr = chr * 10 + nextch - '0';
881 lexerror(lex, "bad character code");
882 return (lex->tok.ttype = TOKEN_ERROR);
885 if (nextch >= '0' && nextch <= '9')
886 chr = chr * 0x10 + nextch - '0';
887 else if (nextch >= 'a' && nextch <= 'f')
888 chr = chr * 0x10 + nextch - 'a' + 10;
889 else if (nextch >= 'A' && nextch <= 'F')
890 chr = chr * 0x10 + nextch - 'A' + 10;
892 lexerror(lex, "bad character code");
893 return (lex->tok.ttype = TOKEN_ERROR);
896 if (nextch >= '0' && nextch <= '9')
897 chr = chr * 8 + chr - '0';
899 lexerror(lex, "bad character code");
900 return (lex->tok.ttype = TOKEN_ERROR);
903 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
905 lexerror(lex, "character code out of range");
906 return (lex->tok.ttype = TOKEN_ERROR);
909 if (OPTS_FLAG(UTF8) && chr >= 128) {
910 u8len = utf8_from(u8buf, chr);
915 lex->column += u8len;
916 for (uc = 0; uc < u8len; ++uc)
917 lex_tokench(lex, u8buf[uc]);
919 * the last character will be inserted with the tokench() call
928 case '\n': ch = '\n'; break;
931 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
932 /* so we just add the character plus backslash no matter what it actually is */
933 lex_tokench(lex, '\\');
935 /* add the character finally */
936 lex_tokench(lex, ch);
939 lex_tokench(lex, ch);
941 lexerror(lex, "unexpected end of file within string constant");
942 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
943 return (lex->tok.ttype = TOKEN_ERROR);
946 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
952 /* parse a number... */
954 lex->tok.ttype = TOKEN_FLOATCONST;
956 lex->tok.ttype = TOKEN_INTCONST;
958 lex_tokench(lex, ch);
961 if (ch != '.' && !util_isdigit(ch))
963 if (lastch != '0' || ch != 'x')
965 /* end of the number or EOF */
966 lex_ungetch(lex, ch);
969 lex->tok.constval.i = lastch - '0';
970 return lex->tok.ttype;
976 /* EOF would have been caught above */
980 lex_tokench(lex, ch);
982 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
984 lex_tokench(lex, ch);
988 /* NOT else, '.' can come from above as well */
989 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
991 /* Allow floating comma in non-hex mode */
992 lex->tok.ttype = TOKEN_FLOATCONST;
993 lex_tokench(lex, ch);
995 /* continue digits-only */
997 while (util_isdigit(ch))
999 lex_tokench(lex, ch);
1000 ch = lex_getch(lex);
1003 /* put back the last character */
1004 /* but do not put back the trailing 'f' or a float */
1005 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
1006 ch = lex_getch(lex);
1008 /* generally we don't want words to follow numbers: */
1010 lexerror(lex, "unexpected trailing characters after number");
1011 return (lex->tok.ttype = TOKEN_ERROR);
1013 lex_ungetch(lex, ch);
1016 if (lex->tok.ttype == TOKEN_FLOATCONST)
1017 lex->tok.constval.f = strtod(lex->tok.value, NULL);
1019 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
1020 return lex->tok.ttype;
1023 int lex_do(lex_file *lex)
1025 int ch, nextch, thirdch;
1026 bool hadwhite = false;
1035 ch = lex_skipwhite(lex, hadwhite);
1037 if (!lex->flags.mergelines || ch != '\\')
1039 ch = lex_getch(lex);
1041 ch = lex_getch(lex);
1043 lex_ungetch(lex, ch);
1047 /* we reached a linemerge */
1048 lex_tokench(lex, '\n');
1052 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1053 return (lex->tok.ttype = ch);
1056 lex->sline = lex->line;
1057 lex->tok.ctx.line = lex->sline;
1058 lex->tok.ctx.file = lex->name;
1061 return (lex->tok.ttype = TOKEN_FATAL);
1063 if (ch == FS_FILE_EOF) {
1065 return (lex->tok.ttype = TOKEN_EOF);
1068 /* modelgen / spiritgen commands */
1069 if (ch == '$' && !lex->flags.preprocessing) {
1073 ch = lex_getch(lex);
1074 if (!isident_start(ch)) {
1075 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1078 lex_tokench(lex, ch);
1079 if (!lex_finish_ident(lex))
1080 return (lex->tok.ttype = TOKEN_ERROR);
1082 /* skip the known commands */
1085 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1087 /* frame/framesave command works like an enum
1088 * similar to fteqcc we handle this in the lexer.
1089 * The reason for this is that it is sensitive to newlines,
1090 * which the parser is unaware of
1092 if (!lex_finish_frames(lex))
1093 return (lex->tok.ttype = TOKEN_ERROR);
1097 if (!strcmp(v, "framevalue"))
1099 ch = lex_getch(lex);
1100 while (ch != FS_FILE_EOF && util_isspace(ch) && ch != '\n')
1101 ch = lex_getch(lex);
1103 if (!util_isdigit(ch)) {
1104 lexerror(lex, "$framevalue requires an integer parameter");
1109 lex->tok.ttype = lex_finish_digit(lex, ch);
1111 if (lex->tok.ttype != TOKEN_INTCONST) {
1112 lexerror(lex, "$framevalue requires an integer parameter");
1115 lex->framevalue = lex->tok.constval.i;
1119 if (!strcmp(v, "framerestore"))
1125 rc = lex_parse_frame(lex);
1128 lexerror(lex, "$framerestore requires a framename parameter");
1132 return (lex->tok.ttype = TOKEN_FATAL);
1135 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1136 if (!strcmp(v, lex->frames[frame].name)) {
1137 lex->framevalue = lex->frames[frame].value;
1141 lexerror(lex, "unknown framename `%s`", v);
1145 if (!strcmp(v, "modelname"))
1151 rc = lex_parse_frame(lex);
1154 lexerror(lex, "$modelname requires a parameter");
1158 return (lex->tok.ttype = TOKEN_FATAL);
1160 if (lex->modelname) {
1162 m.value = lex->framevalue;
1163 m.name = lex->modelname;
1164 lex->modelname = NULL;
1165 vec_push(lex->frames, m);
1167 lex->modelname = lex->tok.value;
1168 lex->tok.value = NULL;
1172 if (!strcmp(v, "flush"))
1175 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1176 mem_d(lex->frames[fi].name);
1177 vec_free(lex->frames);
1178 /* skip line (fteqcc does it too) */
1179 ch = lex_getch(lex);
1180 while (ch != FS_FILE_EOF && ch != '\n')
1181 ch = lex_getch(lex);
1185 if (!strcmp(v, "cd") ||
1186 !strcmp(v, "origin") ||
1187 !strcmp(v, "base") ||
1188 !strcmp(v, "flags") ||
1189 !strcmp(v, "scale") ||
1193 ch = lex_getch(lex);
1194 while (ch != FS_FILE_EOF && ch != '\n')
1195 ch = lex_getch(lex);
1199 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1200 if (!strcmp(v, lex->frames[frame].name)) {
1201 lex->tok.constval.i = lex->frames[frame].value;
1202 return (lex->tok.ttype = TOKEN_INTCONST);
1206 lexerror(lex, "invalid frame macro");
1210 /* single-character tokens */
1214 nextch = lex_getch(lex);
1215 if (nextch == '[') {
1216 lex_tokench(lex, ch);
1217 lex_tokench(lex, nextch);
1219 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1221 lex_ungetch(lex, nextch);
1226 lex_tokench(lex, ch);
1228 if (lex->flags.noops)
1229 return (lex->tok.ttype = ch);
1231 return (lex->tok.ttype = TOKEN_OPERATOR);
1234 if (lex->flags.noops) {
1235 nextch = lex_getch(lex);
1236 if (nextch == ']') {
1237 lex_tokench(lex, ch);
1238 lex_tokench(lex, nextch);
1240 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1242 lex_ungetch(lex, nextch);
1251 lex_tokench(lex, ch);
1253 return (lex->tok.ttype = ch);
1259 nextch = lex_getch(lex);
1260 /* digits starting with a dot */
1261 if (util_isdigit(nextch)) {
1262 lex_ungetch(lex, nextch);
1263 lex->tok.ttype = lex_finish_digit(lex, ch);
1265 return lex->tok.ttype;
1267 lex_ungetch(lex, nextch);
1270 if (lex->flags.noops)
1272 /* Detect characters early which are normally
1273 * operators OR PART of an operator.
1292 lex_tokench(lex, ch);
1294 return (lex->tok.ttype = ch);
1302 lex_tokench(lex, ch);
1303 /* peak ahead once */
1304 nextch = lex_getch(lex);
1305 if (nextch != '.') {
1306 lex_ungetch(lex, nextch);
1308 if (lex->flags.noops)
1309 return (lex->tok.ttype = ch);
1311 return (lex->tok.ttype = TOKEN_OPERATOR);
1313 /* peak ahead again */
1314 nextch = lex_getch(lex);
1315 if (nextch != '.') {
1316 lex_ungetch(lex, nextch);
1317 lex_ungetch(lex, '.');
1319 if (lex->flags.noops)
1320 return (lex->tok.ttype = ch);
1322 return (lex->tok.ttype = TOKEN_OPERATOR);
1324 /* fill the token to be "..." */
1325 lex_tokench(lex, ch);
1326 lex_tokench(lex, ch);
1328 return (lex->tok.ttype = TOKEN_DOTS);
1331 if (ch == ',' || ch == '.') {
1332 lex_tokench(lex, ch);
1334 return (lex->tok.ttype = TOKEN_OPERATOR);
1337 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1338 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1339 ch == '=' || ch == '!' || /* <=>, ==, != */
1340 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1341 ch == '~' || ch == '^' /* ~=, ~, ^ */
1343 lex_tokench(lex, ch);
1345 nextch = lex_getch(lex);
1346 if ((nextch == '=' && ch != '<') ||
1347 (nextch == ch && ch != '!') ||
1348 (nextch == '<' && ch == '>')) {
1349 lex_tokench(lex, nextch);
1350 } else if (ch == '<' && nextch == '=') {
1351 lex_tokench(lex, nextch);
1352 if ((thirdch = lex_getch(lex)) == '>')
1353 lex_tokench(lex, thirdch);
1355 lex_ungetch(lex, thirdch);
1357 } else if (ch == '-' && nextch == '>') {
1358 lex_tokench(lex, nextch);
1359 } else if (ch == '&' && nextch == '~') {
1360 thirdch = lex_getch(lex);
1361 if (thirdch != '=') {
1362 lex_ungetch(lex, thirdch);
1363 lex_ungetch(lex, nextch);
1366 lex_tokench(lex, nextch);
1367 lex_tokench(lex, thirdch);
1370 else if (lex->flags.preprocessing &&
1371 ch == '-' && util_isdigit(nextch))
1373 lex->tok.ttype = lex_finish_digit(lex, nextch);
1374 if (lex->tok.ttype == TOKEN_INTCONST)
1375 lex->tok.constval.i = -lex->tok.constval.i;
1377 lex->tok.constval.f = -lex->tok.constval.f;
1379 return lex->tok.ttype;
1381 lex_ungetch(lex, nextch);
1385 return (lex->tok.ttype = TOKEN_OPERATOR);
1389 if (ch == '^' || ch == '~' || ch == '!')
1391 lex_tokench(lex, ch);
1393 return (lex->tok.ttype = TOKEN_OPERATOR);
1397 if (ch == '*' || ch == '/') /* *=, /= */
1399 lex_tokench(lex, ch);
1401 nextch = lex_getch(lex);
1402 if (nextch == '=' || nextch == '*') {
1403 lex_tokench(lex, nextch);
1405 lex_ungetch(lex, nextch);
1408 return (lex->tok.ttype = TOKEN_OPERATOR);
1412 lex_tokench(lex, ch);
1414 return (lex->tok.ttype = TOKEN_OPERATOR);
1417 if (isident_start(ch))
1421 lex_tokench(lex, ch);
1422 if (!lex_finish_ident(lex)) {
1424 return (lex->tok.ttype = TOKEN_ERROR);
1427 lex->tok.ttype = TOKEN_IDENT;
1430 if (!strcmp(v, "void")) {
1431 lex->tok.ttype = TOKEN_TYPENAME;
1432 lex->tok.constval.t = TYPE_VOID;
1433 } else if (!strcmp(v, "int")) {
1434 lex->tok.ttype = TOKEN_TYPENAME;
1435 lex->tok.constval.t = TYPE_INTEGER;
1436 } else if (!strcmp(v, "float")) {
1437 lex->tok.ttype = TOKEN_TYPENAME;
1438 lex->tok.constval.t = TYPE_FLOAT;
1439 } else if (!strcmp(v, "string")) {
1440 lex->tok.ttype = TOKEN_TYPENAME;
1441 lex->tok.constval.t = TYPE_STRING;
1442 } else if (!strcmp(v, "entity")) {
1443 lex->tok.ttype = TOKEN_TYPENAME;
1444 lex->tok.constval.t = TYPE_ENTITY;
1445 } else if (!strcmp(v, "vector")) {
1446 lex->tok.ttype = TOKEN_TYPENAME;
1447 lex->tok.constval.t = TYPE_VECTOR;
1450 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1451 if (!strcmp(v, keywords_qc[kw]))
1452 return (lex->tok.ttype = TOKEN_KEYWORD);
1454 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1455 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1456 if (!strcmp(v, keywords_fg[kw]))
1457 return (lex->tok.ttype = TOKEN_KEYWORD);
1462 return lex->tok.ttype;
1467 lex->flags.nodigraphs = true;
1468 if (lex->flags.preprocessing)
1469 lex_tokench(lex, ch);
1470 lex->tok.ttype = lex_finish_string(lex, '"');
1471 if (lex->flags.preprocessing)
1472 lex_tokench(lex, ch);
1473 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1475 /* Allow c style "string" "continuation" */
1476 ch = lex_skipwhite(lex, false);
1478 lex_ungetch(lex, ch);
1482 lex->tok.ttype = lex_finish_string(lex, '"');
1484 lex->flags.nodigraphs = false;
1486 return lex->tok.ttype;
1491 /* we parse character constants like string,
1492 * but return TOKEN_CHARCONST, or a vector type if it fits...
1493 * Likewise actual unescaping has to be done by the parser.
1494 * The difference is we don't allow 'char' 'continuation'.
1496 if (lex->flags.preprocessing)
1497 lex_tokench(lex, ch);
1498 lex->tok.ttype = lex_finish_string(lex, '\'');
1499 if (lex->flags.preprocessing)
1500 lex_tokench(lex, ch);
1503 lex->tok.ttype = TOKEN_CHARCONST;
1505 /* It's a vector if we can successfully scan 3 floats */
1506 if (util_sscanf(lex->tok.value, " %f %f %f ",
1507 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1510 lex->tok.ttype = TOKEN_VECTORCONST;
1514 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1516 /* check for a valid utf8 character */
1517 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1518 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1519 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1520 : "multibyte character: `%s`" ),
1522 return (lex->tok.ttype = TOKEN_ERROR);
1525 lex->tok.constval.i = u8char;
1528 lex->tok.constval.i = lex->tok.value[0];
1531 return lex->tok.ttype;
1534 if (util_isdigit(ch))
1536 lex->tok.ttype = lex_finish_digit(lex, ch);
1538 return lex->tok.ttype;
1541 if (lex->flags.preprocessing) {
1542 lex_tokench(lex, ch);
1544 return (lex->tok.ttype = ch);
1547 lexerror(lex, "unknown token: `%c`", ch);
1548 return (lex->tok.ttype = TOKEN_ERROR);