From 5dbadfa0448e3324479ece98045c0a427856f382 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 6 Aug 2018 01:40:02 +1200 Subject: Add TOK_BEGIN This lets us indicate that the lexer succeeded, even on an empty program. Previous behaviour was to return NULL on failure and on lex success of an empty progam. Enforcing the presence of a head for the linked token list ensures that we can reliably detect error as a caller of the lexer. Fixes test just added in previous commit too. --- hence.c | 3 +++ lex.c | 5 +++++ parse.c | 2 ++ token.h | 3 +++ 4 files changed, 13 insertions(+) diff --git a/hence.c b/hence.c index fbcb917..1fcd835 100644 --- a/hence.c +++ b/hence.c @@ -24,6 +24,9 @@ int main(int argc, char **argv) { } struct token *tok = lex_file(argv[1], fd); + if (!tok) { + return 1; + } int p = parse(argv[1], fd, tok); // gate_set_input("a", LOGIC_LOW); diff --git a/lex.c b/lex.c index 6777e42..19a7716 100644 --- a/lex.c +++ b/lex.c @@ -232,12 +232,17 @@ lex_line(void) { struct token * lex_file(const char *filename_local, FILE *fd_local) { + struct token start; + filename = filename_local; fd = fd_local; line_number = 1; tok_cursor = tok_start = NULL; + start.type = TOK_BEGIN; + add_token(start); + while (NULL != fgets(buf, sizeof(buf), fd)) { column_number = 0; if (lex_line()) { diff --git a/parse.c b/parse.c index d955bc4..fb7855e 100644 --- a/parse.c +++ b/parse.c @@ -131,6 +131,8 @@ parse(const char *fname, FILE *f, struct token *t) { filename = fname; cursor = t; + EXPECT_AND_DISCARD_CRITICAL(TOK_BEGIN); + /* Eat leading EOL tokens */ while (cursor && cursor->type == TOK_EOL) { kerchunk(); diff --git a/token.h b/token.h index c00c870..453b554 100644 --- a/token.h +++ b/token.h @@ -4,6 +4,9 @@ #define MAX_IDENT_LENGTH 128 enum TOKEN_TYPE { + /* Nop token to indicate lexer success */ + TOK_BEGIN, + /* Keywords */ TOK_MODULE, TOK_INPUT, -- cgit v1.1