diff --git a/crates/simplexpr/src/ast.rs b/crates/simplexpr/src/ast.rs index 4139e75..b7aa2da 100644 --- a/crates/simplexpr/src/ast.rs +++ b/crates/simplexpr/src/ast.rs @@ -31,11 +31,7 @@ pub enum UnaryOp { #[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum SimplExpr { - // TODO figure out if that span value here is even necessary,.. - // DynVal has span information. However, keeping the span here and the span of the dynval separate - // would allow to separate the span of where a dynval was defined and the - // span of the use-site when a literal is used to replace a varref in the evaluation process. - Literal(Span, DynVal), + Literal(DynVal), VarRef(Span, VarName), BinOp(Span, Box, BinOp, Box), UnaryOp(Span, UnaryOp, Box), @@ -47,8 +43,8 @@ pub enum SimplExpr { impl std::fmt::Display for SimplExpr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + SimplExpr::Literal(x) => write!(f, "\"{}\"", x), SimplExpr::VarRef(_, x) => write!(f, "{}", x), - SimplExpr::Literal(_, x) => write!(f, "\"{}\"", x), SimplExpr::BinOp(_, l, op, r) => write!(f, "({} {} {})", l, op, r), SimplExpr::UnaryOp(_, op, x) => write!(f, "{}{}", op, x), SimplExpr::IfElse(_, a, b, c) => write!(f, "({} ? {} : {})", a, b, c), @@ -61,23 +57,23 @@ impl std::fmt::Display for SimplExpr { } impl SimplExpr { pub fn literal(span: Span, s: String) -> Self { - Self::Literal(span, DynVal(s, span)) + Self::Literal(DynVal(s, span)) } /// Construct a synthetic simplexpr from a literal string, without adding any relevant span information (uses [DUMMY_SPAN]) pub fn synth_string(s: String) -> Self { - Self::Literal(Span::DUMMY, DynVal(s, Span::DUMMY)) + Self::Literal(DynVal(s, Span::DUMMY)) } /// Construct a synthetic simplexpr from a literal dynval, without adding any relevant span information (uses [DUMMY_SPAN]) pub fn synth_literal>(s: T) -> Self { - Self::Literal(Span::DUMMY, s.into()) + Self::Literal(s.into()) } } impl Spanned for SimplExpr { fn span(&self) -> Span { match self { - SimplExpr::Literal(span, _) => *span, + SimplExpr::Literal(x) => x.span(), SimplExpr::VarRef(span, _) => *span, SimplExpr::BinOp(span, ..) => *span, SimplExpr::UnaryOp(span, ..) => *span, diff --git a/crates/simplexpr/src/eval.rs b/crates/simplexpr/src/eval.rs index 159d35b..cf508ca 100644 --- a/crates/simplexpr/src/eval.rs +++ b/crates/simplexpr/src/eval.rs @@ -92,7 +92,7 @@ impl SimplExpr { pub fn resolve_refs(self, variables: &HashMap) -> Result { use SimplExpr::*; match self { - Literal(span, x) => Ok(Literal(span, x)), + Literal(x) => Ok(Literal(x)), BinOp(span, box a, op, box b) => Ok(BinOp(span, box a.resolve_refs(variables)?, op, box b.resolve_refs(variables)?)), UnaryOp(span, op, box x) => Ok(UnaryOp(span, op, box x.resolve_refs(variables)?)), IfElse(span, box a, box b, box c) => { @@ -107,7 +107,7 @@ impl SimplExpr { args.into_iter().map(|a| a.resolve_refs(variables)).collect::>()?, )), VarRef(span, ref name) => match variables.get(name) { - Some(value) => Ok(Literal(span, value.clone())), + Some(value) => Ok(Literal(value.clone())), None => Err(EvalError::UnknownVariable(name.clone()).at(span)), }, } @@ -147,7 +147,7 @@ impl SimplExpr { pub fn eval(&self, values: &HashMap) -> Result { let span = self.span(); let value = match self { - SimplExpr::Literal(_, x) => Ok(x.clone()), + SimplExpr::Literal(x) => Ok(x.clone()), SimplExpr::VarRef(span, ref name) => { Ok(values.get(name).cloned().ok_or_else(|| EvalError::UnknownVariable(name.clone()).at(*span))?.at(*span)) } diff --git a/crates/simplexpr/src/parser/lalrpop_helpers.rs b/crates/simplexpr/src/parser/lalrpop_helpers.rs index 7d22f13..19e2fa5 100644 --- a/crates/simplexpr/src/parser/lalrpop_helpers.rs +++ b/crates/simplexpr/src/parser/lalrpop_helpers.rs @@ -21,7 +21,7 @@ pub fn parse_stringlit( let span = Span(lo, hi, file_id); match segment { StrLitSegment::Literal(lit) if lit.is_empty() => None, - StrLitSegment::Literal(lit) => Some(Ok(SimplExpr::Literal(span, DynVal(lit, span)))), + StrLitSegment::Literal(lit) => Some(Ok(SimplExpr::Literal(DynVal(lit, span)))), StrLitSegment::Interp(toks) => { let token_stream = toks.into_iter().map(|x| Ok(x)); Some(parser.parse(file_id, token_stream)) @@ -32,5 +32,5 @@ pub fn parse_stringlit( Some(ast) => Some(SimplExpr::BinOp(span, Box::new(ast), BinOp::Plus, Box::new(cur))), None => Some(cur), }) - .map(|ast| ast.unwrap_or_else(|| SimplExpr::Literal(span, DynVal(String::new(), span)))) + .map(|ast| ast.unwrap_or_else(|| SimplExpr::Literal(DynVal(String::new(), span)))) } diff --git a/crates/simplexpr/src/simplexpr_parser.lalrpop b/crates/simplexpr/src/simplexpr_parser.lalrpop index a72afd6..10028b7 100644 --- a/crates/simplexpr/src/simplexpr_parser.lalrpop +++ b/crates/simplexpr/src/simplexpr_parser.lalrpop @@ -76,7 +76,7 @@ pub Expr: SimplExpr = { "[" "]" => JsonAccess(Span(l, r, fid), b(value), b(index)), "." => { - JsonAccess(Span(l, r, fid), b(value), b(Literal(Span(lit_l, r, fid), index.into()))) + JsonAccess(Span(l, r, fid), b(value), b(Literal(index.into()))) }, #[precedence(level="2")] #[assoc(side="right")] diff --git a/crates/yuck/src/config/snapshots/yuck__config__test__config.snap b/crates/yuck/src/config/snapshots/yuck__config__test__config.snap index ef3554d..d4a0ba3 100644 --- a/crates/yuck/src/config/snapshots/yuck__config__test__config.snap +++ b/crates/yuck/src/config/snapshots/yuck__config__test__config.snap @@ -18,7 +18,7 @@ Config( attrs: { AttrName("arg"): AttrEntry( key_span: Span(52, 56, 0), - value: SimplExpr(Span(57, 61, 0), Literal(Span(57, 61, 0), DynVal("hi", Span(57, 61, 0)))), + value: SimplExpr(Span(57, 61, 0), Literal(DynVal("hi", Span(57, 61, 0)))), ), }, ), @@ -56,7 +56,7 @@ Config( attrs: { AttrName("arg"): AttrEntry( key_span: Span(468, 472, 0), - value: SimplExpr(Span(473, 478, 0), Literal(Span(473, 478, 0), DynVal("bla", Span(473, 478, 0)))), + value: SimplExpr(Span(473, 478, 0), Literal(DynVal("bla", Span(473, 478, 0)))), ), }, ), diff --git a/crates/yuck/src/parser/ast.rs b/crates/yuck/src/parser/ast.rs index 41ca09c..2361ea5 100644 --- a/crates/yuck/src/parser/ast.rs +++ b/crates/yuck/src/parser/ast.rs @@ -113,7 +113,7 @@ impl std::fmt::Display for Ast { Array(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")), Keyword(_, x) => write!(f, ":{}", x), Symbol(_, x) => write!(f, "{}", x), - SimplExpr(_, simplexpr::SimplExpr::Literal(_, value)) => write!(f, "\"{}\"", value), + SimplExpr(_, simplexpr::SimplExpr::Literal(value)) => write!(f, "\"{}\"", value), SimplExpr(_, x) => write!(f, "{{{}}}", x), Comment(_) => write!(f, ""), } diff --git a/crates/yuck/src/parser/ast_iterator.rs b/crates/yuck/src/parser/ast_iterator.rs index 26d2a4f..c2b5663 100644 --- a/crates/yuck/src/parser/ast_iterator.rs +++ b/crates/yuck/src/parser/ast_iterator.rs @@ -46,11 +46,8 @@ impl> AstIterator { pub fn expect_literal(&mut self) -> AstResult<(Span, DynVal)> { // TODO add some others match self.expect_any()? { - // Ast::List(_, _) => todo!(), // Ast::Array(_, _) => todo!(), - // Ast::Keyword(_, _) => todo!(), - // Ast::Symbol(_, _) => todo!(), - Ast::SimplExpr(span, expr) => Ok((span, dbg!(expr.eval_no_vars()).map_err(|e| AstError::SimplExpr(e.into()))?)), + Ast::SimplExpr(span, expr) => Ok((span, expr.eval_no_vars().map_err(|e| AstError::SimplExpr(e.into()))?)), other => { let span = other.span(); let actual_type = other.expr_type();