forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.rs
79 lines (75 loc) · 2.45 KB
/
pattern.rs
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
77
78
79
use crate::priv_prelude::*;
#[derive(Clone, Debug, Serialize)]
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>>),
// to handle parser recovery: Error represents an incomplete Constructor
Error(Box<[Span]>),
}
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(),
Pattern::Error(spans) => spans.iter().cloned().reduce(Span::join).unwrap(),
}
}
}
#[derive(Clone, Debug, Serialize)]
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(),
},
}
}
}