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 vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap);
23 bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...)
26 int lvl = LVL_WARNING;
28 if (!OPTS_WARN(warntype))
35 vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap);
45 token *tok = (token*)mem_a(sizeof(token));
48 memset(tok, 0, sizeof(*tok));
52 void token_delete(token *self)
54 if (self->next && self->next->prev == self)
55 self->next->prev = self->prev;
56 if (self->prev && self->prev->next == self)
57 self->prev->next = self->next;
58 MEM_VECTOR_CLEAR(self, value);
62 token* token_copy(const token *cp)
64 token* self = token_new();
68 self->value_alloc = cp->value_count + 1;
69 self->value_count = cp->value_count;
70 self->value = (char*)mem_a(self->value_alloc);
75 memcpy(self->value, cp->value, cp->value_count);
76 self->value[self->value_alloc-1] = 0;
80 self->ttype = cp->ttype;
81 memcpy(&self->constval, &cp->constval, sizeof(self->constval));
85 void token_delete_all(token *t)
96 token* token_copy_all(const token *cp)
101 out = cur = token_copy(cp);
107 cur->next = token_copy(cp);
109 token_delete_all(out);
112 cur->next->prev = cur;
119 static void lex_token_new(lex_file *lex)
123 token_delete(lex->tok);
124 lex->tok = token_new();
126 lex->tok.value_count = 0;
127 lex->tok.constval.t = 0;
128 lex->tok.ctx.line = lex->sline;
129 lex->tok.ctx.file = lex->name;
134 lex_file* lex_open(const char *file)
137 FILE *in = util_fopen(file, "rb");
140 lexerror(NULL, "open failed: '%s'\n", file);
144 lex = (lex_file*)mem_a(sizeof(*lex));
147 lexerror(NULL, "out of memory\n");
151 memset(lex, 0, sizeof(*lex));
154 lex->name = util_strdup(file);
155 lex->line = 1; /* we start counting at 1 */
160 lex_filenames_add(lex->name);
165 lex_file* lex_open_string(const char *str, size_t len, const char *name)
169 lex = (lex_file*)mem_a(sizeof(*lex));
171 lexerror(NULL, "out of memory\n");
175 memset(lex, 0, sizeof(*lex));
178 lex->open_string = str;
179 lex->open_string_length = len;
180 lex->open_string_pos = 0;
182 lex->name = util_strdup(name ? name : "<string-source>");
183 lex->line = 1; /* we start counting at 1 */
188 lex_filenames_add(lex->name);
193 void lex_cleanup(void)
196 for (i = 0; i < lex_filenames_elements; ++i)
197 mem_d(lex_filenames_data[i]);
198 mem_d(lex_filenames_data);
201 void lex_close(lex_file *lex)
204 for (i = 0; i < lex->frames_count; ++i)
205 mem_d(lex->frames[i].name);
206 MEM_VECTOR_CLEAR(lex, frames);
209 mem_d(lex->modelname);
215 token_delete(lex->tok);
217 MEM_VECTOR_CLEAR(&(lex->tok), value);
219 /* mem_d(lex->name); collected in lex_filenames */
223 static int lex_fgetc(lex_file *lex)
226 return fgetc(lex->file);
227 if (lex->open_string) {
228 if (lex->open_string_pos >= lex->open_string_length)
230 return lex->open_string[lex->open_string_pos++];
235 /* Get or put-back data
236 * The following to functions do NOT understand what kind of data they
238 * The are merely wrapping get/put in order to count line numbers.
240 static void lex_ungetch(lex_file *lex, int ch);
241 static int lex_try_trigraph(lex_file *lex, int old)
246 lex_ungetch(lex, c2);
252 case '=': return '#';
253 case '/': return '\\';
254 case '\'': return '^';
255 case '(': return '[';
256 case ')': return ']';
257 case '!': return '|';
258 case '<': return '{';
259 case '>': return '}';
260 case '-': return '~';
262 lex_ungetch(lex, c3);
263 lex_ungetch(lex, c2);
268 static int lex_try_digraph(lex_file *lex, int ch)
272 if (ch == '<' && c2 == ':')
274 else if (ch == ':' && c2 == '>')
276 else if (ch == '<' && c2 == '%')
278 else if (ch == '%' && c2 == '>')
280 else if (ch == '%' && c2 == ':')
282 lex_ungetch(lex, c2);
286 static int lex_getch(lex_file *lex)
292 if (lex->peek[lex->peekpos] == '\n')
294 return lex->peek[lex->peekpos];
301 return lex_try_trigraph(lex, ch);
302 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
303 return lex_try_digraph(lex, ch);
307 static void lex_ungetch(lex_file *lex, int ch)
309 lex->peek[lex->peekpos++] = ch;
314 /* classify characters
315 * some additions to the is*() functions of ctype.h
318 /* Idents are alphanumberic, but they start with alpha or _ */
319 static bool isident_start(int ch)
321 return isalpha(ch) || ch == '_';
324 static bool isident(int ch)
326 return isident_start(ch) || isdigit(ch);
329 /* isxdigit_only is used when we already know it's not a digit
330 * and want to see if it's a hex digit anyway.
332 static bool isxdigit_only(int ch)
334 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
337 /* Append a character to the token buffer */
338 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
340 if (!token_value_add(&lex->tok, ch)) {
341 lexerror(lex, "out of memory");
347 /* Append a trailing null-byte */
348 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
350 if (!token_value_add(&lex->tok, 0)) {
351 lexerror(lex, "out of memory");
354 lex->tok.value_count--;
358 /* Skip whitespace and comments and return the first
359 * non-white character.
360 * As this makes use of the above getch() ungetch() functions,
361 * we don't need to care at all about line numbering anymore.
363 * In theory, this function should only be used at the beginning
364 * of lexing, or when we *know* the next character is part of the token.
365 * Otherwise, if the parser throws an error, the linenumber may not be
366 * the line of the error, but the line of the next token AFTER the error.
368 * This is currently only problematic when using c-like string-continuation,
369 * since comments and whitespaces are allowed between 2 such strings.
373 "A continuation of the previous string"
374 // This line is skipped
377 * In this case, if the parse decides it didn't actually want a string,
378 * and uses lex->line to print an error, it will show the ', foo);' line's
381 * On the other hand, the parser is supposed to remember the line of the next
382 * token's beginning. In this case we would want skipwhite() to be called
383 * AFTER reading a token, so that the parser, before reading the NEXT token,
384 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
387 * here is to store the line of the first character after skipping
388 * the initial whitespace in lex->sline, this happens in lex_do.
390 static int lex_skipwhite(lex_file *lex)
393 bool haswhite = false;
398 while (ch != EOF && isspace(ch)) {
399 if (lex->flags.preprocessing) {
402 /* see if there was whitespace first */
403 if (haswhite) { /* (lex->tok.value_count) { */
404 lex_ungetch(lex, ch);
405 if (!lex_endtoken(lex))
409 /* otherwise return EOL */
413 if (!lex_tokench(lex, ch))
423 /* one line comment */
426 if (lex->flags.preprocessing) {
428 if (!lex_tokench(lex, '/') ||
429 !lex_tokench(lex, '/'))
435 while (ch != EOF && ch != '\n') {
436 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
440 if (lex->flags.preprocessing) {
441 lex_ungetch(lex, '\n');
442 if (!lex_endtoken(lex))
450 /* multiline comment */
451 if (lex->flags.preprocessing) {
453 if (!lex_tokench(lex, '/') ||
454 !lex_tokench(lex, '*'))
466 if (lex->flags.preprocessing) {
467 if (!lex_tokench(lex, '*') ||
468 !lex_tokench(lex, '/'))
476 if (lex->flags.preprocessing) {
477 if (!lex_tokench(lex, ch))
481 ch = ' '; /* cause TRUE in the isspace check */
484 /* Otherwise roll back to the slash and break out of the loop */
485 lex_ungetch(lex, ch);
489 } while (ch != EOF && isspace(ch));
492 if (!lex_endtoken(lex))
494 lex_ungetch(lex, ch);
501 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
506 while (ch != EOF && isident(ch))
508 if (!lex_tokench(lex, ch))
509 return (lex->tok.ttype = TOKEN_FATAL);
513 /* last ch was not an ident ch: */
514 lex_ungetch(lex, ch);
519 /* read one ident for the frame list */
520 static int lex_parse_frame(lex_file *lex)
527 while (ch != EOF && ch != '\n' && isspace(ch))
533 if (!isident_start(ch)) {
534 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
538 if (!lex_tokench(lex, ch))
540 if (!lex_finish_ident(lex))
542 if (!lex_endtoken(lex))
547 /* read a list of $frames */
548 static bool lex_finish_frames(lex_file *lex)
555 rc = lex_parse_frame(lex);
556 if (rc > 0) /* end of line */
558 if (rc < 0) /* error */
561 for (i = 0; i < lex->frames_count; ++i) {
562 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
563 lex->frames[i].value = lex->framevalue++;
564 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
569 if (i < lex->frames_count)
572 m.value = lex->framevalue++;
573 m.name = lex->tok.value;
574 lex->tok.value = NULL;
575 lex->tok.value_alloc = lex->tok.value_count = 0;
576 if (!lex_file_frames_add(lex, m)) {
577 lexerror(lex, "out of memory");
583 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
591 return TOKEN_STRINGCONST;
593 if (!lex->flags.preprocessing && ch == '\\') {
596 lexerror(lex, "unexpected end of file");
597 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
598 return (lex->tok.ttype = TOKEN_ERROR);
603 case 'a': ch = '\a'; break;
604 case 'b': ch = '\b'; break;
605 case 'r': ch = '\r'; break;
606 case 'n': ch = '\n'; break;
607 case 't': ch = '\t'; break;
608 case 'f': ch = '\f'; break;
609 case 'v': ch = '\v'; break;
611 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
612 /* so we just add the character plus backslash no matter what it actually is */
613 if (!lex_tokench(lex, '\\'))
614 return (lex->tok.ttype = TOKEN_FATAL);
616 /* add the character finally */
617 if (!lex_tokench(lex, ch))
618 return (lex->tok.ttype = TOKEN_FATAL);
620 else if (!lex_tokench(lex, ch))
621 return (lex->tok.ttype = TOKEN_FATAL);
623 lexerror(lex, "unexpected end of file within string constant");
624 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
625 return (lex->tok.ttype = TOKEN_ERROR);
628 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
634 /* parse a number... */
635 lex->tok.ttype = TOKEN_INTCONST;
637 if (!lex_tokench(lex, ch))
638 return (lex->tok.ttype = TOKEN_FATAL);
641 if (ch != '.' && !isdigit(ch))
643 if (lastch != '0' || ch != 'x')
645 /* end of the number or EOF */
646 lex_ungetch(lex, ch);
647 if (!lex_endtoken(lex))
648 return (lex->tok.ttype = TOKEN_FATAL);
650 lex->tok.constval.i = lastch - '0';
651 return lex->tok.ttype;
657 /* EOF would have been caught above */
661 if (!lex_tokench(lex, ch))
662 return (lex->tok.ttype = TOKEN_FATAL);
664 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
666 if (!lex_tokench(lex, ch))
667 return (lex->tok.ttype = TOKEN_FATAL);
671 /* NOT else, '.' can come from above as well */
672 if (ch == '.' && !ishex)
674 /* Allow floating comma in non-hex mode */
675 lex->tok.ttype = TOKEN_FLOATCONST;
676 if (!lex_tokench(lex, ch))
677 return (lex->tok.ttype = TOKEN_FATAL);
679 /* continue digits-only */
683 if (!lex_tokench(lex, ch))
684 return (lex->tok.ttype = TOKEN_FATAL);
688 /* put back the last character */
689 /* but do not put back the trailing 'f' or a float */
690 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
693 /* generally we don't want words to follow numbers: */
695 lexerror(lex, "unexpected trailing characters after number");
696 return (lex->tok.ttype = TOKEN_ERROR);
698 lex_ungetch(lex, ch);
700 if (!lex_endtoken(lex))
701 return (lex->tok.ttype = TOKEN_FATAL);
702 if (lex->tok.ttype == TOKEN_FLOATCONST)
703 lex->tok.constval.f = strtod(lex->tok.value, NULL);
705 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
706 return lex->tok.ttype;
709 int lex_do(lex_file *lex)
719 ch = lex_skipwhite(lex);
720 lex->sline = lex->line;
721 lex->tok.ctx.line = lex->sline;
722 lex->tok.ctx.file = lex->name;
724 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
725 return (lex->tok.ttype = ch);
729 return (lex->tok.ttype = TOKEN_FATAL);
733 return (lex->tok.ttype = TOKEN_EOF);
736 /* modelgen / spiritgen commands */
742 if (!isident_start(ch)) {
743 lexerror(lex, "hanging '$' modelgen/spritegen command line");
746 if (!lex_tokench(lex, ch))
747 return (lex->tok.ttype = TOKEN_FATAL);
748 if (!lex_finish_ident(lex))
749 return (lex->tok.ttype = TOKEN_ERROR);
750 if (!lex_endtoken(lex))
751 return (lex->tok.ttype = TOKEN_FATAL);
752 /* skip the known commands */
755 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
757 /* frame/framesave command works like an enum
758 * similar to fteqcc we handle this in the lexer.
759 * The reason for this is that it is sensitive to newlines,
760 * which the parser is unaware of
762 if (!lex_finish_frames(lex))
763 return (lex->tok.ttype = TOKEN_ERROR);
767 if (!strcmp(v, "framevalue"))
770 while (ch != EOF && isspace(ch) && ch != '\n')
774 lexerror(lex, "$framevalue requires an integer parameter");
779 lex->tok.ttype = lex_finish_digit(lex, ch);
780 if (!lex_endtoken(lex))
781 return (lex->tok.ttype = TOKEN_FATAL);
782 if (lex->tok.ttype != TOKEN_INTCONST) {
783 lexerror(lex, "$framevalue requires an integer parameter");
786 lex->framevalue = lex->tok.constval.i;
790 if (!strcmp(v, "framerestore"))
796 rc = lex_parse_frame(lex);
799 lexerror(lex, "$framerestore requires a framename parameter");
803 return (lex->tok.ttype = TOKEN_FATAL);
806 for (frame = 0; frame < lex->frames_count; ++frame) {
807 if (!strcmp(v, lex->frames[frame].name)) {
808 lex->framevalue = lex->frames[frame].value;
812 lexerror(lex, "unknown framename `%s`", v);
816 if (!strcmp(v, "modelname"))
822 rc = lex_parse_frame(lex);
825 lexerror(lex, "$framerestore requires a framename parameter");
829 return (lex->tok.ttype = TOKEN_FATAL);
832 if (lex->modelname) {
834 m.value = lex->framevalue;
835 m.name = lex->modelname;
836 lex->modelname = NULL;
837 if (!lex_file_frames_add(lex, m)) {
838 lexerror(lex, "out of memory");
839 return (lex->tok.ttype = TOKEN_FATAL);
842 lex->modelname = lex->tok.value;
843 lex->tok.value = NULL;
844 lex->tok.value_alloc = lex->tok.value_count = 0;
845 for (frame = 0; frame < lex->frames_count; ++frame) {
846 if (!strcmp(v, lex->frames[frame].name)) {
847 lex->framevalue = lex->frames[frame].value;
854 if (!strcmp(v, "flush"))
857 for (frame = 0; frame < lex->frames_count; ++frame)
858 mem_d(lex->frames[frame].name);
859 MEM_VECTOR_CLEAR(lex, frames);
860 /* skip line (fteqcc does it too) */
862 while (ch != EOF && ch != '\n')
867 if (!strcmp(v, "cd") ||
868 !strcmp(v, "origin") ||
869 !strcmp(v, "base") ||
870 !strcmp(v, "flags") ||
871 !strcmp(v, "scale") ||
876 while (ch != EOF && ch != '\n')
881 for (frame = 0; frame < lex->frames_count; ++frame) {
882 if (!strcmp(v, lex->frames[frame].name)) {
883 lex->tok.constval.i = lex->frames[frame].value;
884 return (lex->tok.ttype = TOKEN_INTCONST);
888 lexerror(lex, "invalid frame macro");
892 /* single-character tokens */
897 if (!lex_tokench(lex, ch) ||
900 return (lex->tok.ttype = TOKEN_FATAL);
902 if (lex->flags.noops)
903 return (lex->tok.ttype = ch);
905 return (lex->tok.ttype = TOKEN_OPERATOR);
913 if (!lex_tokench(lex, ch) ||
916 return (lex->tok.ttype = TOKEN_FATAL);
918 return (lex->tok.ttype = ch);
923 if (lex->flags.noops)
925 /* Detect characters early which are normally
926 * operators OR PART of an operator.
943 if (!lex_tokench(lex, ch) ||
946 return (lex->tok.ttype = TOKEN_FATAL);
948 return (lex->tok.ttype = ch);
955 if (!lex_tokench(lex, ch))
956 return (lex->tok.ttype = TOKEN_FATAL);
957 /* peak ahead once */
958 nextch = lex_getch(lex);
960 lex_ungetch(lex, nextch);
961 if (!lex_endtoken(lex))
962 return (lex->tok.ttype = TOKEN_FATAL);
963 return (lex->tok.ttype = ch);
965 /* peak ahead again */
966 nextch = lex_getch(lex);
968 lex_ungetch(lex, nextch);
969 lex_ungetch(lex, nextch);
970 if (!lex_endtoken(lex))
971 return (lex->tok.ttype = TOKEN_FATAL);
972 return (lex->tok.ttype = ch);
974 /* fill the token to be "..." */
975 if (!lex_tokench(lex, ch) ||
976 !lex_tokench(lex, ch) ||
979 return (lex->tok.ttype = TOKEN_FATAL);
981 return (lex->tok.ttype = TOKEN_DOTS);
985 if (ch == ',' || ch == '.') {
986 if (!lex_tokench(lex, ch) ||
989 return (lex->tok.ttype = TOKEN_FATAL);
991 return (lex->tok.ttype = TOKEN_OPERATOR);
994 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
995 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
996 ch == '=' || ch == '!' || /* ==, != */
997 ch == '&' || ch == '|') /* &&, ||, &=, |= */
999 if (!lex_tokench(lex, ch))
1000 return (lex->tok.ttype = TOKEN_FATAL);
1002 nextch = lex_getch(lex);
1003 if (nextch == ch || nextch == '=') {
1004 if (!lex_tokench(lex, nextch))
1005 return (lex->tok.ttype = TOKEN_FATAL);
1006 } else if (ch == '-' && nextch == '>') {
1007 if (!lex_tokench(lex, nextch))
1008 return (lex->tok.ttype = TOKEN_FATAL);
1010 lex_ungetch(lex, nextch);
1012 if (!lex_endtoken(lex))
1013 return (lex->tok.ttype = TOKEN_FATAL);
1014 return (lex->tok.ttype = TOKEN_OPERATOR);
1018 if (ch == '^' || ch == '~' || ch == '!')
1020 if (!lex_tokench(lex, ch) ||
1023 return (lex->tok.ttype = TOKEN_FATAL);
1025 return (lex->tok.ttype = TOKEN_OPERATOR);
1029 if (ch == '*' || ch == '/') /* *=, /= */
1031 if (!lex_tokench(lex, ch))
1032 return (lex->tok.ttype = TOKEN_FATAL);
1034 nextch = lex_getch(lex);
1035 if (nextch == '=') {
1036 if (!lex_tokench(lex, nextch))
1037 return (lex->tok.ttype = TOKEN_FATAL);
1039 lex_ungetch(lex, nextch);
1041 if (!lex_endtoken(lex))
1042 return (lex->tok.ttype = TOKEN_FATAL);
1043 return (lex->tok.ttype = TOKEN_OPERATOR);
1046 if (isident_start(ch))
1050 if (!lex_tokench(lex, ch))
1051 return (lex->tok.ttype = TOKEN_FATAL);
1052 if (!lex_finish_ident(lex)) {
1054 return (lex->tok.ttype = TOKEN_ERROR);
1056 if (!lex_endtoken(lex))
1057 return (lex->tok.ttype = TOKEN_FATAL);
1058 lex->tok.ttype = TOKEN_IDENT;
1061 if (!strcmp(v, "void")) {
1062 lex->tok.ttype = TOKEN_TYPENAME;
1063 lex->tok.constval.t = TYPE_VOID;
1064 } else if (!strcmp(v, "int")) {
1065 lex->tok.ttype = TOKEN_TYPENAME;
1066 lex->tok.constval.t = TYPE_INTEGER;
1067 } else if (!strcmp(v, "float")) {
1068 lex->tok.ttype = TOKEN_TYPENAME;
1069 lex->tok.constval.t = TYPE_FLOAT;
1070 } else if (!strcmp(v, "string")) {
1071 lex->tok.ttype = TOKEN_TYPENAME;
1072 lex->tok.constval.t = TYPE_STRING;
1073 } else if (!strcmp(v, "entity")) {
1074 lex->tok.ttype = TOKEN_TYPENAME;
1075 lex->tok.constval.t = TYPE_ENTITY;
1076 } else if (!strcmp(v, "vector")) {
1077 lex->tok.ttype = TOKEN_TYPENAME;
1078 lex->tok.constval.t = TYPE_VECTOR;
1079 } else if (!strcmp(v, "for") ||
1080 !strcmp(v, "while") ||
1083 !strcmp(v, "else") ||
1084 !strcmp(v, "local") ||
1085 !strcmp(v, "return") ||
1086 !strcmp(v, "not") ||
1087 !strcmp(v, "const"))
1089 lex->tok.ttype = TOKEN_KEYWORD;
1091 else if (opts_standard != COMPILER_QCC)
1093 /* other standards reserve these keywords */
1094 if (!strcmp(v, "switch") ||
1095 !strcmp(v, "struct") ||
1096 !strcmp(v, "union") ||
1097 !strcmp(v, "break") ||
1098 !strcmp(v, "continue") ||
1101 lex->tok.ttype = TOKEN_KEYWORD;
1105 return lex->tok.ttype;
1110 lex->flags.nodigraphs = true;
1111 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1113 lex->tok.ttype = lex_finish_string(lex, '"');
1114 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1116 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1118 /* Allow c style "string" "continuation" */
1119 ch = lex_skipwhite(lex);
1121 lex_ungetch(lex, ch);
1125 lex->tok.ttype = lex_finish_string(lex, '"');
1127 lex->flags.nodigraphs = false;
1128 if (!lex_endtoken(lex))
1129 return (lex->tok.ttype = TOKEN_FATAL);
1130 return lex->tok.ttype;
1135 /* we parse character constants like string,
1136 * but return TOKEN_CHARCONST, or a vector type if it fits...
1137 * Likewise actual unescaping has to be done by the parser.
1138 * The difference is we don't allow 'char' 'continuation'.
1140 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1142 lex->tok.ttype = lex_finish_string(lex, '\'');
1143 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1145 if (!lex_endtoken(lex))
1146 return (lex->tok.ttype = TOKEN_FATAL);
1148 /* It's a vector if we can successfully scan 3 floats */
1150 if (sscanf_s(lex->tok.value, " %f %f %f ",
1151 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1153 if (sscanf(lex->tok.value, " %f %f %f ",
1154 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1158 lex->tok.ttype = TOKEN_VECTORCONST;
1161 return lex->tok.ttype;
1166 lex->tok.ttype = lex_finish_digit(lex, ch);
1167 if (!lex_endtoken(lex))
1168 return (lex->tok.ttype = TOKEN_FATAL);
1169 return lex->tok.ttype;
1172 lexerror(lex, "unknown token");
1173 return (lex->tok.ttype = TOKEN_ERROR);