9 MEM_VEC_FUNCTIONS(token, char, value)
10 MEM_VEC_FUNCTIONS(lex_file, frame_macro, frames)
12 VECTOR_MAKE(char*, lex_filenames);
14 void lexerror(lex_file *lex, const char *fmt, ...)
19 vprintmsg(LVL_ERROR, lex->name, lex->sline, "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 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();
126 lex->tok.value_count = 0;
127 lex->tok.constval.t = 0;
128 lex->tok.ctx.line = lex->sline;
129 lex->tok.ctx.file = lex->name;
134 lex_file* lex_open(const char *file)
137 FILE *in = util_fopen(file, "rb");
140 lexerror(NULL, "open failed: '%s'\n", file);
144 lex = (lex_file*)mem_a(sizeof(*lex));
147 lexerror(NULL, "out of memory\n");
151 memset(lex, 0, sizeof(*lex));
154 lex->name = util_strdup(file);
155 lex->line = 1; /* we start counting at 1 */
160 lex_filenames_add(lex->name);
165 void lex_cleanup(void)
168 for (i = 0; i < lex_filenames_elements; ++i)
169 mem_d(lex_filenames_data[i]);
170 mem_d(lex_filenames_data);
173 void lex_close(lex_file *lex)
176 for (i = 0; i < lex->frames_count; ++i)
177 mem_d(lex->frames[i].name);
178 MEM_VECTOR_CLEAR(lex, frames);
181 mem_d(lex->modelname);
187 token_delete(lex->tok);
189 MEM_VECTOR_CLEAR(&(lex->tok), value);
191 /* mem_d(lex->name); collected in lex_filenames */
195 /* Get or put-back data
196 * The following to functions do NOT understand what kind of data they
198 * The are merely wrapping get/put in order to count line numbers.
200 static void lex_ungetch(lex_file *lex, int ch);
201 static int lex_try_trigraph(lex_file *lex, int old)
204 c2 = fgetc(lex->file);
206 lex_ungetch(lex, c2);
210 c3 = fgetc(lex->file);
212 case '=': return '#';
213 case '/': return '\\';
214 case '\'': return '^';
215 case '(': return '[';
216 case ')': return ']';
217 case '!': return '|';
218 case '<': return '{';
219 case '>': return '}';
220 case '-': return '~';
222 lex_ungetch(lex, c3);
223 lex_ungetch(lex, c2);
228 static int lex_getch(lex_file *lex)
234 if (lex->peek[lex->peekpos] == '\n')
236 return lex->peek[lex->peekpos];
239 ch = fgetc(lex->file);
243 return lex_try_trigraph(lex, ch);
247 static void lex_ungetch(lex_file *lex, int ch)
249 lex->peek[lex->peekpos++] = ch;
254 /* classify characters
255 * some additions to the is*() functions of ctype.h
258 /* Idents are alphanumberic, but they start with alpha or _ */
259 static bool isident_start(int ch)
261 return isalpha(ch) || ch == '_';
264 static bool isident(int ch)
266 return isident_start(ch) || isdigit(ch);
269 /* isxdigit_only is used when we already know it's not a digit
270 * and want to see if it's a hex digit anyway.
272 static bool isxdigit_only(int ch)
274 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
277 /* Skip whitespace and comments and return the first
278 * non-white character.
279 * As this makes use of the above getch() ungetch() functions,
280 * we don't need to care at all about line numbering anymore.
282 * In theory, this function should only be used at the beginning
283 * of lexing, or when we *know* the next character is part of the token.
284 * Otherwise, if the parser throws an error, the linenumber may not be
285 * the line of the error, but the line of the next token AFTER the error.
287 * This is currently only problematic when using c-like string-continuation,
288 * since comments and whitespaces are allowed between 2 such strings.
292 "A continuation of the previous string"
293 // This line is skipped
296 * In this case, if the parse decides it didn't actually want a string,
297 * and uses lex->line to print an error, it will show the ', foo);' line's
300 * On the other hand, the parser is supposed to remember the line of the next
301 * token's beginning. In this case we would want skipwhite() to be called
302 * AFTER reading a token, so that the parser, before reading the NEXT token,
303 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
306 * here is to store the line of the first character after skipping
307 * the initial whitespace in lex->sline, this happens in lex_do.
309 static int lex_skipwhite(lex_file *lex)
316 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
322 /* one line comment */
325 /* check for special: '/', '/', '*', '/' */
334 while (ch != EOF && ch != '\n') {
341 /* multiline comment */
353 if (ch == '/') /* allow *//* direct following comment */
355 lex_ungetch(lex, ch);
356 ch = ' '; /* cause TRUE in the isspace check */
360 /* Otherwise roll back to the slash and break out of the loop */
361 lex_ungetch(lex, ch);
365 } while (ch != EOF && isspace(ch));
370 /* Append a character to the token buffer */
371 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
373 if (!token_value_add(&lex->tok, ch)) {
374 lexerror(lex, "out of memory");
380 /* Append a trailing null-byte */
381 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
383 if (!token_value_add(&lex->tok, 0)) {
384 lexerror(lex, "out of memory");
387 lex->tok.value_count--;
392 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
397 while (ch != EOF && isident(ch))
399 if (!lex_tokench(lex, ch))
400 return (lex->tok.ttype = TOKEN_FATAL);
404 /* last ch was not an ident ch: */
405 lex_ungetch(lex, ch);
410 /* read one ident for the frame list */
411 static int lex_parse_frame(lex_file *lex)
418 while (ch != EOF && ch != '\n' && isspace(ch))
424 if (!isident_start(ch)) {
425 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
429 if (!lex_tokench(lex, ch))
431 if (!lex_finish_ident(lex))
433 if (!lex_endtoken(lex))
438 /* read a list of $frames */
439 static bool lex_finish_frames(lex_file *lex)
446 rc = lex_parse_frame(lex);
447 if (rc > 0) /* end of line */
449 if (rc < 0) /* error */
452 for (i = 0; i < lex->frames_count; ++i) {
453 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
454 lex->frames[i].value = lex->framevalue++;
455 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
460 if (i < lex->frames_count)
463 m.value = lex->framevalue++;
464 m.name = lex->tok.value;
465 lex->tok.value = NULL;
466 lex->tok.value_alloc = lex->tok.value_count = 0;
467 if (!lex_file_frames_add(lex, m)) {
468 lexerror(lex, "out of memory");
474 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
482 return TOKEN_STRINGCONST;
487 lexerror(lex, "unexpected end of file");
488 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
489 return (lex->tok.ttype = TOKEN_ERROR);
494 case 'a': ch = '\a'; break;
495 case 'b': ch = '\b'; break;
496 case 'r': ch = '\r'; break;
497 case 'n': ch = '\n'; break;
498 case 't': ch = '\t'; break;
499 case 'f': ch = '\f'; break;
500 case 'v': ch = '\v'; break;
502 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
503 /* so we just add the character plus backslash no matter what it actually is */
504 if (!lex_tokench(lex, '\\'))
505 return (lex->tok.ttype = TOKEN_FATAL);
507 /* add the character finally */
508 if (!lex_tokench(lex, ch))
509 return (lex->tok.ttype = TOKEN_FATAL);
511 else if (!lex_tokench(lex, ch))
512 return (lex->tok.ttype = TOKEN_FATAL);
514 lexerror(lex, "unexpected end of file within string constant");
515 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
516 return (lex->tok.ttype = TOKEN_ERROR);
519 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
525 /* parse a number... */
526 lex->tok.ttype = TOKEN_INTCONST;
528 if (!lex_tokench(lex, ch))
529 return (lex->tok.ttype = TOKEN_FATAL);
532 if (ch != '.' && !isdigit(ch))
534 if (lastch != '0' || ch != 'x')
536 /* end of the number or EOF */
537 lex_ungetch(lex, ch);
538 if (!lex_endtoken(lex))
539 return (lex->tok.ttype = TOKEN_FATAL);
541 lex->tok.constval.i = lastch - '0';
542 return lex->tok.ttype;
548 /* EOF would have been caught above */
552 if (!lex_tokench(lex, ch))
553 return (lex->tok.ttype = TOKEN_FATAL);
555 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
557 if (!lex_tokench(lex, ch))
558 return (lex->tok.ttype = TOKEN_FATAL);
562 /* NOT else, '.' can come from above as well */
563 if (ch == '.' && !ishex)
565 /* Allow floating comma in non-hex mode */
566 lex->tok.ttype = TOKEN_FLOATCONST;
567 if (!lex_tokench(lex, ch))
568 return (lex->tok.ttype = TOKEN_FATAL);
570 /* continue digits-only */
574 if (!lex_tokench(lex, ch))
575 return (lex->tok.ttype = TOKEN_FATAL);
579 /* put back the last character */
580 /* but do not put back the trailing 'f' or a float */
581 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
584 /* generally we don't want words to follow numbers: */
586 lexerror(lex, "unexpected trailing characters after number");
587 return (lex->tok.ttype = TOKEN_ERROR);
589 lex_ungetch(lex, ch);
591 if (!lex_endtoken(lex))
592 return (lex->tok.ttype = TOKEN_FATAL);
593 if (lex->tok.ttype == TOKEN_FLOATCONST)
594 lex->tok.constval.f = strtod(lex->tok.value, NULL);
596 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
597 return lex->tok.ttype;
600 int lex_do(lex_file *lex)
610 ch = lex_skipwhite(lex);
611 lex->sline = lex->line;
612 lex->tok.ctx.line = lex->sline;
613 lex->tok.ctx.file = lex->name;
616 return (lex->tok.ttype = TOKEN_FATAL);
620 return (lex->tok.ttype = TOKEN_EOF);
623 /* modelgen / spiritgen commands */
629 if (!isident_start(ch)) {
630 lexerror(lex, "hanging '$' modelgen/spritegen command line");
633 if (!lex_tokench(lex, ch))
634 return (lex->tok.ttype = TOKEN_FATAL);
635 if (!lex_finish_ident(lex))
636 return (lex->tok.ttype = TOKEN_ERROR);
637 if (!lex_endtoken(lex))
638 return (lex->tok.ttype = TOKEN_FATAL);
639 /* skip the known commands */
642 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
644 /* frame/framesave command works like an enum
645 * similar to fteqcc we handle this in the lexer.
646 * The reason for this is that it is sensitive to newlines,
647 * which the parser is unaware of
649 if (!lex_finish_frames(lex))
650 return (lex->tok.ttype = TOKEN_ERROR);
654 if (!strcmp(v, "framevalue"))
657 while (ch != EOF && isspace(ch) && ch != '\n')
661 lexerror(lex, "$framevalue requires an integer parameter");
666 lex->tok.ttype = lex_finish_digit(lex, ch);
667 if (!lex_endtoken(lex))
668 return (lex->tok.ttype = TOKEN_FATAL);
669 if (lex->tok.ttype != TOKEN_INTCONST) {
670 lexerror(lex, "$framevalue requires an integer parameter");
673 lex->framevalue = lex->tok.constval.i;
677 if (!strcmp(v, "framerestore"))
683 rc = lex_parse_frame(lex);
686 lexerror(lex, "$framerestore requires a framename parameter");
690 return (lex->tok.ttype = TOKEN_FATAL);
693 for (frame = 0; frame < lex->frames_count; ++frame) {
694 if (!strcmp(v, lex->frames[frame].name)) {
695 lex->framevalue = lex->frames[frame].value;
699 lexerror(lex, "unknown framename `%s`", v);
703 if (!strcmp(v, "modelname"))
709 rc = lex_parse_frame(lex);
712 lexerror(lex, "$framerestore requires a framename parameter");
716 return (lex->tok.ttype = TOKEN_FATAL);
719 if (lex->modelname) {
721 m.value = lex->framevalue;
722 m.name = lex->modelname;
723 lex->modelname = NULL;
724 if (!lex_file_frames_add(lex, m)) {
725 lexerror(lex, "out of memory");
726 return (lex->tok.ttype = TOKEN_FATAL);
729 lex->modelname = lex->tok.value;
730 lex->tok.value = NULL;
731 lex->tok.value_alloc = lex->tok.value_count = 0;
732 for (frame = 0; frame < lex->frames_count; ++frame) {
733 if (!strcmp(v, lex->frames[frame].name)) {
734 lex->framevalue = lex->frames[frame].value;
741 if (!strcmp(v, "flush"))
744 for (frame = 0; frame < lex->frames_count; ++frame)
745 mem_d(lex->frames[frame].name);
746 MEM_VECTOR_CLEAR(lex, frames);
747 /* skip line (fteqcc does it too) */
749 while (ch != EOF && ch != '\n')
754 if (!strcmp(v, "cd") ||
755 !strcmp(v, "origin") ||
756 !strcmp(v, "base") ||
757 !strcmp(v, "flags") ||
758 !strcmp(v, "scale") ||
763 while (ch != EOF && ch != '\n')
768 for (frame = 0; frame < lex->frames_count; ++frame) {
769 if (!strcmp(v, lex->frames[frame].name)) {
770 lex->tok.constval.i = lex->frames[frame].value;
771 return (lex->tok.ttype = TOKEN_INTCONST);
775 lexerror(lex, "invalid frame macro");
779 /* single-character tokens */
783 if (!lex_tokench(lex, ch) ||
786 return (lex->tok.ttype = TOKEN_FATAL);
788 if (lex->flags.noops)
789 return (lex->tok.ttype = ch);
791 return (lex->tok.ttype = TOKEN_OPERATOR);
800 if (!lex_tokench(lex, ch) ||
803 return (lex->tok.ttype = TOKEN_FATAL);
805 return (lex->tok.ttype = ch);
810 if (lex->flags.noops)
812 /* Detect characters early which are normally
813 * operators OR PART of an operator.
830 if (!lex_tokench(lex, ch) ||
833 return (lex->tok.ttype = TOKEN_FATAL);
835 return (lex->tok.ttype = ch);
842 if (!lex_tokench(lex, ch))
843 return (lex->tok.ttype = TOKEN_FATAL);
844 /* peak ahead once */
845 nextch = lex_getch(lex);
847 lex_ungetch(lex, nextch);
848 if (!lex_endtoken(lex))
849 return (lex->tok.ttype = TOKEN_FATAL);
850 return (lex->tok.ttype = ch);
852 /* peak ahead again */
853 nextch = lex_getch(lex);
855 lex_ungetch(lex, nextch);
856 lex_ungetch(lex, nextch);
857 if (!lex_endtoken(lex))
858 return (lex->tok.ttype = TOKEN_FATAL);
859 return (lex->tok.ttype = ch);
861 /* fill the token to be "..." */
862 if (!lex_tokench(lex, ch) ||
863 !lex_tokench(lex, ch) ||
866 return (lex->tok.ttype = TOKEN_FATAL);
868 return (lex->tok.ttype = TOKEN_DOTS);
872 if (ch == ',' || ch == '.') {
873 if (!lex_tokench(lex, ch) ||
876 return (lex->tok.ttype = TOKEN_FATAL);
878 return (lex->tok.ttype = TOKEN_OPERATOR);
881 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
882 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
883 ch == '=' || ch == '!' || /* ==, != */
884 ch == '&' || ch == '|') /* &&, ||, &=, |= */
886 if (!lex_tokench(lex, ch))
887 return (lex->tok.ttype = TOKEN_FATAL);
889 nextch = lex_getch(lex);
890 if (nextch == ch || nextch == '=') {
891 if (!lex_tokench(lex, nextch))
892 return (lex->tok.ttype = TOKEN_FATAL);
893 } else if (ch == '-' && nextch == '>') {
894 if (!lex_tokench(lex, nextch))
895 return (lex->tok.ttype = TOKEN_FATAL);
897 lex_ungetch(lex, nextch);
899 if (!lex_endtoken(lex))
900 return (lex->tok.ttype = TOKEN_FATAL);
901 return (lex->tok.ttype = TOKEN_OPERATOR);
905 if (ch == '^' || ch == '~' || ch == '!')
907 if (!lex_tokench(lex, ch) ||
910 return (lex->tok.ttype = TOKEN_FATAL);
912 return (lex->tok.ttype = TOKEN_OPERATOR);
916 if (ch == '*' || ch == '/') /* *=, /= */
918 if (!lex_tokench(lex, ch))
919 return (lex->tok.ttype = TOKEN_FATAL);
921 nextch = lex_getch(lex);
923 if (!lex_tokench(lex, nextch))
924 return (lex->tok.ttype = TOKEN_FATAL);
926 lex_ungetch(lex, nextch);
928 if (!lex_endtoken(lex))
929 return (lex->tok.ttype = TOKEN_FATAL);
930 return (lex->tok.ttype = TOKEN_OPERATOR);
933 if (isident_start(ch))
937 if (!lex_tokench(lex, ch))
938 return (lex->tok.ttype = TOKEN_FATAL);
939 if (!lex_finish_ident(lex)) {
941 return (lex->tok.ttype = TOKEN_ERROR);
943 if (!lex_endtoken(lex))
944 return (lex->tok.ttype = TOKEN_FATAL);
945 lex->tok.ttype = TOKEN_IDENT;
948 if (!strcmp(v, "void")) {
949 lex->tok.ttype = TOKEN_TYPENAME;
950 lex->tok.constval.t = TYPE_VOID;
951 } else if (!strcmp(v, "int")) {
952 lex->tok.ttype = TOKEN_TYPENAME;
953 lex->tok.constval.t = TYPE_INTEGER;
954 } else if (!strcmp(v, "float")) {
955 lex->tok.ttype = TOKEN_TYPENAME;
956 lex->tok.constval.t = TYPE_FLOAT;
957 } else if (!strcmp(v, "string")) {
958 lex->tok.ttype = TOKEN_TYPENAME;
959 lex->tok.constval.t = TYPE_STRING;
960 } else if (!strcmp(v, "entity")) {
961 lex->tok.ttype = TOKEN_TYPENAME;
962 lex->tok.constval.t = TYPE_ENTITY;
963 } else if (!strcmp(v, "vector")) {
964 lex->tok.ttype = TOKEN_TYPENAME;
965 lex->tok.constval.t = TYPE_VECTOR;
966 } else if (!strcmp(v, "for") ||
967 !strcmp(v, "while") ||
970 !strcmp(v, "else") ||
971 !strcmp(v, "local") ||
972 !strcmp(v, "return") ||
974 lex->tok.ttype = TOKEN_KEYWORD;
976 return lex->tok.ttype;
981 lex->tok.ttype = lex_finish_string(lex, '"');
982 while (lex->tok.ttype == TOKEN_STRINGCONST)
984 /* Allow c style "string" "continuation" */
985 ch = lex_skipwhite(lex);
987 lex_ungetch(lex, ch);
991 lex->tok.ttype = lex_finish_string(lex, '"');
993 if (!lex_endtoken(lex))
994 return (lex->tok.ttype = TOKEN_FATAL);
995 return lex->tok.ttype;
1000 /* we parse character constants like string,
1001 * but return TOKEN_CHARCONST, or a vector type if it fits...
1002 * Likewise actual unescaping has to be done by the parser.
1003 * The difference is we don't allow 'char' 'continuation'.
1005 lex->tok.ttype = lex_finish_string(lex, '\'');
1006 if (!lex_endtoken(lex))
1007 return (lex->tok.ttype = TOKEN_FATAL);
1009 /* It's a vector if we can successfully scan 3 floats */
1011 if (sscanf_s(lex->tok.value, " %f %f %f ",
1012 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1014 if (sscanf(lex->tok.value, " %f %f %f ",
1015 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1018 lex->tok.ttype = TOKEN_VECTORCONST;
1021 return lex->tok.ttype;
1026 lex->tok.ttype = lex_finish_digit(lex, ch);
1027 if (!lex_endtoken(lex))
1028 return (lex->tok.ttype = TOKEN_FATAL);
1029 return lex->tok.ttype;
1032 lexerror(lex, "unknown token");
1033 return (lex->tok.ttype = TOKEN_ERROR);