Create new objects with existing span

I've decided that attempting to create smaller spans is more difficult
and will also lead to worse error messages later on. It makes more sense
to just tag every newly created syntax tree element with the span of the
element it comes from.
This commit is contained in:
Joscha 2022-11-21 13:33:40 +01:00
parent 5c8dd1969f
commit b84d5ae0c8
3 changed files with 19 additions and 32 deletions

View file

@ -1,5 +1,4 @@
use crate::ast::{Call, Expr, Ident, Lit, Separated, Space, TableLit, TableLitElem}; use crate::ast::{Call, Expr, Ident, Lit, Separated, Space, TableLit, TableLitElem};
use crate::span::HasSpan;
// TODO Add span for just the parentheses to ast, or limit span to parentheses // TODO Add span for just the parentheses to ast, or limit span to parentheses
@ -81,14 +80,13 @@ impl Call {
return (new, true); return (new, true);
} }
let arg_span = s1.span().at_start(); let arg = Expr::Lit(Lit::Nil(span));
let arg = Expr::Lit(Lit::Nil(arg_span));
let new = Expr::Call(Self::Arg { let new = Expr::Call(Self::Arg {
expr: Box::new(expr), expr: Box::new(expr),
s0, s0,
s1, s1,
arg: Box::new(arg), arg: Box::new(arg),
s2: Space::empty(arg_span.at_end()), s2: Space::empty(span),
span, span,
}); });
(new, true) (new, true)
@ -112,13 +110,12 @@ impl Call {
} }
let arg = Expr::TableConstr(constr); let arg = Expr::TableConstr(constr);
let arg_span = arg.span();
let new = Expr::Call(Self::Arg { let new = Expr::Call(Self::Arg {
expr: Box::new(expr), expr: Box::new(expr),
s0, s0,
s1: Space::empty(arg_span.at_start()), s1: Space::empty(span),
arg: Box::new(arg), arg: Box::new(arg),
s2: Space::empty(arg_span.at_end()), s2: Space::empty(span),
span, span,
}); });
(new, true) (new, true)

View file

@ -1,5 +1,4 @@
use crate::ast::{Expr, Lit, Program, Space, TableLit}; use crate::ast::{Expr, Lit, Program, Space, TableLit};
use crate::span::HasSpan;
impl Program { impl Program {
pub fn desugar(self) -> (Self, bool) { pub fn desugar(self) -> (Self, bool) {
@ -25,14 +24,14 @@ impl Program {
s2, s2,
span, span,
}; };
(new, true) return (new, true);
} else { }
let elems_span = elems.span();
let table = TableLit { let table = TableLit {
s0: s1, s0: s1,
elems, elems,
s1: Space::empty(elems_span.at_end()), s1: Space::empty(span),
span: elems_span, span,
}; };
let new = Self::Expr { let new = Self::Expr {
s0, s0,
@ -44,5 +43,4 @@ impl Program {
} }
} }
} }
}
} }

View file

@ -24,14 +24,6 @@ impl Span {
let end = self.end.max(other.end); let end = self.end.max(other.end);
Self::new(start, end) Self::new(start, end)
} }
pub fn at_start(self) -> Self {
Self::new(self.start, self.start)
}
pub fn at_end(self) -> Self {
Self::new(self.end, self.end)
}
} }
impl fmt::Debug for Span { impl fmt::Debug for Span {