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)
156 token_delete(lex->tok);
161 /* Get or put-back data
162 * The following to functions do NOT understand what kind of data they
164 * The are merely wrapping get/put in order to count line numbers.
166 static int lex_getch(lex_file *lex)
172 if (lex->peek[lex->peekpos] == '\n')
174 return lex->peek[lex->peekpos];
177 ch = fgetc(lex->file);
183 static void lex_ungetch(lex_file *lex, int ch)
185 lex->peek[lex->peekpos++] = ch;
190 /* classify characters
191 * some additions to the is*() functions of ctype.h
194 /* Idents are alphanumberic, but they start with alpha or _ */
195 static bool isident_start(int ch)
197 return isalpha(ch) || ch == '_';
200 static bool isident(int ch)
202 return isident_start(ch) || isdigit(ch);
205 /* isxdigit_only is used when we already know it's not a digit
206 * and want to see if it's a hex digit anyway.
208 static bool isxdigit_only(int ch)
210 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
213 /* Skip whitespace and comments and return the first
214 * non-white character.
215 * As this makes use of the above getch() ungetch() functions,
216 * we don't need to care at all about line numbering anymore.
218 * In theory, this function should only be used at the beginning
219 * of lexing, or when we *know* the next character is part of the token.
220 * Otherwise, if the parser throws an error, the linenumber may not be
221 * the line of the error, but the line of the next token AFTER the error.
223 * This is currently only problematic when using c-like string-continuation,
224 * since comments and whitespaces are allowed between 2 such strings.
228 "A continuation of the previous string"
229 // This line is skipped
232 * In this case, if the parse decides it didn't actually want a string,
233 * and uses lex->line to print an error, it will show the ', foo);' line's
236 * On the other hand, the parser is supposed to remember the line of the next
237 * token's beginning. In this case we would want skipwhite() to be called
238 * AFTER reading a token, so that the parser, before reading the NEXT token,
239 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
242 * here is to store the line of the first character after skipping
243 * the initial whitespace in lex->sline, this happens in lex_do.
245 static int lex_skipwhite(lex_file *lex)
252 while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
258 /* one line comment */
261 /* check for special: '/', '/', '*', '/' */
270 while (ch != EOF && ch != '\n') {
277 /* multiline comment */
289 if (ch == '/') /* allow *//* direct following comment */
291 lex_ungetch(lex, ch);
292 ch = ' '; /* cause TRUE in the isspace check */
296 /* Otherwise roll back to the slash and break out of the loop */
297 lex_ungetch(lex, ch);
301 } while (ch != EOF && isspace(ch));
306 /* Append a character to the token buffer */
307 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
309 if (!token_value_add(lex->tok, ch)) {
310 lexerror(lex, "out of memory");
316 /* Append a trailing null-byte */
317 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
319 if (!token_value_add(lex->tok, 0)) {
320 lexerror(lex, "out of memory");
323 lex->tok->value_count--;
328 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
333 while (ch != EOF && isident(ch))
335 if (!lex_tokench(lex, ch))
336 return (lex->tok->ttype = TOKEN_FATAL);
340 /* last ch was not an ident ch: */
341 lex_ungetch(lex, ch);
346 /* read one ident for the frame list */
347 static int lex_parse_frame(lex_file *lex)
352 token_delete(lex->tok);
353 lex->tok = token_new();
356 while (ch != EOF && ch != '\n' && isspace(ch))
362 if (!isident_start(ch)) {
363 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
367 if (!lex_tokench(lex, ch))
369 if (!lex_finish_ident(lex))
371 if (!lex_endtoken(lex))
376 /* read a list of $frames */
377 static bool lex_finish_frames(lex_file *lex)
383 rc = lex_parse_frame(lex);
384 if (rc > 0) /* end of line */
386 if (rc < 0) /* error */
389 m.value = lex->framevalue++;
390 m.name = util_strdup(lex->tok->value);
391 if (!m.name || !lex_file_frames_add(lex, m)) {
392 lexerror(lex, "out of memory");
398 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
406 return TOKEN_STRINGCONST;
411 lexerror(lex, "unexpected end of file");
412 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
413 return (lex->tok->ttype = TOKEN_ERROR);
418 case 'a': ch = '\a'; break;
419 case 'b': ch = '\b'; break;
420 case 'r': ch = '\r'; break;
421 case 'n': ch = '\n'; break;
422 case 't': ch = '\t'; break;
423 case 'f': ch = '\f'; break;
424 case 'v': ch = '\v'; break;
426 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
427 /* so we just add the character plus backslash no matter what it actually is */
428 if (!lex_tokench(lex, '\\'))
429 return (lex->tok->ttype = TOKEN_FATAL);
431 /* add the character finally */
432 if (!lex_tokench(lex, ch))
433 return (lex->tok->ttype = TOKEN_FATAL);
435 else if (!lex_tokench(lex, ch))
436 return (lex->tok->ttype = TOKEN_FATAL);
438 lexerror(lex, "unexpected end of file within string constant");
439 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
440 return (lex->tok->ttype = TOKEN_ERROR);
443 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
449 /* parse a number... */
450 lex->tok->ttype = TOKEN_INTCONST;
452 if (!lex_tokench(lex, ch))
453 return (lex->tok->ttype = TOKEN_FATAL);
456 if (ch != '.' && !isdigit(ch))
458 if (lastch != '0' || ch != 'x')
460 /* end of the number or EOF */
461 lex_ungetch(lex, ch);
462 if (!lex_endtoken(lex))
463 return (lex->tok->ttype = TOKEN_FATAL);
465 lex->tok->constval.i = lastch - '0';
466 return lex->tok->ttype;
472 /* EOF would have been caught above */
476 if (!lex_tokench(lex, ch))
477 return (lex->tok->ttype = TOKEN_FATAL);
479 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
481 if (!lex_tokench(lex, ch))
482 return (lex->tok->ttype = TOKEN_FATAL);
486 /* NOT else, '.' can come from above as well */
487 if (ch == '.' && !ishex)
489 /* Allow floating comma in non-hex mode */
490 lex->tok->ttype = TOKEN_FLOATCONST;
491 if (!lex_tokench(lex, ch))
492 return (lex->tok->ttype = TOKEN_FATAL);
494 /* continue digits-only */
498 if (!lex_tokench(lex, ch))
499 return (lex->tok->ttype = TOKEN_FATAL);
503 /* put back the last character */
504 /* but do not put back the trailing 'f' or a float */
505 if (lex->tok->ttype == TOKEN_FLOATCONST && ch == 'f')
508 /* generally we don't want words to follow numbers: */
510 lexerror(lex, "unexpected trailing characters after number");
511 return (lex->tok->ttype = TOKEN_ERROR);
513 lex_ungetch(lex, ch);
515 if (!lex_endtoken(lex))
516 return (lex->tok->ttype = TOKEN_FATAL);
517 if (lex->tok->ttype == TOKEN_FLOATCONST)
518 lex->tok->constval.f = strtod(lex->tok->value, NULL);
520 lex->tok->constval.i = strtol(lex->tok->value, NULL, 0);
521 return lex->tok->ttype;
524 int lex_do(lex_file *lex)
529 token_delete(lex->tok);
530 lex->tok = token_new();
534 ch = lex_skipwhite(lex);
535 lex->sline = lex->line;
536 lex->tok->ctx.line = lex->sline;
537 lex->tok->ctx.file = lex->name;
540 return (lex->tok->ttype = TOKEN_EOF);
542 /* modelgen / spiritgen commands */
548 if (!isident_start(ch)) {
549 lexerror(lex, "hanging '$' modelgen/spritegen command line");
552 if (!lex_tokench(lex, ch))
553 return (lex->tok->ttype = TOKEN_FATAL);
554 if (!lex_finish_ident(lex))
555 return (lex->tok->ttype = TOKEN_ERROR);
556 if (!lex_endtoken(lex))
557 return (lex->tok->ttype = TOKEN_FATAL);
558 /* skip the known commands */
561 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
563 /* frame/framesave command works like an enum
564 * similar to fteqcc we handle this in the lexer.
565 * The reason for this is that it is sensitive to newlines,
566 * which the parser is unaware of
568 if (!lex_finish_frames(lex))
569 return (lex->tok->ttype = TOKEN_ERROR);
573 if (!strcmp(v, "framevalue"))
576 while (ch != EOF && isspace(ch) && ch != '\n')
580 lexerror(lex, "$framevalue requires an integer parameter");
584 token_delete(lex->tok);
585 lex->tok = token_new();
586 lex->tok->ttype = lex_finish_digit(lex, ch);
587 if (!lex_endtoken(lex))
588 return (lex->tok->ttype = TOKEN_FATAL);
589 if (lex->tok->ttype != TOKEN_INTCONST) {
590 lexerror(lex, "$framevalue requires an integer parameter");
593 lex->framevalue = lex->tok->constval.i;
597 if (!strcmp(v, "flush"))
600 for (frame = 0; frame < lex->frames_count; ++frame)
601 mem_d(lex->frames[frame].name);
602 MEM_VECTOR_CLEAR(lex, frames);
603 /* skip line (fteqcc does it too) */
605 while (ch != EOF && ch != '\n')
610 if (!strcmp(v, "cd") ||
611 !strcmp(v, "origin") ||
612 !strcmp(v, "base") ||
613 !strcmp(v, "flags") ||
614 !strcmp(v, "scale") ||
619 while (ch != EOF && ch != '\n')
624 for (frame = 0; frame < lex->frames_count; ++frame) {
625 if (!strcmp(v, lex->frames[frame].name)) {
626 lex->tok->constval.i = lex->frames[frame].value;
627 return (lex->tok->ttype = TOKEN_INTCONST);
631 lexerror(lex, "invalid frame macro");
635 /* single-character tokens */
647 if (!lex_tokench(lex, ch) ||
650 return (lex->tok->ttype = TOKEN_FATAL);
652 return (lex->tok->ttype = ch);
657 if (lex->flags.noops)
659 /* Detect characters early which are normally
660 * operators OR PART of an operator.
678 if (!lex_tokench(lex, ch) ||
681 return (lex->tok->ttype = TOKEN_FATAL);
683 return (lex->tok->ttype = ch);
689 if (ch == ',' || ch == '.') {
690 if (!lex_tokench(lex, ch) ||
693 return (lex->tok->ttype = TOKEN_FATAL);
695 return (lex->tok->ttype = TOKEN_OPERATOR);
698 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
699 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
700 ch == '=' || ch == '!' || /* ==, != */
701 ch == '&' || ch == '|') /* &&, ||, &=, |= */
703 if (!lex_tokench(lex, ch))
704 return (lex->tok->ttype = TOKEN_FATAL);
706 nextch = lex_getch(lex);
707 if (nextch == ch || nextch == '=') {
708 if (!lex_tokench(lex, nextch))
709 return (lex->tok->ttype = TOKEN_FATAL);
710 } else if (ch == '-' && nextch == '>') {
711 if (!lex_tokench(lex, nextch))
712 return (lex->tok->ttype = TOKEN_FATAL);
714 lex_ungetch(lex, nextch);
716 if (!lex_endtoken(lex))
717 return (lex->tok->ttype = TOKEN_FATAL);
718 return (lex->tok->ttype = TOKEN_OPERATOR);
722 if (ch == '^' || ch == '~' || ch == '!')
724 if (!lex_tokench(lex, ch) ||
727 return (lex->tok->ttype = TOKEN_FATAL);
729 return (lex->tok->ttype = TOKEN_OPERATOR);
733 if (ch == '*' || ch == '/') /* *=, /= */
735 if (!lex_tokench(lex, ch))
736 return (lex->tok->ttype = TOKEN_FATAL);
738 nextch = lex_getch(lex);
740 if (!lex_tokench(lex, nextch))
741 return (lex->tok->ttype = TOKEN_FATAL);
743 lex_ungetch(lex, nextch);
745 if (!lex_endtoken(lex))
746 return (lex->tok->ttype = TOKEN_FATAL);
747 return (lex->tok->ttype = TOKEN_OPERATOR);
750 if (isident_start(ch))
754 if (!lex_tokench(lex, ch))
755 return (lex->tok->ttype = TOKEN_FATAL);
756 if (!lex_finish_ident(lex)) {
758 return (lex->tok->ttype = TOKEN_ERROR);
760 if (!lex_endtoken(lex))
761 return (lex->tok->ttype = TOKEN_FATAL);
762 lex->tok->ttype = TOKEN_IDENT;
765 if (!strcmp(v, "void")) {
766 lex->tok->ttype = TOKEN_TYPENAME;
767 lex->tok->constval.t = TYPE_VOID;
768 } else if (!strcmp(v, "int")) {
769 lex->tok->ttype = TOKEN_TYPENAME;
770 lex->tok->constval.t = TYPE_INTEGER;
771 } else if (!strcmp(v, "float")) {
772 lex->tok->ttype = TOKEN_TYPENAME;
773 lex->tok->constval.t = TYPE_FLOAT;
774 } else if (!strcmp(v, "string")) {
775 lex->tok->ttype = TOKEN_TYPENAME;
776 lex->tok->constval.t = TYPE_STRING;
777 } else if (!strcmp(v, "entity")) {
778 lex->tok->ttype = TOKEN_TYPENAME;
779 lex->tok->constval.t = TYPE_ENTITY;
780 } else if (!strcmp(v, "vector")) {
781 lex->tok->ttype = TOKEN_TYPENAME;
782 lex->tok->constval.t = TYPE_VECTOR;
783 } else if (!strcmp(v, "for") ||
784 !strcmp(v, "while") ||
787 !strcmp(v, "else") ||
788 !strcmp(v, "local") ||
789 !strcmp(v, "return") ||
791 lex->tok->ttype = TOKEN_KEYWORD;
793 return lex->tok->ttype;
798 lex->tok->ttype = lex_finish_string(lex, '"');
799 while (lex->tok->ttype == TOKEN_STRINGCONST)
801 /* Allow c style "string" "continuation" */
802 ch = lex_skipwhite(lex);
804 lex_ungetch(lex, ch);
808 lex->tok->ttype = lex_finish_string(lex, '"');
810 if (!lex_endtoken(lex))
811 return (lex->tok->ttype = TOKEN_FATAL);
812 return lex->tok->ttype;
817 /* we parse character constants like string,
818 * but return TOKEN_CHARCONST, or a vector type if it fits...
819 * Likewise actual unescaping has to be done by the parser.
820 * The difference is we don't allow 'char' 'continuation'.
822 lex->tok->ttype = lex_finish_string(lex, '\'');
823 if (!lex_endtoken(lex))
824 return (lex->tok->ttype = TOKEN_FATAL);
826 /* It's a vector if we can successfully scan 3 floats */
828 if (sscanf_s(lex->tok->value, " %f %f %f ",
829 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
831 if (sscanf(lex->tok->value, " %f %f %f ",
832 &lex->tok->constval.v.x, &lex->tok->constval.v.y, &lex->tok->constval.v.z) == 3)
835 lex->tok->ttype = TOKEN_VECTORCONST;
838 return lex->tok->ttype;
843 lex->tok->ttype = lex_finish_digit(lex, ch);
844 if (!lex_endtoken(lex))
845 return (lex->tok->ttype = TOKEN_FATAL);
846 return lex->tok->ttype;
849 lexerror(lex, "unknown token");
850 return (lex->tok->ttype = TOKEN_ERROR);