oak_valkyrie/ast/pattern_nodes.rs
1use super::{Expr, Identifier, NamePath, Span};
2
3/// A match arm
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct MatchArm {
7 /// The pattern to match against.
8 pub pattern: Pattern,
9 /// Optional guard expression.
10 pub guard: Option<Expr>,
11 /// The body expression of the arm.
12 pub body: Expr,
13 /// The source code span.
14 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
15 pub span: Span,
16}
17
18/// A pattern for matching
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub enum Pattern {
22 /// A wildcard pattern that matches anything.
23 Wildcard {
24 /// The source code span.
25 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
26 span: Span,
27 },
28 /// A variable pattern that binds the matched value.
29 Variable {
30 /// The variable name.
31 name: Identifier,
32 /// The source code span.
33 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
34 span: Span,
35 },
36 /// A literal pattern.
37 Literal {
38 /// The literal value as a string.
39 value: String,
40 /// The source code span.
41 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
42 span: Span,
43 },
44 /// A type pattern for matching types.
45 Type {
46 /// The type name path.
47 name: NamePath,
48 /// The source code span.
49 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
50 span: Span,
51 },
52 /// A class pattern for destructuring.
53 ///
54 /// ```v
55 /// let Point { x, y } = p // shorthand syntax
56 /// let Point { x: a, y: b } = p // explicit binding
57 /// let Point { x, y: new_y } = p // mixed syntax
58 /// ```
59 Class {
60 /// The class name path.
61 name: NamePath,
62 /// The field patterns. None for shorthand syntax.
63 fields: Vec<(Identifier, Option<Pattern>)>,
64 /// The source code span.
65 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
66 span: Span,
67 },
68 /// An else pattern (catch-all).
69 Else {
70 /// The source code span.
71 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
72 span: Span,
73 },
74}