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
75
76
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
pub enum Pattern {
    Wildcard {
        underscore_token: UnderscoreToken,
    },
    Var {
        reference: Option<RefToken>,
        mutable: Option<MutToken>,
        name: Ident,
    },
    Literal(Literal),
    Constant(PathExpr),
    Constructor {
        path: PathExpr,
        args: Parens<Punctuated<Pattern, CommaToken>>,
    },
    Struct {
        path: PathExpr,
        fields: Braces<Punctuated<PatternStructField, CommaToken>>,
    },
    Tuple(Parens<Punctuated<Pattern, CommaToken>>),
}

impl Spanned for Pattern {
    fn span(&self) -> Span {
        match self {
            Pattern::Wildcard { underscore_token } => underscore_token.span(),
            Pattern::Var {
                reference,
                mutable,
                name,
            } => match (reference, mutable) {
                (Some(ref_token), Some(mut_token)) => {
                    Span::join(Span::join(ref_token.span(), mut_token.span()), name.span())
                }
                (Some(ref_token), None) => Span::join(ref_token.span(), name.span()),
                (None, Some(mut_token)) => Span::join(mut_token.span(), name.span()),
                (None, None) => name.span(),
            },
            Pattern::Literal(literal) => literal.span(),
            Pattern::Constant(path_expr) => path_expr.span(),
            Pattern::Constructor { path, args } => Span::join(path.span(), args.span()),
            Pattern::Struct { path, fields } => Span::join(path.span(), fields.span()),
            Pattern::Tuple(pat_tuple) => pat_tuple.span(),
        }
    }
}

#[derive(Clone, Debug)]
pub enum PatternStructField {
    Rest {
        token: DoubleDotToken,
    },
    Field {
        field_name: Ident,
        pattern_opt: Option<(ColonToken, Box<Pattern>)>,
    },
}

impl Spanned for PatternStructField {
    fn span(&self) -> Span {
        use PatternStructField::*;
        match &self {
            Rest { token } => token.span(),
            Field {
                field_name,
                pattern_opt,
            } => match pattern_opt {
                Some((_colon_token, pattern)) => Span::join(field_name.span(), pattern.span()),
                None => field_name.span(),
            },
        }
    }
}