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
192
193
194
195
196
197
198
199
200
201
//! Case expression parsing for Nash.
//!
//! Ported from Elm's `Parse/Expression.hs` (case_, chompBranch, chompCaseEnd).
use nash_region::{Located, Position};
use nash_source::{CaseArm, Expr};
use crate::Parser;
use crate::error::{self, Case};
impl<'a> Parser<'a> {
/// Parse a case expression.
///
/// Mirrors Elm's `case_`:
/// ```haskell
/// case_ start =
/// inContext E.Case (Keyword.case_ E.Start) $
/// do Space.chompAndCheckIndent E.CaseSpace E.CaseIndentExpr
/// (expr, exprEnd) <- specialize E.CaseExpr expression
/// Space.checkIndent exprEnd E.CaseIndentOf
/// Keyword.of_ E.CaseOf
/// Space.chompAndCheckIndent E.CaseSpace E.CaseIndentPattern
/// withIndent $
/// do (firstBranch, firstEnd) <- chompBranch
/// (branches, end) <- chompCaseEnd [firstBranch] firstEnd
/// return (A.at start end (Src.Case expr branches), end)
/// ```
pub(crate) fn case_(
&mut self,
start: Position,
) -> Result<(&'a Located<Expr<'a>>, Position), error::Expr<'a>> {
self.in_context(
|bump, e, row, col| error::Expr::Case(bump.alloc(e), row, col),
|p| p.keyword_case(error::Expr::Start),
|p| {
// Chomp whitespace and check indent for scrutinee expression
p.chomp_and_check_indent(Case::Space, Case::IndentExpr)?;
// Parse the scrutinee expression
let (scrutinee, scrutinee_end) = p.specialize(
|bump, e, row, col| Case::Expr(bump.alloc(e), row, col),
|p| p.expression(),
)?;
// Check indent for "of" keyword
p.check_indent(scrutinee_end.line, scrutinee_end.column, Case::IndentOf)?;
// Parse "of" keyword
p.keyword_of(Case::Of)?;
// Chomp whitespace and check indent for first pattern
p.chomp_and_check_indent(Case::Space, Case::IndentPattern)?;
// Parse branches with proper indentation
p.with_indent(|p| {
// Parse first branch
let (first_arm, first_end) = p.chomp_case_branch()?;
// Parse remaining branches
let (arms, end) = p.chomp_case_end(vec![first_arm], first_end)?;
// Build the case expression
let arms_slice = p.alloc_slice_copy(&arms);
let case_expr = Expr::Case {
scrutinee,
arms: arms_slice,
};
Ok((p.add_end(start, case_expr), end))
})
},
)
}
/// Parse a single case branch (pattern -> expression).
///
/// Mirrors Elm's `chompBranch`:
/// ```haskell
/// chompBranch =
/// do (pattern, patternEnd) <- specialize E.CasePattern Pattern.expression
/// Space.checkIndent patternEnd E.CaseIndentArrow
/// word2 0x2D 0x3E {-->-} E.CaseArrow
/// Space.chompAndCheckIndent E.CaseSpace E.CaseIndentBranch
/// (branchExpr, end) <- specialize E.CaseBranch expression
/// return ((pattern, branchExpr), end)
/// ```
fn chomp_case_branch(&mut self) -> Result<(&'a CaseArm<'a>, Position), Case<'a>> {
// Parse the pattern
let (pattern, pattern_end) = self.specialize(
|bump, e, row, col| Case::Pattern(bump.alloc(e), row, col),
|p| p.pattern_expr(),
)?;
// Check indent for arrow
self.check_indent(pattern_end.line, pattern_end.column, Case::IndentArrow)?;
// Parse the arrow ->
self.word2(b'-', b'>', Case::Arrow)?;
// Chomp whitespace and check indent for branch body
self.chomp_and_check_indent(Case::Space, Case::IndentBranch)?;
// Parse the branch expression
let (body, end) = self.specialize(
|bump, e, row, col| Case::Branch(bump.alloc(e), row, col),
|p| p.expression(),
)?;
let arm = self.alloc(CaseArm { pattern, body });
Ok((arm, end))
}
/// Parse remaining case branches.
///
/// Mirrors Elm's `chompCaseEnd`:
/// ```haskell
/// chompCaseEnd branches end =
/// oneOfWithFallback
/// [ do Space.checkAligned E.CasePatternAlignment
/// (branch, newEnd) <- chompBranch
/// chompCaseEnd (branch:branches) newEnd
/// ]
/// (reverse branches, end)
/// ```
fn chomp_case_end(
&mut self,
mut arms: Vec<&'a CaseArm<'a>>,
end: Position,
) -> Result<(Vec<&'a CaseArm<'a>>, Position), Case<'a>> {
// Clone for the fallback
let arms_for_fallback = arms.clone();
self.one_of_with_fallback(
vec![Box::new(|p: &mut Parser<'a>| {
// Check alignment for next pattern
p.check_aligned(Case::PatternAlignment)?;
// Parse the next branch
let (arm, new_end) = p.chomp_case_branch()?;
arms.push(arm);
// Continue parsing more branches
p.chomp_case_end(arms, new_end)
})],
(arms_for_fallback, end),
)
}
}
#[cfg(test)]
mod tests {
use crate::expression::assert_expression_snapshot;
#[test]
fn case_simple() {
assert_expression_snapshot!("case x of\n Just y -> y\n Nothing -> 0");
}
#[test]
fn case_single_branch() {
assert_expression_snapshot!("case x of\n _ -> 1");
}
#[test]
fn case_multiple_branches() {
assert_expression_snapshot!(
r#"
case x of
0 -> "zero"
1 -> "one"
_ -> "other"
"#
);
}
#[test]
fn case_with_patterns() {
assert_expression_snapshot!(
r#"
case list of
[] -> 0
[x] -> x
x :: xs -> x
"#
);
}
#[test]
fn case_nested() {
assert_expression_snapshot!(
r#"
case x of
Just y ->
case y of
Just z -> z
Nothing -> 0
Nothing -> 0
"#
);
}
}