This commit is contained in:
Aleksey Kladov 2018-08-27 10:01:31 +03:00
parent 9b69c7df19
commit 8b0298ce09
7 changed files with 155 additions and 75 deletions

View file

@ -119,7 +119,7 @@ fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNo
panic!("Can't find common ancestor of {:?} and {:?}", n1, n2)
}
fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item=T> {
pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item=T> {
::itertools::unfold(seed, move |slot| {
slot.take().map(|curr| {
*slot = step(&curr);

View file

@ -383,6 +383,24 @@ impl<'a> AstNode<'a> for Expr<'a> {
impl<'a> Expr<'a> {}
// ExprStmt
#[derive(Debug, Clone, Copy)]
pub struct ExprStmt<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for ExprStmt<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
EXPR_STMT => Some(ExprStmt { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> ExprStmt<'a> {}
// FieldExpr
#[derive(Debug, Clone, Copy)]
pub struct FieldExpr<'a> {
@ -442,6 +460,10 @@ impl<'a> FnDef<'a> {
pub fn param_list(self) -> Option<ParamList<'a>> {
super::child_opt(self)
}
pub fn body(self) -> Option<Block<'a>> {
super::child_opt(self)
}
}
// FnPointerType
@ -626,6 +648,10 @@ impl<'a> LetStmt<'a> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self)
}
pub fn initializer(self) -> Option<Expr<'a>> {
super::child_opt(self)
}
}
// LoopExpr

View file

@ -248,7 +248,8 @@ Grammar(
"AttrsOwner",
],
options: [
["param_list", "ParamList"]
["param_list", "ParamList"],
["body", "Block"],
],
),
"StructDef": (
@ -431,7 +432,11 @@ Grammar(
"TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]),
"TypeParam": ( traits: ["NameOwner"] ),
"WhereClause": (),
"LetStmt": ( options: [ ["pat", "Pat"] ]),
"ExprStmt": (),
"LetStmt": ( options: [
["pat", "Pat"],
["initializer", "Expr"],
]),
"Block": (
collections: [
["let_stmts", "LetStmt"],

View file

@ -66,7 +66,7 @@ impl SyntaxRoot {
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub(crate) struct RedPtr(ptr::NonNull<RedNode>);
unsafe impl Send for RedPtr {}

View file

@ -1,4 +1,7 @@
use std::{fmt, sync::Arc};
use std::{
fmt, sync::Arc,
hash::{Hasher, Hash},
};
use smol_str::SmolStr;
@ -27,6 +30,11 @@ impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> {
}
impl<R: TreeRoot> Eq for SyntaxNode<R> {}
impl<R: TreeRoot> Hash for SyntaxNode<R> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.red.hash(state)
}
}
pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>;