aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-11-26 22:57:40 +0000
committerLukas Wirth <[email protected]>2020-11-26 23:00:18 +0000
commit931493e949ce369775bccfcca24878c73e1509c2 (patch)
treee3cf89e43891a3772843c7d20b56b969ed1e0107 /crates/syntax
parentfc0354b2803bccfeb6e0c3335a5d4e52288c05b4 (diff)
Accept more than just the standard rust literal suffixes in *Number::suffix
Diffstat (limited to 'crates/syntax')
-rw-r--r--crates/syntax/src/ast/token_ext.rs40
1 files changed, 18 insertions, 22 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index ac0326420..754ca8c62 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -534,11 +534,6 @@ impl HasFormatSpecifier for ast::String {
534} 534}
535 535
536impl ast::IntNumber { 536impl ast::IntNumber {
537 const SUFFIXES: &'static [&'static str] = &[
538 "u8", "u16", "u32", "u64", "u128", "usize", // Unsigned.
539 "i8", "i16", "i32", "i64", "i128", "isize", // Signed.
540 ];
541
542 pub fn radix(&self) -> Radix { 537 pub fn radix(&self) -> Radix {
543 match self.text().get(..2).unwrap_or_default() { 538 match self.text().get(..2).unwrap_or_default() {
544 "0b" => Radix::Binary, 539 "0b" => Radix::Binary,
@@ -571,29 +566,30 @@ impl ast::IntNumber {
571 566
572 pub fn suffix(&self) -> Option<&str> { 567 pub fn suffix(&self) -> Option<&str> {
573 let text = self.text(); 568 let text = self.text();
574 // FIXME: don't check a fixed set of suffixes, `1_0_1_l_o_l` is valid 569 let radix = self.radix();
575 // syntax, suffix is `l_o_l`. 570 let mut indices = text.char_indices();
576 ast::IntNumber::SUFFIXES.iter().chain(ast::FloatNumber::SUFFIXES.iter()).find_map( 571 if radix != Radix::Decimal {
577 |suffix| { 572 indices.next()?;
578 if text.ends_with(suffix) { 573 indices.next()?;
579 return Some(&text[text.len() - suffix.len()..]); 574 }
580 } 575 let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
581 None 576 Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
582 }, 577 _ => |(_, c)| c.is_ascii_alphabetic(),
583 ) 578 };
579 let (suffix_start, _) = indices.find(is_suffix_start)?;
580 Some(&text[suffix_start..])
584 } 581 }
585} 582}
586 583
587impl ast::FloatNumber { 584impl ast::FloatNumber {
588 const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
589 pub fn suffix(&self) -> Option<&str> { 585 pub fn suffix(&self) -> Option<&str> {
590 let text = self.text(); 586 let text = self.text();
591 ast::FloatNumber::SUFFIXES.iter().find_map(|suffix| { 587 let mut indices = text.char_indices();
592 if text.ends_with(suffix) { 588 let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
593 return Some(&text[text.len() - suffix.len()..]); 589 if c == 'e' || c == 'E' {
594 } 590 suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
595 None 591 }
596 }) 592 Some(&text[suffix_start..])
597 } 593 }
598} 594}
599 595