From 2467cd6272c13b618ad53c5dadff5b8c8e7885bf Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 4 Aug 2020 13:48:07 +0200 Subject: =?UTF-8?q?Refactor=20function=20parsing=20=E2=99=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/scope.rs | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) (limited to 'src/syntax/scope.rs') diff --git a/src/syntax/scope.rs b/src/syntax/scope.rs index aac2b1b8..c17ff64d 100644 --- a/src/syntax/scope.rs +++ b/src/syntax/scope.rs @@ -3,8 +3,7 @@ use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; -use super::parsing::{CallParser, ParseCall}; -use super::tree::DynamicNode; +use super::parsing::CallParser; /// A map from identifiers to function parsers. pub struct Scope { @@ -15,31 +14,16 @@ pub struct Scope { impl Scope { /// Create a new empty scope with a fallback parser that is invoked when no /// match is found. - pub fn new() -> Self - where - F: ParseCall + DynamicNode + 'static - { + pub fn new(fallback: Box) -> Self { Self { parsers: HashMap::new(), - fallback: make_parser::(()), + fallback, } } /// Associate the given function name with a dynamic node type. - pub fn add(&mut self, name: &str) - where - F: ParseCall + DynamicNode + 'static - { - self.add_with_meta::(name, ()); - } - - /// Add a dynamic node type with additional metadata that is passed to the - /// parser. - pub fn add_with_meta(&mut self, name: &str, metadata: ::Meta) - where - F: ParseCall + DynamicNode + 'static - { - self.parsers.insert(name.to_string(), make_parser::(metadata)); + pub fn insert(&mut self, name: impl Into, parser: Box) { + self.parsers.insert(name.into(), parser); } /// Return the parser with the given name if there is one. @@ -58,13 +42,3 @@ impl Debug for Scope { f.debug_set().entries(self.parsers.keys()).finish() } } - -fn make_parser(metadata: ::Meta) -> Box -where - F: ParseCall + DynamicNode + 'static, -{ - Box::new(move |f, s| { - F::parse(f, s, metadata.clone()) - .map(|tree| Box::new(tree) as Box) - }) -} -- cgit v1.2.3