@@ -15,10 +15,10 @@ use crate::hir;
15
15
use crate :: hir:: { PatKind , GenericBound , TraitBoundModifier , RangeEnd } ;
16
16
use crate :: hir:: { GenericParam , GenericParamKind , GenericArg } ;
17
17
18
+ use std:: ascii;
18
19
use std:: borrow:: Cow ;
19
20
use std:: cell:: Cell ;
20
21
use std:: io:: { self , Write , Read } ;
21
- use std:: iter:: Peekable ;
22
22
use std:: vec;
23
23
24
24
pub enum AnnNode < ' a > {
@@ -76,7 +76,6 @@ pub struct State<'a> {
76
76
pub s : pp:: Printer < ' a > ,
77
77
cm : Option < & ' a SourceMap > ,
78
78
comments : Option < Vec < comments:: Comment > > ,
79
- literals : Peekable < vec:: IntoIter < comments:: Literal > > ,
80
79
cur_cmnt : usize ,
81
80
boxes : Vec < pp:: Breaks > ,
82
81
ann : & ' a ( dyn PpAnn + ' a ) ,
@@ -98,14 +97,6 @@ impl<'a> PrintState<'a> for State<'a> {
98
97
fn cur_cmnt ( & mut self ) -> & mut usize {
99
98
& mut self . cur_cmnt
100
99
}
101
-
102
- fn cur_lit ( & mut self ) -> Option < & comments:: Literal > {
103
- self . literals . peek ( )
104
- }
105
-
106
- fn bump_lit ( & mut self ) -> Option < comments:: Literal > {
107
- self . literals . next ( )
108
- }
109
100
}
110
101
111
102
#[ allow( non_upper_case_globals) ]
@@ -116,18 +107,16 @@ pub const default_columns: usize = 78;
116
107
117
108
118
109
/// Requires you to pass an input filename and reader so that
119
- /// it can scan the input text for comments and literals to
120
- /// copy forward.
110
+ /// it can scan the input text for comments to copy forward.
121
111
pub fn print_crate < ' a > ( cm : & ' a SourceMap ,
122
112
sess : & ParseSess ,
123
113
krate : & hir:: Crate ,
124
114
filename : FileName ,
125
115
input : & mut dyn Read ,
126
116
out : Box < dyn Write + ' a > ,
127
- ann : & ' a dyn PpAnn ,
128
- is_expanded : bool )
117
+ ann : & ' a dyn PpAnn )
129
118
-> io:: Result < ( ) > {
130
- let mut s = State :: new_from_input ( cm, sess, filename, input, out, ann, is_expanded ) ;
119
+ let mut s = State :: new_from_input ( cm, sess, filename, input, out, ann) ;
131
120
132
121
// When printing the AST, we sometimes need to inject `#[no_std]` here.
133
122
// Since you can't compile the HIR, it's not necessary.
@@ -143,36 +132,21 @@ impl<'a> State<'a> {
143
132
filename : FileName ,
144
133
input : & mut dyn Read ,
145
134
out : Box < dyn Write + ' a > ,
146
- ann : & ' a dyn PpAnn ,
147
- is_expanded : bool )
135
+ ann : & ' a dyn PpAnn )
148
136
-> State < ' a > {
149
- let ( cmnts, lits) = comments:: gather_comments_and_literals ( sess, filename, input) ;
150
-
151
- State :: new ( cm,
152
- out,
153
- ann,
154
- Some ( cmnts) ,
155
- // If the code is post expansion, don't use the table of
156
- // literals, since it doesn't correspond with the literals
157
- // in the AST anymore.
158
- if is_expanded {
159
- None
160
- } else {
161
- Some ( lits)
162
- } )
137
+ let comments = comments:: gather_comments ( sess, filename, input) ;
138
+ State :: new ( cm, out, ann, Some ( comments) )
163
139
}
164
140
165
141
pub fn new ( cm : & ' a SourceMap ,
166
142
out : Box < dyn Write + ' a > ,
167
143
ann : & ' a dyn PpAnn ,
168
- comments : Option < Vec < comments:: Comment > > ,
169
- literals : Option < Vec < comments:: Literal > > )
144
+ comments : Option < Vec < comments:: Comment > > )
170
145
-> State < ' a > {
171
146
State {
172
147
s : pp:: mk_printer ( out, default_columns) ,
173
148
cm : Some ( cm) ,
174
149
comments,
175
- literals : literals. unwrap_or_default ( ) . into_iter ( ) . peekable ( ) ,
176
150
cur_cmnt : 0 ,
177
151
boxes : Vec :: new ( ) ,
178
152
ann,
@@ -189,7 +163,6 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
189
163
s : pp:: mk_printer ( Box :: new ( & mut wr) , default_columns) ,
190
164
cm : None ,
191
165
comments : None ,
192
- literals : vec ! [ ] . into_iter ( ) . peekable ( ) ,
193
166
cur_cmnt : 0 ,
194
167
boxes : Vec :: new ( ) ,
195
168
ann,
@@ -1335,6 +1308,61 @@ impl<'a> State<'a> {
1335
1308
self . print_expr_maybe_paren ( expr, parser:: PREC_PREFIX )
1336
1309
}
1337
1310
1311
+ fn print_literal ( & mut self , lit : & hir:: Lit ) -> io:: Result < ( ) > {
1312
+ self . maybe_print_comment ( lit. span . lo ( ) ) ?;
1313
+ match lit. node {
1314
+ hir:: LitKind :: Str ( st, style) => self . print_string ( & st. as_str ( ) , style) ,
1315
+ hir:: LitKind :: Err ( st) => {
1316
+ let st = st. as_str ( ) . escape_debug ( ) . to_string ( ) ;
1317
+ let mut res = String :: with_capacity ( st. len ( ) + 2 ) ;
1318
+ res. push ( '\'' ) ;
1319
+ res. push_str ( & st) ;
1320
+ res. push ( '\'' ) ;
1321
+ self . writer ( ) . word ( res)
1322
+ }
1323
+ hir:: LitKind :: Byte ( byte) => {
1324
+ let mut res = String :: from ( "b'" ) ;
1325
+ res. extend ( ascii:: escape_default ( byte) . map ( |c| c as char ) ) ;
1326
+ res. push ( '\'' ) ;
1327
+ self . writer ( ) . word ( res)
1328
+ }
1329
+ hir:: LitKind :: Char ( ch) => {
1330
+ let mut res = String :: from ( "'" ) ;
1331
+ res. extend ( ch. escape_default ( ) ) ;
1332
+ res. push ( '\'' ) ;
1333
+ self . writer ( ) . word ( res)
1334
+ }
1335
+ hir:: LitKind :: Int ( i, t) => {
1336
+ match t {
1337
+ ast:: LitIntType :: Signed ( st) => {
1338
+ self . writer ( ) . word ( st. val_to_string ( i as i128 ) )
1339
+ }
1340
+ ast:: LitIntType :: Unsigned ( ut) => {
1341
+ self . writer ( ) . word ( ut. val_to_string ( i) )
1342
+ }
1343
+ ast:: LitIntType :: Unsuffixed => {
1344
+ self . writer ( ) . word ( i. to_string ( ) )
1345
+ }
1346
+ }
1347
+ }
1348
+ hir:: LitKind :: Float ( ref f, t) => {
1349
+ self . writer ( ) . word ( format ! ( "{}{}" , & f, t. ty_to_string( ) ) )
1350
+ }
1351
+ hir:: LitKind :: FloatUnsuffixed ( ref f) => self . writer ( ) . word ( f. as_str ( ) . to_string ( ) ) ,
1352
+ hir:: LitKind :: Bool ( val) => {
1353
+ if val { self . writer ( ) . word ( "true" ) } else { self . writer ( ) . word ( "false" ) }
1354
+ }
1355
+ hir:: LitKind :: ByteStr ( ref v) => {
1356
+ let mut escaped: String = String :: new ( ) ;
1357
+ for & ch in v. iter ( ) {
1358
+ escaped. extend ( ascii:: escape_default ( ch)
1359
+ . map ( |c| c as char ) ) ;
1360
+ }
1361
+ self . writer ( ) . word ( format ! ( "b\" {}\" " , escaped) )
1362
+ }
1363
+ }
1364
+ }
1365
+
1338
1366
pub fn print_expr ( & mut self , expr : & hir:: Expr ) -> io:: Result < ( ) > {
1339
1367
self . maybe_print_comment ( expr. span . lo ( ) ) ?;
1340
1368
self . print_outer_attributes ( & expr. attrs ) ?;
0 commit comments