Repository navigation
Add an AST visitor/walker - #1954
addisoncrump wants to merge 11 commits into
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1954 +/- ##
==========================================
- Coverage 42.04% 39.98% -2.06%
==========================================
Files 231 232 +1
Lines 21271 22364 +1093
==========================================
Hits 8943 8943
- Misses 12328 13421 +1093
Continue to review full report at Codecov.
|
raskad
left a comment
There was a problem hiding this comment.
There is no good way to avoid making all of the node struct field pub(crate) with the visitor architecture right? I assume we would need getters and setters for all of them for this to make sense? Not that I'm against pub(crate), but I think we should at least discuss it.
Other than that, I think some documentation on the Visitor trait would be good, so that to usage is obvious.
I don't think it's worth the massive amount of boilerplate that change would add to the AST. Usually getters and setters are used when:
|
jedel1043
left a comment
There was a problem hiding this comment.
Looks really good! Documenting how to use the trait would be good though.
Also, did you measure the performance of the fuzzer with this?
|
@jedel1043 please confirm requested changes :) |
Razican
left a comment
There was a problem hiding this comment.
Thanks for the contribution! And sorry for my delay in reviewing it. I've made some comments, mostly related to documentation. The design is good from my point of view.
Also, due to the delay in the review, this will need a rebase.
| #[allow(clippy::unsafe_derive_deserialize)] | ||
| #[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] | ||
| pub(crate) struct FormalParameterListFlags: u8 { | ||
| pub struct FormalParameterListFlags: u8 { |
| pub mod template; | ||
| pub mod throw; | ||
| pub mod try_node; | ||
| pub mod visit; |
There was a problem hiding this comment.
Could we add a feature flag for this? I don't think we need it in normal operation, right? and we would avoid compiling it for most of the cases. We would need to add the feature to CI, though, to make sure we still test it.
| /// use boa_engine::syntax::ast::node::visit::Visitor; | ||
| /// use boa_engine::Context; |
There was a problem hiding this comment.
We usually prefer scoped imports:
| /// use boa_engine::syntax::ast::node::visit::Visitor; | |
| /// use boa_engine::Context; | |
| /// use boa_engine::{Context, syntax::ast::node::visit::Visitor}; |
| /// find: Sym, | ||
| /// replace: Sym, | ||
| /// } | ||
| /// impl<'ast> Visitor<'ast> for NameReplacer { |
There was a problem hiding this comment.
| /// impl<'ast> Visitor<'ast> for NameReplacer { | |
| /// | |
| /// impl<'ast> Visitor<'ast> for NameReplacer { |
| /// use boa_engine::Context; | ||
| /// use boa_interner::Sym; | ||
| /// | ||
| /// struct NameReplacer { |
There was a problem hiding this comment.
Even if this is just an example, I would still derive Debug, Clone and Copy.
| type Output; | ||
| type Error; | ||
|
|
||
| fn visit_node(&mut self, n: &'ast Node) -> Result<Self::Output, Self::Error> { |
There was a problem hiding this comment.
These functions would need some documentation, so that they show up in the implementing types, and users know how to implement the trait, especially being pub. All methods would need an explanation, in case they want to be used by external crates.
|
|
||
| fn visit_node(&mut self, n: &'ast Node) -> Result<Self::Output, Self::Error> { | ||
| self.walk_node(n) | ||
| } |
There was a problem hiding this comment.
I've seen this in multiple places. I understand that in some cases this is done because the two functions without spacing are related to each others, but it's more clean if we add spaces, it should look better, especially once they have documentation.
| } | |
| } | |
| type Output; | ||
| type Error; |
There was a problem hiding this comment.
These types would need some documentation.
| } | ||
| } | ||
|
|
||
| fn get_default_ok() -> Result<Self::Output, Self::Error>; |
There was a problem hiding this comment.
This function, being mandatory for external types to implement, needs documentation on how to use it, and what it's supposed to do.
|
Closing as the internal API has changed dramatically. |
This pull request seeks to develop an AST visitor/walker so that we can simplify a lot of the context-sensitive parsing.