diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-04-29 12:43:58 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-04-29 12:43:58 +0200 |
| commit | d514a05af1e7249412b3ecd257cd4673db3cd14b (patch) | |
| tree | 50b4fb3802a5406c290f8b97917e4854760c168a /src/func.rs | |
| parent | 383d8365cfd07d7e4c552293a537c519cd2d9c86 (diff) | |
Make parse tokens more static and efficient 🗜
Diffstat (limited to 'src/func.rs')
| -rw-r--r-- | src/func.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/func.rs b/src/func.rs index d85d7a18..d90cd85e 100644 --- a/src/func.rs +++ b/src/func.rs @@ -4,16 +4,12 @@ use std::any::Any; use std::collections::HashMap; use std::fmt::Debug; -use crate::syntax::{Token, FuncHeader, Expression}; -use crate::parsing::ParseResult; +use crate::syntax::{FuncHeader, Expression}; +use crate::parsing::{ParseTokens, ParseResult}; -/// An optional iterator over the tokens of a function body. -pub type BodyTokens<'a> = Option<Box<dyn Iterator<Item=Token<'a>> + 'a>>; - /// Parser functions. -pub type ParseFunc = dyn Fn(&FuncHeader, BodyTokens<'_>, &Scope) - -> ParseResult<Box<dyn Function>>; +pub type ParseFunc = dyn Fn(ParseContext) -> ParseResult<Box<dyn Function>>; /// Types that act as functions. /// @@ -24,8 +20,7 @@ pub type ParseFunc = dyn Fn(&FuncHeader, BodyTokens<'_>, &Scope) /// used as functions, that is they fulfill the bounds `Debug + PartialEq + 'static`. pub trait Function: FunctionBounds { /// Parse the function. - fn parse(header: &FuncHeader, tokens: BodyTokens<'_>, scope: &Scope) - -> ParseResult<Self> where Self: Sized; + fn parse(context: ParseContext) -> ParseResult<Self> where Self: Sized; /// Execute the function and optionally yield a return value. fn typeset(&self, header: &FuncHeader) -> Option<Expression>; @@ -46,7 +41,7 @@ impl Scope { pub fn add<F: Function + 'static>(&mut self, name: &str) { self.parsers.insert( name.to_owned(), - Box::new(|header, tokens, scope| match F::parse(header, tokens, scope) { + Box::new(|context| match F::parse(context) { Ok(func) => Ok(Box::new(func)), Err(err) => Err(err), }) @@ -59,6 +54,16 @@ impl Scope { } } +/// The context for parsing a function. +pub struct ParseContext<'s, 't> { + /// The header of the function to be parsed. + pub header: &'s FuncHeader, + /// Tokens if the function has a body, otherwise nothing. + pub tokens: Option<&'s mut ParseTokens<'t>>, + /// The current scope containing function definitions. + pub scope: &'s Scope, +} + /// A helper trait that describes requirements for types that can implement [`Function`]. /// /// Automatically implemented for all types which fulfill to the bounds |
