11 void lexerror(lex_file *lex, const char *fmt, ...)
16 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
20 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
23 int lvl = LVL_WARNING;
25 if (!OPTS_WARN(warntype))
32 con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
42 token *tok = (token*)mem_a(sizeof(token));
45 memset(tok, 0, sizeof(*tok));
49 void token_delete(token *self)
51 if (self->next && self->next->prev == self)
52 self->next->prev = self->prev;
53 if (self->prev && self->prev->next == self)
54 self->prev->next = self->next;
55 MEM_VECTOR_CLEAR(self, value);
59 token* token_copy(const token *cp)
61 token* self = token_new();
65 self->value_alloc = cp->value_count + 1;
66 self->value_count = cp->value_count;
67 self->value = (char*)mem_a(self->value_alloc);
72 memcpy(self->value, cp->value, cp->value_count);
73 self->value[self->value_alloc-1] = 0;
77 self->ttype = cp->ttype;
78 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
82 void token_delete_all(token *t)
93 token* token_copy_all(const token *cp)
98 out = cur = token_copy(cp);
104 cur->next = token_copy(cp);
106 token_delete_all(out);
109 cur->next->prev = cur;
116 static void lex_token_new(lex_file *lex)
120 token_delete(lex->tok);
121 lex->tok = token_new();
124 vec_shrinkto(lex->tok.value, 0);
125 lex->tok.constval.t = 0;
126 lex->tok.ctx.line = lex->sline;
127 lex->tok.ctx.file = lex->name;
132 lex_file* lex_open(const char *file)
135 FILE *in = util_fopen(file, "rb");
138 lexerror(NULL, "open failed: '%s'\n", file);
142 lex = (lex_file*)mem_a(sizeof(*lex));
145 lexerror(NULL, "out of memory\n");
149 memset(lex, 0, sizeof(*lex));
152 lex->name = util_strdup(file);
153 lex->line = 1; /* we start counting at 1 */
158 vec_push(lex_filenames, lex->name);
162 lex_file* lex_open_string(const char *str, size_t len, const char *name)
166 lex = (lex_file*)mem_a(sizeof(*lex));
168 lexerror(NULL, "out of memory\n");
172 memset(lex, 0, sizeof(*lex));
175 lex->open_string = str;
176 lex->open_string_length = len;
177 lex->open_string_pos = 0;
179 lex->name = util_strdup(name ? name : "<string-source>");
180 lex->line = 1; /* we start counting at 1 */
185 vec_push(lex_filenames, lex->name);
190 void lex_cleanup(void)
193 for (i = 0; i < vec_size(lex_filenames); ++i)
194 mem_d(lex_filenames[i]);
195 vec_free(lex_filenames);
198 void lex_close(lex_file *lex)
201 for (i = 0; i < vec_size(lex->frames); ++i)
202 mem_d(lex->frames[i].name);
203 vec_free(lex->frames);
206 vec_free(lex->modelname);
212 token_delete(lex->tok);
214 vec_free(lex->tok.value);
216 /* mem_d(lex->name); collected in lex_filenames */
220 static int lex_fgetc(lex_file *lex)
223 return fgetc(lex->file);
224 if (lex->open_string) {
225 if (lex->open_string_pos >= lex->open_string_length)
227 return lex->open_string[lex->open_string_pos++];
232 /* Get or put-back data
233 * The following to functions do NOT understand what kind of data they
235 * The are merely wrapping get/put in order to count line numbers.
237 static void lex_ungetch(lex_file *lex, int ch);
238 static int lex_try_trigraph(lex_file *lex, int old)
243 lex_ungetch(lex, c2);
249 case '=': return '#';
250 case '/': return '\\';
251 case '\'': return '^';
252 case '(': return '[';
253 case ')': return ']';
254 case '!': return '|';
255 case '<': return '{';
256 case '>': return '}';
257 case '-': return '~';
259 lex_ungetch(lex, c3);
260 lex_ungetch(lex, c2);
265 static int lex_try_digraph(lex_file *lex, int ch)
269 if (ch == '<' && c2 == ':')
271 else if (ch == ':' && c2 == '>')
273 else if (ch == '<' && c2 == '%')
275 else if (ch == '%' && c2 == '>')
277 else if (ch == '%' && c2 == ':')
279 lex_ungetch(lex, c2);
283 static int lex_getch(lex_file *lex)
289 if (lex->peek[lex->peekpos] == '\n')
291 return lex->peek[lex->peekpos];
298 return lex_try_trigraph(lex, ch);
299 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
300 return lex_try_digraph(lex, ch);
304 static void lex_ungetch(lex_file *lex, int ch)
306 lex->peek[lex->peekpos++] = ch;
311 /* classify characters
312 * some additions to the is*() functions of ctype.h
315 /* Idents are alphanumberic, but they start with alpha or _ */
316 static bool isident_start(int ch)
318 return isalpha(ch) || ch == '_';
321 static bool isident(int ch)
323 return isident_start(ch) || isdigit(ch);
326 /* isxdigit_only is used when we already know it's not a digit
327 * and want to see if it's a hex digit anyway.
329 static bool isxdigit_only(int ch)
331 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
334 /* Append a character to the token buffer */
335 static void lex_tokench(lex_file *lex, int ch)
337 vec_push(lex->tok.value, ch);
340 /* Append a trailing null-byte */
341 static void lex_endtoken(lex_file *lex)
343 vec_push(lex->tok.value, 0);
344 vec_shrinkby(lex->tok.value, 1);
347 /* Skip whitespace and comments and return the first
348 * non-white character.
349 * As this makes use of the above getch() ungetch() functions,
350 * we don't need to care at all about line numbering anymore.
352 * In theory, this function should only be used at the beginning
353 * of lexing, or when we *know* the next character is part of the token.
354 * Otherwise, if the parser throws an error, the linenumber may not be
355 * the line of the error, but the line of the next token AFTER the error.
357 * This is currently only problematic when using c-like string-continuation,
358 * since comments and whitespaces are allowed between 2 such strings.
362 "A continuation of the previous string"
363 // This line is skipped
366 * In this case, if the parse decides it didn't actually want a string,
367 * and uses lex->line to print an error, it will show the ', foo);' line's
370 * On the other hand, the parser is supposed to remember the line of the next
371 * token's beginning. In this case we would want skipwhite() to be called
372 * AFTER reading a token, so that the parser, before reading the NEXT token,
373 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
376 * here is to store the line of the first character after skipping
377 * the initial whitespace in lex->sline, this happens in lex_do.
379 static int lex_skipwhite(lex_file *lex)
382 bool haswhite = false;
387 while (ch != EOF && isspace(ch)) {
388 if (lex->flags.preprocessing) {
391 /* see if there was whitespace first */
392 if (haswhite) { /* (vec_size(lex->tok.value)) { */
393 lex_ungetch(lex, ch);
397 /* otherwise return EOL */
401 lex_tokench(lex, ch);
410 /* one line comment */
413 if (lex->flags.preprocessing) {
416 lex_tokench(lex, '/');
417 lex_tokench(lex, '/');
419 lex_tokench(lex, ' ');
420 lex_tokench(lex, ' ');
423 while (ch != EOF && ch != '\n') {
424 if (lex->flags.preprocessing)
425 lex_tokench(lex, ' '); /* ch); */
428 if (lex->flags.preprocessing) {
429 lex_ungetch(lex, '\n');
437 /* multiline comment */
438 if (lex->flags.preprocessing) {
441 lex_tokench(lex, '/');
442 lex_tokench(lex, '*');
444 lex_tokench(lex, ' ');
445 lex_tokench(lex, ' ');
454 if (lex->flags.preprocessing) {
456 lex_tokench(lex, '*');
457 lex_tokench(lex, '/');
459 lex_tokench(lex, ' ');
460 lex_tokench(lex, ' ');
465 if (lex->flags.preprocessing) {
466 lex_tokench(lex, ' '); /* ch); */
469 ch = ' '; /* cause TRUE in the isspace check */
472 /* Otherwise roll back to the slash and break out of the loop */
473 lex_ungetch(lex, ch);
477 } while (ch != EOF && isspace(ch));
481 lex_ungetch(lex, ch);
488 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
493 while (ch != EOF && isident(ch))
495 lex_tokench(lex, ch);
499 /* last ch was not an ident ch: */
500 lex_ungetch(lex, ch);
505 /* read one ident for the frame list */
506 static int lex_parse_frame(lex_file *lex)
513 while (ch != EOF && ch != '\n' && isspace(ch))
519 if (!isident_start(ch)) {
520 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
524 lex_tokench(lex, ch);
525 if (!lex_finish_ident(lex))
531 /* read a list of $frames */
532 static bool lex_finish_frames(lex_file *lex)
539 rc = lex_parse_frame(lex);
540 if (rc > 0) /* end of line */
542 if (rc < 0) /* error */
545 for (i = 0; i < vec_size(lex->frames); ++i) {
546 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
547 lex->frames[i].value = lex->framevalue++;
548 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
553 if (i < vec_size(lex->frames))
556 m.value = lex->framevalue++;
557 m.name = util_strdup(lex->tok.value);
558 vec_shrinkto(lex->tok.value, 0);
559 vec_push(lex->frames, m);
563 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
571 return TOKEN_STRINGCONST;
573 if (!lex->flags.preprocessing && ch == '\\') {
576 lexerror(lex, "unexpected end of file");
577 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
578 return (lex->tok.ttype = TOKEN_ERROR);
583 case 'a': ch = '\a'; break;
584 case 'b': ch = '\b'; break;
585 case 'r': ch = '\r'; break;
586 case 'n': ch = '\n'; break;
587 case 't': ch = '\t'; break;
588 case 'f': ch = '\f'; break;
589 case 'v': ch = '\v'; break;
591 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
592 /* so we just add the character plus backslash no matter what it actually is */
593 lex_tokench(lex, '\\');
595 /* add the character finally */
596 lex_tokench(lex, ch);
599 lex_tokench(lex, ch);
601 lexerror(lex, "unexpected end of file within string constant");
602 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
603 return (lex->tok.ttype = TOKEN_ERROR);
606 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
612 /* parse a number... */
613 lex->tok.ttype = TOKEN_INTCONST;
615 lex_tokench(lex, ch);
618 if (ch != '.' && !isdigit(ch))
620 if (lastch != '0' || ch != 'x')
622 /* end of the number or EOF */
623 lex_ungetch(lex, ch);
626 lex->tok.constval.i = lastch - '0';
627 return lex->tok.ttype;
633 /* EOF would have been caught above */
637 lex_tokench(lex, ch);
639 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
641 lex_tokench(lex, ch);
645 /* NOT else, '.' can come from above as well */
646 if (ch == '.' && !ishex)
648 /* Allow floating comma in non-hex mode */
649 lex->tok.ttype = TOKEN_FLOATCONST;
650 lex_tokench(lex, ch);
652 /* continue digits-only */
656 lex_tokench(lex, ch);
660 /* put back the last character */
661 /* but do not put back the trailing 'f' or a float */
662 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
665 /* generally we don't want words to follow numbers: */
667 lexerror(lex, "unexpected trailing characters after number");
668 return (lex->tok.ttype = TOKEN_ERROR);
670 lex_ungetch(lex, ch);
673 if (lex->tok.ttype == TOKEN_FLOATCONST)
674 lex->tok.constval.f = strtod(lex->tok.value, NULL);
676 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
677 return lex->tok.ttype;
680 int lex_do(lex_file *lex)
691 ch = lex_skipwhite(lex);
692 if (!lex->flags.mergelines || ch != '\\')
696 lex_ungetch(lex, ch);
700 /* we reached a linemerge */
701 lex_tokench(lex, '\n');
705 lex->sline = lex->line;
706 lex->tok.ctx.line = lex->sline;
707 lex->tok.ctx.file = lex->name;
709 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
710 return (lex->tok.ttype = ch);
714 return (lex->tok.ttype = TOKEN_FATAL);
718 return (lex->tok.ttype = TOKEN_EOF);
721 /* modelgen / spiritgen commands */
727 if (!isident_start(ch)) {
728 lexerror(lex, "hanging '$' modelgen/spritegen command line");
731 lex_tokench(lex, ch);
732 if (!lex_finish_ident(lex))
733 return (lex->tok.ttype = TOKEN_ERROR);
735 /* skip the known commands */
738 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
740 /* frame/framesave command works like an enum
741 * similar to fteqcc we handle this in the lexer.
742 * The reason for this is that it is sensitive to newlines,
743 * which the parser is unaware of
745 if (!lex_finish_frames(lex))
746 return (lex->tok.ttype = TOKEN_ERROR);
750 if (!strcmp(v, "framevalue"))
753 while (ch != EOF && isspace(ch) && ch != '\n')
757 lexerror(lex, "$framevalue requires an integer parameter");
762 lex->tok.ttype = lex_finish_digit(lex, ch);
764 if (lex->tok.ttype != TOKEN_INTCONST) {
765 lexerror(lex, "$framevalue requires an integer parameter");
768 lex->framevalue = lex->tok.constval.i;
772 if (!strcmp(v, "framerestore"))
778 rc = lex_parse_frame(lex);
781 lexerror(lex, "$framerestore requires a framename parameter");
785 return (lex->tok.ttype = TOKEN_FATAL);
788 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
789 if (!strcmp(v, lex->frames[frame].name)) {
790 lex->framevalue = lex->frames[frame].value;
794 lexerror(lex, "unknown framename `%s`", v);
798 if (!strcmp(v, "modelname"))
804 rc = lex_parse_frame(lex);
807 lexerror(lex, "$modelname requires a parameter");
811 return (lex->tok.ttype = TOKEN_FATAL);
814 if (lex->modelname) {
816 m.value = lex->framevalue;
817 m.name = lex->modelname;
818 lex->modelname = NULL;
819 vec_push(lex->frames, m);
821 lex->modelname = lex->tok.value;
822 lex->tok.value = NULL;
826 if (!strcmp(v, "flush"))
829 for (frame = 0; frame < vec_size(lex->frames); ++frame)
830 mem_d(lex->frames[frame].name);
831 vec_free(lex->frames);
832 /* skip line (fteqcc does it too) */
834 while (ch != EOF && ch != '\n')
839 if (!strcmp(v, "cd") ||
840 !strcmp(v, "origin") ||
841 !strcmp(v, "base") ||
842 !strcmp(v, "flags") ||
843 !strcmp(v, "scale") ||
848 while (ch != EOF && ch != '\n')
853 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
854 if (!strcmp(v, lex->frames[frame].name)) {
855 lex->tok.constval.i = lex->frames[frame].value;
856 return (lex->tok.ttype = TOKEN_INTCONST);
860 lexerror(lex, "invalid frame macro");
864 /* single-character tokens */
869 lex_tokench(lex, ch);
871 if (lex->flags.noops)
872 return (lex->tok.ttype = ch);
874 return (lex->tok.ttype = TOKEN_OPERATOR);
883 lex_tokench(lex, ch);
885 return (lex->tok.ttype = ch);
890 if (lex->flags.noops)
892 /* Detect characters early which are normally
893 * operators OR PART of an operator.
910 lex_tokench(lex, ch);
912 return (lex->tok.ttype = ch);
919 lex_tokench(lex, ch);
920 /* peak ahead once */
921 nextch = lex_getch(lex);
923 lex_ungetch(lex, nextch);
925 return (lex->tok.ttype = ch);
927 /* peak ahead again */
928 nextch = lex_getch(lex);
930 lex_ungetch(lex, nextch);
931 lex_ungetch(lex, nextch);
933 return (lex->tok.ttype = ch);
935 /* fill the token to be "..." */
936 lex_tokench(lex, ch);
937 lex_tokench(lex, ch);
939 return (lex->tok.ttype = TOKEN_DOTS);
943 if (ch == ',' || ch == '.') {
944 lex_tokench(lex, ch);
946 return (lex->tok.ttype = TOKEN_OPERATOR);
949 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
950 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
951 ch == '=' || ch == '!' || /* ==, != */
952 ch == '&' || ch == '|') /* &&, ||, &=, |= */
954 lex_tokench(lex, ch);
956 nextch = lex_getch(lex);
957 if (nextch == ch || nextch == '=') {
958 lex_tokench(lex, nextch);
959 } else if (ch == '-' && nextch == '>') {
960 lex_tokench(lex, nextch);
962 lex_ungetch(lex, nextch);
965 return (lex->tok.ttype = TOKEN_OPERATOR);
969 if (ch == '^' || ch == '~' || ch == '!')
971 lex_tokench(lex, ch);
973 return (lex->tok.ttype = TOKEN_OPERATOR);
977 if (ch == '*' || ch == '/') /* *=, /= */
979 lex_tokench(lex, ch);
981 nextch = lex_getch(lex);
983 lex_tokench(lex, nextch);
985 lex_ungetch(lex, nextch);
988 return (lex->tok.ttype = TOKEN_OPERATOR);
991 if (isident_start(ch))
995 lex_tokench(lex, ch);
996 if (!lex_finish_ident(lex)) {
998 return (lex->tok.ttype = TOKEN_ERROR);
1001 lex->tok.ttype = TOKEN_IDENT;
1004 if (!strcmp(v, "void")) {
1005 lex->tok.ttype = TOKEN_TYPENAME;
1006 lex->tok.constval.t = TYPE_VOID;
1007 } else if (!strcmp(v, "int")) {
1008 lex->tok.ttype = TOKEN_TYPENAME;
1009 lex->tok.constval.t = TYPE_INTEGER;
1010 } else if (!strcmp(v, "float")) {
1011 lex->tok.ttype = TOKEN_TYPENAME;
1012 lex->tok.constval.t = TYPE_FLOAT;
1013 } else if (!strcmp(v, "string")) {
1014 lex->tok.ttype = TOKEN_TYPENAME;
1015 lex->tok.constval.t = TYPE_STRING;
1016 } else if (!strcmp(v, "entity")) {
1017 lex->tok.ttype = TOKEN_TYPENAME;
1018 lex->tok.constval.t = TYPE_ENTITY;
1019 } else if (!strcmp(v, "vector")) {
1020 lex->tok.ttype = TOKEN_TYPENAME;
1021 lex->tok.constval.t = TYPE_VECTOR;
1022 } else if (!strcmp(v, "for") ||
1023 !strcmp(v, "while") ||
1026 !strcmp(v, "else") ||
1027 !strcmp(v, "local") ||
1028 !strcmp(v, "return") ||
1029 !strcmp(v, "not") ||
1030 !strcmp(v, "const"))
1032 lex->tok.ttype = TOKEN_KEYWORD;
1034 else if (opts_standard != COMPILER_QCC)
1036 /* other standards reserve these keywords */
1037 if (!strcmp(v, "switch") ||
1038 !strcmp(v, "struct") ||
1039 !strcmp(v, "union") ||
1040 !strcmp(v, "break") ||
1041 !strcmp(v, "continue") ||
1044 lex->tok.ttype = TOKEN_KEYWORD;
1048 return lex->tok.ttype;
1053 lex->flags.nodigraphs = true;
1054 if (lex->flags.preprocessing)
1055 lex_tokench(lex, ch);
1056 lex->tok.ttype = lex_finish_string(lex, '"');
1057 if (lex->flags.preprocessing)
1058 lex_tokench(lex, ch);
1059 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1061 /* Allow c style "string" "continuation" */
1062 ch = lex_skipwhite(lex);
1064 lex_ungetch(lex, ch);
1068 lex->tok.ttype = lex_finish_string(lex, '"');
1070 lex->flags.nodigraphs = false;
1072 return lex->tok.ttype;
1077 /* we parse character constants like string,
1078 * but return TOKEN_CHARCONST, or a vector type if it fits...
1079 * Likewise actual unescaping has to be done by the parser.
1080 * The difference is we don't allow 'char' 'continuation'.
1082 if (lex->flags.preprocessing)
1083 lex_tokench(lex, ch);
1084 lex->tok.ttype = lex_finish_string(lex, '\'');
1085 if (lex->flags.preprocessing)
1086 lex_tokench(lex, ch);
1089 /* It's a vector if we can successfully scan 3 floats */
1091 if (sscanf_s(lex->tok.value, " %f %f %f ",
1092 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1094 if (sscanf(lex->tok.value, " %f %f %f ",
1095 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1099 lex->tok.ttype = TOKEN_VECTORCONST;
1102 return lex->tok.ttype;
1107 lex->tok.ttype = lex_finish_digit(lex, ch);
1109 return lex->tok.ttype;
1112 lexerror(lex, "unknown token");
1113 return (lex->tok.ttype = TOKEN_ERROR);