From a3b104aa6df205e74c116d8c9e41900807924e70 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 11 Feb 2020 22:33:11 +0100 Subject: Implement slice pattern AST > HIR lowering --- crates/ra_syntax/src/ast.rs | 3 ++- crates/ra_syntax/src/ast/extensions.rs | 36 ++++++++++++++++++++++++++++++++++ crates/ra_syntax/src/ast/generated.rs | 6 +++++- 3 files changed, 43 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 89cb9a9f3..d3e8888bd 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -18,7 +18,8 @@ use crate::{ pub use self::{ expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, extensions::{ - FieldKind, PathSegmentKind, SelfParamKind, StructKind, TypeBoundKind, VisibilityKind, + FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, + VisibilityKind, }, generated::*, tokens::*, diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index cb0aee422..7dcf084de 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -1,6 +1,8 @@ //! Various extension methods to ast Nodes, which are hard to code-generate. //! Extensions for various expressions live in a sibling `expr_extensions` module. +use itertools::Itertools; + use crate::{ ast::{self, child_opt, children, AstNode, AttrInput, SyntaxNode}, SmolStr, SyntaxElement, @@ -293,6 +295,40 @@ impl ast::BindPat { } } +pub struct SlicePatComponents { + pub prefix: Vec, + pub slice: Option, + pub suffix: Vec, +} + +impl ast::SlicePat { + pub fn components(&self) -> SlicePatComponents { + let mut args = self.args().peekable(); + let prefix = args + .peeking_take_while(|p| match p { + ast::Pat::DotDotPat(_) => false, + ast::Pat::BindPat(bp) => match bp.pat() { + Some(ast::Pat::DotDotPat(_)) => false, + _ => true, + }, + ast::Pat::RefPat(rp) => match rp.pat() { + Some(ast::Pat::DotDotPat(_)) => false, + Some(ast::Pat::BindPat(bp)) => match bp.pat() { + Some(ast::Pat::DotDotPat(_)) => false, + _ => true, + }, + _ => true, + }, + _ => true, + }) + .collect(); + let slice = args.next(); + let suffix = args.collect(); + + SlicePatComponents { prefix, slice, suffix } + } +} + impl ast::PointerType { pub fn is_mut(&self) -> bool { self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 33d5578e7..8a3669bd1 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -2063,7 +2063,11 @@ impl AstNode for SlicePat { &self.syntax } } -impl SlicePat {} +impl SlicePat { + pub fn args(&self) -> AstChildren { + AstChildren::new(&self.syntax) + } +} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RangePat { pub(crate) syntax: SyntaxNode, -- cgit v1.2.3