Skip to main content

java_lang/ast/
pat.rs

1//! Pattern types.
2
3use crate::{ident::Ident, span::Span};
4
5use super::{attribute::Annotation, expr::Expr, ty::Type};
6
7/// A Java pattern (used in instanceof, switch, etc.).
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub enum Pattern {
10    TypePattern(TypePattern),
11    RecordPattern(RecordPattern),
12}
13
14impl Pattern {
15    pub fn span(&self) -> Span {
16        match self {
17            Self::TypePattern(p) => p.span,
18            Self::RecordPattern(p) => p.span,
19        }
20    }
21}
22
23/// A type pattern: `Type name` or just `Type`.
24#[derive(Debug, Clone, PartialEq, Eq, Hash)]
25pub struct TypePattern {
26    pub annotations: Vec<Annotation>,
27    pub ty: Type,
28    pub name: Option<Ident>,
29    pub span: Span,
30}
31
32/// A record pattern: `Type(comp1, comp2, ...)`.
33#[derive(Debug, Clone, PartialEq, Eq, Hash)]
34pub struct RecordPattern {
35    pub record_type: Type,
36    pub components: Vec<Pattern>,
37    pub span: Span,
38}
39
40/// A guard in a switch case pattern: `when expr`.
41#[derive(Debug, Clone, PartialEq, Eq, Hash)]
42pub struct Guard {
43    pub when_span: Span,
44    pub expr: Expr,
45}