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))
412 m.value = lex->framevalue++;
413 m.name = lex->tok->value;
414 lex->tok->value = NULL;
415 if (!lex_file_frames_add(lex, m)) {
416 lexerror(lex, "out of memory");
422 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
430 return TOKEN_STRINGCONST;
435 lexerror(lex, "unexpected end of file");
436 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
437 return (lex->tok->ttype = TOKEN_ERROR);
442 case 'a': ch = '\a'; break;
443 case 'b': ch = '\b'; break;
444 case 'r': ch = '\r'; break;
445 case 'n': ch = '\n'; break;
446 case 't': ch = '\t'; break;
447 case 'f': ch = '\f'; break;
448 case 'v': ch = '\v'; break;
450 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
451 /* so we just add the character plus backslash no matter what it actually is */
452 if (!lex_tokench(lex, '\\'))
453 return (lex->tok->ttype = TOKEN_FATAL);
455 /* add the character finally */
456 if (!lex_tokench(lex, ch))
457 return (lex->tok->ttype = TOKEN_FATAL);
459 else if (!lex_tokench(lex, ch))
460 return (lex->tok->ttype = TOKEN_FATAL);
462 lexerror(lex, "unexpected end of file within string constant");
463 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
464 return (lex->tok->ttype = TOKEN_ERROR);
467 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
473 /* parse a number... */
474 lex->tok->ttype = TOKEN_INTCONST;
476 if (!lex_tokench(lex, ch))
477 return (lex->tok->ttype = TOKEN_FATAL);
480 if (ch != '.' && !isdigit(ch))
482 if (lastch != '0' || ch != 'x')
484 /* end of the number or EOF */
485 lex_ungetch(lex, ch);
486 if (!lex_endtoken(lex))
487 return (lex->tok->ttype = TOKEN_FATAL);
489 lex->tok->constval.i = lastch - '0';
490 return lex->tok->ttype;
496 /* EOF would have been caught above */
500 if (!lex_tokench(lex, ch))
501 return (lex->tok->ttype = TOKEN_FATAL);
503 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
505 if (!lex_tokench(lex, ch))
506 return (lex->tok->ttype = TOKEN_FATAL);
510 /* NOT else, '.' can come from above as well */
511 if (ch == '.' && !ishex)
513 /* Allow floating comma in non-hex mode */
514 lex->tok->ttype = TOKEN_FLOATCONST;
515 if (!lex_tokench(lex, ch))
516 return (lex->tok->ttype = TOKEN_FATAL);
518 /* continue digits-only */
522 if (!lex_tokench(lex, ch))
523 return (lex->tok->ttype = TOKEN_FATAL);
527 /* put back the last character */
528 /* but do not put back the trailing 'f' or a float */
529 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
532 /* generally we don't want words to follow numbers: */
534 lexerror(lex, "unexpected trailing characters after number");
535 return (lex->tok->ttype = TOKEN_ERROR);
537 lex_ungetch(lex, ch);
539 if (!lex_endtoken(lex))
540 return (lex->tok->ttype = TOKEN_FATAL);
541 if (lex->tok->ttype == TOKEN_FLOATCONST)
542 lex->tok->constval.f = strtod(lex->tok->value, NULL);
544 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
545 return lex->tok->ttype;
548 int lex_do(lex_file *lex)
553 token_delete(lex->tok);
554 lex->tok = token_new();
558 ch = lex_skipwhite(lex);
559 lex->sline = lex->line;
560 lex->tok->ctx.line = lex->sline;
561 lex->tok->ctx.file = lex->name;
564 return (lex->tok->ttype = TOKEN_FATAL);
568 return (lex->tok->ttype = TOKEN_EOF);
571 /* modelgen / spiritgen commands */
577 if (!isident_start(ch)) {
578 lexerror(lex, "hanging '$' modelgen/spritegen command line");
581 if (!lex_tokench(lex, ch))
582 return (lex->tok->ttype = TOKEN_FATAL);
583 if (!lex_finish_ident(lex))
584 return (lex->tok->ttype = TOKEN_ERROR);
585 if (!lex_endtoken(lex))
586 return (lex->tok->ttype = TOKEN_FATAL);
587 /* skip the known commands */
590 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
592 /* frame/framesave command works like an enum
593 * similar to fteqcc we handle this in the lexer.
594 * The reason for this is that it is sensitive to newlines,
595 * which the parser is unaware of
597 if (!lex_finish_frames(lex))
598 return (lex->tok->ttype = TOKEN_ERROR);
602 if (!strcmp(v, "framevalue"))
605 while (ch != EOF && isspace(ch) && ch != '\n')
609 lexerror(lex, "$framevalue requires an integer parameter");
613 token_delete(lex->tok);
614 lex->tok = token_new();
615 lex->tok->ttype = lex_finish_digit(lex, ch);
616 if (!lex_endtoken(lex))
617 return (lex->tok->ttype = TOKEN_FATAL);
618 if (lex->tok->ttype != TOKEN_INTCONST) {
619 lexerror(lex, "$framevalue requires an integer parameter");
622 lex->framevalue = lex->tok->constval.i;
626 if (!strcmp(v, "framerestore"))
630 token_delete(lex->tok);
631 lex->tok = token_new();
633 rc = lex_parse_frame(lex);
636 lexerror(lex, "$framerestore requires a framename parameter");
640 return (lex->tok->ttype = TOKEN_FATAL);
643 for (frame = 0; frame < lex->frames_count; ++frame) {
644 if (!strcmp(v, lex->frames[frame].name)) {
645 lex->framevalue = lex->frames[frame].value;
649 lexerror(lex, "unknown framename `%s`", v);
653 if (!strcmp(v, "modelname"))
657 token_delete(lex->tok);
658 lex->tok = token_new();
660 rc = lex_parse_frame(lex);
663 lexerror(lex, "$framerestore requires a framename parameter");
667 return (lex->tok->ttype = TOKEN_FATAL);
670 if (lex->modelname) {
672 m.value = lex->framevalue;
673 m.name = lex->modelname;
674 lex->modelname = NULL;
675 if (!lex_file_frames_add(lex, m)) {
676 lexerror(lex, "out of memory");
677 return (lex->tok->ttype = TOKEN_FATAL);
680 lex->modelname = lex->tok->value;
681 lex->tok->value = NULL;
682 for (frame = 0; frame < lex->frames_count; ++frame) {
683 if (!strcmp(v, lex->frames[frame].name)) {
684 lex->framevalue = lex->frames[frame].value;
691 if (!strcmp(v, "flush"))
694 for (frame = 0; frame < lex->frames_count; ++frame)
695 mem_d(lex->frames[frame].name);
696 MEM_VECTOR_CLEAR(lex, frames);
697 /* skip line (fteqcc does it too) */
699 while (ch != EOF && ch != '\n')
704 if (!strcmp(v, "cd") ||
705 !strcmp(v, "origin") ||
706 !strcmp(v, "base") ||
707 !strcmp(v, "flags") ||
708 !strcmp(v, "scale") ||
713 while (ch != EOF && ch != '\n')
718 for (frame = 0; frame < lex->frames_count; ++frame) {
719 if (!strcmp(v, lex->frames[frame].name)) {
720 lex->tok->constval.i = lex->frames[frame].value;
721 return (lex->tok->ttype = TOKEN_INTCONST);
725 lexerror(lex, "invalid frame macro");
729 /* single-character tokens */
733 if (!lex_tokench(lex, ch) ||
736 return (lex->tok->ttype = TOKEN_FATAL);
738 if (lex->flags.noops)
739 return (lex->tok->ttype = ch);
741 return (lex->tok->ttype = TOKEN_OPERATOR);
750 if (!lex_tokench(lex, ch) ||
753 return (lex->tok->ttype = TOKEN_FATAL);
755 return (lex->tok->ttype = ch);
760 if (lex->flags.noops)
762 /* Detect characters early which are normally
763 * operators OR PART of an operator.
780 if (!lex_tokench(lex, ch) ||
783 return (lex->tok->ttype = TOKEN_FATAL);
785 return (lex->tok->ttype = ch);
792 if (!lex_tokench(lex, ch))
793 return (lex->tok->ttype = TOKEN_FATAL);
794 /* peak ahead once */
795 nextch = lex_getch(lex);
797 lex_ungetch(lex, nextch);
798 if (!lex_endtoken(lex))
799 return (lex->tok->ttype = TOKEN_FATAL);
800 return (lex->tok->ttype = ch);
802 /* peak ahead again */
803 nextch = lex_getch(lex);
805 lex_ungetch(lex, nextch);
806 lex_ungetch(lex, nextch);
807 if (!lex_endtoken(lex))
808 return (lex->tok->ttype = TOKEN_FATAL);
809 return (lex->tok->ttype = ch);
811 /* fill the token to be "..." */
812 if (!lex_tokench(lex, ch) ||
813 !lex_tokench(lex, ch) ||
816 return (lex->tok->ttype = TOKEN_FATAL);
818 return (lex->tok->ttype = TOKEN_DOTS);
822 if (ch == ',' || ch == '.') {
823 if (!lex_tokench(lex, ch) ||
826 return (lex->tok->ttype = TOKEN_FATAL);
828 return (lex->tok->ttype = TOKEN_OPERATOR);
831 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
832 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
833 ch == '=' || ch == '!' || /* ==, != */
834 ch == '&' || ch == '|') /* &&, ||, &=, |= */
836 if (!lex_tokench(lex, ch))
837 return (lex->tok->ttype = TOKEN_FATAL);
839 nextch = lex_getch(lex);
840 if (nextch == ch || nextch == '=') {
841 if (!lex_tokench(lex, nextch))
842 return (lex->tok->ttype = TOKEN_FATAL);
843 } else if (ch == '-' && nextch == '>') {
844 if (!lex_tokench(lex, nextch))
845 return (lex->tok->ttype = TOKEN_FATAL);
847 lex_ungetch(lex, nextch);
849 if (!lex_endtoken(lex))
850 return (lex->tok->ttype = TOKEN_FATAL);
851 return (lex->tok->ttype = TOKEN_OPERATOR);
855 if (ch == '^' || ch == '~' || ch == '!')
857 if (!lex_tokench(lex, ch) ||
860 return (lex->tok->ttype = TOKEN_FATAL);
862 return (lex->tok->ttype = TOKEN_OPERATOR);
866 if (ch == '*' || ch == '/') /* *=, /= */
868 if (!lex_tokench(lex, ch))
869 return (lex->tok->ttype = TOKEN_FATAL);
871 nextch = lex_getch(lex);
873 if (!lex_tokench(lex, nextch))
874 return (lex->tok->ttype = TOKEN_FATAL);
876 lex_ungetch(lex, nextch);
878 if (!lex_endtoken(lex))
879 return (lex->tok->ttype = TOKEN_FATAL);
880 return (lex->tok->ttype = TOKEN_OPERATOR);
883 if (isident_start(ch))
887 if (!lex_tokench(lex, ch))
888 return (lex->tok->ttype = TOKEN_FATAL);
889 if (!lex_finish_ident(lex)) {
891 return (lex->tok->ttype = TOKEN_ERROR);
893 if (!lex_endtoken(lex))
894 return (lex->tok->ttype = TOKEN_FATAL);
895 lex->tok->ttype = TOKEN_IDENT;
898 if (!strcmp(v, "void")) {
899 lex->tok->ttype = TOKEN_TYPENAME;
900 lex->tok->constval.t = TYPE_VOID;
901 } else if (!strcmp(v, "int")) {
902 lex->tok->ttype = TOKEN_TYPENAME;
903 lex->tok->constval.t = TYPE_INTEGER;
904 } else if (!strcmp(v, "float")) {
905 lex->tok->ttype = TOKEN_TYPENAME;
906 lex->tok->constval.t = TYPE_FLOAT;
907 } else if (!strcmp(v, "string")) {
908 lex->tok->ttype = TOKEN_TYPENAME;
909 lex->tok->constval.t = TYPE_STRING;
910 } else if (!strcmp(v, "entity")) {
911 lex->tok->ttype = TOKEN_TYPENAME;
912 lex->tok->constval.t = TYPE_ENTITY;
913 } else if (!strcmp(v, "vector")) {
914 lex->tok->ttype = TOKEN_TYPENAME;
915 lex->tok->constval.t = TYPE_VECTOR;
916 } else if (!strcmp(v, "for") ||
917 !strcmp(v, "while") ||
920 !strcmp(v, "else") ||
921 !strcmp(v, "local") ||
922 !strcmp(v, "return") ||
924 lex->tok->ttype = TOKEN_KEYWORD;
926 return lex->tok->ttype;
931 lex->tok->ttype = lex_finish_string(lex, '"');
932 while (lex->tok->ttype == TOKEN_STRINGCONST)
934 /* Allow c style "string" "continuation" */
935 ch = lex_skipwhite(lex);
937 lex_ungetch(lex, ch);
941 lex->tok->ttype = lex_finish_string(lex, '"');
943 if (!lex_endtoken(lex))
944 return (lex->tok->ttype = TOKEN_FATAL);
945 return lex->tok->ttype;
950 /* we parse character constants like string,
951 * but return TOKEN_CHARCONST, or a vector type if it fits...
952 * Likewise actual unescaping has to be done by the parser.
953 * The difference is we don't allow 'char' 'continuation'.
955 lex->tok->ttype = lex_finish_string(lex, '\'');
956 if (!lex_endtoken(lex))
957 return (lex->tok->ttype = TOKEN_FATAL);
959 /* It's a vector if we can successfully scan 3 floats */
961 if (sscanf_s(lex->tok->value, " %f %f %f ",
962 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
964 if (sscanf(lex->tok->value, " %f %f %f ",
965 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
968 lex->tok->ttype = TOKEN_VECTORCONST;
971 return lex->tok->ttype;
976 lex->tok->ttype = lex_finish_digit(lex, ch);
977 if (!lex_endtoken(lex))
978 return (lex->tok->ttype = TOKEN_FATAL);
979 return lex->tok->ttype;
982 lexerror(lex, "unknown token");
983 return (lex->tok->ttype = TOKEN_ERROR);