aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r--crates/ra_syntax/src/ast.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 8bf439b60..211ba31e5 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -609,6 +609,52 @@ impl SelfParam {
609 } 609 }
610} 610}
611 611
612#[derive(Clone, Debug, PartialEq, Eq, Hash)]
613pub enum LiteralFlavor {
614 String,
615 ByteString,
616 Char,
617 Byte,
618 IntNumber { suffix: Option<SmolStr> },
619 FloatNumber { suffix: Option<SmolStr> },
620 Bool,
621}
622
623impl LiteralExpr {
624 pub fn flavor(&self) -> LiteralFlavor {
625 let syntax = self.syntax();
626 match syntax.kind() {
627 INT_NUMBER => {
628 let allowed_suffix_list = [
629 "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32",
630 "u16", "u8",
631 ];
632 let text = syntax.text().to_string();
633 let suffix = allowed_suffix_list
634 .iter()
635 .find(|&s| text.ends_with(s))
636 .map(|&suf| SmolStr::new(suf));
637 LiteralFlavor::IntNumber { suffix: suffix }
638 }
639 FLOAT_NUMBER => {
640 let allowed_suffix_list = ["f64", "f32"];
641 let text = syntax.text().to_string();
642 let suffix = allowed_suffix_list
643 .iter()
644 .find(|&s| text.ends_with(s))
645 .map(|&suf| SmolStr::new(suf));
646 LiteralFlavor::FloatNumber { suffix: suffix }
647 }
648 STRING | RAW_STRING => LiteralFlavor::String,
649 TRUE_KW | FALSE_KW => LiteralFlavor::Bool,
650 BYTE_STRING | RAW_BYTE_STRING => LiteralFlavor::ByteString,
651 CHAR => LiteralFlavor::Char,
652 BYTE => LiteralFlavor::Byte,
653 _ => unreachable!(),
654 }
655 }
656}
657
612#[test] 658#[test]
613fn test_doc_comment_of_items() { 659fn test_doc_comment_of_items() {
614 let file = SourceFile::parse( 660 let file = SourceFile::parse(