From b84d5ae0c8a8ebedab1e6f8217e5384fd2723a6d Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 21 Nov 2022 13:33:40 +0100 Subject: [PATCH] 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. --- src/desugar/call.rs | 11 ++++------- src/desugar/program.rs | 32 +++++++++++++++----------------- src/span.rs | 8 -------- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/desugar/call.rs b/src/desugar/call.rs index 52cd41f..339c956 100644 --- a/src/desugar/call.rs +++ b/src/desugar/call.rs @@ -1,5 +1,4 @@ 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 @@ -81,14 +80,13 @@ impl Call { return (new, true); } - let arg_span = s1.span().at_start(); - let arg = Expr::Lit(Lit::Nil(arg_span)); + let arg = Expr::Lit(Lit::Nil(span)); let new = Expr::Call(Self::Arg { expr: Box::new(expr), s0, s1, arg: Box::new(arg), - s2: Space::empty(arg_span.at_end()), + s2: Space::empty(span), span, }); (new, true) @@ -112,13 +110,12 @@ impl Call { } let arg = Expr::TableConstr(constr); - let arg_span = arg.span(); let new = Expr::Call(Self::Arg { expr: Box::new(expr), s0, - s1: Space::empty(arg_span.at_start()), + s1: Space::empty(span), arg: Box::new(arg), - s2: Space::empty(arg_span.at_end()), + s2: Space::empty(span), span, }); (new, true) diff --git a/src/desugar/program.rs b/src/desugar/program.rs index 6598fed..a529b07 100644 --- a/src/desugar/program.rs +++ b/src/desugar/program.rs @@ -1,5 +1,4 @@ use crate::ast::{Expr, Lit, Program, Space, TableLit}; -use crate::span::HasSpan; impl Program { pub fn desugar(self) -> (Self, bool) { @@ -25,23 +24,22 @@ impl Program { s2, span, }; - (new, true) - } else { - let elems_span = elems.span(); - let table = TableLit { - s0: s1, - elems, - s1: Space::empty(elems_span.at_end()), - span: elems_span, - }; - let new = Self::Expr { - s0, - expr: Expr::Lit(Lit::Table(table)), - s1: s2, - span, - }; - (new, true) + return (new, true); } + + let table = TableLit { + s0: s1, + elems, + s1: Space::empty(span), + span, + }; + let new = Self::Expr { + s0, + expr: Expr::Lit(Lit::Table(table)), + s1: s2, + span, + }; + (new, true) } } } diff --git a/src/span.rs b/src/span.rs index 42cf32c..296f992 100644 --- a/src/span.rs +++ b/src/span.rs @@ -24,14 +24,6 @@ impl Span { let end = self.end.max(other.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 {