Support variable shorthands in map literals & translation maps in Hornbeam templates

This commit is contained in:
Olivier 'reivilibre' 2023-03-04 10:52:36 +00:00 committed by Olivier 'reivilibre
parent 63d03f3afb
commit 27d27a6955
3 changed files with 23 additions and 5 deletions

View File

@ -5,4 +5,4 @@ html
body
h1
@hello{name = $name}
@hello{$name}

View File

@ -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 ~ "}" }

View File

@ -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<BTreeMap<IStr, Expression>> {
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<IStr> {