Skip to main content

yog_script_analyze/ast/
pattern.rs

1//! Patterns (`ys-grammar.md` §3.3) and types (§4).
2
3use super::{BinOp, Expr, Name};
4use crate::source::token::Span;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum Pattern {
8    Wild(Span),
9    /// A literal or a path to a value or a case (`0`, `Mode.Idle`); a
10    /// lone path may be a type too: the resolver and the checker tell.
11    Value(Expr),
12    /// `let e`.
13    Let(Name),
14    /// `Monster m`, `Monster`.
15    Type {
16        ty: Type,
17        bind: Option<Name>,
18    },
19    /// `Mode.Charging(let rate)`.
20    Variant {
21        path: Vec<Name>,
22        args: Vec<Pattern>,
23    },
24    /// `< 5`.
25    Relational {
26        op: BinOp,
27        value: Expr,
28    },
29    Range {
30        from: Expr,
31        to: Expr,
32    },
33    Tuple(Vec<Pattern>),
34    /// `{ Health: < 5 }`, `Monster { Health: < 5 }`.
35    Property {
36        ty: Option<Type>,
37        fields: Vec<(Name, Pattern)>,
38    },
39    Or(Box<Pattern>, Box<Pattern>),
40    And(Box<Pattern>, Box<Pattern>),
41    Not(Box<Pattern>),
42}
43
44#[derive(Debug, Clone, PartialEq)]
45pub struct Type {
46    pub kind: TypeKind,
47    pub span: Span,
48}
49
50#[derive(Debug, Clone, PartialEq)]
51pub enum TypeKind {
52    /// `i32`, `List<T>`, `Mode.Charging`.
53    Path {
54        path: Vec<Name>,
55        args: Vec<Type>,
56    },
57    /// `(i32 X, i32 Y)`.
58    Tuple(Vec<(Type, Option<Name>)>),
59    Nullable(Box<Type>),
60    /// `Charge & Owned`.
61    Both(Vec<Type>),
62    /// `in 'a T`, `ref 'a T`: a borrow (Rust's `&'a T`, `&'a mut T`).
63    Borrow {
64        mutable: bool,
65        lifetime: Option<Name>,
66        ty: Box<Type>,
67    },
68    /// `'a` among a type's arguments (`Cursor<'a>`).
69    Lifetime(Name),
70}