1
1
use proc_macro:: Span ;
2
- use proc_macro2:: { Delimiter , Ident , Spacing , TokenStream , TokenTree , LineColumn } ;
2
+ use proc_macro2:: { Delimiter , Ident , Spacing , TokenStream , TokenTree } ;
3
3
use quote:: quote_spanned;
4
4
use std:: collections:: BTreeMap ;
5
5
use std:: fmt:: Write ;
@@ -8,7 +8,8 @@ pub struct EmbedPython {
8
8
pub python : String ,
9
9
pub variables : BTreeMap < String , Ident > ,
10
10
pub first_indent : Option < usize > ,
11
- pub loc : LineColumn ,
11
+ pub line : usize ,
12
+ pub column : usize ,
12
13
pub compile_time : bool ,
13
14
}
14
15
@@ -17,30 +18,31 @@ impl EmbedPython {
17
18
Self {
18
19
python : String :: new ( ) ,
19
20
variables : BTreeMap :: new ( ) ,
20
- loc : LineColumn { line : 1 , column : 0 } ,
21
+ line : 1 ,
22
+ column : 0 ,
21
23
first_indent : None ,
22
24
compile_time : false ,
23
25
}
24
26
}
25
27
26
- fn add_whitespace ( & mut self , span : Span , loc : LineColumn ) -> Result < ( ) , TokenStream > {
28
+ fn add_whitespace ( & mut self , span : Span , line : usize , column : usize ) -> Result < ( ) , TokenStream > {
27
29
#[ allow( clippy:: comparison_chain) ]
28
- if loc . line > self . loc . line {
29
- while loc . line > self . loc . line {
30
+ if line > self . line {
31
+ while line > self . line {
30
32
self . python . push ( '\n' ) ;
31
- self . loc . line += 1 ;
33
+ self . line += 1 ;
32
34
}
33
- let first_indent = * self . first_indent . get_or_insert ( loc . column ) ;
34
- let indent = loc . column . checked_sub ( first_indent) ;
35
+ let first_indent = * self . first_indent . get_or_insert ( column) ;
36
+ let indent = column. checked_sub ( first_indent) ;
35
37
let indent = indent. ok_or_else ( || quote_spanned ! ( span. into( ) => compile_error!{ "Invalid indentation" } ) ) ?;
36
38
for _ in 0 ..indent {
37
39
self . python . push ( ' ' ) ;
38
40
}
39
- self . loc . column = loc . column ;
40
- } else if loc . line == self . loc . line {
41
- while loc . column > self . loc . column {
41
+ self . column = column;
42
+ } else if line == self . line {
43
+ while column > self . column {
42
44
self . python . push ( ' ' ) ;
43
- self . loc . column += 1 ;
45
+ self . column += 1 ;
44
46
}
45
47
}
46
48
@@ -52,9 +54,7 @@ impl EmbedPython {
52
54
53
55
while let Some ( token) = tokens. next ( ) {
54
56
let span = token. span ( ) . unwrap ( ) ;
55
- let start_span = span. start ( ) ;
56
- let lc = LineColumn { line : start_span. line ( ) , column : start_span. column ( ) } ;
57
- self . add_whitespace ( span, lc) ?;
57
+ self . add_whitespace ( span, span. line ( ) , span. column ( ) ) ?;
58
58
59
59
match & token {
60
60
TokenTree :: Group ( x) => {
@@ -65,14 +65,12 @@ impl EmbedPython {
65
65
Delimiter :: None => ( "" , "" ) ,
66
66
} ;
67
67
self . python . push_str ( start) ;
68
- self . loc . column += start. len ( ) ;
68
+ self . column += start. len ( ) ;
69
69
self . add ( x. stream ( ) ) ?;
70
- let end_loc = token. span ( ) . unwrap ( ) . end ( ) ;
71
- let end_col = end_loc. column ( ) . saturating_sub ( end. len ( ) ) ;
72
- let elc = LineColumn { line : end_loc. line ( ) , column : end_col } ;
73
- self . add_whitespace ( span, elc) ?;
70
+ let end_span = token. span ( ) . unwrap ( ) . end ( ) ;
71
+ self . add_whitespace ( span, end_span. line ( ) , end_span. column ( ) . saturating_sub ( end. len ( ) ) ) ?;
74
72
self . python . push_str ( end) ;
75
- self . loc . column += end. len ( ) ;
73
+ self . column += end. len ( ) ;
76
74
}
77
75
TokenTree :: Punct ( x) => {
78
76
if !self . compile_time && x. as_char ( ) == '\'' && x. spacing ( ) == Spacing :: Joint {
@@ -83,34 +81,35 @@ impl EmbedPython {
83
81
} ;
84
82
let name_str = format ! ( "_RUST_{}" , name) ;
85
83
self . python . push_str ( & name_str) ;
86
- self . loc . column += name_str. chars ( ) . count ( ) - 6 + 1 ;
84
+ self . column += name_str. chars ( ) . count ( ) - 6 + 1 ;
87
85
self . variables . entry ( name_str) . or_insert ( name) ;
88
86
} else if x. as_char ( ) == '#' && x. spacing ( ) == Spacing :: Joint {
89
87
// Convert '##' to '//', because otherwise it's
90
88
// impossible to use the Python operators '//' and '//='.
91
89
match tokens. next ( ) {
92
90
Some ( TokenTree :: Punct ( ref p) ) if p. as_char ( ) == '#' => {
93
91
self . python . push_str ( "//" ) ;
94
- self . loc . column += 2 ;
92
+ self . column += 2 ;
95
93
}
96
94
Some ( TokenTree :: Punct ( p) ) => {
97
95
self . python . push ( x. as_char ( ) ) ;
98
96
self . python . push ( p. as_char ( ) ) ;
99
- self . loc . column += 2 ;
97
+ self . column += 2 ;
100
98
}
101
99
_ => {
102
100
unreachable ! ( ) ;
103
101
}
104
102
}
105
103
} else {
106
104
self . python . push ( x. as_char ( ) ) ;
107
- self . loc . column += 1 ;
105
+ self . column += 1 ;
108
106
}
109
107
}
110
108
TokenTree :: Ident ( x) => {
111
109
write ! ( & mut self . python, "{}" , x) . unwrap ( ) ;
112
110
let end_span = token. span ( ) . unwrap ( ) . end ( ) ;
113
- self . loc = LineColumn { line : end_span. line ( ) , column : end_span. column ( ) } ;
111
+ self . line = end_span. line ( ) ;
112
+ self . column = end_span. column ( ) ;
114
113
}
115
114
TokenTree :: Literal ( x) => {
116
115
let s = x. to_string ( ) ;
@@ -124,7 +123,8 @@ impl EmbedPython {
124
123
}
125
124
self . python += & s;
126
125
let end_span = token. span ( ) . unwrap ( ) . end ( ) ;
127
- self . loc = LineColumn { line : end_span. line ( ) , column : end_span. column ( ) } ;
126
+ self . line = end_span. line ( ) ;
127
+ self . column = end_span. column ( ) ;
128
128
}
129
129
}
130
130
}
0 commit comments