|
3 | 3 |
|
4 | 4 | use std::convert::TryInto; |
5 | 5 |
|
6 | | -use rustc_lexer::{LiteralKind as LK, RawStrError}; |
| 6 | +use rustc_lexer::RawStrError; |
7 | 7 |
|
8 | 8 | use crate::{ |
9 | 9 | SyntaxError, |
@@ -185,63 +185,77 @@ fn rustc_token_kind_to_syntax_kind( |
185 | 185 | return (syntax_kind, None); |
186 | 186 |
|
187 | 187 | fn match_literal_kind(kind: &rustc_lexer::LiteralKind) -> (SyntaxKind, Option<&'static str>) { |
188 | | - #[rustfmt::skip] |
| 188 | + let mut err = ""; |
189 | 189 | let syntax_kind = match *kind { |
190 | | - LK::Int { empty_int: false, .. } => INT_NUMBER, |
191 | | - LK::Int { empty_int: true, .. } => { |
192 | | - return (INT_NUMBER, Some("Missing digits after the integer base prefix")) |
| 190 | + rustc_lexer::LiteralKind::Int { empty_int, base: _ } => { |
| 191 | + if empty_int { |
| 192 | + err = "Missing digits after the integer base prefix"; |
| 193 | + } |
| 194 | + INT_NUMBER |
193 | 195 | } |
194 | | - |
195 | | - LK::Float { empty_exponent: false, .. } => FLOAT_NUMBER, |
196 | | - LK::Float { empty_exponent: true, .. } => { |
197 | | - return (FLOAT_NUMBER, Some("Missing digits after the exponent symbol")) |
| 196 | + rustc_lexer::LiteralKind::Float { empty_exponent, base: _ } => { |
| 197 | + if empty_exponent { |
| 198 | + err = "Missing digits after the exponent symbol"; |
| 199 | + } |
| 200 | + FLOAT_NUMBER |
198 | 201 | } |
199 | | - |
200 | | - LK::Char { terminated: true } => CHAR, |
201 | | - LK::Char { terminated: false } => { |
202 | | - return (CHAR, Some("Missing trailing `'` symbol to terminate the character literal")) |
| 202 | + rustc_lexer::LiteralKind::Char { terminated } => { |
| 203 | + if !terminated { |
| 204 | + err = "Missing trailing `'` symbol to terminate the character literal"; |
| 205 | + } |
| 206 | + CHAR |
203 | 207 | } |
204 | | - |
205 | | - LK::Byte { terminated: true } => BYTE, |
206 | | - LK::Byte { terminated: false } => { |
207 | | - return (BYTE, Some("Missing trailing `'` symbol to terminate the byte literal")) |
| 208 | + rustc_lexer::LiteralKind::Byte { terminated } => { |
| 209 | + if !terminated { |
| 210 | + err = "Missing trailing `'` symbol to terminate the byte literal"; |
| 211 | + } |
| 212 | + BYTE |
208 | 213 | } |
209 | | - |
210 | | - LK::Str { terminated: true } => STRING, |
211 | | - LK::Str { terminated: false } => { |
212 | | - return (STRING, Some("Missing trailing `\"` symbol to terminate the string literal")) |
| 214 | + rustc_lexer::LiteralKind::Str { terminated } => { |
| 215 | + if !terminated { |
| 216 | + err = "Missing trailing `\"` symbol to terminate the string literal"; |
| 217 | + } |
| 218 | + STRING |
213 | 219 | } |
214 | | - |
215 | | - |
216 | | - LK::ByteStr { terminated: true } => BYTE_STRING, |
217 | | - LK::ByteStr { terminated: false } => { |
218 | | - return (BYTE_STRING, Some("Missing trailing `\"` symbol to terminate the byte string literal")) |
| 220 | + rustc_lexer::LiteralKind::ByteStr { terminated } => { |
| 221 | + if !terminated { |
| 222 | + err = "Missing trailing `\"` symbol to terminate the byte string literal"; |
| 223 | + } |
| 224 | + BYTE_STRING |
| 225 | + } |
| 226 | + rustc_lexer::LiteralKind::RawStr { err: raw_str_err, .. } => { |
| 227 | + if let Some(raw_str_err) = raw_str_err { |
| 228 | + err = match raw_str_err { |
| 229 | + RawStrError::InvalidStarter { .. } => "Missing `\"` symbol after `#` symbols to begin the raw string literal", |
| 230 | + RawStrError::NoTerminator { expected, found, .. } => if expected == found { |
| 231 | + "Missing trailing `\"` to terminate the raw string literal" |
| 232 | + } else { |
| 233 | + "Missing trailing `\"` with `#` symbols to terminate the raw string literal" |
| 234 | + }, |
| 235 | + RawStrError::TooManyDelimiters { .. } => "Too many `#` symbols: raw strings may be delimited by up to 65535 `#` symbols", |
| 236 | + }; |
| 237 | + }; |
| 238 | + RAW_STRING |
| 239 | + } |
| 240 | + rustc_lexer::LiteralKind::RawByteStr { err: raw_str_err, .. } => { |
| 241 | + if let Some(raw_str_err) = raw_str_err { |
| 242 | + err = match raw_str_err { |
| 243 | + RawStrError::InvalidStarter { .. } => "Missing `\"` symbol after `#` symbols to begin the raw byte string literal", |
| 244 | + RawStrError::NoTerminator { expected, found, .. } => if expected == found { |
| 245 | + "Missing trailing `\"` to terminate the raw byte string literal" |
| 246 | + } else { |
| 247 | + "Missing trailing `\"` with `#` symbols to terminate the raw byte string literal" |
| 248 | + }, |
| 249 | + RawStrError::TooManyDelimiters { .. } => "Too many `#` symbols: raw byte strings may be delimited by up to 65535 `#` symbols", |
| 250 | + }; |
| 251 | + }; |
| 252 | + |
| 253 | + RAW_BYTE_STRING |
219 | 254 | } |
220 | | - |
221 | | - LK::RawStr { err, .. } => match err { |
222 | | - None => RAW_STRING, |
223 | | - Some(RawStrError::InvalidStarter { .. }) => return (RAW_STRING, Some("Missing `\"` symbol after `#` symbols to begin the raw string literal")), |
224 | | - Some(RawStrError::NoTerminator { expected, found, .. }) => if expected == found { |
225 | | - return (RAW_STRING, Some("Missing trailing `\"` to terminate the raw string literal")) |
226 | | - } else { |
227 | | - return (RAW_STRING, Some("Missing trailing `\"` with `#` symbols to terminate the raw string literal")) |
228 | | - |
229 | | - }, |
230 | | - Some(RawStrError::TooManyDelimiters { .. }) => return (RAW_STRING, Some("Too many `#` symbols: raw strings may be delimited by up to 65535 `#` symbols")), |
231 | | - }, |
232 | | - LK::RawByteStr { err, .. } => match err { |
233 | | - None => RAW_BYTE_STRING, |
234 | | - Some(RawStrError::InvalidStarter { .. }) => return (RAW_BYTE_STRING, Some("Missing `\"` symbol after `#` symbols to begin the raw byte string literal")), |
235 | | - Some(RawStrError::NoTerminator { expected, found, .. }) => if expected == found { |
236 | | - return (RAW_BYTE_STRING, Some("Missing trailing `\"` to terminate the raw byte string literal")) |
237 | | - } else { |
238 | | - return (RAW_BYTE_STRING, Some("Missing trailing `\"` with `#` symbols to terminate the raw byte string literal")) |
239 | | - |
240 | | - }, |
241 | | - Some(RawStrError::TooManyDelimiters { .. }) => return (RAW_BYTE_STRING, Some("Too many `#` symbols: raw byte strings may be delimited by up to 65535 `#` symbols")), |
242 | | - }, |
243 | 255 | }; |
244 | 256 |
|
245 | | - (syntax_kind, None) |
| 257 | + let err = if err.is_empty() { None } else { Some(err) }; |
| 258 | + |
| 259 | + (syntax_kind, err) |
246 | 260 | } |
247 | 261 | } |
0 commit comments