11 void lexerror(lex_file *lex, const char *fmt, ...)
16 con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
20 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
23 int lvl = LVL_WARNING;
25 if (!OPTS_WARN(warntype))
32 con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
42 token *tok = (token*)mem_a(sizeof(token));
45 memset(tok, 0, sizeof(*tok));
49 void token_delete(token *self)
51 if (self->next && self->next->prev == self)
52 self->next->prev = self->prev;
53 if (self->prev && self->prev->next == self)
54 self->prev->next = self->next;
55 MEM_VECTOR_CLEAR(self, value);
59 token* token_copy(const token *cp)
61 token* self = token_new();
65 self->value_alloc = cp->value_count + 1;
66 self->value_count = cp->value_count;
67 self->value = (char*)mem_a(self->value_alloc);
72 memcpy(self->value, cp->value, cp->value_count);
73 self->value[self->value_alloc-1] = 0;
77 self->ttype = cp->ttype;
78 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
82 void token_delete_all(token *t)
93 token* token_copy_all(const token *cp)
98 out = cur = token_copy(cp);
104 cur->next = token_copy(cp);
106 token_delete_all(out);
109 cur->next->prev = cur;
116 static void lex_token_new(lex_file *lex)
120 token_delete(lex->tok);
121 lex->tok = token_new();
124 vec_shrinkto(lex->tok.value, 0);
125 lex->tok.constval.t = 0;
126 lex->tok.ctx.line = lex->sline;
127 lex->tok.ctx.file = lex->name;
132 lex_file* lex_open(const char *file)
135 FILE *in = util_fopen(file, "rb");
138 lexerror(NULL, "open failed: '%s'\n", file);
142 lex = (lex_file*)mem_a(sizeof(*lex));
145 lexerror(NULL, "out of memory\n");
149 memset(lex, 0, sizeof(*lex));
152 lex->name = util_strdup(file);
153 lex->line = 1; /* we start counting at 1 */
158 vec_push(lex_filenames, lex->name);
162 lex_file* lex_open_string(const char *str, size_t len, const char *name)
166 lex = (lex_file*)mem_a(sizeof(*lex));
168 lexerror(NULL, "out of memory\n");
172 memset(lex, 0, sizeof(*lex));
175 lex->open_string = str;
176 lex->open_string_length = len;
177 lex->open_string_pos = 0;
179 lex->name = util_strdup(name ? name : "<string-source>");
180 lex->line = 1; /* we start counting at 1 */
185 vec_push(lex_filenames, lex->name);
190 void lex_cleanup(void)
193 for (i = 0; i < vec_size(lex_filenames); ++i)
194 mem_d(lex_filenames[i]);
195 vec_free(lex_filenames);
198 void lex_close(lex_file *lex)
201 for (i = 0; i < vec_size(lex->frames); ++i)
202 mem_d(lex->frames[i].name);
203 vec_free(lex->frames);
206 vec_free(lex->modelname);
212 token_delete(lex->tok);
214 vec_free(lex->tok.value);
216 /* mem_d(lex->name); collected in lex_filenames */
220 static int lex_fgetc(lex_file *lex)
223 return fgetc(lex->file);
224 if (lex->open_string) {
225 if (lex->open_string_pos >= lex->open_string_length)
227 return lex->open_string[lex->open_string_pos++];
232 /* Get or put-back data
233 * The following to functions do NOT understand what kind of data they
235 * The are merely wrapping get/put in order to count line numbers.
237 static void lex_ungetch(lex_file *lex, int ch);
238 static int lex_try_trigraph(lex_file *lex, int old)
243 lex_ungetch(lex, c2);
249 case '=': return '#';
250 case '/': return '\\';
251 case '\'': return '^';
252 case '(': return '[';
253 case ')': return ']';
254 case '!': return '|';
255 case '<': return '{';
256 case '>': return '}';
257 case '-': return '~';
259 lex_ungetch(lex, c3);
260 lex_ungetch(lex, c2);
265 static int lex_try_digraph(lex_file *lex, int ch)
269 if (ch == '<' && c2 == ':')
271 else if (ch == ':' && c2 == '>')
273 else if (ch == '<' && c2 == '%')
275 else if (ch == '%' && c2 == '>')
277 else if (ch == '%' && c2 == ':')
279 lex_ungetch(lex, c2);
283 static int lex_getch(lex_file *lex)
289 if (lex->peek[lex->peekpos] == '\n')
291 return lex->peek[lex->peekpos];
298 return lex_try_trigraph(lex, ch);
299 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
300 return lex_try_digraph(lex, ch);
304 static void lex_ungetch(lex_file *lex, int ch)
306 lex->peek[lex->peekpos++] = ch;
311 /* classify characters
312 * some additions to the is*() functions of ctype.h
315 /* Idents are alphanumberic, but they start with alpha or _ */
316 static bool isident_start(int ch)
318 return isalpha(ch) || ch == '_';
321 static bool isident(int ch)
323 return isident_start(ch) || isdigit(ch);
326 /* isxdigit_only is used when we already know it's not a digit
327 * and want to see if it's a hex digit anyway.
329 static bool isxdigit_only(int ch)
331 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
334 /* Append a character to the token buffer */
335 static void lex_tokench(lex_file *lex, int ch)
337 vec_push(lex->tok.value, ch);
340 /* Append a trailing null-byte */
341 static void lex_endtoken(lex_file *lex)
343 vec_push(lex->tok.value, 0);
344 vec_shrinkby(lex->tok.value, 1);
347 /* Skip whitespace and comments and return the first
348 * non-white character.
349 * As this makes use of the above getch() ungetch() functions,
350 * we don't need to care at all about line numbering anymore.
352 * In theory, this function should only be used at the beginning
353 * of lexing, or when we *know* the next character is part of the token.
354 * Otherwise, if the parser throws an error, the linenumber may not be
355 * the line of the error, but the line of the next token AFTER the error.
357 * This is currently only problematic when using c-like string-continuation,
358 * since comments and whitespaces are allowed between 2 such strings.
362 "A continuation of the previous string"
363 // This line is skipped
366 * In this case, if the parse decides it didn't actually want a string,
367 * and uses lex->line to print an error, it will show the ', foo);' line's
370 * On the other hand, the parser is supposed to remember the line of the next
371 * token's beginning. In this case we would want skipwhite() to be called
372 * AFTER reading a token, so that the parser, before reading the NEXT token,
373 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
376 * here is to store the line of the first character after skipping
377 * the initial whitespace in lex->sline, this happens in lex_do.
379 static int lex_skipwhite(lex_file *lex)
382 bool haswhite = false;
387 while (ch != EOF && isspace(ch)) {
388 if (lex->flags.preprocessing) {
391 /* see if there was whitespace first */
392 if (haswhite) { /* (vec_size(lex->tok.value)) { */
393 lex_ungetch(lex, ch);
397 /* otherwise return EOL */
401 lex_tokench(lex, ch);
410 /* one line comment */
413 if (lex->flags.preprocessing) {
415 lex_tokench(lex, '/');
416 lex_tokench(lex, '/');
419 while (ch != EOF && ch != '\n') {
420 if (lex->flags.preprocessing)
421 lex_tokench(lex, ch);
424 if (lex->flags.preprocessing) {
425 lex_ungetch(lex, '\n');
433 /* multiline comment */
434 if (lex->flags.preprocessing) {
436 lex_tokench(lex, '/');
437 lex_tokench(lex, '*');
446 if (lex->flags.preprocessing) {
447 lex_tokench(lex, '*');
448 lex_tokench(lex, '/');
453 if (lex->flags.preprocessing) {
454 lex_tokench(lex, ch);
457 ch = ' '; /* cause TRUE in the isspace check */
460 /* Otherwise roll back to the slash and break out of the loop */
461 lex_ungetch(lex, ch);
465 } while (ch != EOF && isspace(ch));
469 lex_ungetch(lex, ch);
476 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
481 while (ch != EOF && isident(ch))
483 lex_tokench(lex, ch);
487 /* last ch was not an ident ch: */
488 lex_ungetch(lex, ch);
493 /* read one ident for the frame list */
494 static int lex_parse_frame(lex_file *lex)
501 while (ch != EOF && ch != '\n' && isspace(ch))
507 if (!isident_start(ch)) {
508 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
512 lex_tokench(lex, ch);
513 if (!lex_finish_ident(lex))
519 /* read a list of $frames */
520 static bool lex_finish_frames(lex_file *lex)
527 rc = lex_parse_frame(lex);
528 if (rc > 0) /* end of line */
530 if (rc < 0) /* error */
533 for (i = 0; i < vec_size(lex->frames); ++i) {
534 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
535 lex->frames[i].value = lex->framevalue++;
536 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
541 if (i < vec_size(lex->frames))
544 m.value = lex->framevalue++;
545 m.name = util_strdup(lex->tok.value);
546 vec_shrinkto(lex->tok.value, 0);
547 vec_push(lex->frames, m);
551 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
559 return TOKEN_STRINGCONST;
561 if (!lex->flags.preprocessing && ch == '\\') {
564 lexerror(lex, "unexpected end of file");
565 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
566 return (lex->tok.ttype = TOKEN_ERROR);
571 case 'a': ch = '\a'; break;
572 case 'b': ch = '\b'; break;
573 case 'r': ch = '\r'; break;
574 case 'n': ch = '\n'; break;
575 case 't': ch = '\t'; break;
576 case 'f': ch = '\f'; break;
577 case 'v': ch = '\v'; break;
579 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
580 /* so we just add the character plus backslash no matter what it actually is */
581 lex_tokench(lex, '\\');
583 /* add the character finally */
584 lex_tokench(lex, ch);
587 lex_tokench(lex, ch);
589 lexerror(lex, "unexpected end of file within string constant");
590 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
591 return (lex->tok.ttype = TOKEN_ERROR);
594 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
600 /* parse a number... */
601 lex->tok.ttype = TOKEN_INTCONST;
603 lex_tokench(lex, ch);
606 if (ch != '.' && !isdigit(ch))
608 if (lastch != '0' || ch != 'x')
610 /* end of the number or EOF */
611 lex_ungetch(lex, ch);
614 lex->tok.constval.i = lastch - '0';
615 return lex->tok.ttype;
621 /* EOF would have been caught above */
625 lex_tokench(lex, ch);
627 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
629 lex_tokench(lex, ch);
633 /* NOT else, '.' can come from above as well */
634 if (ch == '.' && !ishex)
636 /* Allow floating comma in non-hex mode */
637 lex->tok.ttype = TOKEN_FLOATCONST;
638 lex_tokench(lex, ch);
640 /* continue digits-only */
644 lex_tokench(lex, ch);
648 /* put back the last character */
649 /* but do not put back the trailing 'f' or a float */
650 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
653 /* generally we don't want words to follow numbers: */
655 lexerror(lex, "unexpected trailing characters after number");
656 return (lex->tok.ttype = TOKEN_ERROR);
658 lex_ungetch(lex, ch);
661 if (lex->tok.ttype == TOKEN_FLOATCONST)
662 lex->tok.constval.f = strtod(lex->tok.value, NULL);
664 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
665 return lex->tok.ttype;
668 int lex_do(lex_file *lex)
678 ch = lex_skipwhite(lex);
679 lex->sline = lex->line;
680 lex->tok.ctx.line = lex->sline;
681 lex->tok.ctx.file = lex->name;
683 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
684 return (lex->tok.ttype = ch);
688 return (lex->tok.ttype = TOKEN_FATAL);
692 return (lex->tok.ttype = TOKEN_EOF);
695 /* modelgen / spiritgen commands */
701 if (!isident_start(ch)) {
702 lexerror(lex, "hanging '$' modelgen/spritegen command line");
705 lex_tokench(lex, ch);
706 if (!lex_finish_ident(lex))
707 return (lex->tok.ttype = TOKEN_ERROR);
709 /* skip the known commands */
712 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
714 /* frame/framesave command works like an enum
715 * similar to fteqcc we handle this in the lexer.
716 * The reason for this is that it is sensitive to newlines,
717 * which the parser is unaware of
719 if (!lex_finish_frames(lex))
720 return (lex->tok.ttype = TOKEN_ERROR);
724 if (!strcmp(v, "framevalue"))
727 while (ch != EOF && isspace(ch) && ch != '\n')
731 lexerror(lex, "$framevalue requires an integer parameter");
736 lex->tok.ttype = lex_finish_digit(lex, ch);
738 if (lex->tok.ttype != TOKEN_INTCONST) {
739 lexerror(lex, "$framevalue requires an integer parameter");
742 lex->framevalue = lex->tok.constval.i;
746 if (!strcmp(v, "framerestore"))
752 rc = lex_parse_frame(lex);
755 lexerror(lex, "$framerestore requires a framename parameter");
759 return (lex->tok.ttype = TOKEN_FATAL);
762 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
763 if (!strcmp(v, lex->frames[frame].name)) {
764 lex->framevalue = lex->frames[frame].value;
768 lexerror(lex, "unknown framename `%s`", v);
772 if (!strcmp(v, "modelname"))
778 rc = lex_parse_frame(lex);
781 lexerror(lex, "$modelname requires a parameter");
785 return (lex->tok.ttype = TOKEN_FATAL);
788 if (lex->modelname) {
790 m.value = lex->framevalue;
791 m.name = lex->modelname;
792 lex->modelname = NULL;
793 vec_push(lex->frames, m);
795 lex->modelname = lex->tok.value;
796 lex->tok.value = NULL;
800 if (!strcmp(v, "flush"))
803 for (frame = 0; frame < vec_size(lex->frames); ++frame)
804 mem_d(lex->frames[frame].name);
805 vec_free(lex->frames);
806 /* skip line (fteqcc does it too) */
808 while (ch != EOF && ch != '\n')
813 if (!strcmp(v, "cd") ||
814 !strcmp(v, "origin") ||
815 !strcmp(v, "base") ||
816 !strcmp(v, "flags") ||
817 !strcmp(v, "scale") ||
822 while (ch != EOF && ch != '\n')
827 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
828 if (!strcmp(v, lex->frames[frame].name)) {
829 lex->tok.constval.i = lex->frames[frame].value;
830 return (lex->tok.ttype = TOKEN_INTCONST);
834 lexerror(lex, "invalid frame macro");
838 /* single-character tokens */
843 lex_tokench(lex, ch);
845 if (lex->flags.noops)
846 return (lex->tok.ttype = ch);
848 return (lex->tok.ttype = TOKEN_OPERATOR);
856 lex_tokench(lex, ch);
858 return (lex->tok.ttype = ch);
863 if (lex->flags.noops)
865 /* Detect characters early which are normally
866 * operators OR PART of an operator.
883 lex_tokench(lex, ch);
885 return (lex->tok.ttype = ch);
892 lex_tokench(lex, ch);
893 /* peak ahead once */
894 nextch = lex_getch(lex);
896 lex_ungetch(lex, nextch);
898 return (lex->tok.ttype = ch);
900 /* peak ahead again */
901 nextch = lex_getch(lex);
903 lex_ungetch(lex, nextch);
904 lex_ungetch(lex, nextch);
906 return (lex->tok.ttype = ch);
908 /* fill the token to be "..." */
909 lex_tokench(lex, ch);
910 lex_tokench(lex, ch);
912 return (lex->tok.ttype = TOKEN_DOTS);
916 if (ch == ',' || ch == '.') {
917 lex_tokench(lex, ch);
919 return (lex->tok.ttype = TOKEN_OPERATOR);
922 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
923 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
924 ch == '=' || ch == '!' || /* ==, != */
925 ch == '&' || ch == '|') /* &&, ||, &=, |= */
927 lex_tokench(lex, ch);
929 nextch = lex_getch(lex);
930 if (nextch == ch || nextch == '=') {
931 lex_tokench(lex, nextch);
932 } else if (ch == '-' && nextch == '>') {
933 lex_tokench(lex, nextch);
935 lex_ungetch(lex, nextch);
938 return (lex->tok.ttype = TOKEN_OPERATOR);
942 if (ch == '^' || ch == '~' || ch == '!')
944 lex_tokench(lex, ch);
946 return (lex->tok.ttype = TOKEN_OPERATOR);
950 if (ch == '*' || ch == '/') /* *=, /= */
952 lex_tokench(lex, ch);
954 nextch = lex_getch(lex);
956 lex_tokench(lex, nextch);
958 lex_ungetch(lex, nextch);
961 return (lex->tok.ttype = TOKEN_OPERATOR);
964 if (isident_start(ch))
968 lex_tokench(lex, ch);
969 if (!lex_finish_ident(lex)) {
971 return (lex->tok.ttype = TOKEN_ERROR);
974 lex->tok.ttype = TOKEN_IDENT;
977 if (!strcmp(v, "void")) {
978 lex->tok.ttype = TOKEN_TYPENAME;
979 lex->tok.constval.t = TYPE_VOID;
980 } else if (!strcmp(v, "int")) {
981 lex->tok.ttype = TOKEN_TYPENAME;
982 lex->tok.constval.t = TYPE_INTEGER;
983 } else if (!strcmp(v, "float")) {
984 lex->tok.ttype = TOKEN_TYPENAME;
985 lex->tok.constval.t = TYPE_FLOAT;
986 } else if (!strcmp(v, "string")) {
987 lex->tok.ttype = TOKEN_TYPENAME;
988 lex->tok.constval.t = TYPE_STRING;
989 } else if (!strcmp(v, "entity")) {
990 lex->tok.ttype = TOKEN_TYPENAME;
991 lex->tok.constval.t = TYPE_ENTITY;
992 } else if (!strcmp(v, "vector")) {
993 lex->tok.ttype = TOKEN_TYPENAME;
994 lex->tok.constval.t = TYPE_VECTOR;
995 } else if (!strcmp(v, "for") ||
996 !strcmp(v, "while") ||
999 !strcmp(v, "else") ||
1000 !strcmp(v, "local") ||
1001 !strcmp(v, "return") ||
1002 !strcmp(v, "not") ||
1003 !strcmp(v, "const"))
1005 lex->tok.ttype = TOKEN_KEYWORD;
1007 else if (opts_standard != COMPILER_QCC)
1009 /* other standards reserve these keywords */
1010 if (!strcmp(v, "switch") ||
1011 !strcmp(v, "struct") ||
1012 !strcmp(v, "union") ||
1013 !strcmp(v, "break") ||
1014 !strcmp(v, "continue") ||
1017 lex->tok.ttype = TOKEN_KEYWORD;
1021 return lex->tok.ttype;
1026 lex->flags.nodigraphs = true;
1027 if (lex->flags.preprocessing)
1028 lex_tokench(lex, ch);
1029 lex->tok.ttype = lex_finish_string(lex, '"');
1030 if (lex->flags.preprocessing)
1031 lex_tokench(lex, ch);
1032 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1034 /* Allow c style "string" "continuation" */
1035 ch = lex_skipwhite(lex);
1037 lex_ungetch(lex, ch);
1041 lex->tok.ttype = lex_finish_string(lex, '"');
1043 lex->flags.nodigraphs = false;
1045 return lex->tok.ttype;
1050 /* we parse character constants like string,
1051 * but return TOKEN_CHARCONST, or a vector type if it fits...
1052 * Likewise actual unescaping has to be done by the parser.
1053 * The difference is we don't allow 'char' 'continuation'.
1055 if (lex->flags.preprocessing)
1056 lex_tokench(lex, ch);
1057 lex->tok.ttype = lex_finish_string(lex, '\'');
1058 if (lex->flags.preprocessing)
1059 lex_tokench(lex, ch);
1062 /* It's a vector if we can successfully scan 3 floats */
1064 if (sscanf_s(lex->tok.value, " %f %f %f ",
1065 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1067 if (sscanf(lex->tok.value, " %f %f %f ",
1068 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1072 lex->tok.ttype = TOKEN_VECTORCONST;
1075 return lex->tok.ttype;
1080 lex->tok.ttype = lex_finish_digit(lex, ch);
1082 return lex->tok.ttype;
1085 lexerror(lex, "unknown token");
1086 return (lex->tok.ttype = TOKEN_ERROR);