Parse integer / float types

This commit is contained in:
Florian Diebold 2018-12-22 22:17:55 +01:00
parent 3ac605e687
commit 3899898d75
4 changed files with 58 additions and 4 deletions

View File

@ -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

View File

@ -33,6 +33,18 @@ impl IntTy {
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 @@ impl UintTy {
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 @@ impl FloatTy {
FloatTy::F64 => "f64",
}
}
pub fn from_string(s: &str) -> Option<FloatTy> {
match s {
"f32" => Some(FloatTy::F32),
"f64" => Some(FloatTy::F64),
_ => None,
}
}
}

View File

@ -2697,7 +2697,11 @@ impl<R: TreeRoot<RaTypes>> PathTypeNode<R> {
}
impl<'a> PathType<'a> {}
impl<'a> PathType<'a> {
pub fn path(self) -> Option<Path<'a>> {
super::child_opt(self)
}
}
// PlaceholderPat
#[derive(Debug, Clone, Copy,)]

View File

@ -304,7 +304,7 @@ Grammar(
"ParenType": (),
"TupleType": (),
"NeverType": (),
"PathType": (),
"PathType": (options: ["Path"]),
"PointerType": (),
"ArrayType": (),
"SliceType": (),