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
36 static const char *keywords_qc[] = {
43 static size_t num_keywords_qc = sizeof(keywords_qc) / sizeof(keywords_qc[0]);
46 static const char *keywords_fg[] = {
47 "switch", "case", "default",
53 "__builtin_debug_printtype"
55 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
63 void lexerror(lex_file *lex, const char *fmt, ...)
69 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
71 con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
75 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
82 ctx.line = lex->sline;
85 r = vcompile_warning(ctx, warntype, fmt, ap);
94 token *tok = (token*)mem_a(sizeof(token));
97 memset(tok, 0, sizeof(*tok));
101 void token_delete(token *self)
103 if (self->next && self->next->prev == self)
104 self->next->prev = self->prev;
105 if (self->prev && self->prev->next == self)
106 self->prev->next = self->next;
107 MEM_VECTOR_CLEAR(self, value);
111 token* token_copy(const token *cp)
113 token* self = token_new();
117 self->value_alloc = cp->value_count + 1;
118 self->value_count = cp->value_count;
119 self->value = (char*)mem_a(self->value_alloc);
124 memcpy(self->value, cp->value, cp->value_count);
125 self->value[self->value_alloc-1] = 0;
129 self->ttype = cp->ttype;
130 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
134 void token_delete_all(token *t)
145 token* token_copy_all(const token *cp)
150 out = cur = token_copy(cp);
156 cur->next = token_copy(cp);
158 token_delete_all(out);
161 cur->next->prev = cur;
168 static void lex_token_new(lex_file *lex)
172 token_delete(lex->tok);
173 lex->tok = token_new();
176 vec_shrinkto(lex->tok.value, 0);
177 lex->tok.constval.t = 0;
178 lex->tok.ctx.line = lex->sline;
179 lex->tok.ctx.file = lex->name;
184 lex_file* lex_open(const char *file)
187 FILE *in = file_open(file, "rb");
190 lexerror(NULL, "open failed: '%s'\n", file);
194 lex = (lex_file*)mem_a(sizeof(*lex));
197 lexerror(NULL, "out of memory\n");
201 memset(lex, 0, sizeof(*lex));
204 lex->name = util_strdup(file);
205 lex->line = 1; /* we start counting at 1 */
210 vec_push(lex_filenames, lex->name);
214 lex_file* lex_open_string(const char *str, size_t len, const char *name)
218 lex = (lex_file*)mem_a(sizeof(*lex));
220 lexerror(NULL, "out of memory\n");
224 memset(lex, 0, sizeof(*lex));
227 lex->open_string = str;
228 lex->open_string_length = len;
229 lex->open_string_pos = 0;
231 lex->name = util_strdup(name ? name : "<string-source>");
232 lex->line = 1; /* we start counting at 1 */
237 vec_push(lex_filenames, lex->name);
242 void lex_cleanup(void)
245 for (i = 0; i < vec_size(lex_filenames); ++i)
246 mem_d(lex_filenames[i]);
247 vec_free(lex_filenames);
250 void lex_close(lex_file *lex)
253 for (i = 0; i < vec_size(lex->frames); ++i)
254 mem_d(lex->frames[i].name);
255 vec_free(lex->frames);
258 vec_free(lex->modelname);
261 file_close(lex->file);
264 token_delete(lex->tok);
266 vec_free(lex->tok.value);
268 /* mem_d(lex->name); collected in lex_filenames */
272 static int lex_fgetc(lex_file *lex)
275 return fgetc(lex->file);
276 if (lex->open_string) {
277 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)
295 lex_ungetch(lex, c2);
301 case '=': return '#';
302 case '/': return '\\';
303 case '\'': return '^';
304 case '(': return '[';
305 case ')': return ']';
306 case '!': return '|';
307 case '<': return '{';
308 case '>': return '}';
309 case '-': return '~';
311 lex_ungetch(lex, c3);
312 lex_ungetch(lex, c2);
317 static int lex_try_digraph(lex_file *lex, int ch)
321 /* we just used fgetc() so count lines
322 * need to offset a \n the ungetch would recognize
324 if (!lex->push_line && c2 == '\n')
326 if (ch == '<' && c2 == ':')
328 else if (ch == ':' && c2 == '>')
330 else if (ch == '<' && c2 == '%')
332 else if (ch == '%' && c2 == '>')
334 else if (ch == '%' && c2 == ':')
336 lex_ungetch(lex, c2);
340 static int lex_getch(lex_file *lex)
346 if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
348 return lex->peek[lex->peekpos];
352 if (!lex->push_line && ch == '\n')
355 return lex_try_trigraph(lex, ch);
356 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
357 return lex_try_digraph(lex, ch);
361 static void lex_ungetch(lex_file *lex, int ch)
363 lex->peek[lex->peekpos++] = ch;
364 if (!lex->push_line && ch == '\n')
368 /* classify characters
369 * some additions to the is*() functions of ctype.h
372 /* Idents are alphanumberic, but they start with alpha or _ */
373 static bool isident_start(int ch)
375 return isalpha(ch) || ch == '_';
378 static bool isident(int ch)
380 return isident_start(ch) || isdigit(ch);
383 /* isxdigit_only is used when we already know it's not a digit
384 * and want to see if it's a hex digit anyway.
386 static bool isxdigit_only(int ch)
388 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
391 /* Append a character to the token buffer */
392 static void lex_tokench(lex_file *lex, int ch)
394 vec_push(lex->tok.value, ch);
397 /* Append a trailing null-byte */
398 static void lex_endtoken(lex_file *lex)
400 vec_push(lex->tok.value, 0);
401 vec_shrinkby(lex->tok.value, 1);
404 static bool lex_try_pragma(lex_file *lex)
408 char *command = NULL;
412 if (lex->flags.preprocessing)
419 lex_ungetch(lex, ch);
423 for (ch = lex_getch(lex); vec_size(pragma) < 8 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
424 vec_push(pragma, ch);
427 if (ch != ' ' || strcmp(pragma, "pragma")) {
428 lex_ungetch(lex, ch);
432 for (ch = lex_getch(lex); vec_size(command) < 32 && ch >= 'a' && ch <= 'z'; ch = lex_getch(lex))
433 vec_push(command, ch);
434 vec_push(command, 0);
437 lex_ungetch(lex, ch);
441 for (ch = lex_getch(lex); vec_size(param) < 1024 && ch != ')' && ch != '\n'; ch = lex_getch(lex))
446 lex_ungetch(lex, ch);
450 if (!strcmp(command, "push")) {
451 if (!strcmp(param, "line")) {
453 if (lex->push_line == 1)
459 else if (!strcmp(command, "pop")) {
460 if (!strcmp(param, "line")) {
463 if (lex->push_line == 0)
469 else if (!strcmp(command, "file")) {
470 lex->name = util_strdup(param);
471 vec_push(lex_filenames, lex->name);
473 else if (!strcmp(command, "line")) {
474 line = strtol(param, NULL, 0)-1;
480 while (ch != '\n' && ch != EOF)
487 while (vec_size(command)) {
488 lex_ungetch(lex, (unsigned char)vec_last(command));
492 lex_ungetch(lex, ' ');
496 while (vec_size(command)) {
497 lex_ungetch(lex, (unsigned char)vec_last(command));
501 lex_ungetch(lex, ' ');
505 while (vec_size(pragma)) {
506 lex_ungetch(lex, (unsigned char)vec_last(pragma));
511 lex_ungetch(lex, '#');
517 /* Skip whitespace and comments and return the first
518 * non-white character.
519 * As this makes use of the above getch() ungetch() functions,
520 * we don't need to care at all about line numbering anymore.
522 * In theory, this function should only be used at the beginning
523 * of lexing, or when we *know* the next character is part of the token.
524 * Otherwise, if the parser throws an error, the linenumber may not be
525 * the line of the error, but the line of the next token AFTER the error.
527 * This is currently only problematic when using c-like string-continuation,
528 * since comments and whitespaces are allowed between 2 such strings.
532 "A continuation of the previous string"
533 // This line is skipped
536 * In this case, if the parse decides it didn't actually want a string,
537 * and uses lex->line to print an error, it will show the ', foo);' line's
540 * On the other hand, the parser is supposed to remember the line of the next
541 * token's beginning. In this case we would want skipwhite() to be called
542 * AFTER reading a token, so that the parser, before reading the NEXT token,
543 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
546 * here is to store the line of the first character after skipping
547 * the initial whitespace in lex->sline, this happens in lex_do.
549 static int lex_skipwhite(lex_file *lex, bool hadwhite)
552 bool haswhite = hadwhite;
557 while (ch != EOF && isspace(ch)) {
559 if (lex_try_pragma(lex))
562 if (lex->flags.preprocessing) {
565 /* see if there was whitespace first */
566 if (haswhite) { /* (vec_size(lex->tok.value)) { */
567 lex_ungetch(lex, ch);
571 /* otherwise return EOL */
575 lex_tokench(lex, ch);
584 /* one line comment */
587 if (lex->flags.preprocessing) {
590 lex_tokench(lex, '/');
591 lex_tokench(lex, '/');
593 lex_tokench(lex, ' ');
594 lex_tokench(lex, ' ');
597 while (ch != EOF && ch != '\n') {
598 if (lex->flags.preprocessing)
599 lex_tokench(lex, ' '); /* ch); */
602 if (lex->flags.preprocessing) {
603 lex_ungetch(lex, '\n');
611 /* multiline comment */
612 if (lex->flags.preprocessing) {
615 lex_tokench(lex, '/');
616 lex_tokench(lex, '*');
618 lex_tokench(lex, ' ');
619 lex_tokench(lex, ' ');
628 if (lex->flags.preprocessing) {
630 lex_tokench(lex, '*');
631 lex_tokench(lex, '/');
633 lex_tokench(lex, ' ');
634 lex_tokench(lex, ' ');
638 lex_ungetch(lex, ch);
640 if (lex->flags.preprocessing) {
642 lex_tokench(lex, '\n');
644 lex_tokench(lex, ' '); /* ch); */
647 ch = ' '; /* cause TRUE in the isspace check */
650 /* Otherwise roll back to the slash and break out of the loop */
651 lex_ungetch(lex, ch);
655 } while (ch != EOF && isspace(ch));
659 lex_ungetch(lex, ch);
666 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
671 while (ch != EOF && isident(ch))
673 lex_tokench(lex, ch);
677 /* last ch was not an ident ch: */
678 lex_ungetch(lex, ch);
683 /* read one ident for the frame list */
684 static int lex_parse_frame(lex_file *lex)
691 while (ch != EOF && ch != '\n' && isspace(ch))
697 if (!isident_start(ch)) {
698 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
702 lex_tokench(lex, ch);
703 if (!lex_finish_ident(lex))
709 /* read a list of $frames */
710 static bool lex_finish_frames(lex_file *lex)
717 rc = lex_parse_frame(lex);
718 if (rc > 0) /* end of line */
720 if (rc < 0) /* error */
723 for (i = 0; i < vec_size(lex->frames); ++i) {
724 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
725 lex->frames[i].value = lex->framevalue++;
726 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
731 if (i < vec_size(lex->frames))
734 m.value = lex->framevalue++;
735 m.name = util_strdup(lex->tok.value);
736 vec_shrinkto(lex->tok.value, 0);
737 vec_push(lex->frames, m);
743 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
749 char u8buf[8]; /* way more than enough */
756 return TOKEN_STRINGCONST;
758 if (lex->flags.preprocessing && ch == '\\') {
759 lex_tokench(lex, ch);
762 lexerror(lex, "unexpected end of file");
763 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
764 return (lex->tok.ttype = TOKEN_ERROR);
766 lex_tokench(lex, ch);
768 else if (ch == '\\') {
771 lexerror(lex, "unexpected end of file");
772 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
773 return (lex->tok.ttype = TOKEN_ERROR);
780 case 'a': ch = '\a'; break;
781 case 'b': ch = '\b'; break;
782 case 'r': ch = '\r'; break;
783 case 'n': ch = '\n'; break;
784 case 't': ch = '\t'; break;
785 case 'f': ch = '\f'; break;
786 case 'v': ch = '\v'; break;
789 /* same procedure as in fteqcc */
791 nextch = lex_getch(lex);
792 if (nextch >= '0' && nextch <= '9')
794 else if (nextch >= 'a' && nextch <= 'f')
795 ch += nextch - 'a' + 10;
796 else if (nextch >= 'A' && nextch <= 'F')
797 ch += nextch - 'A' + 10;
799 lexerror(lex, "bad character code");
800 lex_ungetch(lex, nextch);
801 return (lex->tok.ttype = TOKEN_ERROR);
805 nextch = lex_getch(lex);
806 if (nextch >= '0' && nextch <= '9')
808 else if (nextch >= 'a' && nextch <= 'f')
809 ch += nextch - 'a' + 10;
810 else if (nextch >= 'A' && nextch <= 'F')
811 ch += nextch - 'A' + 10;
813 lexerror(lex, "bad character code");
814 lex_ungetch(lex, nextch);
815 return (lex->tok.ttype = TOKEN_ERROR);
820 case '0': case '1': case '2': case '3':
821 case '4': case '5': case '6': case '7':
825 case '<': ch = 29; break;
826 case '-': ch = 30; break;
827 case '>': ch = 31; break;
828 case '[': ch = 16; break;
829 case ']': ch = 17; break;
832 nextch = lex_getch(lex);
833 hex = (nextch == 'x');
835 lex_ungetch(lex, nextch);
836 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
838 if (nextch >= '0' && nextch <= '9')
839 chr = chr * 10 + nextch - '0';
841 lexerror(lex, "bad character code");
842 return (lex->tok.ttype = TOKEN_ERROR);
845 if (nextch >= '0' && nextch <= '9')
846 chr = chr * 0x10 + nextch - '0';
847 else if (nextch >= 'a' && nextch <= 'f')
848 chr = chr * 0x10 + nextch - 'a' + 10;
849 else if (nextch >= 'A' && nextch <= 'F')
850 chr = chr * 0x10 + nextch - 'A' + 10;
852 lexerror(lex, "bad character code");
853 return (lex->tok.ttype = TOKEN_ERROR);
856 if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
858 lexerror(lex, "character code out of range");
859 return (lex->tok.ttype = TOKEN_ERROR);
862 if (OPTS_FLAG(UTF8) && chr >= 128) {
863 u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
868 for (uc = 0; uc < u8len; ++uc)
869 lex_tokench(lex, u8buf[uc]);
870 /* the last character will be inserted with the tokench() call
879 case '\n': ch = '\n'; break;
882 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
883 /* so we just add the character plus backslash no matter what it actually is */
884 lex_tokench(lex, '\\');
886 /* add the character finally */
887 lex_tokench(lex, ch);
890 lex_tokench(lex, ch);
892 lexerror(lex, "unexpected end of file within string constant");
893 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
894 return (lex->tok.ttype = TOKEN_ERROR);
897 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
903 /* parse a number... */
905 lex->tok.ttype = TOKEN_FLOATCONST;
907 lex->tok.ttype = TOKEN_INTCONST;
909 lex_tokench(lex, ch);
912 if (ch != '.' && !isdigit(ch))
914 if (lastch != '0' || ch != 'x')
916 /* end of the number or EOF */
917 lex_ungetch(lex, ch);
920 lex->tok.constval.i = lastch - '0';
921 return lex->tok.ttype;
927 /* EOF would have been caught above */
931 lex_tokench(lex, ch);
933 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
935 lex_tokench(lex, ch);
939 /* NOT else, '.' can come from above as well */
940 if (lex->tok.ttype != TOKEN_FLOATCONST && ch == '.' && !ishex)
942 /* Allow floating comma in non-hex mode */
943 lex->tok.ttype = TOKEN_FLOATCONST;
944 lex_tokench(lex, ch);
946 /* continue digits-only */
950 lex_tokench(lex, ch);
954 /* put back the last character */
955 /* but do not put back the trailing 'f' or a float */
956 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
959 /* generally we don't want words to follow numbers: */
961 lexerror(lex, "unexpected trailing characters after number");
962 return (lex->tok.ttype = TOKEN_ERROR);
964 lex_ungetch(lex, ch);
967 if (lex->tok.ttype == TOKEN_FLOATCONST)
968 lex->tok.constval.f = strtod(lex->tok.value, NULL);
970 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
971 return lex->tok.ttype;
974 int lex_do(lex_file *lex)
976 int ch, nextch, thirdch;
977 bool hadwhite = false;
986 ch = lex_skipwhite(lex, hadwhite);
988 if (!lex->flags.mergelines || ch != '\\')
992 lex_ungetch(lex, ch);
996 /* we reached a linemerge */
997 lex_tokench(lex, '\n');
1001 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
1002 return (lex->tok.ttype = ch);
1005 lex->sline = lex->line;
1006 lex->tok.ctx.line = lex->sline;
1007 lex->tok.ctx.file = lex->name;
1010 return (lex->tok.ttype = TOKEN_FATAL);
1014 return (lex->tok.ttype = TOKEN_EOF);
1017 /* modelgen / spiritgen commands */
1018 if (ch == '$' && !lex->flags.preprocessing) {
1022 ch = lex_getch(lex);
1023 if (!isident_start(ch)) {
1024 lexerror(lex, "hanging '$' modelgen/spritegen command line");
1027 lex_tokench(lex, ch);
1028 if (!lex_finish_ident(lex))
1029 return (lex->tok.ttype = TOKEN_ERROR);
1031 /* skip the known commands */
1034 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
1036 /* frame/framesave command works like an enum
1037 * similar to fteqcc we handle this in the lexer.
1038 * The reason for this is that it is sensitive to newlines,
1039 * which the parser is unaware of
1041 if (!lex_finish_frames(lex))
1042 return (lex->tok.ttype = TOKEN_ERROR);
1046 if (!strcmp(v, "framevalue"))
1048 ch = lex_getch(lex);
1049 while (ch != EOF && isspace(ch) && ch != '\n')
1050 ch = lex_getch(lex);
1053 lexerror(lex, "$framevalue requires an integer parameter");
1058 lex->tok.ttype = lex_finish_digit(lex, ch);
1060 if (lex->tok.ttype != TOKEN_INTCONST) {
1061 lexerror(lex, "$framevalue requires an integer parameter");
1064 lex->framevalue = lex->tok.constval.i;
1068 if (!strcmp(v, "framerestore"))
1074 rc = lex_parse_frame(lex);
1077 lexerror(lex, "$framerestore requires a framename parameter");
1081 return (lex->tok.ttype = TOKEN_FATAL);
1084 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1085 if (!strcmp(v, lex->frames[frame].name)) {
1086 lex->framevalue = lex->frames[frame].value;
1090 lexerror(lex, "unknown framename `%s`", v);
1094 if (!strcmp(v, "modelname"))
1100 rc = lex_parse_frame(lex);
1103 lexerror(lex, "$modelname requires a parameter");
1107 return (lex->tok.ttype = TOKEN_FATAL);
1109 if (lex->modelname) {
1111 m.value = lex->framevalue;
1112 m.name = lex->modelname;
1113 lex->modelname = NULL;
1114 vec_push(lex->frames, m);
1116 lex->modelname = lex->tok.value;
1117 lex->tok.value = NULL;
1121 if (!strcmp(v, "flush"))
1124 for (fi = 0; fi < vec_size(lex->frames); ++fi)
1125 mem_d(lex->frames[fi].name);
1126 vec_free(lex->frames);
1127 /* skip line (fteqcc does it too) */
1128 ch = lex_getch(lex);
1129 while (ch != EOF && ch != '\n')
1130 ch = lex_getch(lex);
1134 if (!strcmp(v, "cd") ||
1135 !strcmp(v, "origin") ||
1136 !strcmp(v, "base") ||
1137 !strcmp(v, "flags") ||
1138 !strcmp(v, "scale") ||
1142 ch = lex_getch(lex);
1143 while (ch != EOF && ch != '\n')
1144 ch = lex_getch(lex);
1148 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
1149 if (!strcmp(v, lex->frames[frame].name)) {
1150 lex->tok.constval.i = lex->frames[frame].value;
1151 return (lex->tok.ttype = TOKEN_INTCONST);
1155 lexerror(lex, "invalid frame macro");
1159 /* single-character tokens */
1163 nextch = lex_getch(lex);
1164 if (nextch == '[') {
1165 lex_tokench(lex, ch);
1166 lex_tokench(lex, nextch);
1168 return (lex->tok.ttype = TOKEN_ATTRIBUTE_OPEN);
1170 lex_ungetch(lex, nextch);
1175 lex_tokench(lex, ch);
1177 if (lex->flags.noops)
1178 return (lex->tok.ttype = ch);
1180 return (lex->tok.ttype = TOKEN_OPERATOR);
1183 if (lex->flags.noops) {
1184 nextch = lex_getch(lex);
1185 if (nextch == ']') {
1186 lex_tokench(lex, ch);
1187 lex_tokench(lex, nextch);
1189 return (lex->tok.ttype = TOKEN_ATTRIBUTE_CLOSE);
1191 lex_ungetch(lex, nextch);
1200 lex_tokench(lex, ch);
1202 return (lex->tok.ttype = ch);
1208 nextch = lex_getch(lex);
1209 /* digits starting with a dot */
1210 if (isdigit(nextch)) {
1211 lex_ungetch(lex, nextch);
1212 lex->tok.ttype = lex_finish_digit(lex, ch);
1214 return lex->tok.ttype;
1216 lex_ungetch(lex, nextch);
1219 if (lex->flags.noops)
1221 /* Detect characters early which are normally
1222 * operators OR PART of an operator.
1241 lex_tokench(lex, ch);
1243 return (lex->tok.ttype = ch);
1251 lex_tokench(lex, ch);
1252 /* peak ahead once */
1253 nextch = lex_getch(lex);
1254 if (nextch != '.') {
1255 lex_ungetch(lex, nextch);
1257 if (lex->flags.noops)
1258 return (lex->tok.ttype = ch);
1260 return (lex->tok.ttype = TOKEN_OPERATOR);
1262 /* peak ahead again */
1263 nextch = lex_getch(lex);
1264 if (nextch != '.') {
1265 lex_ungetch(lex, nextch);
1266 lex_ungetch(lex, '.');
1268 if (lex->flags.noops)
1269 return (lex->tok.ttype = ch);
1271 return (lex->tok.ttype = TOKEN_OPERATOR);
1273 /* fill the token to be "..." */
1274 lex_tokench(lex, ch);
1275 lex_tokench(lex, ch);
1277 return (lex->tok.ttype = TOKEN_DOTS);
1280 if (ch == ',' || ch == '.') {
1281 lex_tokench(lex, ch);
1283 return (lex->tok.ttype = TOKEN_OPERATOR);
1286 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
1287 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
1288 ch == '=' || ch == '!' || /* ==, != */
1289 ch == '&' || ch == '|') /* &&, ||, &=, |= */
1291 lex_tokench(lex, ch);
1293 nextch = lex_getch(lex);
1294 if (nextch == '=' || (nextch == ch && ch != '!')) {
1295 lex_tokench(lex, nextch);
1296 } else if (ch == '-' && nextch == '>') {
1297 lex_tokench(lex, nextch);
1298 } else if (ch == '&' && nextch == '~') {
1299 thirdch = lex_getch(lex);
1300 if (thirdch != '=') {
1301 lex_ungetch(lex, thirdch);
1302 lex_ungetch(lex, nextch);
1305 lex_tokench(lex, nextch);
1306 lex_tokench(lex, thirdch);
1309 else if (lex->flags.preprocessing &&
1310 ch == '-' && isdigit(nextch))
1312 lex->tok.ttype = lex_finish_digit(lex, nextch);
1313 if (lex->tok.ttype == TOKEN_INTCONST)
1314 lex->tok.constval.i = -lex->tok.constval.i;
1316 lex->tok.constval.f = -lex->tok.constval.f;
1318 return lex->tok.ttype;
1320 lex_ungetch(lex, nextch);
1323 return (lex->tok.ttype = TOKEN_OPERATOR);
1327 if (ch == '^' || ch == '~' || ch == '!')
1329 lex_tokench(lex, ch);
1331 return (lex->tok.ttype = TOKEN_OPERATOR);
1335 if (ch == '*' || ch == '/') /* *=, /= */
1337 lex_tokench(lex, ch);
1339 nextch = lex_getch(lex);
1340 if (nextch == '=') {
1341 lex_tokench(lex, nextch);
1343 lex_ungetch(lex, nextch);
1346 return (lex->tok.ttype = TOKEN_OPERATOR);
1349 if (isident_start(ch))
1353 lex_tokench(lex, ch);
1354 if (!lex_finish_ident(lex)) {
1356 return (lex->tok.ttype = TOKEN_ERROR);
1359 lex->tok.ttype = TOKEN_IDENT;
1362 if (!strcmp(v, "void")) {
1363 lex->tok.ttype = TOKEN_TYPENAME;
1364 lex->tok.constval.t = TYPE_VOID;
1365 } else if (!strcmp(v, "int")) {
1366 lex->tok.ttype = TOKEN_TYPENAME;
1367 lex->tok.constval.t = TYPE_INTEGER;
1368 } else if (!strcmp(v, "float")) {
1369 lex->tok.ttype = TOKEN_TYPENAME;
1370 lex->tok.constval.t = TYPE_FLOAT;
1371 } else if (!strcmp(v, "string")) {
1372 lex->tok.ttype = TOKEN_TYPENAME;
1373 lex->tok.constval.t = TYPE_STRING;
1374 } else if (!strcmp(v, "entity")) {
1375 lex->tok.ttype = TOKEN_TYPENAME;
1376 lex->tok.constval.t = TYPE_ENTITY;
1377 } else if (!strcmp(v, "vector")) {
1378 lex->tok.ttype = TOKEN_TYPENAME;
1379 lex->tok.constval.t = TYPE_VECTOR;
1382 for (kw = 0; kw < num_keywords_qc; ++kw) {
1383 if (!strcmp(v, keywords_qc[kw]))
1384 return (lex->tok.ttype = TOKEN_KEYWORD);
1386 if (opts.standard != COMPILER_QCC) {
1387 for (kw = 0; kw < num_keywords_fg; ++kw) {
1388 if (!strcmp(v, keywords_fg[kw]))
1389 return (lex->tok.ttype = TOKEN_KEYWORD);
1394 return lex->tok.ttype;
1399 lex->flags.nodigraphs = true;
1400 if (lex->flags.preprocessing)
1401 lex_tokench(lex, ch);
1402 lex->tok.ttype = lex_finish_string(lex, '"');
1403 if (lex->flags.preprocessing)
1404 lex_tokench(lex, ch);
1405 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1407 /* Allow c style "string" "continuation" */
1408 ch = lex_skipwhite(lex, false);
1410 lex_ungetch(lex, ch);
1414 lex->tok.ttype = lex_finish_string(lex, '"');
1416 lex->flags.nodigraphs = false;
1418 return lex->tok.ttype;
1423 /* we parse character constants like string,
1424 * but return TOKEN_CHARCONST, or a vector type if it fits...
1425 * Likewise actual unescaping has to be done by the parser.
1426 * The difference is we don't allow 'char' 'continuation'.
1428 if (lex->flags.preprocessing)
1429 lex_tokench(lex, ch);
1430 lex->tok.ttype = lex_finish_string(lex, '\'');
1431 if (lex->flags.preprocessing)
1432 lex_tokench(lex, ch);
1435 lex->tok.ttype = TOKEN_CHARCONST;
1436 /* It's a vector if we can successfully scan 3 floats */
1438 if (sscanf_s(lex->tok.value, " %f %f %f ",
1439 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1441 if (sscanf(lex->tok.value, " %f %f %f ",
1442 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1446 lex->tok.ttype = TOKEN_VECTORCONST;
1450 if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
1452 /* check for a valid utf8 character */
1453 if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
1454 if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
1455 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
1456 : "multibyte character: `%s`" ),
1458 return (lex->tok.ttype = TOKEN_ERROR);
1461 lex->tok.constval.i = u8char;
1464 lex->tok.constval.i = lex->tok.value[0];
1467 return lex->tok.ttype;
1472 lex->tok.ttype = lex_finish_digit(lex, ch);
1474 return lex->tok.ttype;
1477 if (lex->flags.preprocessing) {
1478 lex_tokench(lex, ch);
1480 return (lex->tok.ttype = ch);
1483 lexerror(lex, "unknown token");
1484 return (lex->tok.ttype = TOKEN_ERROR);