1
1
use crate :: ast:: Ident ;
2
2
use crate :: ext:: base:: ExtCtxt ;
3
3
use crate :: ext:: expand:: Marker ;
4
- use crate :: ext:: tt:: macro_parser:: { NamedMatch , MatchedSeq , MatchedNonterminal } ;
4
+ use crate :: ext:: tt:: macro_parser:: { MatchedNonterminal , MatchedSeq , NamedMatch } ;
5
5
use crate :: ext:: tt:: quoted;
6
6
use crate :: mut_visit:: noop_visit_tt;
7
- use crate :: parse:: token:: { self , Token , NtTT } ;
7
+ use crate :: parse:: token:: { self , NtTT , Token } ;
8
8
use crate :: tokenstream:: { DelimSpan , TokenStream , TokenTree , TreeAndJoint } ;
9
9
10
10
use smallvec:: { smallvec, SmallVec } ;
@@ -16,18 +16,10 @@ use std::mem;
16
16
use std:: ops:: Add ;
17
17
use std:: rc:: Rc ;
18
18
19
- // An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
19
+ /// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
20
20
enum Frame {
21
- Delimited {
22
- forest : Lrc < quoted:: Delimited > ,
23
- idx : usize ,
24
- span : DelimSpan ,
25
- } ,
26
- Sequence {
27
- forest : Lrc < quoted:: SequenceRepetition > ,
28
- idx : usize ,
29
- sep : Option < Token > ,
30
- } ,
21
+ Delimited { forest : Lrc < quoted:: Delimited > , idx : usize , span : DelimSpan } ,
22
+ Sequence { forest : Lrc < quoted:: SequenceRepetition > , idx : usize , sep : Option < Token > } ,
31
23
}
32
24
33
25
impl Frame {
@@ -54,13 +46,13 @@ impl Iterator for Frame {
54
46
}
55
47
}
56
48
57
- /// This can do Macro-By-Example transcription. On the other hand, if
58
- /// `src` contains no ` TokenTree::{Sequence, MetaVar, MetaVarDecl}`s, `interp` can
59
- /// (and should) be None.
60
- pub fn transcribe ( cx : & ExtCtxt < ' _ > ,
61
- interp : Option < FxHashMap < Ident , Rc < NamedMatch > > > ,
62
- src : Vec < quoted:: TokenTree > )
63
- -> TokenStream {
49
+ /// This can do Macro-By-Example transcription. On the other hand, if `src` contains no
50
+ /// `TokenTree::{Sequence, MetaVar, MetaVarDecl}`s, `interp` can (and should) be `None`.
51
+ pub fn transcribe (
52
+ cx : & ExtCtxt < ' _ > ,
53
+ interp : Option < FxHashMap < Ident , Rc < NamedMatch > > > ,
54
+ src : Vec < quoted:: TokenTree > ,
55
+ ) -> TokenStream {
64
56
let mut stack: SmallVec < [ Frame ; 1 ] > = smallvec ! [ Frame :: new( src) ] ;
65
57
let interpolations = interp. unwrap_or_else ( FxHashMap :: default) ; /* just a convenience */
66
58
let mut repeats = Vec :: new ( ) ;
@@ -84,7 +76,7 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
84
76
} ;
85
77
result. push ( TokenTree :: Token ( prev_span, sep) . into ( ) ) ;
86
78
}
87
- continue
79
+ continue ;
88
80
}
89
81
}
90
82
@@ -96,29 +88,30 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
96
88
if result_stack. is_empty ( ) {
97
89
return TokenStream :: new ( result) ;
98
90
}
99
- let tree = TokenTree :: Delimited (
100
- span,
101
- forest. delim ,
102
- TokenStream :: new ( result) . into ( ) ,
103
- ) ;
91
+ let tree =
92
+ TokenTree :: Delimited ( span, forest. delim , TokenStream :: new ( result) . into ( ) ) ;
104
93
result = result_stack. pop ( ) . unwrap ( ) ;
105
94
result. push ( tree. into ( ) ) ;
106
95
}
107
96
}
108
- continue
97
+ continue ;
109
98
} ;
110
99
111
100
match tree {
112
101
quoted:: TokenTree :: Sequence ( sp, seq) => {
113
102
// FIXME(pcwalton): Bad copy.
114
- match lockstep_iter_size ( & quoted:: TokenTree :: Sequence ( sp, seq. clone ( ) ) ,
115
- & interpolations,
116
- & repeats) {
103
+ match lockstep_iter_size (
104
+ & quoted:: TokenTree :: Sequence ( sp, seq. clone ( ) ) ,
105
+ & interpolations,
106
+ & repeats,
107
+ ) {
117
108
LockstepIterSize :: Unconstrained => {
118
- cx. span_fatal ( sp. entire ( ) , /* blame macro writer */
109
+ cx. span_fatal (
110
+ sp. entire ( ) , /* blame macro writer */
119
111
"attempted to repeat an expression \
120
112
containing no syntax \
121
- variables matched as repeating at this depth") ;
113
+ variables matched as repeating at this depth",
114
+ ) ;
122
115
}
123
116
LockstepIterSize :: Contradiction ( ref msg) => {
124
117
// FIXME #2887 blame macro invoker instead
@@ -153,8 +146,10 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
153
146
result. push ( token. into ( ) ) ;
154
147
}
155
148
} else {
156
- cx. span_fatal ( sp, /* blame the macro writer */
157
- & format ! ( "variable '{}' is still repeating at this depth" , ident) ) ;
149
+ cx. span_fatal (
150
+ sp, /* blame the macro writer */
151
+ & format ! ( "variable '{}' is still repeating at this depth" , ident) ,
152
+ ) ;
158
153
}
159
154
} else {
160
155
let ident =
@@ -180,10 +175,11 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
180
175
}
181
176
}
182
177
183
- fn lookup_cur_matched ( ident : Ident ,
184
- interpolations : & FxHashMap < Ident , Rc < NamedMatch > > ,
185
- repeats : & [ ( usize , usize ) ] )
186
- -> Option < Rc < NamedMatch > > {
178
+ fn lookup_cur_matched (
179
+ ident : Ident ,
180
+ interpolations : & FxHashMap < Ident , Rc < NamedMatch > > ,
181
+ repeats : & [ ( usize , usize ) ] ,
182
+ ) -> Option < Rc < NamedMatch > > {
187
183
interpolations. get ( & ident) . map ( |matched| {
188
184
let mut matched = matched. clone ( ) ;
189
185
for & ( idx, _) in repeats {
@@ -217,40 +213,44 @@ impl Add for LockstepIterSize {
217
213
LockstepIterSize :: Contradiction ( _) => other,
218
214
LockstepIterSize :: Constraint ( r_len, _) if l_len == r_len => self ,
219
215
LockstepIterSize :: Constraint ( r_len, r_id) => {
220
- let msg = format ! ( "inconsistent lockstep iteration: \
221
- '{}' has {} items, but '{}' has {}",
222
- l_id, l_len, r_id, r_len) ;
216
+ let msg = format ! (
217
+ "inconsistent lockstep iteration: \
218
+ '{}' has {} items, but '{}' has {}",
219
+ l_id, l_len, r_id, r_len
220
+ ) ;
223
221
LockstepIterSize :: Contradiction ( msg)
224
222
}
225
223
} ,
226
224
}
227
225
}
228
226
}
229
227
230
- fn lockstep_iter_size ( tree : & quoted:: TokenTree ,
231
- interpolations : & FxHashMap < Ident , Rc < NamedMatch > > ,
232
- repeats : & [ ( usize , usize ) ] )
233
- -> LockstepIterSize {
228
+ fn lockstep_iter_size (
229
+ tree : & quoted:: TokenTree ,
230
+ interpolations : & FxHashMap < Ident , Rc < NamedMatch > > ,
231
+ repeats : & [ ( usize , usize ) ] ,
232
+ ) -> LockstepIterSize {
234
233
use quoted:: TokenTree ;
235
234
match * tree {
236
235
TokenTree :: Delimited ( _, ref delimed) => {
237
236
delimed. tts . iter ( ) . fold ( LockstepIterSize :: Unconstrained , |size, tt| {
238
237
size + lockstep_iter_size ( tt, interpolations, repeats)
239
238
} )
240
- } ,
239
+ }
241
240
TokenTree :: Sequence ( _, ref seq) => {
242
241
seq. tts . iter ( ) . fold ( LockstepIterSize :: Unconstrained , |size, tt| {
243
242
size + lockstep_iter_size ( tt, interpolations, repeats)
244
243
} )
245
- } ,
246
- TokenTree :: MetaVar ( _, name) | TokenTree :: MetaVarDecl ( _, name, _) =>
244
+ }
245
+ TokenTree :: MetaVar ( _, name) | TokenTree :: MetaVarDecl ( _, name, _) => {
247
246
match lookup_cur_matched ( name, interpolations, repeats) {
248
247
Some ( matched) => match * matched {
249
248
MatchedNonterminal ( _) => LockstepIterSize :: Unconstrained ,
250
249
MatchedSeq ( ref ads, _) => LockstepIterSize :: Constraint ( ads. len ( ) , name) ,
251
250
} ,
252
- _ => LockstepIterSize :: Unconstrained
253
- } ,
251
+ _ => LockstepIterSize :: Unconstrained ,
252
+ }
253
+ }
254
254
TokenTree :: Token ( ..) => LockstepIterSize :: Unconstrained ,
255
255
}
256
256
}
0 commit comments