Compare commits

..

No commits in common. "9940881e46881209835e769d78d58f9e570cf3dc" and "5354d4ac7bf242360bdc80ad4d631e1930e9f91e" have entirely different histories.

14 changed files with 14 additions and 155 deletions

View file

@ -1,9 +1,9 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::parser::TemplateAstExpr;
use crate::input::NomoInput; use crate::input::NomoInput;
use crate::lexer::TemplateToken; use crate::lexer::TemplateToken;
use crate::lexer::TokenOperator; use crate::lexer::TokenOperator;
use crate::parser::TemplateAstExpr;
use crate::value::NomoValue; use crate::value::NomoValue;
pub struct EmitMachine { pub struct EmitMachine {
@ -57,7 +57,6 @@ pub enum Instruction {
LoadFromContextToSlot { LoadFromContextToSlot {
name: NomoInput, name: NomoInput,
slot: VariableSlot, slot: VariableSlot,
fail_on_not_found: bool,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot, slot: VariableSlot,
@ -425,7 +424,6 @@ fn emit_ast_expr(
| TemplateAstExpr::Literal { .. } | TemplateAstExpr::Literal { .. }
| TemplateAstExpr::FunctionCall { .. } | TemplateAstExpr::FunctionCall { .. }
| TemplateAstExpr::Operation { .. } | TemplateAstExpr::Operation { .. }
| TemplateAstExpr::ConditionalAccess { .. }
| TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort), | TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort),
} }
} }
@ -441,14 +439,6 @@ fn emit_expr_load(
eval.push(Instruction::LoadFromContextToSlot { eval.push(Instruction::LoadFromContextToSlot {
name: template_token.source().clone(), name: template_token.source().clone(),
slot: emit_slot, 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 } => { TemplateAstExpr::Literal { source, value } => {
@ -531,7 +521,6 @@ mod tests {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -20,7 +20,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -52,7 +51,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {

View file

@ -93,34 +93,15 @@ pub fn execute(
match instr { match instr {
Instruction::NoOp => (), Instruction::NoOp => (),
Instruction::AppendContent { content } => output.push_str(content), Instruction::AppendContent { content } => output.push_str(content),
Instruction::LoadFromContextToSlot { Instruction::LoadFromContextToSlot { name, slot } => {
name, let value = scopes
slot, .get_scoped(name)
fail_on_not_found, .ok_or(EvaluationError::UnknownVariable(name.clone()))?;
} => {
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
}
};
scopes.insert_into_slot(*slot, value.clone()); scopes.insert_into_slot(*slot, value.clone());
} }
Instruction::EmitFromSlot { slot } => { Instruction::EmitFromSlot { slot } => {
let value = scopes.get(slot); let value = scopes.get(slot).try_to_string().unwrap();
let value = if let Some(value) = value.try_to_string() {
value
} else if matches!(value, NomoValue::Undefined) {
String::new()
} else {
panic!("Unknown variable");
};
output.push_str(&value); 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 ",
)
"#);
}
} }

View file

@ -238,7 +238,6 @@ pub enum TokenOperator {
GreaterOrEqual, GreaterOrEqual,
Lesser, Lesser,
LesserOrEqual, LesserOrEqual,
QuestionMark,
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -568,7 +567,6 @@ fn parse_operator<'input>(input: &mut Input<'input>) -> PResult<'input, Template
"=".value(TokenOperator::Equal), "=".value(TokenOperator::Equal),
cut_err(fail), cut_err(fail),
)), )),
'?' => empty.value(TokenOperator::QuestionMark),
_ => fail, _ => 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, ()> { fn ident_terminator<'input>(input: &mut Input<'input>) -> PResult<'input, ()> {
alt(( alt((
eof.void(), eof.void(),
parse_operator.void(),
one_of(('{', '}')).void(), one_of(('{', '}')).void(),
one_of(('(', ',', ')')).void(), one_of(('(', ',', ')')).void(),
one_of((' ', '\t', '\r', '\n')).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),
],
},
)
"#);
}
} }

View file

