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
use super::{Expr, Identifier, NamePath, Span};
/// A match arm
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MatchArm {
/// The pattern to match against.
pub pattern: Pattern,
/// Optional guard expression.
pub guard: Option<Expr>,
/// The body expression of the arm.
pub body: Expr,
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
pub span: Span,
}
/// A pattern for matching
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Pattern {
/// A wildcard pattern that matches anything.
Wildcard {
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
span: Span,
},
/// A variable pattern that binds the matched value.
Variable {
/// The variable name.
name: Identifier,
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
span: Span,
},
/// A literal pattern.
Literal {
/// The literal value as a string.
value: String,
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
span: Span,
},
/// A type pattern for matching types.
Type {
/// The type name path.
name: NamePath,
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
span: Span,
},
/// A class pattern for destructuring.
///
/// ```v
/// let Point { x, y } = p // shorthand syntax
/// let Point { x: a, y: b } = p // explicit binding
/// let Point { x, y: new_y } = p // mixed syntax
/// ```
Class {
/// The class name path.
name: NamePath,
/// The field patterns. None for shorthand syntax.
fields: Vec<(Identifier, Option<Pattern>)>,
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
span: Span,
},
/// An else pattern (catch-all).
Else {
/// The source code span.
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
span: Span,
},
}