From 3ec781d4f2e2e3087a99b7ace21f97a84b7c7b84 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 22 Jan 2020 02:11:38 +0200 Subject: ra_syntax: remove code duplication and token reevaluation from ast::Literal::kind() --- crates/ra_parser/src/lib.rs | 4 +-- crates/ra_syntax/src/ast/expr_extensions.rs | 50 ++++++++++++----------------- 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; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ParseError(pub String); -/// `TokenSource` abstracts the source of the tokens parser operates one. +/// `TokenSource` abstracts the source of the tokens parser operates on. /// /// Hopefully this will allow us to treat text and token trees in the same way! pub trait TokenSource { @@ -43,7 +43,7 @@ pub trait TokenSource { fn is_keyword(&self, kw: &str) -> bool; } -/// `TokenCursor` abstracts the cursor of `TokenSource` operates one. +/// `Token` abstracts the cursor of `TokenSource` operates on. #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub struct Token { /// 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 { .unwrap() } - pub fn kind(&self) -> LiteralKind { - match self.token().kind() { - INT_NUMBER => { - let int_suffix_list = [ - "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", - "u16", "u8", - ]; + fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option { + possible_suffixes + .iter() + .find(|&suffix| text.ends_with(suffix)) + .map(|&suffix| SmolStr::new(suffix)) + } - // The lexer treats e.g. `1f64` as an integer literal. See - // https://github.com/rust-analyzer/rust-analyzer/issues/1592 - // and the comments on the linked PR. - let float_suffix_list = ["f32", "f64"]; + pub fn kind(&self) -> LiteralKind { + const INT_SUFFIXES: [&'static str; 12] = [ + "u64", "u32", "u16", "u8", "usize", "isize", "i64", "i32", "i16", "i8", "u128", "i128", + ]; + const FLOAT_SUFFIXES: [&'static str; 2] = ["f32", "f64"]; - let text = self.token().text().to_string(); + let token = self.token(); - let float_suffix = float_suffix_list - .iter() - .find(|&s| text.ends_with(s)) - .map(|&suf| SmolStr::new(suf)); + match token.kind() { + INT_NUMBER => { + let text = token.text(); - if float_suffix.is_some() { - LiteralKind::FloatNumber { suffix: float_suffix } + if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) { + LiteralKind::FloatNumber { suffix } } else { - let suffix = int_suffix_list - .iter() - .find(|&s| text.ends_with(s)) - .map(|&suf| SmolStr::new(suf)); - LiteralKind::IntNumber { suffix } + LiteralKind::IntNumber { suffix: Self::find_suffix(&text, &INT_SUFFIXES) } } } FLOAT_NUMBER => { - let allowed_suffix_list = ["f64", "f32"]; - let text = self.token().text().to_string(); - let suffix = allowed_suffix_list - .iter() - .find(|&s| text.ends_with(s)) - .map(|&suf| SmolStr::new(suf)); - LiteralKind::FloatNumber { suffix } + let text = token.text(); + LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) } } STRING | RAW_STRING => LiteralKind::String, T![true] | T![false] => LiteralKind::Bool, -- cgit v1.2.3 From b6be1b6f6122c0a6bdfa18813870a197a4be51bf Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 22 Jan 2020 12:38:25 +0200 Subject: Preserved a comment on the bug previously present in ast::Literal::kind() --- crates/ra_syntax/src/ast/expr_extensions.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index e609e02dc..c91dd2bf4 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -322,6 +322,11 @@ impl ast::Literal { match token.kind() { INT_NUMBER => { + // FYI: there was a bug here previously, thus an if statement here is necessary. + // The lexer treated e.g. `1f64` as an integer literal. See + // https://github.com/rust-analyzer/rust-analyzer/issues/1592 + // and the comments on the linked PR. + let text = token.text(); if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) { -- cgit v1.2.3 From 1ac105056a3f751c3b75a1cbffaa9c5cdafef576 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 22 Jan 2020 12:39:32 +0200 Subject: Fixed a typo --- crates/ra_syntax/src/ast/expr_extensions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index c91dd2bf4..69de23d58 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -323,7 +323,7 @@ impl ast::Literal { match token.kind() { INT_NUMBER => { // FYI: there was a bug here previously, thus an if statement here is necessary. - // The lexer treated e.g. `1f64` as an integer literal. See + // The lexer treats e.g. `1f64` as an integer literal. See // https://github.com/rust-analyzer/rust-analyzer/issues/1592 // and the comments on the linked PR. -- cgit v1.2.3 From fa31841f1f497f84ceb15c0a8636a2bf836705da Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 22 Jan 2020 12:39:56 +0200 Subject: Fixed a typo --- crates/ra_syntax/src/ast/expr_extensions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 69de23d58..539759450 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -322,7 +322,7 @@ impl ast::Literal { match token.kind() { INT_NUMBER => { - // FYI: there was a bug here previously, thus an if statement here is necessary. + // FYI: there was a bug here previously, thus an if statement bellow is necessary. // The lexer treats e.g. `1f64` as an integer literal. See // https://github.com/rust-analyzer/rust-analyzer/issues/1592 // and the comments on the linked PR. -- cgit v1.2.3