summaryrefslogtreecommitdiff
path: root/src/func.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-04-29 13:41:00 +0200
committerLaurenz <laurmaedje@gmail.com>2019-04-29 13:41:00 +0200
commitc384e524800bb55da0c5614f412e7d835ed67945 (patch)
tree91ff5b4de641bdd089b9396a8f3631b9ac0405e6 /src/func.rs
parentd514a05af1e7249412b3ecd257cd4673db3cd14b (diff)
Improve code quality 🎫
Diffstat (limited to 'src/func.rs')
-rw-r--r--src/func.rs23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/func.rs b/src/func.rs
index d90cd85e..e9212278 100644
--- a/src/func.rs
+++ b/src/func.rs
@@ -2,14 +2,14 @@
use std::any::Any;
use std::collections::HashMap;
-use std::fmt::Debug;
+use std::fmt::{self, Debug, Formatter};
use crate::syntax::{FuncHeader, Expression};
use crate::parsing::{ParseTokens, ParseResult};
-/// Parser functions.
-pub type ParseFunc = dyn Fn(ParseContext) -> ParseResult<Box<dyn Function>>;
+/// A function which transforms a parsing context into a boxed function.
+type ParseFunc = dyn Fn(ParseContext) -> ParseResult<Box<dyn Function>>;
/// Types that act as functions.
///
@@ -19,7 +19,7 @@ pub type ParseFunc = dyn Fn(ParseContext) -> ParseResult<Box<dyn Function>>;
/// The trait `FunctionBounds` is automatically implemented for types which can be
/// used as functions, that is they fulfill the bounds `Debug + PartialEq + 'static`.
pub trait Function: FunctionBounds {
- /// Parse the function.
+ /// Parse the tokens of the context with the given header and scope into self.
fn parse(context: ParseContext) -> ParseResult<Self> where Self: Sized;
/// Execute the function and optionally yield a return value.
@@ -41,20 +41,27 @@ impl Scope {
pub fn add<F: Function + 'static>(&mut self, name: &str) {
self.parsers.insert(
name.to_owned(),
- Box::new(|context| match F::parse(context) {
- Ok(func) => Ok(Box::new(func)),
- Err(err) => Err(err),
+ Box::new(|context| {
+ F::parse(context).map(|func| Box::new(func) as Box<dyn Function>)
})
);
}
/// Return the parser with the given name if there is one.
- pub fn get_parser(&self, name: &str) -> Option<&ParseFunc> {
+ pub(crate) fn get_parser(&self, name: &str) -> Option<&ParseFunc> {
self.parsers.get(name).map(|x| &**x)
}
}
+impl Debug for Scope {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "Scope ")?;
+ write!(f, "{:?}", self.parsers.keys())
+ }
+}
+
/// The context for parsing a function.
+#[derive(Debug)]
pub struct ParseContext<'s, 't> {
/// The header of the function to be parsed.
pub header: &'s FuncHeader,