1
1
//! Conversions between [`SyntaxNode`] and [`tt::TokenTree`].
2
2
3
+ use std:: fmt;
4
+
3
5
use rustc_hash:: { FxHashMap , FxHashSet } ;
4
6
use span:: { SpanAnchor , SpanData , SpanMap } ;
5
7
use stdx:: { never, non_empty_vec:: NonEmptyVec } ;
@@ -9,30 +11,27 @@ use syntax::{
9
11
SyntaxKind :: * ,
10
12
SyntaxNode , SyntaxToken , SyntaxTreeBuilder , TextRange , TextSize , WalkEvent , T ,
11
13
} ;
12
- use tt:: {
13
- buffer:: { Cursor , TokenBuffer } ,
14
- Span ,
15
- } ;
14
+ use tt:: buffer:: { Cursor , TokenBuffer } ;
16
15
17
16
use crate :: { to_parser_input:: to_parser_input, tt_iter:: TtIter } ;
18
17
19
18
#[ cfg( test) ]
20
19
mod tests;
21
20
22
- pub trait SpanMapper < S : Span > {
21
+ pub trait SpanMapper < S > {
23
22
fn span_for ( & self , range : TextRange ) -> S ;
24
23
}
25
24
26
25
impl < S > SpanMapper < SpanData < S > > for SpanMap < S >
27
26
where
28
- SpanData < S > : Span ,
27
+ SpanData < S > : Copy ,
29
28
{
30
29
fn span_for ( & self , range : TextRange ) -> SpanData < S > {
31
30
self . span_at ( range. start ( ) )
32
31
}
33
32
}
34
33
35
- impl < S : Span , SM : SpanMapper < S > > SpanMapper < S > for & SM {
34
+ impl < S : Copy , SM : SpanMapper < S > > SpanMapper < S > for & SM {
36
35
fn span_for ( & self , range : TextRange ) -> S {
37
36
SM :: span_for ( self , range)
38
37
}
@@ -78,8 +77,7 @@ pub fn syntax_node_to_token_tree<Ctx, SpanMap>(
78
77
span : SpanData < Ctx > ,
79
78
) -> tt:: Subtree < SpanData < Ctx > >
80
79
where
81
- SpanData < Ctx > : Span ,
82
- Ctx : Copy ,
80
+ SpanData < Ctx > : Copy + fmt:: Debug ,
83
81
SpanMap : SpanMapper < SpanData < Ctx > > ,
84
82
{
85
83
let mut c = Converter :: new ( node, map, Default :: default ( ) , Default :: default ( ) , span) ;
@@ -98,8 +96,7 @@ pub fn syntax_node_to_token_tree_modified<Ctx, SpanMap>(
98
96
) -> tt:: Subtree < SpanData < Ctx > >
99
97
where
100
98
SpanMap : SpanMapper < SpanData < Ctx > > ,
101
- SpanData < Ctx > : Span ,
102
- Ctx : Copy ,
99
+ SpanData < Ctx > : Copy + fmt:: Debug ,
103
100
{
104
101
let mut c = Converter :: new ( node, map, append, remove, call_site) ;
105
102
convert_tokens ( & mut c)
@@ -124,8 +121,7 @@ pub fn token_tree_to_syntax_node<Ctx>(
124
121
entry_point : parser:: TopEntryPoint ,
125
122
) -> ( Parse < SyntaxNode > , SpanMap < Ctx > )
126
123
where
127
- SpanData < Ctx > : Span ,
128
- Ctx : Copy ,
124
+ SpanData < Ctx > : Copy + fmt:: Debug ,
129
125
{
130
126
let buffer = match tt {
131
127
tt:: Subtree {
@@ -161,7 +157,7 @@ pub fn parse_to_token_tree<Ctx>(
161
157
text : & str ,
162
158
) -> Option < tt:: Subtree < SpanData < Ctx > > >
163
159
where
164
- SpanData < Ctx > : Span ,
160
+ SpanData < Ctx > : Copy + fmt :: Debug ,
165
161
Ctx : Copy ,
166
162
{
167
163
let lexed = parser:: LexedStr :: new ( text) ;
@@ -175,7 +171,7 @@ where
175
171
/// Convert a string to a `TokenTree`. The passed span will be used for all spans of the produced subtree.
176
172
pub fn parse_to_token_tree_static_span < S > ( span : S , text : & str ) -> Option < tt:: Subtree < S > >
177
173
where
178
- S : Span ,
174
+ S : Copy + fmt :: Debug ,
179
175
{
180
176
let lexed = parser:: LexedStr :: new ( text) ;
181
177
if lexed. errors ( ) . next ( ) . is_some ( ) {
@@ -186,11 +182,10 @@ where
186
182
}
187
183
188
184
/// Split token tree with separate expr: $($e:expr)SEP*
189
- pub fn parse_exprs_with_sep < S : Span > (
190
- tt : & tt:: Subtree < S > ,
191
- sep : char ,
192
- span : S ,
193
- ) -> Vec < tt:: Subtree < S > > {
185
+ pub fn parse_exprs_with_sep < S > ( tt : & tt:: Subtree < S > , sep : char , span : S ) -> Vec < tt:: Subtree < S > >
186
+ where
187
+ S : Copy + fmt:: Debug ,
188
+ {
194
189
if tt. token_trees . is_empty ( ) {
195
190
return Vec :: new ( ) ;
196
191
}
@@ -226,7 +221,8 @@ pub fn parse_exprs_with_sep<S: Span>(
226
221
fn convert_tokens < S , C > ( conv : & mut C ) -> tt:: Subtree < S >
227
222
where
228
223
C : TokenConverter < S > ,
229
- S : Span ,
224
+ S : Copy + fmt:: Debug ,
225
+ C :: Token : fmt:: Debug ,
230
226
{
231
227
let entry = tt:: SubtreeBuilder {
232
228
delimiter : tt:: Delimiter :: invisible_spanned ( conv. call_site ( ) ) ,
@@ -485,7 +481,7 @@ struct StaticRawConverter<'a, S> {
485
481
span : S ,
486
482
}
487
483
488
- trait SrcToken < Ctx , S > : std :: fmt :: Debug {
484
+ trait SrcToken < Ctx , S > {
489
485
fn kind ( & self , ctx : & Ctx ) -> SyntaxKind ;
490
486
491
487
fn to_char ( & self , ctx : & Ctx ) -> Option < char > ;
@@ -525,7 +521,7 @@ impl<S, Ctx> SrcToken<RawConverter<'_, Ctx>, S> for usize {
525
521
}
526
522
}
527
523
528
- impl < S : Span > SrcToken < StaticRawConverter < ' _ , S > , S > for usize {
524
+ impl < S : Copy > SrcToken < StaticRawConverter < ' _ , S > , S > for usize {
529
525
fn kind ( & self , ctx : & StaticRawConverter < ' _ , S > ) -> SyntaxKind {
530
526
ctx. lexed . kind ( * self )
531
527
}
@@ -541,7 +537,7 @@ impl<S: Span> SrcToken<StaticRawConverter<'_, S>, S> for usize {
541
537
542
538
impl < Ctx : Copy > TokenConverter < SpanData < Ctx > > for RawConverter < ' _ , Ctx >
543
539
where
544
- SpanData < Ctx > : Span ,
540
+ SpanData < Ctx > : Copy ,
545
541
{
546
542
type Token = usize ;
547
543
@@ -584,7 +580,7 @@ where
584
580
585
581
impl < S > TokenConverter < S > for StaticRawConverter < ' _ , S >
586
582
where
587
- S : Span ,
583
+ S : Copy ,
588
584
{
589
585
type Token = usize ;
590
586
@@ -709,7 +705,7 @@ impl<S> SynToken<S> {
709
705
}
710
706
}
711
707
712
- impl < SpanMap , S : std :: fmt :: Debug > SrcToken < Converter < SpanMap , S > , S > for SynToken < S > {
708
+ impl < SpanMap , S > SrcToken < Converter < SpanMap , S > , S > for SynToken < S > {
713
709
fn kind ( & self , _ctx : & Converter < SpanMap , S > ) -> SyntaxKind {
714
710
match self {
715
711
SynToken :: Ordinary ( token) => token. kind ( ) ,
@@ -748,7 +744,7 @@ impl<SpanMap, S: std::fmt::Debug> SrcToken<Converter<SpanMap, S>, S> for SynToke
748
744
749
745
impl < S , SpanMap > TokenConverter < S > for Converter < SpanMap , S >
750
746
where
751
- S : Span ,
747
+ S : Copy ,
752
748
SpanMap : SpanMapper < S > ,
753
749
{
754
750
type Token = SynToken < S > ;
@@ -828,7 +824,7 @@ where
828
824
829
825
struct TtTreeSink < ' a , Ctx >
830
826
where
831
- SpanData < Ctx > : Span ,
827
+ SpanData < Ctx > : Copy ,
832
828
{
833
829
buf : String ,
834
830
cursor : Cursor < ' a , SpanData < Ctx > > ,
@@ -839,7 +835,7 @@ where
839
835
840
836
impl < ' a , Ctx > TtTreeSink < ' a , Ctx >
841
837
where
842
- SpanData < Ctx > : Span ,
838
+ SpanData < Ctx > : Copy ,
843
839
{
844
840
fn new ( cursor : Cursor < ' a , SpanData < Ctx > > ) -> Self {
845
841
TtTreeSink {
@@ -871,7 +867,7 @@ fn delim_to_str(d: tt::DelimiterKind, closing: bool) -> Option<&'static str> {
871
867
872
868
impl < Ctx > TtTreeSink < ' _ , Ctx >
873
869
where
874
- SpanData < Ctx > : Span ,
870
+ SpanData < Ctx > : Copy ,
875
871
{
876
872
/// Parses a float literal as if it was a one to two name ref nodes with a dot inbetween.
877
873
/// This occurs when a float literal is used as a field access.
0 commit comments