aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/algo/visit.rs
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2018-10-15 22:44:23 +0100
committerJeremy A. Kolb <[email protected]>2018-10-16 14:41:10 +0100
commit61f3a438d3a729a6be941bca1ff4c6a97a33f221 (patch)
tree6551967cc8c6e921b66071453ad7888a9121d326 /crates/ra_syntax/src/algo/visit.rs
parent39cb6c6d3f78b193f5873c3492e530bbd24d5dd2 (diff)
Cargo Format
Run `cargo fmt` and ignore generated files
Diffstat (limited to 'crates/ra_syntax/src/algo/visit.rs')
-rw-r--r--crates/ra_syntax/src/algo/visit.rs63
1 files changed, 37 insertions, 26 deletions
diff --git a/crates/ra_syntax/src/algo/visit.rs b/crates/ra_syntax/src/algo/visit.rs
index 1ae988a87..c021f464c 100644
--- a/crates/ra_syntax/src/algo/visit.rs
+++ b/crates/ra_syntax/src/algo/visit.rs
@@ -1,23 +1,31 @@
1use std::marker::PhantomData; 1use crate::{AstNode, SyntaxNodeRef};
2use crate::{SyntaxNodeRef, AstNode};
3 2
3use std::marker::PhantomData;
4 4
5pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { 5pub fn visitor<'a, T>() -> impl Visitor<'a, Output = T> {
6 EmptyVisitor { ph: PhantomData } 6 EmptyVisitor { ph: PhantomData }
7} 7}
8 8
9pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output=T, Ctx=C> { 9pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output = T, Ctx = C> {
10 EmptyVisitorCtx { ph: PhantomData, ctx } 10 EmptyVisitorCtx {
11 ph: PhantomData,
12 ctx,
13 }
11} 14}
12 15
13pub trait Visitor<'a>: Sized { 16pub trait Visitor<'a>: Sized {
14 type Output; 17 type Output;
15 fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>; 18 fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>;
16 fn visit<N, F>(self, f: F) -> Vis<Self, N, F> 19 fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
17 where N: AstNode<'a>, 20 where
18 F: FnOnce(N) -> Self::Output, 21 N: AstNode<'a>,
22 F: FnOnce(N) -> Self::Output,
19 { 23 {
20 Vis { inner: self, f, ph: PhantomData } 24 Vis {
25 inner: self,
26 f,
27 ph: PhantomData,
28 }
21 } 29 }
22} 30}
23 31
@@ -26,16 +34,21 @@ pub trait VisitorCtx<'a>: Sized {
26 type Ctx; 34 type Ctx;
27 fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx>; 35 fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx>;
28 fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F> 36 fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F>
29 where N: AstNode<'a>, 37 where
30 F: FnOnce(N, Self::Ctx) -> Self::Output, 38 N: AstNode<'a>,
39 F: FnOnce(N, Self::Ctx) -> Self::Output,
31 { 40 {
32 VisCtx { inner: self, f, ph: PhantomData } 41 VisCtx {
42 inner: self,
43 f,
44 ph: PhantomData,
45 }
33 } 46 }
34} 47}
35 48
36#[derive(Debug)] 49#[derive(Debug)]
37struct EmptyVisitor<T> { 50struct EmptyVisitor<T> {
38 ph: PhantomData<fn() -> T> 51 ph: PhantomData<fn() -> T>,
39} 52}
40 53
41impl<'a, T> Visitor<'a> for EmptyVisitor<T> { 54impl<'a, T> Visitor<'a> for EmptyVisitor<T> {
@@ -69,10 +82,10 @@ pub struct Vis<V, N, F> {
69} 82}
70 83
71impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F> 84impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
72 where 85where
73 V: Visitor<'a>, 86 V: Visitor<'a>,
74 N: AstNode<'a>, 87 N: AstNode<'a>,
75 F: FnOnce(N) -> <V as Visitor<'a>>::Output, 88 F: FnOnce(N) -> <V as Visitor<'a>>::Output,
76{ 89{
77 type Output = <V as Visitor<'a>>::Output; 90 type Output = <V as Visitor<'a>>::Output;
78 91
@@ -90,21 +103,19 @@ pub struct VisCtx<V, N, F> {
90} 103}
91 104
92impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F> 105impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F>
93 where 106where
94 V: VisitorCtx<'a>, 107 V: VisitorCtx<'a>,
95 N: AstNode<'a>, 108 N: AstNode<'a>,
96 F: FnOnce(N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output, 109 F: FnOnce(N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output,
97{ 110{
98 type Output = <V as VisitorCtx<'a>>::Output; 111 type Output = <V as VisitorCtx<'a>>::Output;
99 type Ctx = <V as VisitorCtx<'a>>::Ctx; 112 type Ctx = <V as VisitorCtx<'a>>::Ctx;
100 113
101 fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx> { 114 fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx> {
102 let VisCtx { inner, f, .. } = self; 115 let VisCtx { inner, f, .. } = self;
103 inner.accept(node).or_else(|ctx| 116 inner.accept(node).or_else(|ctx| match N::cast(node) {
104 match N::cast(node) { 117 None => Err(ctx),
105 None => Err(ctx), 118 Some(node) => Ok(f(node, ctx)),
106 Some(node) => Ok(f(node, ctx)) 119 })
107 }
108 )
109 } 120 }
110} 121}