From 0346bb874bde174b3cd9ffaeb3512c893bb1538b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 29 Jun 2021 18:41:03 +0800 Subject: [PATCH] Make Expr::position const. --- src/ast.rs | 35 ++++++++++++----------------------- src/custom_syntax.rs | 4 ++-- src/engine.rs | 4 ++-- src/optimize.rs | 8 ++++---- src/parse.rs | 8 ++++---- 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 2c6c1297..e7c92ae8 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1052,7 +1052,7 @@ impl Stmt { } /// Get the [position][Position] of this statement. #[must_use] - pub fn position(&self) -> Position { + pub const fn position(&self) -> Position { match self { Self::Noop(pos) | Self::Continue(pos) @@ -1727,7 +1727,7 @@ pub enum Expr { /// [String][ImmutableString] constant. StringConstant(ImmutableString, Position), /// An interpolated [string][ImmutableString]. - InterpolatedString(Box>), + InterpolatedString(Box>, Position), /// [ expr, ... ] Array(Box>, Position), /// #{ name:expr, ... } @@ -1798,7 +1798,7 @@ impl fmt::Debug for Expr { Self::StringConstant(value, _) => write!(f, "{:?}", value), Self::Unit(_) => f.write_str("()"), - Self::InterpolatedString(x) => { + Self::InterpolatedString(x, _) => { f.write_str("InterpolatedString")?; return f.debug_list().entries(x.iter()).finish(); } @@ -1948,7 +1948,7 @@ impl Expr { /// Get the [position][Position] of the expression. #[inline] #[must_use] - pub fn position(&self) -> Position { + pub const fn position(&self) -> Position { match self { #[cfg(not(feature = "no_float"))] Self::FloatConstant(_, pos) => *pos, @@ -1964,14 +1964,8 @@ impl Expr { | Self::Variable(_, pos, _) | Self::Stack(_, pos) | Self::FnCall(_, pos) - | Self::Custom(_, pos) => *pos, - - Self::InterpolatedString(x) => x - .first() - .expect( - "never fails because an interpolated string always contains at least one item", - ) - .position(), + | Self::Custom(_, pos) + | Self::InterpolatedString(_, pos) => *pos, Self::Property(x) => (x.2).1, Self::Stmt(x) => x.1, @@ -2003,13 +1997,8 @@ impl Expr { | Self::Variable(_, pos, _) | Self::Stack(_, pos) | Self::FnCall(_, pos) - | Self::Custom(_, pos) => *pos = new_pos, - - Self::InterpolatedString(x) => { - x.first_mut() - .expect("never fails because an interpolated string always contains at least one item") - .set_position(new_pos); - } + | Self::Custom(_, pos) + | Self::InterpolatedString(_, pos) => *pos = new_pos, Self::Property(x) => (x.2).1 = new_pos, Self::Stmt(x) => x.1 = new_pos, @@ -2024,7 +2013,7 @@ impl Expr { #[must_use] pub fn is_pure(&self) -> bool { match self { - Self::InterpolatedString(x) | Self::Array(x, _) => x.iter().all(Self::is_pure), + Self::InterpolatedString(x, _) | Self::Array(x, _) => x.iter().all(Self::is_pure), Self::Map(x, _) => x.0.iter().map(|(_, v)| v).all(Self::is_pure), @@ -2062,7 +2051,7 @@ impl Expr { | Self::Unit(_) | Self::Stack(_, _) => true, - Self::InterpolatedString(x) | Self::Array(x, _) => x.iter().all(Self::is_constant), + Self::InterpolatedString(x, _) | Self::Array(x, _) => x.iter().all(Self::is_constant), Self::Map(x, _) => x.0.iter().map(|(_, expr)| expr).all(Self::is_constant), @@ -2092,7 +2081,7 @@ impl Expr { Self::IntegerConstant(_, _) | Self::StringConstant(_, _) - | Self::InterpolatedString(_) + | Self::InterpolatedString(_, _) | Self::FnCall(_, _) | Self::Stmt(_) | Self::Dot(_, _) @@ -2147,7 +2136,7 @@ impl Expr { } } } - Self::InterpolatedString(x) | Self::Array(x, _) => { + Self::InterpolatedString(x, _) | Self::Array(x, _) => { for e in x.as_ref() { if !e.walk(path, on_node) { return false; diff --git a/src/custom_syntax.rs b/src/custom_syntax.rs index adcd1a12..766720a7 100644 --- a/src/custom_syntax.rs +++ b/src/custom_syntax.rs @@ -67,13 +67,13 @@ impl Expression<'_> { /// Get the expression. #[inline(always)] #[must_use] - pub(crate) fn expr(&self) -> &Expr { + pub(crate) const fn expr(&self) -> &Expr { &self.0 } /// Get the position of this expression. #[inline(always)] #[must_use] - pub fn position(&self) -> Position { + pub const fn position(&self) -> Position { self.0.position() } /// Get the value of this expression if it is a literal constant. diff --git a/src/engine.rs b/src/engine.rs index 99d9ff8a..45b63da4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2009,8 +2009,8 @@ impl Engine { } // `... ${...} ...` - Expr::InterpolatedString(x) => { - let mut pos = expr.position(); + Expr::InterpolatedString(x, pos) => { + let mut pos = *pos; let mut result: Dynamic = self.empty_string.clone().into(); for expr in x.iter() { diff --git a/src/optimize.rs b/src/optimize.rs index 87179054..60a3596b 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -787,17 +787,17 @@ fn optimize_expr(expr: &mut Expr, state: &mut State, _chaining: bool) { #[cfg(not(feature = "no_index"))] Expr::Index(x, _) => { optimize_expr(&mut x.lhs, state, false); optimize_expr(&mut x.rhs, state, _chaining); } // `` - Expr::InterpolatedString(x) if x.is_empty() => { + Expr::InterpolatedString(x, pos) if x.is_empty() => { state.set_dirty(); - *expr = Expr::StringConstant(state.engine.empty_string.clone(), Position::NONE); + *expr = Expr::StringConstant(state.engine.empty_string.clone(), *pos); } // `...` - Expr::InterpolatedString(x) if x.len() == 1 && matches!(x[0], Expr::StringConstant(_, _)) => { + Expr::InterpolatedString(x, _) if x.len() == 1 && matches!(x[0], Expr::StringConstant(_, _)) => { state.set_dirty(); *expr = mem::take(&mut x[0]); } // `... ${ ... } ...` - Expr::InterpolatedString(x) => { + Expr::InterpolatedString(x, _) => { let mut n = 0; // Merge consecutive strings diff --git a/src/parse.rs b/src/parse.rs index ca3d601d..7786d489 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -515,7 +515,7 @@ fn parse_index_chain( Expr::IntegerConstant(_, _) | Expr::Array(_, _) | Expr::StringConstant(_, _) - | Expr::InterpolatedString(_) => (), + | Expr::InterpolatedString(_, _) => (), Expr::Map(_, _) => { return Err(PERR::MalformedIndexExpr( @@ -547,10 +547,10 @@ fn parse_index_chain( }, // lhs[string] - Expr::StringConstant(_, _) | Expr::InterpolatedString(_) => match lhs { + Expr::StringConstant(_, _) | Expr::InterpolatedString(_, _) => match lhs { Expr::Map(_, _) => (), - Expr::Array(_, _) | Expr::StringConstant(_, _) | Expr::InterpolatedString(_) => { + Expr::Array(_, _) | Expr::StringConstant(_, _) | Expr::InterpolatedString(_, _) => { return Err(PERR::MalformedIndexExpr( "Array or string expects numeric index, not a string".into(), ) @@ -1132,7 +1132,7 @@ fn parse_primary( } segments.shrink_to_fit(); - Expr::InterpolatedString(segments.into()) + Expr::InterpolatedString(segments.into(), settings.pos) } // Array literal