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);
159 mem_d(lex->modelname);
164 token_delete(lex->tok);
169 /* Get or put-back data
170 * The following to functions do NOT understand what kind of data they
172 * The are merely wrapping get/put in order to count line numbers.
174 static int lex_getch(lex_file *lex)
180 if (lex->peek[lex->peekpos] == '\n')
182 return lex->peek[lex->peekpos];
185 ch = fgetc(lex->file);
191 static void lex_ungetch(lex_file *lex, int ch)
193 lex->peek[lex->peekpos++] = ch;
198 /* classify characters
199 * some additions to the is*() functions of ctype.h
202 /* Idents are alphanumberic, but they start with alpha or _ */
203 static bool isident_start(int ch)
205 return isalpha(ch) || ch == '_';
208 static bool isident(int ch)
210 return isident_start(ch) || isdigit(ch);
213 /* isxdigit_only is used when we already know it's not a digit
214 * and want to see if it's a hex digit anyway.
216 static bool isxdigit_only(int ch)
218 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
221 /* Skip whitespace and comments and return the first
222 * non-white character.
223 * As this makes use of the above getch() ungetch() functions,
224 * we don't need to care at all about line numbering anymore.
226 * In theory, this function should only be used at the beginning
227 * of lexing, or when we *know* the next character is part of the token.
228 * Otherwise, if the parser throws an error, the linenumber may not be
229 * the line of the error, but the line of the next token AFTER the error.
231 * This is currently only problematic when using c-like string-continuation,
232 * since comments and whitespaces are allowed between 2 such strings.
236 "A continuation of the previous string"
237 // This line is skipped
240 * In this case, if the parse decides it didn't actually want a string,
241 * and uses lex->line to print an error, it will show the ', foo);' line's
244 * On the other hand, the parser is supposed to remember the line of the next
245 * token's beginning. In this case we would want skipwhite() to be called
246 * AFTER reading a token, so that the parser, before reading the NEXT token,
247 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
250 * here is to store the line of the first character after skipping
251 * the initial whitespace in lex->sline, this happens in lex_do.
253 static int lex_skipwhite(lex_file *lex)
260 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
266 /* one line comment */
269 /* check for special: '/', '/', '*', '/' */
278 while (ch != EOF && ch != '\n') {
285 /* multiline comment */
297 if (ch == '/') /* allow *//* direct following comment */
299 lex_ungetch(lex, ch);
300 ch = ' '; /* cause TRUE in the isspace check */
304 /* Otherwise roll back to the slash and break out of the loop */
305 lex_ungetch(lex, ch);
309 } while (ch != EOF && isspace(ch));
314 /* Append a character to the token buffer */
315 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
317 if (!token_value_add(lex->tok, ch)) {
318 lexerror(lex, "out of memory");
324 /* Append a trailing null-byte */
325 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
327 if (!token_value_add(lex->tok, 0)) {
328 lexerror(lex, "out of memory");
331 lex->tok->value_count--;
336 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
341 while (ch != EOF && isident(ch))
343 if (!lex_tokench(lex, ch))
344 return (lex->tok->ttype = TOKEN_FATAL);
348 /* last ch was not an ident ch: */
349 lex_ungetch(lex, ch);
354 /* read one ident for the frame list */
355 static int lex_parse_frame(lex_file *lex)
360 token_delete(lex->tok);
361 lex->tok = token_new();
364 while (ch != EOF && ch != '\n' && isspace(ch))
370 if (!isident_start(ch)) {
371 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
375 if (!lex_tokench(lex, ch))
377 if (!lex_finish_ident(lex))
379 if (!lex_endtoken(lex))
384 /* read a list of $frames */
385 static bool lex_finish_frames(lex_file *lex)
391 rc = lex_parse_frame(lex);
392 if (rc > 0) /* end of line */
394 if (rc < 0) /* error */
397 m.value = lex->framevalue++;
398 m.name = lex->tok->value;
399 lex->tok->value = NULL;
400 if (!lex_file_frames_add(lex, m)) {
401 lexerror(lex, "out of memory");
407 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
415 return TOKEN_STRINGCONST;
420 lexerror(lex, "unexpected end of file");
421 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
422 return (lex->tok->ttype = TOKEN_ERROR);
427 case 'a': ch = '\a'; break;
428 case 'b': ch = '\b'; break;
429 case 'r': ch = '\r'; break;
430 case 'n': ch = '\n'; break;
431 case 't': ch = '\t'; break;
432 case 'f': ch = '\f'; break;
433 case 'v': ch = '\v'; break;
435 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
436 /* so we just add the character plus backslash no matter what it actually is */
437 if (!lex_tokench(lex, '\\'))
438 return (lex->tok->ttype = TOKEN_FATAL);
440 /* add the character finally */
441 if (!lex_tokench(lex, ch))
442 return (lex->tok->ttype = TOKEN_FATAL);
444 else if (!lex_tokench(lex, ch))
445 return (lex->tok->ttype = TOKEN_FATAL);
447 lexerror(lex, "unexpected end of file within string constant");
448 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
449 return (lex->tok->ttype = TOKEN_ERROR);
452 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
458 /* parse a number... */
459 lex->tok->ttype = TOKEN_INTCONST;
461 if (!lex_tokench(lex, ch))
462 return (lex->tok->ttype = TOKEN_FATAL);
465 if (ch != '.' && !isdigit(ch))
467 if (lastch != '0' || ch != 'x')
469 /* end of the number or EOF */
470 lex_ungetch(lex, ch);
471 if (!lex_endtoken(lex))
472 return (lex->tok->ttype = TOKEN_FATAL);
474 lex->tok->constval.i = lastch - '0';
475 return lex->tok->ttype;
481 /* EOF would have been caught above */
485 if (!lex_tokench(lex, ch))
486 return (lex->tok->ttype = TOKEN_FATAL);
488 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
490 if (!lex_tokench(lex, ch))
491 return (lex->tok->ttype = TOKEN_FATAL);
495 /* NOT else, '.' can come from above as well */
496 if (ch == '.' && !ishex)
498 /* Allow floating comma in non-hex mode */
499 lex->tok->ttype = TOKEN_FLOATCONST;
500 if (!lex_tokench(lex, ch))
501 return (lex->tok->ttype = TOKEN_FATAL);
503 /* continue digits-only */
507 if (!lex_tokench(lex, ch))
508 return (lex->tok->ttype = TOKEN_FATAL);
512 /* put back the last character */
513 /* but do not put back the trailing 'f' or a float */
514 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
517 /* generally we don't want words to follow numbers: */
519 lexerror(lex, "unexpected trailing characters after number");
520 return (lex->tok->ttype = TOKEN_ERROR);
522 lex_ungetch(lex, ch);
524 if (!lex_endtoken(lex))
525 return (lex->tok->ttype = TOKEN_FATAL);
526 if (lex->tok->ttype == TOKEN_FLOATCONST)
527 lex->tok->constval.f = strtod(lex->tok->value, NULL);
529 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
530 return lex->tok->ttype;
533 int lex_do(lex_file *lex)
538 token_delete(lex->tok);
539 lex->tok = token_new();
543 ch = lex_skipwhite(lex);
544 lex->sline = lex->line;
545 lex->tok->ctx.line = lex->sline;
546 lex->tok->ctx.file = lex->name;
549 return (lex->tok->ttype = TOKEN_EOF);
551 /* modelgen / spiritgen commands */
557 if (!isident_start(ch)) {
558 lexerror(lex, "hanging '$' modelgen/spritegen command line");
561 if (!lex_tokench(lex, ch))
562 return (lex->tok->ttype = TOKEN_FATAL);
563 if (!lex_finish_ident(lex))
564 return (lex->tok->ttype = TOKEN_ERROR);
565 if (!lex_endtoken(lex))
566 return (lex->tok->ttype = TOKEN_FATAL);
567 /* skip the known commands */
570 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
572 /* frame/framesave command works like an enum
573 * similar to fteqcc we handle this in the lexer.
574 * The reason for this is that it is sensitive to newlines,
575 * which the parser is unaware of
577 if (!lex_finish_frames(lex))
578 return (lex->tok->ttype = TOKEN_ERROR);
582 if (!strcmp(v, "framevalue"))
585 while (ch != EOF && isspace(ch) && ch != '\n')
589 lexerror(lex, "$framevalue requires an integer parameter");
593 token_delete(lex->tok);
594 lex->tok = token_new();
595 lex->tok->ttype = lex_finish_digit(lex, ch);
596 if (!lex_endtoken(lex))
597 return (lex->tok->ttype = TOKEN_FATAL);
598 if (lex->tok->ttype != TOKEN_INTCONST) {
599 lexerror(lex, "$framevalue requires an integer parameter");
602 lex->framevalue = lex->tok->constval.i;
606 if (!strcmp(v, "framerestore"))
610 token_delete(lex->tok);
611 lex->tok = token_new();
613 rc = lex_parse_frame(lex);
616 lexerror(lex, "$framerestore requires a framename parameter");
620 return (lex->tok->ttype = TOKEN_FATAL);
623 for (frame = 0; frame < lex->frames_count; ++frame) {
624 if (!strcmp(v, lex->frames[frame].name)) {
625 lex->framevalue = lex->frames[frame].value;
629 lexerror(lex, "unknown framename `%s`", v);
633 if (!strcmp(v, "modelname"))
637 token_delete(lex->tok);
638 lex->tok = token_new();
640 rc = lex_parse_frame(lex);
643 lexerror(lex, "$framerestore requires a framename parameter");
647 return (lex->tok->ttype = TOKEN_FATAL);
650 if (lex->modelname) {
652 m.value = lex->framevalue;
653 m.name = lex->modelname;
654 lex->modelname = NULL;
655 if (!lex_file_frames_add(lex, m)) {
656 lexerror(lex, "out of memory");
657 return (lex->tok->ttype = TOKEN_FATAL);
660 lex->modelname = lex->tok->value;
661 lex->tok->value = NULL;
662 for (frame = 0; frame < lex->frames_count; ++frame) {
663 if (!strcmp(v, lex->frames[frame].name)) {
664 lex->framevalue = lex->frames[frame].value;
671 if (!strcmp(v, "flush"))
674 for (frame = 0; frame < lex->frames_count; ++frame)
675 mem_d(lex->frames[frame].name);
676 MEM_VECTOR_CLEAR(lex, frames);
677 /* skip line (fteqcc does it too) */
679 while (ch != EOF && ch != '\n')
684 if (!strcmp(v, "cd") ||
685 !strcmp(v, "origin") ||
686 !strcmp(v, "base") ||
687 !strcmp(v, "flags") ||
688 !strcmp(v, "scale") ||
693 while (ch != EOF && ch != '\n')
698 for (frame = 0; frame < lex->frames_count; ++frame) {
699 if (!strcmp(v, lex->frames[frame].name)) {
700 lex->tok->constval.i = lex->frames[frame].value;
701 return (lex->tok->ttype = TOKEN_INTCONST);
705 lexerror(lex, "invalid frame macro");
709 /* single-character tokens */
713 if (!lex_tokench(lex, ch) ||
716 return (lex->tok->ttype = TOKEN_FATAL);
718 if (lex->flags.noops)
719 return (lex->tok->ttype = ch);
721 return (lex->tok->ttype = TOKEN_OPERATOR);
730 if (!lex_tokench(lex, ch) ||
733 return (lex->tok->ttype = TOKEN_FATAL);
735 return (lex->tok->ttype = ch);
740 if (lex->flags.noops)
742 /* Detect characters early which are normally
743 * operators OR PART of an operator.
761 if (!lex_tokench(lex, ch) ||
764 return (lex->tok->ttype = TOKEN_FATAL);
766 return (lex->tok->ttype = ch);
772 if (ch == ',' || ch == '.') {
773 if (!lex_tokench(lex, ch) ||
776 return (lex->tok->ttype = TOKEN_FATAL);
778 return (lex->tok->ttype = TOKEN_OPERATOR);
781 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
782 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
783 ch == '=' || ch == '!' || /* ==, != */
784 ch == '&' || ch == '|') /* &&, ||, &=, |= */
786 if (!lex_tokench(lex, ch))
787 return (lex->tok->ttype = TOKEN_FATAL);
789 nextch = lex_getch(lex);
790 if (nextch == ch || nextch == '=') {
791 if (!lex_tokench(lex, nextch))
792 return (lex->tok->ttype = TOKEN_FATAL);
793 } else if (ch == '-' && nextch == '>') {
794 if (!lex_tokench(lex, nextch))
795 return (lex->tok->ttype = TOKEN_FATAL);
797 lex_ungetch(lex, nextch);
799 if (!lex_endtoken(lex))
800 return (lex->tok->ttype = TOKEN_FATAL);
801 return (lex->tok->ttype = TOKEN_OPERATOR);
805 if (ch == '^' || ch == '~' || ch == '!')
807 if (!lex_tokench(lex, ch) ||
810 return (lex->tok->ttype = TOKEN_FATAL);
812 return (lex->tok->ttype = TOKEN_OPERATOR);
816 if (ch == '*' || ch == '/') /* *=, /= */
818 if (!lex_tokench(lex, ch))
819 return (lex->tok->ttype = TOKEN_FATAL);
821 nextch = lex_getch(lex);
823 if (!lex_tokench(lex, nextch))
824 return (lex->tok->ttype = TOKEN_FATAL);
826 lex_ungetch(lex, nextch);
828 if (!lex_endtoken(lex))
829 return (lex->tok->ttype = TOKEN_FATAL);
830 return (lex->tok->ttype = TOKEN_OPERATOR);
833 if (isident_start(ch))
837 if (!lex_tokench(lex, ch))
838 return (lex->tok->ttype = TOKEN_FATAL);
839 if (!lex_finish_ident(lex)) {
841 return (lex->tok->ttype = TOKEN_ERROR);
843 if (!lex_endtoken(lex))
844 return (lex->tok->ttype = TOKEN_FATAL);
845 lex->tok->ttype = TOKEN_IDENT;
848 if (!strcmp(v, "void")) {
849 lex->tok->ttype = TOKEN_TYPENAME;
850 lex->tok->constval.t = TYPE_VOID;
851 } else if (!strcmp(v, "int")) {
852 lex->tok->ttype = TOKEN_TYPENAME;
853 lex->tok->constval.t = TYPE_INTEGER;
854 } else if (!strcmp(v, "float")) {
855 lex->tok->ttype = TOKEN_TYPENAME;
856 lex->tok->constval.t = TYPE_FLOAT;
857 } else if (!strcmp(v, "string")) {
858 lex->tok->ttype = TOKEN_TYPENAME;
859 lex->tok->constval.t = TYPE_STRING;
860 } else if (!strcmp(v, "entity")) {
861 lex->tok->ttype = TOKEN_TYPENAME;
862 lex->tok->constval.t = TYPE_ENTITY;
863 } else if (!strcmp(v, "vector")) {
864 lex->tok->ttype = TOKEN_TYPENAME;
865 lex->tok->constval.t = TYPE_VECTOR;
866 } else if (!strcmp(v, "for") ||
867 !strcmp(v, "while") ||
870 !strcmp(v, "else") ||
871 !strcmp(v, "local") ||
872 !strcmp(v, "return") ||
874 lex->tok->ttype = TOKEN_KEYWORD;
876 return lex->tok->ttype;
881 lex->tok->ttype = lex_finish_string(lex, '"');
882 while (lex->tok->ttype == TOKEN_STRINGCONST)
884 /* Allow c style "string" "continuation" */
885 ch = lex_skipwhite(lex);
887 lex_ungetch(lex, ch);
891 lex->tok->ttype = lex_finish_string(lex, '"');
893 if (!lex_endtoken(lex))
894 return (lex->tok->ttype = TOKEN_FATAL);
895 return lex->tok->ttype;
900 /* we parse character constants like string,
901 * but return TOKEN_CHARCONST, or a vector type if it fits...
902 * Likewise actual unescaping has to be done by the parser.
903 * The difference is we don't allow 'char' 'continuation'.
905 lex->tok->ttype = lex_finish_string(lex, '\'');
906 if (!lex_endtoken(lex))
907 return (lex->tok->ttype = TOKEN_FATAL);
909 /* It's a vector if we can successfully scan 3 floats */
911 if (sscanf_s(lex->tok->value, " %f %f %f ",
912 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
914 if (sscanf(lex->tok->value, " %f %f %f ",
915 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
918 lex->tok->ttype = TOKEN_VECTORCONST;
921 return lex->tok->ttype;
926 lex->tok->ttype = lex_finish_digit(lex, ch);
927 if (!lex_endtoken(lex))
928 return (lex->tok->ttype = TOKEN_FATAL);
929 return lex->tok->ttype;
932 lexerror(lex, "unknown token");
933 return (lex->tok->ttype = TOKEN_ERROR);