Add parsing for conditionals (cont.)
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
974086a877
commit
8afc2d1bde
29 changed files with 994 additions and 746 deletions
345
src/ast/mod.rs
345
src/ast/mod.rs
|
|
@ -6,18 +6,22 @@ use winnow::combinator::cut_err;
|
|||
use winnow::combinator::delimited;
|
||||
use winnow::combinator::not;
|
||||
use winnow::combinator::opt;
|
||||
use winnow::combinator::peek;
|
||||
use winnow::combinator::preceded;
|
||||
use winnow::combinator::repeat;
|
||||
use winnow::combinator::repeat_till;
|
||||
use winnow::combinator::trace;
|
||||
use winnow::error::AddContext;
|
||||
use winnow::error::FromRecoverableError;
|
||||
use winnow::error::ModalError;
|
||||
use winnow::error::ParserError;
|
||||
use winnow::stream::Offset;
|
||||
use winnow::stream::Recoverable;
|
||||
use winnow::stream::Stream;
|
||||
use winnow::stream::TokenSlice;
|
||||
use winnow::token::any;
|
||||
|
||||
use crate::SourceSpan;
|
||||
use crate::parser::TemplateToken;
|
||||
use crate::parser::TokenKind;
|
||||
use crate::resume_after_cut;
|
||||
|
|
@ -83,6 +87,29 @@ impl FromRecoverableError<Input<'_>, AstError> for AstError {
|
|||
input: &Input,
|
||||
mut e: AstError,
|
||||
) -> Self {
|
||||
e.span = e.span.or_else(|| {
|
||||
let offset = input.offset_from(token_start);
|
||||
|
||||
let mut tokens = input
|
||||
.previous_tokens()
|
||||
.take(offset)
|
||||
.filter(|t| t.kind() != TokenKind::Whitespace);
|
||||
let last = tokens.next();
|
||||
let first = tokens.last();
|
||||
match (last, first) {
|
||||
(None, None) => None,
|
||||
(None, Some(single)) | (Some(single), None) => Some(SourceSpan {
|
||||
range: single.source().get_range(),
|
||||
}),
|
||||
(Some(last), Some(first)) => {
|
||||
let start = first.source().get_range().start;
|
||||
let end = last.source().get_range().end;
|
||||
|
||||
Some(SourceSpan { range: start..end })
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
e
|
||||
}
|
||||
}
|
||||
|
|
@ -117,7 +144,9 @@ impl ParserError<Input<'_>> for AstError {
|
|||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub struct AstFailure {}
|
||||
pub struct AstFailure {
|
||||
errors: Vec<AstError>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AstFailure {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
|
|
@ -126,8 +155,40 @@ impl std::fmt::Display for AstFailure {
|
|||
}
|
||||
|
||||
impl AstFailure {
|
||||
fn from_errors(_errors: Vec<AstError>, _input: &[TemplateToken]) -> AstFailure {
|
||||
AstFailure {}
|
||||
fn from_errors(errors: Vec<AstError>) -> AstFailure {
|
||||
AstFailure { errors }
|
||||
}
|
||||
|
||||
pub fn to_report(&self, source: &str) -> String {
|
||||
let reports = self
|
||||
.errors
|
||||
.iter()
|
||||
.map(|error| {
|
||||
annotate_snippets::Level::ERROR
|
||||
.primary_title(
|
||||
error
|
||||
.message
|
||||
.as_deref()
|
||||
.unwrap_or("An error occurred while producing an Ast"),
|
||||
)
|
||||
.element(
|
||||
annotate_snippets::Snippet::source(source).annotation(
|
||||
annotate_snippets::AnnotationKind::Primary
|
||||
.span(error.span.clone().map(|s| s.range).unwrap_or_else(|| 0..0)),
|
||||
),
|
||||
)
|
||||
.elements(
|
||||
error
|
||||
.help
|
||||
.as_ref()
|
||||
.map(|help| annotate_snippets::Level::HELP.message(help)),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let renderer = annotate_snippets::Renderer::styled()
|
||||
.decor_style(annotate_snippets::renderer::DecorStyle::Unicode);
|
||||
renderer.render(&reports)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -149,7 +210,7 @@ pub fn parse(input: &[TemplateToken]) -> Result<TemplateAst<'_>, AstFailure> {
|
|||
{
|
||||
Ok(TemplateAst { root: val })
|
||||
} else {
|
||||
Err(AstFailure::from_errors(errors, input))
|
||||
Err(AstFailure::from_errors(errors))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -157,12 +218,6 @@ pub fn parse(input: &[TemplateToken]) -> Result<TemplateAst<'_>, AstFailure> {
|
|||
pub enum TemplateAstExpr<'input> {
|
||||
StaticContent(TemplateToken),
|
||||
Interpolation {
|
||||
prev_whitespace_content: Option<TemplateToken>,
|
||||
wants_output: TemplateToken,
|
||||
expression: Box<TemplateAstExpr<'input>>,
|
||||
post_whitespace_content: Option<TemplateToken>,
|
||||
},
|
||||
Action {
|
||||
prev_whitespace_content: Option<TemplateToken>,
|
||||
expression: Box<TemplateAstExpr<'input>>,
|
||||
post_whitespace_content: Option<TemplateToken>,
|
||||
|
|
@ -172,7 +227,7 @@ pub enum TemplateAstExpr<'input> {
|
|||
chain: Vec<TemplateAstExpr<'input>>,
|
||||
},
|
||||
IfConditional {
|
||||
expression: Box<TemplateAstExpr<'input>>,
|
||||
if_block: Box<TemplateAstExpr<'input>>,
|
||||
content: Vec<TemplateAstExpr<'input>>,
|
||||
end_block: Box<TemplateAstExpr<'input>>,
|
||||
},
|
||||
|
|
@ -193,8 +248,11 @@ fn parse_asts<'input>(input: &mut Input<'input>) -> Result<Vec<TemplateAstExpr<'
|
|||
}
|
||||
fn parse_ast<'input>(input: &mut Input<'input>) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
alt((
|
||||
TokenKind::Content.map(TemplateAstExpr::StaticContent),
|
||||
parse_interpolation,
|
||||
trace(
|
||||
"content",
|
||||
TokenKind::Content.map(TemplateAstExpr::StaticContent),
|
||||
),
|
||||
trace("interpolation", parse_interpolation),
|
||||
parse_action,
|
||||
))
|
||||
.parse_next(input)
|
||||
|
|
@ -205,16 +263,16 @@ fn parse_interpolation<'input>(
|
|||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
let expr_parser = resume_after_cut(
|
||||
parse_value_expression,
|
||||
repeat_till(1.., any, TokenKind::RightDelim).map(|((), _)| ()),
|
||||
repeat_till(0.., any, peek(TokenKind::RightDelim)).map(|((), _)| ()),
|
||||
)
|
||||
.with_taken()
|
||||
.map(|(expr, taken)| expr.unwrap_or(TemplateAstExpr::Invalid(taken)));
|
||||
let (prev_whitespace, _left, wants_output, (expression, _right, post_whitespace)) = (
|
||||
let (prev_whitespace, _left, _wants_output, (expression, _right, post_whitespace)) = (
|
||||
opt(TokenKind::Whitespace),
|
||||
TokenKind::LeftDelim,
|
||||
TokenKind::WantsOutput,
|
||||
cut_err((
|
||||
delimited(ignore_ws, expr_parser, ignore_ws).map(Box::new),
|
||||
surrounded(ws, expr_parser).map(Box::new),
|
||||
TokenKind::RightDelim,
|
||||
opt(TokenKind::Whitespace),
|
||||
)),
|
||||
|
|
@ -223,52 +281,99 @@ fn parse_interpolation<'input>(
|
|||
|
||||
Ok(TemplateAstExpr::Interpolation {
|
||||
prev_whitespace_content: prev_whitespace,
|
||||
wants_output,
|
||||
expression,
|
||||
post_whitespace_content: post_whitespace,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_value_expression<'input>(
|
||||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
alt((parse_variable_access,)).parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_action<'input>(input: &mut Input<'input>) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
alt((parse_conditional_chain,)).parse_next(input)
|
||||
trace(
|
||||
"action",
|
||||
alt((
|
||||
parse_conditional_chain,
|
||||
(parse_block(
|
||||
cut_err(not(repeat_till(
|
||||
0..,
|
||||
any,
|
||||
peek((ws, TokenKind::RightDelim)),
|
||||
)
|
||||
.map(|((), _)| ())))
|
||||
.context(
|
||||
AstError::ctx()
|
||||
.msg("Standlone action block")
|
||||
.help("If you want to output this expression, add a '=' to the block"),
|
||||
)
|
||||
.take()
|
||||
.map(TemplateAstExpr::Invalid),
|
||||
)),
|
||||
)),
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_conditional_chain<'input>(
|
||||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
let if_expression = parse_conditional.parse_next(input)?;
|
||||
let mut chain = vec![];
|
||||
trace("conditional_chain", |input: &mut Input<'input>| {
|
||||
let if_block = parse_conditional.map(Box::new).parse_next(input)?;
|
||||
let mut chain = vec![];
|
||||
|
||||
let (content, end_block): (Vec<_>, _) =
|
||||
repeat_till(1.., parse_ast, parse_end).parse_next(input)?;
|
||||
let (content, end_block): (Vec<_>, _) =
|
||||
repeat_till(0.., parse_ast, parse_end.map(Box::new)).parse_next(input)?;
|
||||
|
||||
chain.push(TemplateAstExpr::IfConditional {
|
||||
expression: Box::new(if_expression),
|
||||
content,
|
||||
end_block: Box::new(end_block),
|
||||
});
|
||||
chain.push(TemplateAstExpr::IfConditional {
|
||||
if_block,
|
||||
content,
|
||||
end_block,
|
||||
});
|
||||
|
||||
Ok(TemplateAstExpr::ConditionalChain { chain })
|
||||
Ok(TemplateAstExpr::ConditionalChain { chain })
|
||||
})
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_conditional<'input>(
|
||||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
parse_block(preceded(
|
||||
TokenKind::ConditionalIf,
|
||||
surrounded(ignore_ws, parse_value_expression),
|
||||
))
|
||||
trace(
|
||||
"conditional",
|
||||
parse_block(preceded(
|
||||
TokenKind::ConditionalIf,
|
||||
cut_err(
|
||||
surrounded(ws, parse_value_expression)
|
||||
.context(AstError::ctx().msg("Expected an expression after 'if'")),
|
||||
),
|
||||
)),
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_end<'input>(input: &mut Input<'input>) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
parse_block(TokenKind::End.value(TemplateAstExpr::EndBlock)).parse_next(input)
|
||||
trace(
|
||||
"end",
|
||||
parse_block(
|
||||
TokenKind::End
|
||||
.value(TemplateAstExpr::EndBlock)
|
||||
.context(AstError::ctx().msg("Expected an end block here")),
|
||||
),
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_value_expression<'input>(
|
||||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
trace("value_expression", alt((parse_variable_access,))).parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_variable_access<'input>(
|
||||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
trace(
|
||||
"variable_access",
|
||||
TokenKind::Ident.map(TemplateAstExpr::VariableAccess),
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_block<'input, ParseNext>(
|
||||
|
|
@ -279,7 +384,7 @@ where
|
|||
{
|
||||
let expr_parser = resume_after_cut(
|
||||
parser,
|
||||
repeat_till(1.., any, TokenKind::RightDelim).map(|((), _)| ()),
|
||||
repeat_till(0.., any, peek(TokenKind::RightDelim)).map(|((), _)| ()),
|
||||
)
|
||||
.with_taken()
|
||||
.map(|(expr, taken)| expr.unwrap_or(TemplateAstExpr::Invalid(taken)));
|
||||
|
|
@ -288,11 +393,11 @@ where
|
|||
opt(TokenKind::Whitespace),
|
||||
TokenKind::LeftDelim,
|
||||
not(TokenKind::WantsOutput),
|
||||
cut_err((
|
||||
delimited(ignore_ws, expr_parser.map(Box::new), ignore_ws),
|
||||
(
|
||||
surrounded(ws, expr_parser.map(Box::new)),
|
||||
TokenKind::RightDelim,
|
||||
opt(TokenKind::Whitespace),
|
||||
)),
|
||||
),
|
||||
)
|
||||
.map(
|
||||
|(prev_whitespace, _left, _not_token, (expression, _right, post_whitespace))| {
|
||||
|
|
@ -305,15 +410,7 @@ where
|
|||
)
|
||||
}
|
||||
|
||||
fn parse_variable_access<'input>(
|
||||
input: &mut Input<'input>,
|
||||
) -> Result<TemplateAstExpr<'input>, AstError> {
|
||||
TokenKind::Ident
|
||||
.map(TemplateAstExpr::VariableAccess)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn ignore_ws<'input>(input: &mut Input<'input>) -> Result<(), AstError> {
|
||||
fn ws<'input>(input: &mut Input<'input>) -> Result<(), AstError> {
|
||||
repeat(.., TokenKind::Whitespace).parse_next(input)
|
||||
}
|
||||
|
||||
|
|
@ -333,7 +430,19 @@ where
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use winnow::Parser;
|
||||
use winnow::combinator::alt;
|
||||
use winnow::combinator::fail;
|
||||
use winnow::stream::TokenSlice;
|
||||
|
||||
use crate::ast::AstError;
|
||||
use crate::ast::AstFailure;
|
||||
use crate::ast::TemplateAst;
|
||||
use crate::ast::TemplateAstExpr;
|
||||
use crate::ast::parse;
|
||||
use crate::ast::parse_block;
|
||||
use crate::ast::parse_end;
|
||||
use crate::parser::TokenKind;
|
||||
|
||||
#[test]
|
||||
fn check_only_content() {
|
||||
|
|
@ -347,10 +456,7 @@ mod tests {
|
|||
TemplateAst {
|
||||
root: [
|
||||
StaticContent(
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hello World",
|
||||
},
|
||||
"Hello World" (0..11),
|
||||
),
|
||||
],
|
||||
}
|
||||
|
|
@ -369,27 +475,14 @@ mod tests {
|
|||
TemplateAst {
|
||||
root: [
|
||||
StaticContent(
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hello",
|
||||
},
|
||||
"Hello" (0..5),
|
||||
),
|
||||
Interpolation {
|
||||
prev_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
" " (5..6),
|
||||
),
|
||||
wants_output: TemplateToken {
|
||||
kind: WantsOutput,
|
||||
source: "=",
|
||||
},
|
||||
expression: VariableAccess(
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "world",
|
||||
},
|
||||
"world" (10..15),
|
||||
),
|
||||
post_whitespace_content: None,
|
||||
},
|
||||
|
|
@ -412,35 +505,23 @@ mod tests {
|
|||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
expression: Block {
|
||||
if_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: VariableAccess(
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "foo",
|
||||
},
|
||||
"foo" (6..9),
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
" " (12..13),
|
||||
),
|
||||
},
|
||||
content: [
|
||||
StaticContent(
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hiii",
|
||||
},
|
||||
"Hiii" (13..17),
|
||||
),
|
||||
],
|
||||
end_block: Block {
|
||||
prev_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
" " (17..18),
|
||||
),
|
||||
expression: EndBlock,
|
||||
post_whitespace_content: None,
|
||||
|
|
@ -453,18 +534,98 @@ mod tests {
|
|||
"#);
|
||||
}
|
||||
|
||||
fn panic_pretty<'a>(
|
||||
input: &'_ str,
|
||||
tokens: Result<TemplateAst<'a>, AstFailure>,
|
||||
) -> TemplateAst<'a> {
|
||||
match tokens {
|
||||
Ok(ast) => ast,
|
||||
Err(failure) => {
|
||||
panic!("{}", failure.to_report(input));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_invalid_action() {
|
||||
let input = r#"{{ value }}
|
||||
{{ value }}
|
||||
{{ value }}
|
||||
{{ value }}
|
||||
{{ value }}"#;
|
||||
|
||||
let parsed = crate::parser::parse(input.into()).unwrap();
|
||||
|
||||
let ast = parse(parsed.tokens()).unwrap_err();
|
||||
|
||||
insta::assert_snapshot!(ast.to_report(input));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_nested_simple_if() {
|
||||
let input = r#"{{ if foo }}
|
||||
{{ if bar }}
|
||||
Hiii
|
||||
{{ end }}
|
||||
{{ end }}"#;
|
||||
{{ end }}
|
||||
|
||||
{{= value }}
|
||||
"#;
|
||||
|
||||
let parsed = crate::parser::parse(input.into()).unwrap();
|
||||
|
||||
let ast = parse(parsed.tokens()).unwrap();
|
||||
insta::assert_debug_snapshot!("simple_if_tokens", parsed);
|
||||
|
||||
insta::assert_debug_snapshot!(ast);
|
||||
let ast = panic_pretty(input, parse(parsed.tokens()));
|
||||
|
||||
insta::assert_debug_snapshot!("simple_if_ast", ast);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_parsing_block() {
|
||||
use winnow::RecoverableParser;
|
||||
|
||||
let input = "{{ foo }}";
|
||||
|
||||
let parsed = crate::parser::parse(input.into()).unwrap();
|
||||
|
||||
let result = alt((
|
||||
parse_end,
|
||||
parse_block(
|
||||
(TokenKind::Ident.void(), fail::<_, (), _>)
|
||||
.void()
|
||||
.context(AstError::ctx().msg("No ident allowed"))
|
||||
.take()
|
||||
.map(TemplateAstExpr::Invalid),
|
||||
),
|
||||
))
|
||||
.recoverable_parse(TokenSlice::new(parsed.tokens()));
|
||||
|
||||
insta::assert_debug_snapshot!(result, @r#"
|
||||
(
|
||||
[
|
||||
"{{" (0..2),
|
||||
" " (2..3),
|
||||
"foo" (3..6),
|
||||
" " (6..7),
|
||||
"}}" (7..9),
|
||||
],
|
||||
None,
|
||||
[
|
||||
AstError {
|
||||
message: Some(
|
||||
"No ident allowed",
|
||||
),
|
||||
help: None,
|
||||
span: Some(
|
||||
SourceSpan {
|
||||
range: 0..6,
|
||||
},
|
||||
),
|
||||
is_fatal: false,
|
||||
},
|
||||
],
|
||||
)
|
||||
"#);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
source: src/ast/mod.rs
|
||||
expression: ast.to_report(input)
|
||||
---
|
||||
[1m[91merror[0m[1m: Standlone action block[0m
|
||||
[1m[94m ╭▸ [0m
|
||||
[1m[94m1[0m [1m[94m│[0m {{ value }}
|
||||
[1m[94m│[0m [1m[91m━━━━━[0m
|
||||
[1m[94m│[0m
|
||||
[1m[94m╰ [0m[1mhelp[0m: If you want to output this expression, add a '=' to the block
|
||||
[1m[91merror[0m[1m: Standlone action block[0m
|
||||
[1m[94m ╭▸ [0m
|
||||
[1m[94m2[0m [1m[94m│[0m {{ value }}
|
||||
[1m[94m│[0m [1m[91m━━━━━[0m
|
||||
[1m[94m╰ [0m[1mhelp[0m: If you want to output this expression, add a '=' to the block
|
||||
[1m[91merror[0m[1m: Standlone action block[0m
|
||||
[1m[94m ╭▸ [0m
|
||||
[1m[94m3[0m [1m[94m│[0m {{ value }}
|
||||
[1m[94m│[0m [1m[91m━━━━━[0m
|
||||
[1m[94m╰ [0m[1mhelp[0m: If you want to output this expression, add a '=' to the block
|
||||
[1m[91merror[0m[1m: Standlone action block[0m
|
||||
[1m[94m ╭▸ [0m
|
||||
[1m[94m4[0m [1m[94m│[0m {{ value }}
|
||||
[1m[94m│[0m [1m[91m━━━━━[0m
|
||||
[1m[94m╰ [0m[1mhelp[0m: If you want to output this expression, add a '=' to the block
|
||||
[1m[91merror[0m[1m: Standlone action block[0m
|
||||
[1m[94m ╭▸ [0m
|
||||
[1m[94m5[0m [1m[94m│[0m {{ value }}
|
||||
[1m[94m│[0m [1m[91m━━━━━[0m
|
||||
[1m[94m╰ [0m[1mhelp[0m: If you want to output this expression, add a '=' to the block
|
||||
|
|
@ -1,84 +1,152 @@
|
|||
---
|
||||
source: src/ast/mod.rs
|
||||
expression: ast
|
||||
expression: parsed
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
expression: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: VariableAccess(
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "foo",
|
||||
},
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "
|
||||
",
|
||||
},
|
||||
),
|
||||
},
|
||||
content: [
|
||||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
expression: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: VariableAccess(
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "bar",
|
||||
},
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "
|
||||
",
|
||||
},
|
||||
),
|
||||
},
|
||||
content: [
|
||||
StaticContent(
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hiii",
|
||||
},
|
||||
),
|
||||
],
|
||||
end_block: Block {
|
||||
prev_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "
|
||||
",
|
||||
},
|
||||
),
|
||||
expression: EndBlock,
|
||||
post_whitespace_content: Some(
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "
|
||||
",
|
||||
},
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
end_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: EndBlock,
|
||||
post_whitespace_content: None,
|
||||
},
|
||||
},
|
||||
],
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{" (0..2),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (2..3),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: ConditionalIf,
|
||||
source: "if" (3..5),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (5..6),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "foo" (6..9),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (9..10),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}" (10..12),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "\n " (12..25),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{" (25..27),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (27..28),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: ConditionalIf,
|
||||
source: "if" (28..30),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (30..31),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "bar" (31..34),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (34..35),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}" (35..37),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "\n " (37..54),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hiii" (54..58),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "\n " (58..71),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{" (71..73),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (73..74),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: End,
|
||||
source: "end" (74..77),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (77..78),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}" (78..80),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "\n " (80..89),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{" (89..91),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (91..92),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: End,
|
||||
source: "end" (92..95),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (95..96),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}" (96..98),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "\n\n " (98..108),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{" (108..110),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (110..111),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "value" (111..116),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " " (116..117),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}" (117..119),
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: "\n " (119..128),
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
|
|||
70
src/ast/snapshots/nomo__ast__tests__simple_if_ast.snap
Normal file
70
src/ast/snapshots/nomo__ast__tests__simple_if_ast.snap
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
---
|
||||
source: src/ast/mod.rs
|
||||
expression: ast
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
if_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: VariableAccess(
|
||||
"foo" (6..9),
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
"\n " (12..25),
|
||||
),
|
||||
},
|
||||
content: [
|
||||
ConditionalChain {
|
||||
chain: [
|
||||
IfConditional {
|
||||
if_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: VariableAccess(
|
||||
"bar" (31..34),
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
"\n " (37..54),
|
||||
),
|
||||
},
|
||||
content: [
|
||||
StaticContent(
|
||||
"Hiii" (54..58),
|
||||
),
|
||||
],
|
||||
end_block: Block {
|
||||
prev_whitespace_content: Some(
|
||||
"\n " (58..71),
|
||||
),
|
||||
expression: EndBlock,
|
||||
post_whitespace_content: Some(
|
||||
"\n " (80..89),
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
end_block: Block {
|
||||
prev_whitespace_content: None,
|
||||
expression: EndBlock,
|
||||
post_whitespace_content: Some(
|
||||
"\n\n " (98..108),
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: VariableAccess(
|
||||
"value" (112..117),
|
||||
),
|
||||
post_whitespace_content: Some(
|
||||
"\n " (120..129),
|
||||
),
|
||||
},
|
||||
],
|
||||
}
|
||||
45
src/ast/snapshots/nomo__ast__tests__simple_if_tokens.snap
Normal file
45
src/ast/snapshots/nomo__ast__tests__simple_if_tokens.snap
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
source: src/ast/mod.rs
|
||||
expression: parsed
|
||||
---
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
"{{" (0..2),
|
||||
" " (2..3),
|
||||
"if" (3..5),
|
||||
" " (5..6),
|
||||
"foo" (6..9),
|
||||
" " (9..10),
|
||||
"}}" (10..12),
|
||||
"\n " (12..25),
|
||||
"{{" (25..27),
|
||||
" " (27..28),
|
||||
"if" (28..30),
|
||||
" " (30..31),
|
||||
"bar" (31..34),
|
||||
" " (34..35),
|
||||
"}}" (35..37),
|
||||
"\n " (37..54),
|
||||
"Hiii" (54..58),
|
||||
"\n " (58..71),
|
||||
"{{" (71..73),
|
||||
" " (73..74),
|
||||
"end" (74..77),
|
||||
" " (77..78),
|
||||
"}}" (78..80),
|
||||
"\n " (80..89),
|
||||
"{{" (89..91),
|
||||
" " (91..92),
|
||||
"end" (92..95),
|
||||
" " (95..96),
|
||||
"}}" (96..98),
|
||||
"\n\n " (98..108),
|
||||
"{{" (108..110),
|
||||
"=" (110..111),
|
||||
" " (111..112),
|
||||
"value" (112..117),
|
||||
" " (117..118),
|
||||
"}}" (118..120),
|
||||
"\n " (120..129),
|
||||
],
|
||||
}
|
||||
161
src/emit/mod.rs
161
src/emit/mod.rs
|
|
@ -24,11 +24,24 @@ pub struct VariableSlot {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum Instruction {
|
||||
AppendContent { content: NomoInput },
|
||||
LoadFromContextToSlot { name: NomoInput, slot: VariableSlot },
|
||||
EmitFromSlot { slot: VariableSlot },
|
||||
PushScope { inherit_parent: bool },
|
||||
AppendContent {
|
||||
content: NomoInput,
|
||||
},
|
||||
LoadFromContextToSlot {
|
||||
name: NomoInput,
|
||||
slot: VariableSlot,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot,
|
||||
},
|
||||
PushScope {
|
||||
inherit_parent: bool,
|
||||
},
|
||||
Abort,
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot,
|
||||
jump: isize,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn emit_machine(input: crate::ast::TemplateAst<'_>) -> Vec<Instruction> {
|
||||
|
|
@ -55,53 +68,110 @@ fn emit_ast_expr(
|
|||
});
|
||||
}
|
||||
TemplateAstExpr::Interpolation {
|
||||
prev_whitespace_content: prev_whitespace,
|
||||
wants_output,
|
||||
prev_whitespace_content,
|
||||
expression,
|
||||
post_whitespace_content: post_whitespace,
|
||||
post_whitespace_content,
|
||||
} => {
|
||||
if let Some(ws) = prev_whitespace {
|
||||
if let Some(ws) = prev_whitespace_content {
|
||||
eval.push(Instruction::AppendContent {
|
||||
content: ws.source().clone(),
|
||||
});
|
||||
}
|
||||
|
||||
let emit_slot = machine.reserve_slot();
|
||||
emit_expr(machine, eval, emit_slot, expression);
|
||||
emit_expr_load(machine, eval, emit_slot, expression);
|
||||
eval.push(Instruction::EmitFromSlot { slot: emit_slot });
|
||||
|
||||
if let Some(ws) = post_whitespace {
|
||||
if let Some(ws) = post_whitespace_content {
|
||||
eval.push(Instruction::AppendContent {
|
||||
content: ws.source().clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
TemplateAstExpr::Invalid { .. } | TemplateAstExpr::VariableAccess { .. } => {
|
||||
eval.push(Instruction::Abort)
|
||||
TemplateAstExpr::ConditionalChain { chain } => {
|
||||
let mut chain = chain.iter();
|
||||
let Some(TemplateAstExpr::IfConditional {
|
||||
if_block: expression,
|
||||
content,
|
||||
end_block,
|
||||
}) = chain.next()
|
||||
else {
|
||||
unreachable!("First element in conditional chain should be an IfConditional");
|
||||
};
|
||||
|
||||
let TemplateAstExpr::Block {
|
||||
prev_whitespace_content,
|
||||
expression,
|
||||
post_whitespace_content,
|
||||
} = expression.as_ref()
|
||||
else {
|
||||
unreachable!("The end of an IfConditional must be a Block");
|
||||
};
|
||||
|
||||
if let Some(ws) = prev_whitespace_content {
|
||||
eval.push(Instruction::AppendContent {
|
||||
content: ws.source().clone(),
|
||||
});
|
||||
}
|
||||
|
||||
let emit_slot = machine.reserve_slot();
|
||||
emit_expr_load(machine, eval, emit_slot, expression);
|
||||
|
||||
let index = eval.len();
|
||||
eval.push(Instruction::JumpIfNotTrue {
|
||||
emit_slot,
|
||||
jump: isize::MAX,
|
||||
});
|
||||
|
||||
if let Some(ws) = post_whitespace_content {
|
||||
eval.push(Instruction::AppendContent {
|
||||
content: ws.source().clone(),
|
||||
});
|
||||
}
|
||||
|
||||
for ast in content {
|
||||
emit_ast_expr(machine, eval, ast);
|
||||
}
|
||||
|
||||
let TemplateAstExpr::Block {
|
||||
prev_whitespace_content,
|
||||
post_whitespace_content,
|
||||
..
|
||||
} = end_block.as_ref()
|
||||
else {
|
||||
unreachable!("The end of an IfConditional must be a Block");
|
||||
};
|
||||
|
||||
if let Some(ws) = prev_whitespace_content {
|
||||
eval.push(Instruction::AppendContent {
|
||||
content: ws.source().clone(),
|
||||
});
|
||||
}
|
||||
|
||||
let jump = eval.len() - index - 1;
|
||||
eval[index] = Instruction::JumpIfNotTrue {
|
||||
emit_slot,
|
||||
jump: jump as isize,
|
||||
};
|
||||
|
||||
if let Some(ws) = post_whitespace_content {
|
||||
eval.push(Instruction::AppendContent {
|
||||
content: ws.source().clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
TemplateAstExpr::ConditionalChain { chain } => todo!(),
|
||||
TemplateAstExpr::ElseConditional { expression } => todo!(),
|
||||
TemplateAstExpr::Action {
|
||||
prev_whitespace_content,
|
||||
expression,
|
||||
post_whitespace_content,
|
||||
} => todo!(),
|
||||
TemplateAstExpr::EndBlock => todo!(),
|
||||
TemplateAstExpr::Block {
|
||||
prev_whitespace_content,
|
||||
expression,
|
||||
post_whitespace_content,
|
||||
} => todo!(),
|
||||
TemplateAstExpr::IfConditional {
|
||||
expression,
|
||||
content,
|
||||
end_block,
|
||||
} => todo!(),
|
||||
|
||||
TemplateAstExpr::Block { .. }
|
||||
| TemplateAstExpr::EndBlock
|
||||
| TemplateAstExpr::IfConditional { .. }
|
||||
| TemplateAstExpr::ElseConditional { .. }
|
||||
| TemplateAstExpr::Invalid { .. }
|
||||
| TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort),
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_expr(
|
||||
machine: &mut EmitMachine,
|
||||
fn emit_expr_load(
|
||||
_machine: &mut EmitMachine,
|
||||
eval: &mut Vec<Instruction>,
|
||||
emit_slot: VariableSlot,
|
||||
expression: &TemplateAstExpr<'_>,
|
||||
|
|
@ -117,24 +187,11 @@ fn emit_expr(
|
|||
TemplateAstExpr::StaticContent { .. } | TemplateAstExpr::Interpolation { .. } => {
|
||||
unreachable!("Invalid AST here")
|
||||
}
|
||||
TemplateAstExpr::ConditionalChain { chain } => todo!(),
|
||||
TemplateAstExpr::ElseConditional { expression } => todo!(),
|
||||
TemplateAstExpr::Action {
|
||||
prev_whitespace_content,
|
||||
expression,
|
||||
post_whitespace_content,
|
||||
} => todo!(),
|
||||
TemplateAstExpr::ConditionalChain { .. } => todo!(),
|
||||
TemplateAstExpr::ElseConditional { .. } => todo!(),
|
||||
TemplateAstExpr::EndBlock => todo!(),
|
||||
TemplateAstExpr::Block {
|
||||
prev_whitespace_content,
|
||||
expression,
|
||||
post_whitespace_content,
|
||||
} => todo!(),
|
||||
TemplateAstExpr::IfConditional {
|
||||
expression,
|
||||
content,
|
||||
end_block,
|
||||
} => todo!(),
|
||||
TemplateAstExpr::Block { .. } => todo!(),
|
||||
TemplateAstExpr::IfConditional { .. } => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -155,13 +212,13 @@ mod tests {
|
|||
insta::assert_debug_snapshot!(emit, @r#"
|
||||
[
|
||||
AppendContent {
|
||||
content: "Hello",
|
||||
content: "Hello" (0..5),
|
||||
},
|
||||
AppendContent {
|
||||
content: " ",
|
||||
content: " " (5..6),
|
||||
},
|
||||
LoadFromContextToSlot {
|
||||
name: "world",
|
||||
name: "world" (10..15),
|
||||
slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ pub enum EvaluationError {
|
|||
UnknownVariable(NomoInput),
|
||||
/// An explicit abort was requested
|
||||
ExplicitAbort,
|
||||
/** The instruction pointer overflowed
|
||||
**
|
||||
** This is an internal error and is a bug that should be reported
|
||||
*/
|
||||
InstructionPointerOverflow,
|
||||
}
|
||||
|
||||
pub fn execute(
|
||||
|
|
@ -23,7 +28,14 @@ pub fn execute(
|
|||
|
||||
let mut scopes: HashMap<crate::emit::VariableSlot, serde_json::Value> = HashMap::new();
|
||||
|
||||
for instr in instructions {
|
||||
let mut ip = 0;
|
||||
loop {
|
||||
if ip >= instructions.len() {
|
||||
break;
|
||||
}
|
||||
|
||||
let instr = instructions.get(ip).unwrap();
|
||||
|
||||
match instr {
|
||||
Instruction::AppendContent { content } => output.push_str(content),
|
||||
Instruction::LoadFromContextToSlot { name, slot } => {
|
||||
|
|
@ -40,7 +52,24 @@ pub fn execute(
|
|||
}
|
||||
Instruction::PushScope { inherit_parent: _ } => todo!(),
|
||||
Instruction::Abort => return Err(EvaluationError::ExplicitAbort),
|
||||
Instruction::JumpIfNotTrue { emit_slot, jump } => {
|
||||
let dont_jump = scopes.get(emit_slot).unwrap().as_bool().unwrap();
|
||||
if dont_jump {
|
||||
// We are done
|
||||
} else {
|
||||
let (new_ip, overflow) = ip.overflowing_add_signed(*jump);
|
||||
|
||||
if overflow {
|
||||
return Err(EvaluationError::InstructionPointerOverflow);
|
||||
} else {
|
||||
ip = new_ip;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ip += 1;
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
|
|
|
|||
29
src/input.rs
29
src/input.rs
|
|
@ -22,11 +22,20 @@ impl NomoInput {
|
|||
pub fn into_parts(self) -> (Arc<str>, Range<usize>) {
|
||||
(self.backing, self.range)
|
||||
}
|
||||
|
||||
pub fn get_range(&self) -> Range<usize> {
|
||||
self.range.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NomoInputCheckpoint {
|
||||
range: Range<usize>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for NomoInput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "\"{}\"", self.as_str())
|
||||
write!(f, "{:?} ({:?})", self.as_str(), self.range)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,6 +83,18 @@ impl NomoInput {
|
|||
}
|
||||
}
|
||||
|
||||
impl Offset for NomoInputCheckpoint {
|
||||
fn offset_from(&self, start: &Self) -> usize {
|
||||
self.range.start - start.range.start
|
||||
}
|
||||
}
|
||||
|
||||
impl Offset<NomoInputCheckpoint> for NomoInput {
|
||||
fn offset_from(&self, start: &NomoInputCheckpoint) -> usize {
|
||||
self.range.start - start.range.start
|
||||
}
|
||||
}
|
||||
|
||||
impl Offset for NomoInput {
|
||||
fn offset_from(&self, start: &Self) -> usize {
|
||||
self.as_str().offset_from(&start.as_str())
|
||||
|
|
@ -116,7 +137,7 @@ impl Stream for NomoInput {
|
|||
|
||||
type IterOffsets = NomoInputIter;
|
||||
|
||||
type Checkpoint = NomoInput;
|
||||
type Checkpoint = NomoInputCheckpoint;
|
||||
|
||||
fn iter_offsets(&self) -> Self::IterOffsets {
|
||||
NomoInputIter {
|
||||
|
|
@ -167,7 +188,9 @@ impl Stream for NomoInput {
|
|||
}
|
||||
|
||||
fn checkpoint(&self) -> Self::Checkpoint {
|
||||
self.clone()
|
||||
NomoInputCheckpoint {
|
||||
range: self.get_range(),
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self, checkpoint: &Self::Checkpoint) {
|
||||
|
|
|
|||
10
src/lib.rs
10
src/lib.rs
|
|
@ -65,13 +65,8 @@ impl Nomo {
|
|||
|
||||
let instructions = emit::emit_machine(ast);
|
||||
|
||||
self.templates.insert(
|
||||
name.into(),
|
||||
Template {
|
||||
source,
|
||||
instructions,
|
||||
},
|
||||
);
|
||||
self.templates
|
||||
.insert(name.into(), Template { instructions });
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -89,7 +84,6 @@ impl Nomo {
|
|||
}
|
||||
|
||||
struct Template {
|
||||
source: NomoInput,
|
||||
instructions: Vec<Instruction>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -245,12 +245,28 @@ impl<const LEN: usize> winnow::stream::ContainsToken<&'_ TemplateToken> for [Tok
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct TemplateToken {
|
||||
kind: TokenKind,
|
||||
source: NomoInput,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for TemplateToken {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self.source())
|
||||
}
|
||||
}
|
||||
|
||||
impl Location for TemplateToken {
|
||||
fn previous_token_end(&self) -> usize {
|
||||
NomoInput::get_range(&self.source).start
|
||||
}
|
||||
|
||||
fn current_token_start(&self) -> usize {
|
||||
NomoInput::get_range(&self.source).start
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_token_kind_builders {
|
||||
($($name:ident => $kind:expr),+ $(,)?) => {
|
||||
$(
|
||||
|
|
@ -459,10 +475,7 @@ mod tests {
|
|||
Ok(
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hello There",
|
||||
},
|
||||
"Hello There" (0..11),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
|
@ -478,34 +491,13 @@ mod tests {
|
|||
Ok(
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hello",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Ident,
|
||||
source: "there",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}",
|
||||
},
|
||||
"Hello" (0..5),
|
||||
" " (5..6),
|
||||
"{{" (6..8),
|
||||
" " (8..9),
|
||||
"there" (9..14),
|
||||
" " (14..15),
|
||||
"}}" (15..17),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
|
@ -555,70 +547,21 @@ mod tests {
|
|||
Ok(
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: ConditionalIf,
|
||||
source: "if",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Literal(
|
||||
Bool(
|
||||
true,
|
||||
),
|
||||
),
|
||||
source: "true",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Content,
|
||||
source: "Hello!",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: LeftDelim,
|
||||
source: "{{",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: End,
|
||||
source: "end",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: Whitespace,
|
||||
source: " ",
|
||||
},
|
||||
TemplateToken {
|
||||
kind: RightDelim,
|
||||
source: "}}",
|
||||
},
|
||||
"{{" (0..2),
|
||||
" " (2..3),
|
||||
"if" (3..5),
|
||||
" " (5..6),
|
||||
"true" (6..10),
|
||||
" " (10..11),
|
||||
"}}" (11..13),
|
||||
" " (13..14),
|
||||
"Hello!" (14..20),
|
||||
" " (20..21),
|
||||
"{{" (21..23),
|
||||
" " (23..24),
|
||||
"end" (24..27),
|
||||
" " (27..28),
|
||||
"}}" (28..30),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue