diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-02 22:21:58 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-02 22:21:58 +0200 |
| commit | 5a8f2fb73ddafba9fdbe952385ae2676126183ae (patch) | |
| tree | f65dcc5c8fc3809171e0cc1a985d466159ccb254 /src/syntax | |
| parent | 266d457292e7461d448f9141030028ea68b573d1 (diff) | |
Replace body! macro with functions 🧰
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/mod.rs | 8 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 21 | ||||
| -rw-r--r-- | src/syntax/scope.rs | 15 | ||||
| -rw-r--r-- | src/syntax/test.rs | 3 |
4 files changed, 38 insertions, 9 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 86c2fd24..6b1f3d08 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -4,6 +4,14 @@ #[macro_use] mod test; +/// Basic types used around the syntax side. +pub mod prelude { + pub use super::expr::*; + pub use super::tree::{SyntaxTree, SyntaxNode, DynamicNode}; + pub use super::span::{SpanVec, Span, Spanned}; + pub use super::value::*; +} + pub mod decoration; pub mod expr; pub mod tree; diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 7594c14d..bcbcb8d4 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -13,6 +13,27 @@ use super::tree::{SyntaxTree, SyntaxNode, DynamicNode}; /// A function which parses a function call into a tree. pub type CallParser = dyn Fn(FuncCall, &ParseState) -> Pass<Box<dyn DynamicNode>>; +/// Parse a function call. +pub trait ParseCall { + /// A metadata type whose value is passed into the function parser. This + /// allows a single function to do different things depending on the value + /// that needs to be given when inserting the function into a + /// [scope](crate::syntax::Scope). + /// + /// For example, the functions `word.spacing`, `line.spacing` and + /// `par.spacing` are actually all the same function + /// [`ContentSpacingFunc`](crate::library::ContentSpacingFunc) with the + /// metadata specifiy which content should be spaced. + type Meta: Clone; + + /// Parse the header and body into this function given a context. + fn parse( + header: FuncCall, + state: &ParseState, + metadata: Self::Meta, + ) -> Pass<Self> where Self: Sized; +} + /// An invocation of a function. #[derive(Debug, Clone, PartialEq)] pub struct FuncCall<'s> { diff --git a/src/syntax/scope.rs b/src/syntax/scope.rs index d3092944..8fdad6a0 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 crate::func::ParseFunc; -use super::parsing::CallParser; +use super::parsing::{CallParser, ParseCall}; use super::tree::DynamicNode; /// A map from identifiers to function parsers. @@ -17,7 +16,7 @@ impl Scope { /// Create a new empty scope with a fallback parser that is invoked when no /// match is found. pub fn new<F>() -> Scope - where F: ParseFunc<Meta=()> + DynamicNode + 'static { + where F: ParseCall<Meta=()> + DynamicNode + 'static { Scope { parsers: HashMap::new(), fallback: make_parser::<F>(()), @@ -31,14 +30,14 @@ impl Scope { /// Associate the given name with a type that is parseable into a function. pub fn add<F>(&mut self, name: &str) - where F: ParseFunc<Meta=()> + DynamicNode + 'static { + where F: ParseCall<Meta=()> + DynamicNode + 'static { self.add_with_meta::<F>(name, ()); } /// Add a parseable type with additional metadata that is given to the /// parser (other than the default of `()`). - pub fn add_with_meta<F>(&mut self, name: &str, metadata: <F as ParseFunc>::Meta) - where F: ParseFunc + DynamicNode + 'static { + 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), @@ -64,8 +63,8 @@ impl Debug for Scope { } } -fn make_parser<F>(metadata: <F as ParseFunc>::Meta) -> Box<CallParser> -where F: ParseFunc + DynamicNode + 'static { +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>) diff --git a/src/syntax/test.rs b/src/syntax/test.rs index b701e577..504bc334 100644 --- a/src/syntax/test.rs +++ b/src/syntax/test.rs @@ -1,5 +1,6 @@ use std::fmt::Debug; +use crate::func::parse_maybe_body; use super::decoration::Decoration; use super::expr::{Expr, Ident, Tuple, NamedTuple, Object, Pair}; use super::parsing::{FuncHeader, FuncArgs, FuncArg}; @@ -71,7 +72,7 @@ function! { header.args.key.0.clear(); DebugFn { header: cloned, - body: body!(opt: body, state, f), + body: parse_maybe_body(body, state, f), } } |
