aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeetaha <[email protected]>2020-01-22 00:11:38 +0000
committerVeetaha <[email protected]>2020-01-22 11:02:21 +0000
commit3ec781d4f2e2e3087a99b7ace21f97a84b7c7b84 (patch)
tree594ad1a64392afa38b438615aec3124c36350b40
parentb982d60a4d512602b54f79b39119bde0f7e50f87 (diff)
ra_syntax: remove code duplication and token reevaluation from ast::Literal::kind()
-rw-r--r--crates/ra_parser/src/lib.rs4
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs50
2 files changed, 22 insertions, 32 deletions
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs
index 65134277e..81055746b 100644
--- a/crates/ra_parser/src/lib.rs
+++ b/crates/ra_parser/src/lib.rs
@@ -27,7 +27,7 @@ pub use syntax_kind::SyntaxKind;
27#[derive(Debug, Clone, PartialEq, Eq, Hash)] 27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct ParseError(pub String); 28pub struct ParseError(pub String);
29 29
30/// `TokenSource` abstracts the source of the tokens parser operates one. 30/// `TokenSource` abstracts the source of the tokens parser operates on.
31/// 31///
32/// Hopefully this will allow us to treat text and token trees in the same way! 32/// Hopefully this will allow us to treat text and token trees in the same way!
33pub trait TokenSource { 33pub trait TokenSource {
@@ -43,7 +43,7 @@ pub trait TokenSource {
43 fn is_keyword(&self, kw: &str) -> bool; 43 fn is_keyword(&self, kw: &str) -> bool;
44} 44}
45 45
46/// `TokenCursor` abstracts the cursor of `TokenSource` operates one. 46/// `Token` abstracts the cursor of `TokenSource` operates on.
47#[derive(Debug, Copy, Clone, Eq, PartialEq)] 47#[derive(Debug, Copy, Clone, Eq, PartialEq)]
48pub struct Token { 48pub struct Token {
49 /// What is the current token? 49 /// What is the current token?
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs
index 6da4b1309..e609e02dc 100644
--- a/crates/ra_syntax/src/ast/expr_extensions.rs
+++ b/crates/ra_syntax/src/ast/expr_extensions.rs
@@ -305,44 +305,34 @@ impl ast::Literal {
305 .unwrap() 305 .unwrap()
306 } 306 }
307 307
308 pub fn kind(&self) -> LiteralKind { 308 fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option<SmolStr> {
309 match self.token().kind() { 309 possible_suffixes
310 INT_NUMBER => { 310 .iter()
311 let int_suffix_list = [ 311 .find(|&suffix| text.ends_with(suffix))
312 "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", 312 .map(|&suffix| SmolStr::new(suffix))
313 "u16", "u8", 313 }
314 ];
315 314
316 // The lexer treats e.g. `1f64` as an integer literal. See 315 pub fn kind(&self) -> LiteralKind {
317 // https://github.com/rust-analyzer/rust-analyzer/issues/1592 316 const INT_SUFFIXES: [&'static str; 12] = [
318 // and the comments on the linked PR. 317 "u64", "u32", "u16", "u8", "usize", "isize", "i64", "i32", "i16", "i8", "u128", "i128",
319 let float_suffix_list = ["f32", "f64"]; 318 ];
319 const FLOAT_SUFFIXES: [&'static str; 2] = ["f32", "f64"];
320 320
321 let text = self.token().text().to_string(); 321 let token = self.token();
322 322
323 let float_suffix = float_suffix_list 323 match token.kind() {
324 .iter() 324 INT_NUMBER => {
325 .find(|&s| text.ends_with(s)) 325 let text = token.text();
326 .map(|&suf| SmolStr::new(suf));
327 326
328 if float_suffix.is_some() { 327 if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) {
329 LiteralKind::FloatNumber { suffix: float_suffix } 328 LiteralKind::FloatNumber { suffix }
330 } else { 329 } else {
331 let suffix = int_suffix_list 330 LiteralKind::IntNumber { suffix: Self::find_suffix(&text, &INT_SUFFIXES) }
332 .iter()
333 .find(|&s| text.ends_with(s))
334 .map(|&suf| SmolStr::new(suf));
335 LiteralKind::IntNumber { suffix }
336 } 331 }
337 } 332 }
338 FLOAT_NUMBER => { 333 FLOAT_NUMBER => {
339 let allowed_suffix_list = ["f64", "f32"]; 334 let text = token.text();
340 let text = self.token().text().to_string(); 335 LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) }
341 let suffix = allowed_suffix_list
342 .iter()
343 .find(|&s| text.ends_with(s))
344 .map(|&suf| SmolStr::new(suf));
345 LiteralKind::FloatNumber { suffix }
346 } 336 }
347 STRING | RAW_STRING => LiteralKind::String, 337 STRING | RAW_STRING => LiteralKind::String,
348 T![true] | T![false] => LiteralKind::Bool, 338 T![true] | T![false] => LiteralKind::Bool,