11use crate :: ast:: Ident ;
22use crate :: ext:: base:: ExtCtxt ;
33use crate :: ext:: expand:: Marker ;
4- use crate :: ext:: tt:: macro_parser:: { NamedMatch , MatchedSeq , MatchedNonterminal } ;
4+ use crate :: ext:: tt:: macro_parser:: { MatchedNonterminal , MatchedSeq , NamedMatch } ;
55use crate :: ext:: tt:: quoted;
66use crate :: mut_visit:: noop_visit_tt;
7- use crate :: parse:: token:: { self , Token , NtTT } ;
7+ use crate :: parse:: token:: { self , NtTT , Token } ;
88use crate :: tokenstream:: { DelimSpan , TokenStream , TokenTree , TreeAndJoint } ;
99
1010use smallvec:: { smallvec, SmallVec } ;
@@ -16,18 +16,10 @@ use std::mem;
1616use std:: ops:: Add ;
1717use std:: rc:: Rc ;
1818
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 (`$(...)`).
2020enum 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 > } ,
3123}
3224
3325impl Frame {
@@ -54,13 +46,13 @@ impl Iterator for Frame {
5446 }
5547}
5648
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 {
6456 let mut stack: SmallVec < [ Frame ; 1 ] > = smallvec ! [ Frame :: new( src) ] ;
6557 let interpolations = interp. unwrap_or_else ( FxHashMap :: default) ; /* just a convenience */
6658 let mut repeats = Vec :: new ( ) ;
@@ -84,7 +76,7 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
8476 } ;
8577 result. push ( TokenTree :: Token ( prev_span, sep) . into ( ) ) ;
8678 }
87- continue
79+ continue ;
8880 }
8981 }
9082
@@ -96,29 +88,30 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
9688 if result_stack. is_empty ( ) {
9789 return TokenStream :: new ( result) ;
9890 }
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 ( ) ) ;
10493 result = result_stack. pop ( ) . unwrap ( ) ;
10594 result. push ( tree. into ( ) ) ;
10695 }
10796 }
108- continue
97+ continue ;
10998 } ;
11099
111100 match tree {
112101 quoted:: TokenTree :: Sequence ( sp, seq) => {
113102 // 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+ ) {
117108 LockstepIterSize :: Unconstrained => {
118- cx. span_fatal ( sp. entire ( ) , /* blame macro writer */
109+ cx. span_fatal (
110+ sp. entire ( ) , /* blame macro writer */
119111 "attempted to repeat an expression \
120112 containing no syntax \
121- variables matched as repeating at this depth") ;
113+ variables matched as repeating at this depth",
114+ ) ;
122115 }
123116 LockstepIterSize :: Contradiction ( ref msg) => {
124117 // FIXME #2887 blame macro invoker instead
@@ -153,8 +146,10 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
153146 result. push ( token. into ( ) ) ;
154147 }
155148 } 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+ ) ;
158153 }
159154 } else {
160155 let ident =
@@ -180,10 +175,11 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
180175 }
181176}
182177
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 > > {
187183 interpolations. get ( & ident) . map ( |matched| {
188184 let mut matched = matched. clone ( ) ;
189185 for & ( idx, _) in repeats {
@@ -217,40 +213,44 @@ impl Add for LockstepIterSize {
217213 LockstepIterSize :: Contradiction ( _) => other,
218214 LockstepIterSize :: Constraint ( r_len, _) if l_len == r_len => self ,
219215 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+ ) ;
223221 LockstepIterSize :: Contradiction ( msg)
224222 }
225223 } ,
226224 }
227225 }
228226}
229227
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 {
234233 use quoted:: TokenTree ;
235234 match * tree {
236235 TokenTree :: Delimited ( _, ref delimed) => {
237236 delimed. tts . iter ( ) . fold ( LockstepIterSize :: Unconstrained , |size, tt| {
238237 size + lockstep_iter_size ( tt, interpolations, repeats)
239238 } )
240- } ,
239+ }
241240 TokenTree :: Sequence ( _, ref seq) => {
242241 seq. tts . iter ( ) . fold ( LockstepIterSize :: Unconstrained , |size, tt| {
243242 size + lockstep_iter_size ( tt, interpolations, repeats)
244243 } )
245- } ,
246- TokenTree :: MetaVar ( _, name) | TokenTree :: MetaVarDecl ( _, name, _) =>
244+ }
245+ TokenTree :: MetaVar ( _, name) | TokenTree :: MetaVarDecl ( _, name, _) => {
247246 match lookup_cur_matched ( name, interpolations, repeats) {
248247 Some ( matched) => match * matched {
249248 MatchedNonterminal ( _) => LockstepIterSize :: Unconstrained ,
250249 MatchedSeq ( ref ads, _) => LockstepIterSize :: Constraint ( ads. len ( ) , name) ,
251250 } ,
252- _ => LockstepIterSize :: Unconstrained
253- } ,
251+ _ => LockstepIterSize :: Unconstrained ,
252+ }
253+ }
254254 TokenTree :: Token ( ..) => LockstepIterSize :: Unconstrained ,
255255 }
256256}
0 commit comments