From 91c0f5ae9d867b0cacd887f63b695b52bb967892 Mon Sep 17 00:00:00 2001 From: gluax <16431709+gluax@users.noreply.github.com> Date: Wed, 9 Mar 2022 09:46:07 -0800 Subject: [PATCH] resolve some error messages panicing --- compiler/parser/src/tokenizer/lexer.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/parser/src/tokenizer/lexer.rs b/compiler/parser/src/tokenizer/lexer.rs index ce74b35547..8fc9663c1b 100644 --- a/compiler/parser/src/tokenizer/lexer.rs +++ b/compiler/parser/src/tokenizer/lexer.rs @@ -164,7 +164,10 @@ impl Token { while i < input.len() { if i == 1 && input[0] == b'0' && input[i] == b'x' { - return Err(ParserError::lexer_hex_number_provided(&input_tendril[0..3]).into()); + return Err(ParserError::lexer_hex_number_provided( + &input_tendril[0..input_tendril.find('\n').unwrap_or(i) + 1], + ) + .into()); } if !input[i].is_ascii_digit() { break; @@ -305,7 +308,9 @@ impl Token { break; } else if unicode_char { return Err(ParserError::lexer_invalid_char( - &input_tendril[0..input_tendril[1..].find('\'').unwrap_or(i + 1)], + // grab the contents of everything between the '' if possible. + // else just show the character right before stuff went wrong. + &input_tendril[0..input_tendril[1..].find('\'').unwrap_or(i - 1) + 1], ) .into()); } else if input[i] == b'\\' { @@ -315,8 +320,11 @@ impl Token { if input[i] == b'x' { hex = true; } else if input[i] == b'u' { - if input[i + 1] == b'{' { + let one_ahead = input.get(i + 1); + if matches!(one_ahead, Some(b'{')) { escaped_unicode = true; + } else if one_ahead.is_some() { + return Err(ParserError::lexer_expected_valid_escaped_char(input[i + 1]).into()); } else { return Err(ParserError::lexer_expected_valid_escaped_char(input[i]).into()); }