Skip to content

Commit 2d00350

Browse files
committed
chore: Add comments
1 parent 4b171f2 commit 2d00350

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

rusty_linter/src/core/visitor.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@ use rusty_parser::*;
33

44
use crate::LintErrorPos;
55

6+
/// The result of a visitor.
67
pub type VisitResult = Result<(), LintErrorPos>;
78

9+
/// A visitor can visit an immutable element by reference.
10+
/// The visitor can mutate its own state.
811
pub 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+
1624
impl<P, T> Visitor<Vec<T>> for P
1725
where
1826
P: Visitor<T>,
@@ -25,26 +33,35 @@ where
2533
}
2634
}
2735

36+
// Blanket implementation for Positioned
37+
2838
impl<P, T> Visitor<Positioned<T>> for P
2939
where
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+
3962
impl<P> Visitor<GlobalStatement> for P
4063
where
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 {

rusty_linter/src/pre_linter/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::core::IntoTypeQualifier;
2-
use crate::core::PosVisitor;
2+
use crate::core::SetPosition;
3+
use crate::core::ShallowGlobalStatementVisitor;
34
use crate::core::TypeResolverImpl;
45
use crate::core::VisitResult;
56
use crate::core::Visitor;
@@ -94,8 +95,8 @@ impl MainContext {
9495
}
9596
}
9697

97-
impl PosVisitor for MainContext {
98-
fn set_pos(&mut self, pos: Position) {
98+
impl SetPosition for MainContext {
99+
fn set_position(&mut self, pos: Position) {
99100
self.declaration_pos = pos;
100101
}
101102
}
@@ -180,3 +181,5 @@ impl Visitor<UserDefinedType> for MainContext {
180181
)
181182
}
182183
}
184+
185+
impl ShallowGlobalStatementVisitor for MainContext {}

0 commit comments

Comments
 (0)