Rename emit to compiler
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
705c6a8818
commit
d72f888849
9 changed files with 221 additions and 15 deletions
|
|
@ -12,7 +12,7 @@ fuzz_target!(|data: String| -> Corpus {
|
||||||
return Corpus::Keep;
|
return Corpus::Keep;
|
||||||
};
|
};
|
||||||
|
|
||||||
let _instructions = nomo::emit::emit_machine(ast);
|
let _instructions = nomo::compiler::emit_machine(ast);
|
||||||
|
|
||||||
Corpus::Keep
|
Corpus::Keep
|
||||||
});
|
});
|
||||||
|
|
|
||||||
206
src/compiler/lib.rs
Normal file
206
src/compiler/lib.rs
Normal file
|
|
@ -0,0 +1,206 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use displaydoc::Display;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
use crate::compiler::VMInstructions;
|
||||||
|
use crate::functions::FunctionMap;
|
||||||
|
use crate::input::NomoInput;
|
||||||
|
use crate::value::NomoValue;
|
||||||
|
use crate::value::NomoValueError;
|
||||||
|
|
||||||
|
pub mod parser;
|
||||||
|
pub mod compiler;
|
||||||
|
pub mod eval;
|
||||||
|
pub mod functions;
|
||||||
|
pub mod input;
|
||||||
|
pub mod lexer;
|
||||||
|
pub mod value;
|
||||||
|
|
||||||
|
#[derive(Debug, Error, Display)]
|
||||||
|
pub enum NomoError {
|
||||||
|
/// Could not parse the given template
|
||||||
|
ParseError {
|
||||||
|
#[from]
|
||||||
|
source: lexer::ParseFailure,
|
||||||
|
},
|
||||||
|
/// Invalid Template
|
||||||
|
AstError {
|
||||||
|
#[from]
|
||||||
|
source: parser::AstFailure,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// An error occurred while evaluating
|
||||||
|
EvaluationError {
|
||||||
|
#[from]
|
||||||
|
source: eval::EvaluationError,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// The template '{0}' could not be found
|
||||||
|
UnknownTemplate(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Nomo {
|
||||||
|
templates: HashMap<String, Template>,
|
||||||
|
function_map: FunctionMap,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Nomo {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Nomo {
|
||||||
|
pub fn new() -> Nomo {
|
||||||
|
Nomo {
|
||||||
|
templates: HashMap::new(),
|
||||||
|
function_map: FunctionMap::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_template(
|
||||||
|
&mut self,
|
||||||
|
name: impl Into<String>,
|
||||||
|
value: impl Into<NomoInput>,
|
||||||
|
) -> Result<(), NomoError> {
|
||||||
|
let source = value.into();
|
||||||
|
let parse = lexer::parse(source.clone())?;
|
||||||
|
let ast = parser::parse(parse.tokens())?;
|
||||||
|
|
||||||
|
let instructions = compiler::emit_machine(ast);
|
||||||
|
|
||||||
|
self.templates
|
||||||
|
.insert(name.into(), Template { instructions });
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&self, name: &str, ctx: &Context) -> Result<String, NomoError> {
|
||||||
|
let template = self
|
||||||
|
.templates
|
||||||
|
.get(name)
|
||||||
|
.ok_or_else(|| NomoError::UnknownTemplate(name.to_string()))?;
|
||||||
|
|
||||||
|
let res = eval::execute(&self.function_map, &template.instructions, ctx)?;
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Template {
|
||||||
|
instructions: VMInstructions,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Context {
|
||||||
|
values: HashMap<String, NomoValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Context {
|
||||||
|
fn default() -> Self {
|
||||||
|
Context::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Context {
|
||||||
|
pub fn new() -> Context {
|
||||||
|
Context {
|
||||||
|
values: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_insert(
|
||||||
|
&mut self,
|
||||||
|
key: impl Into<String>,
|
||||||
|
value: impl TryInto<NomoValue, Error = NomoValueError>,
|
||||||
|
) -> Result<(), NomoValueError> {
|
||||||
|
self.values.insert(key.into(), value.try_into()?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<NomoValue>) {
|
||||||
|
self.values.insert(key.into(), value.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn values(&self) -> &HashMap<String, NomoValue> {
|
||||||
|
&self.values
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct SourceSpan {
|
||||||
|
pub range: std::ops::Range<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is just like the standard .resume_after(), except we only resume on Cut errors.
|
||||||
|
fn resume_after_cut<Input, Output, Error, ParseNext, ParseRecover>(
|
||||||
|
mut parser: ParseNext,
|
||||||
|
mut recover: ParseRecover,
|
||||||
|
) -> impl winnow::Parser<Input, Option<Output>, Error>
|
||||||
|
where
|
||||||
|
Input: winnow::stream::Stream + winnow::stream::Recover<Error>,
|
||||||
|
Error: winnow::error::ParserError<Input> + winnow::error::FromRecoverableError<Input, Error>,
|
||||||
|
ParseNext: winnow::Parser<Input, Output, Error>,
|
||||||
|
ParseRecover: winnow::Parser<Input, (), Error>,
|
||||||
|
{
|
||||||
|
winnow::combinator::trace("resume_after_cut", move |input: &mut Input| {
|
||||||
|
resume_after_cut_inner(&mut parser, &mut recover, input)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resume_after_cut_inner<P, R, I, O, E>(
|
||||||
|
parser: &mut P,
|
||||||
|
recover: &mut R,
|
||||||
|
i: &mut I,
|
||||||
|
) -> winnow::Result<Option<O>, E>
|
||||||
|
where
|
||||||
|
P: winnow::Parser<I, O, E>,
|
||||||
|
R: winnow::Parser<I, (), E>,
|
||||||
|
I: winnow::stream::Stream,
|
||||||
|
I: winnow::stream::Recover<E>,
|
||||||
|
E: winnow::error::ParserError<I> + winnow::error::FromRecoverableError<I, E>,
|
||||||
|
{
|
||||||
|
let token_start = i.checkpoint();
|
||||||
|
let mut err = match parser.parse_next(i) {
|
||||||
|
Ok(o) => {
|
||||||
|
return Ok(Some(o));
|
||||||
|
}
|
||||||
|
Err(e) if e.is_incomplete() || e.is_backtrack() => {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
Err(err) => err,
|
||||||
|
};
|
||||||
|
let err_start = i.checkpoint();
|
||||||
|
if recover.parse_next(i).is_ok() {
|
||||||
|
if let Err(err_) = i.record_err(&token_start, &err_start, err) {
|
||||||
|
err = err_;
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i.reset(&err_start);
|
||||||
|
err = E::from_recoverable_error(&token_start, &err_start, i, err);
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::Context;
|
||||||
|
use crate::Nomo;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_simple_template() {
|
||||||
|
let mut temp = Nomo::new();
|
||||||
|
|
||||||
|
temp.add_template("base", "Hello {{= name }}").unwrap();
|
||||||
|
|
||||||
|
let mut ctx = Context::new();
|
||||||
|
ctx.insert("name", "World");
|
||||||
|
|
||||||
|
let rendered = temp.render("base", &ctx).unwrap();
|
||||||
|
|
||||||
|
insta::assert_snapshot!(rendered, @"Hello World")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -494,7 +494,7 @@ fn emit_expr_load(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::emit::emit_machine;
|
use crate::compiler::emit_machine;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_simple_variable_interpolation() {
|
fn check_simple_variable_interpolation() {
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
source: src/emit/mod.rs
|
source: src/compiler/mod.rs
|
||||||
expression: emit
|
expression: emit
|
||||||
---
|
---
|
||||||
VMInstructions {
|
VMInstructions {
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
source: src/emit/mod.rs
|
source: src/compiler/mod.rs
|
||||||
expression: emit
|
expression: emit
|
||||||
---
|
---
|
||||||
VMInstructions {
|
VMInstructions {
|
||||||
|
|
@ -4,9 +4,9 @@ use displaydoc::Display;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::Context;
|
use crate::Context;
|
||||||
use crate::emit::Instruction;
|
use crate::compiler::Instruction;
|
||||||
use crate::emit::VMInstructions;
|
use crate::compiler::VMInstructions;
|
||||||
use crate::emit::VariableSlot;
|
use crate::compiler::VariableSlot;
|
||||||
use crate::functions::FunctionMap;
|
use crate::functions::FunctionMap;
|
||||||
use crate::input::NomoInput;
|
use crate::input::NomoInput;
|
||||||
use crate::value::NomoValue;
|
use crate::value::NomoValue;
|
||||||
|
|
@ -247,7 +247,7 @@ mod tests {
|
||||||
|
|
||||||
let ast = crate::parser::parse(parsed.tokens()).unwrap();
|
let ast = crate::parser::parse(parsed.tokens()).unwrap();
|
||||||
|
|
||||||
let emit = crate::emit::emit_machine(ast);
|
let emit = crate::compiler::emit_machine(ast);
|
||||||
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
context.insert("world", "World");
|
context.insert("world", "World");
|
||||||
|
|
@ -268,7 +268,7 @@ mod tests {
|
||||||
|
|
||||||
let ast = crate::parser::parse(parsed.tokens()).unwrap();
|
let ast = crate::parser::parse(parsed.tokens()).unwrap();
|
||||||
|
|
||||||
let emit = crate::emit::emit_machine(ast);
|
let emit = crate::compiler::emit_machine(ast);
|
||||||
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
context.insert("world", "World");
|
context.insert("world", "World");
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,18 @@ use std::collections::HashMap;
|
||||||
use displaydoc::Display;
|
use displaydoc::Display;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::emit::VMInstructions;
|
use crate::compiler::VMInstructions;
|
||||||
use crate::functions::FunctionMap;
|
use crate::functions::FunctionMap;
|
||||||
use crate::input::NomoInput;
|
use crate::input::NomoInput;
|
||||||
use crate::value::NomoValue;
|
use crate::value::NomoValue;
|
||||||
use crate::value::NomoValueError;
|
use crate::value::NomoValueError;
|
||||||
|
|
||||||
pub mod parser;
|
pub mod compiler;
|
||||||
pub mod emit;
|
|
||||||
pub mod eval;
|
pub mod eval;
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod lexer;
|
pub mod lexer;
|
||||||
|
pub mod parser;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
#[derive(Debug, Error, Display)]
|
#[derive(Debug, Error, Display)]
|
||||||
|
|
@ -68,7 +68,7 @@ impl Nomo {
|
||||||
let parse = lexer::parse(source.clone())?;
|
let parse = lexer::parse(source.clone())?;
|
||||||
let ast = parser::parse(parse.tokens())?;
|
let ast = parser::parse(parse.tokens())?;
|
||||||
|
|
||||||
let instructions = emit::emit_machine(ast);
|
let instructions = compiler::emit_machine(ast);
|
||||||
|
|
||||||
self.templates
|
self.templates
|
||||||
.insert(name.into(), Template { instructions });
|
.insert(name.into(), Template { instructions });
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,6 @@ fn check_files() {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let _emit = nomo::emit::emit_machine(ast);
|
let _emit = nomo::compiler::emit_machine(ast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ fn check_for_input([path]: [&Path; 1]) {
|
||||||
|
|
||||||
insta::assert_debug_snapshot!(format!("{basename}.2-ast"), ast);
|
insta::assert_debug_snapshot!(format!("{basename}.2-ast"), ast);
|
||||||
|
|
||||||
let emit = nomo::emit::emit_machine(ast);
|
let emit = nomo::compiler::emit_machine(ast);
|
||||||
|
|
||||||
insta::assert_debug_snapshot!(format!("{basename}.3-instructions"), emit);
|
insta::assert_debug_snapshot!(format!("{basename}.3-instructions"), emit);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue