diff options
Diffstat (limited to 'src/func')
| -rw-r--r-- | src/func/helpers.rs | 46 | ||||
| -rw-r--r-- | src/func/mod.rs | 6 |
2 files changed, 26 insertions, 26 deletions
diff --git a/src/func/helpers.rs b/src/func/helpers.rs index ea0f6b2f..a7c270c2 100644 --- a/src/func/helpers.rs +++ b/src/func/helpers.rs @@ -5,16 +5,16 @@ use super::prelude::*; /// Implement the function trait more concisely. #[macro_export] macro_rules! function { - (data: $ident:ident, $($tts:tt)*) => { + (data: $ident:ident, $($tts:tt)*) => ( #[allow(unused_imports)] use $crate::func::prelude::*; impl Function for $ident { function!(@parse $ident, $($tts)*); } - }; + ); - (@parse $ident:ident, parse: plain, $($tts:tt)*) => { + (@parse $ident:ident, parse: plain, $($tts:tt)*) => ( fn parse(header: &FuncHeader, body: Option<&str>, _: ParseContext) -> ParseResult<Self> where Self: Sized { @@ -25,14 +25,14 @@ macro_rules! function { Ok($ident) } function!(@layout $($tts)*); - }; + ); ( @parse $ident:ident, parse($args:ident, $body:ident, $ctx:ident) $block:block $($tts:tt)* - ) => { + ) => ( fn parse(header: &FuncHeader, body: Option<&str>, ctx: ParseContext) -> ParseResult<Self> where Self: Sized { @@ -42,15 +42,15 @@ macro_rules! function { $block } function!(@layout $($tts)*); - }; + ); - (@layout layout($this:pat, $ctx:pat) $block:block) => { + (@layout layout($this:pat, $ctx:pat) $block:block) => ( fn layout(&self, ctx: LayoutContext) -> LayoutResult<CommandList> { let $ctx = ctx; let $this = self; $block } - }; + ); } /// Parse the body of a function. @@ -65,56 +65,56 @@ macro_rules! parse { } }; - (optional: $body:expr, $ctx:expr) => { + (optional: $body:expr, $ctx:expr) => ( if let Some(body) = $body { Some($crate::syntax::parse(body, $ctx)?) } else { None } - }; + ); - (required: $body:expr, $ctx:expr) => { + (required: $body:expr, $ctx:expr) => ( if let Some(body) = $body { $crate::syntax::parse(body, $ctx)? } else { err!("expected body"); } - } + ) } /// Return a formatted parsing error. #[macro_export] macro_rules! err { - ($($tts:tt)*) => { - return Err($crate::syntax::ParseError::new(format!($($tts)*))); - }; + (@$($tts:tt)*) => ($crate::syntax::ParseError::new(format!($($tts)*))); + ($($tts:tt)*) => (return Err(err!(@$($tts)*));); } /// Convenient interface for parsing function arguments. pub struct Arguments<'a> { - args: Peekable<Iter<'a, Expression>>, + args: Peekable<Iter<'a, Spanned<Expression>>>, } impl<'a> Arguments<'a> { pub fn new(header: &'a FuncHeader) -> Arguments<'a> { Arguments { - args: header.args.iter().peekable() + args: header.args.positional.iter().peekable() } } - pub fn get_expr(&mut self) -> ParseResult<&'a Expression> { + pub fn get_expr(&mut self) -> ParseResult<&'a Spanned<Expression>> { self.args.next() .ok_or_else(|| ParseError::new("expected expression")) } - pub fn get_ident(&mut self) -> ParseResult<&'a str> { - match self.get_expr()? { - Expression::Ident(s) => Ok(s.as_str()), - _ => Err(ParseError::new("expected identifier")), + pub fn get_ident(&mut self) -> ParseResult<Spanned<&'a str>> { + let expr = self.get_expr()?; + match &expr.val { + Expression::Ident(s) => Ok(Spanned::new(s.as_str(), expr.span)), + _ => err!("expected identifier"), } } - pub fn get_ident_if_present(&mut self) -> ParseResult<Option<&'a str>> { + pub fn get_ident_if_present(&mut self) -> ParseResult<Option<Spanned<&'a str>>> { if self.args.peek().is_some() { self.get_ident().map(|s| Some(s)) } else { diff --git a/src/func/mod.rs b/src/func/mod.rs index 30b5b825..402f0111 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -15,7 +15,7 @@ pub mod prelude { pub use crate::func::{Command, CommandList, Function}; pub use crate::layout::{layout_tree, Layout, LayoutContext, MultiLayout}; pub use crate::layout::{Flow, Alignment, LayoutError, LayoutResult}; - pub use crate::syntax::{Expression, FuncHeader, SyntaxTree}; + pub use crate::syntax::{SyntaxTree, FuncHeader, FuncArgs, Expression, Spanned, Span}; pub use crate::syntax::{parse, ParseContext, ParseError, ParseResult}; pub use crate::size::{Size, Size2D, SizeBox}; pub use crate::style::{PageStyle, TextStyle}; @@ -144,9 +144,9 @@ pub enum Command<'a> { } macro_rules! commands { - ($($x:expr),*$(,)*) => ({ + ($($x:expr),*$(,)*) => ( $crate::func::CommandList::from_vec(vec![$($x,)*]) - }); + ); } /// A map from identifiers to functions. |
