mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
Hash out ast structure
This commit is contained in:
parent
9726ecdc99
commit
7a6361b219
2 changed files with 72 additions and 5 deletions
73
minirust.rs
73
minirust.rs
|
@ -1,3 +1,4 @@
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct NodeKind(u16);
|
pub struct NodeKind(u16);
|
||||||
|
|
||||||
pub struct File {
|
pub struct File {
|
||||||
|
@ -67,8 +68,6 @@ impl<'f> Iterator for Children<'f> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub const ERROR: NodeKind = NodeKind(0);
|
pub const ERROR: NodeKind = NodeKind(0);
|
||||||
pub const WHITESPACE: NodeKind = NodeKind(1);
|
pub const WHITESPACE: NodeKind = NodeKind(1);
|
||||||
pub const STRUCT_KW: NodeKind = NodeKind(2);
|
pub const STRUCT_KW: NodeKind = NodeKind(2);
|
||||||
|
@ -82,4 +81,72 @@ pub const LINE_COMMENT: NodeKind = NodeKind(9);
|
||||||
pub const FILE: NodeKind = NodeKind(10);
|
pub const FILE: NodeKind = NodeKind(10);
|
||||||
pub const STRUCT_DEF: NodeKind = NodeKind(11);
|
pub const STRUCT_DEF: NodeKind = NodeKind(11);
|
||||||
pub const FIELD_DEF: NodeKind = NodeKind(12);
|
pub const FIELD_DEF: NodeKind = NodeKind(12);
|
||||||
pub const TYPE: NodeKind = NodeKind(13);
|
pub const TYPE_REF: NodeKind = NodeKind(13);
|
||||||
|
|
||||||
|
|
||||||
|
pub trait AstNode<'f>: Copy + 'f {
|
||||||
|
fn new(node: Node<'f>) -> Option<Self>;
|
||||||
|
fn node(&self) -> Node<'f>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn child_of_kind<'f>(node: Node<'f>, kind: NodeKind) -> Option<Node<'f>> {
|
||||||
|
node.children().find(|child| child.kind() == kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ast_children<'f, A: AstNode<'f>>(node: Node<'f>) -> Box<Iterator<Item=A> + 'f> {
|
||||||
|
Box::new(node.children().filter_map(A::new))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct StructDef<'f>(Node<'f>);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct FieldDef<'f>(Node<'f>);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct TypeRef<'f>(Node<'f>);
|
||||||
|
|
||||||
|
pub trait NameOwner<'f>: AstNode<'f> {
|
||||||
|
fn name_ident(&self) -> Node<'f> {
|
||||||
|
child_of_kind(self.node(), IDENT).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &'f str { self.name_ident().text() }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl<'f> AstNode<'f> for StructDef<'f> {
|
||||||
|
fn new(node: Node<'f>) -> Option<Self> {
|
||||||
|
if node.kind() == STRUCT_DEF { Some(StructDef(node)) } else { None }
|
||||||
|
}
|
||||||
|
fn node(&self) -> Node<'f> { self.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'f> AstNode<'f> for FieldDef<'f> {
|
||||||
|
fn new(node: Node<'f>) -> Option<Self> {
|
||||||
|
if node.kind() == FIELD_DEF { Some(FieldDef(node)) } else { None }
|
||||||
|
}
|
||||||
|
fn node(&self) -> Node<'f> { self.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'f> AstNode<'f> for TypeRef<'f> {
|
||||||
|
fn new(node: Node<'f>) -> Option<Self> {
|
||||||
|
if node.kind() == TYPE_REF { Some(TypeRef(node)) } else { None }
|
||||||
|
}
|
||||||
|
fn node(&self) -> Node<'f> { self.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'f> NameOwner<'f> for StructDef<'f> {}
|
||||||
|
impl<'f> NameOwner<'f> for FieldDef<'f> {}
|
||||||
|
|
||||||
|
impl<'f> StructDef<'f> {
|
||||||
|
pub fn fields(&self) -> Box<Iterator<Item=FieldDef<'f>> + 'f> {
|
||||||
|
ast_children(self.node())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'f> FieldDef<'f> {
|
||||||
|
pub fn type_ref(&self) -> Option<TypeRef<'f>> {
|
||||||
|
ast_children(self.node()).next()
|
||||||
|
}
|
||||||
|
}
|
2
rfc.md
2
rfc.md
|
@ -150,7 +150,7 @@ FILE
|
||||||
IDENT
|
IDENT
|
||||||
COLON
|
COLON
|
||||||
WHITESPACE
|
WHITESPACE
|
||||||
TYPE
|
TYPE_REF
|
||||||
IDENT
|
IDENT
|
||||||
COMMA
|
COMMA
|
||||||
WHITESPACE
|
WHITESPACE
|
||||||
|
|
Loading…
Reference in a new issue