9 MEM_VEC_FUNCTIONS(token, char, value)
10 MEM_VEC_FUNCTIONS(lex_file, frame_macro, frames)
12 void lexerror(lex_file *lex, const char *fmt, ...)
17 printf("error %s:%lu: ", lex->name, (unsigned long)lex->sline);
28 void lexwarn(lex_file *lex, int warn, const char *fmt, ...)
36 printf("warning %s:%lu: ", lex->name, (unsigned long)lex->sline);
49 token *tok = (token*)mem_a(sizeof(token));
52 memset(tok, 0, sizeof(*tok));
56 void token_delete(token *self)
58 if (self->next && self->next->prev == self)
59 self->next->prev = self->prev;
60 if (self->prev && self->prev->next == self)
61 self->prev->next = self->next;
62 MEM_VECTOR_CLEAR(self, value);
66 token* token_copy(const token *cp)
68 token* self = token_new();
72 self->value_alloc = cp->value_count + 1;
73 self->value_count = cp->value_count;
74 self->value = (char*)mem_a(self->value_alloc);
79 memcpy(self->value, cp->value, cp->value_count);
80 self->value[self->value_alloc-1] = 0;
84 self->ttype = cp->ttype;
85 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
89 void token_delete_all(token *t)
100 token* token_copy_all(const token *cp)
105 out = cur = token_copy(cp);
111 cur->next = token_copy(cp);
113 token_delete_all(out);
116 cur->next->prev = cur;
123 lex_file* lex_open(const char *file)
126 FILE *in = util_fopen(file, "rb");
129 lexerror(NULL, "open failed: '%s'\n", file);
133 lex = (lex_file*)mem_a(sizeof(*lex));
136 lexerror(NULL, "out of memory\n");
140 memset(lex, 0, sizeof(*lex));
143 lex->name = util_strdup(file);
144 lex->line = 1; /* we start counting at 1 */
151 void lex_close(lex_file *lex)
154 for (i = 0; i < lex->frames_count; ++i)
155 mem_d(lex->frames[i].name);
156 MEM_VECTOR_CLEAR(lex, frames);
161 token_delete(lex->tok);
166 /* Get or put-back data
167 * The following to functions do NOT understand what kind of data they
169 * The are merely wrapping get/put in order to count line numbers.
171 static int lex_getch(lex_file *lex)
177 if (lex->peek[lex->peekpos] == '\n')
179 return lex->peek[lex->peekpos];
182 ch = fgetc(lex->file);
188 static void lex_ungetch(lex_file *lex, int ch)
190 lex->peek[lex->peekpos++] = ch;
195 /* classify characters
196 * some additions to the is*() functions of ctype.h
199 /* Idents are alphanumberic, but they start with alpha or _ */
200 static bool isident_start(int ch)
202 return isalpha(ch) || ch == '_';
205 static bool isident(int ch)
207 return isident_start(ch) || isdigit(ch);
210 /* isxdigit_only is used when we already know it's not a digit
211 * and want to see if it's a hex digit anyway.
213 static bool isxdigit_only(int ch)
215 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
218 /* Skip whitespace and comments and return the first
219 * non-white character.
220 * As this makes use of the above getch() ungetch() functions,
221 * we don't need to care at all about line numbering anymore.
223 * In theory, this function should only be used at the beginning
224 * of lexing, or when we *know* the next character is part of the token.
225 * Otherwise, if the parser throws an error, the linenumber may not be
226 * the line of the error, but the line of the next token AFTER the error.
228 * This is currently only problematic when using c-like string-continuation,
229 * since comments and whitespaces are allowed between 2 such strings.
233 "A continuation of the previous string"
234 // This line is skipped
237 * In this case, if the parse decides it didn't actually want a string,
238 * and uses lex->line to print an error, it will show the ', foo);' line's
241 * On the other hand, the parser is supposed to remember the line of the next
242 * token's beginning. In this case we would want skipwhite() to be called
243 * AFTER reading a token, so that the parser, before reading the NEXT token,
244 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
247 * here is to store the line of the first character after skipping
248 * the initial whitespace in lex->sline, this happens in lex_do.
250 static int lex_skipwhite(lex_file *lex)
257 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
263 /* one line comment */
266 /* check for special: '/', '/', '*', '/' */
275 while (ch != EOF && ch != '\n') {
282 /* multiline comment */
294 if (ch == '/') /* allow *//* direct following comment */
296 lex_ungetch(lex, ch);
297 ch = ' '; /* cause TRUE in the isspace check */
301 /* Otherwise roll back to the slash and break out of the loop */
302 lex_ungetch(lex, ch);
306 } while (ch != EOF && isspace(ch));
311 /* Append a character to the token buffer */
312 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
314 if (!token_value_add(lex->tok, ch)) {
315 lexerror(lex, "out of memory");
321 /* Append a trailing null-byte */
322 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
324 if (!token_value_add(lex->tok, 0)) {
325 lexerror(lex, "out of memory");
328 lex->tok->value_count--;
333 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
338 while (ch != EOF && isident(ch))
340 if (!lex_tokench(lex, ch))
341 return (lex->tok->ttype = TOKEN_FATAL);
345 /* last ch was not an ident ch: */
346 lex_ungetch(lex, ch);
351 /* read one ident for the frame list */
352 static int lex_parse_frame(lex_file *lex)
357 token_delete(lex->tok);
358 lex->tok = token_new();
361 while (ch != EOF && ch != '\n' && isspace(ch))
367 if (!isident_start(ch)) {
368 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
372 if (!lex_tokench(lex, ch))
374 if (!lex_finish_ident(lex))
376 if (!lex_endtoken(lex))
381 /* read a list of $frames */
382 static bool lex_finish_frames(lex_file *lex)
388 rc = lex_parse_frame(lex);
389 if (rc > 0) /* end of line */
391 if (rc < 0) /* error */
394 m.value = lex->framevalue++;
395 m.name = lex->tok->value;
396 lex->tok->value = NULL;
397 if (!lex_file_frames_add(lex, m)) {
398 lexerror(lex, "out of memory");
404 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
412 return TOKEN_STRINGCONST;
417 lexerror(lex, "unexpected end of file");
418 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
419 return (lex->tok->ttype = TOKEN_ERROR);
424 case 'a': ch = '\a'; break;
425 case 'b': ch = '\b'; break;
426 case 'r': ch = '\r'; break;
427 case 'n': ch = '\n'; break;
428 case 't': ch = '\t'; break;
429 case 'f': ch = '\f'; break;
430 case 'v': ch = '\v'; break;
432 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
433 /* so we just add the character plus backslash no matter what it actually is */
434 if (!lex_tokench(lex, '\\'))
435 return (lex->tok->ttype = TOKEN_FATAL);
437 /* add the character finally */
438 if (!lex_tokench(lex, ch))
439 return (lex->tok->ttype = TOKEN_FATAL);
441 else if (!lex_tokench(lex, ch))
442 return (lex->tok->ttype = TOKEN_FATAL);
444 lexerror(lex, "unexpected end of file within string constant");
445 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
446 return (lex->tok->ttype = TOKEN_ERROR);
449 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
455 /* parse a number... */
456 lex->tok->ttype = TOKEN_INTCONST;
458 if (!lex_tokench(lex, ch))
459 return (lex->tok->ttype = TOKEN_FATAL);
462 if (ch != '.' && !isdigit(ch))
464 if (lastch != '0' || ch != 'x')
466 /* end of the number or EOF */
467 lex_ungetch(lex, ch);
468 if (!lex_endtoken(lex))
469 return (lex->tok->ttype = TOKEN_FATAL);
471 lex->tok->constval.i = lastch - '0';
472 return lex->tok->ttype;
478 /* EOF would have been caught above */
482 if (!lex_tokench(lex, ch))
483 return (lex->tok->ttype = TOKEN_FATAL);
485 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
487 if (!lex_tokench(lex, ch))
488 return (lex->tok->ttype = TOKEN_FATAL);
492 /* NOT else, '.' can come from above as well */
493 if (ch == '.' && !ishex)
495 /* Allow floating comma in non-hex mode */
496 lex->tok->ttype = TOKEN_FLOATCONST;
497 if (!lex_tokench(lex, ch))
498 return (lex->tok->ttype = TOKEN_FATAL);
500 /* continue digits-only */
504 if (!lex_tokench(lex, ch))
505 return (lex->tok->ttype = TOKEN_FATAL);
509 /* put back the last character */
510 /* but do not put back the trailing 'f' or a float */
511 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
514 /* generally we don't want words to follow numbers: */
516 lexerror(lex, "unexpected trailing characters after number");
517 return (lex->tok->ttype = TOKEN_ERROR);
519 lex_ungetch(lex, ch);
521 if (!lex_endtoken(lex))
522 return (lex->tok->ttype = TOKEN_FATAL);
523 if (lex->tok->ttype == TOKEN_FLOATCONST)
524 lex->tok->constval.f = strtod(lex->tok->value, NULL);
526 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
527 return lex->tok->ttype;
530 int lex_do(lex_file *lex)
535 token_delete(lex->tok);
536 lex->tok = token_new();
540 ch = lex_skipwhite(lex);
541 lex->sline = lex->line;
542 lex->tok->ctx.line = lex->sline;
543 lex->tok->ctx.file = lex->name;
546 return (lex->tok->ttype = TOKEN_EOF);
548 /* modelgen / spiritgen commands */
554 if (!isident_start(ch)) {
555 lexerror(lex, "hanging '$' modelgen/spritegen command line");
558 if (!lex_tokench(lex, ch))
559 return (lex->tok->ttype = TOKEN_FATAL);
560 if (!lex_finish_ident(lex))
561 return (lex->tok->ttype = TOKEN_ERROR);
562 if (!lex_endtoken(lex))
563 return (lex->tok->ttype = TOKEN_FATAL);
564 /* skip the known commands */
567 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
569 /* frame/framesave command works like an enum
570 * similar to fteqcc we handle this in the lexer.
571 * The reason for this is that it is sensitive to newlines,
572 * which the parser is unaware of
574 if (!lex_finish_frames(lex))
575 return (lex->tok->ttype = TOKEN_ERROR);
579 if (!strcmp(v, "framevalue"))
582 while (ch != EOF && isspace(ch) && ch != '\n')
586 lexerror(lex, "$framevalue requires an integer parameter");
590 token_delete(lex->tok);
591 lex->tok = token_new();
592 lex->tok->ttype = lex_finish_digit(lex, ch);
593 if (!lex_endtoken(lex))
594 return (lex->tok->ttype = TOKEN_FATAL);
595 if (lex->tok->ttype != TOKEN_INTCONST) {
596 lexerror(lex, "$framevalue requires an integer parameter");
599 lex->framevalue = lex->tok->constval.i;
603 if (!strcmp(v, "framerestore"))
607 token_delete(lex->tok);
608 lex->tok = token_new();
610 rc = lex_parse_frame(lex);
613 lexerror(lex, "$framerestore requires a framename parameter");
617 return (lex->tok->ttype = TOKEN_FATAL);
620 for (frame = 0; frame < lex->frames_count; ++frame) {
621 if (!strcmp(v, lex->frames[frame].name)) {
622 lex->framevalue = lex->frames[frame].value;
626 lexerror(lex, "unknown framename `%s`", v);
630 if (!strcmp(v, "modelname"))
634 token_delete(lex->tok);
635 lex->tok = token_new();
637 rc = lex_parse_frame(lex);
640 lexerror(lex, "$framerestore requires a framename parameter");
644 return (lex->tok->ttype = TOKEN_FATAL);
647 if (lex->modelname) {
649 m.value = lex->framevalue;
650 m.name = lex->modelname;
651 lex->modelname = NULL;
652 if (!lex_file_frames_add(lex, m)) {
653 lexerror(lex, "out of memory");
654 return (lex->tok->ttype = TOKEN_FATAL);
657 lex->modelname = lex->tok->value;
658 lex->tok->value = NULL;
659 for (frame = 0; frame < lex->frames_count; ++frame) {
660 if (!strcmp(v, lex->frames[frame].name)) {
661 lex->framevalue = lex->frames[frame].value;
668 if (!strcmp(v, "flush"))
671 for (frame = 0; frame < lex->frames_count; ++frame)
672 mem_d(lex->frames[frame].name);
673 MEM_VECTOR_CLEAR(lex, frames);
674 /* skip line (fteqcc does it too) */
676 while (ch != EOF && ch != '\n')
681 if (!strcmp(v, "cd") ||
682 !strcmp(v, "origin") ||
683 !strcmp(v, "base") ||
684 !strcmp(v, "flags") ||
685 !strcmp(v, "scale") ||
690 while (ch != EOF && ch != '\n')
695 for (frame = 0; frame < lex->frames_count; ++frame) {
696 if (!strcmp(v, lex->frames[frame].name)) {
697 lex->tok->constval.i = lex->frames[frame].value;
698 return (lex->tok->ttype = TOKEN_INTCONST);
702 lexerror(lex, "invalid frame macro");
706 /* single-character tokens */
710 if (!lex_tokench(lex, ch) ||
713 return (lex->tok->ttype = TOKEN_FATAL);
715 if (lex->flags.noops)
716 return (lex->tok->ttype = ch);
718 return (lex->tok->ttype = TOKEN_OPERATOR);
727 if (!lex_tokench(lex, ch) ||
730 return (lex->tok->ttype = TOKEN_FATAL);
732 return (lex->tok->ttype = ch);
737 if (lex->flags.noops)
739 /* Detect characters early which are normally
740 * operators OR PART of an operator.
758 if (!lex_tokench(lex, ch) ||
761 return (lex->tok->ttype = TOKEN_FATAL);
763 return (lex->tok->ttype = ch);
769 if (ch == ',' || ch == '.') {
770 if (!lex_tokench(lex, ch) ||
773 return (lex->tok->ttype = TOKEN_FATAL);
775 return (lex->tok->ttype = TOKEN_OPERATOR);
778 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
779 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
780 ch == '=' || ch == '!' || /* ==, != */
781 ch == '&' || ch == '|') /* &&, ||, &=, |= */
783 if (!lex_tokench(lex, ch))
784 return (lex->tok->ttype = TOKEN_FATAL);
786 nextch = lex_getch(lex);
787 if (nextch == ch || nextch == '=') {
788 if (!lex_tokench(lex, nextch))
789 return (lex->tok->ttype = TOKEN_FATAL);
790 } else if (ch == '-' && nextch == '>') {
791 if (!lex_tokench(lex, nextch))
792 return (lex->tok->ttype = TOKEN_FATAL);
794 lex_ungetch(lex, nextch);
796 if (!lex_endtoken(lex))
797 return (lex->tok->ttype = TOKEN_FATAL);
798 return (lex->tok->ttype = TOKEN_OPERATOR);
802 if (ch == '^' || ch == '~' || ch == '!')
804 if (!lex_tokench(lex, ch) ||
807 return (lex->tok->ttype = TOKEN_FATAL);
809 return (lex->tok->ttype = TOKEN_OPERATOR);
813 if (ch == '*' || ch == '/') /* *=, /= */
815 if (!lex_tokench(lex, ch))
816 return (lex->tok->ttype = TOKEN_FATAL);
818 nextch = lex_getch(lex);
820 if (!lex_tokench(lex, nextch))
821 return (lex->tok->ttype = TOKEN_FATAL);
823 lex_ungetch(lex, nextch);
825 if (!lex_endtoken(lex))
826 return (lex->tok->ttype = TOKEN_FATAL);
827 return (lex->tok->ttype = TOKEN_OPERATOR);
830 if (isident_start(ch))
834 if (!lex_tokench(lex, ch))
835 return (lex->tok->ttype = TOKEN_FATAL);
836 if (!lex_finish_ident(lex)) {
838 return (lex->tok->ttype = TOKEN_ERROR);
840 if (!lex_endtoken(lex))
841 return (lex->tok->ttype = TOKEN_FATAL);
842 lex->tok->ttype = TOKEN_IDENT;
845 if (!strcmp(v, "void")) {
846 lex->tok->ttype = TOKEN_TYPENAME;
847 lex->tok->constval.t = TYPE_VOID;
848 } else if (!strcmp(v, "int")) {
849 lex->tok->ttype = TOKEN_TYPENAME;
850 lex->tok->constval.t = TYPE_INTEGER;
851 } else if (!strcmp(v, "float")) {
852 lex->tok->ttype = TOKEN_TYPENAME;
853 lex->tok->constval.t = TYPE_FLOAT;
854 } else if (!strcmp(v, "string")) {
855 lex->tok->ttype = TOKEN_TYPENAME;
856 lex->tok->constval.t = TYPE_STRING;
857 } else if (!strcmp(v, "entity")) {
858 lex->tok->ttype = TOKEN_TYPENAME;
859 lex->tok->constval.t = TYPE_ENTITY;
860 } else if (!strcmp(v, "vector")) {
861 lex->tok->ttype = TOKEN_TYPENAME;
862 lex->tok->constval.t = TYPE_VECTOR;
863 } else if (!strcmp(v, "for") ||
864 !strcmp(v, "while") ||
867 !strcmp(v, "else") ||
868 !strcmp(v, "local") ||
869 !strcmp(v, "return") ||
871 lex->tok->ttype = TOKEN_KEYWORD;
873 return lex->tok->ttype;
878 lex->tok->ttype = lex_finish_string(lex, '"');
879 while (lex->tok->ttype == TOKEN_STRINGCONST)
881 /* Allow c style "string" "continuation" */
882 ch = lex_skipwhite(lex);
884 lex_ungetch(lex, ch);
888 lex->tok->ttype = lex_finish_string(lex, '"');
890 if (!lex_endtoken(lex))
891 return (lex->tok->ttype = TOKEN_FATAL);
892 return lex->tok->ttype;
897 /* we parse character constants like string,
898 * but return TOKEN_CHARCONST, or a vector type if it fits...
899 * Likewise actual unescaping has to be done by the parser.
900 * The difference is we don't allow 'char' 'continuation'.
902 lex->tok->ttype = lex_finish_string(lex, '\'');
903 if (!lex_endtoken(lex))
904 return (lex->tok->ttype = TOKEN_FATAL);
906 /* It's a vector if we can successfully scan 3 floats */
908 if (sscanf_s(lex->tok->value, " %f %f %f ",
909 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
911 if (sscanf(lex->tok->value, " %f %f %f ",
912 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
915 lex->tok->ttype = TOKEN_VECTORCONST;
918 return lex->tok->ttype;
923 lex->tok->ttype = lex_finish_digit(lex, ch);
924 if (!lex_endtoken(lex))
925 return (lex->tok->ttype = TOKEN_FATAL);
926 return lex->tok->ttype;
929 lexerror(lex, "unknown token");
930 return (lex->tok->ttype = TOKEN_ERROR);