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
239 lines (209 loc) · 7.91 KB
/
Copy pathattr.rs
File metadata and controls
239 lines (209 loc) · 7.91 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::convert::Infallible;
use unwrap_infallible::UnwrapInfallible;
use super::path::PathScope;
use super::{
Checkpoint, ErrProof, Parser, Recovery, define_scope, parse_list, token_stream::TokenStream,
};
use crate::{ExpectedKind, SyntaxKind};
pub(super) fn parse_attr_list<S: TokenStream>(
parser: &mut Parser<S>,
) -> Result<Option<Checkpoint>, Recovery<ErrProof>> {
let lookahead = parser.peek_n_non_trivia(2);
if matches!(
lookahead.as_slice(),
[SyntaxKind::Pound, SyntaxKind::LBracket]
) || parser.current_kind() == Some(SyntaxKind::DocComment)
{
parser.parse_cp(AttrListScope::default(), None).map(Some)
} else {
Ok(None)
}
}
pub(super) fn parse_inner_attr_list<S: TokenStream>(
parser: &mut Parser<S>,
) -> Result<Option<Checkpoint>, Recovery<ErrProof>> {
if matches!(
parser.peek_n_non_trivia(3).as_slice(),
[SyntaxKind::Pound, SyntaxKind::Not, SyntaxKind::LBracket]
) {
parser
.parse_cp(InnerAttrListScope::default(), None)
.map(Some)
} else {
Ok(None)
}
}
define_scope! { pub(crate) AttrListScope, AttrList, (Newline) }
impl super::Parse for AttrListScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
loop {
parser.set_newline_as_trivia(true);
match parser.current_kind() {
Some(SyntaxKind::Pound) => {
parser.parse(AttrScope::default())?;
}
Some(SyntaxKind::DocComment) => parser
.parse(DocCommentAttrScope::default())
.unwrap_infallible(),
_ => break,
};
parser.set_newline_as_trivia(false);
if parser.find(
SyntaxKind::Newline,
ExpectedKind::Separator {
separator: SyntaxKind::Newline,
element: SyntaxKind::Attr,
},
)? {
parser.bump();
}
}
Ok(())
}
}
define_scope! { pub(crate) InnerAttrListScope, AttrList, (Newline) }
impl super::Parse for InnerAttrListScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
loop {
parser.set_newline_as_trivia(true);
if !matches!(
parser.peek_n_non_trivia(3).as_slice(),
[SyntaxKind::Pound, SyntaxKind::Not, SyntaxKind::LBracket]
) {
break;
}
parser.parse(InnerAttrScope::default())?;
parser.set_newline_as_trivia(false);
if parser.find(
SyntaxKind::Newline,
ExpectedKind::Separator {
separator: SyntaxKind::Newline,
element: SyntaxKind::Attr,
},
)? {
parser.bump();
}
}
Ok(())
}
}
define_scope! { AttrScope, Attr, (RBracket) }
impl super::Parse for AttrScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::Pound);
// Expect the opening bracket for a Rust-style outer attribute: #[ ... ]
parser.bump_or_recover(SyntaxKind::LBracket, "expected `[` after `#`")?;
// Parse the attribute path (e.g., foo, foo::bar). Recover on failure.
parser.parse_or_recover(PathScope::default())?;
// After the path, support either a meta list `(...)` or a name-value `= <expr>`.
match parser.current_kind() {
Some(SyntaxKind::LParen) => {
parser.parse(AttrArgListScope::default())?;
}
Some(SyntaxKind::Eq) => {
// Bump '=' then parse an expression value (e.g. `#[selector = sol("...")]`).
parser.bump();
parser.parse(AttrValueExprScope::default())?;
}
_ => {}
}
// Expect the closing bracket of the attribute.
parser.bump_or_recover(SyntaxKind::RBracket, "expected `]` to close attribute")?;
Ok(())
}
}
define_scope! { InnerAttrScope, Attr, (RBracket) }
impl super::Parse for InnerAttrScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::Pound);
parser.bump_or_recover(SyntaxKind::Not, "expected `!` after `#` in inner attribute")?;
parser.bump_or_recover(SyntaxKind::LBracket, "expected `[` after `#!`")?;
parser.parse_or_recover(PathScope::default())?;
match parser.current_kind() {
Some(SyntaxKind::LParen) => {
parser.parse(AttrArgListScope::default())?;
}
Some(SyntaxKind::Eq) => {
parser.bump();
parser.parse(AttrValueExprScope::default())?;
}
_ => {}
}
parser.bump_or_recover(SyntaxKind::RBracket, "expected `]` to close attribute")?;
Ok(())
}
}
define_scope! { AttrArgListScope, AttrArgList, (Comma, RParen) }
impl super::Parse for AttrArgListScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parse_list(
parser,
false,
SyntaxKind::AttrArgList,
(SyntaxKind::LParen, SyntaxKind::RParen),
|parser| parser.parse(AttrArgScope::default()),
)
}
}
define_scope! { AttrArgScope, AttrArg }
impl super::Parse for AttrArgScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
// Parse the key as a path
parser.set_scope_recovery_stack(&[SyntaxKind::Ident, SyntaxKind::Eq]);
// TODO: this should be a "SimplePath" that doesn't allow generic args
parser.parse_or_recover(PathScope::default())?;
// Optional `= value`
if parser.current_kind() == Some(SyntaxKind::Eq) {
parser.bump();
parser.parse(AttrArgValueScope::default())?;
}
Ok(())
}
}
define_scope! { AttrArgValueScope, AttrArgValue }
impl super::Parse for AttrArgValueScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
use crate::parser::lit::{LitScope, is_lit};
match parser.current_kind() {
Some(kind) if is_lit(kind) => {
// Parse a literal as a nested `Lit` node under `AttrArgValue`.
parser.parse(LitScope::default()).unwrap_infallible();
Ok(())
}
Some(SyntaxKind::Ident) => {
parser.bump();
Ok(())
}
_ => parser.error_and_recover("attribute value must be an ident or literal value"),
}
}
}
// Parses an expression value for the `#[attr = <expr>]` form.
//
// This is distinct from `AttrArgValueScope` (used in `#[attr(key = value)]`),
// which intentionally keeps values restricted to ident/literal for now.
define_scope! { AttrValueExprScope, AttrArgValue }
impl super::Parse for AttrValueExprScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
super::expr::parse_expr(parser)
}
}
define_scope! { DocCommentAttrScope, DocCommentAttr }
impl super::Parse for DocCommentAttrScope {
type Error = Infallible;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::DocComment);
parser.bump_if(SyntaxKind::Newline);
Ok(())
}
}