From 72a9631b038d1a60e4e4a78e92cd69e6f8ce4316 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 5 Dec 2019 19:48:37 +0100 Subject: =?UTF-8?q?Move=20arg=20parser=20into=20`FuncArgs`=20and=20create?= =?UTF-8?q?=20(incomplete)=20consistent=20map=20=F0=9F=A7=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/func/mod.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/func/mod.rs') diff --git a/src/func/mod.rs b/src/func/mod.rs index 31e31592..7ee7d779 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -8,17 +8,12 @@ use self::prelude::*; #[macro_use] pub mod macros; -pub mod args; +pub mod map; /// Useful imports for creating your own functions. pub mod prelude { - pub use Command::*; - pub use super::args::*; - pub use super::{Scope, ParseFunc, LayoutFunc, Command, Commands}; - pub use crate::syntax::{SyntaxTree, FuncHeader, FuncArgs, Expression, Spanned, Span}; - pub use crate::syntax::{parse, ParseContext, ParseResult}; - pub use crate::size::{Size, Size2D, SizeBox}; - pub use crate::style::{PageStyle, TextStyle}; + pub use super::map::ConsistentMap; + pub use crate::func::{Scope, ParseFunc, LayoutFunc, Command, Commands}; pub use crate::layout::{ layout_tree, Layout, MultiLayout, LayoutContext, LayoutSpace, LayoutSpaces, @@ -26,16 +21,25 @@ pub mod prelude { LayoutAlignment, Alignment, SpacingKind, LayoutResult, }; + pub use crate::syntax::{ + parse, ParseContext, ParseResult, + SyntaxTree, FuncCall, FuncArgs, PosArg, KeyArg, + Expression, Ident, ExpressionKind, + Spanned, Span + }; + pub use crate::size::{Size, Size2D, SizeBox, ScaleSize, FSize, PSize}; + pub use crate::style::{LayoutStyle, PageStyle, TextStyle}; + pub use Command::*; } /// Types representing functions that are parsed from source code. pub trait ParseFunc { - type Meta; + type Meta: Clone; /// Parse the header and body into this function given a context. fn parse( - header: &FuncHeader, - body: Option<&str>, + args: FuncArgs, + body: Option>, ctx: ParseContext, metadata: Self::Meta, ) -> ParseResult where Self: Sized; @@ -126,8 +130,8 @@ pub struct Scope { /// A function which parses the source of a function into a function type which /// implements [`LayoutFunc`]. type Parser = dyn Fn( - &FuncHeader, - Option<&str>, + FuncArgs, + Option>, ParseContext ) -> ParseResult>; @@ -153,11 +157,11 @@ impl Scope { /// Add a parseable type with additional metadata that is given to the /// parser (other than the default of `()`). pub fn add_with_metadata(&mut self, name: &str, metadata: T) - where F: ParseFunc + LayoutFunc + 'static, T: 'static { + where F: ParseFunc + LayoutFunc + 'static, T: 'static + Clone { self.parsers.insert( name.to_owned(), - Box::new(|h, b, c| { - F::parse(h, b, c, metadata) + Box::new(move |a, b, c| { + F::parse(a, b, c, metadata.clone()) .map(|f| Box::new(f) as Box) }) ); -- cgit v1.2.3