forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattr.rs
More file actions
124 lines (112 loc) · 3.47 KB
/
Copy pathattr.rs
File metadata and controls
124 lines (112 loc) · 3.47 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use super::{define_scope, token_stream::TokenStream, Checkpoint, Parser};
use crate::SyntaxKind;
pub(super) fn parse_attr_list<S: TokenStream>(parser: &mut Parser<S>) -> Option<Checkpoint> {
if let Some(SyntaxKind::DocComment) | Some(SyntaxKind::Pound) = parser.current_kind() {
Some(parser.parse(super::attr::AttrListScope::default(), None).1)
} else {
None
}
}
define_scope! {
pub(crate) AttrListScope,
AttrList,
Override(
Newline
)
}
impl super::Parse for AttrListScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
loop {
parser.set_newline_as_trivia(true);
match parser.current_kind() {
Some(SyntaxKind::Pound) => parser.parse(AttrScope::default(), None),
Some(SyntaxKind::DocComment) => parser.parse(DocCommentAttrScope::default(), None),
_ => break,
};
parser.set_newline_as_trivia(false);
parser.bump_or_recover(
SyntaxKind::Newline,
"expected newline after Attribute",
None,
)
}
}
}
define_scope! {
AttrScope,
Attr,
Inheritance
}
impl super::Parse for AttrScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::Pound);
parser.with_recovery_tokens(
|parser| {
parser.bump_or_recover(SyntaxKind::Ident, "expected attribute name", None);
},
&[SyntaxKind::LParen],
);
if parser.current_kind() == Some(SyntaxKind::LParen) {
parser.parse(AttrArgListScope::default(), None);
}
}
}
define_scope! {
AttrArgListScope,
AttrArgList,
Override(
RParen
)
}
impl super::Parse for AttrArgListScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.bump_expected(SyntaxKind::LParen);
if parser.bump_if(SyntaxKind::RParen) {
return;
}
parser.with_next_expected_tokens(
|parser| parser.parse(AttrArgScope::default(), None),
&[SyntaxKind::Comma, SyntaxKind::RParen],
);
while parser.bump_if(SyntaxKind::Comma) {
parser.with_next_expected_tokens(
|parser| parser.parse(AttrArgScope::default(), None),
&[SyntaxKind::Comma, SyntaxKind::RParen],
);
}
parser.bump_or_recover(SyntaxKind::RParen, "expected `)`", None);
}
}
define_scope! {
AttrArgScope,
AttrArg,
Override(
Comma,
RParen
)
}
impl super::Parse for AttrArgScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.with_next_expected_tokens(
|parser| parser.bump_or_recover(SyntaxKind::Ident, "Expected `key: value`", None),
&[SyntaxKind::Colon],
);
parser.with_next_expected_tokens(
|parser| parser.bump_or_recover(SyntaxKind::Colon, "Expected `key: value`", None),
&[SyntaxKind::Ident],
);
parser.bump_or_recover(SyntaxKind::Ident, "Expected `key: value`", None);
}
}
define_scope! {
DocCommentAttrScope,
DocCommentAttr,
Inheritance
}
impl super::Parse for DocCommentAttrScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.bump_expected(SyntaxKind::DocComment);
parser.bump_if(SyntaxKind::Newline);
}
}