From 05793bd2413d57bb737993b139b393393464ecbe Mon Sep 17 00:00:00 2001 From: Josh Pschorr Date: Wed, 9 Oct 2024 10:29:40 -0700 Subject: [PATCH] Add a source location for UnexpectedEndOfInput parse error (#504) --- CHANGELOG.md | 1 + partiql-parser/src/error.rs | 6 +++--- partiql-parser/src/parse/mod.rs | 12 ++++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 968c8ca4..589188ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed +- *BREAKING* partiql-parser: Added a source location to `ParseError::UnexpectedEndOfInput` ### Added diff --git a/partiql-parser/src/error.rs b/partiql-parser/src/error.rs index 1cd048f2..5c5fad16 100644 --- a/partiql-parser/src/error.rs +++ b/partiql-parser/src/error.rs @@ -50,7 +50,7 @@ where /// There were not enough tokens to complete a parse #[error("Unexpected end of input")] - UnexpectedEndOfInput, + UnexpectedEndOfInput(Loc), /// An otherwise un-categorized error occurred #[error("Unknown parse error at `{}`", _0)] @@ -83,14 +83,14 @@ where Loc: Display, { /// Maps an `ParserError` to `ParserError` by applying a function to each variant - pub fn map_loc(self, tx: F) -> ParseError<'input, Loc2> + pub fn map_loc(self, mut tx: F) -> ParseError<'input, Loc2> where Loc2: Display, F: FnMut(Loc) -> Loc2, { match self { ParseError::SyntaxError(l) => ParseError::SyntaxError(l.map_loc(tx)), - ParseError::UnexpectedEndOfInput => ParseError::UnexpectedEndOfInput, + ParseError::UnexpectedEndOfInput(l) => ParseError::UnexpectedEndOfInput((tx)(l)), ParseError::UnexpectedToken(l) => ParseError::UnexpectedToken(l.map_loc(tx)), ParseError::LexicalError(l) => ParseError::LexicalError(l.map_loc(tx)), ParseError::IllegalState(s) => ParseError::IllegalState(s), diff --git a/partiql-parser/src/parse/mod.rs b/partiql-parser/src/parse/mod.rs index 4ba6a951..31396661 100644 --- a/partiql-parser/src/parse/mod.rs +++ b/partiql-parser/src/parse/mod.rs @@ -119,9 +119,10 @@ impl<'input> From> for ParseError<'input, BytePosition> { } // TODO do something with UnrecognizedEof.expected - lalrpop_util::ParseError::UnrecognizedEof { expected: _, .. } => { - ParseError::UnexpectedEndOfInput - } + lalrpop_util::ParseError::UnrecognizedEof { + location, + expected: _, + } => ParseError::UnexpectedEndOfInput(location.into()), lalrpop_util::ParseError::ExtraToken { token: (start, token, end), @@ -769,7 +770,10 @@ mod tests { assert!(res.is_err()); let err_data = res.unwrap_err(); assert_eq!(1, err_data.errors.len()); - assert_eq!(err_data.errors[0], ParseError::UnexpectedEndOfInput); + assert_eq!( + err_data.errors[0], + ParseError::UnexpectedEndOfInput(BytePosition::from(6)) + ); } #[test]