From e9001129282c45db1c3aff0f6bd35cfd595bca6a Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 26 Mar 2019 11:32:07 -0400 Subject: [PATCH] reworked error location translator function to find the file by path in list of open and closed files --- example.src | 6 +++--- source/lex.c | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/example.src b/example.src index 12c6dfb..6200d0d 100644 --- a/example.src +++ b/example.src @@ -2,9 +2,9 @@ require ("fmt") provide (main) let const_true bool = true -#let const_false bool = false -#let const_uint int = 123 -#let const_string string = " +let const_false bool = false +let const_uint int = 123 +let const_string string = "" var var_true bool = true var var_false bool = false diff --git a/source/lex.c b/source/lex.c index c150f94..de5bba9 100644 --- a/source/lex.c +++ b/source/lex.c @@ -195,6 +195,7 @@ void lexfile(Parser* ctx, char* path) { } void lex(Parser* ctx) { + ctx->tok.file = ctx->file->path; ctx->tok.type = T_NONE; while (ctx->tok.type == T_NONE) { if (!ctx->file) { @@ -214,9 +215,21 @@ void lex(Parser* ctx) { } } +static LexFile* get_file(Parser* p, char* path) { + LexFile* lf = p->file; + while (lf && strcmp(lf->path, path)) + lf = lf->next; + if (!lf) { + lf = p->done; + while (lf && strcmp(lf->path, path)) + lf = lf->next; + } + return lf; +} + void lexprintpos(Parser* p, FILE* file, Tok* tok) { size_t line = 1, col = 1; - char* data = p->file->fbeg; + char* data = get_file(p, tok->file)->fbeg; char* end = data + tok->offset; for (; *data && data < end; data++) { if (*data == '\n') { @@ -226,5 +239,5 @@ void lexprintpos(Parser* p, FILE* file, Tok* tok) { col++; } } - fprintf(file, "%s:%zu:%zu:", p->file->path, line, col); + fprintf(file, "%s:%zu:%zu:", tok->file, line, col); } -- 2.54.0