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 void lexwarn(lex_file *lex, int warn, const char *fmt, ...)
38 printf("warning %s:%lu: ", lex->name, (unsigned long)lex->sline);
51 token *tok = (token*)mem_a(sizeof(token));
54 memset(tok, 0, sizeof(*tok));
58 void token_delete(token *self)
60 if (self->next && self->next->prev == self)
61 self->next->prev = self->prev;
62 if (self->prev && self->prev->next == self)
63 self->prev->next = self->next;
64 MEM_VECTOR_CLEAR(self, value);
68 token* token_copy(const token *cp)
70 token* self = token_new();
74 self->value_alloc = cp->value_count + 1;
75 self->value_count = cp->value_count;
76 self->value = (char*)mem_a(self->value_alloc);
81 memcpy(self->value, cp->value, cp->value_count);
82 self->value[self->value_alloc-1] = 0;
86 self->ttype = cp->ttype;
87 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
91 void token_delete_all(token *t)
102 token* token_copy_all(const token *cp)
107 out = cur = token_copy(cp);
113 cur->next = token_copy(cp);
115 token_delete_all(out);
118 cur->next->prev = cur;
125 lex_file* lex_open(const char *file)
128 FILE *in = util_fopen(file, "rb");
131 lexerror(NULL, "open failed: '%s'\n", file);
135 lex = (lex_file*)mem_a(sizeof(*lex));
138 lexerror(NULL, "out of memory\n");
142 memset(lex, 0, sizeof(*lex));
145 lex->name = util_strdup(file);
146 lex->line = 1; /* we start counting at 1 */
151 lex_filenames_add(lex->name);
156 void lex_cleanup(void)
159 for (i = 0; i < lex_filenames_elements; ++i)
160 mem_d(lex_filenames_data[i]);
161 mem_d(lex_filenames_data);
164 void lex_close(lex_file *lex)
167 for (i = 0; i < lex->frames_count; ++i)
168 mem_d(lex->frames[i].name);
169 MEM_VECTOR_CLEAR(lex, frames);
172 mem_d(lex->modelname);
177 token_delete(lex->tok);
178 /* mem_d(lex->name); collected in lex_filenames */
182 /* Get or put-back data
183 * The following to functions do NOT understand what kind of data they
185 * The are merely wrapping get/put in order to count line numbers.
187 static int lex_getch(lex_file *lex)
193 if (lex->peek[lex->peekpos] == '\n')
195 return lex->peek[lex->peekpos];
198 ch = fgetc(lex->file);
204 static void lex_ungetch(lex_file *lex, int ch)
206 lex->peek[lex->peekpos++] = ch;
211 /* classify characters
212 * some additions to the is*() functions of ctype.h
215 /* Idents are alphanumberic, but they start with alpha or _ */
216 static bool isident_start(int ch)
218 return isalpha(ch) || ch == '_';
221 static bool isident(int ch)
223 return isident_start(ch) || isdigit(ch);
226 /* isxdigit_only is used when we already know it's not a digit
227 * and want to see if it's a hex digit anyway.
229 static bool isxdigit_only(int ch)
231 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
234 /* Skip whitespace and comments and return the first
235 * non-white character.
236 * As this makes use of the above getch() ungetch() functions,
237 * we don't need to care at all about line numbering anymore.
239 * In theory, this function should only be used at the beginning
240 * of lexing, or when we *know* the next character is part of the token.
241 * Otherwise, if the parser throws an error, the linenumber may not be
242 * the line of the error, but the line of the next token AFTER the error.
244 * This is currently only problematic when using c-like string-continuation,
245 * since comments and whitespaces are allowed between 2 such strings.
249 "A continuation of the previous string"
250 // This line is skipped
253 * In this case, if the parse decides it didn't actually want a string,
254 * and uses lex->line to print an error, it will show the ', foo);' line's
257 * On the other hand, the parser is supposed to remember the line of the next
258 * token's beginning. In this case we would want skipwhite() to be called
259 * AFTER reading a token, so that the parser, before reading the NEXT token,
260 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
263 * here is to store the line of the first character after skipping
264 * the initial whitespace in lex->sline, this happens in lex_do.
266 static int lex_skipwhite(lex_file *lex)
273 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
279 /* one line comment */
282 /* check for special: '/', '/', '*', '/' */
291 while (ch != EOF && ch != '\n') {
298 /* multiline comment */
310 if (ch == '/') /* allow *//* direct following comment */
312 lex_ungetch(lex, ch);
313 ch = ' '; /* cause TRUE in the isspace check */
317 /* Otherwise roll back to the slash and break out of the loop */
318 lex_ungetch(lex, ch);
322 } while (ch != EOF && isspace(ch));
327 /* Append a character to the token buffer */
328 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
330 if (!token_value_add(lex->tok, ch)) {
331 lexerror(lex, "out of memory");
337 /* Append a trailing null-byte */
338 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
340 if (!token_value_add(lex->tok, 0)) {
341 lexerror(lex, "out of memory");
344 lex->tok->value_count--;
349 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
354 while (ch != EOF && isident(ch))
356 if (!lex_tokench(lex, ch))
357 return (lex->tok->ttype = TOKEN_FATAL);
361 /* last ch was not an ident ch: */
362 lex_ungetch(lex, ch);
367 /* read one ident for the frame list */
368 static int lex_parse_frame(lex_file *lex)
373 token_delete(lex->tok);
374 lex->tok = token_new();
377 while (ch != EOF && ch != '\n' && isspace(ch))
383 if (!isident_start(ch)) {
384 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
388 if (!lex_tokench(lex, ch))
390 if (!lex_finish_ident(lex))
392 if (!lex_endtoken(lex))
397 /* read a list of $frames */
398 static bool lex_finish_frames(lex_file *lex)
404 rc = lex_parse_frame(lex);
405 if (rc > 0) /* end of line */
407 if (rc < 0) /* error */
410 m.value = lex->framevalue++;
411 m.name = lex->tok->value;
412 lex->tok->value = NULL;
413 if (!lex_file_frames_add(lex, m)) {
414 lexerror(lex, "out of memory");
420 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
428 return TOKEN_STRINGCONST;
433 lexerror(lex, "unexpected end of file");
434 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
435 return (lex->tok->ttype = TOKEN_ERROR);
440 case 'a': ch = '\a'; break;
441 case 'b': ch = '\b'; break;
442 case 'r': ch = '\r'; break;
443 case 'n': ch = '\n'; break;
444 case 't': ch = '\t'; break;
445 case 'f': ch = '\f'; break;
446 case 'v': ch = '\v'; break;
448 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
449 /* so we just add the character plus backslash no matter what it actually is */
450 if (!lex_tokench(lex, '\\'))
451 return (lex->tok->ttype = TOKEN_FATAL);
453 /* add the character finally */
454 if (!lex_tokench(lex, ch))
455 return (lex->tok->ttype = TOKEN_FATAL);
457 else if (!lex_tokench(lex, ch))
458 return (lex->tok->ttype = TOKEN_FATAL);
460 lexerror(lex, "unexpected end of file within string constant");
461 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
462 return (lex->tok->ttype = TOKEN_ERROR);
465 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
471 /* parse a number... */
472 lex->tok->ttype = TOKEN_INTCONST;
474 if (!lex_tokench(lex, ch))
475 return (lex->tok->ttype = TOKEN_FATAL);
478 if (ch != '.' && !isdigit(ch))
480 if (lastch != '0' || ch != 'x')
482 /* end of the number or EOF */
483 lex_ungetch(lex, ch);
484 if (!lex_endtoken(lex))
485 return (lex->tok->ttype = TOKEN_FATAL);
487 lex->tok->constval.i = lastch - '0';
488 return lex->tok->ttype;
494 /* EOF would have been caught above */
498 if (!lex_tokench(lex, ch))
499 return (lex->tok->ttype = TOKEN_FATAL);
501 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
503 if (!lex_tokench(lex, ch))
504 return (lex->tok->ttype = TOKEN_FATAL);
508 /* NOT else, '.' can come from above as well */
509 if (ch == '.' && !ishex)
511 /* Allow floating comma in non-hex mode */
512 lex->tok->ttype = TOKEN_FLOATCONST;
513 if (!lex_tokench(lex, ch))
514 return (lex->tok->ttype = TOKEN_FATAL);
516 /* continue digits-only */
520 if (!lex_tokench(lex, ch))
521 return (lex->tok->ttype = TOKEN_FATAL);
525 /* put back the last character */
526 /* but do not put back the trailing 'f' or a float */
527 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
530 /* generally we don't want words to follow numbers: */
532 lexerror(lex, "unexpected trailing characters after number");
533 return (lex->tok->ttype = TOKEN_ERROR);
535 lex_ungetch(lex, ch);
537 if (!lex_endtoken(lex))
538 return (lex->tok->ttype = TOKEN_FATAL);
539 if (lex->tok->ttype == TOKEN_FLOATCONST)
540 lex->tok->constval.f = strtod(lex->tok->value, NULL);
542 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
543 return lex->tok->ttype;
546 int lex_do(lex_file *lex)
551 token_delete(lex->tok);
552 lex->tok = token_new();
556 ch = lex_skipwhite(lex);
557 lex->sline = lex->line;
558 lex->tok->ctx.line = lex->sline;
559 lex->tok->ctx.file = lex->name;
562 return (lex->tok->ttype = TOKEN_FATAL);
566 return (lex->tok->ttype = TOKEN_EOF);
569 /* modelgen / spiritgen commands */
575 if (!isident_start(ch)) {
576 lexerror(lex, "hanging '$' modelgen/spritegen command line");
579 if (!lex_tokench(lex, ch))
580 return (lex->tok->ttype = TOKEN_FATAL);
581 if (!lex_finish_ident(lex))
582 return (lex->tok->ttype = TOKEN_ERROR);
583 if (!lex_endtoken(lex))
584 return (lex->tok->ttype = TOKEN_FATAL);
585 /* skip the known commands */
588 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
590 /* frame/framesave command works like an enum
591 * similar to fteqcc we handle this in the lexer.
592 * The reason for this is that it is sensitive to newlines,
593 * which the parser is unaware of
595 if (!lex_finish_frames(lex))
596 return (lex->tok->ttype = TOKEN_ERROR);
600 if (!strcmp(v, "framevalue"))
603 while (ch != EOF && isspace(ch) && ch != '\n')
607 lexerror(lex, "$framevalue requires an integer parameter");
611 token_delete(lex->tok);
612 lex->tok = token_new();
613 lex->tok->ttype = lex_finish_digit(lex, ch);
614 if (!lex_endtoken(lex))
615 return (lex->tok->ttype = TOKEN_FATAL);
616 if (lex->tok->ttype != TOKEN_INTCONST) {
617 lexerror(lex, "$framevalue requires an integer parameter");
620 lex->framevalue = lex->tok->constval.i;
624 if (!strcmp(v, "framerestore"))
628 token_delete(lex->tok);
629 lex->tok = token_new();
631 rc = lex_parse_frame(lex);
634 lexerror(lex, "$framerestore requires a framename parameter");
638 return (lex->tok->ttype = TOKEN_FATAL);
641 for (frame = 0; frame < lex->frames_count; ++frame) {
642 if (!strcmp(v, lex->frames[frame].name)) {
643 lex->framevalue = lex->frames[frame].value;
647 lexerror(lex, "unknown framename `%s`", v);
651 if (!strcmp(v, "modelname"))
655 token_delete(lex->tok);
656 lex->tok = token_new();
658 rc = lex_parse_frame(lex);
661 lexerror(lex, "$framerestore requires a framename parameter");
665 return (lex->tok->ttype = TOKEN_FATAL);
668 if (lex->modelname) {
670 m.value = lex->framevalue;
671 m.name = lex->modelname;
672 lex->modelname = NULL;
673 if (!lex_file_frames_add(lex, m)) {
674 lexerror(lex, "out of memory");
675 return (lex->tok->ttype = TOKEN_FATAL);
678 lex->modelname = lex->tok->value;
679 lex->tok->value = NULL;
680 for (frame = 0; frame < lex->frames_count; ++frame) {
681 if (!strcmp(v, lex->frames[frame].name)) {
682 lex->framevalue = lex->frames[frame].value;
689 if (!strcmp(v, "flush"))
692 for (frame = 0; frame < lex->frames_count; ++frame)
693 mem_d(lex->frames[frame].name);
694 MEM_VECTOR_CLEAR(lex, frames);
695 /* skip line (fteqcc does it too) */
697 while (ch != EOF && ch != '\n')
702 if (!strcmp(v, "cd") ||
703 !strcmp(v, "origin") ||
704 !strcmp(v, "base") ||
705 !strcmp(v, "flags") ||
706 !strcmp(v, "scale") ||
711 while (ch != EOF && ch != '\n')
716 for (frame = 0; frame < lex->frames_count; ++frame) {
717 if (!strcmp(v, lex->frames[frame].name)) {
718 lex->tok->constval.i = lex->frames[frame].value;
719 return (lex->tok->ttype = TOKEN_INTCONST);
723 lexerror(lex, "invalid frame macro");
727 /* single-character tokens */
731 if (!lex_tokench(lex, ch) ||
734 return (lex->tok->ttype = TOKEN_FATAL);
736 if (lex->flags.noops)
737 return (lex->tok->ttype = ch);
739 return (lex->tok->ttype = TOKEN_OPERATOR);
748 if (!lex_tokench(lex, ch) ||
751 return (lex->tok->ttype = TOKEN_FATAL);
753 return (lex->tok->ttype = ch);
758 if (lex->flags.noops)
760 /* Detect characters early which are normally
761 * operators OR PART of an operator.
779 if (!lex_tokench(lex, ch) ||
782 return (lex->tok->ttype = TOKEN_FATAL);
784 return (lex->tok->ttype = ch);
790 if (ch == ',' || ch == '.') {
791 if (!lex_tokench(lex, ch) ||
794 return (lex->tok->ttype = TOKEN_FATAL);
796 return (lex->tok->ttype = TOKEN_OPERATOR);
799 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
800 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
801 ch == '=' || ch == '!' || /* ==, != */
802 ch == '&' || ch == '|') /* &&, ||, &=, |= */
804 if (!lex_tokench(lex, ch))
805 return (lex->tok->ttype = TOKEN_FATAL);
807 nextch = lex_getch(lex);
808 if (nextch == ch || nextch == '=') {
809 if (!lex_tokench(lex, nextch))
810 return (lex->tok->ttype = TOKEN_FATAL);
811 } else if (ch == '-' && nextch == '>') {
812 if (!lex_tokench(lex, nextch))
813 return (lex->tok->ttype = TOKEN_FATAL);
815 lex_ungetch(lex, nextch);
817 if (!lex_endtoken(lex))
818 return (lex->tok->ttype = TOKEN_FATAL);
819 return (lex->tok->ttype = TOKEN_OPERATOR);
823 if (ch == '^' || ch == '~' || ch == '!')
825 if (!lex_tokench(lex, ch) ||
828 return (lex->tok->ttype = TOKEN_FATAL);
830 return (lex->tok->ttype = TOKEN_OPERATOR);
834 if (ch == '*' || ch == '/') /* *=, /= */
836 if (!lex_tokench(lex, ch))
837 return (lex->tok->ttype = TOKEN_FATAL);
839 nextch = lex_getch(lex);
841 if (!lex_tokench(lex, nextch))
842 return (lex->tok->ttype = TOKEN_FATAL);
844 lex_ungetch(lex, nextch);
846 if (!lex_endtoken(lex))
847 return (lex->tok->ttype = TOKEN_FATAL);
848 return (lex->tok->ttype = TOKEN_OPERATOR);
851 if (isident_start(ch))
855 if (!lex_tokench(lex, ch))
856 return (lex->tok->ttype = TOKEN_FATAL);
857 if (!lex_finish_ident(lex)) {
859 return (lex->tok->ttype = TOKEN_ERROR);
861 if (!lex_endtoken(lex))
862 return (lex->tok->ttype = TOKEN_FATAL);
863 lex->tok->ttype = TOKEN_IDENT;
866 if (!strcmp(v, "void")) {
867 lex->tok->ttype = TOKEN_TYPENAME;
868 lex->tok->constval.t = TYPE_VOID;
869 } else if (!strcmp(v, "int")) {
870 lex->tok->ttype = TOKEN_TYPENAME;
871 lex->tok->constval.t = TYPE_INTEGER;
872 } else if (!strcmp(v, "float")) {
873 lex->tok->ttype = TOKEN_TYPENAME;
874 lex->tok->constval.t = TYPE_FLOAT;
875 } else if (!strcmp(v, "string")) {
876 lex->tok->ttype = TOKEN_TYPENAME;
877 lex->tok->constval.t = TYPE_STRING;
878 } else if (!strcmp(v, "entity")) {
879 lex->tok->ttype = TOKEN_TYPENAME;
880 lex->tok->constval.t = TYPE_ENTITY;
881 } else if (!strcmp(v, "vector")) {
882 lex->tok->ttype = TOKEN_TYPENAME;
883 lex->tok->constval.t = TYPE_VECTOR;
884 } else if (!strcmp(v, "for") ||
885 !strcmp(v, "while") ||
888 !strcmp(v, "else") ||
889 !strcmp(v, "local") ||
890 !strcmp(v, "return") ||
892 lex->tok->ttype = TOKEN_KEYWORD;
894 return lex->tok->ttype;
899 lex->tok->ttype = lex_finish_string(lex, '"');
900 while (lex->tok->ttype == TOKEN_STRINGCONST)
902 /* Allow c style "string" "continuation" */
903 ch = lex_skipwhite(lex);
905 lex_ungetch(lex, ch);
909 lex->tok->ttype = lex_finish_string(lex, '"');
911 if (!lex_endtoken(lex))
912 return (lex->tok->ttype = TOKEN_FATAL);
913 return lex->tok->ttype;
918 /* we parse character constants like string,
919 * but return TOKEN_CHARCONST, or a vector type if it fits...
920 * Likewise actual unescaping has to be done by the parser.
921 * The difference is we don't allow 'char' 'continuation'.
923 lex->tok->ttype = lex_finish_string(lex, '\'');
924 if (!lex_endtoken(lex))
925 return (lex->tok->ttype = TOKEN_FATAL);
927 /* It's a vector if we can successfully scan 3 floats */
929 if (sscanf_s(lex->tok->value, " %f %f %f ",
930 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
932 if (sscanf(lex->tok->value, " %f %f %f ",
933 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
936 lex->tok->ttype = TOKEN_VECTORCONST;
939 return lex->tok->ttype;
944 lex->tok->ttype = lex_finish_digit(lex, ch);
945 if (!lex_endtoken(lex))
946 return (lex->tok->ttype = TOKEN_FATAL);
947 return lex->tok->ttype;
950 lexerror(lex, "unknown token");
951 return (lex->tok->ttype = TOKEN_ERROR);