Add if else if chains
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
ef02e94591
commit
ff308649b9
10 changed files with 241 additions and 115 deletions
|
|
@ -227,7 +227,7 @@ pub enum TemplateAstExpr<'input> {
|
|||
chain: Vec<TemplateAstExpr<'input>>,
|
||||
},
|
||||
IfConditional {
|
||||
if_block: Box<TemplateAstExpr<'input>>,
|
||||
expression: Box<TemplateAstExpr<'input>>,
|
||||
},
|
||||
ConditionalContent {
|
||||
content: Vec<TemplateAstExpr<'input>>,
|
||||
|
|
@ -316,10 +316,10 @@ fn parse_conditional_chain<'input>(
|
|||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
trace("conditional_chain", |input: &mut Input<'input>| {
|
||||
let if_block = parse_conditional_if.map(Box::new).parse_next(input)?;
|
||||
let if_block = parse_conditional_if.parse_next(input)?;
|
||||
let mut chain = vec![];
|
||||
|
||||
chain.push(TemplateAstExpr::IfConditional { if_block });
|
||||
chain.push(if_block);
|
||||
|
||||
loop {
|
||||
let (content, end_block): (Vec<_>, _) = repeat_till(
|
||||
|
|
@ -344,7 +344,7 @@ fn parse_conditional_chain<'input>(
|
|||
|
||||
chain.push(end_block);
|
||||
|
||||
if dbg!(is_end) {
|
||||
if is_end {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -359,13 +359,17 @@ fn parse_conditional_if<'input>(
|
|||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
trace(
|
||||
"conditional",
|
||||
parse_block(preceded(
|
||||
TokenKind::ConditionalIf,
|
||||
cut_err(
|
||||
surrounded(ws, parse_value_expression)
|
||||
.context(AstError::ctx().msg("Expected an expression after 'if'")),
|
||||
),
|
||||
)),
|
||||
parse_block(
|
||||
preceded(
|
||||
TokenKind::ConditionalIf,
|
||||
cut_err(
|
||||
surrounded(ws, parse_value_expression)
|
||||
.map(Box::new)
|
||||
.context(AstError::ctx().msg("Expected an expression after 'if'")),
|
||||
),
|
||||
)
|
||||
.map(|expression| TemplateAstExpr::IfConditional { expression }),
|
||||
),
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
|
@ -559,16 +563,16 @@ mod tests {
|
|||
root: [
|
||||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
if_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: IfConditional {
|
||||
expression: VariableAccess(
|
||||
[Ident]"foo" (6..9),
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
[Whitespace]" " (12..13),
|
||||
),
|
||||
},
|
||||
post_whitespace_content: Some(
|
||||
[Whitespace]" " (12..13),
|
||||
),
|
||||
},
|
||||
ConditionalContent {
|
||||
content: [
|
||||
|
|
@ -687,14 +691,14 @@ mod tests {
|
|||
root: [
|
||||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
if_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: IfConditional {
|
||||
expression: VariableAccess(
|
||||
[Ident]"foo" (6..9),
|
||||
),
|
||||
post_whitespace_content: None,
|
||||
},
|
||||
post_whitespace_content: None,
|
||||
},
|
||||
ConditionalContent {
|
||||
content: [],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue