From 08b91a265fcda74f5463473938ec33873b49a7f7 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 16 Jan 2020 17:51:04 +0100 Subject: =?UTF-8?q?Powerful=20parser=20testing=20=F0=9F=90=B1=E2=80=8D?= =?UTF-8?q?=F0=9F=91=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/func/macros.rs | 41 +++++++++++++++++++---------------------- src/func/mod.rs | 11 +++-------- 2 files changed, 22 insertions(+), 30 deletions(-) (limited to 'src/func') diff --git a/src/func/macros.rs b/src/func/macros.rs index 1083e53c..90c3b11e 100644 --- a/src/func/macros.rs +++ b/src/func/macros.rs @@ -52,46 +52,43 @@ macro_rules! function { }; // (1-arg) Parse a parse-definition with only the first argument. - (@parse $type:ident $meta:ty | parse($args:ident) $code:block $($rest:tt)*) => { - function!(@parse $type $meta | parse($args, _body, _ctx, _meta) $code $($rest)*); + (@parse $type:ident $meta:ty | parse($header:ident) $code:block $($rest:tt)*) => { + function!(@parse $type $meta | parse($header, _body, _ctx, _meta) $code $($rest)*); }; // (2-arg) Parse a parse-definition with only the first two arguments. (@parse $type:ident $meta:ty | - parse($args:ident, $body:pat) $code:block $($rest:tt)* + parse($header:ident, $body:pat) $code:block $($rest:tt)* ) => { - function!(@parse $type $meta | parse($args, $body, _ctx, _meta) $code $($rest)*); + function!(@parse $type $meta | parse($header, $body, _ctx, _meta) $code $($rest)*); }; // (3-arg) Parse a parse-definition with only the first three arguments. (@parse $type:ident $meta:ty | - parse($args:ident, $body:pat, $ctx:pat) $code:block $($rest:tt)* + parse($header:ident, $body:pat, $ctx:pat) $code:block $($rest:tt)* ) => { - function!(@parse $type $meta | parse($args, $body, $ctx, _meta) $code $($rest)*); + function!(@parse $type $meta | parse($header, $body, $ctx, _meta) $code $($rest)*); }; // (4-arg) Parse a parse-definition with all four arguments. (@parse $type:ident $meta:ty | - parse($args:ident, $body:pat, $ctx:pat, $metadata:pat) $code:block + parse($header:ident, $body:pat, $ctx:pat, $metadata:pat) $code:block $($rest:tt)* ) => { - use $crate::func::prelude::*; - impl $crate::func::ParseFunc for $type { type Meta = $meta; fn parse( - args: FuncArgs, + header: $crate::syntax::FuncHeader, $body: Option<&str>, - $ctx: ParseContext, + $ctx: $crate::syntax::ParseContext, $metadata: Self::Meta, - ) -> ParseResult where Self: Sized { + ) -> $crate::syntax::ParseResult where Self: Sized { #[allow(unused_mut)] - let mut $args = args; + let mut $header = header; let val = $code; - if !$args.is_empty() { - return Err($crate::TypesetError - ::with_message("unexpected arguments")); + if !$header.args.is_empty() { + return Err($crate::TypesetError::with_message("unexpected arguments")); } Ok(val) } @@ -112,14 +109,14 @@ macro_rules! function { // (2-arg) Parse a layout-definition with all arguments. (@layout $type:ident | layout($this:ident, $ctx:pat) $code:block) => { - use $crate::func::prelude::*; - - impl LayoutFunc for $type { + impl $crate::func::LayoutFunc for $type { fn layout<'a, 'life0, 'life1, 'async_trait>( &'a $this, - $ctx: LayoutContext<'life0, 'life1> - ) -> std::pin::Pin>> + 'async_trait + $ctx: $crate::layout::LayoutContext<'life0, 'life1> + ) -> std::pin::Pin> + > + 'async_trait >> where 'a: 'async_trait, diff --git a/src/func/mod.rs b/src/func/mod.rs index 90b2a31d..bfc2774c 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -14,12 +14,7 @@ mod macros; pub mod prelude { pub use crate::func::{Scope, ParseFunc, LayoutFunc, Command, Commands}; pub use crate::layout::prelude::*; - pub use crate::syntax::{ - ParseContext, ParseResult, - SyntaxTree, FuncCall, FuncArgs, - Expression, Ident, ExpressionKind, - Spanned, Span - }; + pub use crate::syntax::*; pub use crate::size::{Size, Size2D, SizeBox, ValueBox, ScaleSize, FSize, PSize}; pub use crate::style::{LayoutStyle, PageStyle, TextStyle}; pub use Command::*; @@ -31,7 +26,7 @@ pub trait ParseFunc { /// Parse the header and body into this function given a context. fn parse( - args: FuncArgs, + header: FuncHeader, body: Option<&str>, ctx: ParseContext, metadata: Self::Meta, @@ -125,7 +120,7 @@ pub struct Scope { /// A function which parses the source of a function into a function type which /// implements [`LayoutFunc`]. type Parser = dyn Fn( - FuncArgs, + FuncHeader, Option<&str>, ParseContext ) -> ParseResult>; -- cgit v1.2.3