X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=lex.c;h=9296158fd196cd5390e47fb60d7e1d096a936e7f;hp=9d15301d8d0070da7aec062df4af6e2b5864f83b;hb=a440c1410bad2a06b6a9385094a3dae0142f9395;hpb=1deeb8c9cfeeadd6b8073b38e76257c3c16cdc72 diff --git a/lex.c b/lex.c index 9d15301..9296158 100644 --- a/lex.c +++ b/lex.c @@ -27,14 +27,17 @@ #include #include "gmqcc.h" +/* + * Keywords are multichar, punctuation lexing is a bit more complicated + * than keyword lexing. + */ static const char *const lex_keywords[] = { "do", "else", "if", "while", "break", "continue", "return", "goto", - "for", + "for", "typedef", /* types */ "int", - "bool", "void", "string", "float", @@ -161,8 +164,10 @@ static int lex_get(struct lex_file *file) { while (isspace(ch) && ch != '\n') ch = lex_getch(file); - if (ch == '\n') + if (ch == '\n') { + file->line ++; return ch; + } lex_unget(ch, file); return ' '; @@ -244,7 +249,7 @@ static int lex_skipcmt(struct lex_file *file) { lex_addch(ch, file); while ((ch = lex_getch(file)) != '*') { if (ch == EOF) - return error(ERROR_LEX, "malformatted comment", " "); + return error(ERROR_LEX, "malformatted comment at line %d", file->line); else lex_addch(ch, file); } @@ -288,6 +293,12 @@ int lex_token(struct lex_file *file) { if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it]))) return it; + /* try the hashtable for typedefs? */ + if (typedef_find(file->lastok)) + for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++) + if (!strncmp(typedef_find(file->lastok)->name, lex_keywords[it], sizeof(lex_keywords[it]))) + return it; + return LEX_IDENT; } return ch; @@ -349,6 +360,7 @@ int lex_debug(struct lex_file *file) { while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) if (token == LEX_IDENT) printf("%s ", file->lastok); + fputc('\n', stdout); lex_reset(file); return 1; }