Support variable shorthands in map literals & translation maps in Hornbeam templates
This commit is contained in:
parent
63d03f3afb
commit
27d27a6955
@ -5,4 +5,4 @@ html
|
||||
|
||||
body
|
||||
h1
|
||||
@hello{name = $name}
|
||||
@hello{$name}
|
||||
|
@ -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 ~ "}" }
|
||||
|
||||
|
||||
|
@ -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> {
|
||||
|
Loading…
Reference in New Issue
Block a user