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);
43 token *tok = (token*)mem_a(sizeof(token));
46 memset(tok, 0, sizeof(*tok));
50 void token_delete(token *self)
52 if (self->next && self->next->prev == self)
53 self->next->prev = self->prev;
54 if (self->prev && self->prev->next == self)
55 self->prev->next = self->next;
56 MEM_VECTOR_CLEAR(self, value);
60 token* token_copy(const token *cp)
62 token* self = token_new();
66 self->value_alloc = cp->value_count + 1;
67 self->value_count = cp->value_count;
68 self->value = (char*)mem_a(self->value_alloc);
73 memcpy(self->value, cp->value, cp->value_count);
74 self->value[self->value_alloc-1] = 0;
78 self->ttype = cp->ttype;
79 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
83 void token_delete_all(token *t)
94 token* token_copy_all(const token *cp)
99 out = cur = token_copy(cp);
105 cur->next = token_copy(cp);
107 token_delete_all(out);
110 cur->next->prev = cur;
117 lex_file* lex_open(const char *file)
120 FILE *in = util_fopen(file, "rb");
123 lexerror(NULL, "open failed: '%s'\n", file);
127 lex = (lex_file*)mem_a(sizeof(*lex));
130 lexerror(NULL, "out of memory\n");
134 memset(lex, 0, sizeof(*lex));
137 lex->name = util_strdup(file);
138 lex->line = 1; /* we start counting at 1 */
143 lex_filenames_add(lex->name);
148 void lex_cleanup(void)
151 for (i = 0; i < lex_filenames_elements; ++i)
152 mem_d(lex_filenames_data[i]);
153 mem_d(lex_filenames_data);
156 void lex_close(lex_file *lex)
159 for (i = 0; i < lex->frames_count; ++i)
160 mem_d(lex->frames[i].name);
161 MEM_VECTOR_CLEAR(lex, frames);
164 mem_d(lex->modelname);
169 token_delete(lex->tok);
170 /* mem_d(lex->name); collected in lex_filenames */
174 /* Get or put-back data
175 * The following to functions do NOT understand what kind of data they
177 * The are merely wrapping get/put in order to count line numbers.
179 static int lex_getch(lex_file *lex)
185 if (lex->peek[lex->peekpos] == '\n')
187 return lex->peek[lex->peekpos];
190 ch = fgetc(lex->file);
196 static void lex_ungetch(lex_file *lex, int ch)
198 lex->peek[lex->peekpos++] = ch;
203 /* classify characters
204 * some additions to the is*() functions of ctype.h
207 /* Idents are alphanumberic, but they start with alpha or _ */
208 static bool isident_start(int ch)
210 return isalpha(ch) || ch == '_';
213 static bool isident(int ch)
215 return isident_start(ch) || isdigit(ch);
218 /* isxdigit_only is used when we already know it's not a digit
219 * and want to see if it's a hex digit anyway.
221 static bool isxdigit_only(int ch)
223 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
226 /* Skip whitespace and comments and return the first
227 * non-white character.
228 * As this makes use of the above getch() ungetch() functions,
229 * we don't need to care at all about line numbering anymore.
231 * In theory, this function should only be used at the beginning
232 * of lexing, or when we *know* the next character is part of the token.
233 * Otherwise, if the parser throws an error, the linenumber may not be
234 * the line of the error, but the line of the next token AFTER the error.
236 * This is currently only problematic when using c-like string-continuation,
237 * since comments and whitespaces are allowed between 2 such strings.
241 "A continuation of the previous string"
242 // This line is skipped
245 * In this case, if the parse decides it didn't actually want a string,
246 * and uses lex->line to print an error, it will show the ', foo);' line's
249 * On the other hand, the parser is supposed to remember the line of the next
250 * token's beginning. In this case we would want skipwhite() to be called
251 * AFTER reading a token, so that the parser, before reading the NEXT token,
252 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
255 * here is to store the line of the first character after skipping
256 * the initial whitespace in lex->sline, this happens in lex_do.
258 static int lex_skipwhite(lex_file *lex)
265 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
271 /* one line comment */
274 /* check for special: '/', '/', '*', '/' */
283 while (ch != EOF && ch != '\n') {
290 /* multiline comment */
302 if (ch == '/') /* allow *//* direct following comment */
304 lex_ungetch(lex, ch);
305 ch = ' '; /* cause TRUE in the isspace check */
309 /* Otherwise roll back to the slash and break out of the loop */
310 lex_ungetch(lex, ch);
314 } while (ch != EOF && isspace(ch));
319 /* Append a character to the token buffer */
320 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
322 if (!token_value_add(lex->tok, ch)) {
323 lexerror(lex, "out of memory");
329 /* Append a trailing null-byte */
330 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
332 if (!token_value_add(lex->tok, 0)) {
333 lexerror(lex, "out of memory");
336 lex->tok->value_count--;
341 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
346 while (ch != EOF && isident(ch))
348 if (!lex_tokench(lex, ch))
349 return (lex->tok->ttype = TOKEN_FATAL);
353 /* last ch was not an ident ch: */
354 lex_ungetch(lex, ch);
359 /* read one ident for the frame list */
360 static int lex_parse_frame(lex_file *lex)
365 token_delete(lex->tok);
366 lex->tok = token_new();
369 while (ch != EOF && ch != '\n' && isspace(ch))
375 if (!isident_start(ch)) {
376 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
380 if (!lex_tokench(lex, ch))
382 if (!lex_finish_ident(lex))
384 if (!lex_endtoken(lex))
389 /* read a list of $frames */
390 static bool lex_finish_frames(lex_file *lex)
397 rc = lex_parse_frame(lex);
398 if (rc > 0) /* end of line */
400 if (rc < 0) /* error */
403 for (i = 0; i < lex->frames_count; ++i) {
404 if (!strcmp(lex->tok->value, lex->frames[i].name)) {
405 lex->frames[i].value = lex->framevalue++;
406 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok->value))
411 if (i < lex->frames_count)
414 m.value = lex->framevalue++;
415 m.name = lex->tok->value;
416 lex->tok->value = NULL;
417 if (!lex_file_frames_add(lex, m)) {
418 lexerror(lex, "out of memory");
424 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
432 return TOKEN_STRINGCONST;
437 lexerror(lex, "unexpected end of file");
438 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
439 return (lex->tok->ttype = TOKEN_ERROR);
444 case 'a': ch = '\a'; break;
445 case 'b': ch = '\b'; break;
446 case 'r': ch = '\r'; break;
447 case 'n': ch = '\n'; break;
448 case 't': ch = '\t'; break;
449 case 'f': ch = '\f'; break;
450 case 'v': ch = '\v'; break;
452 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
453 /* so we just add the character plus backslash no matter what it actually is */
454 if (!lex_tokench(lex, '\\'))
455 return (lex->tok->ttype = TOKEN_FATAL);
457 /* add the character finally */
458 if (!lex_tokench(lex, ch))
459 return (lex->tok->ttype = TOKEN_FATAL);
461 else if (!lex_tokench(lex, ch))
462 return (lex->tok->ttype = TOKEN_FATAL);
464 lexerror(lex, "unexpected end of file within string constant");
465 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
466 return (lex->tok->ttype = TOKEN_ERROR);
469 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
475 /* parse a number... */
476 lex->tok->ttype = TOKEN_INTCONST;
478 if (!lex_tokench(lex, ch))
479 return (lex->tok->ttype = TOKEN_FATAL);
482 if (ch != '.' && !isdigit(ch))
484 if (lastch != '0' || ch != 'x')
486 /* end of the number or EOF */
487 lex_ungetch(lex, ch);
488 if (!lex_endtoken(lex))
489 return (lex->tok->ttype = TOKEN_FATAL);
491 lex->tok->constval.i = lastch - '0';
492 return lex->tok->ttype;
498 /* EOF would have been caught above */
502 if (!lex_tokench(lex, ch))
503 return (lex->tok->ttype = TOKEN_FATAL);
505 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
507 if (!lex_tokench(lex, ch))
508 return (lex->tok->ttype = TOKEN_FATAL);
512 /* NOT else, '.' can come from above as well */
513 if (ch == '.' && !ishex)
515 /* Allow floating comma in non-hex mode */
516 lex->tok->ttype = TOKEN_FLOATCONST;
517 if (!lex_tokench(lex, ch))
518 return (lex->tok->ttype = TOKEN_FATAL);
520 /* continue digits-only */
524 if (!lex_tokench(lex, ch))
525 return (lex->tok->ttype = TOKEN_FATAL);
529 /* put back the last character */
530 /* but do not put back the trailing 'f' or a float */
531 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
534 /* generally we don't want words to follow numbers: */
536 lexerror(lex, "unexpected trailing characters after number");
537 return (lex->tok->ttype = TOKEN_ERROR);
539 lex_ungetch(lex, ch);
541 if (!lex_endtoken(lex))
542 return (lex->tok->ttype = TOKEN_FATAL);
543 if (lex->tok->ttype == TOKEN_FLOATCONST)
544 lex->tok->constval.f = strtod(lex->tok->value, NULL);
546 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
547 return lex->tok->ttype;
550 int lex_do(lex_file *lex)
555 token_delete(lex->tok);
556 lex->tok = token_new();
560 ch = lex_skipwhite(lex);
561 lex->sline = lex->line;
562 lex->tok->ctx.line = lex->sline;
563 lex->tok->ctx.file = lex->name;
566 return (lex->tok->ttype = TOKEN_FATAL);
570 return (lex->tok->ttype = TOKEN_EOF);
573 /* modelgen / spiritgen commands */
579 if (!isident_start(ch)) {
580 lexerror(lex, "hanging '$' modelgen/spritegen command line");
583 if (!lex_tokench(lex, ch))
584 return (lex->tok->ttype = TOKEN_FATAL);
585 if (!lex_finish_ident(lex))
586 return (lex->tok->ttype = TOKEN_ERROR);
587 if (!lex_endtoken(lex))
588 return (lex->tok->ttype = TOKEN_FATAL);
589 /* skip the known commands */
592 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
594 /* frame/framesave command works like an enum
595 * similar to fteqcc we handle this in the lexer.
596 * The reason for this is that it is sensitive to newlines,
597 * which the parser is unaware of
599 if (!lex_finish_frames(lex))
600 return (lex->tok->ttype = TOKEN_ERROR);
604 if (!strcmp(v, "framevalue"))
607 while (ch != EOF && isspace(ch) && ch != '\n')
611 lexerror(lex, "$framevalue requires an integer parameter");
615 token_delete(lex->tok);
616 lex->tok = token_new();
617 lex->tok->ttype = lex_finish_digit(lex, ch);
618 if (!lex_endtoken(lex))
619 return (lex->tok->ttype = TOKEN_FATAL);
620 if (lex->tok->ttype != TOKEN_INTCONST) {
621 lexerror(lex, "$framevalue requires an integer parameter");
624 lex->framevalue = lex->tok->constval.i;
628 if (!strcmp(v, "framerestore"))
632 token_delete(lex->tok);
633 lex->tok = token_new();
635 rc = lex_parse_frame(lex);
638 lexerror(lex, "$framerestore requires a framename parameter");
642 return (lex->tok->ttype = TOKEN_FATAL);
645 for (frame = 0; frame < lex->frames_count; ++frame) {
646 if (!strcmp(v, lex->frames[frame].name)) {
647 lex->framevalue = lex->frames[frame].value;
651 lexerror(lex, "unknown framename `%s`", v);
655 if (!strcmp(v, "modelname"))
659 token_delete(lex->tok);
660 lex->tok = token_new();
662 rc = lex_parse_frame(lex);
665 lexerror(lex, "$framerestore requires a framename parameter");
669 return (lex->tok->ttype = TOKEN_FATAL);
672 if (lex->modelname) {
674 m.value = lex->framevalue;
675 m.name = lex->modelname;
676 lex->modelname = NULL;
677 if (!lex_file_frames_add(lex, m)) {
678 lexerror(lex, "out of memory");
679 return (lex->tok->ttype = TOKEN_FATAL);
682 lex->modelname = lex->tok->value;
683 lex->tok->value = NULL;
684 for (frame = 0; frame < lex->frames_count; ++frame) {
685 if (!strcmp(v, lex->frames[frame].name)) {
686 lex->framevalue = lex->frames[frame].value;
693 if (!strcmp(v, "flush"))
696 for (frame = 0; frame < lex->frames_count; ++frame)
697 mem_d(lex->frames[frame].name);
698 MEM_VECTOR_CLEAR(lex, frames);
699 /* skip line (fteqcc does it too) */
701 while (ch != EOF && ch != '\n')
706 if (!strcmp(v, "cd") ||
707 !strcmp(v, "origin") ||
708 !strcmp(v, "base") ||
709 !strcmp(v, "flags") ||
710 !strcmp(v, "scale") ||
715 while (ch != EOF && ch != '\n')
720 for (frame = 0; frame < lex->frames_count; ++frame) {
721 if (!strcmp(v, lex->frames[frame].name)) {
722 lex->tok->constval.i = lex->frames[frame].value;
723 return (lex->tok->ttype = TOKEN_INTCONST);
727 lexerror(lex, "invalid frame macro");
731 /* single-character tokens */
735 if (!lex_tokench(lex, ch) ||
738 return (lex->tok->ttype = TOKEN_FATAL);
740 if (lex->flags.noops)
741 return (lex->tok->ttype = ch);
743 return (lex->tok->ttype = TOKEN_OPERATOR);
752 if (!lex_tokench(lex, ch) ||
755 return (lex->tok->ttype = TOKEN_FATAL);
757 return (lex->tok->ttype = ch);
762 if (lex->flags.noops)
764 /* Detect characters early which are normally
765 * operators OR PART of an operator.
782 if (!lex_tokench(lex, ch) ||
785 return (lex->tok->ttype = TOKEN_FATAL);
787 return (lex->tok->ttype = ch);
794 if (!lex_tokench(lex, ch))
795 return (lex->tok->ttype = TOKEN_FATAL);
796 /* peak ahead once */
797 nextch = lex_getch(lex);
799 lex_ungetch(lex, nextch);
800 if (!lex_endtoken(lex))
801 return (lex->tok->ttype = TOKEN_FATAL);
802 return (lex->tok->ttype = ch);
804 /* peak ahead again */
805 nextch = lex_getch(lex);
807 lex_ungetch(lex, nextch);
808 lex_ungetch(lex, nextch);
809 if (!lex_endtoken(lex))
810 return (lex->tok->ttype = TOKEN_FATAL);
811 return (lex->tok->ttype = ch);
813 /* fill the token to be "..." */
814 if (!lex_tokench(lex, ch) ||
815 !lex_tokench(lex, ch) ||
818 return (lex->tok->ttype = TOKEN_FATAL);
820 return (lex->tok->ttype = TOKEN_DOTS);
824 if (ch == ',' || ch == '.') {
825 if (!lex_tokench(lex, ch) ||
828 return (lex->tok->ttype = TOKEN_FATAL);
830 return (lex->tok->ttype = TOKEN_OPERATOR);
833 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
834 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
835 ch == '=' || ch == '!' || /* ==, != */
836 ch == '&' || ch == '|') /* &&, ||, &=, |= */
838 if (!lex_tokench(lex, ch))
839 return (lex->tok->ttype = TOKEN_FATAL);
841 nextch = lex_getch(lex);
842 if (nextch == ch || nextch == '=') {
843 if (!lex_tokench(lex, nextch))
844 return (lex->tok->ttype = TOKEN_FATAL);
845 } else if (ch == '-' && nextch == '>') {
846 if (!lex_tokench(lex, nextch))
847 return (lex->tok->ttype = TOKEN_FATAL);
849 lex_ungetch(lex, nextch);
851 if (!lex_endtoken(lex))
852 return (lex->tok->ttype = TOKEN_FATAL);
853 return (lex->tok->ttype = TOKEN_OPERATOR);
857 if (ch == '^' || ch == '~' || ch == '!')
859 if (!lex_tokench(lex, ch) ||
862 return (lex->tok->ttype = TOKEN_FATAL);
864 return (lex->tok->ttype = TOKEN_OPERATOR);
868 if (ch == '*' || ch == '/') /* *=, /= */
870 if (!lex_tokench(lex, ch))
871 return (lex->tok->ttype = TOKEN_FATAL);
873 nextch = lex_getch(lex);
875 if (!lex_tokench(lex, nextch))
876 return (lex->tok->ttype = TOKEN_FATAL);
878 lex_ungetch(lex, nextch);
880 if (!lex_endtoken(lex))
881 return (lex->tok->ttype = TOKEN_FATAL);
882 return (lex->tok->ttype = TOKEN_OPERATOR);
885 if (isident_start(ch))
889 if (!lex_tokench(lex, ch))
890 return (lex->tok->ttype = TOKEN_FATAL);
891 if (!lex_finish_ident(lex)) {
893 return (lex->tok->ttype = TOKEN_ERROR);
895 if (!lex_endtoken(lex))
896 return (lex->tok->ttype = TOKEN_FATAL);
897 lex->tok->ttype = TOKEN_IDENT;
900 if (!strcmp(v, "void")) {
901 lex->tok->ttype = TOKEN_TYPENAME;
902 lex->tok->constval.t = TYPE_VOID;
903 } else if (!strcmp(v, "int")) {
904 lex->tok->ttype = TOKEN_TYPENAME;
905 lex->tok->constval.t = TYPE_INTEGER;
906 } else if (!strcmp(v, "float")) {
907 lex->tok->ttype = TOKEN_TYPENAME;
908 lex->tok->constval.t = TYPE_FLOAT;
909 } else if (!strcmp(v, "string")) {
910 lex->tok->ttype = TOKEN_TYPENAME;
911 lex->tok->constval.t = TYPE_STRING;
912 } else if (!strcmp(v, "entity")) {
913 lex->tok->ttype = TOKEN_TYPENAME;
914 lex->tok->constval.t = TYPE_ENTITY;
915 } else if (!strcmp(v, "vector")) {
916 lex->tok->ttype = TOKEN_TYPENAME;
917 lex->tok->constval.t = TYPE_VECTOR;
918 } else if (!strcmp(v, "for") ||
919 !strcmp(v, "while") ||
922 !strcmp(v, "else") ||
923 !strcmp(v, "local") ||
924 !strcmp(v, "return") ||
926 lex->tok->ttype = TOKEN_KEYWORD;
928 return lex->tok->ttype;
933 lex->tok->ttype = lex_finish_string(lex, '"');
934 while (lex->tok->ttype == TOKEN_STRINGCONST)
936 /* Allow c style "string" "continuation" */
937 ch = lex_skipwhite(lex);
939 lex_ungetch(lex, ch);
943 lex->tok->ttype = lex_finish_string(lex, '"');
945 if (!lex_endtoken(lex))
946 return (lex->tok->ttype = TOKEN_FATAL);
947 return lex->tok->ttype;
952 /* we parse character constants like string,
953 * but return TOKEN_CHARCONST, or a vector type if it fits...
954 * Likewise actual unescaping has to be done by the parser.
955 * The difference is we don't allow 'char' 'continuation'.
957 lex->tok->ttype = lex_finish_string(lex, '\'');
958 if (!lex_endtoken(lex))
959 return (lex->tok->ttype = TOKEN_FATAL);
961 /* It's a vector if we can successfully scan 3 floats */
963 if (sscanf_s(lex->tok->value, " %f %f %f ",
964 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
966 if (sscanf(lex->tok->value, " %f %f %f ",
967 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
970 lex->tok->ttype = TOKEN_VECTORCONST;
973 return lex->tok->ttype;
978 lex->tok->ttype = lex_finish_digit(lex, ch);
979 if (!lex_endtoken(lex))
980 return (lex->tok->ttype = TOKEN_FATAL);
981 return lex->tok->ttype;
984 lexerror(lex, "unknown token");
985 return (lex->tok->ttype = TOKEN_ERROR);