11 void lexerror(lex_file *lex, const char *fmt, ...)
17 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
19 con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap);
23 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
26 int lvl = LVL_WARNING;
28 if (!OPTS_WARN(warntype))
35 con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
45 token *tok = (token*)mem_a(sizeof(token));
48 memset(tok, 0, sizeof(*tok));
52 void token_delete(token *self)
54 if (self->next && self->next->prev == self)
55 self->next->prev = self->prev;
56 if (self->prev && self->prev->next == self)
57 self->prev->next = self->next;
58 MEM_VECTOR_CLEAR(self, value);
62 token* token_copy(const token *cp)
64 token* self = token_new();
68 self->value_alloc = cp->value_count + 1;
69 self->value_count = cp->value_count;
70 self->value = (char*)mem_a(self->value_alloc);
75 memcpy(self->value, cp->value, cp->value_count);
76 self->value[self->value_alloc-1] = 0;
80 self->ttype = cp->ttype;
81 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
85 void token_delete_all(token *t)
96 token* token_copy_all(const token *cp)
101 out = cur = token_copy(cp);
107 cur->next = token_copy(cp);
109 token_delete_all(out);
112 cur->next->prev = cur;
119 static void lex_token_new(lex_file *lex)
123 token_delete(lex->tok);
124 lex->tok = token_new();
127 vec_shrinkto(lex->tok.value, 0);
128 lex->tok.constval.t = 0;
129 lex->tok.ctx.line = lex->sline;
130 lex->tok.ctx.file = lex->name;
135 lex_file* lex_open(const char *file)
138 FILE *in = util_fopen(file, "rb");
141 lexerror(NULL, "open failed: '%s'\n", file);
145 lex = (lex_file*)mem_a(sizeof(*lex));
148 lexerror(NULL, "out of memory\n");
152 memset(lex, 0, sizeof(*lex));
155 lex->name = util_strdup(file);
156 lex->line = 1; /* we start counting at 1 */
161 vec_push(lex_filenames, lex->name);
165 lex_file* lex_open_string(const char *str, size_t len, const char *name)
169 lex = (lex_file*)mem_a(sizeof(*lex));
171 lexerror(NULL, "out of memory\n");
175 memset(lex, 0, sizeof(*lex));
178 lex->open_string = str;
179 lex->open_string_length = len;
180 lex->open_string_pos = 0;
182 lex->name = util_strdup(name ? name : "<string-source>");
183 lex->line = 1; /* we start counting at 1 */
188 vec_push(lex_filenames, lex->name);
193 void lex_cleanup(void)
196 for (i = 0; i < vec_size(lex_filenames); ++i)
197 mem_d(lex_filenames[i]);
198 vec_free(lex_filenames);
201 void lex_close(lex_file *lex)
204 for (i = 0; i < vec_size(lex->frames); ++i)
205 mem_d(lex->frames[i].name);
206 vec_free(lex->frames);
209 vec_free(lex->modelname);
215 token_delete(lex->tok);
217 vec_free(lex->tok.value);
219 /* mem_d(lex->name); collected in lex_filenames */
223 static int lex_fgetc(lex_file *lex)
226 return fgetc(lex->file);
227 if (lex->open_string) {
228 if (lex->open_string_pos >= lex->open_string_length)
230 return lex->open_string[lex->open_string_pos++];
235 /* Get or put-back data
236 * The following to functions do NOT understand what kind of data they
238 * The are merely wrapping get/put in order to count line numbers.
240 static void lex_ungetch(lex_file *lex, int ch);
241 static int lex_try_trigraph(lex_file *lex, int old)
246 lex_ungetch(lex, c2);
252 case '=': return '#';
253 case '/': return '\\';
254 case '\'': return '^';
255 case '(': return '[';
256 case ')': return ']';
257 case '!': return '|';
258 case '<': return '{';
259 case '>': return '}';
260 case '-': return '~';
262 lex_ungetch(lex, c3);
263 lex_ungetch(lex, c2);
268 static int lex_try_digraph(lex_file *lex, int ch)
272 if (ch == '<' && c2 == ':')
274 else if (ch == ':' && c2 == '>')
276 else if (ch == '<' && c2 == '%')
278 else if (ch == '%' && c2 == '>')
280 else if (ch == '%' && c2 == ':')
282 lex_ungetch(lex, c2);
286 static int lex_getch(lex_file *lex)
292 if (lex->peek[lex->peekpos] == '\n')
294 return lex->peek[lex->peekpos];
301 return lex_try_trigraph(lex, ch);
302 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
303 return lex_try_digraph(lex, ch);
307 static void lex_ungetch(lex_file *lex, int ch)
309 lex->peek[lex->peekpos++] = ch;
314 /* classify characters
315 * some additions to the is*() functions of ctype.h
318 /* Idents are alphanumberic, but they start with alpha or _ */
319 static bool isident_start(int ch)
321 return isalpha(ch) || ch == '_';
324 static bool isident(int ch)
326 return isident_start(ch) || isdigit(ch);
329 /* isxdigit_only is used when we already know it's not a digit
330 * and want to see if it's a hex digit anyway.
332 static bool isxdigit_only(int ch)
334 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
337 /* Append a character to the token buffer */
338 static void lex_tokench(lex_file *lex, int ch)
340 vec_push(lex->tok.value, ch);
343 /* Append a trailing null-byte */
344 static void lex_endtoken(lex_file *lex)
346 vec_push(lex->tok.value, 0);
347 vec_shrinkby(lex->tok.value, 1);
350 /* Skip whitespace and comments and return the first
351 * non-white character.
352 * As this makes use of the above getch() ungetch() functions,
353 * we don't need to care at all about line numbering anymore.
355 * In theory, this function should only be used at the beginning
356 * of lexing, or when we *know* the next character is part of the token.
357 * Otherwise, if the parser throws an error, the linenumber may not be
358 * the line of the error, but the line of the next token AFTER the error.
360 * This is currently only problematic when using c-like string-continuation,
361 * since comments and whitespaces are allowed between 2 such strings.
365 "A continuation of the previous string"
366 // This line is skipped
369 * In this case, if the parse decides it didn't actually want a string,
370 * and uses lex->line to print an error, it will show the ', foo);' line's
373 * On the other hand, the parser is supposed to remember the line of the next
374 * token's beginning. In this case we would want skipwhite() to be called
375 * AFTER reading a token, so that the parser, before reading the NEXT token,
376 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
379 * here is to store the line of the first character after skipping
380 * the initial whitespace in lex->sline, this happens in lex_do.
382 static int lex_skipwhite(lex_file *lex)
385 bool haswhite = false;
390 while (ch != EOF && isspace(ch)) {
391 if (lex->flags.preprocessing) {
394 /* see if there was whitespace first */
395 if (haswhite) { /* (vec_size(lex->tok.value)) { */
396 lex_ungetch(lex, ch);
400 /* otherwise return EOL */
404 lex_tokench(lex, ch);
413 /* one line comment */
416 if (lex->flags.preprocessing) {
419 lex_tokench(lex, '/');
420 lex_tokench(lex, '/');
422 lex_tokench(lex, ' ');
423 lex_tokench(lex, ' ');
426 while (ch != EOF && ch != '\n') {
427 if (lex->flags.preprocessing)
428 lex_tokench(lex, ' '); /* ch); */
431 if (lex->flags.preprocessing) {
432 lex_ungetch(lex, '\n');
440 /* multiline comment */
441 if (lex->flags.preprocessing) {
444 lex_tokench(lex, '/');
445 lex_tokench(lex, '*');
447 lex_tokench(lex, ' ');
448 lex_tokench(lex, ' ');
457 if (lex->flags.preprocessing) {
459 lex_tokench(lex, '*');
460 lex_tokench(lex, '/');
462 lex_tokench(lex, ' ');
463 lex_tokench(lex, ' ');
468 if (lex->flags.preprocessing) {
469 lex_tokench(lex, ' '); /* ch); */
472 ch = ' '; /* cause TRUE in the isspace check */
475 /* Otherwise roll back to the slash and break out of the loop */
476 lex_ungetch(lex, ch);
480 } while (ch != EOF && isspace(ch));
484 lex_ungetch(lex, ch);
491 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
496 while (ch != EOF && isident(ch))
498 lex_tokench(lex, ch);
502 /* last ch was not an ident ch: */
503 lex_ungetch(lex, ch);
508 /* read one ident for the frame list */
509 static int lex_parse_frame(lex_file *lex)
516 while (ch != EOF && ch != '\n' && isspace(ch))
522 if (!isident_start(ch)) {
523 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
527 lex_tokench(lex, ch);
528 if (!lex_finish_ident(lex))
534 /* read a list of $frames */
535 static bool lex_finish_frames(lex_file *lex)
542 rc = lex_parse_frame(lex);
543 if (rc > 0) /* end of line */
545 if (rc < 0) /* error */
548 for (i = 0; i < vec_size(lex->frames); ++i) {
549 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
550 lex->frames[i].value = lex->framevalue++;
551 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
556 if (i < vec_size(lex->frames))
559 m.value = lex->framevalue++;
560 m.name = util_strdup(lex->tok.value);
561 vec_shrinkto(lex->tok.value, 0);
562 vec_push(lex->frames, m);
566 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
574 return TOKEN_STRINGCONST;
576 if (!lex->flags.preprocessing && ch == '\\') {
579 lexerror(lex, "unexpected end of file");
580 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
581 return (lex->tok.ttype = TOKEN_ERROR);
586 case 'a': ch = '\a'; break;
587 case 'b': ch = '\b'; break;
588 case 'r': ch = '\r'; break;
589 case 'n': ch = '\n'; break;
590 case 't': ch = '\t'; break;
591 case 'f': ch = '\f'; break;
592 case 'v': ch = '\v'; break;
594 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
595 /* so we just add the character plus backslash no matter what it actually is */
596 lex_tokench(lex, '\\');
598 /* add the character finally */
599 lex_tokench(lex, ch);
602 lex_tokench(lex, ch);
604 lexerror(lex, "unexpected end of file within string constant");
605 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
606 return (lex->tok.ttype = TOKEN_ERROR);
609 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
615 /* parse a number... */
616 lex->tok.ttype = TOKEN_INTCONST;
618 lex_tokench(lex, ch);
621 if (ch != '.' && !isdigit(ch))
623 if (lastch != '0' || ch != 'x')
625 /* end of the number or EOF */
626 lex_ungetch(lex, ch);
629 lex->tok.constval.i = lastch - '0';
630 return lex->tok.ttype;
636 /* EOF would have been caught above */
640 lex_tokench(lex, ch);
642 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
644 lex_tokench(lex, ch);
648 /* NOT else, '.' can come from above as well */
649 if (ch == '.' && !ishex)
651 /* Allow floating comma in non-hex mode */
652 lex->tok.ttype = TOKEN_FLOATCONST;
653 lex_tokench(lex, ch);
655 /* continue digits-only */
659 lex_tokench(lex, ch);
663 /* put back the last character */
664 /* but do not put back the trailing 'f' or a float */
665 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
668 /* generally we don't want words to follow numbers: */
670 lexerror(lex, "unexpected trailing characters after number");
671 return (lex->tok.ttype = TOKEN_ERROR);
673 lex_ungetch(lex, ch);
676 if (lex->tok.ttype == TOKEN_FLOATCONST)
677 lex->tok.constval.f = strtod(lex->tok.value, NULL);
679 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
680 return lex->tok.ttype;
683 int lex_do(lex_file *lex)
694 ch = lex_skipwhite(lex);
695 if (!lex->flags.mergelines || ch != '\\')
699 lex_ungetch(lex, ch);
703 /* we reached a linemerge */
704 lex_tokench(lex, '\n');
708 lex->sline = lex->line;
709 lex->tok.ctx.line = lex->sline;
710 lex->tok.ctx.file = lex->name;
712 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
713 return (lex->tok.ttype = ch);
717 return (lex->tok.ttype = TOKEN_FATAL);
721 return (lex->tok.ttype = TOKEN_EOF);
724 /* modelgen / spiritgen commands */
730 if (!isident_start(ch)) {
731 lexerror(lex, "hanging '$' modelgen/spritegen command line");
734 lex_tokench(lex, ch);
735 if (!lex_finish_ident(lex))
736 return (lex->tok.ttype = TOKEN_ERROR);
738 /* skip the known commands */
741 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
743 /* frame/framesave command works like an enum
744 * similar to fteqcc we handle this in the lexer.
745 * The reason for this is that it is sensitive to newlines,
746 * which the parser is unaware of
748 if (!lex_finish_frames(lex))
749 return (lex->tok.ttype = TOKEN_ERROR);
753 if (!strcmp(v, "framevalue"))
756 while (ch != EOF && isspace(ch) && ch != '\n')
760 lexerror(lex, "$framevalue requires an integer parameter");
765 lex->tok.ttype = lex_finish_digit(lex, ch);
767 if (lex->tok.ttype != TOKEN_INTCONST) {
768 lexerror(lex, "$framevalue requires an integer parameter");
771 lex->framevalue = lex->tok.constval.i;
775 if (!strcmp(v, "framerestore"))
781 rc = lex_parse_frame(lex);
784 lexerror(lex, "$framerestore requires a framename parameter");
788 return (lex->tok.ttype = TOKEN_FATAL);
791 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
792 if (!strcmp(v, lex->frames[frame].name)) {
793 lex->framevalue = lex->frames[frame].value;
797 lexerror(lex, "unknown framename `%s`", v);
801 if (!strcmp(v, "modelname"))
807 rc = lex_parse_frame(lex);
810 lexerror(lex, "$modelname requires a parameter");
814 return (lex->tok.ttype = TOKEN_FATAL);
817 if (lex->modelname) {
819 m.value = lex->framevalue;
820 m.name = lex->modelname;
821 lex->modelname = NULL;
822 vec_push(lex->frames, m);
824 lex->modelname = lex->tok.value;
825 lex->tok.value = NULL;
829 if (!strcmp(v, "flush"))
832 for (frame = 0; frame < vec_size(lex->frames); ++frame)
833 mem_d(lex->frames[frame].name);
834 vec_free(lex->frames);
835 /* skip line (fteqcc does it too) */
837 while (ch != EOF && ch != '\n')
842 if (!strcmp(v, "cd") ||
843 !strcmp(v, "origin") ||
844 !strcmp(v, "base") ||
845 !strcmp(v, "flags") ||
846 !strcmp(v, "scale") ||
851 while (ch != EOF && ch != '\n')
856 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
857 if (!strcmp(v, lex->frames[frame].name)) {
858 lex->tok.constval.i = lex->frames[frame].value;
859 return (lex->tok.ttype = TOKEN_INTCONST);
863 lexerror(lex, "invalid frame macro");
867 /* single-character tokens */
872 lex_tokench(lex, ch);
874 if (lex->flags.noops)
875 return (lex->tok.ttype = ch);
877 return (lex->tok.ttype = TOKEN_OPERATOR);
886 lex_tokench(lex, ch);
888 return (lex->tok.ttype = ch);
893 if (lex->flags.noops)
895 /* Detect characters early which are normally
896 * operators OR PART of an operator.
913 lex_tokench(lex, ch);
915 return (lex->tok.ttype = ch);
922 lex_tokench(lex, ch);
923 /* peak ahead once */
924 nextch = lex_getch(lex);
926 lex_ungetch(lex, nextch);
928 return (lex->tok.ttype = ch);
930 /* peak ahead again */
931 nextch = lex_getch(lex);
933 lex_ungetch(lex, nextch);
934 lex_ungetch(lex, nextch);
936 return (lex->tok.ttype = ch);
938 /* fill the token to be "..." */
939 lex_tokench(lex, ch);
940 lex_tokench(lex, ch);
942 return (lex->tok.ttype = TOKEN_DOTS);
946 if (ch == ',' || ch == '.') {
947 lex_tokench(lex, ch);
949 return (lex->tok.ttype = TOKEN_OPERATOR);
952 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
953 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
954 ch == '=' || ch == '!' || /* ==, != */
955 ch == '&' || ch == '|') /* &&, ||, &=, |= */
957 lex_tokench(lex, ch);
959 nextch = lex_getch(lex);
960 if (nextch == ch || nextch == '=') {
961 lex_tokench(lex, nextch);
962 } else if (ch == '-' && nextch == '>') {
963 lex_tokench(lex, nextch);
965 lex_ungetch(lex, nextch);
968 return (lex->tok.ttype = TOKEN_OPERATOR);
972 if (ch == '^' || ch == '~' || ch == '!')
974 lex_tokench(lex, ch);
976 return (lex->tok.ttype = TOKEN_OPERATOR);
980 if (ch == '*' || ch == '/') /* *=, /= */
982 lex_tokench(lex, ch);
984 nextch = lex_getch(lex);
986 lex_tokench(lex, nextch);
988 lex_ungetch(lex, nextch);
991 return (lex->tok.ttype = TOKEN_OPERATOR);
994 if (isident_start(ch))
998 lex_tokench(lex, ch);
999 if (!lex_finish_ident(lex)) {
1001 return (lex->tok.ttype = TOKEN_ERROR);
1004 lex->tok.ttype = TOKEN_IDENT;
1007 if (!strcmp(v, "void")) {
1008 lex->tok.ttype = TOKEN_TYPENAME;
1009 lex->tok.constval.t = TYPE_VOID;
1010 } else if (!strcmp(v, "int")) {
1011 lex->tok.ttype = TOKEN_TYPENAME;
1012 lex->tok.constval.t = TYPE_INTEGER;
1013 } else if (!strcmp(v, "float")) {
1014 lex->tok.ttype = TOKEN_TYPENAME;
1015 lex->tok.constval.t = TYPE_FLOAT;
1016 } else if (!strcmp(v, "string")) {
1017 lex->tok.ttype = TOKEN_TYPENAME;
1018 lex->tok.constval.t = TYPE_STRING;
1019 } else if (!strcmp(v, "entity")) {
1020 lex->tok.ttype = TOKEN_TYPENAME;
1021 lex->tok.constval.t = TYPE_ENTITY;
1022 } else if (!strcmp(v, "vector")) {
1023 lex->tok.ttype = TOKEN_TYPENAME;
1024 lex->tok.constval.t = TYPE_VECTOR;
1025 } else if (!strcmp(v, "for") ||
1026 !strcmp(v, "while") ||
1029 !strcmp(v, "else") ||
1030 !strcmp(v, "local") ||
1031 !strcmp(v, "return") ||
1032 !strcmp(v, "not") ||
1033 !strcmp(v, "const"))
1035 lex->tok.ttype = TOKEN_KEYWORD;
1037 else if (opts_standard != COMPILER_QCC)
1039 /* other standards reserve these keywords */
1040 if (!strcmp(v, "switch") ||
1041 !strcmp(v, "struct") ||
1042 !strcmp(v, "union") ||
1043 !strcmp(v, "break") ||
1044 !strcmp(v, "continue") ||
1047 lex->tok.ttype = TOKEN_KEYWORD;
1051 return lex->tok.ttype;
1056 lex->flags.nodigraphs = true;
1057 if (lex->flags.preprocessing)
1058 lex_tokench(lex, ch);
1059 lex->tok.ttype = lex_finish_string(lex, '"');
1060 if (lex->flags.preprocessing)
1061 lex_tokench(lex, ch);
1062 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1064 /* Allow c style "string" "continuation" */
1065 ch = lex_skipwhite(lex);
1067 lex_ungetch(lex, ch);
1071 lex->tok.ttype = lex_finish_string(lex, '"');
1073 lex->flags.nodigraphs = false;
1075 return lex->tok.ttype;
1080 /* we parse character constants like string,
1081 * but return TOKEN_CHARCONST, or a vector type if it fits...
1082 * Likewise actual unescaping has to be done by the parser.
1083 * The difference is we don't allow 'char' 'continuation'.
1085 if (lex->flags.preprocessing)
1086 lex_tokench(lex, ch);
1087 lex->tok.ttype = lex_finish_string(lex, '\'');
1088 if (lex->flags.preprocessing)
1089 lex_tokench(lex, ch);
1092 /* It's a vector if we can successfully scan 3 floats */
1094 if (sscanf_s(lex->tok.value, " %f %f %f ",
1095 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1097 if (sscanf(lex->tok.value, " %f %f %f ",
1098 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1102 lex->tok.ttype = TOKEN_VECTORCONST;
1105 return lex->tok.ttype;
1110 lex->tok.ttype = lex_finish_digit(lex, ch);
1112 return lex->tok.ttype;
1115 lexerror(lex, "unknown token");
1116 return (lex->tok.ttype = TOKEN_ERROR);