diff --git a/demo_hornbeam_project/templates/pages/say_hello.hnb b/demo_hornbeam_project/templates/pages/say_hello.hnb index f05beef..e899198 100644 --- a/demo_hornbeam_project/templates/pages/say_hello.hnb +++ b/demo_hornbeam_project/templates/pages/say_hello.hnb @@ -5,4 +5,4 @@ html body h1 - @hello{name = $name} + @hello{$name} diff --git a/hornbeam_grammar/src/hornbeam.pest b/hornbeam_grammar/src/hornbeam.pest index 4b9b310..c04e388 100644 --- a/hornbeam_grammar/src/hornbeam.pest +++ b/hornbeam_grammar/src/hornbeam.pest @@ -167,7 +167,8 @@ Variable = { "$" ~ Identifier } ListLiteral = { "[" ~ commaSeparatedExprs ~ "]" } KVPair = { Identifier ~ wsnl* ~ "=" ~ wsnl* ~ Expr } -commaSeparatedKVPairs = _{ wsnl* ~ (KVPair ~ wsnl* ~ ("," ~ wsnl* ~ KVPair ~ wsnl*)* ~ ("," ~ wsnl*)?)? } +KVarShorthand = { Variable } +commaSeparatedKVPairs = _{ wsnl* ~ ((KVPair | KVarShorthand) ~ wsnl* ~ ("," ~ wsnl* ~ (KVPair | KVarShorthand) ~ wsnl*)* ~ ("," ~ wsnl*)?)? } MapLiteral = { "{" ~ commaSeparatedKVPairs ~ "}" } diff --git a/hornbeam_grammar/src/parser.rs b/hornbeam_grammar/src/parser.rs index 267d1a2..b2fff71 100644 --- a/hornbeam_grammar/src/parser.rs +++ b/hornbeam_grammar/src/parser.rs @@ -235,10 +235,27 @@ impl HornbeamParser { )) } + fn KVarShorthand(input: Node) -> PCResult<(IStr, Expression)> { + let node = input.into_children().single()?; + let var_expr = HornbeamParser::Variable(node)?; + if let Expression::Variable { name, .. } = &var_expr { + Ok((name.clone(), var_expr)) + } else { + unreachable!("Variable should also be returned from Variable"); + } + } + fn MapLiteral(input: Node) -> PCResult> { - Ok(match_nodes!(input.into_children(); - [KVPair(kv_pair)..] => kv_pair.collect() - )) + input + .into_children() + .map(|node| match node.as_rule() { + Rule::KVPair => HornbeamParser::KVPair(node), + Rule::KVarShorthand => HornbeamParser::KVarShorthand(node), + other => { + unimplemented!("unexpected {other:?} in MapLiteral"); + } + }) + .collect() } fn SEscape(input: Node) -> PCResult {