diff options
Diffstat (limited to 'src/func')
| -rw-r--r-- | src/func/macros.rs | 22 | ||||
| -rw-r--r-- | src/func/mod.rs | 13 |
2 files changed, 23 insertions, 12 deletions
diff --git a/src/func/macros.rs b/src/func/macros.rs index 1e92d735..92b6c16e 100644 --- a/src/func/macros.rs +++ b/src/func/macros.rs @@ -22,7 +22,7 @@ macro_rules! function { // Parse trait. (@parse($($a:tt)*) parse(default) $($r:tt)*) => { - function!(@parse($($a)*) parse() { Default::default() } $($r)*); + function!(@parse($($a)*) parse(_h, _b, _c, _e, _d, _m) {Default::default() } $($r)*); }; (@parse($($a:tt)*) parse($h:ident, $b:ident, $c:ident, $e:ident, $d:ident) $($r:tt)* ) => { function!(@parse($($a)*) parse($h, $b, $c, $e, $d, _metadata) $($r)*); @@ -40,7 +40,7 @@ macro_rules! function { fn parse( #[allow(unused)] mut $header: FuncHeader, - #[allow(unused)] $body: Option<Spanned<&str>>, + #[allow(unused)] $body: Option<(Position, &str)>, #[allow(unused)] $ctx: ParseContext, #[allow(unused)] $metadata: Self::Meta, ) -> Parsed<Self> where Self: Sized { @@ -85,7 +85,7 @@ macro_rules! function { macro_rules! body { (opt: $body:expr, $ctx:expr, $errors:expr, $decos:expr) => ({ $body.map(|body| { - let parsed = $crate::syntax::parse(body.span.start, body.v, $ctx); + let parsed = $crate::syntax::parse(body.0, body.1, $ctx); $errors.extend(parsed.errors); $decos.extend(parsed.decorations); parsed.output @@ -99,13 +99,19 @@ macro_rules! body { }; } -/// Construct a spanned error. +/// Construct an error with an optional span. #[macro_export] macro_rules! err { - ($span:expr, $($args:tt)*) => { - $crate::syntax::Spanned { - v: $crate::error::Error::new(format!($($args)*)), - span: $span, + (@$severity:ident: $span:expr; $($args:tt)*) => { + $crate::syntax::Spanned { v: err!(@Error: $($args)*), span: $span } + }; + + (@$severity:ident: $($args:tt)*) => { + $crate::error::Error { + message: format!($($args)*), + severity: $crate::error::Severity::$severity, } }; + + ($($tts:tt)*) => { err!(@Error: $($tts)*) }; } diff --git a/src/func/mod.rs b/src/func/mod.rs index 1ca226c3..e3399903 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -1,6 +1,5 @@ //! Dynamic typesetting functions. -use std::any::Any; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; @@ -12,6 +11,7 @@ mod macros; /// Useful imports for creating your own functions. pub mod prelude { pub use super::{Scope, Parse, Command, Commands}; + pub use crate::error::Error; pub use crate::layout::prelude::*; pub use crate::syntax::prelude::*; pub use crate::size::{Size, Size2D, SizeBox, ValueBox, ScaleSize, FSize, PSize}; @@ -26,7 +26,7 @@ pub trait Parse { /// Parse the header and body into this function given a context. fn parse( header: FuncHeader, - body: Option<Spanned<&str>>, + body: Option<(Position, &str)>, ctx: ParseContext, metadata: Self::Meta, ) -> Parsed<Self> where Self: Sized; @@ -36,7 +36,7 @@ pub trait Parse { /// implements [`Model`]. type Parser = dyn Fn( FuncHeader, - Option<Spanned<&str>>, + Option<(Position, &str)>, ParseContext, ) -> Parsed<Box<dyn Model>>; @@ -102,11 +102,16 @@ impl Scope { } /// Return the parser with the given name if there is one. - pub(crate) fn get_parser(&self, name: &str) -> Result<&Parser, &Parser> { + pub fn get_parser(&self, name: &str) -> Result<&Parser, &Parser> { self.parsers.get(name) .map(|x| &**x) .ok_or_else(|| &*self.fallback) } + + /// Return the fallback parser. + pub fn get_fallback_parser(&self) -> &Parser { + &*self.fallback + } } impl Debug for Scope { |
