From 1141d448d960eedba0a5647d525910de706bf778 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Aug 2018 13:33:44 +0300 Subject: Add derive intention --- crates/libsyntax2/src/algo/search.rs | 136 --------------------------------- crates/libsyntax2/src/ast/generated.rs | 25 ++++++ crates/libsyntax2/src/grammar.ron | 4 +- 3 files changed, 28 insertions(+), 137 deletions(-) delete mode 100644 crates/libsyntax2/src/algo/search.rs (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/algo/search.rs b/crates/libsyntax2/src/algo/search.rs deleted file mode 100644 index 46404f537..000000000 --- a/crates/libsyntax2/src/algo/search.rs +++ /dev/null @@ -1,136 +0,0 @@ -use {Node, NodeType, TextUnit, TextRange}; -use ::visitor::{visitor, process_subtree_bottom_up}; - -pub fn child_of_type(node: Node, ty: NodeType) -> Option { - node.children().find(|n| n.ty() == ty) -} - -pub fn children_of_type<'f>(node: Node<'f>, ty: NodeType) -> Box> + 'f> { - Box::new(node.children().filter(move |n| n.ty() == ty)) -} - -pub fn subtree<'f>(node: Node<'f>) -> Box> + 'f> { - Box::new(node.children().flat_map(subtree).chain(::std::iter::once(node))) -} - -pub fn descendants_of_type<'f>(node: Node<'f>, ty: NodeType) -> Vec> { - process_subtree_bottom_up( - node, - visitor(Vec::new()) - .visit_nodes(&[ty], |node, nodes| nodes.push(node)) - ) -} - -pub fn child_of_type_exn(node: Node, ty: NodeType) -> Node { - child_of_type(node, ty).unwrap_or_else(|| { - panic!("No child of type {:?} for {:?}\ - ----\ - {}\ - ----", ty, node.ty(), node.text()) - }) -} - - -pub fn ancestors(node: Node) -> Ancestors { - Ancestors(Some(node)) -} - -pub struct Ancestors<'f>(Option>); - -impl<'f> Iterator for Ancestors<'f> { - type Item = Node<'f>; - - fn next(&mut self) -> Option { - let current = self.0; - self.0 = current.and_then(|n| n.parent()); - current - } -} - -pub fn is_leaf(node: Node) -> bool { - node.children().next().is_none() && !node.range().is_empty() -} - - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -pub enum Direction { - Left, Right -} - -pub fn sibling(node: Node, dir: Direction) -> Option { - let (parent, idx) = child_position(node)?; - let idx = match dir { - Direction::Left => idx.checked_sub(1)?, - Direction::Right => idx + 1, - }; - parent.children().nth(idx) -} - -pub mod ast { - use {Node, AstNode, TextUnit, AstChildren}; - use visitor::{visitor, process_subtree_bottom_up}; - use super::{ancestors, find_leaf_at_offset, LeafAtOffset}; - - pub fn ancestor<'f, T: AstNode<'f>>(node: Node<'f>) -> Option { - ancestors(node) - .filter_map(T::wrap) - .next() - } - - pub fn ancestor_exn<'f, T: AstNode<'f>>(node: Node<'f>) -> T { - ancestor(node).unwrap() - } - - pub fn children_of_type<'f, N: AstNode<'f>>(node: Node<'f>) -> AstChildren { - AstChildren::new(node.children()) - } - - pub fn descendants_of_type<'f, N: AstNode<'f>>(node: Node<'f>) -> Vec { - process_subtree_bottom_up( - node, - visitor(Vec::new()) - .visit::(|node, acc| acc.push(node)) - ) - } - - pub fn node_at_offset<'f, T: AstNode<'f>>(node: Node<'f>, offset: TextUnit) -> Option { - match find_leaf_at_offset(node, offset) { - LeafAtOffset::None => None, - LeafAtOffset::Single(node) => ancestor(node), - LeafAtOffset::Between(left, right) => ancestor(left).or_else(|| ancestor(right)), - } - } -} - -pub mod traversal { - use {Node}; - - pub fn bottom_up<'f, F: FnMut(Node<'f>)>(node: Node<'f>, mut f: F) - { - go(node, &mut f); - - fn go<'f, F: FnMut(Node<'f>)>(node: Node<'f>, f: &mut F) { - for child in node.children() { - go(child, f) - } - f(node); - } - } -} - -fn child_position(child: Node) -> Option<(Node, usize)> { - child.parent() - .map(|parent| { - (parent, parent.children().position(|n| n == child).unwrap()) - }) -} - -fn common_ancestor<'f>(n1: Node<'f>, n2: Node<'f>) -> Node<'f> { - for p in ancestors(n1) { - if ancestors(n2).any(|a| a == p) { - return p; - } - } - panic!("Can't find common ancestor of {:?} and {:?}", n1, n2) -} - diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 3e6c673ab..80670ce71 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -267,6 +267,31 @@ impl AstNode for NeverType { impl NeverType {} +// NominalDef +#[derive(Debug, Clone, Copy)] +pub enum NominalDef> { + StructDef(StructDef), + EnumDef(EnumDef), +} + +impl AstNode for NominalDef { + fn cast(syntax: SyntaxNode) -> Option { + match syntax.kind() { + STRUCT_DEF => Some(NominalDef::StructDef(StructDef { syntax })), + ENUM_DEF => Some(NominalDef::EnumDef(EnumDef { syntax })), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { + match self { + NominalDef::StructDef(inner) => inner.syntax(), + NominalDef::EnumDef(inner) => inner.syntax(), + } + } +} + +impl NominalDef {} + // ParenType #[derive(Debug, Clone, Copy)] pub struct ParenType> { diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 3641b65e2..3ae403bb5 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -261,6 +261,8 @@ Grammar( "ForType", "ImplTraitType", "DynTraitType", - ]) + ]), + + "NominalDef": ( enum: ["StructDef", "EnumDef"]), }, ) -- cgit v1.2.3