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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Lambda expression parsing for Nash.
//!
//! Ported from Elm's `Parse/Expression.hs` (function, chompArgs).
//!
//! Parses: `\arg1 arg2 -> body`
use bumpalo::collections::Vec as BumpVec;
use nash_region::{Located, Position, Region};
use nash_source::Expr;
use crate::Parser;
use crate::error::{self, Func};
impl<'a> Parser<'a> {
/// Parse a lambda expression: `\arg1 arg2 -> body`.
///
/// Mirrors Elm's `function`:
/// ```haskell
/// function start =
/// inContext E.Func (word1 0x5C {-\-} E.Start) $
/// do Space.chompAndCheckIndent E.FuncSpace E.FuncIndentArg
/// arg <- specialize E.FuncArg Pattern.term
/// Space.chompAndCheckIndent E.FuncSpace E.FuncIndentArrow
/// revArgs <- chompArgs [arg]
/// Space.chompAndCheckIndent E.FuncSpace E.FuncIndentBody
/// (body, end) <- specialize E.FuncBody expression
/// let funcExpr = Src.Lambda (reverse revArgs) body
/// return (A.at start end funcExpr, end)
/// ```
pub(crate) fn lambda(
&mut self,
start: Position,
) -> Result<(&'a Located<Expr<'a>>, Position), error::Expr<'a>> {
self.in_context(
|bump, e, row, col| error::Expr::Func(bump.alloc(e), row, col),
|p| p.word1(b'\\', error::Expr::Start),
|p| {
p.chomp_and_check_indent(Func::Space, Func::IndentArg)?;
let arg = p.specialize(
|bump, e, r, c| Func::Arg(bump.alloc(e), r, c),
|p| p.pattern_term(),
)?;
p.chomp_and_check_indent(Func::Space, Func::IndentArrow)?;
let rev_args = p.chomp_lambda_args(vec![arg])?;
p.chomp_and_check_indent(Func::Space, Func::IndentBody)?;
let (body, end) = p.specialize(
|bump, e, r, c| Func::Body(bump.alloc(e), r, c),
|p| p.expression(),
)?;
// Convert to bump slice (already in correct order)
let mut params: BumpVec<'a, &'a Located<_>> = BumpVec::new_in(p.bump);
for arg in rev_args {
params.push(arg);
}
let params_slice = params.into_bump_slice();
let lambda = Expr::Lambda {
parameters: params_slice,
body,
};
Ok((p.alloc(Located::at(Region::new(start, end), lambda)), end))
},
)
}
/// Chomp additional lambda arguments until we hit `->`.
///
/// Mirrors Elm's `chompArgs`:
/// ```haskell
/// chompArgs revArgs =
/// oneOf E.FuncArrow
/// [ do arg <- specialize E.FuncArg Pattern.term
/// Space.chompAndCheckIndent E.FuncSpace E.FuncIndentArrow
/// chompArgs (arg:revArgs)
/// , do word2 0x2D 0x3E {-->-} E.FuncArrow
/// return revArgs
/// ]
/// ```
///
/// Note: Elm uses `:` (prepend) to build the list in reverse order, then
/// reverses at the end. We use `push` (append) to build in forward order,
/// so no reversal is needed.
fn chomp_lambda_args(
&mut self,
mut args: Vec<&'a Located<nash_source::Pattern<'a>>>,
) -> Result<Vec<&'a Located<nash_source::Pattern<'a>>>, Func<'a>> {
loop {
let next = self.one_of(
Func::Arrow,
vec![
Box::new(|p: &mut Parser<'a>| {
let arg = p.specialize(
|bump, e, r, c| Func::Arg(bump.alloc(e), r, c),
|p| p.pattern_term(),
)?;
p.chomp_and_check_indent(Func::Space, Func::IndentArrow)?;
Ok(Some(arg))
}),
Box::new(|p: &mut Parser<'a>| {
p.word2(b'-', b'>', Func::Arrow)?;
Ok(None)
}),
],
)?;
match next {
Some(arg) => args.push(arg),
None => return Ok(args),
}
}
}
}
#[cfg(test)]
mod tests {
use crate::expression::assert_expression_snapshot;
#[test]
fn lambda_single_arg() {
assert_expression_snapshot!(r"\x -> x");
}
#[test]
fn lambda_multiple_args() {
assert_expression_snapshot!(r"\x y -> x");
}
#[test]
fn lambda_pattern_arg() {
assert_expression_snapshot!(r"\(a, b) -> a");
}
#[test]
fn lambda_wildcard() {
assert_expression_snapshot!(r"\_ -> 42");
}
#[test]
fn lambda_with_body() {
assert_expression_snapshot!(r"\x -> f x");
}
}