@@ -70,7 +70,7 @@ pub(super) struct Match {
70
70
}
71
71
72
72
impl Match {
73
- pub ( super ) fn add_err ( & mut self , err : ExpandError ) {
73
+ fn add_err ( & mut self , err : ExpandError ) {
74
74
let prev_err = self . err . take ( ) ;
75
75
self . err = prev_err. or ( Some ( err) ) ;
76
76
self . err_count += 1 ;
@@ -79,12 +79,10 @@ impl Match {
79
79
80
80
/// Matching errors are added to the `Match`.
81
81
pub ( super ) fn match_ ( pattern : & MetaTemplate , src : & tt:: Subtree ) -> Match {
82
- assert ! ( pattern. delimiter == None ) ;
83
-
84
82
let mut res = Match :: default ( ) ;
85
83
let mut src = TtIter :: new ( src) ;
86
84
87
- match_subtree ( & mut res, pattern, & mut src) ;
85
+ match_tokens ( & mut res, pattern, & mut src) ;
88
86
89
87
if src. len ( ) > 0 {
90
88
res. unmatched_tts += src. len ( ) ;
@@ -94,49 +92,29 @@ pub(super) fn match_(pattern: &MetaTemplate, src: &tt::Subtree) -> Match {
94
92
res
95
93
}
96
94
97
- fn match_subtree ( res : & mut Match , pattern : & MetaTemplate , src : & mut TtIter ) {
95
+ fn match_tokens ( res : & mut Match , pattern : & MetaTemplate , src : & mut TtIter ) {
98
96
for op in pattern. iter ( ) {
99
97
match op {
100
98
Op :: Leaf ( lhs) => {
101
- let rhs = match src. expect_leaf ( ) {
102
- Ok ( l) => l,
103
- Err ( ( ) ) => {
104
- res. add_err ( err ! ( "expected leaf: `{}`" , lhs) ) ;
105
- continue ;
106
- }
107
- } ;
108
- match ( lhs, rhs) {
109
- (
110
- tt:: Leaf :: Punct ( tt:: Punct { char : lhs, .. } ) ,
111
- tt:: Leaf :: Punct ( tt:: Punct { char : rhs, .. } ) ,
112
- ) if lhs == rhs => ( ) ,
113
- (
114
- tt:: Leaf :: Ident ( tt:: Ident { text : lhs, .. } ) ,
115
- tt:: Leaf :: Ident ( tt:: Ident { text : rhs, .. } ) ,
116
- ) if lhs == rhs => ( ) ,
117
- (
118
- tt:: Leaf :: Literal ( tt:: Literal { text : lhs, .. } ) ,
119
- tt:: Leaf :: Literal ( tt:: Literal { text : rhs, .. } ) ,
120
- ) if lhs == rhs => ( ) ,
121
- _ => {
122
- res. add_err ( ExpandError :: UnexpectedToken ) ;
123
- }
99
+ if let Err ( err) = match_leaf ( lhs, src) {
100
+ res. add_err ( err) ;
101
+ continue ;
124
102
}
125
103
}
126
- Op :: Subtree ( lhs ) => {
104
+ Op :: Subtree { tokens , delimiter : delim } => {
127
105
let rhs = match src. expect_subtree ( ) {
128
106
Ok ( s) => s,
129
107
Err ( ( ) ) => {
130
108
res. add_err ( err ! ( "expected subtree" ) ) ;
131
109
continue ;
132
110
}
133
111
} ;
134
- if lhs . delimiter_kind ( ) != rhs. delimiter_kind ( ) {
112
+ if delim . map ( |it| it . kind ) != rhs. delimiter_kind ( ) {
135
113
res. add_err ( err ! ( "mismatched delimiter" ) ) ;
136
114
continue ;
137
115
}
138
116
let mut src = TtIter :: new ( rhs) ;
139
- match_subtree ( res, lhs , & mut src) ;
117
+ match_tokens ( res, tokens , & mut src) ;
140
118
if src. len ( ) > 0 {
141
119
res. add_err ( err ! ( "leftover tokens" ) ) ;
142
120
}
@@ -162,14 +140,42 @@ fn match_subtree(res: &mut Match, pattern: &MetaTemplate, src: &mut TtIter) {
162
140
res. add_err ( err) ;
163
141
}
164
142
}
165
- Op :: Repeat { subtree, kind, separator } => {
143
+ Op :: Repeat { tokens : subtree, kind, separator } => {
166
144
match_repeat ( res, subtree, * kind, separator, src) ;
167
145
}
168
146
}
169
147
}
170
148
}
171
149
172
- pub ( super ) fn match_repeat (
150
+ fn match_leaf ( lhs : & tt:: Leaf , src : & mut TtIter ) -> Result < ( ) , ExpandError > {
151
+ let rhs = match src. expect_leaf ( ) {
152
+ Ok ( l) => l,
153
+ Err ( ( ) ) => {
154
+ return Err ( err ! ( "expected leaf: `{}`" , lhs) ) ;
155
+ }
156
+ } ;
157
+ match ( lhs, rhs) {
158
+ (
159
+ tt:: Leaf :: Punct ( tt:: Punct { char : lhs, .. } ) ,
160
+ tt:: Leaf :: Punct ( tt:: Punct { char : rhs, .. } ) ,
161
+ ) if lhs == rhs => ( ) ,
162
+ (
163
+ tt:: Leaf :: Ident ( tt:: Ident { text : lhs, .. } ) ,
164
+ tt:: Leaf :: Ident ( tt:: Ident { text : rhs, .. } ) ,
165
+ ) if lhs == rhs => ( ) ,
166
+ (
167
+ tt:: Leaf :: Literal ( tt:: Literal { text : lhs, .. } ) ,
168
+ tt:: Leaf :: Literal ( tt:: Literal { text : rhs, .. } ) ,
169
+ ) if lhs == rhs => ( ) ,
170
+ _ => {
171
+ return Err ( ExpandError :: UnexpectedToken ) ;
172
+ }
173
+ }
174
+
175
+ Ok ( ( ) )
176
+ }
177
+
178
+ fn match_repeat (
173
179
res : & mut Match ,
174
180
pattern : & MetaTemplate ,
175
181
kind : RepeatKind ,
@@ -191,7 +197,7 @@ pub(super) fn match_repeat(
191
197
}
192
198
193
199
let mut nested = Match :: default ( ) ;
194
- match_subtree ( & mut nested, pattern, & mut fork) ;
200
+ match_tokens ( & mut nested, pattern, & mut fork) ;
195
201
if nested. err . is_none ( ) {
196
202
limit -= 1 ;
197
203
if limit == 0 {
@@ -292,8 +298,8 @@ fn collect_vars(buf: &mut Vec<SmolStr>, pattern: &MetaTemplate) {
292
298
match op {
293
299
Op :: Var { name, .. } => buf. push ( name. clone ( ) ) ,
294
300
Op :: Leaf ( _) => ( ) ,
295
- Op :: Subtree ( subtree ) => collect_vars ( buf, subtree ) ,
296
- Op :: Repeat { subtree , .. } => collect_vars ( buf, subtree ) ,
301
+ Op :: Subtree { tokens , .. } => collect_vars ( buf, tokens ) ,
302
+ Op :: Repeat { tokens , .. } => collect_vars ( buf, tokens ) ,
297
303
}
298
304
}
299
305
}
@@ -325,7 +331,7 @@ impl<'a> TtIter<'a> {
325
331
ok
326
332
}
327
333
328
- pub ( crate ) fn expect_tt ( & mut self ) -> Result < tt:: TokenTree , ( ) > {
334
+ fn expect_tt ( & mut self ) -> Result < tt:: TokenTree , ( ) > {
329
335
match self . peek_n ( 0 ) {
330
336
Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) ) if punct. char == '\'' => {
331
337
return self . expect_lifetime ( ) ;
@@ -386,7 +392,7 @@ impl<'a> TtIter<'a> {
386
392
}
387
393
}
388
394
389
- pub ( crate ) fn expect_lifetime ( & mut self ) -> Result < tt:: TokenTree , ( ) > {
395
+ fn expect_lifetime ( & mut self ) -> Result < tt:: TokenTree , ( ) > {
390
396
let punct = self . expect_punct ( ) ?;
391
397
if punct. char != '\'' {
392
398
return Err ( ( ) ) ;
@@ -403,13 +409,13 @@ impl<'a> TtIter<'a> {
403
409
. into ( ) )
404
410
}
405
411
406
- pub ( crate ) fn expect_fragment (
412
+ fn expect_fragment (
407
413
& mut self ,
408
414
fragment_kind : parser:: FragmentKind ,
409
415
) -> ExpandResult < Option < tt:: TokenTree > > {
410
- pub ( crate ) struct OffsetTokenSink < ' a > {
411
- pub ( crate ) cursor : Cursor < ' a > ,
412
- pub ( crate ) error : bool ,
416
+ struct OffsetTokenSink < ' a > {
417
+ cursor : Cursor < ' a > ,
418
+ error : bool ,
413
419
}
414
420
415
421
impl < ' a > TreeSink for OffsetTokenSink < ' a > {
@@ -465,7 +471,7 @@ impl<'a> TtIter<'a> {
465
471
ExpandResult { value : res, err }
466
472
}
467
473
468
- pub ( crate ) fn eat_vis ( & mut self ) -> Option < tt:: TokenTree > {
474
+ fn eat_vis ( & mut self ) -> Option < tt:: TokenTree > {
469
475
let mut fork = self . clone ( ) ;
470
476
match fork. expect_fragment ( Visibility ) {
471
477
ExpandResult { value : tt, err : None } => {
@@ -476,7 +482,7 @@ impl<'a> TtIter<'a> {
476
482
}
477
483
}
478
484
479
- pub ( crate ) fn eat_char ( & mut self , c : char ) -> Option < tt:: TokenTree > {
485
+ fn eat_char ( & mut self , c : char ) -> Option < tt:: TokenTree > {
480
486
let mut fork = self . clone ( ) ;
481
487
match fork. expect_char ( c) {
482
488
Ok ( _) => {
0 commit comments