Skip to content

Commit 3ec781d

Browse files
author
Veetaha
committed
ra_syntax: remove code duplication and token reevaluation from ast::Literal::kind()
1 parent b982d60 commit 3ec781d

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

crates/ra_parser/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use syntax_kind::SyntaxKind;
2727
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2828
pub struct ParseError(pub String);
2929

30-
/// `TokenSource` abstracts the source of the tokens parser operates one.
30+
/// `TokenSource` abstracts the source of the tokens parser operates on.
3131
///
3232
/// Hopefully this will allow us to treat text and token trees in the same way!
3333
pub trait TokenSource {
@@ -43,7 +43,7 @@ pub trait TokenSource {
4343
fn is_keyword(&self, kw: &str) -> bool;
4444
}
4545

46-
/// `TokenCursor` abstracts the cursor of `TokenSource` operates one.
46+
/// `Token` abstracts the cursor of `TokenSource` operates on.
4747
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
4848
pub struct Token {
4949
/// What is the current token?

crates/ra_syntax/src/ast/expr_extensions.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -305,44 +305,34 @@ impl ast::Literal {
305305
.unwrap()
306306
}
307307

308-
pub fn kind(&self) -> LiteralKind {
309-
match self.token().kind() {
310-
INT_NUMBER => {
311-
let int_suffix_list = [
312-
"isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32",
313-
"u16", "u8",
314-
];
308+
fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option<SmolStr> {
309+
possible_suffixes
310+
.iter()
311+
.find(|&suffix| text.ends_with(suffix))
312+
.map(|&suffix| SmolStr::new(suffix))
313+
}
315314

316-
// The lexer treats e.g. `1f64` as an integer literal. See
317-
// https://github.com/rust-analyzer/rust-analyzer/issues/1592
318-
// and the comments on the linked PR.
319-
let float_suffix_list = ["f32", "f64"];
315+
pub fn kind(&self) -> LiteralKind {
316+
const INT_SUFFIXES: [&'static str; 12] = [
317+
"u64", "u32", "u16", "u8", "usize", "isize", "i64", "i32", "i16", "i8", "u128", "i128",
318+
];
319+
const FLOAT_SUFFIXES: [&'static str; 2] = ["f32", "f64"];
320320

321-
let text = self.token().text().to_string();
321+
let token = self.token();
322322

323-
let float_suffix = float_suffix_list
324-
.iter()
325-
.find(|&s| text.ends_with(s))
326-
.map(|&suf| SmolStr::new(suf));
323+
match token.kind() {
324+
INT_NUMBER => {
325+
let text = token.text();
327326

328-
if float_suffix.is_some() {
329-
LiteralKind::FloatNumber { suffix: float_suffix }
327+
if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) {
328+
LiteralKind::FloatNumber { suffix }
330329
} else {
331-
let suffix = int_suffix_list
332-
.iter()
333-
.find(|&s| text.ends_with(s))
334-
.map(|&suf| SmolStr::new(suf));
335-
LiteralKind::IntNumber { suffix }
330+
LiteralKind::IntNumber { suffix: Self::find_suffix(&text, &INT_SUFFIXES) }
336331
}
337332
}
338333
FLOAT_NUMBER => {
339-
let allowed_suffix_list = ["f64", "f32"];
340-
let text = self.token().text().to_string();
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 }
334+
let text = token.text();
335+
LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) }
346336
}
347337
STRING | RAW_STRING => LiteralKind::String,
348338
T![true] | T![false] => LiteralKind::Bool,

0 commit comments

Comments
 (0)