Compare commits

...

4 commits

Author SHA1 Message Date
9940881e46 Add conditional value emitting
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-15 12:46:00 +01:00
662e574588 Add undefined value
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-15 12:35:45 +01:00
9b87e7089f Parse conditional access
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-15 12:34:35 +01:00
beac224f5b Add lexing of '?'
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-15 12:11:30 +01:00
14 changed files with 155 additions and 14 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,6 +57,7 @@ pub enum Instruction {
LoadFromContextToSlot { LoadFromContextToSlot {
name: NomoInput, name: NomoInput,
slot: VariableSlot, slot: VariableSlot,
fail_on_not_found: bool,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot, slot: VariableSlot,
@ -424,6 +425,7 @@ 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),
} }
} }
@ -439,6 +441,14 @@ 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 } => {
@ -521,6 +531,7 @@ mod tests {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -20,6 +20,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -51,6 +52,7 @@ 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,15 +93,34 @@ 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 { name, slot } => { Instruction::LoadFromContextToSlot {
let value = scopes name,
.get_scoped(name) slot,
.ok_or(EvaluationError::UnknownVariable(name.clone()))?; 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
}
};
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).try_to_string().unwrap(); 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");
};
output.push_str(&value); output.push_str(&value);
} }
@ -286,4 +305,26 @@ 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,6 +238,7 @@ pub enum TokenOperator {
GreaterOrEqual, GreaterOrEqual,
Lesser, Lesser,
LesserOrEqual, LesserOrEqual,
QuestionMark,
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -567,6 +568,7 @@ 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,
}, },
) )
@ -604,6 +606,7 @@ 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(),
@ -905,4 +908,26 @@ 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,11 +2,13 @@ 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;
@ -248,6 +250,7 @@ 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>>,
@ -685,8 +688,9 @@ 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
} }
}; };
} }
@ -707,6 +711,16 @@ 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)
@ -737,6 +751,7 @@ 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;
@ -744,7 +759,6 @@ 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,
@ -1046,4 +1060,15 @@ 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

@ -0,0 +1,15 @@
---
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,6 +31,7 @@ pub enum NomoValue {
Iterator { Iterator {
value: Box<dyn CloneIterator<Item = NomoValue>>, value: Box<dyn CloneIterator<Item = NomoValue>>,
}, },
Undefined,
} }
impl NomoValue { impl NomoValue {
@ -241,6 +242,7 @@ 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,
} }
} }
} }
@ -281,6 +283,7 @@ 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,6 +22,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -56,6 +57,7 @@ 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:
name1: Foo
_name: Foo
_namE: Foo
a_name: Foo a_name: Foo
name: Foo _name: Foo
_name1: Foo _name1: Foo
_namE: Foo
name1: Foo
name: Foo
--- ---
VMInstructions { VMInstructions {
labels: {}, labels: {},
@ -19,6 +19,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -33,6 +34,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -47,6 +49,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 2, index: 2,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -61,6 +64,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -75,6 +79,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 4, index: 4,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -89,6 +94,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 5, index: 5,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

@ -26,6 +26,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -57,6 +58,7 @@ 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,6 +20,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 0, index: 0,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {

View file

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

View file

@ -39,6 +39,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 4, index: 4,
}, },
fail_on_not_found: true,
}, },
CreateIteratorFromSlotToSlot { CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot { iterator_slot: VariableSlot {
@ -78,6 +79,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 6, index: 6,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {
@ -104,6 +106,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 11, index: 11,
}, },
fail_on_not_found: true,
}, },
CreateIteratorFromSlotToSlot { CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot { iterator_slot: VariableSlot {
@ -143,6 +146,7 @@ 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:
stuff: Hemera
test: true test: true
stuff: Hemera
--- ---
VMInstructions { VMInstructions {
labels: { labels: {
@ -22,6 +22,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 1, index: 1,
}, },
fail_on_not_found: true,
}, },
JumpIfNotTrue { JumpIfNotTrue {
emit_slot: VariableSlot { emit_slot: VariableSlot {
@ -42,6 +43,7 @@ VMInstructions {
slot: VariableSlot { slot: VariableSlot {
index: 3, index: 3,
}, },
fail_on_not_found: true,
}, },
EmitFromSlot { EmitFromSlot {
slot: VariableSlot { slot: VariableSlot {