@@ -13,7 +13,7 @@ use tt::{
13
13
Span , SpanData , SyntaxContext ,
14
14
} ;
15
15
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 } ;
17
17
18
18
#[ cfg( test) ]
19
19
mod tests;
@@ -22,7 +22,7 @@ pub trait SpanMapper<S: Span> {
22
22
fn span_for ( & self , range : TextRange ) -> S ;
23
23
}
24
24
25
- impl < S : Span > SpanMapper < S > for TokenMap < S > {
25
+ impl < S : Span > SpanMapper < S > for SpanMap < S > {
26
26
fn span_for ( & self , range : TextRange ) -> S {
27
27
self . span_at ( range. start ( ) )
28
28
}
@@ -34,10 +34,12 @@ impl<S: Span, SM: SpanMapper<S>> SpanMapper<S> for &SM {
34
34
}
35
35
}
36
36
37
+ /// Dummy things for testing where spans don't matter.
37
38
pub ( crate ) mod dummy_test_span_utils {
38
39
use super :: * ;
39
40
40
41
pub type DummyTestSpanData = tt:: SpanData < DummyTestSpanAnchor , DummyTestSyntaxContext > ;
42
+ pub const DUMMY : DummyTestSpanData = DummyTestSpanData :: DUMMY ;
41
43
42
44
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
43
45
pub struct DummyTestSpanAnchor ;
@@ -62,9 +64,8 @@ pub(crate) mod dummy_test_span_utils {
62
64
}
63
65
}
64
66
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.
68
69
pub fn syntax_node_to_token_tree < Anchor , Ctx , SpanMap > (
69
70
node : & SyntaxNode ,
70
71
map : SpanMap ,
79
80
convert_tokens ( & mut c)
80
81
}
81
82
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.
82
86
pub fn syntax_node_to_token_tree_modified < Anchor , Ctx , SpanMap > (
83
87
node : & SyntaxNode ,
84
88
map : SpanMap ,
@@ -107,10 +111,12 @@ where
107
111
// * AssocItems(SmallVec<[ast::AssocItem; 1]>)
108
112
// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
109
113
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.
110
116
pub fn token_tree_to_syntax_node < Anchor , Ctx > (
111
117
tt : & tt:: Subtree < SpanData < Anchor , Ctx > > ,
112
118
entry_point : parser:: TopEntryPoint ,
113
- ) -> ( Parse < SyntaxNode > , TokenMap < SpanData < Anchor , Ctx > > )
119
+ ) -> ( Parse < SyntaxNode > , SpanMap < SpanData < Anchor , Ctx > > )
114
120
where
115
121
SpanData < Anchor , Ctx > : Span ,
116
122
Anchor : Copy ,
@@ -142,7 +148,8 @@ where
142
148
tree_sink. finish ( )
143
149
}
144
150
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.
146
153
pub fn parse_to_token_tree < Anchor , Ctx > (
147
154
anchor : Anchor ,
148
155
ctx : Ctx ,
@@ -161,7 +168,7 @@ where
161
168
Some ( convert_tokens ( & mut conv) )
162
169
}
163
170
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.
165
172
pub fn parse_to_token_tree_static_span < S > ( span : S , text : & str ) -> Option < tt:: Subtree < S > >
166
173
where
167
174
S : Span ,
@@ -798,7 +805,7 @@ where
798
805
cursor : Cursor < ' a , SpanData < Anchor , Ctx > > ,
799
806
text_pos : TextSize ,
800
807
inner : SyntaxTreeBuilder ,
801
- token_map : TokenMap < SpanData < Anchor , Ctx > > ,
808
+ token_map : SpanMap < SpanData < Anchor , Ctx > > ,
802
809
}
803
810
804
811
impl < ' a , Anchor , Ctx > TtTreeSink < ' a , Anchor , Ctx >
@@ -811,11 +818,11 @@ where
811
818
cursor,
812
819
text_pos : 0 . into ( ) ,
813
820
inner : SyntaxTreeBuilder :: default ( ) ,
814
- token_map : TokenMap :: empty ( ) ,
821
+ token_map : SpanMap :: empty ( ) ,
815
822
}
816
823
}
817
824
818
- fn finish ( mut self ) -> ( Parse < SyntaxNode > , TokenMap < SpanData < Anchor , Ctx > > ) {
825
+ fn finish ( mut self ) -> ( Parse < SyntaxNode > , SpanMap < SpanData < Anchor , Ctx > > ) {
819
826
self . token_map . finish ( ) ;
820
827
( self . inner . finish ( ) , self . token_map )
821
828
}
0 commit comments