@@ -13,7 +13,7 @@ use tt::{
1313 Span , SpanData , SyntaxContext ,
1414} ;
1515
16- use crate :: { to_parser_input:: to_parser_input, tt_iter:: TtIter , TokenMap } ;
16+ use crate :: { to_parser_input:: to_parser_input, tt_iter:: TtIter , SpanMap } ;
1717
1818#[ cfg( test) ]
1919mod tests;
@@ -22,7 +22,7 @@ pub trait SpanMapper<S: Span> {
2222 fn span_for ( & self , range : TextRange ) -> S ;
2323}
2424
25- impl < S : Span > SpanMapper < S > for TokenMap < S > {
25+ impl < S : Span > SpanMapper < S > for SpanMap < S > {
2626 fn span_for ( & self , range : TextRange ) -> S {
2727 self . span_at ( range. start ( ) )
2828 }
@@ -34,10 +34,12 @@ impl<S: Span, SM: SpanMapper<S>> SpanMapper<S> for &SM {
3434 }
3535}
3636
37+ /// Dummy things for testing where spans don't matter.
3738pub ( crate ) mod dummy_test_span_utils {
3839 use super :: * ;
3940
4041 pub type DummyTestSpanData = tt:: SpanData < DummyTestSpanAnchor , DummyTestSyntaxContext > ;
42+ pub const DUMMY : DummyTestSpanData = DummyTestSpanData :: DUMMY ;
4143
4244 #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
4345 pub struct DummyTestSpanAnchor ;
@@ -62,9 +64,8 @@ pub(crate) mod dummy_test_span_utils {
6264 }
6365}
6466
65- /// Convert the syntax node to a `TokenTree` (what macro
66- /// will consume).
67- /// FIXME: Flesh out the doc comment more thoroughly
67+ /// Converts a syntax tree to a [`tt::Subtree`] using the provided span map to populate the
68+ /// subtree's spans.
6869pub fn syntax_node_to_token_tree < Anchor , Ctx , SpanMap > (
6970 node : & SyntaxNode ,
7071 map : SpanMap ,
7980 convert_tokens ( & mut c)
8081}
8182
83+ /// Converts a syntax tree to a [`tt::Subtree`] using the provided span map to populate the
84+ /// subtree's spans. Additionally using the append and remove parameters, the additional tokens can
85+ /// be injected or hidden from the output.
8286pub fn syntax_node_to_token_tree_modified < Anchor , Ctx , SpanMap > (
8387 node : & SyntaxNode ,
8488 map : SpanMap ,
@@ -107,10 +111,12 @@ where
107111// * AssocItems(SmallVec<[ast::AssocItem; 1]>)
108112// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
109113
114+ /// Converts a [`tt::Subtree`] back to a [`SyntaxNode`].
115+ /// The produced `SpanMap` contains a mapping from the syntax nodes offsets to the subtree's spans.
110116pub fn token_tree_to_syntax_node < Anchor , Ctx > (
111117 tt : & tt:: Subtree < SpanData < Anchor , Ctx > > ,
112118 entry_point : parser:: TopEntryPoint ,
113- ) -> ( Parse < SyntaxNode > , TokenMap < SpanData < Anchor , Ctx > > )
119+ ) -> ( Parse < SyntaxNode > , SpanMap < SpanData < Anchor , Ctx > > )
114120where
115121 SpanData < Anchor , Ctx > : Span ,
116122 Anchor : Copy ,
@@ -142,7 +148,8 @@ where
142148 tree_sink. finish ( )
143149}
144150
145- /// Convert a string to a `TokenTree`
151+ /// Convert a string to a `TokenTree`. The spans of the subtree will be anchored to the provided
152+ /// anchor with the given context.
146153pub fn parse_to_token_tree < Anchor , Ctx > (
147154 anchor : Anchor ,
148155 ctx : Ctx ,
@@ -161,7 +168,7 @@ where
161168 Some ( convert_tokens ( & mut conv) )
162169}
163170
164- /// Convert a string to a `TokenTree`
171+ /// Convert a string to a `TokenTree`. The passed span will be used for all spans of the produced subtree.
165172pub fn parse_to_token_tree_static_span < S > ( span : S , text : & str ) -> Option < tt:: Subtree < S > >
166173where
167174 S : Span ,
@@ -798,7 +805,7 @@ where
798805 cursor : Cursor < ' a , SpanData < Anchor , Ctx > > ,
799806 text_pos : TextSize ,
800807 inner : SyntaxTreeBuilder ,
801- token_map : TokenMap < SpanData < Anchor , Ctx > > ,
808+ token_map : SpanMap < SpanData < Anchor , Ctx > > ,
802809}
803810
804811impl < ' a , Anchor , Ctx > TtTreeSink < ' a , Anchor , Ctx >
@@ -811,11 +818,11 @@ where
811818 cursor,
812819 text_pos : 0 . into ( ) ,
813820 inner : SyntaxTreeBuilder :: default ( ) ,
814- token_map : TokenMap :: empty ( ) ,
821+ token_map : SpanMap :: empty ( ) ,
815822 }
816823 }
817824
818- fn finish ( mut self ) -> ( Parse < SyntaxNode > , TokenMap < SpanData < Anchor , Ctx > > ) {
825+ fn finish ( mut self ) -> ( Parse < SyntaxNode > , SpanMap < SpanData < Anchor , Ctx > > ) {
819826 self . token_map . finish ( ) ;
820827 ( self . inner . finish ( ) , self . token_map )
821828 }
0 commit comments