diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 98e350e..eb06315 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -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 { diff --git a/src/compiler/snapshots/nomo__compiler__tests__check_if_else_if.snap b/src/compiler/snapshots/nomo__compiler__tests__check_if_else_if.snap index 5cf26c0..de57cc6 100644 --- a/src/compiler/snapshots/nomo__compiler__tests__check_if_else_if.snap +++ b/src/compiler/snapshots/nomo__compiler__tests__check_if_else_if.snap @@ -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 { diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 62a013f..96ae66e 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -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 ", - ) - "#); - } } diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 6fb66a2..a35e096 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -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), - ], - }, - ) - "#); - } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index d45eea7..ed4456f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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>, }, ForElse, - ConditionalAccess(TemplateToken), VariableAccess(TemplateToken), IfConditional { expression: Box>, @@ -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); - } } diff --git a/src/parser/snapshots/nomo__parser__tests__check_conditional_access.snap b/src/parser/snapshots/nomo__parser__tests__check_conditional_access.snap deleted file mode 100644 index f575f85..0000000 --- a/src/parser/snapshots/nomo__parser__tests__check_conditional_access.snap +++ /dev/null @@ -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, - }, - ], -} diff --git a/src/value.rs b/src/value.rs index c587f82..2d70084 100644 --- a/src/value.rs +++ b/src/value.rs @@ -31,7 +31,6 @@ pub enum NomoValue { Iterator { value: Box>, }, - 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(), } } } diff --git a/tests/cases/condition.3-instructions.snap b/tests/cases/condition.3-instructions.snap index 50c3a5f..a317547 100644 --- a/tests/cases/condition.3-instructions.snap +++ b/tests/cases/condition.3-instructions.snap @@ -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 { diff --git a/tests/cases/identifiers.3-instructions.snap b/tests/cases/identifiers.3-instructions.snap index 6d13773..f3d8ddc 100644 --- a/tests/cases/identifiers.3-instructions.snap +++ b/tests/cases/identifiers.3-instructions.snap @@ -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 { diff --git a/tests/cases/if_else_if.3-instructions.snap b/tests/cases/if_else_if.3-instructions.snap index 338a105..5504ab9 100644 --- a/tests/cases/if_else_if.3-instructions.snap +++ b/tests/cases/if_else_if.3-instructions.snap @@ -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 { diff --git a/tests/cases/interpolation.3-instructions.snap b/tests/cases/interpolation.3-instructions.snap index 4465366..c47d18a 100644 --- a/tests/cases/interpolation.3-instructions.snap +++ b/tests/cases/interpolation.3-instructions.snap @@ -20,7 +20,6 @@ VMInstructions { slot: VariableSlot { index: 0, }, - fail_on_not_found: true, }, EmitFromSlot { slot: VariableSlot { diff --git a/tests/cases/multiple.3-instructions.snap b/tests/cases/multiple.3-instructions.snap index 2b475e7..ef538c1 100644 --- a/tests/cases/multiple.3-instructions.snap +++ b/tests/cases/multiple.3-instructions.snap @@ -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 { diff --git a/tests/cases/simple_for.3-instructions.snap b/tests/cases/simple_for.3-instructions.snap index 415d516..f082c47 100644 --- a/tests/cases/simple_for.3-instructions.snap +++ b/tests/cases/simple_for.3-instructions.snap @@ -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 { diff --git a/tests/cases/trim_whitespace.3-instructions.snap b/tests/cases/trim_whitespace.3-instructions.snap index f484bbf..31112d0 100644 --- a/tests/cases/trim_whitespace.3-instructions.snap +++ b/tests/cases/trim_whitespace.3-instructions.snap @@ -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 {