From 73fb33ccab5f987361a9a405d6e141986061c230 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 1 Aug 2018 20:41:48 +1200 Subject: Fix whitespace trimming on non-whitespace lines Accounts for the case that whitespace doesn't start a line, but a token does instead. In this case, the whitespace span length should be set to 0 and additionally, still not reset each time whitespace is found. --- lexer.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lexer.c b/lexer.c index d9810a5..a541f4e 100644 --- a/lexer.c +++ b/lexer.c @@ -65,7 +65,7 @@ get_current_loc(void) { struct location l; l.line = line_number; l.column = column_number + 1; - l.leading_whitespace_len = leading_whitespace_len; + l.leading_whitespace_len = leading_whitespace_len == -1 ? 0 : leading_whitespace_len; return l; } @@ -92,6 +92,10 @@ static void add_token(struct token t) { struct token *last = tok_cursor; + if (leading_whitespace_len == -1) { + leading_whitespace_len = 0; + } + tok_cursor = malloc(sizeof(*tok_cursor)); if (tok_cursor == NULL) { emit_error("Internal error: malloc failed:"); @@ -189,7 +193,7 @@ lex_colon(void) { static int lex_line(void) { size_t length = strlen(buf); - leading_whitespace_len = 0; + leading_whitespace_len = -1; while (column_number < length && column_number < strlen(buf)) { switch (buf[column_number]) { @@ -199,7 +203,7 @@ lex_line(void) { case ' ': case '\t': eat_whitespace(); - if (leading_whitespace_len == 0) { + if (leading_whitespace_len == -1) { leading_whitespace_len = column_number; } break; -- cgit v1.1