Author | Jamozed <[email protected]> |
Date | 2021-11-19 04:17:03 |
Commit | eb2d3d49c4af21075e194c16ca170ee04ccb68cb |
Parent | 680103862717022549f8b3f49715229b1a1e4b73 |
Handle unexpected characters properly
Diffstat
M | src/lex.c | | | 5 | ++++- |
M | src/parse.c | | | 22 | ++++++++++++++-------- |
2 files changed, 18 insertions, 9 deletions
diff --git a/src/lex.c b/src/lex.c index 8cc07c1..409eeb9 100644 --- a/src/lex.c +++ b/src/lex.c @@ -74,7 +74,10 @@ skip:; if (C == '\n' || C == ';') { P += 1; T.k = TK_END; return t; } /* Skip comments */ - if (C == '#') { for (P += 1; P != Q && C != '\n'; P += 1) {} goto skip; } + if (C == '#') { + for (P += 1; P != Q && C != '\n'; P += 1); + if (C == '\n') { P += 1; } goto skip; + } /* Handle punctuators and operators */ switch (C) { diff --git a/src/parse.c b/src/parse.c index 5ea114a..09107cd 100644 --- a/src/parse.c +++ b/src/parse.c @@ -62,19 +62,26 @@ void ast_free(ast a) { if (a) { ast_free(a->lc); ast_free(a->rc); stack_free(&a->c); free(a); }; } +#define T (lex_peek(l)) /* Current Token */ + /* Parse a program. */ ast parse(lex *l) { - if (lex_peek(l).k == TK_EOF) { return NULL; } + for (; T.k == TK_END; lex_next(l)); + if (T.k == TK_EOF) { return NULL; } + + /* Throw an error if the current token is not a word */ + if (T.k != TK_WORD) { + warn("Unexpected token \"%s\"", tok_ks[T.k]); return NULL; + } - /* TODO assert that the token is a WORD? */ ast lhs = parse_comm(l); switch (lex_next(l).k) { - case TK_PIPE: { return parse_pipe(l, lhs); } - case TK_RDIN: { return lhs; } - case TK_ROUT: { return lhs; } - case TK_RERR: { return lhs; } - default: { return lhs; } + case TK_PIPE: {} return parse_pipe(l, lhs); + case TK_RDIN: {} return lhs; + case TK_ROUT: {} return lhs; + case TK_RERR: {} return lhs; + default: {} return lhs; } } @@ -111,7 +118,6 @@ static ast parse_rerr(lex *l) { } - /* Print parser debug output with an indent. */ static void ast_debug_indent(ast a, UINT i) { for (UINT j = 0; j != i; ++j) { printf("\t"); }