From 3899898d75176ce3cd87f9e2acecd7e3a987dda5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 22 Dec 2018 22:17:55 +0100 Subject: [PATCH] Parse integer / float types --- crates/ra_hir/src/ty.rs | 22 ++++++++++++++++-- crates/ra_hir/src/ty/primitive.rs | 32 +++++++++++++++++++++++++++ crates/ra_syntax/src/ast/generated.rs | 6 ++++- crates/ra_syntax/src/grammar.ron | 2 +- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 36dc5d137ca0..087385b98f7f 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; 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 @@ impl Ty { 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 diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index 4a5ce5a97ba9..ad79b17e4185 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs @@ -33,6 +33,18 @@ impl IntTy { IntTy::I128 => "i128", } } + + pub fn from_string(s: &str) -> Option { + 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 @@ impl UintTy { UintTy::U128 => "u128", } } + + pub fn from_string(s: &str) -> Option { + 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 @@ impl FloatTy { FloatTy::F64 => "f64", } } + + pub fn from_string(s: &str) -> Option { + match s { + "f32" => Some(FloatTy::F32), + "f64" => Some(FloatTy::F64), + _ => None, + } + } } diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 91f27fb26ec9..74bf4d3cc315 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -2697,7 +2697,11 @@ impl> PathTypeNode { } -impl<'a> PathType<'a> {} +impl<'a> PathType<'a> { + pub fn path(self) -> Option> { + super::child_opt(self) + } +} // PlaceholderPat #[derive(Debug, Clone, Copy,)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index c43db51b6863..29b84854a35f 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -304,7 +304,7 @@ Grammar( "ParenType": (), "TupleType": (), "NeverType": (), - "PathType": (), + "PathType": (options: ["Path"]), "PointerType": (), "ArrayType": (), "SliceType": (),