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 /// `[1, let second, .., let last]` (C#'s list pattern): an array's, a
35 /// string's or a list's items, a `..` among them at most once.
36 List(Vec<Pattern>, Span),
37 /// `..` in a list pattern: the items it doesn't name, `.. let rest`
38 /// matching them as a slice.
39 Slice(Option<Box<Pattern>>),
40 /// `{ Health: < 5 }`, `Monster { Health: < 5 }`.
41 Property {
42 ty: Option<Type>,
43 fields: Vec<(Name, Pattern)>,
44 },
45 Or(Box<Pattern>, Box<Pattern>),
46 And(Box<Pattern>, Box<Pattern>),
47 Not(Box<Pattern>),
48}
49
50#[derive(Debug, Clone, PartialEq)]
51pub struct Type {
52 pub kind: TypeKind,
53 pub span: Span,
54}
55
56#[derive(Debug, Clone, PartialEq)]
57pub enum TypeKind {
58 /// `i32`, `List<T>`, `Mode.Charging`.
59 Path {
60 path: Vec<Name>,
61 args: Vec<Type>,
62 },
63 /// `(i32 X, i32 Y)`.
64 Tuple(Vec<(Type, Option<Name>)>),
65 Nullable(Box<Type>),
66 /// `T[]`, `T[,]` (`rank` 2), `T[][]` (an array of `T[]`).
67 Array {
68 item: Box<Type>,
69 rank: usize,
70 },
71 /// `Charge & Owned`.
72 Both(Vec<Type>),
73 /// `in 'a T`, `ref 'a T`: a borrow (Rust's `&'a T`, `&'a mut T`).
74 Borrow {
75 mutable: bool,
76 lifetime: Option<Name>,
77 ty: Box<Type>,
78 },
79 /// `'a` among a type's arguments (`Cursor<'a>`).
80 Lifetime(Name),
81}