1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
use super::prelude::*;
use std::iter::Peekable;
use std::slice::Iter;
/// Implement the function trait more concisely.
#[macro_export]
macro_rules! function {
(data: $ident:ident, $($tts:tt)*) => {
impl Function for $ident {
function!(@parse $ident, $($tts)*);
}
};
(@parse $ident:ident, parse: plain, $($tts:tt)*) => {
fn parse(header: &FuncHeader, body: Option<&str>, _: ParseContext)
-> ParseResult<Self> where Self: Sized
{
Arguments::new(header).done()?;
if body.is_some() {
err!("expected no body");
}
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
{
#[allow(unused_mut)] let mut $args = Arguments::new(header);
let $body = body;
let $ctx = ctx;
$block
}
function!(@layout $($tts)*);
};
(@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.
/// - If the function does not expect a body, use `forbidden`.
/// - If the function can have a body, use `optional`.
/// - If the function must have a body, use `required`.
#[macro_export]
macro_rules! parse {
(forbidden: $body:expr) => {
if $body.is_some() {
err!("unexpected body");
}
};
(optional: $body:expr, $ctx:expr) => {
if let Some(body) = $body {
Some($crate::parsing::parse(body, $ctx)?)
} else {
None
}
};
(required: $body:expr, $ctx:expr) => {
if let Some(body) = $body {
$crate::parsing::parse(body, $ctx)?
} else {
err!("expected body");
}
}
}
/// Return a formatted parsing error.
#[macro_export]
macro_rules! err {
($($tts:tt)*) => {
return Err(ParseError::new(format!($($tts)*)));
};
}
/// Convenient interface for parsing function arguments.
pub struct Arguments<'a> {
args: Peekable<Iter<'a, Expression>>,
}
impl<'a> Arguments<'a> {
pub fn new(header: &'a FuncHeader) -> Arguments<'a> {
Arguments {
args: header.args.iter().peekable()
}
}
pub fn get_expr(&mut self) -> ParseResult<&'a 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_if_present(&mut self) -> ParseResult<Option<&'a str>> {
if self.args.peek().is_some() {
self.get_ident().map(|s| Some(s))
} else {
Ok(None)
}
}
pub fn done(&mut self) -> ParseResult<()> {
if self.args.peek().is_none() {
Ok(())
} else {
Err(ParseError::new("unexpected argument"))
}
}
}
|