|
4 | 4 | use super::StringReader; |
5 | 5 | use errors::{Applicability, DiagnosticBuilder}; |
6 | 6 | use syntax_pos::{BytePos, Pos, Span, NO_EXPANSION}; |
| 7 | +use crate::parse::token; |
7 | 8 |
|
8 | 9 | #[rustfmt::skip] // for line breaks |
9 | 10 | const UNICODE_ARRAY: &[(char, &str, char)] = &[ |
@@ -297,53 +298,53 @@ const UNICODE_ARRAY: &[(char, &str, char)] = &[ |
297 | 298 | ('>', "Fullwidth Greater-Than Sign", '>'), |
298 | 299 | ]; |
299 | 300 |
|
300 | | -const ASCII_ARRAY: &[(char, &str)] = &[ |
301 | | - (' ', "Space"), |
302 | | - ('_', "Underscore"), |
303 | | - ('-', "Minus/Hyphen"), |
304 | | - (',', "Comma"), |
305 | | - (';', "Semicolon"), |
306 | | - (':', "Colon"), |
307 | | - ('!', "Exclamation Mark"), |
308 | | - ('?', "Question Mark"), |
309 | | - ('.', "Period"), |
310 | | - ('\'', "Single Quote"), |
311 | | - ('"', "Quotation Mark"), |
312 | | - ('(', "Left Parenthesis"), |
313 | | - (')', "Right Parenthesis"), |
314 | | - ('[', "Left Square Bracket"), |
315 | | - (']', "Right Square Bracket"), |
316 | | - ('{', "Left Curly Brace"), |
317 | | - ('}', "Right Curly Brace"), |
318 | | - ('*', "Asterisk"), |
319 | | - ('/', "Slash"), |
320 | | - ('\\', "Backslash"), |
321 | | - ('&', "Ampersand"), |
322 | | - ('+', "Plus Sign"), |
323 | | - ('<', "Less-Than Sign"), |
324 | | - ('=', "Equals Sign"), |
325 | | - ('>', "Greater-Than Sign"), |
| 301 | +const ASCII_ARRAY: &[(char, &str, Option<token::TokenKind>)] = &[ |
| 302 | + (' ', "Space", Some(token::Whitespace)), |
| 303 | + ('_', "Underscore", None), |
| 304 | + ('-', "Minus/Hyphen", Some(token::BinOp(token::Minus))), |
| 305 | + (',', "Comma", Some(token::Comma)), |
| 306 | + (';', "Semicolon", Some(token::Semi)), |
| 307 | + (':', "Colon", Some(token::Colon)), |
| 308 | + ('!', "Exclamation Mark", Some(token::Not)), |
| 309 | + ('?', "Question Mark", Some(token::Question)), |
| 310 | + ('.', "Period", Some(token::Dot)), |
| 311 | + ('\'', "Single Quote", None), // Literals are already lexed by this point, so we can't recover |
| 312 | + ('"', "Quotation Mark", None), // gracefully just by spitting the correct token out. |
| 313 | + ('(', "Left Parenthesis", Some(token::OpenDelim(token::Paren))), |
| 314 | + (')', "Right Parenthesis", Some(token::CloseDelim(token::Paren))), |
| 315 | + ('[', "Left Square Bracket", Some(token::OpenDelim(token::Bracket))), |
| 316 | + (']', "Right Square Bracket", Some(token::CloseDelim(token::Bracket))), |
| 317 | + ('{', "Left Curly Brace", Some(token::OpenDelim(token::Brace))), |
| 318 | + ('}', "Right Curly Brace", Some(token::CloseDelim(token::Brace))), |
| 319 | + ('*', "Asterisk", Some(token::BinOp(token::Star))), |
| 320 | + ('/', "Slash", Some(token::BinOp(token::Slash))), |
| 321 | + ('\\', "Backslash", None), |
| 322 | + ('&', "Ampersand", Some(token::BinOp(token::And))), |
| 323 | + ('+', "Plus Sign", Some(token::BinOp(token::Plus))), |
| 324 | + ('<', "Less-Than Sign", Some(token::Lt)), |
| 325 | + ('=', "Equals Sign", Some(token::Eq)), |
| 326 | + ('>', "Greater-Than Sign", Some(token::Gt)), |
326 | 327 | ]; |
327 | 328 |
|
328 | 329 | crate fn check_for_substitution<'a>( |
329 | 330 | reader: &StringReader<'a>, |
330 | 331 | pos: BytePos, |
331 | 332 | ch: char, |
332 | 333 | err: &mut DiagnosticBuilder<'a>, |
333 | | -) -> bool { |
| 334 | +) -> Option<token::TokenKind> { |
334 | 335 | let (u_name, ascii_char) = match UNICODE_ARRAY.iter().find(|&&(c, _, _)| c == ch) { |
335 | 336 | Some(&(_u_char, u_name, ascii_char)) => (u_name, ascii_char), |
336 | | - None => return false, |
| 337 | + None => return None, |
337 | 338 | }; |
338 | 339 |
|
339 | 340 | let span = Span::new(pos, pos + Pos::from_usize(ch.len_utf8()), NO_EXPANSION); |
340 | 341 |
|
341 | | - let ascii_name = match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) { |
342 | | - Some((_ascii_char, ascii_name)) => ascii_name, |
| 342 | + let (ascii_name, token) = match ASCII_ARRAY.iter().find(|&&(c, _, _)| c == ascii_char) { |
| 343 | + Some((_ascii_char, ascii_name, token)) => (ascii_name, token), |
343 | 344 | None => { |
344 | 345 | let msg = format!("substitution character not found for '{}'", ch); |
345 | 346 | reader.sess.span_diagnostic.span_bug_no_panic(span, &msg); |
346 | | - return false; |
| 347 | + return None; |
347 | 348 | } |
348 | 349 | }; |
349 | 350 |
|
@@ -371,7 +372,7 @@ crate fn check_for_substitution<'a>( |
371 | 372 | ); |
372 | 373 | err.span_suggestion(span, &msg, ascii_char.to_string(), Applicability::MaybeIncorrect); |
373 | 374 | } |
374 | | - true |
| 375 | + token.clone() |
375 | 376 | } |
376 | 377 |
|
377 | 378 | /// Extract string if found at current position with given delimiters |
|
0 commit comments