Compare commits
No commits in common. "9940881e46881209835e769d78d58f9e570cf3dc" and "5354d4ac7bf242360bdc80ad4d631e1930e9f91e" have entirely different histories.
9940881e46
...
5354d4ac7b
14 changed files with 14 additions and 155 deletions
|
|
@ -1,9 +1,9 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::parser::TemplateAstExpr;
|
||||
use crate::input::NomoInput;
|
||||
use crate::lexer::TemplateToken;
|
||||
use crate::lexer::TokenOperator;
|
||||
use crate::parser::TemplateAstExpr;
|
||||
use crate::value::NomoValue;
|
||||
|
||||
pub struct EmitMachine {
|
||||
|
|
@ -57,7 +57,6 @@ pub enum Instruction {
|
|||
LoadFromContextToSlot {
|
||||
name: NomoInput,
|
||||
slot: VariableSlot,
|
||||
fail_on_not_found: bool,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot,
|
||||
|
|
@ -425,7 +424,6 @@ fn emit_ast_expr(
|
|||
| TemplateAstExpr::Literal { .. }
|
||||
| TemplateAstExpr::FunctionCall { .. }
|
||||
| TemplateAstExpr::Operation { .. }
|
||||
| TemplateAstExpr::ConditionalAccess { .. }
|
||||
| TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort),
|
||||
}
|
||||
}
|
||||
|
|
@ -441,14 +439,6 @@ fn emit_expr_load(
|
|||
eval.push(Instruction::LoadFromContextToSlot {
|
||||
name: template_token.source().clone(),
|
||||
slot: emit_slot,
|
||||
fail_on_not_found: true,
|
||||
});
|
||||
}
|
||||
TemplateAstExpr::ConditionalAccess(template_token) => {
|
||||
eval.push(Instruction::LoadFromContextToSlot {
|
||||
name: template_token.source().clone(),
|
||||
slot: emit_slot,
|
||||
fail_on_not_found: false,
|
||||
});
|
||||
}
|
||||
TemplateAstExpr::Literal { source, value } => {
|
||||
|
|
@ -531,7 +521,6 @@ mod tests {
|
|||
slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot {
|
||||
|
|
@ -52,7 +51,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -93,34 +93,15 @@ pub fn execute(
|
|||
match instr {
|
||||
Instruction::NoOp => (),
|
||||
Instruction::AppendContent { content } => output.push_str(content),
|
||||
Instruction::LoadFromContextToSlot {
|
||||
name,
|
||||
slot,
|
||||
fail_on_not_found,
|
||||
} => {
|
||||
let value = scopes.get_scoped(name);
|
||||
|
||||
let value = if let Some(val) = value {
|
||||
val
|
||||
} else {
|
||||
if *fail_on_not_found {
|
||||
return Err(EvaluationError::UnknownVariable(name.clone()));
|
||||
} else {
|
||||
&NomoValue::Undefined
|
||||
}
|
||||
};
|
||||
Instruction::LoadFromContextToSlot { name, slot } => {
|
||||
let value = scopes
|
||||
.get_scoped(name)
|
||||
.ok_or(EvaluationError::UnknownVariable(name.clone()))?;
|
||||
|
||||
scopes.insert_into_slot(*slot, value.clone());
|
||||
}
|
||||
Instruction::EmitFromSlot { slot } => {
|
||||
let value = scopes.get(slot);
|
||||
let value = if let Some(value) = value.try_to_string() {
|
||||
value
|
||||
} else if matches!(value, NomoValue::Undefined) {
|
||||
String::new()
|
||||
} else {
|
||||
panic!("Unknown variable");
|
||||
};
|
||||
let value = scopes.get(slot).try_to_string().unwrap();
|
||||
|
||||
output.push_str(&value);
|
||||
}
|
||||
|
|
@ -305,26 +286,4 @@ mod tests {
|
|||
)
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_conditional_access() {
|
||||
let input = "Hello {{= unknown? }}";
|
||||
|
||||
let parsed = crate::lexer::parse(input.into()).unwrap();
|
||||
|
||||
let ast = crate::parser::parse(parsed.tokens()).unwrap();
|
||||
|
||||
let emit = crate::compiler::emit_machine(ast);
|
||||
|
||||
let context = Context::new();
|
||||
let function_map = FunctionMap::default();
|
||||
|
||||
let output = execute(&function_map, &emit, &context);
|
||||
|
||||
insta::assert_debug_snapshot!(output, @r#"
|
||||
Ok(
|
||||
"Hello ",
|
||||
)
|
||||
"#);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -238,7 +238,6 @@ pub enum TokenOperator {
|
|||
GreaterOrEqual,
|
||||
Lesser,
|
||||
LesserOrEqual,
|
||||
QuestionMark,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
|
|
@ -568,7 +567,6 @@ fn parse_operator<'input>(input: &mut Input<'input>) -> PResult<'input, Template
|
|||
"=".value(TokenOperator::Equal),
|
||||
cut_err(fail),
|
||||
)),
|
||||
'?' => empty.value(TokenOperator::QuestionMark),
|
||||
_ => fail,
|
||||
},
|
||||
)
|
||||
|
|
@ -606,7 +604,6 @@ fn ident_terminator_check<'input>(input: &mut Input<'input>) -> PResult<'input,
|
|||
fn ident_terminator<'input>(input: &mut Input<'input>) -> PResult<'input, ()> {
|
||||
alt((
|
||||
eof.void(),
|
||||
parse_operator.void(),
|
||||
one_of(('{', '}')).void(),
|
||||
one_of(('(', ',', ')')).void(),
|
||||
one_of((' ', '\t', '\r', '\n')).void(),
|
||||
|
|
@ -908,26 +905,4 @@ mod tests {
|
|||
)
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_question_mark() {
|
||||
let input = "{{= foo? }}";
|
||||
let output = parse(input.into());
|
||||
|
||||
insta::assert_debug_snapshot!(output, @r#"
|
||||
Ok(
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
[LeftDelim]"{{" (0..2),
|
||||
[WantsOutput]"=" (2..3),
|
||||
[Whitespace]" " (3..4),
|
||||
[Ident]"foo" (4..7),
|
||||
[Operator(QuestionMark)]"?" (7..8),
|
||||
[Whitespace]" " (8..9),
|
||||
[RightDelim]"}}" (9..11),
|
||||
],
|
||||
},
|
||||
)
|
||||
"#);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,11 @@ use thiserror::Error;
|
|||
use winnow::Parser;
|
||||
use winnow::RecoverableParser;
|
||||
use winnow::combinator::Infix::Left;
|
||||
use winnow::combinator::Postfix;
|
||||
use winnow::combinator::alt;
|
||||
use winnow::combinator::cut_err;
|
||||
use winnow::combinator::delimited;
|
||||
use winnow::combinator::dispatch;
|
||||
use winnow::combinator::expression;
|
||||
use winnow::combinator::fail;
|
||||
use winnow::combinator::not;
|
||||
use winnow::combinator::opt;
|
||||
use winnow::combinator::peek;
|
||||
|
|
@ -250,7 +248,6 @@ pub enum TemplateAstExpr<'input> {
|
|||
value_expression: Box<TemplateAstExpr<'input>>,
|
||||
},
|
||||
ForElse,
|
||||
ConditionalAccess(TemplateToken),
|
||||
VariableAccess(TemplateToken),
|
||||
IfConditional {
|
||||
expression: Box<TemplateAstExpr<'input>>,
|
||||
|
|
@ -688,9 +685,8 @@ fn parse_expression<'input>(
|
|||
op: TokenOperator::$val,
|
||||
lhs: Box::new(lhs),
|
||||
rhs: Box::new(rhs)
|
||||
})),
|
||||
)*
|
||||
_ => fail
|
||||
}))
|
||||
),*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -711,16 +707,6 @@ fn parse_expression<'input>(
|
|||
Left Lesser => 15,
|
||||
Left LesserOrEqual => 15,
|
||||
]
|
||||
}).postfix(dispatch! { surrounded(ws, parse_operator);
|
||||
TokenOperator::QuestionMark => Postfix(22, |input, rhs| {
|
||||
match rhs {
|
||||
TemplateAstExpr::VariableAccess(access) => Ok(TemplateAstExpr::ConditionalAccess(access)),
|
||||
_ => Err(AstError::from_input(input)),
|
||||
}
|
||||
|
||||
|
||||
}),
|
||||
_ => fail
|
||||
}),
|
||||
)
|
||||
.parse_next(input)
|
||||
|
|
@ -751,7 +737,6 @@ mod tests {
|
|||
use winnow::combinator::fail;
|
||||
use winnow::stream::TokenSlice;
|
||||
|
||||
use crate::lexer::TokenKind;
|
||||
use crate::parser::AstError;
|
||||
use crate::parser::AstFailure;
|
||||
use crate::parser::TemplateAst;
|
||||
|
|
@ -759,6 +744,7 @@ mod tests {
|
|||
use crate::parser::parse;
|
||||
use crate::parser::parse_block;
|
||||
use crate::parser::parse_end;
|
||||
use crate::lexer::TokenKind;
|
||||
|
||||
fn panic_pretty<'a>(
|
||||
input: &'_ str,
|
||||
|
|
@ -1060,15 +1046,4 @@ mod tests {
|
|||
|
||||
insta::assert_debug_snapshot!(ast);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_conditional_access() {
|
||||
let input = "{{= foo? }}";
|
||||
|
||||
let parsed = crate::lexer::parse(input.into()).unwrap();
|
||||
|
||||
let ast = panic_pretty(input, parse(parsed.tokens()));
|
||||
|
||||
insta::assert_debug_snapshot!(ast);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
---
|
||||
source: src/parser/mod.rs
|
||||
expression: ast
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: ConditionalAccess(
|
||||
[Ident]"foo" (4..7),
|
||||
),
|
||||
post_whitespace_content: None,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
@ -31,7 +31,6 @@ pub enum NomoValue {
|
|||
Iterator {
|
||||
value: Box<dyn CloneIterator<Item = NomoValue>>,
|
||||
},
|
||||
Undefined,
|
||||
}
|
||||
|
||||
impl NomoValue {
|
||||
|
|
@ -242,7 +241,6 @@ impl NomoValue {
|
|||
NomoValue::SignedInteger { value } => Some(value.to_string()),
|
||||
NomoValue::Float { value } => Some(value.to_string()),
|
||||
NomoValue::Iterator { .. } => None,
|
||||
NomoValue::Undefined => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -283,7 +281,6 @@ impl std::fmt::Debug for NomoValue {
|
|||
.debug_struct("Iterator")
|
||||
.field("value", &"Iterator")
|
||||
.finish(),
|
||||
Self::Undefined => f.debug_tuple("Undefined").finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot {
|
||||
|
|
@ -57,7 +56,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ expression: emit
|
|||
info:
|
||||
input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}"
|
||||
context:
|
||||
a_name: Foo
|
||||
_name: Foo
|
||||
_name1: Foo
|
||||
_namE: Foo
|
||||
name1: Foo
|
||||
_name: Foo
|
||||
_namE: Foo
|
||||
a_name: Foo
|
||||
name: Foo
|
||||
_name1: Foo
|
||||
---
|
||||
VMInstructions {
|
||||
labels: {},
|
||||
|
|
@ -19,7 +19,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -34,7 +33,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -49,7 +47,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 2,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -64,7 +61,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -79,7 +75,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 4,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -94,7 +89,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 5,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot {
|
||||
|
|
@ -58,7 +57,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -36,7 +35,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 4,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
CreateIteratorFromSlotToSlot {
|
||||
iterator_slot: VariableSlot {
|
||||
|
|
@ -79,7 +78,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 6,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
@ -106,7 +104,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 11,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
CreateIteratorFromSlotToSlot {
|
||||
iterator_slot: VariableSlot {
|
||||
|
|
@ -146,7 +143,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 13,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ expression: emit
|
|||
info:
|
||||
input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}"
|
||||
context:
|
||||
test: true
|
||||
stuff: Hemera
|
||||
test: true
|
||||
---
|
||||
VMInstructions {
|
||||
labels: {
|
||||
|
|
@ -22,7 +22,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
JumpIfNotTrue {
|
||||
emit_slot: VariableSlot {
|
||||
|
|
@ -43,7 +42,6 @@ VMInstructions {
|
|||
slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
fail_on_not_found: true,
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue