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 printf("error %s:%lu: ", lex->name, (unsigned long)lex->sline);
30 bool lexwarn(lex_file *lex, int warn, const char *fmt, ...)
38 printf("warning %s:%lu: ", lex->name, (unsigned long)lex->sline);
53 token *tok = (token*)mem_a(sizeof(token));
56 memset(tok, 0, sizeof(*tok));
60 void token_delete(token *self)
62 if (self->next && self->next->prev == self)
63 self->next->prev = self->prev;
64 if (self->prev && self->prev->next == self)
65 self->prev->next = self->next;
66 MEM_VECTOR_CLEAR(self, value);
70 token* token_copy(const token *cp)
72 token* self = token_new();
76 self->value_alloc = cp->value_count + 1;
77 self->value_count = cp->value_count;
78 self->value = (char*)mem_a(self->value_alloc);
83 memcpy(self->value, cp->value, cp->value_count);
84 self->value[self->value_alloc-1] = 0;
88 self->ttype = cp->ttype;
89 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
93 void token_delete_all(token *t)
104 token* token_copy_all(const token *cp)
109 out = cur = token_copy(cp);
115 cur->next = token_copy(cp);
117 token_delete_all(out);
120 cur->next->prev = cur;
127 lex_file* lex_open(const char *file)
130 FILE *in = util_fopen(file, "rb");
133 lexerror(NULL, "open failed: '%s'\n", file);
137 lex = (lex_file*)mem_a(sizeof(*lex));
140 lexerror(NULL, "out of memory\n");
144 memset(lex, 0, sizeof(*lex));
147 lex->name = util_strdup(file);
148 lex->line = 1; /* we start counting at 1 */
153 lex_filenames_add(lex->name);
158 void lex_cleanup(void)
161 for (i = 0; i < lex_filenames_elements; ++i)
162 mem_d(lex_filenames_data[i]);
163 mem_d(lex_filenames_data);
166 void lex_close(lex_file *lex)
169 for (i = 0; i < lex->frames_count; ++i)
170 mem_d(lex->frames[i].name);
171 MEM_VECTOR_CLEAR(lex, frames);
174 mem_d(lex->modelname);
179 token_delete(lex->tok);
180 /* mem_d(lex->name); collected in lex_filenames */
184 /* Get or put-back data
185 * The following to functions do NOT understand what kind of data they
187 * The are merely wrapping get/put in order to count line numbers.
189 static int lex_getch(lex_file *lex)
195 if (lex->peek[lex->peekpos] == '\n')
197 return lex->peek[lex->peekpos];
200 ch = fgetc(lex->file);
206 static void lex_ungetch(lex_file *lex, int ch)
208 lex->peek[lex->peekpos++] = ch;
213 /* classify characters
214 * some additions to the is*() functions of ctype.h
217 /* Idents are alphanumberic, but they start with alpha or _ */
218 static bool isident_start(int ch)
220 return isalpha(ch) || ch == '_';
223 static bool isident(int ch)
225 return isident_start(ch) || isdigit(ch);
228 /* isxdigit_only is used when we already know it's not a digit
229 * and want to see if it's a hex digit anyway.
231 static bool isxdigit_only(int ch)
233 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
236 /* Skip whitespace and comments and return the first
237 * non-white character.
238 * As this makes use of the above getch() ungetch() functions,
239 * we don't need to care at all about line numbering anymore.
241 * In theory, this function should only be used at the beginning
242 * of lexing, or when we *know* the next character is part of the token.
243 * Otherwise, if the parser throws an error, the linenumber may not be
244 * the line of the error, but the line of the next token AFTER the error.
246 * This is currently only problematic when using c-like string-continuation,
247 * since comments and whitespaces are allowed between 2 such strings.
251 "A continuation of the previous string"
252 // This line is skipped
255 * In this case, if the parse decides it didn't actually want a string,
256 * and uses lex->line to print an error, it will show the ', foo);' line's
259 * On the other hand, the parser is supposed to remember the line of the next
260 * token's beginning. In this case we would want skipwhite() to be called
261 * AFTER reading a token, so that the parser, before reading the NEXT token,
262 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
265 * here is to store the line of the first character after skipping
266 * the initial whitespace in lex->sline, this happens in lex_do.
268 static int lex_skipwhite(lex_file *lex)
275 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
281 /* one line comment */
284 /* check for special: '/', '/', '*', '/' */
293 while (ch != EOF && ch != '\n') {
300 /* multiline comment */
312 if (ch == '/') /* allow *//* direct following comment */
314 lex_ungetch(lex, ch);
315 ch = ' '; /* cause TRUE in the isspace check */
319 /* Otherwise roll back to the slash and break out of the loop */
320 lex_ungetch(lex, ch);
324 } while (ch != EOF && isspace(ch));
329 /* Append a character to the token buffer */
330 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
332 if (!token_value_add(lex->tok, ch)) {
333 lexerror(lex, "out of memory");
339 /* Append a trailing null-byte */
340 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
342 if (!token_value_add(lex->tok, 0)) {
343 lexerror(lex, "out of memory");
346 lex->tok->value_count--;
351 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
356 while (ch != EOF && isident(ch))
358 if (!lex_tokench(lex, ch))
359 return (lex->tok->ttype = TOKEN_FATAL);
363 /* last ch was not an ident ch: */
364 lex_ungetch(lex, ch);
369 /* read one ident for the frame list */
370 static int lex_parse_frame(lex_file *lex)
375 token_delete(lex->tok);
376 lex->tok = token_new();
379 while (ch != EOF && ch != '\n' && isspace(ch))
385 if (!isident_start(ch)) {
386 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
390 if (!lex_tokench(lex, ch))
392 if (!lex_finish_ident(lex))
394 if (!lex_endtoken(lex))
399 /* read a list of $frames */
400 static bool lex_finish_frames(lex_file *lex)
407 rc = lex_parse_frame(lex);
408 if (rc > 0) /* end of line */
410 if (rc < 0) /* error */
413 for (i = 0; i < lex->frames_count; ++i) {
414 if (!strcmp(lex->tok->value, lex->frames[i].name)) {
415 lex->frames[i].value = lex->framevalue++;
416 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok->value))
422 m.value = lex->framevalue++;
423 m.name = lex->tok->value;
424 lex->tok->value = NULL;
425 if (!lex_file_frames_add(lex, m)) {
426 lexerror(lex, "out of memory");
432 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
440 return TOKEN_STRINGCONST;
445 lexerror(lex, "unexpected end of file");
446 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
447 return (lex->tok->ttype = TOKEN_ERROR);
452 case 'a': ch = '\a'; break;
453 case 'b': ch = '\b'; break;
454 case 'r': ch = '\r'; break;
455 case 'n': ch = '\n'; break;
456 case 't': ch = '\t'; break;
457 case 'f': ch = '\f'; break;
458 case 'v': ch = '\v'; break;
460 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
461 /* so we just add the character plus backslash no matter what it actually is */
462 if (!lex_tokench(lex, '\\'))
463 return (lex->tok->ttype = TOKEN_FATAL);
465 /* add the character finally */
466 if (!lex_tokench(lex, ch))
467 return (lex->tok->ttype = TOKEN_FATAL);
469 else if (!lex_tokench(lex, ch))
470 return (lex->tok->ttype = TOKEN_FATAL);
472 lexerror(lex, "unexpected end of file within string constant");
473 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
474 return (lex->tok->ttype = TOKEN_ERROR);
477 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
483 /* parse a number... */
484 lex->tok->ttype = TOKEN_INTCONST;
486 if (!lex_tokench(lex, ch))
487 return (lex->tok->ttype = TOKEN_FATAL);
490 if (ch != '.' && !isdigit(ch))
492 if (lastch != '0' || ch != 'x')
494 /* end of the number or EOF */
495 lex_ungetch(lex, ch);
496 if (!lex_endtoken(lex))
497 return (lex->tok->ttype = TOKEN_FATAL);
499 lex->tok->constval.i = lastch - '0';
500 return lex->tok->ttype;
506 /* EOF would have been caught above */
510 if (!lex_tokench(lex, ch))
511 return (lex->tok->ttype = TOKEN_FATAL);
513 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
515 if (!lex_tokench(lex, ch))
516 return (lex->tok->ttype = TOKEN_FATAL);
520 /* NOT else, '.' can come from above as well */
521 if (ch == '.' && !ishex)
523 /* Allow floating comma in non-hex mode */
524 lex->tok->ttype = TOKEN_FLOATCONST;
525 if (!lex_tokench(lex, ch))
526 return (lex->tok->ttype = TOKEN_FATAL);
528 /* continue digits-only */
532 if (!lex_tokench(lex, ch))
533 return (lex->tok->ttype = TOKEN_FATAL);
537 /* put back the last character */
538 /* but do not put back the trailing 'f' or a float */
539 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
542 /* generally we don't want words to follow numbers: */
544 lexerror(lex, "unexpected trailing characters after number");
545 return (lex->tok->ttype = TOKEN_ERROR);
547 lex_ungetch(lex, ch);
549 if (!lex_endtoken(lex))
550 return (lex->tok->ttype = TOKEN_FATAL);
551 if (lex->tok->ttype == TOKEN_FLOATCONST)
552 lex->tok->constval.f = strtod(lex->tok->value, NULL);
554 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
555 return lex->tok->ttype;
558 int lex_do(lex_file *lex)
563 token_delete(lex->tok);
564 lex->tok = token_new();
568 ch = lex_skipwhite(lex);
569 lex->sline = lex->line;
570 lex->tok->ctx.line = lex->sline;
571 lex->tok->ctx.file = lex->name;
574 return (lex->tok->ttype = TOKEN_FATAL);
578 return (lex->tok->ttype = TOKEN_EOF);
581 /* modelgen / spiritgen commands */
587 if (!isident_start(ch)) {
588 lexerror(lex, "hanging '$' modelgen/spritegen command line");
591 if (!lex_tokench(lex, ch))
592 return (lex->tok->ttype = TOKEN_FATAL);
593 if (!lex_finish_ident(lex))
594 return (lex->tok->ttype = TOKEN_ERROR);
595 if (!lex_endtoken(lex))
596 return (lex->tok->ttype = TOKEN_FATAL);
597 /* skip the known commands */
600 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
602 /* frame/framesave command works like an enum
603 * similar to fteqcc we handle this in the lexer.
604 * The reason for this is that it is sensitive to newlines,
605 * which the parser is unaware of
607 if (!lex_finish_frames(lex))
608 return (lex->tok->ttype = TOKEN_ERROR);
612 if (!strcmp(v, "framevalue"))
615 while (ch != EOF && isspace(ch) && ch != '\n')
619 lexerror(lex, "$framevalue requires an integer parameter");
623 token_delete(lex->tok);
624 lex->tok = token_new();
625 lex->tok->ttype = lex_finish_digit(lex, ch);
626 if (!lex_endtoken(lex))
627 return (lex->tok->ttype = TOKEN_FATAL);
628 if (lex->tok->ttype != TOKEN_INTCONST) {
629 lexerror(lex, "$framevalue requires an integer parameter");
632 lex->framevalue = lex->tok->constval.i;
636 if (!strcmp(v, "framerestore"))
640 token_delete(lex->tok);
641 lex->tok = token_new();
643 rc = lex_parse_frame(lex);
646 lexerror(lex, "$framerestore requires a framename parameter");
650 return (lex->tok->ttype = TOKEN_FATAL);
653 for (frame = 0; frame < lex->frames_count; ++frame) {
654 if (!strcmp(v, lex->frames[frame].name)) {
655 lex->framevalue = lex->frames[frame].value;
659 lexerror(lex, "unknown framename `%s`", v);
663 if (!strcmp(v, "modelname"))
667 token_delete(lex->tok);
668 lex->tok = token_new();
670 rc = lex_parse_frame(lex);
673 lexerror(lex, "$framerestore requires a framename parameter");
677 return (lex->tok->ttype = TOKEN_FATAL);
680 if (lex->modelname) {
682 m.value = lex->framevalue;
683 m.name = lex->modelname;
684 lex->modelname = NULL;
685 if (!lex_file_frames_add(lex, m)) {
686 lexerror(lex, "out of memory");
687 return (lex->tok->ttype = TOKEN_FATAL);
690 lex->modelname = lex->tok->value;
691 lex->tok->value = NULL;
692 for (frame = 0; frame < lex->frames_count; ++frame) {
693 if (!strcmp(v, lex->frames[frame].name)) {
694 lex->framevalue = lex->frames[frame].value;
701 if (!strcmp(v, "flush"))
704 for (frame = 0; frame < lex->frames_count; ++frame)
705 mem_d(lex->frames[frame].name);
706 MEM_VECTOR_CLEAR(lex, frames);
707 /* skip line (fteqcc does it too) */
709 while (ch != EOF && ch != '\n')
714 if (!strcmp(v, "cd") ||
715 !strcmp(v, "origin") ||
716 !strcmp(v, "base") ||
717 !strcmp(v, "flags") ||
718 !strcmp(v, "scale") ||
723 while (ch != EOF && ch != '\n')
728 for (frame = 0; frame < lex->frames_count; ++frame) {
729 if (!strcmp(v, lex->frames[frame].name)) {
730 lex->tok->constval.i = lex->frames[frame].value;
731 return (lex->tok->ttype = TOKEN_INTCONST);
735 lexerror(lex, "invalid frame macro");
739 /* single-character tokens */
743 if (!lex_tokench(lex, ch) ||
746 return (lex->tok->ttype = TOKEN_FATAL);
748 if (lex->flags.noops)
749 return (lex->tok->ttype = ch);
751 return (lex->tok->ttype = TOKEN_OPERATOR);
760 if (!lex_tokench(lex, ch) ||
763 return (lex->tok->ttype = TOKEN_FATAL);
765 return (lex->tok->ttype = ch);
770 if (lex->flags.noops)
772 /* Detect characters early which are normally
773 * operators OR PART of an operator.
790 if (!lex_tokench(lex, ch) ||
793 return (lex->tok->ttype = TOKEN_FATAL);
795 return (lex->tok->ttype = ch);
802 if (!lex_tokench(lex, ch))
803 return (lex->tok->ttype = TOKEN_FATAL);
804 /* peak ahead once */
805 nextch = lex_getch(lex);
807 lex_ungetch(lex, nextch);
808 if (!lex_endtoken(lex))
809 return (lex->tok->ttype = TOKEN_FATAL);
810 return (lex->tok->ttype = ch);
812 /* peak ahead again */
813 nextch = lex_getch(lex);
815 lex_ungetch(lex, nextch);
816 lex_ungetch(lex, nextch);
817 if (!lex_endtoken(lex))
818 return (lex->tok->ttype = TOKEN_FATAL);
819 return (lex->tok->ttype = ch);
821 /* fill the token to be "..." */
822 if (!lex_tokench(lex, ch) ||
823 !lex_tokench(lex, ch) ||
826 return (lex->tok->ttype = TOKEN_FATAL);
828 return (lex->tok->ttype = TOKEN_DOTS);
832 if (ch == ',' || ch == '.') {
833 if (!lex_tokench(lex, ch) ||
836 return (lex->tok->ttype = TOKEN_FATAL);
838 return (lex->tok->ttype = TOKEN_OPERATOR);
841 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
842 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
843 ch == '=' || ch == '!' || /* ==, != */
844 ch == '&' || ch == '|') /* &&, ||, &=, |= */
846 if (!lex_tokench(lex, ch))
847 return (lex->tok->ttype = TOKEN_FATAL);
849 nextch = lex_getch(lex);
850 if (nextch == ch || nextch == '=') {
851 if (!lex_tokench(lex, nextch))
852 return (lex->tok->ttype = TOKEN_FATAL);
853 } else if (ch == '-' && nextch == '>') {
854 if (!lex_tokench(lex, nextch))
855 return (lex->tok->ttype = TOKEN_FATAL);
857 lex_ungetch(lex, nextch);
859 if (!lex_endtoken(lex))
860 return (lex->tok->ttype = TOKEN_FATAL);
861 return (lex->tok->ttype = TOKEN_OPERATOR);
865 if (ch == '^' || ch == '~' || ch == '!')
867 if (!lex_tokench(lex, ch) ||
870 return (lex->tok->ttype = TOKEN_FATAL);
872 return (lex->tok->ttype = TOKEN_OPERATOR);
876 if (ch == '*' || ch == '/') /* *=, /= */
878 if (!lex_tokench(lex, ch))
879 return (lex->tok->ttype = TOKEN_FATAL);
881 nextch = lex_getch(lex);
883 if (!lex_tokench(lex, nextch))
884 return (lex->tok->ttype = TOKEN_FATAL);
886 lex_ungetch(lex, nextch);
888 if (!lex_endtoken(lex))
889 return (lex->tok->ttype = TOKEN_FATAL);
890 return (lex->tok->ttype = TOKEN_OPERATOR);
893 if (isident_start(ch))
897 if (!lex_tokench(lex, ch))
898 return (lex->tok->ttype = TOKEN_FATAL);
899 if (!lex_finish_ident(lex)) {
901 return (lex->tok->ttype = TOKEN_ERROR);
903 if (!lex_endtoken(lex))
904 return (lex->tok->ttype = TOKEN_FATAL);
905 lex->tok->ttype = TOKEN_IDENT;
908 if (!strcmp(v, "void")) {
909 lex->tok->ttype = TOKEN_TYPENAME;
910 lex->tok->constval.t = TYPE_VOID;
911 } else if (!strcmp(v, "int")) {
912 lex->tok->ttype = TOKEN_TYPENAME;
913 lex->tok->constval.t = TYPE_INTEGER;
914 } else if (!strcmp(v, "float")) {
915 lex->tok->ttype = TOKEN_TYPENAME;
916 lex->tok->constval.t = TYPE_FLOAT;
917 } else if (!strcmp(v, "string")) {
918 lex->tok->ttype = TOKEN_TYPENAME;
919 lex->tok->constval.t = TYPE_STRING;
920 } else if (!strcmp(v, "entity")) {
921 lex->tok->ttype = TOKEN_TYPENAME;
922 lex->tok->constval.t = TYPE_ENTITY;
923 } else if (!strcmp(v, "vector")) {
924 lex->tok->ttype = TOKEN_TYPENAME;
925 lex->tok->constval.t = TYPE_VECTOR;
926 } else if (!strcmp(v, "for") ||
927 !strcmp(v, "while") ||
930 !strcmp(v, "else") ||
931 !strcmp(v, "local") ||
932 !strcmp(v, "return") ||
934 lex->tok->ttype = TOKEN_KEYWORD;
936 return lex->tok->ttype;
941 lex->tok->ttype = lex_finish_string(lex, '"');
942 while (lex->tok->ttype == TOKEN_STRINGCONST)
944 /* Allow c style "string" "continuation" */
945 ch = lex_skipwhite(lex);
947 lex_ungetch(lex, ch);
951 lex->tok->ttype = lex_finish_string(lex, '"');
953 if (!lex_endtoken(lex))
954 return (lex->tok->ttype = TOKEN_FATAL);
955 return lex->tok->ttype;
960 /* we parse character constants like string,
961 * but return TOKEN_CHARCONST, or a vector type if it fits...
962 * Likewise actual unescaping has to be done by the parser.
963 * The difference is we don't allow 'char' 'continuation'.
965 lex->tok->ttype = lex_finish_string(lex, '\'');
966 if (!lex_endtoken(lex))
967 return (lex->tok->ttype = TOKEN_FATAL);
969 /* It's a vector if we can successfully scan 3 floats */
971 if (sscanf_s(lex->tok->value, " %f %f %f ",
972 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
974 if (sscanf(lex->tok->value, " %f %f %f ",
975 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
978 lex->tok->ttype = TOKEN_VECTORCONST;
981 return lex->tok->ttype;
986 lex->tok->ttype = lex_finish_digit(lex, ch);
987 if (!lex_endtoken(lex))
988 return (lex->tok->ttype = TOKEN_FATAL);
989 return lex->tok->ttype;
992 lexerror(lex, "unknown token");
993 return (lex->tok->ttype = TOKEN_ERROR);