1
1
use crate :: ast;
2
- use crate :: { Parse , ParseError , ParseErrorKind , Parser , Peek , Spanned , ToTokens } ;
2
+ use crate :: {
3
+ OptionSpanned as _, Parse , ParseError , ParseErrorKind , Parser , Peek , Spanned , ToTokens ,
4
+ } ;
3
5
use std:: mem:: take;
4
6
use std:: ops;
5
7
@@ -173,28 +175,27 @@ impl Expr {
173
175
/// are arguments to statements immediately followed by blocks. Like `if`,
174
176
/// `while`, and `match`.
175
177
pub ( crate ) fn parse_without_eager_brace ( parser : & mut Parser < ' _ > ) -> Result < Self , ParseError > {
176
- Self :: parse_full ( parser, EagerBrace ( false ) , ExprChain ( true ) , vec ! [ ] )
178
+ Self :: parse_full ( parser, EagerBrace ( false ) , ExprChain ( true ) , & mut vec ! [ ] )
177
179
}
178
180
179
181
/// Full, configurable parsing of an expression.
180
- pub ( crate ) fn parse_full (
182
+ fn parse_full (
181
183
parser : & mut Parser < ' _ > ,
182
184
eager_brace : EagerBrace ,
183
185
expr_chain : ExprChain ,
184
- attributes : Vec < ast:: Attribute > ,
186
+ attributes : & mut Vec < ast:: Attribute > ,
185
187
) -> Result < Self , ParseError > {
186
188
let lhs = Self :: parse_primary ( parser, eager_brace, expr_chain, attributes) ?;
187
189
Ok ( Self :: parse_expr_binary ( parser, lhs, 0 , eager_brace) ?)
188
190
}
189
191
190
192
/// Parse expressions that start with an identifier.
191
- pub ( crate ) fn parse_ident_start (
193
+ pub ( crate ) fn parse_with_path (
192
194
parser : & mut Parser < ' _ > ,
195
+ path : ast:: Path ,
193
196
eager_brace : EagerBrace ,
194
197
attributes : & mut Vec < ast:: Attribute > ,
195
198
) -> Result < Self , ParseError > {
196
- let path = parser. parse :: < ast:: Path > ( ) ?;
197
-
198
199
if * eager_brace && parser. peek :: < ast:: OpenBrace > ( ) ? {
199
200
let ident = ast:: LitObjectIdent :: Named ( path) ;
200
201
@@ -226,7 +227,7 @@ impl Expr {
226
227
}
227
228
228
229
let open = parser. parse :: < ast:: OpenParen > ( ) ?;
229
- let expr = ast:: Expr :: parse_full ( parser, EagerBrace ( true ) , ExprChain ( true ) , vec ! [ ] ) ?;
230
+ let expr = ast:: Expr :: parse_full ( parser, EagerBrace ( true ) , ExprChain ( true ) , & mut vec ! [ ] ) ?;
230
231
231
232
if parser. peek :: < ast:: CloseParen > ( ) ? {
232
233
return Ok ( Expr :: ExprGroup ( ast:: ExprGroup {
@@ -244,19 +245,27 @@ impl Expr {
244
245
} ) )
245
246
}
246
247
247
- pub ( crate ) fn parse_primary_with_attributes (
248
+ pub ( crate ) fn parse_with_meta (
248
249
parser : & mut Parser < ' _ > ,
249
- attributes : Vec < ast:: Attribute > ,
250
+ attributes : & mut Vec < ast:: Attribute > ,
251
+ path : Option < ast:: Path > ,
250
252
) -> Result < Self , ParseError > {
251
- Self :: parse_full ( parser, EagerBrace ( true ) , ExprChain ( true ) , attributes)
253
+ let lhs = if let Some ( path) = path {
254
+ let expr = Self :: parse_with_path ( parser, path, EagerBrace ( true ) , attributes) ?;
255
+ Self :: parse_expr_chain ( parser, expr) ?
256
+ } else {
257
+ Self :: parse_primary ( parser, EagerBrace ( true ) , ExprChain ( true ) , attributes) ?
258
+ } ;
259
+
260
+ Ok ( Self :: parse_expr_binary ( parser, lhs, 0 , EagerBrace ( true ) ) ?)
252
261
}
253
262
254
263
/// Parse a single expression value.
255
264
pub ( crate ) fn parse_primary (
256
265
parser : & mut Parser < ' _ > ,
257
266
eager_brace : EagerBrace ,
258
267
expr_chain : ExprChain ,
259
- mut attributes : Vec < ast:: Attribute > ,
268
+ attributes : & mut Vec < ast:: Attribute > ,
260
269
) -> Result < Self , ParseError > {
261
270
while parser. peek :: < ast:: Attribute > ( ) ? {
262
271
attributes. push ( parser. parse :: < ast:: attribute:: InnerAttribute > ( ) ?. 0 ) ;
@@ -265,25 +274,26 @@ impl Expr {
265
274
let expr = if ast:: Lit :: peek_in_expr ( parser) ? {
266
275
ast:: Expr :: ExprLit ( ast:: ExprLit :: parse_with_attributes (
267
276
parser,
268
- take ( & mut attributes) ,
277
+ take ( attributes) ,
269
278
) ?)
270
279
} else {
271
280
let token = parser. token_peek_eof ( ) ?;
272
281
273
282
match token. kind {
274
283
ast:: Kind :: Async => {
275
284
let async_: ast:: Async = parser. parse ( ) ?;
276
- let expr: Self = Self :: parse_primary ( parser, eager_brace, expr_chain, vec ! [ ] ) ?;
285
+ let expr: Self =
286
+ Self :: parse_primary ( parser, eager_brace, expr_chain, & mut vec ! [ ] ) ?;
277
287
278
288
match expr {
279
289
Self :: ExprClosure ( expr_closure) => Self :: ExprClosure ( ast:: ExprClosure {
280
- attributes : take ( & mut attributes) ,
290
+ attributes : take ( attributes) ,
281
291
async_ : Some ( async_) ,
282
292
args : expr_closure. args ,
283
293
body : expr_closure. body ,
284
294
} ) ,
285
295
Self :: ExprBlock ( expr_block) => Self :: ExprAsync ( ast:: ExprAsync {
286
- attributes : take ( & mut attributes) ,
296
+ attributes : take ( attributes) ,
287
297
async_,
288
298
block : expr_block. block ,
289
299
} ) ,
@@ -296,12 +306,12 @@ impl Expr {
296
306
}
297
307
}
298
308
ast:: Kind :: PipePipe | ast:: Kind :: Pipe => Self :: ExprClosure (
299
- ast:: ExprClosure :: parse_with_attributes ( parser, take ( & mut attributes) ) ?,
309
+ ast:: ExprClosure :: parse_with_attributes ( parser, take ( attributes) ) ?,
300
310
) ,
301
311
ast:: Kind :: Self_ => Self :: Self_ ( parser. parse ( ) ?) ,
302
312
ast:: Kind :: Select => Self :: ExprSelect ( ast:: ExprSelect :: parse_with_attributes (
303
313
parser,
304
- take ( & mut attributes) ,
314
+ take ( attributes) ,
305
315
) ?) ,
306
316
ast:: Kind :: Label ( ..) => {
307
317
let label =
@@ -312,21 +322,21 @@ impl Expr {
312
322
ast:: Kind :: While => {
313
323
Self :: ExprWhile ( ast:: ExprWhile :: parse_with_attributes_and_label (
314
324
parser,
315
- take ( & mut attributes) ,
325
+ take ( attributes) ,
316
326
label,
317
327
) ?)
318
328
}
319
329
ast:: Kind :: Loop => {
320
330
Self :: ExprLoop ( ast:: ExprLoop :: parse_with_attributes_and_label (
321
331
parser,
322
- take ( & mut attributes) ,
332
+ take ( attributes) ,
323
333
label,
324
334
) ?)
325
335
}
326
336
ast:: Kind :: For => {
327
337
Self :: ExprFor ( ast:: ExprFor :: parse_with_attributes_and_label (
328
338
parser,
329
- take ( & mut attributes) ,
339
+ take ( attributes) ,
330
340
label,
331
341
) ?)
332
342
}
@@ -340,70 +350,56 @@ impl Expr {
340
350
}
341
351
ast:: Kind :: While => Self :: ExprWhile ( ast:: ExprWhile :: parse_with_attributes (
342
352
parser,
343
- take ( & mut attributes) ,
353
+ take ( attributes) ,
344
354
) ?) ,
345
355
ast:: Kind :: Loop => Self :: ExprLoop ( ast:: ExprLoop :: parse_with_attributes (
346
356
parser,
347
- take ( & mut attributes) ,
357
+ take ( attributes) ,
348
358
) ?) ,
349
359
ast:: Kind :: For => Self :: ExprFor ( ast:: ExprFor :: parse_with_attributes (
350
360
parser,
351
- take ( & mut attributes) ,
361
+ take ( attributes) ,
352
362
) ?) ,
353
363
ast:: Kind :: Let => Self :: ExprLet ( ast:: ExprLet :: parse_with_attributes (
354
364
parser,
355
- take ( & mut attributes) ,
365
+ take ( attributes) ,
356
366
) ?) ,
357
367
ast:: Kind :: If => Self :: ExprIf ( ast:: ExprIf :: parse_with_attributes (
358
368
parser,
359
- take ( & mut attributes) ,
369
+ take ( attributes) ,
360
370
) ?) ,
361
371
ast:: Kind :: Match => Self :: ExprMatch ( ast:: ExprMatch :: parse_with_attributes (
362
372
parser,
363
- take ( & mut attributes) ,
373
+ take ( attributes) ,
364
374
) ?) ,
365
375
ast:: Kind :: Open ( ast:: Delimiter :: Parenthesis ) => {
366
- Self :: parse_open_paren ( parser, & mut attributes) ?
376
+ Self :: parse_open_paren ( parser, attributes) ?
367
377
}
368
378
ast:: Kind :: Open ( ast:: Delimiter :: Brace ) => Self :: ExprBlock (
369
- ast:: ExprBlock :: parse_with_attributes ( parser, take ( & mut attributes) ) ?,
379
+ ast:: ExprBlock :: parse_with_attributes ( parser, take ( attributes) ) ?,
370
380
) ,
371
381
ast:: Kind :: Ident ( ..) => {
372
- Self :: parse_ident_start ( parser, eager_brace, & mut attributes) ?
382
+ let path = parser. parse ( ) ?;
383
+ Self :: parse_with_path ( parser, path, eager_brace, attributes) ?
373
384
}
374
385
ast:: Kind :: Break => Self :: ExprBreak ( ast:: ExprBreak :: parse_with_attributes (
375
386
parser,
376
- take ( & mut attributes) ,
387
+ take ( attributes) ,
377
388
) ?) ,
378
389
ast:: Kind :: Yield => Self :: ExprYield ( ast:: ExprYield :: parse_with_attributes (
379
390
parser,
380
- take ( & mut attributes) ,
391
+ take ( attributes) ,
381
392
) ?) ,
382
393
ast:: Kind :: Return => Self :: ExprReturn ( ast:: ExprReturn :: parse_with_attributes (
383
394
parser,
384
- take ( & mut attributes) ,
395
+ take ( attributes) ,
385
396
) ?) ,
386
397
_ => {
387
398
return Err ( ParseError :: expected ( token, "expression" ) ) ;
388
399
}
389
400
}
390
401
} ;
391
402
392
- let mut it = attributes. into_iter ( ) ;
393
-
394
- if let Some ( first) = it. next ( ) {
395
- let span = if let Some ( last) = it. next_back ( ) {
396
- first. span ( ) . join ( last. span ( ) )
397
- } else {
398
- first. span ( )
399
- } ;
400
-
401
- return Err ( ParseError :: new (
402
- span,
403
- ParseErrorKind :: AttributesNotSupported ,
404
- ) ) ;
405
- }
406
-
407
403
if !* expr_chain {
408
404
return Ok ( expr) ;
409
405
}
@@ -469,8 +465,12 @@ impl Expr {
469
465
}
470
466
}
471
467
472
- let next =
473
- Expr :: parse_primary ( parser, EagerBrace ( false ) , ExprChain ( false ) , vec ! [ ] ) ?;
468
+ let next = Expr :: parse_primary (
469
+ parser,
470
+ EagerBrace ( false ) ,
471
+ ExprChain ( false ) ,
472
+ & mut vec ! [ ] ,
473
+ ) ?;
474
474
475
475
let span = match next {
476
476
Expr :: Path ( path) => {
@@ -536,7 +536,7 @@ impl Expr {
536
536
parser. token_next ( ) ?;
537
537
}
538
538
539
- let mut rhs = Self :: parse_primary ( parser, eager_brace, ExprChain ( true ) , vec ! [ ] ) ?;
539
+ let mut rhs = Self :: parse_primary ( parser, eager_brace, ExprChain ( true ) , & mut vec ! [ ] ) ?;
540
540
541
541
lookahead_tok = parser. token_peek_pair ( ) ?;
542
542
@@ -607,8 +607,17 @@ impl Expr {
607
607
/// ```
608
608
impl Parse for Expr {
609
609
fn parse ( parser : & mut Parser < ' _ > ) -> Result < Self , ParseError > {
610
- let attributes = parser. parse ( ) ?;
611
- Self :: parse_full ( parser, EagerBrace ( true ) , ExprChain ( true ) , attributes)
610
+ let mut attributes = parser. parse ( ) ?;
611
+ let expr = Self :: parse_full ( parser, EagerBrace ( true ) , ExprChain ( true ) , & mut attributes) ?;
612
+
613
+ if let Some ( span) = attributes. option_span ( ) {
614
+ return Err ( ParseError :: new (
615
+ span,
616
+ ParseErrorKind :: AttributesNotSupported ,
617
+ ) ) ;
618
+ }
619
+
620
+ Ok ( expr)
612
621
}
613
622
}
614
623
0 commit comments