Add source string to Span

This is the path to the location that this span is pointing to. This is
usually going to be a file path, but it can really be anything you want.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-01-16 14:40:09 -08:00
parent effa99e65d
commit 2752bdf6d3
5 changed files with 49 additions and 32 deletions

View File

@@ -10,14 +10,9 @@ pub struct Parser<'t> {
token: Result<Option<SpToken>>,
}
impl<'t> From<&'t str> for Parser<'t> {
fn from(text: &'t str) -> Self {
Parser::new(Lexer::new(text))
}
}
impl<'t> Parser<'t> {
pub fn new(mut lexer: Lexer<'t>) -> Self {
pub fn new(source: impl ToString, text: &'t str) -> Self {
let mut lexer = Lexer::new(source, text);
let token = lexer.next();
Self { lexer, token }
}
@@ -101,7 +96,7 @@ impl<'t> Parser<'t> {
_ => {
let expr = self.next_expr()?;
let span = expr.span();
Ok(SpStmt::new(span, Stmt::Expr(expr)))
Ok(SpStmt::new(span.clone(), Stmt::Expr(expr)))
}
}
}
@@ -114,7 +109,10 @@ impl<'t> Parser<'t> {
// get the include location string
let (path_span, _token) = self.expect_any_token(&[Token::Str])?.into_split();
let path = unescape_string(path_span.text_at(self.lexer.text()));
Ok(SpStmt::new(meta_span.union(path_span), Stmt::Include(path)))
Ok(SpStmt::new(
meta_span.union(&path_span),
Stmt::Include(path),
))
}
_ => {
todo!(
@@ -132,7 +130,7 @@ impl<'t> Parser<'t> {
_ => {
let atom = self.next_atom()?;
let span = atom.span();
Ok(SpExpr::new(span, Expr::Atom(atom)))
Ok(SpExpr::new(span.clone(), Expr::Atom(atom)))
}
}
}
@@ -262,7 +260,8 @@ macro_rules! make_atom {
#[test]
fn test_parser_atoms() {
let mut parser = Parser::from(
let mut parser = Parser::new(
"test",
r#"
a ab bcd dcefg foo bar baz
1 2 3 4 5
@@ -303,7 +302,8 @@ fn test_parser_atoms() {
#[test]
fn test_parser_quotes() {
let mut parser = Parser::from(
let mut parser = Parser::new(
"test",
r#"
[
a ab bcd dcefg foo bar baz