1
1
//! A higher level attributes based on TokenTree, with also some shortcuts.
2
2
3
- use std:: { borrow:: Cow , hash:: Hash , ops, slice } ;
3
+ use std:: { borrow:: Cow , hash:: Hash , ops} ;
4
4
5
5
use base_db:: CrateId ;
6
6
use cfg:: { CfgExpr , CfgOptions } ;
@@ -17,6 +17,7 @@ use syntax::{
17
17
AstPtr ,
18
18
} ;
19
19
use triomphe:: Arc ;
20
+ use tt:: iter:: { TtElement , TtIter } ;
20
21
21
22
use crate :: {
22
23
db:: DefDatabase ,
@@ -154,15 +155,15 @@ impl Attrs {
154
155
155
156
pub fn has_doc_hidden ( & self ) -> bool {
156
157
self . by_key ( & sym:: doc) . tt_values ( ) . any ( |tt| {
157
- tt. delimiter . kind == DelimiterKind :: Parenthesis &&
158
- matches ! ( & * tt. token_trees, [ tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( ident) ) ] if ident. sym == sym:: hidden)
158
+ tt. top_subtree ( ) . delimiter . kind == DelimiterKind :: Parenthesis &&
159
+ matches ! ( tt. token_trees( ) . flat_tokens ( ) , [ tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( ident) ) ] if ident. sym == sym:: hidden)
159
160
} )
160
161
}
161
162
162
163
pub fn has_doc_notable_trait ( & self ) -> bool {
163
164
self . by_key ( & sym:: doc) . tt_values ( ) . any ( |tt| {
164
- tt. delimiter . kind == DelimiterKind :: Parenthesis &&
165
- matches ! ( & * tt. token_trees, [ tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( ident) ) ] if ident. sym == sym:: notable_trait)
165
+ tt. top_subtree ( ) . delimiter . kind == DelimiterKind :: Parenthesis &&
166
+ matches ! ( tt. token_trees( ) . flat_tokens ( ) , [ tt:: TokenTree :: Leaf ( tt:: Leaf :: Ident ( ident) ) ] if ident. sym == sym:: notable_trait)
166
167
} )
167
168
}
168
169
@@ -245,8 +246,8 @@ impl From<DocAtom> for DocExpr {
245
246
}
246
247
247
248
impl DocExpr {
248
- fn parse < S > ( tt : & tt:: Subtree < S > ) -> DocExpr {
249
- next_doc_expr ( & mut tt . token_trees . iter ( ) ) . unwrap_or ( DocExpr :: Invalid )
249
+ fn parse < S : Copy > ( tt : & tt:: TopSubtree < S > ) -> DocExpr {
250
+ next_doc_expr ( tt . iter ( ) ) . unwrap_or ( DocExpr :: Invalid )
250
251
}
251
252
252
253
pub fn aliases ( & self ) -> & [ Symbol ] {
@@ -260,62 +261,47 @@ impl DocExpr {
260
261
}
261
262
}
262
263
263
- fn next_doc_expr < S > ( it : & mut slice :: Iter < ' _ , tt :: TokenTree < S > > ) -> Option < DocExpr > {
264
+ fn next_doc_expr < S : Copy > ( mut it : TtIter < ' _ , S > ) -> Option < DocExpr > {
264
265
let name = match it. next ( ) {
265
266
None => return None ,
266
- Some ( tt :: TokenTree :: Leaf ( tt:: Leaf :: Ident ( ident) ) ) => ident. sym . clone ( ) ,
267
+ Some ( TtElement :: Leaf ( tt:: Leaf :: Ident ( ident) ) ) => ident. sym . clone ( ) ,
267
268
Some ( _) => return Some ( DocExpr :: Invalid ) ,
268
269
} ;
269
270
270
271
// Peek
271
- let ret = match it. as_slice ( ) . first ( ) {
272
- Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) ) if punct. char == '=' => {
273
- match it. as_slice ( ) . get ( 1 ) {
274
- Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
272
+ let ret = match it. peek ( ) {
273
+ Some ( TtElement :: Leaf ( tt:: Leaf :: Punct ( punct) ) ) if punct. char == '=' => {
274
+ it. next ( ) ;
275
+ match it. next ( ) {
276
+ Some ( TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
275
277
symbol : text,
276
278
kind : tt:: LitKind :: Str ,
277
279
..
278
- } ) ) ) => {
279
- it. next ( ) ;
280
- it. next ( ) ;
281
- DocAtom :: KeyValue { key : name, value : text. clone ( ) } . into ( )
282
- }
280
+ } ) ) ) => DocAtom :: KeyValue { key : name, value : text. clone ( ) } . into ( ) ,
283
281
_ => return Some ( DocExpr :: Invalid ) ,
284
282
}
285
283
}
286
- Some ( tt :: TokenTree :: Subtree ( subtree ) ) => {
284
+ Some ( TtElement :: Subtree ( _ , subtree_iter ) ) => {
287
285
it. next ( ) ;
288
- let subs = parse_comma_sep ( subtree ) ;
286
+ let subs = parse_comma_sep ( subtree_iter ) ;
289
287
match & name {
290
288
s if * s == sym:: alias => DocExpr :: Alias ( subs) ,
291
289
_ => DocExpr :: Invalid ,
292
290
}
293
291
}
294
292
_ => DocAtom :: Flag ( name) . into ( ) ,
295
293
} ;
296
-
297
- // Eat comma separator
298
- if let Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) ) = it. as_slice ( ) . first ( ) {
299
- if punct. char == ',' {
300
- it. next ( ) ;
301
- }
302
- }
303
294
Some ( ret)
304
295
}
305
296
306
- fn parse_comma_sep < S > ( subtree : & tt:: Subtree < S > ) -> Vec < Symbol > {
307
- subtree
308
- . token_trees
309
- . iter ( )
310
- . filter_map ( |tt| match tt {
311
- tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
312
- kind : tt:: LitKind :: Str ,
313
- symbol,
314
- ..
315
- } ) ) => Some ( symbol. clone ( ) ) ,
316
- _ => None ,
317
- } )
318
- . collect ( )
297
+ fn parse_comma_sep < S > ( iter : TtIter < ' _ , S > ) -> Vec < Symbol > {
298
+ iter. filter_map ( |tt| match tt {
299
+ TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
300
+ kind : tt:: LitKind :: Str , symbol, ..
301
+ } ) ) => Some ( symbol. clone ( ) ) ,
302
+ _ => None ,
303
+ } )
304
+ . collect ( )
319
305
}
320
306
321
307
impl AttrsWithOwner {
@@ -563,7 +549,7 @@ pub struct AttrQuery<'attr> {
563
549
}
564
550
565
551
impl < ' attr > AttrQuery < ' attr > {
566
- pub fn tt_values ( self ) -> impl Iterator < Item = & ' attr crate :: tt:: Subtree > {
552
+ pub fn tt_values ( self ) -> impl Iterator < Item = & ' attr crate :: tt:: TopSubtree > {
567
553
self . attrs ( ) . filter_map ( |attr| attr. token_tree_value ( ) )
568
554
}
569
555
@@ -585,7 +571,7 @@ impl<'attr> AttrQuery<'attr> {
585
571
586
572
pub fn attrs ( self ) -> impl Iterator < Item = & ' attr Attr > + Clone {
587
573
let key = self . key ;
588
- self . attrs . iter ( ) . filter ( move |attr| attr. path . as_ident ( ) . map_or ( false , |s| * s == * key) )
574
+ self . attrs . iter ( ) . filter ( move |attr| attr. path . as_ident ( ) . is_some_and ( |s| * s == * key) )
589
575
}
590
576
591
577
/// Find string value for a specific key inside token tree
@@ -596,12 +582,12 @@ impl<'attr> AttrQuery<'attr> {
596
582
/// ```
597
583
pub fn find_string_value_in_tt ( self , key : & ' attr Symbol ) -> Option < & ' attr str > {
598
584
self . tt_values ( ) . find_map ( |tt| {
599
- let name = tt. token_trees . iter ( )
600
- . skip_while ( |tt| !matches ! ( tt, tt :: TokenTree :: Leaf ( tt:: Leaf :: Ident ( tt:: Ident { sym, ..} ) ) if * sym == * key) )
585
+ let name = tt. iter ( )
586
+ . skip_while ( |tt| !matches ! ( tt, TtElement :: Leaf ( tt:: Leaf :: Ident ( tt:: Ident { sym, ..} ) ) if * sym == * key) )
601
587
. nth ( 2 ) ;
602
588
603
589
match name {
604
- Some ( tt :: TokenTree :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal { symbol : text, kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) , ..} ) ) ) => Some ( text. as_str ( ) ) ,
590
+ Some ( TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal { symbol : text, kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) , ..} ) ) ) => Some ( text. as_str ( ) ) ,
605
591
_ => None
606
592
}
607
593
} )
0 commit comments