11//! A higher level attributes based on TokenTree, with also some shortcuts.
22
3- use std:: { borrow:: Cow , hash:: Hash , ops, slice } ;
3+ use std:: { borrow:: Cow , hash:: Hash , ops} ;
44
55use base_db:: CrateId ;
66use cfg:: { CfgExpr , CfgOptions } ;
@@ -17,6 +17,7 @@ use syntax::{
1717 AstPtr ,
1818} ;
1919use triomphe:: Arc ;
20+ use tt:: iter:: { TtElement , TtIter } ;
2021
2122use crate :: {
2223 db:: DefDatabase ,
@@ -154,15 +155,15 @@ impl Attrs {
154155
155156 pub fn has_doc_hidden ( & self ) -> bool {
156157 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)
159160 } )
160161 }
161162
162163 pub fn has_doc_notable_trait ( & self ) -> bool {
163164 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)
166167 } )
167168 }
168169
@@ -245,8 +246,8 @@ impl From<DocAtom> for DocExpr {
245246}
246247
247248impl 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 )
250251 }
251252
252253 pub fn aliases ( & self ) -> & [ Symbol ] {
@@ -260,62 +261,47 @@ impl DocExpr {
260261 }
261262}
262263
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 > {
264265 let name = match it. next ( ) {
265266 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 ( ) ,
267268 Some ( _) => return Some ( DocExpr :: Invalid ) ,
268269 } ;
269270
270271 // 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 {
275277 symbol : text,
276278 kind : tt:: LitKind :: Str ,
277279 ..
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 ( ) ,
283281 _ => return Some ( DocExpr :: Invalid ) ,
284282 }
285283 }
286- Some ( tt :: TokenTree :: Subtree ( subtree ) ) => {
284+ Some ( TtElement :: Subtree ( _ , subtree_iter ) ) => {
287285 it. next ( ) ;
288- let subs = parse_comma_sep ( subtree ) ;
286+ let subs = parse_comma_sep ( subtree_iter ) ;
289287 match & name {
290288 s if * s == sym:: alias => DocExpr :: Alias ( subs) ,
291289 _ => DocExpr :: Invalid ,
292290 }
293291 }
294292 _ => DocAtom :: Flag ( name) . into ( ) ,
295293 } ;
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- }
303294 Some ( ret)
304295}
305296
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 ( )
319305}
320306
321307impl AttrsWithOwner {
@@ -563,7 +549,7 @@ pub struct AttrQuery<'attr> {
563549}
564550
565551impl < ' 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 > {
567553 self . attrs ( ) . filter_map ( |attr| attr. token_tree_value ( ) )
568554 }
569555
@@ -596,12 +582,12 @@ impl<'attr> AttrQuery<'attr> {
596582 /// ```
597583 pub fn find_string_value_in_tt ( self , key : & ' attr Symbol ) -> Option < & ' attr str > {
598584 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) )
601587 . nth ( 2 ) ;
602588
603589 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 ( ) ) ,
605591 _ => None
606592 }
607593 } )
0 commit comments