diff --git a/src/compile_error.rs b/src/compile_error.rs index 02e29af213..593f4f5912 100644 --- a/src/compile_error.rs +++ b/src/compile_error.rs @@ -247,13 +247,17 @@ impl Display for CompileError<'_> { ), UndefinedVariable { variable } => write!(f, "Variable `{variable}` not defined"), UnexpectedCharacter { expected } => { - write!(f, "Expected character {}", List::or_ticked(expected.iter())) + write!(f, "Expected character {}", List::or_ticked(expected)) } UnexpectedClosingDelimiter { close } => { write!(f, "Unexpected closing delimiter `{}`", close.close()) } UnexpectedEndOfToken { expected } => { - write!(f, "Expected character `{expected}` but found end-of-file") + write!( + f, + "Expected character `{}` but found end-of-file", + List::or_ticked(expected), + ) } UnexpectedToken { ref expected, diff --git a/src/compile_error_kind.rs b/src/compile_error_kind.rs index 91c5cd071b..09e2eb337c 100644 --- a/src/compile_error_kind.rs +++ b/src/compile_error_kind.rs @@ -113,7 +113,7 @@ pub(crate) enum CompileErrorKind<'src> { close: Delimiter, }, UnexpectedEndOfToken { - expected: char, + expected: Vec, }, UnexpectedToken { expected: Vec, diff --git a/src/lexer.rs b/src/lexer.rs index 0e7f76c67e..afef81c21e 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -639,13 +639,17 @@ impl<'src> Lexer<'src> { // Emit an unspecified token to consume the current character, self.token(Unspecified); + let expected = choices.iter().map(|choice| choice.0).collect(); + + if self.at_eof() { + return Err(self.error(UnexpectedEndOfToken { expected })); + } + // …and advance past another character, self.advance()?; // …so that the error we produce highlights the unexpected character. - return Err(self.error(UnexpectedCharacter { - expected: choices.iter().map(|choice| choice.0).collect(), - })); + return Err(self.error(UnexpectedCharacter { expected })); } Ok(()) @@ -710,7 +714,9 @@ impl<'src> Lexer<'src> { self.token(Unspecified); if self.at_eof() { - return Err(self.error(UnexpectedEndOfToken { expected: right })); + return Err(self.error(UnexpectedEndOfToken { + expected: vec![right], + })); } // …and advance past another character, @@ -2281,9 +2287,10 @@ mod tests { column: 1, width: 0, kind: UnexpectedEndOfToken { - expected: '&', + expected: vec!['&'], }, } + error! { name: ampersand_unexpected, input: "&%", @@ -2296,6 +2303,18 @@ mod tests { }, } + error! { + name: bang_eof, + input: "!", + offset: 1, + line: 0, + column: 1, + width: 0, + kind: UnexpectedEndOfToken { + expected: vec!['=', '~'], + }, + } + #[test] fn presume_error() { let compile_error = Lexer::new("justfile".as_ref(), "!")