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)
769 char u8buf[8]; /* way more than enough */
772 while (ch != FS_FILE_EOF)
776 return TOKEN_STRINGCONST;
778 if (lex->flags.preprocessing && ch == '\\') {
779 lex_tokench(lex, ch);
781 if (ch == FS_FILE_EOF) {
782 lexerror(lex, "unexpected end of file");
783 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
784 return (lex->tok.ttype = TOKEN_ERROR);
786 lex_tokench(lex, ch);
788 else if (ch == '\\') {
790 if (ch == FS_FILE_EOF) {
791 lexerror(lex, "unexpected end of file");
792 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
793 return (lex->tok.ttype = TOKEN_ERROR);
800 case 'a': ch = '\a'; break;
801 case 'b': ch = '\b'; break;
802 case 'r': ch = '\r'; break;
803 case 'n': ch = '\n'; break;
804 case 't': ch = '\t'; break;
805 case 'f': ch = '\f'; break;
806 case 'v': ch = '\v'; break;
809 /* same procedure as in fteqcc */
811 nextch = lex_getch(lex);
812 if (nextch >= '0' && nextch <= '9')
814 else if (nextch >= 'a' && nextch <= 'f')
815 ch += nextch - 'a' + 10;
816 else if (nextch >= 'A' && nextch <= 'F')
817 ch += nextch - 'A' + 10;
819 lexerror(lex, "bad character code");
820 lex_ungetch(lex, nextch);
821 return (lex->tok.ttype = TOKEN_ERROR);
825 nextch = lex_getch(lex);
826 if (nextch >= '0' && nextch <= '9')
828 else if (nextch >= 'a' && nextch <= 'f')
829 ch += nextch - 'a' + 10;
830 else if (nextch >= 'A' && nextch <= 'F')
831 ch += nextch - 'A' + 10;
833 lexerror(lex, "bad character code");
834 lex_ungetch(lex, nextch);
835 return (lex->tok.ttype = TOKEN_ERROR);
840 case '0': case '1': case '2': case '3':
841 case '4': case '5': case '6': case '7':
845 case '<': ch = 29; break;
846 case '-': ch = 30; break;
847 case '>': ch = 31; break;
848 case '[': ch = 16; break;
849 case ']': ch = 17; break;
852 nextch = lex_getch(lex);
853 hex = (nextch == 'x');
855 lex_ungetch(lex, nextch);
856 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
858 if (nextch >= '0' && nextch <= '9')
859 chr = chr * 10 + nextch - '0';
861 lexerror(lex, "bad character code");
862 return (lex->tok.ttype = TOKEN_ERROR);
865 if (nextch >= '0' && nextch <= '9')
866 chr = chr * 0x10 + nextch - '0';
867 else if (nextch >= 'a' && nextch <= 'f')
868 chr = chr * 0x10 + nextch - 'a' + 10;
869 else if (nextch >= 'A' && nextch <= 'F')
870 chr = chr * 0x10 + nextch - 'A' + 10;
872 lexerror(lex, "bad character code");
873 return (lex->tok.ttype = TOKEN_ERROR);
876 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
878 lexerror(lex, "character code out of range");
879 return (lex->tok.ttype = TOKEN_ERROR);
882 if (OPTS_FLAG(UTF8) && chr >= 128) {
883 u8len = utf8_from(u8buf, chr);
888 lex->column += u8len;
889 for (uc = 0; uc < u8len; ++uc)
890 lex_tokench(lex, u8buf[uc]);
892 * the last character will be inserted with the tokench() call
901 case '\n': ch = '\n'; break;
904 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
905 /* so we just add the character plus backslash no matter what it actually is */
906 lex_tokench(lex, '\\');
908 /* add the character finally */
909 lex_tokench(lex, ch);
912 lex_tokench(lex, ch);
914 lexerror(lex, "unexpected end of file within string constant");
915 lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
916 return (lex->tok.ttype = TOKEN_ERROR);
919 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
925 /* parse a number... */
927 lex->tok.ttype = TOKEN_FLOATCONST;
929 lex->tok.ttype = TOKEN_INTCONST;
931 lex_tokench(lex, ch);
934 if (ch != '.' && !util_isdigit(ch))
936 if (lastch != '0' || ch != 'x')
938 /* end of the number or EOF */
939 lex_ungetch(lex, ch);
942 lex->tok.constval.i = lastch - '0';
943 return lex->tok.ttype;
949 /* EOF would have been caught above */
953 lex_tokench(lex, ch);
955 while (util_isdigit(ch) || (ishex && isxdigit_only(ch)))
957 lex_tokench(lex, ch);
961 /* NOT else, '.' can come from above as well */
962 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
964 /* Allow floating comma in non-hex mode */
965 lex->tok.ttype = TOKEN_FLOATCONST;
966 lex_tokench(lex, ch);
968 /* continue digits-only */
970 while (util_isdigit(ch))
972 lex_tokench(lex, ch);
976 /* put back the last character */
977 /* but do not put back the trailing 'f' or a float */
978 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
981 /* generally we don't want words to follow numbers: */
983 lexerror(lex, "unexpected trailing characters after number");
984 return (lex->tok.ttype = TOKEN_ERROR);
986 lex_ungetch(lex, ch);
989 if (lex->tok.ttype == TOKEN_FLOATCONST)
990 lex->tok.constval.f = strtod(lex->tok.value, NULL);
992 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
993 return lex->tok.ttype;
996 int lex_do(lex_file *lex)
998 int ch, nextch, thirdch;
999 bool hadwhite = false;
1008 ch = lex_skipwhite(lex, hadwhite);
1010 if (!lex->flags.mergelines || ch != '\\')
1012 ch = lex_getch(lex);
1014 ch = lex_getch(lex);
1016 lex_ungetch(lex, ch);
1020 /* we reached a linemerge */
1021 lex_tokench(lex, '\n');
1025 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1026 return (lex->tok.ttype = ch);
1029 lex->sline = lex->line;
1030 lex->tok.ctx.line = lex->sline;
1031 lex->tok.ctx.file = lex->name;
1034 return (lex->tok.ttype = TOKEN_FATAL);
1036 if (ch == FS_FILE_EOF) {
1038 return (lex->tok.ttype = TOKEN_EOF);
1041 /* modelgen / spiritgen commands */
1042 if (ch == '$' && !lex->flags.preprocessing) {
1046 ch = lex_getch(lex);
1047 if (!isident_start(ch)) {
1048 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1051 lex_tokench(lex, ch);
1052 if (!lex_finish_ident(lex))
1053 return (lex->tok.ttype = TOKEN_ERROR);
1055 /* skip the known commands */
1058 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1060 /* frame/framesave command works like an enum
1061 * similar to fteqcc we handle this in the lexer.
1062 * The reason for this is that it is sensitive to newlines,
1063 * which the parser is unaware of
1065 if (!lex_finish_frames(lex))
1066 return (lex->tok.ttype = TOKEN_ERROR);
1070 if (!strcmp(v, "framevalue"))
1072 ch = lex_getch(lex);
1073 while (ch != FS_FILE_EOF && util_isspace(ch) && ch != '\n')
1074 ch = lex_getch(lex);
1076 if (!util_isdigit(ch)) {
1077 lexerror(lex, "$framevalue requires an integer parameter");
1082 lex->tok.ttype = lex_finish_digit(lex, ch);
1084 if (lex->tok.ttype != TOKEN_INTCONST) {
1085 lexerror(lex, "$framevalue requires an integer parameter");
1088 lex->framevalue = lex->tok.constval.i;
1092 if (!strcmp(v, "framerestore"))
1098 rc = lex_parse_frame(lex);
1101 lexerror(lex, "$framerestore requires a framename parameter");
1105 return (lex->tok.ttype = TOKEN_FATAL);
1108 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1109 if (!strcmp(v, lex->frames[frame].name)) {
1110 lex->framevalue = lex->frames[frame].value;
1114 lexerror(lex, "unknown framename `%s`", v);
1118 if (!strcmp(v, "modelname"))
1124 rc = lex_parse_frame(lex);
1127 lexerror(lex, "$modelname requires a parameter");
1131 return (lex->tok.ttype = TOKEN_FATAL);
1133 if (lex->modelname) {
1135 m.value = lex->framevalue;
1136 m.name = lex->modelname;
1137 lex->modelname = NULL;
1138 vec_push(lex->frames, m);
1140 lex->modelname = lex->tok.value;
1141 lex->tok.value = NULL;
1145 if (!strcmp(v, "flush"))
1148 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1149 mem_d(lex->frames[fi].name);
1150 vec_free(lex->frames);
1151 /* skip line (fteqcc does it too) */
1152 ch = lex_getch(lex);
1153 while (ch != FS_FILE_EOF && ch != '\n')
1154 ch = lex_getch(lex);
1158 if (!strcmp(v, "cd") ||
1159 !strcmp(v, "origin") ||
1160 !strcmp(v, "base") ||
1161 !strcmp(v, "flags") ||
1162 !strcmp(v, "scale") ||
1166 ch = lex_getch(lex);
1167 while (ch != FS_FILE_EOF && ch != '\n')
1168 ch = lex_getch(lex);
1172 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1173 if (!strcmp(v, lex->frames[frame].name)) {
1174 lex->tok.constval.i = lex->frames[frame].value;
1175 return (lex->tok.ttype = TOKEN_INTCONST);
1179 lexerror(lex, "invalid frame macro");
1183 /* single-character tokens */
1187 nextch = lex_getch(lex);
1188 if (nextch == '[') {
1189 lex_tokench(lex, ch);
1190 lex_tokench(lex, nextch);
1192 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1194 lex_ungetch(lex, nextch);
1199 lex_tokench(lex, ch);
1201 if (lex->flags.noops)
1202 return (lex->tok.ttype = ch);
1204 return (lex->tok.ttype = TOKEN_OPERATOR);
1207 if (lex->flags.noops) {
1208 nextch = lex_getch(lex);
1209 if (nextch == ']') {
1210 lex_tokench(lex, ch);
1211 lex_tokench(lex, nextch);
1213 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1215 lex_ungetch(lex, nextch);
1224 lex_tokench(lex, ch);
1226 return (lex->tok.ttype = ch);
1232 nextch = lex_getch(lex);
1233 /* digits starting with a dot */
1234 if (util_isdigit(nextch)) {
1235 lex_ungetch(lex, nextch);
1236 lex->tok.ttype = lex_finish_digit(lex, ch);
1238 return lex->tok.ttype;
1240 lex_ungetch(lex, nextch);
1243 if (lex->flags.noops)
1245 /* Detect characters early which are normally
1246 * operators OR PART of an operator.
1265 lex_tokench(lex, ch);
1267 return (lex->tok.ttype = ch);
1275 lex_tokench(lex, ch);
1276 /* peak ahead once */
1277 nextch = lex_getch(lex);
1278 if (nextch != '.') {
1279 lex_ungetch(lex, nextch);
1281 if (lex->flags.noops)
1282 return (lex->tok.ttype = ch);
1284 return (lex->tok.ttype = TOKEN_OPERATOR);
1286 /* peak ahead again */
1287 nextch = lex_getch(lex);
1288 if (nextch != '.') {
1289 lex_ungetch(lex, nextch);
1290 lex_ungetch(lex, '.');
1292 if (lex->flags.noops)
1293 return (lex->tok.ttype = ch);
1295 return (lex->tok.ttype = TOKEN_OPERATOR);
1297 /* fill the token to be "..." */
1298 lex_tokench(lex, ch);
1299 lex_tokench(lex, ch);
1301 return (lex->tok.ttype = TOKEN_DOTS);
1304 if (ch == ',' || ch == '.') {
1305 lex_tokench(lex, ch);
1307 return (lex->tok.ttype = TOKEN_OPERATOR);
1310 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1311 ch == '>' || ch == '<' || /* <<, >>, <=, >= and >< as well! */
1312 ch == '=' || ch == '!' || /* <=>, ==, != */
1313 ch == '&' || ch == '|' || /* &&, ||, &=, |= */
1314 ch == '~' || ch == '^' /* ~=, ~, ^ */
1316 lex_tokench(lex, ch);
1318 nextch = lex_getch(lex);
1319 if ((nextch == '=' && ch != '<') ||
1320 (nextch == ch && ch != '!') ||
1321 (nextch == '<' && ch == '>')) {
1322 lex_tokench(lex, nextch);
1323 } else if (ch == '<' && nextch == '=') {
1324 lex_tokench(lex, nextch);
1325 if ((thirdch = lex_getch(lex)) == '>')
1326 lex_tokench(lex, thirdch);
1328 lex_ungetch(lex, thirdch);
1330 } else if (ch == '-' && nextch == '>') {
1331 lex_tokench(lex, nextch);
1332 } else if (ch == '&' && nextch == '~') {
1333 thirdch = lex_getch(lex);
1334 if (thirdch != '=') {
1335 lex_ungetch(lex, thirdch);
1336 lex_ungetch(lex, nextch);
1339 lex_tokench(lex, nextch);
1340 lex_tokench(lex, thirdch);
1343 else if (lex->flags.preprocessing &&
1344 ch == '-' && util_isdigit(nextch))
1346 lex->tok.ttype = lex_finish_digit(lex, nextch);
1347 if (lex->tok.ttype == TOKEN_INTCONST)
1348 lex->tok.constval.i = -lex->tok.constval.i;
1350 lex->tok.constval.f = -lex->tok.constval.f;
1352 return lex->tok.ttype;
1354 lex_ungetch(lex, nextch);
1358 return (lex->tok.ttype = TOKEN_OPERATOR);
1362 if (ch == '^' || ch == '~' || ch == '!')
1364 lex_tokench(lex, ch);
1366 return (lex->tok.ttype = TOKEN_OPERATOR);
1370 if (ch == '*' || ch == '/') /* *=, /= */
1372 lex_tokench(lex, ch);
1374 nextch = lex_getch(lex);
1375 if (nextch == '=' || nextch == '*') {
1376 lex_tokench(lex, nextch);
1378 lex_ungetch(lex, nextch);
1381 return (lex->tok.ttype = TOKEN_OPERATOR);
1385 lex_tokench(lex, ch);
1387 return (lex->tok.ttype = TOKEN_OPERATOR);
1390 if (isident_start(ch))
1394 lex_tokench(lex, ch);
1395 if (!lex_finish_ident(lex)) {
1397 return (lex->tok.ttype = TOKEN_ERROR);
1400 lex->tok.ttype = TOKEN_IDENT;
1403 if (!strcmp(v, "void")) {
1404 lex->tok.ttype = TOKEN_TYPENAME;
1405 lex->tok.constval.t = TYPE_VOID;
1406 } else if (!strcmp(v, "int")) {
1407 lex->tok.ttype = TOKEN_TYPENAME;
1408 lex->tok.constval.t = TYPE_INTEGER;
1409 } else if (!strcmp(v, "float")) {
1410 lex->tok.ttype = TOKEN_TYPENAME;
1411 lex->tok.constval.t = TYPE_FLOAT;
1412 } else if (!strcmp(v, "string")) {
1413 lex->tok.ttype = TOKEN_TYPENAME;
1414 lex->tok.constval.t = TYPE_STRING;
1415 } else if (!strcmp(v, "entity")) {
1416 lex->tok.ttype = TOKEN_TYPENAME;
1417 lex->tok.constval.t = TYPE_ENTITY;
1418 } else if (!strcmp(v, "vector")) {
1419 lex->tok.ttype = TOKEN_TYPENAME;
1420 lex->tok.constval.t = TYPE_VECTOR;
1423 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_qc); ++kw) {
1424 if (!strcmp(v, keywords_qc[kw]))
1425 return (lex->tok.ttype = TOKEN_KEYWORD);
1427 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC) {
1428 for (kw = 0; kw < GMQCC_ARRAY_COUNT(keywords_fg); ++kw) {
1429 if (!strcmp(v, keywords_fg[kw]))
1430 return (lex->tok.ttype = TOKEN_KEYWORD);
1435 return lex->tok.ttype;
1440 lex->flags.nodigraphs = true;
1441 if (lex->flags.preprocessing)
1442 lex_tokench(lex, ch);
1443 lex->tok.ttype = lex_finish_string(lex, '"');
1444 if (lex->flags.preprocessing)
1445 lex_tokench(lex, ch);
1446 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1448 /* Allow c style "string" "continuation" */
1449 ch = lex_skipwhite(lex, false);
1451 lex_ungetch(lex, ch);
1455 lex->tok.ttype = lex_finish_string(lex, '"');
1457 lex->flags.nodigraphs = false;
1459 return lex->tok.ttype;
1464 /* we parse character constants like string,
1465 * but return TOKEN_CHARCONST, or a vector type if it fits...
1466 * Likewise actual unescaping has to be done by the parser.
1467 * The difference is we don't allow 'char' 'continuation'.
1469 if (lex->flags.preprocessing)
1470 lex_tokench(lex, ch);
1471 lex->tok.ttype = lex_finish_string(lex, '\'');
1472 if (lex->flags.preprocessing)
1473 lex_tokench(lex, ch);
1476 lex->tok.ttype = TOKEN_CHARCONST;
1478 /* It's a vector if we can successfully scan 3 floats */
1479 if (util_sscanf(lex->tok.value, " %f %f %f ",
1480 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1483 lex->tok.ttype = TOKEN_VECTORCONST;
1487 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1489 /* check for a valid utf8 character */
1490 if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
1491 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1492 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1493 : "multibyte character: `%s`" ),
1495 return (lex->tok.ttype = TOKEN_ERROR);
1498 lex->tok.constval.i = u8char;
1501 lex->tok.constval.i = lex->tok.value[0];
1504 return lex->tok.ttype;
1507 if (util_isdigit(ch))
1509 lex->tok.ttype = lex_finish_digit(lex, ch);
1511 return lex->tok.ttype;
1514 if (lex->flags.preprocessing) {
1515 lex_tokench(lex, ch);
1517 return (lex->tok.ttype = ch);
1520 lexerror(lex, "unknown token: `%c`", ch);
1521 return (lex->tok.ttype = TOKEN_ERROR);