@@ -162,14 +162,41 @@ impl<T: PartialEq, R: PartialEq> PartialEq for Info<T, R> {
162
162
impl < T : fmt:: Display , R : fmt:: Display > fmt:: Display for Info < T , R > {
163
163
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
164
164
match * self {
165
- Info :: Token ( ref c) => write ! ( f , "`{}`" , c ) ,
166
- Info :: Range ( ref c) => write ! ( f , "`{}`" , c ) ,
165
+ Info :: Token ( ref c) => escaped_token ( c , '`' , f ) ,
166
+ Info :: Range ( ref c) => escaped_token ( c , '`' , f ) ,
167
167
Info :: Owned ( ref s) => write ! ( f, "{}" , s) ,
168
168
Info :: Static ( s) => write ! ( f, "{}" , s) ,
169
169
}
170
170
}
171
171
}
172
172
173
+ fn escaped_token < T : fmt:: Display > (
174
+ token : & T ,
175
+ quote : char ,
176
+ f : & mut fmt:: Formatter < ' _ > ,
177
+ ) -> fmt:: Result {
178
+ let token = token. to_string ( ) ;
179
+ write ! ( f, "{}" , quote) ?;
180
+ for c in token. chars ( ) {
181
+ match c {
182
+ '\t' | '\n' | '\r' => {
183
+ write ! ( f, "{}" , c. escape_debug( ) ) ?;
184
+ }
185
+ c if c. is_ascii_control ( ) => {
186
+ write ! ( f, "{}" , c. escape_debug( ) ) ?;
187
+ }
188
+ c if c == quote => {
189
+ write ! ( f, "\\ {}" , quote) ?;
190
+ }
191
+ _ => {
192
+ write ! ( f, "{}" , c) ?;
193
+ }
194
+ }
195
+ }
196
+ write ! ( f, "{}" , quote) ?;
197
+ Ok ( ( ) )
198
+ }
199
+
173
200
impl < R > From < char > for Info < char , R > {
174
201
fn from ( s : char ) -> Info < char , R > {
175
202
Info :: Token ( s)
0 commit comments