diff options
author | David Phillips <david@sighup.nz> | 2018-08-01 20:41:48 +1200 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2018-08-01 20:41:48 +1200 |
commit | 73fb33ccab5f987361a9a405d6e141986061c230 (patch) | |
tree | 51b5514fa3bb589732a103592f32a5e1b7c0e33d | |
parent | 328e34077a2017d4017f907ac69c7ed5d6c160d4 (diff) | |
download | hence-73fb33ccab5f987361a9a405d6e141986061c230.tar.xz |
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.
-rw-r--r-- | lexer.c | 10 |
1 files changed, 7 insertions, 3 deletions
@@ -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; |