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)
679 ch = lex_skipwhite(lex);
680 if (!lex->flags.mergelines || ch != '\\')
684 lex_ungetch(lex, ch);
688 /* we reached a linemerge */
692 lex->sline = lex->line;
693 lex->tok.ctx.line = lex->sline;
694 lex->tok.ctx.file = lex->name;
696 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
697 return (lex->tok.ttype = ch);
701 return (lex->tok.ttype = TOKEN_FATAL);
705 return (lex->tok.ttype = TOKEN_EOF);
708 /* modelgen / spiritgen commands */
714 if (!isident_start(ch)) {
715 lexerror(lex, "hanging '$' modelgen/spritegen command line");
718 lex_tokench(lex, ch);
719 if (!lex_finish_ident(lex))
720 return (lex->tok.ttype = TOKEN_ERROR);
722 /* skip the known commands */
725 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
727 /* frame/framesave command works like an enum
728 * similar to fteqcc we handle this in the lexer.
729 * The reason for this is that it is sensitive to newlines,
730 * which the parser is unaware of
732 if (!lex_finish_frames(lex))
733 return (lex->tok.ttype = TOKEN_ERROR);
737 if (!strcmp(v, "framevalue"))
740 while (ch != EOF && isspace(ch) && ch != '\n')
744 lexerror(lex, "$framevalue requires an integer parameter");
749 lex->tok.ttype = lex_finish_digit(lex, ch);
751 if (lex->tok.ttype != TOKEN_INTCONST) {
752 lexerror(lex, "$framevalue requires an integer parameter");
755 lex->framevalue = lex->tok.constval.i;
759 if (!strcmp(v, "framerestore"))
765 rc = lex_parse_frame(lex);
768 lexerror(lex, "$framerestore requires a framename parameter");
772 return (lex->tok.ttype = TOKEN_FATAL);
775 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
776 if (!strcmp(v, lex->frames[frame].name)) {
777 lex->framevalue = lex->frames[frame].value;
781 lexerror(lex, "unknown framename `%s`", v);
785 if (!strcmp(v, "modelname"))
791 rc = lex_parse_frame(lex);
794 lexerror(lex, "$modelname requires a parameter");
798 return (lex->tok.ttype = TOKEN_FATAL);
801 if (lex->modelname) {
803 m.value = lex->framevalue;
804 m.name = lex->modelname;
805 lex->modelname = NULL;
806 vec_push(lex->frames, m);
808 lex->modelname = lex->tok.value;
809 lex->tok.value = NULL;
813 if (!strcmp(v, "flush"))
816 for (frame = 0; frame < vec_size(lex->frames); ++frame)
817 mem_d(lex->frames[frame].name);
818 vec_free(lex->frames);
819 /* skip line (fteqcc does it too) */
821 while (ch != EOF && ch != '\n')
826 if (!strcmp(v, "cd") ||
827 !strcmp(v, "origin") ||
828 !strcmp(v, "base") ||
829 !strcmp(v, "flags") ||
830 !strcmp(v, "scale") ||
835 while (ch != EOF && ch != '\n')
840 for (frame = 0; frame < vec_size(lex->frames); ++frame) {
841 if (!strcmp(v, lex->frames[frame].name)) {
842 lex->tok.constval.i = lex->frames[frame].value;
843 return (lex->tok.ttype = TOKEN_INTCONST);
847 lexerror(lex, "invalid frame macro");
851 /* single-character tokens */
856 lex_tokench(lex, ch);
858 if (lex->flags.noops)
859 return (lex->tok.ttype = ch);
861 return (lex->tok.ttype = TOKEN_OPERATOR);
870 lex_tokench(lex, ch);
872 return (lex->tok.ttype = ch);
877 if (lex->flags.noops)
879 /* Detect characters early which are normally
880 * operators OR PART of an operator.
897 lex_tokench(lex, ch);
899 return (lex->tok.ttype = ch);
906 lex_tokench(lex, ch);
907 /* peak ahead once */
908 nextch = lex_getch(lex);
910 lex_ungetch(lex, nextch);
912 return (lex->tok.ttype = ch);
914 /* peak ahead again */
915 nextch = lex_getch(lex);
917 lex_ungetch(lex, nextch);
918 lex_ungetch(lex, nextch);
920 return (lex->tok.ttype = ch);
922 /* fill the token to be "..." */
923 lex_tokench(lex, ch);
924 lex_tokench(lex, ch);
926 return (lex->tok.ttype = TOKEN_DOTS);
930 if (ch == ',' || ch == '.') {
931 lex_tokench(lex, ch);
933 return (lex->tok.ttype = TOKEN_OPERATOR);
936 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
937 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
938 ch == '=' || ch == '!' || /* ==, != */
939 ch == '&' || ch == '|') /* &&, ||, &=, |= */
941 lex_tokench(lex, ch);
943 nextch = lex_getch(lex);
944 if (nextch == ch || nextch == '=') {
945 lex_tokench(lex, nextch);
946 } else if (ch == '-' && nextch == '>') {
947 lex_tokench(lex, nextch);
949 lex_ungetch(lex, nextch);
952 return (lex->tok.ttype = TOKEN_OPERATOR);
956 if (ch == '^' || ch == '~' || ch == '!')
958 lex_tokench(lex, ch);
960 return (lex->tok.ttype = TOKEN_OPERATOR);
964 if (ch == '*' || ch == '/') /* *=, /= */
966 lex_tokench(lex, ch);
968 nextch = lex_getch(lex);
970 lex_tokench(lex, nextch);
972 lex_ungetch(lex, nextch);
975 return (lex->tok.ttype = TOKEN_OPERATOR);
978 if (isident_start(ch))
982 lex_tokench(lex, ch);
983 if (!lex_finish_ident(lex)) {
985 return (lex->tok.ttype = TOKEN_ERROR);
988 lex->tok.ttype = TOKEN_IDENT;
991 if (!strcmp(v, "void")) {
992 lex->tok.ttype = TOKEN_TYPENAME;
993 lex->tok.constval.t = TYPE_VOID;
994 } else if (!strcmp(v, "int")) {
995 lex->tok.ttype = TOKEN_TYPENAME;
996 lex->tok.constval.t = TYPE_INTEGER;
997 } else if (!strcmp(v, "float")) {
998 lex->tok.ttype = TOKEN_TYPENAME;
999 lex->tok.constval.t = TYPE_FLOAT;
1000 } else if (!strcmp(v, "string")) {
1001 lex->tok.ttype = TOKEN_TYPENAME;
1002 lex->tok.constval.t = TYPE_STRING;
1003 } else if (!strcmp(v, "entity")) {
1004 lex->tok.ttype = TOKEN_TYPENAME;
1005 lex->tok.constval.t = TYPE_ENTITY;
1006 } else if (!strcmp(v, "vector")) {
1007 lex->tok.ttype = TOKEN_TYPENAME;
1008 lex->tok.constval.t = TYPE_VECTOR;
1009 } else if (!strcmp(v, "for") ||
1010 !strcmp(v, "while") ||
1013 !strcmp(v, "else") ||
1014 !strcmp(v, "local") ||
1015 !strcmp(v, "return") ||
1016 !strcmp(v, "not") ||
1017 !strcmp(v, "const"))
1019 lex->tok.ttype = TOKEN_KEYWORD;
1021 else if (opts_standard != COMPILER_QCC)
1023 /* other standards reserve these keywords */
1024 if (!strcmp(v, "switch") ||
1025 !strcmp(v, "struct") ||
1026 !strcmp(v, "union") ||
1027 !strcmp(v, "break") ||
1028 !strcmp(v, "continue") ||
1031 lex->tok.ttype = TOKEN_KEYWORD;
1035 return lex->tok.ttype;
1040 lex->flags.nodigraphs = true;
1041 if (lex->flags.preprocessing)
1042 lex_tokench(lex, ch);
1043 lex->tok.ttype = lex_finish_string(lex, '"');
1044 if (lex->flags.preprocessing)
1045 lex_tokench(lex, ch);
1046 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1048 /* Allow c style "string" "continuation" */
1049 ch = lex_skipwhite(lex);
1051 lex_ungetch(lex, ch);
1055 lex->tok.ttype = lex_finish_string(lex, '"');
1057 lex->flags.nodigraphs = false;
1059 return lex->tok.ttype;
1064 /* we parse character constants like string,
1065 * but return TOKEN_CHARCONST, or a vector type if it fits...
1066 * Likewise actual unescaping has to be done by the parser.
1067 * The difference is we don't allow 'char' 'continuation'.
1069 if (lex->flags.preprocessing)
1070 lex_tokench(lex, ch);
1071 lex->tok.ttype = lex_finish_string(lex, '\'');
1072 if (lex->flags.preprocessing)
1073 lex_tokench(lex, ch);
1076 /* It's a vector if we can successfully scan 3 floats */
1078 if (sscanf_s(lex->tok.value, " %f %f %f ",
1079 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1081 if (sscanf(lex->tok.value, " %f %f %f ",
1082 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1086 lex->tok.ttype = TOKEN_VECTORCONST;
1089 return lex->tok.ttype;
1094 lex->tok.ttype = lex_finish_digit(lex, ch);
1096 return lex->tok.ttype;
1099 lexerror(lex, "unknown token");
1100 return (lex->tok.ttype = TOKEN_ERROR);