Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 2fcce27

Browse files
committed
Enhance the documentation.
1 parent 1b345b3 commit 2fcce27

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//! - `parser`, which exports the parsing interface.
1010
//! - `syntax`, which exports the AST and language definitions.
1111
//! - `transpiler`, which provides you with GLSL transpilers.
12+
//! - `visitor`, which gives you a way to visit AST nodes and mutate them, both with inner and
13+
//! outer mutation.
1214
//!
1315
//! Feel free to inspect those modules for further information.
1416
//!
@@ -22,6 +24,10 @@
2224
//! [`ExternalDeclaration`]s. An [`ExternalDeclaration`] is just a declaration at the top-most level
2325
//! of a shader. It can be a global, uniform declarations, vertex attributes, a function, a
2426
//! structure, etc.
27+
//!
28+
//! The crate is also getting more and more combinators and functions to transform the AST or create
29+
//! nodes with regular Rust. The [`Visitor`] trait will be a great friend of yours when you will
30+
//! want to cope with deep mutation, filtering and validation.
2531
2632
#[macro_use]
2733
extern crate nom;

src/visitor.rs

+44-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
//! AST visitors (i.e. on-the-fly mutation at different places in the AST).
22
//!
3-
//! Visitors are mutable objects that can mutate parts of an AST while traversing it. Visitors must
4-
//! implement the [`Visitor`] trait in order to be usable.
3+
//! Visitors are mutable objects that can mutate parts of an AST while traversing it. You can see
4+
//! them as flexible mutations taking place on *patterns* representing your AST – they get called
5+
//! everytime an interesting node gets visited. Because of their mutable nature, you can accumulate
6+
//! a state as you traverse the AST and implement exotic filtering.
7+
//!
8+
//! Visitors must implement the [`Visitor`] trait in order to be usable.
9+
//!
10+
//! In order to visit any part of an AST (from its very top root or from any part of it), you must
11+
//! use the [`Host`] interface, that provides the `Host::visit` function.
512
613
use syntax;
714

815
/// Visit strategy after having visited an AST node.
916
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
1017
pub enum Visit {
11-
/// The visitor will go deeper in the AST by visiting all the children.
18+
/// The visitor will go deeper in the AST by visiting all the children, if any. If no children are
19+
/// present are if having children doesn’t make sense for a specific part of the AST, this
20+
/// strategy will be ignored.
1221
Children,
1322
/// The visitor won’t visit children nor siblings and will go up.
1423
Parent
1524
}
1625

26+
/// Visitor object, visiting AST nodes.
1727
pub trait Visitor {
1828
fn visit_translation_unit(&mut self, _: &mut syntax::TranslationUnit) -> Visit {
1929
Visit::Children
@@ -23,23 +33,27 @@ pub trait Visitor {
2333
Visit::Children
2434
}
2535

36+
fn visit_identifier(&mut self, _: &mut syntax::Identifier) -> Visit {
37+
Visit::Children
38+
}
39+
2640
fn visit_arrayed_identifier(&mut self, _: &mut syntax::ArrayedIdentifier) -> Visit {
2741
Visit::Children
2842
}
2943

30-
fn visit_block(&mut self, _: &mut syntax::Block) -> Visit {
44+
fn visit_type_name(&mut self, _: &mut syntax::TypeName) -> Visit {
3145
Visit::Children
3246
}
3347

34-
fn visit_for_init_statement(&mut self, _: &mut syntax::ForInitStatement) -> Visit {
48+
fn visit_block(&mut self, _: &mut syntax::Block) -> Visit {
3549
Visit::Children
3650
}
3751

38-
fn visit_for_rest_statement(&mut self, _: &mut syntax::ForRestStatement) -> Visit {
52+
fn visit_for_init_statement(&mut self, _: &mut syntax::ForInitStatement) -> Visit {
3953
Visit::Children
4054
}
4155

42-
fn visit_full_specified_type(&mut self, _: &mut syntax::FullySpecifiedType) -> Visit {
56+
fn visit_for_rest_statement(&mut self, _: &mut syntax::ForRestStatement) -> Visit {
4357
Visit::Children
4458
}
4559

@@ -123,6 +137,10 @@ pub trait Visitor {
123137
Visit::Children
124138
}
125139

140+
fn visit_full_specified_type(&mut self, _: &mut syntax::FullySpecifiedType) -> Visit {
141+
Visit::Children
142+
}
143+
126144
fn visit_array_specifier(&mut self, _: &mut syntax::ArraySpecifier) -> Visit {
127145
Visit::Children
128146
}
@@ -214,17 +232,28 @@ pub trait Visitor {
214232
fn visit_expr_statement(&mut self, _: &mut syntax::ExprStatement) -> Visit {
215233
Visit::Children
216234
}
217-
218-
fn visit_identifier(&mut self, _: &mut syntax::Identifier) -> Visit {
219-
Visit::Children
220-
}
221-
222-
fn visit_type_name(&mut self, _: &mut syntax::TypeName) -> Visit {
223-
Visit::Children
224-
}
225235
}
226236

237+
/// Part of the AST that can be visited.
238+
///
239+
/// You shouldn’t have to worry about this type nor how to implement it – it’s completely
240+
/// implemented for you. However, it works in a pretty simple way: any implementor of [`Host`] can
241+
/// be used with a [`Visitor`].
242+
///
243+
/// The idea is that visiting an AST node is a two-step process:
244+
///
245+
/// - First, you *can* get your visitor called once as soon as an interesting node gets visited.
246+
/// For instance, if your visitor has an implementation for visiting expressions, everytime an
247+
/// expression gets visited, your visitor will run.
248+
/// - If your implementation of visiting an AST node returns `Visit::Children` and if the given
249+
/// node has children, the visitor will go deeper, invoking other calls if you have defined any.
250+
/// A typical pattern you might want to do is to implement your visitor to gets run on all
251+
/// typenames. Since expressions contains variables, you will get your visitor called once again
252+
/// there.
253+
/// - Notice that since visitors are mutable, you can accumulate a state as you go deeper in the
254+
/// AST to implement various checks and validations.
227255
pub trait Host {
256+
/// Visit an AST node.
228257
fn visit<V>(&mut self, visitor: &mut V) where V: Visitor;
229258
}
230259

0 commit comments

Comments
 (0)