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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! If expression parsing for Nash.
//!
//! Ported from Elm's `Parse/Expression.hs` (if_, chompIfEnd).
//!
//! Parses: `if cond then branch else branch`
//! Also handles: `if c1 then b1 else if c2 then b2 else b3`
use nash_region::{Located, Position, Region};
use nash_source::{Expr, IfBranch};
use crate::Parser;
use crate::error::{self, If};
impl<'a> Parser<'a> {
/// Parse an if expression: `if cond then branch else branch`.
///
/// Mirrors Elm's `if_`:
/// ```haskell
/// if_ start =
/// inContext E.If (Keyword.if_ E.Start) $
/// chompIfEnd start []
/// ```
pub(crate) fn if_(
&mut self,
start: Position,
) -> Result<(&'a Located<Expr<'a>>, Position), error::Expr<'a>> {
self.in_context(
|bump, e, row, col| error::Expr::If(bump.alloc(e), row, col),
|p| p.keyword_if(error::Expr::Start),
|p| p.chomp_if_end(start, vec![]),
)
}
/// Parse the body of an if expression after the initial `if` keyword.
///
/// Mirrors Elm's `chompIfEnd`:
/// ```haskell
/// chompIfEnd start branches =
/// do Space.chompAndCheckIndent E.IfSpace E.IfIndentCondition
/// (condition, condEnd) <- specialize E.IfCondition expression
/// Space.checkIndent condEnd E.IfIndentThen
/// Keyword.then_ E.IfThen
/// Space.chompAndCheckIndent E.IfSpace E.IfIndentThenBranch
/// (thenBranch, thenEnd) <- specialize E.IfThenBranch expression
/// Space.checkIndent thenEnd E.IfIndentElse
/// Keyword.else_ E.IfElse
/// Space.chompAndCheckIndent E.IfSpace E.IfIndentElseBranch
/// let newBranches = (condition, thenBranch) : branches
/// oneOf E.IfElseBranchStart
/// [ do Keyword.if_ E.IfElseBranchStart
/// chompIfEnd start newBranches
/// , do (elseBranch, elseEnd) <- specialize E.IfElseBranch expression
/// let ifExpr = Src.If (reverse newBranches) elseBranch
/// return (A.at start elseEnd ifExpr, elseEnd)
/// ]
/// ```
fn chomp_if_end(
&mut self,
start: Position,
mut branches: Vec<&'a IfBranch<'a>>,
) -> Result<(&'a Located<Expr<'a>>, Position), If<'a>> {
loop {
// Parse condition
self.chomp_and_check_indent(If::Space, If::IndentCondition)?;
let (condition, cond_end) = self.if_condition()?;
// Parse `then`
self.check_indent(cond_end.line, cond_end.column, If::IndentThen)?;
self.keyword_then(If::Then)?;
// Parse then branch
self.chomp_and_check_indent(If::Space, If::IndentThenBranch)?;
let (then_branch, then_end) = self.if_then_branch()?;
// Parse `else`
self.check_indent(then_end.line, then_end.column, If::IndentElse)?;
self.keyword_else(If::Else)?;
// Create the new branch
let branch = self.bump.alloc(IfBranch {
condition,
then_branch,
});
branches.push(branch);
// Parse else branch: either `else if ...` or final else expression
self.chomp_and_check_indent(If::Space, If::IndentElseBranch)?;
let final_else = self.one_of(
If::ElseBranchStart,
vec![
Box::new(|p: &mut Parser<'a>| {
p.keyword_if(If::ElseBranchStart)?;
Ok(None)
}),
Box::new(|p: &mut Parser<'a>| p.if_else_branch().map(Some)),
],
)?;
if let Some((else_branch, else_end)) = final_else {
let if_expr = Expr::If {
branches: self.bump.alloc_slice_copy(&branches),
final_else: else_branch,
};
return Ok((
self.alloc(Located::at(Region::new(start, else_end), if_expr)),
else_end,
));
}
}
}
/// Parse condition expression in an if.
fn if_condition(&mut self) -> Result<(&'a Located<Expr<'a>>, Position), If<'a>> {
self.specialize(
|bump, e, r, c| If::Condition(bump.alloc(e), r, c),
|p| p.expression(),
)
}
/// Parse then branch expression in an if.
fn if_then_branch(&mut self) -> Result<(&'a Located<Expr<'a>>, Position), If<'a>> {
self.specialize(
|bump, e, r, c| If::ThenBranch(bump.alloc(e), r, c),
|p| p.expression(),
)
}
/// Parse else branch expression in an if.
fn if_else_branch(&mut self) -> Result<(&'a Located<Expr<'a>>, Position), If<'a>> {
self.specialize(
|bump, e, r, c| If::ElseBranch(bump.alloc(e), r, c),
|p| p.expression(),
)
}
}
#[cfg(test)]
mod tests {
use crate::expression::{assert_expression_snapshot, assert_indented_expression_snapshot};
#[test]
fn if_simple() {
assert_expression_snapshot!("if True then 1 else 2");
}
#[test]
fn if_with_vars() {
assert_expression_snapshot!("if x then y else z");
}
#[test]
fn if_else_if() {
assert_expression_snapshot!("if a then 1 else if b then 2 else 3");
}
#[test]
fn if_multiline() {
assert_indented_expression_snapshot!(
r#"
if condition then
trueBranch
else
falseBranch
"#
);
}
#[test]
fn if_nested_condition() {
assert_expression_snapshot!("if f x then 1 else 2");
}
#[test]
fn if_nested_branches() {
assert_expression_snapshot!("if a then f x else g y");
}
#[test]
fn if_else_if_multiline() {
assert_indented_expression_snapshot!(
r#"
if a then
1
else if b then
2
else
3
"#
);
}
}