@@ -3,16 +3,24 @@ use rusty_parser::*;
33
44use crate :: LintErrorPos ;
55
6+ /// The result of a visitor.
67pub type VisitResult = Result < ( ) , LintErrorPos > ;
78
9+ /// A visitor can visit an immutable element by reference.
10+ /// The visitor can mutate its own state.
811pub trait Visitor < T > {
12+ /// Visits the given element.
913 fn visit ( & mut self , element : & T ) -> VisitResult ;
1014}
1115
12- pub trait PosVisitor {
13- fn set_pos ( & mut self , pos : Position ) ;
16+ /// Indicates an object that can hold the most recently visited [Position].
17+ pub trait SetPosition {
18+ /// Sets the most recently visited [Position].
19+ fn set_position ( & mut self , pos : Position ) ;
1420}
1521
22+ // Blanket implementation for Vec
23+
1624impl < P , T > Visitor < Vec < T > > for P
1725where
1826 P : Visitor < T > ,
@@ -25,26 +33,35 @@ where
2533 }
2634}
2735
36+ // Blanket implementation for Positioned
37+
2838impl < P , T > Visitor < Positioned < T > > for P
2939where
30- P : Visitor < T > + PosVisitor ,
40+ P : Visitor < T > + SetPosition ,
3141{
3242 fn visit ( & mut self , element : & Positioned < T > ) -> VisitResult {
3343 let Positioned { element, pos } = element;
34- self . set_pos ( * pos) ;
44+ self . set_position ( * pos) ;
3545 self . visit ( element)
3646 }
3747}
3848
49+ /// Indicates a visitor that can visit global statements
50+ /// but does not enter the implementations of functions or subs.
51+ pub trait ShallowGlobalStatementVisitor :
52+ Visitor < DefType >
53+ + Visitor < FunctionDeclaration >
54+ + Visitor < FunctionImplementation >
55+ + Visitor < Statement >
56+ + Visitor < SubDeclaration >
57+ + Visitor < SubImplementation >
58+ + Visitor < UserDefinedType >
59+ {
60+ }
61+
3962impl < P > Visitor < GlobalStatement > for P
4063where
41- P : Visitor < DefType >
42- + Visitor < FunctionDeclaration >
43- + Visitor < FunctionImplementation >
44- + Visitor < Statement >
45- + Visitor < SubDeclaration >
46- + Visitor < SubImplementation >
47- + Visitor < UserDefinedType > ,
64+ P : ShallowGlobalStatementVisitor ,
4865{
4966 fn visit ( & mut self , element : & GlobalStatement ) -> VisitResult {
5067 match element {
0 commit comments