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
//! Expression parsing: `match` patterns.
use super::*;
impl<'a> ExprParser<'a> {
/// Reads a `match` pattern (up to before `=>`).
pub fn parse_pattern(&mut self) -> PResult<Pattern> {
self.skip_ws()?;
let pos = self.here();
match self.peek() {
None => unexpected_eol(pos),
Some('"') => match self.parse_string()? {
Expr::Str(parts, p) => match parts.as_slice() {
[StrPart::Text(t, _)] => Ok(Pattern::Str(t.clone(), p)),
_ => err(DiagCode::NotConstant, vec![], p),
},
_ => unreachable!("文字列リテラルを読んだ"),
},
Some('-') => {
self.bump();
match self.parse_number()? {
Expr::Int(n, _) => match n.checked_neg() {
Some(n) => Ok(Pattern::Int(n, pos)),
None => err(
DiagCode::InvalidNumber,
vec![("text", DiagArg::Str(format!("-{n}")))],
pos,
),
},
_ => unexpected("-", pos),
}
}
Some(c) if c.is_ascii_digit() => {
let start = self.pos;
match self.parse_number()? {
Expr::Int(n, _) => Ok(Pattern::Int(n, pos)),
// `float` cannot be a pattern (compare with `==`)
_ => unexpected(&self.line.text[start..self.pos], pos),
}
}
Some(c) if is_ident_start(c) => {
let (name, p) = self.ident()?.expect("識別子の始まりを確かめた");
match name.as_str() {
"_" => Ok(Pattern::Wild(p)),
"true" => Ok(Pattern::Bool(true, p)),
"false" => Ok(Pattern::Bool(false, p)),
"None" => Ok(Pattern::None(p)),
"Some" => {
let binds = self.parse_binders()?;
match <[_; 1]>::try_from(binds) {
Ok([b]) => Ok(Pattern::Some(b, p)),
Err(_) => unexpected("Some", p),
}
}
n if is_reserved(n) => unexpected(n, p),
_ => {
let binds = if self.peek() == Some('(') {
self.parse_binders()?
} else {
Vec::new()
};
Ok(Pattern::Variant {
name,
binds,
pos: p,
})
}
}
}
Some(c) => unexpected(&c.to_string(), pos),
}
}
/// Reads `(name, ...)` (`_` is the mark to discard). The `(` is at the current position.
pub(super) fn parse_binders(&mut self) -> PResult<Vec<Option<(String, Pos)>>> {
self.expect_char('(')?;
let mut out = Vec::new();
loop {
self.skip_ws()?;
let at = self.here();
let Some((n, p)) = self.ident()? else {
return unexpected_char(self.peek(), at);
};
if n == "_" {
out.push(None);
} else if is_reserved(&n) {
return unexpected(&n, p);
} else {
out.push(Some((n, p)));
}
self.skip_ws()?;
if self.peek() == Some(',') {
self.bump();
} else {
break;
}
}
self.expect_char(')')?;
Ok(out)
}
}