From: Michael D. Lowis Date: Tue, 26 Mar 2019 15:32:07 +0000 (-0400) Subject: reworked error location translator function to find the file by path in list of open... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=e9001129282c45db1c3aff0f6bd35cfd595bca6a;p=proto%2Fsclpl.git reworked error location translator function to find the file by path in list of open and closed files --- 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); }