Skip to content

Commit 7539dda

Browse files
committed
add fileid to span
1 parent a6fe813 commit 7539dda

File tree

5 files changed

+47
-42
lines changed

5 files changed

+47
-42
lines changed

src/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use crate::dynval::DynVal;
22
use itertools::Itertools;
33
use serde::{Deserialize, Serialize};
44

5+
/// stores the left and right end of a span, and a given file identifier.
56
#[derive(Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
6-
pub struct Span(pub usize, pub usize);
7+
pub struct Span(pub usize, pub usize, pub usize);
78

89
impl std::fmt::Display for Span {
910
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

src/error.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub type Result<T> = std::result::Result<T, Error>;
88
#[derive(thiserror::Error, Debug)]
99
pub enum Error {
1010
#[error("Parse error: {source}")]
11-
ParseError { source: lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError> },
11+
ParseError { file_id: usize, source: lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError> },
1212

1313
#[error("Type error: {0}")]
1414
ConversionError(#[from] dynval::ConversionError),
@@ -24,8 +24,8 @@ pub enum Error {
2424
}
2525

2626
impl Error {
27-
pub fn from_parse_error(err: lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError>) -> Self {
28-
Error::ParseError { source: err }
27+
pub fn from_parse_error(file_id: usize, err: lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError>) -> Self {
28+
Error::ParseError { file_id, source: err }
2929
}
3030

3131
pub fn at(self, span: Span) -> Self {
@@ -34,7 +34,7 @@ impl Error {
3434

3535
pub fn get_span(&self) -> Option<Span> {
3636
match self {
37-
Self::ParseError { source } => get_parse_error_span(source),
37+
Self::ParseError { file_id, source } => get_parse_error_span(*file_id, source),
3838
Self::Spanned(span, _) => Some(*span),
3939
Self::Eval(err) => err.span(),
4040
Self::ConversionError(err) => err.span(),
@@ -43,13 +43,16 @@ impl Error {
4343
}
4444
}
4545

46-
fn get_parse_error_span(err: &lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError>) -> Option<Span> {
46+
fn get_parse_error_span(
47+
file_id: usize,
48+
err: &lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError>,
49+
) -> Option<Span> {
4750
match err {
48-
lalrpop_util::ParseError::InvalidToken { location } => Some(Span(*location, *location)),
49-
lalrpop_util::ParseError::UnrecognizedEOF { location, expected: _ } => Some(Span(*location, *location)),
50-
lalrpop_util::ParseError::UnrecognizedToken { token, expected: _ } => Some(Span(token.0, token.2)),
51-
lalrpop_util::ParseError::ExtraToken { token } => Some(Span(token.0, token.2)),
52-
lalrpop_util::ParseError::User { error: LexicalError(l, r) } => Some(Span(*l, *r)),
51+
lalrpop_util::ParseError::InvalidToken { location } => Some(Span(*location, *location, file_id)),
52+
lalrpop_util::ParseError::UnrecognizedEOF { location, expected: _ } => Some(Span(*location, *location, file_id)),
53+
lalrpop_util::ParseError::UnrecognizedToken { token, expected: _ } => Some(Span(token.0, token.2, file_id)),
54+
lalrpop_util::ParseError::ExtraToken { token } => Some(Span(token.0, token.2, file_id)),
55+
lalrpop_util::ParseError::User { error: LexicalError(l, r) } => Some(Span(*l, *r, file_id)),
5356
}
5457
}
5558

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ pub mod dynval;
66
pub mod error;
77
pub mod eval;
88
pub mod parser;
9-
use ast::SimplExpr;
9+
10+
pub use ast::{SimplExpr, Span};
11+
1012
use lalrpop_util::lalrpop_mod;
1113

1214
lalrpop_mod!(
1315
#[allow(clippy::all)]
1416
pub simplexpr_parser
1517
);
1618

17-
pub fn parse_string(s: &str) -> Result<SimplExpr, error::Error> {
18-
parser::parse_string(s)
19+
pub fn parse_string(file_id: usize, s: &str) -> Result<SimplExpr, error::Error> {
20+
parser::parse_string(file_id, s)
1921
}
20-
pub use ast::Span;

src/parser/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::{
66
error::{Error, Result},
77
};
88

9-
pub fn parse_string(s: &str) -> Result<SimplExpr> {
9+
pub fn parse_string(file_id: usize, s: &str) -> Result<SimplExpr> {
1010
let lexer = lexer::Lexer::new(s);
1111
let parser = crate::simplexpr_parser::ExprParser::new();
12-
parser.parse(lexer).map_err(Error::from_parse_error)
12+
parser.parse(file_id, lexer).map_err(|e| Error::from_parse_error(file_id, e))
1313
}
1414

1515
#[cfg(test)]
@@ -20,7 +20,7 @@ mod tests {
2020
use crate::parser::lexer::Lexer;
2121
::insta::with_settings!({sort_maps => true}, {
2222
$(
23-
::insta::assert_debug_snapshot!(p.parse(Lexer::new($text)));
23+
::insta::assert_debug_snapshot!(p.parse(0, Lexer::new($text)));
2424
)*
2525
});
2626
}}

src/simplexpr_parser.lalrpop

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::parser::lalrpop_helpers::*;
44
use lalrpop_util::ParseError;
55

66

7-
grammar;
7+
grammar(fid: usize);
88

99
extern {
1010
type Location = usize;
@@ -66,54 +66,54 @@ pub Expr: SimplExpr = {
6666
},
6767

6868
<Literal>,
69-
<l:@L> <ident:"identifier"> <r:@R> => VarRef(Span(l, r), ident.to_string()),
69+
<l:@L> <ident:"identifier"> <r:@R> => VarRef(Span(l, r, fid), ident.to_string()),
7070
"(" <ExprReset> ")",
7171

7272
#[precedence(level="1")] #[assoc(side="right")]
73-
<l:@L> <ident:"identifier"> "(" <args: Comma<ExprReset>> ")" <r:@R> => FunctionCall(Span(l, r), ident, args),
74-
<l:@L> <value:Expr> "[" <index: ExprReset> "]" <r:@R> => JsonAccess(Span(l, r), b(value), b(index)),
73+
<l:@L> <ident:"identifier"> "(" <args: Comma<ExprReset>> ")" <r:@R> => FunctionCall(Span(l, r, fid), ident, args),
74+
<l:@L> <value:Expr> "[" <index: ExprReset> "]" <r:@R> => JsonAccess(Span(l, r, fid), b(value), b(index)),
7575

7676
<l:@L> <value:Expr> "." <lit_l:@L> <index:"identifier"> <r:@R> => {
77-
JsonAccess(Span(l, r), b(value), b(Literal(Span(lit_l, r), index.into())))
77+
JsonAccess(Span(l, r, fid), b(value), b(Literal(Span(lit_l, r, fid), index.into())))
7878
},
7979

8080
#[precedence(level="2")] #[assoc(side="right")]
81-
<l:@L> "!" <e:Expr> <r:@R> => UnaryOp(Span(l, r), Not, b(e)),
81+
<l:@L> "!" <e:Expr> <r:@R> => UnaryOp(Span(l, r, fid), Not, b(e)),
8282

8383
#[precedence(level="3")] #[assoc(side="left")]
84-
<l:@L> <le:Expr> "*" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Times, b(re)),
85-
<l:@L> <le:Expr> "/" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Div, b(re)),
86-
<l:@L> <le:Expr> "%" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Mod, b(re)),
84+
<l:@L> <le:Expr> "*" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Times, b(re)),
85+
<l:@L> <le:Expr> "/" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Div, b(re)),
86+
<l:@L> <le:Expr> "%" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Mod, b(re)),
8787

8888
#[precedence(level="4")] #[assoc(side="left")]
89-
<l:@L> <le:Expr> "+" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Plus, b(re)),
90-
<l:@L> <le:Expr> "-" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Minus, b(re)),
89+
<l:@L> <le:Expr> "+" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Plus, b(re)),
90+
<l:@L> <le:Expr> "-" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Minus, b(re)),
9191

9292
#[precedence(level="5")] #[assoc(side="left")]
93-
<l:@L> <le:Expr> "==" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Equals, b(re)),
94-
<l:@L> <le:Expr> "!=" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), NotEquals, b(re)),
95-
<l:@L> <le:Expr> "<" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), GT, b(re)),
96-
<l:@L> <le:Expr> ">" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), LT, b(re)),
97-
<l:@L> <le:Expr> "=~" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), RegexMatch, b(re)),
93+
<l:@L> <le:Expr> "==" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Equals, b(re)),
94+
<l:@L> <le:Expr> "!=" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), NotEquals, b(re)),
95+
<l:@L> <le:Expr> "<" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), GT, b(re)),
96+
<l:@L> <le:Expr> ">" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), LT, b(re)),
97+
<l:@L> <le:Expr> "=~" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), RegexMatch, b(re)),
9898

