diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-04 13:48:07 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-04 13:48:07 +0200 |
| commit | 2467cd6272c13b618ad53c5dadff5b8c8e7885bf (patch) | |
| tree | 6ad13ec06a04997564efc514b40daa3fb65233e2 /src/syntax/scope.rs | |
| parent | ed4fdcb0ada909f1cc3d7436334e253f0ec14d55 (diff) | |
Refactor function parsing ♻
Diffstat (limited to 'src/syntax/scope.rs')
| -rw-r--r-- | src/syntax/scope.rs | 36 |
1 files changed, 5 insertions, 31 deletions
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<F>() -> Self - where - F: ParseCall<Meta = ()> + DynamicNode + 'static - { + pub fn new(fallback: Box<CallParser>) -> Self { Self { parsers: HashMap::new(), - fallback: make_parser::<F>(()), + fallback, } } /// Associate the given function name with a dynamic node type. - pub fn add<F>(&mut self, name: &str) - where - F: ParseCall<Meta = ()> + DynamicNode + 'static - { - self.add_with_meta::<F>(name, ()); - } - - /// Add a dynamic node type with additional metadata that is passed to the - /// parser. - pub fn add_with_meta<F>(&mut self, name: &str, metadata: <F as ParseCall>::Meta) - where - F: ParseCall + DynamicNode + 'static - { - self.parsers.insert(name.to_string(), make_parser::<F>(metadata)); + pub fn insert(&mut self, name: impl Into<String>, parser: Box<CallParser>) { + 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<F>(metadata: <F as ParseCall>::Meta) -> Box<CallParser> -where - F: ParseCall + DynamicNode + 'static, -{ - Box::new(move |f, s| { - F::parse(f, s, metadata.clone()) - .map(|tree| Box::new(tree) as Box<dyn DynamicNode>) - }) -} |
