From 8b0298ce095b6dd635f7ed35dc97f1874157040b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 27 Aug 2018 10:01:31 +0300 Subject: scopes --- crates/libsyntax2/src/algo/mod.rs | 2 +- crates/libsyntax2/src/ast/generated.rs | 26 ++++++++++++++++++++++++++ crates/libsyntax2/src/grammar.ron | 9 +++++++-- crates/libsyntax2/src/yellow/mod.rs | 2 +- crates/libsyntax2/src/yellow/syntax.rs | 10 +++++++++- 5 files changed, 44 insertions(+), 5 deletions(-) (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/algo/mod.rs b/crates/libsyntax2/src/algo/mod.rs index 2640d60ea..7287f5bb2 100644 --- a/crates/libsyntax2/src/algo/mod.rs +++ b/crates/libsyntax2/src/algo/mod.rs @@ -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(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { +pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { ::itertools::unfold(seed, move |slot| { slot.take().map(|curr| { *slot = step(&curr); diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 6926c0535..b937fe5a2 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -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 { + 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> { super::child_opt(self) } + + pub fn body(self) -> Option> { + super::child_opt(self) + } } // FnPointerType @@ -626,6 +648,10 @@ impl<'a> LetStmt<'a> { pub fn pat(self) -> Option> { super::child_opt(self) } + + pub fn initializer(self) -> Option> { + super::child_opt(self) + } } // LoopExpr diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 3a125ace6..aa2742b3e 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -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"], diff --git a/crates/libsyntax2/src/yellow/mod.rs b/crates/libsyntax2/src/yellow/mod.rs index 3c4510fe7..b94c794fe 100644 --- a/crates/libsyntax2/src/yellow/mod.rs +++ b/crates/libsyntax2/src/yellow/mod.rs @@ -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); unsafe impl Send for RedPtr {} diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index 0045598d4..75b6cb7dc 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs @@ -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 PartialEq> for SyntaxNode { } impl Eq for SyntaxNode {} +impl Hash for SyntaxNode { + fn hash(&self, state: &mut H) { + self.red.hash(state) + } +} pub type SyntaxNodeRef<'a> = SyntaxNode>; -- cgit v1.2.3