]> git.lizzy.rs Git - rust.git/commitdiff
Parse integer / float types
authorFlorian Diebold <flodiebold@gmail.com>
Sat, 22 Dec 2018 21:17:55 +0000 (22:17 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Sun, 23 Dec 2018 12:48:04 +0000 (13:48 +0100)
crates/ra_hir/src/ty.rs
crates/ra_hir/src/ty/primitive.rs
crates/ra_syntax/src/ast/generated.rs
crates/ra_syntax/src/grammar.ron

index 36dc5d137ca0599904b015a042f7b4ce46f735c2..087385b98f7fc0883739b636da97c184de897a2e 100644 (file)
@@ -9,7 +9,7 @@
 
 use ra_db::LocalSyntaxPtr;
 use ra_syntax::{
-    TextRange, TextUnit,
+    TextRange, TextUnit, SmolStr,
     algo::visit::{visitor, Visitor},
     ast::{self, AstNode, DocCommentsOwner, NameOwner, LoopBodyOwner, ArgListOwner},
     SyntaxNodeRef
@@ -148,7 +148,25 @@ pub fn new(node: ast::TypeRef) -> Self {
             ParenType(_inner) => Ty::Unknown, // TODO
             TupleType(_inner) => Ty::Unknown, // TODO
             NeverType(..) => Ty::Never,
-            PathType(_inner) => Ty::Unknown, // TODO
+            PathType(inner) => {
+                let path = if let Some(p) = inner.path() { p } else { return Ty::Unknown };
+                if path.qualifier().is_none() {
+                    let name = path.segment().and_then(|s| s.name_ref()).map(|n| n.text()).unwrap_or(SmolStr::new(""));
+                    if let Some(int_ty) = primitive::IntTy::from_string(&name) {
+                        Ty::Int(int_ty)
+                    } else if let Some(uint_ty) = primitive::UintTy::from_string(&name) {
+                        Ty::Uint(uint_ty)
+                    } else if let Some(float_ty) = primitive::FloatTy::from_string(&name) {
+                        Ty::Float(float_ty)
+                    } else {
+                        // TODO
+                        Ty::Unknown
+                    }
+                } else {
+                    // TODO
+                    Ty::Unknown
+                }
+            },
             PointerType(_inner) => Ty::Unknown, // TODO
             ArrayType(_inner) => Ty::Unknown, // TODO
             SliceType(_inner) => Ty::Unknown, // TODO
index 4a5ce5a97ba9b7d036eaf7f3b9d82b45926f8ca5..ad79b17e41859f186a759fd0785615d8ba81c07c 100644 (file)
@@ -33,6 +33,18 @@ pub fn ty_to_string(&self) -> &'static str {
             IntTy::I128 => "i128",
         }
     }
+
+    pub fn from_string(s: &str) -> Option<IntTy> {
+        match s {
+            "isize" => Some(IntTy::Isize),
+            "i8" => Some(IntTy::I8),
+            "i16" => Some(IntTy::I16),
+            "i32" => Some(IntTy::I32),
+            "i64" => Some(IntTy::I64),
+            "i128" => Some(IntTy::I128),
+            _ => None,
+        }
+    }
 }
 
 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
@@ -56,6 +68,18 @@ pub fn ty_to_string(&self) -> &'static str {
             UintTy::U128 => "u128",
         }
     }
+
+    pub fn from_string(s: &str) -> Option<UintTy> {
+        match s {
+            "usize" => Some(UintTy::Usize),
+            "u8" => Some(UintTy::U8),
+            "u16" => Some(UintTy::U16),
+            "u32" => Some(UintTy::U32),
+            "u64" => Some(UintTy::U64),
+            "u128" => Some(UintTy::U128),
+            _ => None,
+        }
+    }
 }
 
 impl fmt::Debug for UintTy {
@@ -95,4 +119,12 @@ pub fn ty_to_string(self) -> &'static str {
             FloatTy::F64 => "f64",
         }
     }
+
+    pub fn from_string(s: &str) -> Option<FloatTy> {
+        match s {
+            "f32" => Some(FloatTy::F32),
+            "f64" => Some(FloatTy::F64),
+            _ => None,
+        }
+    }
 }
index 91f27fb26ec9ddbdb59f777c9ef75379aa630b24..74bf4d3cc3153b5ba04e7814eb70112a0bd143d6 100644 (file)
@@ -2697,7 +2697,11 @@ pub fn owned(&self) -> PathTypeNode {
 }
 
 
-impl<'a> PathType<'a> {}
+impl<'a> PathType<'a> {
+    pub fn path(self) -> Option<Path<'a>> {
+        super::child_opt(self)
+    }
+}
 
 // PlaceholderPat
 #[derive(Debug, Clone, Copy,)]
index c43db51b6863e73b73a10dd40a9e39a96d9aaca7..29b84854a35f10e1e372941b7b516590f0f5dd6c 100644 (file)
@@ -304,7 +304,7 @@ Grammar(
         "ParenType": (),
         "TupleType": (),
         "NeverType": (),
-        "PathType": (),
+        "PathType": (options: ["Path"]),
         "PointerType": (),
         "ArrayType": (),
         "SliceType": (),