diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-05-20 11:36:31 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-05-20 11:36:31 +0200 |
| commit | e3215fa3b92574e2087c28b1d494d397e6819236 (patch) | |
| tree | 27be3c1b7679a1905ec2340d3cee2b335d021142 /src/func.rs | |
| parent | 5c66bac689f4551e30c20e57087d47245853b5fe (diff) | |
Unify parsing and typesetting functions. 🗳
Diffstat (limited to 'src/func.rs')
| -rw-r--r-- | src/func.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/func.rs b/src/func.rs index 769c77bf..c90e87ad 100644 --- a/src/func.rs +++ b/src/func.rs @@ -4,8 +4,9 @@ use std::any::Any; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; -use crate::syntax::{FuncHeader, Expression}; -use crate::parsing::{FuncContext, ParseResult}; +use crate::syntax::FuncHeader; +use crate::parsing::{ParseContext, ParseResult}; +use crate::engine::{TypesetContext, TypesetResult}; /// Types that act as functions. @@ -17,10 +18,11 @@ use crate::parsing::{FuncContext, ParseResult}; /// used as functions, that is they fulfill the bounds `Debug + PartialEq + 'static`. pub trait Function: FunctionBounds { /// Parse the tokens of the context with the given header and scope into self. - fn parse(context: FuncContext) -> ParseResult<Self> where Self: Sized; + fn parse(header: &FuncHeader, body: Option<&str>, ctx: &ParseContext) + -> ParseResult<Self> where Self: Sized; /// Execute the function and optionally yield a return value. - fn typeset(&self, header: &FuncHeader) -> Option<Expression>; + fn typeset(&self, ctx: &TypesetContext) -> TypesetResult<()>; } impl PartialEq for dyn Function { @@ -61,7 +63,8 @@ pub struct Scope { } /// A function which transforms a parsing context into a boxed function. -type ParseFunc = dyn Fn(FuncContext) -> ParseResult<Box<dyn Function>>; +type ParseFunc = dyn Fn(&FuncHeader, Option<&str>, &ParseContext) + -> ParseResult<Box<dyn Function>>; impl Scope { /// Create a new empty scope. @@ -69,12 +72,17 @@ impl Scope { Scope { parsers: HashMap::new() } } + /// Create a new scope with the standard functions contained. + pub fn with_std() -> Scope { + Scope::new() + } + /// Add a function type to the scope with a given name. pub fn add<F: Function + 'static>(&mut self, name: &str) { self.parsers.insert( name.to_owned(), - Box::new(|context| { - F::parse(context).map(|func| Box::new(func) as Box<dyn Function>) + Box::new(|h, b, c| { + F::parse(h, b, c).map(|func| Box::new(func) as Box<dyn Function>) }) ); } |
