Skip to content

Commit

Permalink
Fix error when lexing ! at end-of-file
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Dec 11, 2024
1 parent 53f8619 commit b0c240a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/compile_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub(crate) enum CompileErrorKind<'src> {
close: Delimiter,
},
UnexpectedEndOfToken {
expected: char,
expected: Vec<char>,
},
UnexpectedToken {
expected: Vec<TokenKind>,
Expand Down
29 changes: 24 additions & 5 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2281,9 +2287,10 @@ mod tests {
column: 1,
width: 0,
kind: UnexpectedEndOfToken {
expected: '&',
expected: vec!['&'],
},
}

error! {
name: ampersand_unexpected,
input: "&%",
Expand All @@ -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(), "!")
Expand Down

0 comments on commit b0c240a

Please sign in to comment.