@ -2,13 +2,11 @@ use thiserror::Error;
use winnow::Parser; use winnow::Parser;
use winnow::RecoverableParser; use winnow::RecoverableParser;
use winnow::combinator::Infix::Left; use winnow::combinator::Infix::Left;
use winnow::combinator::Postfix;
use winnow::combinator::alt; use winnow::combinator::alt;
use winnow::combinator::cut_err; use winnow::combinator::cut_err;
use winnow::combinator::delimited; use winnow::combinator::delimited;
use winnow::combinator::dispatch; use winnow::combinator::dispatch;
use winnow::combinator::expression; use winnow::combinator::expression;
use winnow::combinator::fail;
use winnow::combinator::not; use winnow::combinator::not;
use winnow::combinator::opt; use winnow::combinator::opt;
use winnow::combinator::peek; use winnow::combinator::peek;
@ -250,7 +248,6 @@ pub enum TemplateAstExpr<'input> {
value_expression: Box<TemplateAstExpr<'input>>, value_expression: Box<TemplateAstExpr<'input>>,
}, },
ForElse, ForElse,
ConditionalAccess(TemplateToken),
VariableAccess(TemplateToken), VariableAccess(TemplateToken),
IfConditional { IfConditional {
expression: Box<TemplateAstExpr<'input>>, expression: Box<TemplateAstExpr<'input>>,
@ -688,9 +685,8 @@ fn parse_expression<'input>(
op: TokenOperator::$val, op: TokenOperator::$val,
lhs: Box::new(lhs), lhs: Box::new(lhs),
rhs: Box::new(rhs) rhs: Box::new(rhs)
})), }))
)* ),*
_ => fail
} }
}; };
} }
@ -711,16 +707,6 @@ fn parse_expression<'input>(
Left Lesser => 15, Left Lesser => 15,
Left LesserOrEqual => 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) .parse_next(input)
@ -751,7 +737,6 @@ mod tests {
use winnow::combinator::fail; use winnow::combinator::fail;
use winnow::stream::TokenSlice; use winnow::stream::TokenSlice;
use crate::lexer::TokenKind;
use crate::parser::AstError; use crate::parser::AstError;
use crate::parser::AstFailure; use crate::parser::AstFailure;
use crate::parser::TemplateAst; use crate::parser::TemplateAst;
@ -759,6 +744,7 @@ mod tests {
use crate::parser::parse; use crate::parser::parse;
use crate::parser::parse_block; use crate::parser::parse_block;
use crate::parser::parse_end; use crate::parser::parse_end;
use crate::lexer::TokenKind;
fn panic_pretty<'a>( fn panic_pretty<'a>(
input: &'_ str, input: &'_ str,
@ -1060,15 +1046,4 @@ mod tests {
insta::assert_debug_snapshot!(ast); 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);
}
} }

View file

@ -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,
},
],
}

View file

@ -31,7 +31,6 @@ pub enum NomoValue {
Iterator { Iterator {
value: Box<dyn CloneIterator<Item = NomoValue>>, value: Box<dyn CloneIterator<Item = NomoValue>>,
}, },
Undefined,
} }
impl NomoValue { impl NomoValue {
@ -242,7 +241,6 @@ impl NomoValue {
NomoValue::SignedInteger { value } => Some(value.to_string()), NomoValue::SignedInteger { value } => Some(value.to_string()),
NomoValue::Float { value } => Some(value.to_string()), NomoValue::Float { value } => Some(value.to_string()),
NomoValue::Iterator { .. } => None, NomoValue::Iterator { .. } => None,
NomoValue::Undefined => None,
} }
} }
} }
@ -283,7 +281,6 @@ impl std::fmt::Debug for NomoValue {
.debug_struct("Iterator") .debug_struct("Iterator")
.field("value", &"Iterator") .field("value", &"Iterator")
.finish(), .finish(),
Self::Undefined => f.debug_tuple("Undefined").finish(),
} }
} }
} }

View file

@ -22,7 +22,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -57,7 +56,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -4,12 +4,12 @@ expression: emit
info: info:
input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}" input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}"
context: context:
a_name: Foo
_name: Foo
_name1: Foo
_namE: Foo
name1: Foo name1: Foo
_name: Foo
_namE: Foo
a_name: Foo
name: Foo name: Foo
_name1: Foo
--- ---
VMInstructions { VMInstructions {
labels: {}, labels: {},
@ -19,7 +19,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -34,7 +33,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -49,7 +47,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 2, index: 2,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -64,7 +61,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -79,7 +75,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 4, index: 4,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -94,7 +89,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 5, index: 5,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -26,7 +26,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -58,7 +57,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {

View file

@ -20,7 +20,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -21,7 +21,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -36,7 +35,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -39,7 +39,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 4, index: 4,
}, },
fail_on_not_found: true,
}, },
CreateIteratorFromSlotToSlot { CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot { iterator_slot: VariableSlot {
@ -79,7 +78,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 6, index: 6,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -106,7 +104,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 11, index: 11,
}, },
fail_on_not_found: true,
}, },
CreateIteratorFromSlotToSlot { CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot { iterator_slot: VariableSlot {
@ -146,7 +143,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 13, index: 13,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -4,8 +4,8 @@ expression: emit
info: info:
input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}" input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}"
context: context:
test: true
stuff: Hemera stuff: Hemera
test: true
--- ---
VMInstructions { VMInstructions {
labels: { labels: {
@ -22,7 +22,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -43,7 +42,6 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {