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
35 static const char *keywords_qc[] = {
43 static const char *keywords_fg[] = {
44 "switch", "case", "default",
50 "__builtin_debug_printtype"
56 static char* *lex_filenames;
58 static void lexerror(lex_file *lex, const char *fmt, ...)
64 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap);
66 con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap);
70 static bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
77 ctx.line = lex->sline;
78 ctx.column = lex->column;
81 r = vcompile_warning(ctx, warntype, fmt, ap);
90 token *tok = (token*)mem_a(sizeof(token));
93 memset(tok, 0, sizeof(*tok));
97 void token_delete(token *self)
99 if (self->next && self->next->prev == self)
100 self->next->prev = self->prev;
101 if (self->prev && self->prev->next == self)
102 self->prev->next = self->next;
103 MEM_VECTOR_CLEAR(self, value);
107 token* token_copy(const token *cp)
109 token* self = token_new();
113 self->value_alloc = cp->value_count + 1;
114 self->value_count = cp->value_count;
115 self->value = (char*)mem_a(self->value_alloc);
120 memcpy(self->value, cp->value, cp->value_count);
121 self->value[self->value_alloc-1] = 0;
125 self->ttype = cp->ttype;
126 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
130 void token_delete_all(token *t)
141 token* token_copy_all(const token *cp)
146 out = cur = token_copy(cp);
152 cur->next = token_copy(cp);
154 token_delete_all(out);
157 cur->next->prev = cur;
164 static void lex_token_new(lex_file *lex)
168 token_delete(lex->tok);
169 lex->tok = token_new();
172 vec_shrinkto(lex->tok.value, 0);
174 lex->tok.constval.t = 0;
175 lex->tok.ctx.line = lex->sline;
176 lex->tok.ctx.file = lex->name;
177 lex->tok.ctx.column = lex->column;
182 lex_file* lex_open(const char *file)
185 FILE *in = fs_file_open(file, "rb");
188 lexerror(NULL, "open failed: '%s'\n", file);
192 lex = (lex_file*)mem_a(sizeof(*lex));
195 lexerror(NULL, "out of memory\n");
199 memset(lex, 0, sizeof(*lex));
202 lex->name = util_strdup(file);
203 lex->line = 1; /* we start counting at 1 */
208 vec_push(lex_filenames, lex->name);
212 lex_file* lex_open_string(const char *str, size_t len, const char *name)
216 lex = (lex_file*)mem_a(sizeof(*lex));
218 lexerror(NULL, "out of memory\n");
222 memset(lex, 0, sizeof(*lex));
225 lex->open_string = str;
226 lex->open_string_length = len;
227 lex->open_string_pos = 0;
229 lex->name = util_strdup(name ? name : "<string-source>");
230 lex->line = 1; /* we start counting at 1 */
235 vec_push(lex_filenames, lex->name);
240 void lex_cleanup(void)
243 for (i = 0; i < vec_size(lex_filenames); ++i)
244 mem_d(lex_filenames[i]);
245 vec_free(lex_filenames);
248 void lex_close(lex_file *lex)
251 for (i = 0; i < vec_size(lex->frames); ++i)
252 mem_d(lex->frames[i].name);
253 vec_free(lex->frames);
256 vec_free(lex->modelname);
259 fs_file_close(lex->file);
262 token_delete(lex->tok);
264 vec_free(lex->tok.value);
266 /* mem_d(lex->name); collected in lex_filenames */
270 static int lex_fgetc(lex_file *lex)
274 return fs_file_getc(lex->file);
276 if (lex->open_string) {
277 if (lex->open_string_pos >= lex->open_string_length)
280 return lex->open_string[lex->open_string_pos++];
285 /* Get or put-back data
286 * The following to functions do NOT understand what kind of data they
288 * The are merely wrapping get/put in order to count line numbers.
290 static void lex_ungetch(lex_file *lex, int ch);
291 static int lex_try_trigraph(lex_file *lex, int old)
295 if (!lex->push_line && c2 == '\n') {
301 lex_ungetch(lex, c2);
306 if (!lex->push_line && c3 == '\n') {
312 case '=': return '#';
313 case '/': return '\\';
314 case '\'': return '^';
315 case '(': return '[';
316 case ')': return ']';
317 case '!': return '|';
318 case '<': return '{';
319 case '>': return '}';
320 case '-': return '~';
322 lex_ungetch(lex, c3);
323 lex_ungetch(lex, c2);
328 static int lex_try_digraph(lex_file *lex, int ch)
332 /* we just used fgetc() so count lines
333 * need to offset a \n the ungetch would recognize
335 if (!lex->push_line && c2 == '\n')
337 if (ch == '<' && c2 == ':')
339 else if (ch == ':' && c2 == '>')
341 else if (ch == '<' && c2 == '%')
343 else if (ch == '%' && c2 == '>')
345 else if (ch == '%' && c2 == ':')
347 lex_ungetch(lex, c2);
351 static int lex_getch(lex_file *lex)
357 if (!lex->push_line && lex->peek[lex->peekpos] == '\n') {
361 return lex->peek[lex->peekpos];
365 if (!lex->push_line && ch == '\n') {
370 return lex_try_trigraph(lex, ch);
371 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
372 return lex_try_digraph(lex, ch);
376 static void lex_ungetch(lex_file *lex, int ch)
378 lex->peek[lex->peekpos++] = ch;
380 if (!lex->push_line && ch == '\n') {
386 /* classify characters
387 * some additions to the is*() functions of ctype.h
390 /* Idents are alphanumberic, but they start with alpha or _ */
391 static bool isident_start(int ch)
393 return util_isalpha(ch) || ch == '_';
396 static bool isident(int ch)
398 return isident_start(ch) || util_isdigit(ch);
401 /* isxdigit_only is used when we already know it's not a digit
402 * and want to see if it's a hex digit anyway.
404 static bool isxdigit_only(int ch)
406 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
409 /* Append a character to the token buffer */
410 static void lex_tokench(lex_file *lex, int ch)
412 vec_push(lex->tok.value, ch);
415 /* Append a trailing null-byte */
416 static void lex_endtoken(lex_file *lex)
418 vec_push(lex->tok.value, 0);
419 vec_shrinkby(lex->tok.value, 1);
422 static bool lex_try_pragma(lex_file *lex)
426 char *command = NULL;
430 if (lex->flags.preprocessing)
437 lex_ungetch(lex, ch);
441 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
442 vec_push(pragma, ch);
445 if (ch != ' ' || strcmp(pragma, "pragma")) {
446 lex_ungetch(lex, ch);
450 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
451 vec_push(command, ch);
452 vec_push(command, 0);
455 lex_ungetch(lex, ch);
459 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
464 lex_ungetch(lex, ch);
468 if (!strcmp(command, "push")) {
469 if (!strcmp(param, "line")) {
471 if (lex->push_line == 1)
477 else if (!strcmp(command, "pop")) {
478 if (!strcmp(param, "line")) {
481 if (lex->push_line == 0)
487 else if (!strcmp(command, "file")) {
488 lex->name = util_strdup(param);
489 vec_push(lex_filenames, lex->name);
491 else if (!strcmp(command, "line")) {
492 line = strtol(param, NULL, 0)-1;
498 while (ch != '\n' && ch != EOF)
508 while (vec_size(command)) {
509 lex_ungetch(lex, (unsigned char)vec_last(command));
513 lex_ungetch(lex, ' ');
517 while (vec_size(param)) {
518 lex_ungetch(lex, (unsigned char)vec_last(param));
522 lex_ungetch(lex, ' ');
526 while (vec_size(pragma)) {
527 lex_ungetch(lex, (unsigned char)vec_last(pragma));
532 lex_ungetch(lex, '#');
538 /* Skip whitespace and comments and return the first
539 * non-white character.
540 * As this makes use of the above getch() ungetch() functions,
541 * we don't need to care at all about line numbering anymore.
543 * In theory, this function should only be used at the beginning
544 * of lexing, or when we *know* the next character is part of the token.
545 * Otherwise, if the parser throws an error, the linenumber may not be
546 * the line of the error, but the line of the next token AFTER the error.
548 * This is currently only problematic when using c-like string-continuation,
549 * since comments and whitespaces are allowed between 2 such strings.
553 "A continuation of the previous string"
554 // This line is skipped
557 * In this case, if the parse decides it didn't actually want a string,
558 * and uses lex->line to print an error, it will show the ', foo);' line's
561 * On the other hand, the parser is supposed to remember the line of the next
562 * token's beginning. In this case we would want skipwhite() to be called
563 * AFTER reading a token, so that the parser, before reading the NEXT token,
564 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
567 * here is to store the line of the first character after skipping
568 * the initial whitespace in lex->sline, this happens in lex_do.
570 static int lex_skipwhite(lex_file *lex, bool hadwhite)
573 bool haswhite = hadwhite;
578 while (ch != EOF && util_isspace(ch)) {
580 if (lex_try_pragma(lex))
583 if (lex->flags.preprocessing) {
586 /* see if there was whitespace first */
587 if (haswhite) { /* (vec_size(lex->tok.value)) { */
588 lex_ungetch(lex, ch);
592 /* otherwise return EOL */
596 lex_tokench(lex, ch);
605 /* one line comment */
608 if (lex->flags.preprocessing) {
611 lex_tokench(lex, '/');
612 lex_tokench(lex, '/');
614 lex_tokench(lex, ' ');
615 lex_tokench(lex, ' ');
618 while (ch != EOF && ch != '\n') {
619 if (lex->flags.preprocessing)
620 lex_tokench(lex, ' '); /* ch); */
623 if (lex->flags.preprocessing) {
624 lex_ungetch(lex, '\n');
632 /* multiline comment */
633 if (lex->flags.preprocessing) {
636 lex_tokench(lex, '/');
637 lex_tokench(lex, '*');
639 lex_tokench(lex, ' ');
640 lex_tokench(lex, ' ');
649 if (lex->flags.preprocessing) {
651 lex_tokench(lex, '*');
652 lex_tokench(lex, '/');
654 lex_tokench(lex, ' ');
655 lex_tokench(lex, ' ');
659 lex_ungetch(lex, ch);
661 if (lex->flags.preprocessing) {
663 lex_tokench(lex, '\n');
665 lex_tokench(lex, ' '); /* ch); */
668 ch = ' '; /* cause TRUE in the isspace check */
671 /* Otherwise roll back to the slash and break out of the loop */
672 lex_ungetch(lex, ch);
676 } while (ch != EOF && util_isspace(ch));
680 lex_ungetch(lex, ch);
687 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
692 while (ch != EOF && isident(ch))
694 lex_tokench(lex, ch);
698 /* last ch was not an ident ch: */
699 lex_ungetch(lex, ch);
704 /* read one ident for the frame list */
705 static int lex_parse_frame(lex_file *lex)
712 while (ch != EOF && ch != '\n' && util_isspace(ch))
718 if (!isident_start(ch)) {
719 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
723 lex_tokench(lex, ch);
724 if (!lex_finish_ident(lex))
730 /* read a list of $frames */
731 static bool lex_finish_frames(lex_file *lex)
738 rc = lex_parse_frame(lex);
739 if (rc > 0) /* end of line */
741 if (rc < 0) /* error */
744 for (i = 0; i < vec_size(lex->frames); ++i) {
745 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
746 lex->frames[i].value = lex->framevalue++;
747 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
752 if (i < vec_size(lex->frames))
755 m.value = lex->framevalue++;
756 m.name = util_strdup(lex->tok.value);
757 vec_shrinkto(lex->tok.value, 0);
758 vec_push(lex->frames, m);
764 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
770 char u8buf[8]; /* way more than enough */
777 return TOKEN_STRINGCONST;
779 if (lex->flags.preprocessing && ch == '\\') {
780 lex_tokench(lex, ch);
783 lexerror(lex, "unexpected end of file");
784 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
785 return (lex->tok.ttype = TOKEN_ERROR);
787 lex_tokench(lex, ch);
789 else if (ch == '\\') {
792 lexerror(lex, "unexpected end of file");
793 lex_ungetch(lex, 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');
856 lex_ungetch(lex, nextch);
857 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
859 if (nextch >= '0' && nextch <= '9')
860 chr = chr * 10 + nextch - '0';
862 lexerror(lex, "bad character code");
863 return (lex->tok.ttype = TOKEN_ERROR);
866 if (nextch >= '0' && nextch <= '9')
867 chr = chr * 0x10 + nextch - '0';
868 else if (nextch >= 'a' && nextch <= 'f')
869 chr = chr * 0x10 + nextch - 'a' + 10;
870 else if (nextch >= 'A' && nextch <= 'F')
871 chr = chr * 0x10 + nextch - 'A' + 10;
873 lexerror(lex, "bad character code");
874 return (lex->tok.ttype = TOKEN_ERROR);
877 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
879 lexerror(lex, "character code out of range");
880 return (lex->tok.ttype = TOKEN_ERROR);
883 if (OPTS_FLAG(UTF8) && chr >= 128) {
884 u8len = utf8_from(u8buf, chr);
889 lex->column += u8len;
890 for (uc = 0; uc < u8len; ++uc)
891 lex_tokench(lex, u8buf[uc]);
893 * the last character will be inserted with the tokench() call
902 case '\n': ch = '\n'; break;
905 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
906 /* so we just add the character plus backslash no matter what it actually is */
907 lex_tokench(lex, '\\');
909 /* add the character finally */
910 lex_tokench(lex, ch);
913 lex_tokench(lex, ch);
915 lexerror(lex, "unexpected end of file within string constant");
916 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
917 return (lex->tok.ttype = TOKEN_ERROR);
920 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
926 /* parse a number... */
928 lex->tok.ttype = TOKEN_FLOATCONST;
930 lex->tok.ttype = TOKEN_INTCONST;
932 lex_tokench(lex, ch);
935 if (ch != '.' && !util_isdigit(ch))
937 if (lastch != '0' || ch != 'x')
939 /* end of the number or EOF */
940 lex_ungetch(lex, ch);
943 lex->tok.constval.i = lastch - '0';
944 return lex->tok.ttype;
950 /* EOF would have been caught above */
954 lex_tokench(lex, ch);
956 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
958 lex_tokench(lex, ch);
962 /* NOT else, '.' can come from above as well */
963 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
965 /* Allow floating comma in non-hex mode */
966 lex->tok.ttype = TOKEN_FLOATCONST;
967 lex_tokench(lex, ch);
969 /* continue digits-only */
971 while (util_isdigit(ch))
973 lex_tokench(lex, ch);
977 /* put back the last character */
978 /* but do not put back the trailing 'f' or a float */
979 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
982 /* generally we don't want words to follow numbers: */
984 lexerror(lex, "unexpected trailing characters after number");
985 return (lex->tok.ttype = TOKEN_ERROR);
987 lex_ungetch(lex, ch);
990 if (lex->tok.ttype == TOKEN_FLOATCONST)
991 lex->tok.constval.f = strtod(lex->tok.value, NULL);
993 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
994 return lex->tok.ttype;
997 int lex_do(lex_file *lex)
999 int ch, nextch, thirdch;
1000 bool hadwhite = false;
1009 ch = lex_skipwhite(lex, hadwhite);
1011 if (!lex->flags.mergelines || ch != '\\')
1013 ch = lex_getch(lex);
1015 ch = lex_getch(lex);
1017 lex_ungetch(lex, ch);
1021 /* we reached a linemerge */
1022 lex_tokench(lex, '\n');
1026 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1027 return (lex->tok.ttype = ch);
1030 lex->sline = lex->line;
1031 lex->tok.ctx.line = lex->sline;
1032 lex->tok.ctx.file = lex->name;
1035 return (lex->tok.ttype = TOKEN_FATAL);
1039 return (lex->tok.ttype = TOKEN_EOF);
1042 /* modelgen / spiritgen commands */
1043 if (ch == '$' && !lex->flags.preprocessing) {
1047 ch = lex_getch(lex);
1048 if (!isident_start(ch)) {
1049 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1052 lex_tokench(lex, ch);
1053 if (!lex_finish_ident(lex))
1054 return (lex->tok.ttype = TOKEN_ERROR);
1056 /* skip the known commands */
1059 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1061 /* frame/framesave command works like an enum
1062 * similar to fteqcc we handle this in the lexer.
1063 * The reason for this is that it is sensitive to newlines,
1064 * which the parser is unaware of
1066 if (!lex_finish_frames(lex))
1067 return (lex->tok.ttype = TOKEN_ERROR);
1071 if (!strcmp(v, "framevalue"))
1073 ch = lex_getch(lex);
1074 while (ch != EOF && util_isspace(ch) && ch != '\n')
1075 ch = lex_getch(lex);
1077 if (!util_isdigit(ch)) {
1078 lexerror(lex, "$framevalue requires an integer parameter");
1083 lex->tok.ttype = lex_finish_digit(lex, ch);
1085 if (lex->tok.ttype != TOKEN_INTCONST) {
1086 lexerror(lex, "$framevalue requires an integer parameter");
1089 lex->framevalue = lex->tok.constval.i;
1093 if (!strcmp(v, "framerestore"))
1099 rc = lex_parse_frame(lex);
1102 lexerror(lex, "$framerestore requires a framename parameter");
1106 return (lex->tok.ttype = TOKEN_FATAL);
1109 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1110 if (!strcmp(v, lex->frames[frame].name)) {
1111 lex->framevalue = lex->frames[frame].value;
1115 lexerror(lex, "unknown framename `%s`", v);
1119 if (!strcmp(v, "modelname"))
1125 rc = lex_parse_frame(lex);
1128 lexerror(lex, "$modelname requires a parameter");
1132 return (lex->tok.ttype = TOKEN_FATAL);
1134 if (lex->modelname) {
1136 m.value = lex->framevalue;
1137 m.name = lex->modelname;
1138 lex->modelname = NULL;
1139 vec_push(lex->frames, m);
1141 lex->modelname = lex->tok.value;
1142 lex->tok.value = NULL;
1146 if (!strcmp(v, "flush"))
1149 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1150 mem_d(lex->frames[fi].name);
1151 vec_free(lex->frames);
1152 /* skip line (fteqcc does it too) */
1153 ch = lex_getch(lex);
1154 while (ch != EOF && ch != '\n')
1155 ch = lex_getch(lex);
1159 if (!strcmp(v, "cd") ||
1160 !strcmp(v, "origin") ||
1161 !strcmp(v, "base") ||
1162 !strcmp(v, "flags") ||
1163 !strcmp(v, "scale") ||
1167 ch = lex_getch(lex);
1168 while (ch != EOF && ch != '\n')
1169 ch = lex_getch(lex);
1173 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1174 if (!strcmp(v, lex->frames[frame].name)) {
1175 lex->tok.constval.i = lex->frames[frame].value;
1176 return (lex->tok.ttype = TOKEN_INTCONST);
1180 lexerror(lex, "invalid frame macro");
1184 /* single-character tokens */
1188 nextch = lex_getch(lex);
1189 if (nextch == '[') {
1190 lex_tokench(lex, ch);
1191 lex_tokench(lex, nextch);
1193 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1195 lex_ungetch(lex, nextch);
1200 lex_tokench(lex, ch);
1202 if (lex->flags.noops)
1203 return (lex->tok.ttype = ch);
1205 return (lex->tok.ttype = TOKEN_OPERATOR);
1208 if (lex->flags.noops) {
1209 nextch = lex_getch(lex);
1210 if (nextch == ']') {
1211 lex_tokench(lex, ch);
1212 lex_tokench(lex, nextch);
1214 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1216 lex_ungetch(lex, nextch);
1225 lex_tokench(lex, ch);
1227 return (lex->tok.ttype = ch);
1233 nextch = lex_getch(lex);
1234 /* digits starting with a dot */
1235 if (util_isdigit(nextch)) {
1236 lex_ungetch(lex, nextch);
1237 lex->tok.ttype = lex_finish_digit(lex, ch);
1239 return lex->tok.ttype;
1241 lex_ungetch(lex, nextch);
1244 if (lex->flags.noops)
1246 /* Detect characters early which are normally
1247 * operators OR PART of an operator.
1266 lex_tokench(lex, ch);
1268 return (lex->tok.ttype = ch);
1276 lex_tokench(lex, ch);
1277 /* peak ahead once */
1278 nextch = lex_getch(lex);
1279 if (nextch != '.') {
1280 lex_ungetch(lex, nextch);
1282 if (lex->flags.noops)
1283 return (lex->tok.ttype = ch);
1285 return (lex->tok.ttype = TOKEN_OPERATOR);
1287 /* peak ahead again */
1288 nextch = lex_getch(lex);
1289 if (nextch != '.') {
1290 lex_ungetch(lex, nextch);
1291 lex_ungetch(lex, '.');
1293 if (lex->flags.noops)
1294 return (lex->tok.ttype = ch);
1296 return (lex->tok.ttype = TOKEN_OPERATOR);
1298 /* fill the token to be "..." */
1299 lex_tokench(lex, ch);
1300 lex_tokench(lex, ch);
1302 return (lex->tok.ttype = TOKEN_DOTS);
1305 if (ch == ',' || ch == '.') {
1306 lex_tokench(lex, ch);
1308 return (lex->tok.ttype = TOKEN_OPERATOR);
1311 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1312 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1313 ch == '=' || ch == '!' || /* <=>, ==, != */
1314 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1315 ch == '~' || ch == '^' /* ~=, ~, ^ */
1317 lex_tokench(lex, ch);
1319 nextch = lex_getch(lex);
1320 if ((nextch == '=' && ch != '<') ||
1321 (nextch == ch && ch != '!') ||
1322 (nextch == '<' && ch == '>')) {
1323 lex_tokench(lex, nextch);
1324 } else if (ch == '<' && nextch == '=') {
1325 lex_tokench(lex, nextch);
1326 if ((thirdch = lex_getch(lex)) == '>')
1327 lex_tokench(lex, thirdch);
1329 lex_ungetch(lex, thirdch);
1331 } else if (ch == '-' && nextch == '>') {
1332 lex_tokench(lex, nextch);
1333 } else if (ch == '&' && nextch == '~') {
1334 thirdch = lex_getch(lex);
1335 if (thirdch != '=') {
1336 lex_ungetch(lex, thirdch);
1337 lex_ungetch(lex, nextch);
1340 lex_tokench(lex, nextch);
1341 lex_tokench(lex, thirdch);
1344 else if (lex->flags.preprocessing &&
1345 ch == '-' && util_isdigit(nextch))
1347 lex->tok.ttype = lex_finish_digit(lex, nextch);
1348 if (lex->tok.ttype == TOKEN_INTCONST)
1349 lex->tok.constval.i = -lex->tok.constval.i;
1351 lex->tok.constval.f = -lex->tok.constval.f;
1353 return lex->tok.ttype;
1355 lex_ungetch(lex, nextch);
1359 return (lex->tok.ttype = TOKEN_OPERATOR);
1363 if (ch == '^' || ch == '~' || ch == '!')
1365 lex_tokench(lex, ch);
1367 return (lex->tok.ttype = TOKEN_OPERATOR);
1371 if (ch == '*' || ch == '/') /* *=, /= */
1373 lex_tokench(lex, ch);
1375 nextch = lex_getch(lex);
1376 if (nextch == '=' || nextch == '*') {
1377 lex_tokench(lex, nextch);
1379 lex_ungetch(lex, nextch);
1382 return (lex->tok.ttype = TOKEN_OPERATOR);
1386 lex_tokench(lex, ch);
1388 return (lex->tok.ttype = TOKEN_OPERATOR);
1391 if (isident_start(ch))
1395 lex_tokench(lex, ch);
1396 if (!lex_finish_ident(lex)) {
1398 return (lex->tok.ttype = TOKEN_ERROR);
1401 lex->tok.ttype = TOKEN_IDENT;
1404 if (!strcmp(v, "void")) {
1405 lex->tok.ttype = TOKEN_TYPENAME;
1406 lex->tok.constval.t = TYPE_VOID;
1407 } else if (!strcmp(v, "int")) {
1408 lex->tok.ttype = TOKEN_TYPENAME;
1409 lex->tok.constval.t = TYPE_INTEGER;
1410 } else if (!strcmp(v, "float")) {
1411 lex->tok.ttype = TOKEN_TYPENAME;
1412 lex->tok.constval.t = TYPE_FLOAT;
1413 } else if (!strcmp(v, "string")) {
1414 lex->tok.ttype = TOKEN_TYPENAME;
1415 lex->tok.constval.t = TYPE_STRING;
1416 } else if (!strcmp(v, "entity")) {
1417 lex->tok.ttype = TOKEN_TYPENAME;
1418 lex->tok.constval.t = TYPE_ENTITY;
1419 } else if (!strcmp(v, "vector")) {
1420 lex->tok.ttype = TOKEN_TYPENAME;
1421 lex->tok.constval.t = TYPE_VECTOR;
1424 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1425 if (!strcmp(v, keywords_qc[kw]))
1426 return (lex->tok.ttype = TOKEN_KEYWORD);
1428 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1429 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1430 if (!strcmp(v, keywords_fg[kw]))
1431 return (lex->tok.ttype = TOKEN_KEYWORD);
1436 return lex->tok.ttype;
1441 lex->flags.nodigraphs = true;
1442 if (lex->flags.preprocessing)
1443 lex_tokench(lex, ch);
1444 lex->tok.ttype = lex_finish_string(lex, '"');
1445 if (lex->flags.preprocessing)
1446 lex_tokench(lex, ch);
1447 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1449 /* Allow c style "string" "continuation" */
1450 ch = lex_skipwhite(lex, false);
1452 lex_ungetch(lex, ch);
1456 lex->tok.ttype = lex_finish_string(lex, '"');
1458 lex->flags.nodigraphs = false;
1460 return lex->tok.ttype;
1465 /* we parse character constants like string,
1466 * but return TOKEN_CHARCONST, or a vector type if it fits...
1467 * Likewise actual unescaping has to be done by the parser.
1468 * The difference is we don't allow 'char' 'continuation'.
1470 if (lex->flags.preprocessing)
1471 lex_tokench(lex, ch);
1472 lex->tok.ttype = lex_finish_string(lex, '\'');
1473 if (lex->flags.preprocessing)
1474 lex_tokench(lex, ch);
1477 lex->tok.ttype = TOKEN_CHARCONST;
1479 /* It's a vector if we can successfully scan 3 floats */
1480 if (platform_sscanf(lex->tok.value, " %f %f %f ",
1481 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1484 lex->tok.ttype = TOKEN_VECTORCONST;
1488 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1490 /* check for a valid utf8 character */
1491 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1492 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1493 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1494 : "multibyte character: `%s`" ),
1496 return (lex->tok.ttype = TOKEN_ERROR);
1499 lex->tok.constval.i = u8char;
1502 lex->tok.constval.i = lex->tok.value[0];
1505 return lex->tok.ttype;
1508 if (util_isdigit(ch))
1510 lex->tok.ttype = lex_finish_digit(lex, ch);
1512 return lex->tok.ttype;
1515 if (lex->flags.preprocessing) {
1516 lex_tokench(lex, ch);
1518 return (lex->tok.ttype = ch);
1521 lexerror(lex, "unknown token: `%c`", ch);
1522 return (lex->tok.ttype = TOKEN_ERROR);