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 int lex_getch(lex_file *lex)
206 if (lex->peek[lex->peekpos] == '\n')
208 return lex->peek[lex->peekpos];
211 ch = fgetc(lex->file);
217 static void lex_ungetch(lex_file *lex, int ch)
219 lex->peek[lex->peekpos++] = ch;
224 /* classify characters
225 * some additions to the is*() functions of ctype.h
228 /* Idents are alphanumberic, but they start with alpha or _ */
229 static bool isident_start(int ch)
231 return isalpha(ch) || ch == '_';
234 static bool isident(int ch)
236 return isident_start(ch) || isdigit(ch);
239 /* isxdigit_only is used when we already know it's not a digit
240 * and want to see if it's a hex digit anyway.
242 static bool isxdigit_only(int ch)
244 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
247 /* Skip whitespace and comments and return the first
248 * non-white character.
249 * As this makes use of the above getch() ungetch() functions,
250 * we don't need to care at all about line numbering anymore.
252 * In theory, this function should only be used at the beginning
253 * of lexing, or when we *know* the next character is part of the token.
254 * Otherwise, if the parser throws an error, the linenumber may not be
255 * the line of the error, but the line of the next token AFTER the error.
257 * This is currently only problematic when using c-like string-continuation,
258 * since comments and whitespaces are allowed between 2 such strings.
262 "A continuation of the previous string"
263 // This line is skipped
266 * In this case, if the parse decides it didn't actually want a string,
267 * and uses lex->line to print an error, it will show the ', foo);' line's
270 * On the other hand, the parser is supposed to remember the line of the next
271 * token's beginning. In this case we would want skipwhite() to be called
272 * AFTER reading a token, so that the parser, before reading the NEXT token,
273 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
276 * here is to store the line of the first character after skipping
277 * the initial whitespace in lex->sline, this happens in lex_do.
279 static int lex_skipwhite(lex_file *lex)
286 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
292 /* one line comment */
295 /* check for special: '/', '/', '*', '/' */
304 while (ch != EOF && ch != '\n') {
311 /* multiline comment */
323 if (ch == '/') /* allow *//* direct following comment */
325 lex_ungetch(lex, ch);
326 ch = ' '; /* cause TRUE in the isspace check */
330 /* Otherwise roll back to the slash and break out of the loop */
331 lex_ungetch(lex, ch);
335 } while (ch != EOF && isspace(ch));
340 /* Append a character to the token buffer */
341 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
343 if (!token_value_add(&lex->tok, ch)) {
344 lexerror(lex, "out of memory");
350 /* Append a trailing null-byte */
351 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
353 if (!token_value_add(&lex->tok, 0)) {
354 lexerror(lex, "out of memory");
357 lex->tok.value_count--;
362 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
367 while (ch != EOF && isident(ch))
369 if (!lex_tokench(lex, ch))
370 return (lex->tok.ttype = TOKEN_FATAL);
374 /* last ch was not an ident ch: */
375 lex_ungetch(lex, ch);
380 /* read one ident for the frame list */
381 static int lex_parse_frame(lex_file *lex)
388 while (ch != EOF && ch != '\n' && isspace(ch))
394 if (!isident_start(ch)) {
395 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
399 if (!lex_tokench(lex, ch))
401 if (!lex_finish_ident(lex))
403 if (!lex_endtoken(lex))
408 /* read a list of $frames */
409 static bool lex_finish_frames(lex_file *lex)
416 rc = lex_parse_frame(lex);
417 if (rc > 0) /* end of line */
419 if (rc < 0) /* error */
422 for (i = 0; i < lex->frames_count; ++i) {
423 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
424 lex->frames[i].value = lex->framevalue++;
425 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
430 if (i < lex->frames_count)
433 m.value = lex->framevalue++;
434 m.name = lex->tok.value;
435 lex->tok.value = NULL;
436 lex->tok.value_alloc = lex->tok.value_count = 0;
437 if (!lex_file_frames_add(lex, m)) {
438 lexerror(lex, "out of memory");
444 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
452 return TOKEN_STRINGCONST;
457 lexerror(lex, "unexpected end of file");
458 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
459 return (lex->tok.ttype = TOKEN_ERROR);
464 case 'a': ch = '\a'; break;
465 case 'b': ch = '\b'; break;
466 case 'r': ch = '\r'; break;
467 case 'n': ch = '\n'; break;
468 case 't': ch = '\t'; break;
469 case 'f': ch = '\f'; break;
470 case 'v': ch = '\v'; break;
472 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
473 /* so we just add the character plus backslash no matter what it actually is */
474 if (!lex_tokench(lex, '\\'))
475 return (lex->tok.ttype = TOKEN_FATAL);
477 /* add the character finally */
478 if (!lex_tokench(lex, ch))
479 return (lex->tok.ttype = TOKEN_FATAL);
481 else if (!lex_tokench(lex, ch))
482 return (lex->tok.ttype = TOKEN_FATAL);
484 lexerror(lex, "unexpected end of file within string constant");
485 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
486 return (lex->tok.ttype = TOKEN_ERROR);
489 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
495 /* parse a number... */
496 lex->tok.ttype = TOKEN_INTCONST;
498 if (!lex_tokench(lex, ch))
499 return (lex->tok.ttype = TOKEN_FATAL);
502 if (ch != '.' && !isdigit(ch))
504 if (lastch != '0' || ch != 'x')
506 /* end of the number or EOF */
507 lex_ungetch(lex, ch);
508 if (!lex_endtoken(lex))
509 return (lex->tok.ttype = TOKEN_FATAL);
511 lex->tok.constval.i = lastch - '0';
512 return lex->tok.ttype;
518 /* EOF would have been caught above */
522 if (!lex_tokench(lex, ch))
523 return (lex->tok.ttype = TOKEN_FATAL);
525 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
527 if (!lex_tokench(lex, ch))
528 return (lex->tok.ttype = TOKEN_FATAL);
532 /* NOT else, '.' can come from above as well */
533 if (ch == '.' && !ishex)
535 /* Allow floating comma in non-hex mode */
536 lex->tok.ttype = TOKEN_FLOATCONST;
537 if (!lex_tokench(lex, ch))
538 return (lex->tok.ttype = TOKEN_FATAL);
540 /* continue digits-only */
544 if (!lex_tokench(lex, ch))
545 return (lex->tok.ttype = TOKEN_FATAL);
549 /* put back the last character */
550 /* but do not put back the trailing 'f' or a float */
551 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
554 /* generally we don't want words to follow numbers: */
556 lexerror(lex, "unexpected trailing characters after number");
557 return (lex->tok.ttype = TOKEN_ERROR);
559 lex_ungetch(lex, ch);
561 if (!lex_endtoken(lex))
562 return (lex->tok.ttype = TOKEN_FATAL);
563 if (lex->tok.ttype == TOKEN_FLOATCONST)
564 lex->tok.constval.f = strtod(lex->tok.value, NULL);
566 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
567 return lex->tok.ttype;
570 int lex_do(lex_file *lex)
580 ch = lex_skipwhite(lex);
581 lex->sline = lex->line;
582 lex->tok.ctx.line = lex->sline;
583 lex->tok.ctx.file = lex->name;
586 return (lex->tok.ttype = TOKEN_FATAL);
590 return (lex->tok.ttype = TOKEN_EOF);
593 /* modelgen / spiritgen commands */
599 if (!isident_start(ch)) {
600 lexerror(lex, "hanging '$' modelgen/spritegen command line");
603 if (!lex_tokench(lex, ch))
604 return (lex->tok.ttype = TOKEN_FATAL);
605 if (!lex_finish_ident(lex))
606 return (lex->tok.ttype = TOKEN_ERROR);
607 if (!lex_endtoken(lex))
608 return (lex->tok.ttype = TOKEN_FATAL);
609 /* skip the known commands */
612 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
614 /* frame/framesave command works like an enum
615 * similar to fteqcc we handle this in the lexer.
616 * The reason for this is that it is sensitive to newlines,
617 * which the parser is unaware of
619 if (!lex_finish_frames(lex))
620 return (lex->tok.ttype = TOKEN_ERROR);
624 if (!strcmp(v, "framevalue"))
627 while (ch != EOF && isspace(ch) && ch != '\n')
631 lexerror(lex, "$framevalue requires an integer parameter");
636 lex->tok.ttype = lex_finish_digit(lex, ch);
637 if (!lex_endtoken(lex))
638 return (lex->tok.ttype = TOKEN_FATAL);
639 if (lex->tok.ttype != TOKEN_INTCONST) {
640 lexerror(lex, "$framevalue requires an integer parameter");
643 lex->framevalue = lex->tok.constval.i;
647 if (!strcmp(v, "framerestore"))
653 rc = lex_parse_frame(lex);
656 lexerror(lex, "$framerestore requires a framename parameter");
660 return (lex->tok.ttype = TOKEN_FATAL);
663 for (frame = 0; frame < lex->frames_count; ++frame) {
664 if (!strcmp(v, lex->frames[frame].name)) {
665 lex->framevalue = lex->frames[frame].value;
669 lexerror(lex, "unknown framename `%s`", v);
673 if (!strcmp(v, "modelname"))
679 rc = lex_parse_frame(lex);
682 lexerror(lex, "$framerestore requires a framename parameter");
686 return (lex->tok.ttype = TOKEN_FATAL);
689 if (lex->modelname) {
691 m.value = lex->framevalue;
692 m.name = lex->modelname;
693 lex->modelname = NULL;
694 if (!lex_file_frames_add(lex, m)) {
695 lexerror(lex, "out of memory");
696 return (lex->tok.ttype = TOKEN_FATAL);
699 lex->modelname = lex->tok.value;
700 lex->tok.value = NULL;
701 lex->tok.value_alloc = lex->tok.value_count = 0;
702 for (frame = 0; frame < lex->frames_count; ++frame) {
703 if (!strcmp(v, lex->frames[frame].name)) {
704 lex->framevalue = lex->frames[frame].value;
711 if (!strcmp(v, "flush"))
714 for (frame = 0; frame < lex->frames_count; ++frame)
715 mem_d(lex->frames[frame].name);
716 MEM_VECTOR_CLEAR(lex, frames);
717 /* skip line (fteqcc does it too) */
719 while (ch != EOF && ch != '\n')
724 if (!strcmp(v, "cd") ||
725 !strcmp(v, "origin") ||
726 !strcmp(v, "base") ||
727 !strcmp(v, "flags") ||
728 !strcmp(v, "scale") ||
733 while (ch != EOF && ch != '\n')
738 for (frame = 0; frame < lex->frames_count; ++frame) {
739 if (!strcmp(v, lex->frames[frame].name)) {
740 lex->tok.constval.i = lex->frames[frame].value;
741 return (lex->tok.ttype = TOKEN_INTCONST);
745 lexerror(lex, "invalid frame macro");
749 /* single-character tokens */
753 if (!lex_tokench(lex, ch) ||
756 return (lex->tok.ttype = TOKEN_FATAL);
758 if (lex->flags.noops)
759 return (lex->tok.ttype = ch);
761 return (lex->tok.ttype = TOKEN_OPERATOR);
770 if (!lex_tokench(lex, ch) ||
773 return (lex->tok.ttype = TOKEN_FATAL);
775 return (lex->tok.ttype = ch);
780 if (lex->flags.noops)
782 /* Detect characters early which are normally
783 * operators OR PART of an operator.
800 if (!lex_tokench(lex, ch) ||
803 return (lex->tok.ttype = TOKEN_FATAL);
805 return (lex->tok.ttype = ch);
812 if (!lex_tokench(lex, ch))
813 return (lex->tok.ttype = TOKEN_FATAL);
814 /* peak ahead once */
815 nextch = lex_getch(lex);
817 lex_ungetch(lex, nextch);
818 if (!lex_endtoken(lex))
819 return (lex->tok.ttype = TOKEN_FATAL);
820 return (lex->tok.ttype = ch);
822 /* peak ahead again */
823 nextch = lex_getch(lex);
825 lex_ungetch(lex, nextch);
826 lex_ungetch(lex, nextch);
827 if (!lex_endtoken(lex))
828 return (lex->tok.ttype = TOKEN_FATAL);
829 return (lex->tok.ttype = ch);
831 /* fill the token to be "..." */
832 if (!lex_tokench(lex, ch) ||
833 !lex_tokench(lex, ch) ||
836 return (lex->tok.ttype = TOKEN_FATAL);
838 return (lex->tok.ttype = TOKEN_DOTS);
842 if (ch == ',' || ch == '.') {
843 if (!lex_tokench(lex, ch) ||
846 return (lex->tok.ttype = TOKEN_FATAL);
848 return (lex->tok.ttype = TOKEN_OPERATOR);
851 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
852 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
853 ch == '=' || ch == '!' || /* ==, != */
854 ch == '&' || ch == '|') /* &&, ||, &=, |= */
856 if (!lex_tokench(lex, ch))
857 return (lex->tok.ttype = TOKEN_FATAL);
859 nextch = lex_getch(lex);
860 if (nextch == ch || nextch == '=') {
861 if (!lex_tokench(lex, nextch))
862 return (lex->tok.ttype = TOKEN_FATAL);
863 } else if (ch == '-' && nextch == '>') {
864 if (!lex_tokench(lex, nextch))
865 return (lex->tok.ttype = TOKEN_FATAL);
867 lex_ungetch(lex, nextch);
869 if (!lex_endtoken(lex))
870 return (lex->tok.ttype = TOKEN_FATAL);
871 return (lex->tok.ttype = TOKEN_OPERATOR);
875 if (ch == '^' || ch == '~' || ch == '!')
877 if (!lex_tokench(lex, ch) ||
880 return (lex->tok.ttype = TOKEN_FATAL);
882 return (lex->tok.ttype = TOKEN_OPERATOR);
886 if (ch == '*' || ch == '/') /* *=, /= */
888 if (!lex_tokench(lex, ch))
889 return (lex->tok.ttype = TOKEN_FATAL);
891 nextch = lex_getch(lex);
893 if (!lex_tokench(lex, nextch))
894 return (lex->tok.ttype = TOKEN_FATAL);
896 lex_ungetch(lex, nextch);
898 if (!lex_endtoken(lex))
899 return (lex->tok.ttype = TOKEN_FATAL);
900 return (lex->tok.ttype = TOKEN_OPERATOR);
903 if (isident_start(ch))
907 if (!lex_tokench(lex, ch))
908 return (lex->tok.ttype = TOKEN_FATAL);
909 if (!lex_finish_ident(lex)) {
911 return (lex->tok.ttype = TOKEN_ERROR);
913 if (!lex_endtoken(lex))
914 return (lex->tok.ttype = TOKEN_FATAL);
915 lex->tok.ttype = TOKEN_IDENT;
918 if (!strcmp(v, "void")) {
919 lex->tok.ttype = TOKEN_TYPENAME;
920 lex->tok.constval.t = TYPE_VOID;
921 } else if (!strcmp(v, "int")) {
922 lex->tok.ttype = TOKEN_TYPENAME;
923 lex->tok.constval.t = TYPE_INTEGER;
924 } else if (!strcmp(v, "float")) {
925 lex->tok.ttype = TOKEN_TYPENAME;
926 lex->tok.constval.t = TYPE_FLOAT;
927 } else if (!strcmp(v, "string")) {
928 lex->tok.ttype = TOKEN_TYPENAME;
929 lex->tok.constval.t = TYPE_STRING;
930 } else if (!strcmp(v, "entity")) {
931 lex->tok.ttype = TOKEN_TYPENAME;
932 lex->tok.constval.t = TYPE_ENTITY;
933 } else if (!strcmp(v, "vector")) {
934 lex->tok.ttype = TOKEN_TYPENAME;
935 lex->tok.constval.t = TYPE_VECTOR;
936 } else if (!strcmp(v, "for") ||
937 !strcmp(v, "while") ||
940 !strcmp(v, "else") ||
941 !strcmp(v, "local") ||
942 !strcmp(v, "return") ||
944 lex->tok.ttype = TOKEN_KEYWORD;
946 return lex->tok.ttype;
951 lex->tok.ttype = lex_finish_string(lex, '"');
952 while (lex->tok.ttype == TOKEN_STRINGCONST)
954 /* Allow c style "string" "continuation" */
955 ch = lex_skipwhite(lex);
957 lex_ungetch(lex, ch);
961 lex->tok.ttype = lex_finish_string(lex, '"');
963 if (!lex_endtoken(lex))
964 return (lex->tok.ttype = TOKEN_FATAL);
965 return lex->tok.ttype;
970 /* we parse character constants like string,
971 * but return TOKEN_CHARCONST, or a vector type if it fits...
972 * Likewise actual unescaping has to be done by the parser.
973 * The difference is we don't allow 'char' 'continuation'.
975 lex->tok.ttype = lex_finish_string(lex, '\'');
976 if (!lex_endtoken(lex))
977 return (lex->tok.ttype = TOKEN_FATAL);
979 /* It's a vector if we can successfully scan 3 floats */
981 if (sscanf_s(lex->tok.value, " %f %f %f ",
982 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
984 if (sscanf(lex->tok.value, " %f %f %f ",
985 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
988 lex->tok.ttype = TOKEN_VECTORCONST;
991 return lex->tok.ttype;
996 lex->tok.ttype = lex_finish_digit(lex, ch);
997 if (!lex_endtoken(lex))
998 return (lex->tok.ttype = TOKEN_FATAL);
999 return lex->tok.ttype;
1002 lexerror(lex, "unknown token");
1003 return (lex->tok.ttype = TOKEN_ERROR);