Include file_id in LexicalError
This commit is contained in:
parent
ee933fccf5
commit
bb741f343d
4 changed files with 10 additions and 9 deletions
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Tok, Loc, Error> = 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());
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn parse_string(byte_offset: usize, file_id: usize, s: &str) -> Result<SimplExpr> {
|
||||
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)));
|
||||
)*
|
||||
});
|
||||
}}
|
||||
|
|
|
@ -63,7 +63,7 @@ pub Expr: SimplExpr = {
|
|||
|
||||
#[precedence(level="0")]
|
||||
<l:@L> "lexer_error" <r:@R> =>? {
|
||||
Err(ParseError::User { error: LexicalError(l, r) })
|
||||
Err(ParseError::User { error: LexicalError(l, r, fid) })
|
||||
},
|
||||
|
||||
<Literal>,
|
||||
|
|
Loading…
Add table
Reference in a new issue