From 2ad66dac6aac5a1ac16032fd0ffc9b26e1a21fe3 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Fri, 3 Sep 2021 13:05:10 -0700 Subject: scan: Improve accuracy of token locations The location should not include preceding whitespace and was off by one. --- scan.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/scan.c b/scan.c index a8327bf..cd85d9d 100644 --- a/scan.c +++ b/scan.c @@ -57,10 +57,11 @@ nextchar(struct scanner *s) bufadd(&s->buf, s->chr); for (;;) { s->chr = getc(s->file); - if (s->chr == '\n') - ++s->loc.line, s->loc.col = 1; - else - ++s->loc.col; + if (s->chr == '\n') { + ++s->loc.line, s->loc.col = 0; + break; + } + ++s->loc.col; if (s->chr != '\\') break; c = getc(s->file); @@ -68,7 +69,7 @@ nextchar(struct scanner *s) ungetc(c, s->file); break; } - ++s->loc.line, s->loc.col = 1; + ++s->loc.line, s->loc.col = 0; } } @@ -269,12 +270,13 @@ comment(struct scanner *s) } static int -scankind(struct scanner *s) +scankind(struct scanner *s, struct location *loc) { enum tokenkind tok; - struct location loc; + struct location oldloc; again: + *loc = s->loc; switch (s->chr) { case ' ': case '\t': @@ -353,11 +355,11 @@ again: } if (s->chr != '.') return TPERIOD; - loc = s->loc; + oldloc = s->loc; nextchar(s); if (s->chr != '.') { ungetc(s->chr, s->file); - s->loc = loc; + s->loc = oldloc; s->chr = '.'; return TPERIOD; } @@ -463,8 +465,7 @@ scan(struct token *t) { scanner->sawspace = false; for (;;) { - t->loc = scanner->loc; - t->kind = scankind(scanner); + t->kind = scankind(scanner, &t->loc); if (t->kind != TEOF || !scanner->next) break; scanclose(); -- cgit v1.2.3