From bb741f343db6832fec955a56ceca45c9d4b5ffc0 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Mon, 26 Jul 2021 19:22:05 +0200 Subject: [PATCH] Include file_id in LexicalError --- crates/simplexpr/src/error.rs | 2 +- crates/simplexpr/src/parser/lexer.rs | 11 ++++++----- crates/simplexpr/src/parser/mod.rs | 4 ++-- crates/simplexpr/src/simplexpr_parser.lalrpop | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/simplexpr/src/error.rs b/crates/simplexpr/src/error.rs index 62300c1..920addf 100644 --- a/crates/simplexpr/src/error.rs +++ b/crates/simplexpr/src/error.rs @@ -52,7 +52,7 @@ fn get_parse_error_span( lalrpop_util::ParseError::UnrecognizedEOF { location, expected: _ } => Some(Span(*location, *location, file_id)), lalrpop_util::ParseError::UnrecognizedToken { token, expected: _ } => Some(Span(token.0, token.2, file_id)), lalrpop_util::ParseError::ExtraToken { token } => Some(Span(token.0, token.2, file_id)), - lalrpop_util::ParseError::User { error: LexicalError(l, r) } => Some(Span(*l, *r, file_id)), + lalrpop_util::ParseError::User { error: LexicalError(l, r, file_id) } => Some(Span(*l, *r, *file_id)), } } diff --git a/crates/simplexpr/src/parser/lexer.rs b/crates/simplexpr/src/parser/lexer.rs index f3e5891..022a82a 100644 --- a/crates/simplexpr/src/parser/lexer.rs +++ b/crates/simplexpr/src/parser/lexer.rs @@ -50,7 +50,7 @@ pub enum Token { // TODO can i include the fileid here already? #[derive(Debug, Eq, PartialEq, Copy, Clone)] -pub struct LexicalError(pub usize, pub usize); +pub struct LexicalError(pub usize, pub usize, pub usize); impl std::fmt::Display for LexicalError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -63,11 +63,12 @@ pub type SpannedResult = Result<(Loc, Tok, Loc), Error>; pub struct Lexer<'input> { lexer: logos::SpannedIter<'input, Token>, byte_offset: usize, + file_id: usize, } impl<'input> Lexer<'input> { - pub fn new(byte_offset: usize, text: &'input str) -> Self { - Lexer { lexer: logos::Lexer::new(text).spanned(), byte_offset } + pub fn new(file_id: usize, byte_offset: usize, text: &'input str) -> Self { + Lexer { lexer: logos::Lexer::new(text).spanned(), byte_offset, file_id } } } @@ -78,7 +79,7 @@ impl<'input> Iterator for Lexer<'input> { let (token, range) = self.lexer.next()?; let range = (range.start + self.byte_offset, range.end + self.byte_offset); if token == Token::Error { - Some(Err(LexicalError(range.0, range.1))) + Some(Err(LexicalError(range.0, range.1, self.file_id))) } else { Some(Ok((range.0, token, range.1))) } @@ -89,5 +90,5 @@ impl<'input> Iterator for Lexer<'input> { #[test] fn test_simplexpr_lexer() { use itertools::Itertools; - insta::assert_debug_snapshot!(Lexer::new(0, r#"(foo + - "()" "a\"b" true false [] 12.2)"#).collect_vec()); + insta::assert_debug_snapshot!(Lexer::new(0, 0, r#"(foo + - "()" "a\"b" true false [] 12.2)"#).collect_vec()); } diff --git a/crates/simplexpr/src/parser/mod.rs b/crates/simplexpr/src/parser/mod.rs index 169490a..f6a2d9b 100644 --- a/crates/simplexpr/src/parser/mod.rs +++ b/crates/simplexpr/src/parser/mod.rs @@ -7,7 +7,7 @@ use crate::{ }; pub fn parse_string(byte_offset: usize, file_id: usize, s: &str) -> Result { - let lexer = lexer::Lexer::new(byte_offset, s); + let lexer = lexer::Lexer::new(file_id, byte_offset, s); let parser = crate::simplexpr_parser::ExprParser::new(); parser.parse(file_id, lexer).map_err(|e| Error::from_parse_error(file_id, e)) } @@ -20,7 +20,7 @@ mod tests { use crate::parser::lexer::Lexer; ::insta::with_settings!({sort_maps => true}, { $( - ::insta::assert_debug_snapshot!(p.parse(0, Lexer::new(0, $text))); + ::insta::assert_debug_snapshot!(p.parse(0, Lexer::new(0, 0, $text))); )* }); }} diff --git a/crates/simplexpr/src/simplexpr_parser.lalrpop b/crates/simplexpr/src/simplexpr_parser.lalrpop index 9620aac..8025af6 100644 --- a/crates/simplexpr/src/simplexpr_parser.lalrpop +++ b/crates/simplexpr/src/simplexpr_parser.lalrpop @@ -63,7 +63,7 @@ pub Expr: SimplExpr = { #[precedence(level="0")] "lexer_error" =>? { - Err(ParseError::User { error: LexicalError(l, r) }) + Err(ParseError::User { error: LexicalError(l, r, fid) }) }, ,