9 MEM_VEC_FUNCTIONS(token, char, value)
10 MEM_VEC_FUNCTIONS(lex_file, frame_macro, frames)
14 void lexerror(lex_file *lex, const char *fmt, ...)
19 con_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 con_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 vec_push(lex_filenames, lex->name);
164 lex_file* lex_open_string(const char *str, size_t len, const char *name)
168 lex = (lex_file*)mem_a(sizeof(*lex));
170 lexerror(NULL, "out of memory\n");
174 memset(lex, 0, sizeof(*lex));
177 lex->open_string = str;
178 lex->open_string_length = len;
179 lex->open_string_pos = 0;
181 lex->name = util_strdup(name ? name : "<string-source>");
182 lex->line = 1; /* we start counting at 1 */
187 vec_push(lex_filenames, lex->name);
192 void lex_cleanup(void)
195 for (i = 0; i < vec_size(lex_filenames); ++i)
196 mem_d(lex_filenames[i]);
197 vec_free(lex_filenames);
200 void lex_close(lex_file *lex)
203 for (i = 0; i < lex->frames_count; ++i)
204 mem_d(lex->frames[i].name);
205 MEM_VECTOR_CLEAR(lex, frames);
208 mem_d(lex->modelname);
214 token_delete(lex->tok);
216 MEM_VECTOR_CLEAR(&(lex->tok), value);
218 /* mem_d(lex->name); collected in lex_filenames */
222 static int lex_fgetc(lex_file *lex)
225 return fgetc(lex->file);
226 if (lex->open_string) {
227 if (lex->open_string_pos >= lex->open_string_length)
229 return lex->open_string[lex->open_string_pos++];
234 /* Get or put-back data
235 * The following to functions do NOT understand what kind of data they
237 * The are merely wrapping get/put in order to count line numbers.
239 static void lex_ungetch(lex_file *lex, int ch);
240 static int lex_try_trigraph(lex_file *lex, int old)
245 lex_ungetch(lex, c2);
251 case '=': return '#';
252 case '/': return '\\';
253 case '\'': return '^';
254 case '(': return '[';
255 case ')': return ']';
256 case '!': return '|';
257 case '<': return '{';
258 case '>': return '}';
259 case '-': return '~';
261 lex_ungetch(lex, c3);
262 lex_ungetch(lex, c2);
267 static int lex_try_digraph(lex_file *lex, int ch)
271 if (ch == '<' && c2 == ':')
273 else if (ch == ':' && c2 == '>')
275 else if (ch == '<' && c2 == '%')
277 else if (ch == '%' && c2 == '>')
279 else if (ch == '%' && c2 == ':')
281 lex_ungetch(lex, c2);
285 static int lex_getch(lex_file *lex)
291 if (lex->peek[lex->peekpos] == '\n')
293 return lex->peek[lex->peekpos];
300 return lex_try_trigraph(lex, ch);
301 else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
302 return lex_try_digraph(lex, ch);
306 static void lex_ungetch(lex_file *lex, int ch)
308 lex->peek[lex->peekpos++] = ch;
313 /* classify characters
314 * some additions to the is*() functions of ctype.h
317 /* Idents are alphanumberic, but they start with alpha or _ */
318 static bool isident_start(int ch)
320 return isalpha(ch) || ch == '_';
323 static bool isident(int ch)
325 return isident_start(ch) || isdigit(ch);
328 /* isxdigit_only is used when we already know it's not a digit
329 * and want to see if it's a hex digit anyway.
331 static bool isxdigit_only(int ch)
333 return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
336 /* Append a character to the token buffer */
337 static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
339 if (!token_value_add(&lex->tok, ch)) {
340 lexerror(lex, "out of memory");
346 /* Append a trailing null-byte */
347 static bool GMQCC_WARN lex_endtoken(lex_file *lex)
349 if (!token_value_add(&lex->tok, 0)) {
350 lexerror(lex, "out of memory");
353 lex->tok.value_count--;
357 /* Skip whitespace and comments and return the first
358 * non-white character.
359 * As this makes use of the above getch() ungetch() functions,
360 * we don't need to care at all about line numbering anymore.
362 * In theory, this function should only be used at the beginning
363 * of lexing, or when we *know* the next character is part of the token.
364 * Otherwise, if the parser throws an error, the linenumber may not be
365 * the line of the error, but the line of the next token AFTER the error.
367 * This is currently only problematic when using c-like string-continuation,
368 * since comments and whitespaces are allowed between 2 such strings.
372 "A continuation of the previous string"
373 // This line is skipped
376 * In this case, if the parse decides it didn't actually want a string,
377 * and uses lex->line to print an error, it will show the ', foo);' line's
380 * On the other hand, the parser is supposed to remember the line of the next
381 * token's beginning. In this case we would want skipwhite() to be called
382 * AFTER reading a token, so that the parser, before reading the NEXT token,
383 * doesn't store teh *comment's* linenumber, but the actual token's linenumber.
386 * here is to store the line of the first character after skipping
387 * the initial whitespace in lex->sline, this happens in lex_do.
389 static int lex_skipwhite(lex_file *lex)
392 bool haswhite = false;
397 while (ch != EOF && isspace(ch)) {
398 if (lex->flags.preprocessing) {
401 /* see if there was whitespace first */
402 if (haswhite) { /* (lex->tok.value_count) { */
403 lex_ungetch(lex, ch);
404 if (!lex_endtoken(lex))
408 /* otherwise return EOL */
412 if (!lex_tokench(lex, ch))
422 /* one line comment */
425 if (lex->flags.preprocessing) {
427 if (!lex_tokench(lex, '/') ||
428 !lex_tokench(lex, '/'))
434 while (ch != EOF && ch != '\n') {
435 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
439 if (lex->flags.preprocessing) {
440 lex_ungetch(lex, '\n');
441 if (!lex_endtoken(lex))
449 /* multiline comment */
450 if (lex->flags.preprocessing) {
452 if (!lex_tokench(lex, '/') ||
453 !lex_tokench(lex, '*'))
465 if (lex->flags.preprocessing) {
466 if (!lex_tokench(lex, '*') ||
467 !lex_tokench(lex, '/'))
475 if (lex->flags.preprocessing) {
476 if (!lex_tokench(lex, ch))
480 ch = ' '; /* cause TRUE in the isspace check */
483 /* Otherwise roll back to the slash and break out of the loop */
484 lex_ungetch(lex, ch);
488 } while (ch != EOF && isspace(ch));
491 if (!lex_endtoken(lex))
493 lex_ungetch(lex, ch);
500 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
505 while (ch != EOF && isident(ch))
507 if (!lex_tokench(lex, ch))
508 return (lex->tok.ttype = TOKEN_FATAL);
512 /* last ch was not an ident ch: */
513 lex_ungetch(lex, ch);
518 /* read one ident for the frame list */
519 static int lex_parse_frame(lex_file *lex)
526 while (ch != EOF && ch != '\n' && isspace(ch))
532 if (!isident_start(ch)) {
533 lexerror(lex, "invalid framename, must start with one of a-z or _, got %c", ch);
537 if (!lex_tokench(lex, ch))
539 if (!lex_finish_ident(lex))
541 if (!lex_endtoken(lex))
546 /* read a list of $frames */
547 static bool lex_finish_frames(lex_file *lex)
554 rc = lex_parse_frame(lex);
555 if (rc > 0) /* end of line */
557 if (rc < 0) /* error */
560 for (i = 0; i < lex->frames_count; ++i) {
561 if (!strcmp(lex->tok.value, lex->frames[i].name)) {
562 lex->frames[i].value = lex->framevalue++;
563 if (lexwarn(lex, WARN_FRAME_MACROS, "duplicate frame macro defined: `%s`", lex->tok.value))
568 if (i < lex->frames_count)
571 m.value = lex->framevalue++;
572 m.name = lex->tok.value;
573 lex->tok.value = NULL;
574 lex->tok.value_alloc = lex->tok.value_count = 0;
575 if (!lex_file_frames_add(lex, m)) {
576 lexerror(lex, "out of memory");
582 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
590 return TOKEN_STRINGCONST;
592 if (!lex->flags.preprocessing && ch == '\\') {
595 lexerror(lex, "unexpected end of file");
596 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
597 return (lex->tok.ttype = TOKEN_ERROR);
602 case 'a': ch = '\a'; break;
603 case 'b': ch = '\b'; break;
604 case 'r': ch = '\r'; break;
605 case 'n': ch = '\n'; break;
606 case 't': ch = '\t'; break;
607 case 'f': ch = '\f'; break;
608 case 'v': ch = '\v'; break;
610 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
611 /* so we just add the character plus backslash no matter what it actually is */
612 if (!lex_tokench(lex, '\\'))
613 return (lex->tok.ttype = TOKEN_FATAL);
615 /* add the character finally */
616 if (!lex_tokench(lex, ch))
617 return (lex->tok.ttype = TOKEN_FATAL);
619 else if (!lex_tokench(lex, ch))
620 return (lex->tok.ttype = TOKEN_FATAL);
622 lexerror(lex, "unexpected end of file within string constant");
623 lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
624 return (lex->tok.ttype = TOKEN_ERROR);
627 static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
633 /* parse a number... */
634 lex->tok.ttype = TOKEN_INTCONST;
636 if (!lex_tokench(lex, ch))
637 return (lex->tok.ttype = TOKEN_FATAL);
640 if (ch != '.' && !isdigit(ch))
642 if (lastch != '0' || ch != 'x')
644 /* end of the number or EOF */
645 lex_ungetch(lex, ch);
646 if (!lex_endtoken(lex))
647 return (lex->tok.ttype = TOKEN_FATAL);
649 lex->tok.constval.i = lastch - '0';
650 return lex->tok.ttype;
656 /* EOF would have been caught above */
660 if (!lex_tokench(lex, ch))
661 return (lex->tok.ttype = TOKEN_FATAL);
663 while (isdigit(ch) || (ishex && isxdigit_only(ch)))
665 if (!lex_tokench(lex, ch))
666 return (lex->tok.ttype = TOKEN_FATAL);
670 /* NOT else, '.' can come from above as well */
671 if (ch == '.' && !ishex)
673 /* Allow floating comma in non-hex mode */
674 lex->tok.ttype = TOKEN_FLOATCONST;
675 if (!lex_tokench(lex, ch))
676 return (lex->tok.ttype = TOKEN_FATAL);
678 /* continue digits-only */
682 if (!lex_tokench(lex, ch))
683 return (lex->tok.ttype = TOKEN_FATAL);
687 /* put back the last character */
688 /* but do not put back the trailing 'f' or a float */
689 if (lex->tok.ttype == TOKEN_FLOATCONST && ch == 'f')
692 /* generally we don't want words to follow numbers: */
694 lexerror(lex, "unexpected trailing characters after number");
695 return (lex->tok.ttype = TOKEN_ERROR);
697 lex_ungetch(lex, ch);
699 if (!lex_endtoken(lex))
700 return (lex->tok.ttype = TOKEN_FATAL);
701 if (lex->tok.ttype == TOKEN_FLOATCONST)
702 lex->tok.constval.f = strtod(lex->tok.value, NULL);
704 lex->tok.constval.i = strtol(lex->tok.value, NULL, 0);
705 return lex->tok.ttype;
708 int lex_do(lex_file *lex)
718 ch = lex_skipwhite(lex);
719 lex->sline = lex->line;
720 lex->tok.ctx.line = lex->sline;
721 lex->tok.ctx.file = lex->name;
723 if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
724 return (lex->tok.ttype = ch);
728 return (lex->tok.ttype = TOKEN_FATAL);
732 return (lex->tok.ttype = TOKEN_EOF);
735 /* modelgen / spiritgen commands */
741 if (!isident_start(ch)) {
742 lexerror(lex, "hanging '$' modelgen/spritegen command line");
745 if (!lex_tokench(lex, ch))
746 return (lex->tok.ttype = TOKEN_FATAL);
747 if (!lex_finish_ident(lex))
748 return (lex->tok.ttype = TOKEN_ERROR);
749 if (!lex_endtoken(lex))
750 return (lex->tok.ttype = TOKEN_FATAL);
751 /* skip the known commands */
754 if (!strcmp(v, "frame") || !strcmp(v, "framesave"))
756 /* frame/framesave command works like an enum
757 * similar to fteqcc we handle this in the lexer.
758 * The reason for this is that it is sensitive to newlines,
759 * which the parser is unaware of
761 if (!lex_finish_frames(lex))
762 return (lex->tok.ttype = TOKEN_ERROR);
766 if (!strcmp(v, "framevalue"))
769 while (ch != EOF && isspace(ch) && ch != '\n')
773 lexerror(lex, "$framevalue requires an integer parameter");
778 lex->tok.ttype = lex_finish_digit(lex, ch);
779 if (!lex_endtoken(lex))
780 return (lex->tok.ttype = TOKEN_FATAL);
781 if (lex->tok.ttype != TOKEN_INTCONST) {
782 lexerror(lex, "$framevalue requires an integer parameter");
785 lex->framevalue = lex->tok.constval.i;
789 if (!strcmp(v, "framerestore"))
795 rc = lex_parse_frame(lex);
798 lexerror(lex, "$framerestore requires a framename parameter");
802 return (lex->tok.ttype = TOKEN_FATAL);
805 for (frame = 0; frame < lex->frames_count; ++frame) {
806 if (!strcmp(v, lex->frames[frame].name)) {
807 lex->framevalue = lex->frames[frame].value;
811 lexerror(lex, "unknown framename `%s`", v);
815 if (!strcmp(v, "modelname"))
821 rc = lex_parse_frame(lex);
824 lexerror(lex, "$framerestore requires a framename parameter");
828 return (lex->tok.ttype = TOKEN_FATAL);
831 if (lex->modelname) {
833 m.value = lex->framevalue;
834 m.name = lex->modelname;
835 lex->modelname = NULL;
836 if (!lex_file_frames_add(lex, m)) {
837 lexerror(lex, "out of memory");
838 return (lex->tok.ttype = TOKEN_FATAL);
841 lex->modelname = lex->tok.value;
842 lex->tok.value = NULL;
843 lex->tok.value_alloc = lex->tok.value_count = 0;
844 for (frame = 0; frame < lex->frames_count; ++frame) {
845 if (!strcmp(v, lex->frames[frame].name)) {
846 lex->framevalue = lex->frames[frame].value;
853 if (!strcmp(v, "flush"))
856 for (frame = 0; frame < lex->frames_count; ++frame)
857 mem_d(lex->frames[frame].name);
858 MEM_VECTOR_CLEAR(lex, frames);
859 /* skip line (fteqcc does it too) */
861 while (ch != EOF && ch != '\n')
866 if (!strcmp(v, "cd") ||
867 !strcmp(v, "origin") ||
868 !strcmp(v, "base") ||
869 !strcmp(v, "flags") ||
870 !strcmp(v, "scale") ||
875 while (ch != EOF && ch != '\n')
880 for (frame = 0; frame < lex->frames_count; ++frame) {
881 if (!strcmp(v, lex->frames[frame].name)) {
882 lex->tok.constval.i = lex->frames[frame].value;
883 return (lex->tok.ttype = TOKEN_INTCONST);
887 lexerror(lex, "invalid frame macro");
891 /* single-character tokens */
896 if (!lex_tokench(lex, ch) ||
899 return (lex->tok.ttype = TOKEN_FATAL);
901 if (lex->flags.noops)
902 return (lex->tok.ttype = ch);
904 return (lex->tok.ttype = TOKEN_OPERATOR);
912 if (!lex_tokench(lex, ch) ||
915 return (lex->tok.ttype = TOKEN_FATAL);
917 return (lex->tok.ttype = ch);
922 if (lex->flags.noops)
924 /* Detect characters early which are normally
925 * operators OR PART of an operator.
942 if (!lex_tokench(lex, ch) ||
945 return (lex->tok.ttype = TOKEN_FATAL);
947 return (lex->tok.ttype = ch);
954 if (!lex_tokench(lex, ch))
955 return (lex->tok.ttype = TOKEN_FATAL);
956 /* peak ahead once */
957 nextch = lex_getch(lex);
959 lex_ungetch(lex, nextch);
960 if (!lex_endtoken(lex))
961 return (lex->tok.ttype = TOKEN_FATAL);
962 return (lex->tok.ttype = ch);
964 /* peak ahead again */
965 nextch = lex_getch(lex);
967 lex_ungetch(lex, nextch);
968 lex_ungetch(lex, nextch);
969 if (!lex_endtoken(lex))
970 return (lex->tok.ttype = TOKEN_FATAL);
971 return (lex->tok.ttype = ch);
973 /* fill the token to be "..." */
974 if (!lex_tokench(lex, ch) ||
975 !lex_tokench(lex, ch) ||
978 return (lex->tok.ttype = TOKEN_FATAL);
980 return (lex->tok.ttype = TOKEN_DOTS);
984 if (ch == ',' || ch == '.') {
985 if (!lex_tokench(lex, ch) ||
988 return (lex->tok.ttype = TOKEN_FATAL);
990 return (lex->tok.ttype = TOKEN_OPERATOR);
993 if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */
994 ch == '>' || ch == '<' || /* <<, >>, <=, >= */
995 ch == '=' || ch == '!' || /* ==, != */
996 ch == '&' || ch == '|') /* &&, ||, &=, |= */
998 if (!lex_tokench(lex, ch))
999 return (lex->tok.ttype = TOKEN_FATAL);
1001 nextch = lex_getch(lex);
1002 if (nextch == ch || nextch == '=') {
1003 if (!lex_tokench(lex, nextch))
1004 return (lex->tok.ttype = TOKEN_FATAL);
1005 } else if (ch == '-' && nextch == '>') {
1006 if (!lex_tokench(lex, nextch))
1007 return (lex->tok.ttype = TOKEN_FATAL);
1009 lex_ungetch(lex, nextch);
1011 if (!lex_endtoken(lex))
1012 return (lex->tok.ttype = TOKEN_FATAL);
1013 return (lex->tok.ttype = TOKEN_OPERATOR);
1017 if (ch == '^' || ch == '~' || ch == '!')
1019 if (!lex_tokench(lex, ch) ||
1022 return (lex->tok.ttype = TOKEN_FATAL);
1024 return (lex->tok.ttype = TOKEN_OPERATOR);
1028 if (ch == '*' || ch == '/') /* *=, /= */
1030 if (!lex_tokench(lex, ch))
1031 return (lex->tok.ttype = TOKEN_FATAL);
1033 nextch = lex_getch(lex);
1034 if (nextch == '=') {
1035 if (!lex_tokench(lex, nextch))
1036 return (lex->tok.ttype = TOKEN_FATAL);
1038 lex_ungetch(lex, nextch);
1040 if (!lex_endtoken(lex))
1041 return (lex->tok.ttype = TOKEN_FATAL);
1042 return (lex->tok.ttype = TOKEN_OPERATOR);
1045 if (isident_start(ch))
1049 if (!lex_tokench(lex, ch))
1050 return (lex->tok.ttype = TOKEN_FATAL);
1051 if (!lex_finish_ident(lex)) {
1053 return (lex->tok.ttype = TOKEN_ERROR);
1055 if (!lex_endtoken(lex))
1056 return (lex->tok.ttype = TOKEN_FATAL);
1057 lex->tok.ttype = TOKEN_IDENT;
1060 if (!strcmp(v, "void")) {
1061 lex->tok.ttype = TOKEN_TYPENAME;
1062 lex->tok.constval.t = TYPE_VOID;
1063 } else if (!strcmp(v, "int")) {
1064 lex->tok.ttype = TOKEN_TYPENAME;
1065 lex->tok.constval.t = TYPE_INTEGER;
1066 } else if (!strcmp(v, "float")) {
1067 lex->tok.ttype = TOKEN_TYPENAME;
1068 lex->tok.constval.t = TYPE_FLOAT;
1069 } else if (!strcmp(v, "string")) {
1070 lex->tok.ttype = TOKEN_TYPENAME;
1071 lex->tok.constval.t = TYPE_STRING;
1072 } else if (!strcmp(v, "entity")) {
1073 lex->tok.ttype = TOKEN_TYPENAME;
1074 lex->tok.constval.t = TYPE_ENTITY;
1075 } else if (!strcmp(v, "vector")) {
1076 lex->tok.ttype = TOKEN_TYPENAME;
1077 lex->tok.constval.t = TYPE_VECTOR;
1078 } else if (!strcmp(v, "for") ||
1079 !strcmp(v, "while") ||
1082 !strcmp(v, "else") ||
1083 !strcmp(v, "local") ||
1084 !strcmp(v, "return") ||
1085 !strcmp(v, "not") ||
1086 !strcmp(v, "const"))
1088 lex->tok.ttype = TOKEN_KEYWORD;
1090 else if (opts_standard != COMPILER_QCC)
1092 /* other standards reserve these keywords */
1093 if (!strcmp(v, "switch") ||
1094 !strcmp(v, "struct") ||
1095 !strcmp(v, "union") ||
1096 !strcmp(v, "break") ||
1097 !strcmp(v, "continue") ||
1100 lex->tok.ttype = TOKEN_KEYWORD;
1104 return lex->tok.ttype;
1109 lex->flags.nodigraphs = true;
1110 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1112 lex->tok.ttype = lex_finish_string(lex, '"');
1113 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1115 while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
1117 /* Allow c style "string" "continuation" */
1118 ch = lex_skipwhite(lex);
1120 lex_ungetch(lex, ch);
1124 lex->tok.ttype = lex_finish_string(lex, '"');
1126 lex->flags.nodigraphs = false;
1127 if (!lex_endtoken(lex))
1128 return (lex->tok.ttype = TOKEN_FATAL);
1129 return lex->tok.ttype;
1134 /* we parse character constants like string,
1135 * but return TOKEN_CHARCONST, or a vector type if it fits...
1136 * Likewise actual unescaping has to be done by the parser.
1137 * The difference is we don't allow 'char' 'continuation'.
1139 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1141 lex->tok.ttype = lex_finish_string(lex, '\'');
1142 if (lex->flags.preprocessing && !lex_tokench(lex, ch))
1144 if (!lex_endtoken(lex))
1145 return (lex->tok.ttype = TOKEN_FATAL);
1147 /* It's a vector if we can successfully scan 3 floats */
1149 if (sscanf_s(lex->tok.value, " %f %f %f ",
1150 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1152 if (sscanf(lex->tok.value, " %f %f %f ",
1153 &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
1157 lex->tok.ttype = TOKEN_VECTORCONST;
1160 return lex->tok.ttype;
1165 lex->tok.ttype = lex_finish_digit(lex, ch);
1166 if (!lex_endtoken(lex))
1167 return (lex->tok.ttype = TOKEN_FATAL);
1168 return lex->tok.ttype;
1171 lexerror(lex, "unknown token");
1172 return (lex->tok.ttype = TOKEN_ERROR);