@@ -237,6 +237,15 @@ fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
237
237
lhs
238
238
}
239
239
240
+ #[ derive( PartialEq ) ]
241
+ enum LastTokenKind {
242
+ DocComment ,
243
+ Comma ,
244
+ Interpolated ,
245
+ Eof ,
246
+ Other ,
247
+ }
248
+
240
249
/* ident is handled by common.rs */
241
250
242
251
pub struct Parser < ' a > {
@@ -248,10 +257,8 @@ pub struct Parser<'a> {
248
257
/// the span of the prior token:
249
258
pub last_span : Span ,
250
259
pub cfg : CrateConfig ,
251
- /// the previous token or None (only stashed sometimes).
252
- pub last_token : Option < Box < token:: Token > > ,
253
- last_token_interpolated : bool ,
254
- last_token_eof : bool ,
260
+ /// the previous token kind
261
+ last_token_kind : LastTokenKind ,
255
262
pub buffer : [ TokenAndSpan ; 4 ] ,
256
263
pub buffer_start : isize ,
257
264
pub buffer_end : isize ,
@@ -362,9 +369,7 @@ impl<'a> Parser<'a> {
362
369
token : tok0. tok ,
363
370
span : span,
364
371
last_span : span,
365
- last_token : None ,
366
- last_token_interpolated : false ,
367
- last_token_eof : false ,
372
+ last_token_kind : LastTokenKind :: Other ,
368
373
buffer : [
369
374
placeholder. clone ( ) ,
370
375
placeholder. clone ( ) ,
@@ -500,7 +505,7 @@ impl<'a> Parser<'a> {
500
505
expr : PResult < ' a , P < Expr > > )
501
506
-> PResult < ' a , ( Span , P < Expr > ) > {
502
507
expr. map ( |e| {
503
- if self . last_token_interpolated {
508
+ if self . last_token_kind == LastTokenKind :: Interpolated {
504
509
( self . last_span , e)
505
510
} else {
506
511
( e. span , e)
@@ -520,21 +525,19 @@ impl<'a> Parser<'a> {
520
525
self . bug ( "ident interpolation not converted to real token" ) ;
521
526
}
522
527
_ => {
523
- let last_token = self . last_token . clone ( ) . map ( |t| * t) ;
524
- Err ( match last_token {
525
- Some ( token:: DocComment ( _) ) => self . span_fatal_help ( self . last_span ,
528
+ Err ( if self . last_token_kind == LastTokenKind :: DocComment {
529
+ self . span_fatal_help ( self . last_span ,
526
530
"found a documentation comment that doesn't document anything" ,
527
531
"doc comments must come before what they document, maybe a comment was \
528
- intended with `//`?") ,
529
- _ => {
532
+ intended with `//`?")
533
+ } else {
530
534
let mut err = self . fatal ( & format ! ( "expected identifier, found `{}`" ,
531
535
self . this_token_to_string( ) ) ) ;
532
536
if self . token == token:: Underscore {
533
537
err. note ( "`_` is a wildcard pattern, not an identifier" ) ;
534
538
}
535
539
err
536
- }
537
- } )
540
+ } )
538
541
}
539
542
}
540
543
}
@@ -923,26 +926,22 @@ impl<'a> Parser<'a> {
923
926
924
927
/// Advance the parser by one token
925
928
pub fn bump ( & mut self ) {
926
- if self . last_token_eof {
929
+ if self . last_token_kind == LastTokenKind :: Eof {
927
930
// Bumping after EOF is a bad sign, usually an infinite loop.
928
931
self . bug ( "attempted to bump the parser past EOF (may be stuck in a loop)" ) ;
929
932
}
930
933
931
- if self . token == token:: Eof {
932
- self . last_token_eof = true ;
933
- }
934
-
935
934
self . last_span = self . span ;
936
- // Stash token for error recovery (sometimes; clone is not necessarily cheap).
937
- self . last_token = if self . token . is_ident ( ) ||
938
- self . token . is_path ( ) ||
939
- self . token . is_doc_comment ( ) ||
940
- self . token == token :: Comma {
941
- Some ( Box :: new ( self . token . clone ( ) ) )
942
- } else {
943
- None
935
+
936
+ // Record last token kind for possible error recovery.
937
+ self . last_token_kind = match self . token {
938
+ token:: DocComment ( .. ) => LastTokenKind :: DocComment ,
939
+ token:: Comma => LastTokenKind :: Comma ,
940
+ token :: Interpolated ( .. ) => LastTokenKind :: Interpolated ,
941
+ token :: Eof => LastTokenKind :: Eof ,
942
+ _ => LastTokenKind :: Other ,
944
943
} ;
945
- self . last_token_interpolated = self . token . is_interpolated ( ) ;
944
+
946
945
let next = if self . buffer_start == self . buffer_end {
947
946
self . reader . real_token ( )
948
947
} else {
@@ -979,11 +978,10 @@ impl<'a> Parser<'a> {
979
978
lo : BytePos ,
980
979
hi : BytePos ) {
981
980
self . last_span = mk_sp ( self . span . lo , lo) ;
982
- // It would be incorrect to just stash current token, but fortunately
983
- // for tokens currently using `bump_with`, last_token will be of no
984
- // use anyway.
985
- self . last_token = None ;
986
- self . last_token_interpolated = false ;
981
+ // It would be incorrect to record the kind of the current token, but
982
+ // fortunately for tokens currently using `bump_with`, the
983
+ // last_token_kind will be of no use anyway.
984
+ self . last_token_kind = LastTokenKind :: Other ;
987
985
self . span = mk_sp ( lo, hi) ;
988
986
self . token = next;
989
987
self . expected_tokens . clear ( ) ;
@@ -2974,7 +2972,7 @@ impl<'a> Parser<'a> {
2974
2972
self . expected_tokens . push ( TokenType :: Operator ) ;
2975
2973
while let Some ( op) = AssocOp :: from_token ( & self . token ) {
2976
2974
2977
- let lhs_span = if self . last_token_interpolated {
2975
+ let lhs_span = if self . last_token_kind == LastTokenKind :: Interpolated {
2978
2976
self . last_span
2979
2977
} else {
2980
2978
lhs. span
@@ -4036,13 +4034,13 @@ impl<'a> Parser<'a> {
4036
4034
None => {
4037
4035
let unused_attrs = |attrs : & [ _ ] , s : & mut Self | {
4038
4036
if attrs. len ( ) > 0 {
4039
- let last_token = s. last_token . clone ( ) . map ( |t| * t) ;
4040
- match last_token {
4041
- Some ( token:: DocComment ( _) ) => s. span_err_help ( s. last_span ,
4037
+ if s. last_token_kind == LastTokenKind :: DocComment {
4038
+ s. span_err_help ( s. last_span ,
4042
4039
"found a documentation comment that doesn't document anything" ,
4043
4040
"doc comments must come before what they document, maybe a \
4044
- comment was intended with `//`?") ,
4045
- _ => s. span_err ( s. span , "expected statement after outer attribute" ) ,
4041
+ comment was intended with `//`?") ;
4042
+ } else {
4043
+ s. span_err ( s. span , "expected statement after outer attribute" ) ;
4046
4044
}
4047
4045
}
4048
4046
} ;
@@ -4332,9 +4330,7 @@ impl<'a> Parser<'a> {
4332
4330
4333
4331
let missing_comma = !lifetimes. is_empty ( ) &&
4334
4332
!self . token . is_like_gt ( ) &&
4335
- self . last_token
4336
- . as_ref ( ) . map_or ( true ,
4337
- |x| & * * x != & token:: Comma ) ;
4333
+ self . last_token_kind != LastTokenKind :: Comma ;
4338
4334
4339
4335
if missing_comma {
4340
4336
0 commit comments