9999
#[precedence(level="6")] #[assoc(side="left")]
100-
<l:@L> <le:Expr> "&&" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), And, b(re)),
101-
<l:@L> <le:Expr> "||" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Or, b(re)),
102-
<l:@L> <le:Expr> "?:" <re:Expr> <r:@R> => BinOp(Span(l, r), b(le), Elvis, b(re)),
100+
<l:@L> <le:Expr> "&&" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), And, b(re)),
101+
<l:@L> <le:Expr> "||" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Or, b(re)),
102+
<l:@L> <le:Expr> "?:" <re:Expr> <r:@R> => BinOp(Span(l, r, fid), b(le), Elvis, b(re)),
103103

104104
#[precedence(level="7")] #[assoc(side="right")]
105105
<l:@L> <cond:Expr> "?" <then:ExprReset> ":" <els:Expr> <r:@R> => {
106-
IfElse(Span(l, r), b(cond), b(then), b(els))
106+
IfElse(Span(l, r, fid), b(cond), b(then), b(els))
107107
},
108108
};
109109

110110
ExprReset = <Expr>;
111111

112112
Literal: SimplExpr = {
113-
<l:@L> <x:StrLit> <r:@R> => SimplExpr::literal(Span(l, r), x),
114-
<l:@L> <x:"number"> <r:@R> => SimplExpr::literal(Span(l, r), x),
115-
<l:@L> "true" <r:@R> => SimplExpr::literal(Span(l, r), "true".into()),
116-
<l:@L> "false" <r:@R> => SimplExpr::literal(Span(l, r), "false".into()),
113+
<l:@L> <x:StrLit> <r:@R> => SimplExpr::literal(Span(l, r, fid), x),
114+
<l:@L> <x:"number"> <r:@R> => SimplExpr::literal(Span(l, r, fid), x),
115+
<l:@L> "true" <r:@R> => SimplExpr::literal(Span(l, r, fid), "true".into()),
116+
<l:@L> "false" <r:@R> => SimplExpr::literal(Span(l, r, fid), "false".into()),
117117
}
118118

119119
StrLit: String = {

0 commit comments

Comments
 (0)