-
-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - Implement AST Visitor pattern (attempt #3) #2392
Conversation
cc: @jedel1043 |
Codecov Report
@@ Coverage Diff @@
## main #2392 +/- ##
==========================================
- Coverage 39.61% 38.36% -1.26%
==========================================
Files 307 310 +3
Lines 23507 24273 +766
==========================================
Hits 9313 9313
- Misses 14194 14960 +766
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
boa_engine/src/value/mod.rs
Outdated
pub use equality::*; | ||
pub use hash::*; | ||
// pub use equality::*; | ||
// pub use hash::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was causing a build failure for me locally...
error: unreachable `pub` item
--> boa_engine/src/value/mod.rs:43:9
|
43 | pub use equality::*;
| --- ^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
|
= help: or consider exporting it for use by other crates
note: the lint level is defined here
--> boa_engine/src/lib.rs:41:5
|
41 | unreachable_pub,
| ^^^^^^^^^^^^^^^
Didn't mean to commit it, but easy enough to revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this didn't break any APIs, I think you can remove it
58fa8c2
to
ad14efe
Compare
Okay -- I have updated the style of visitor. Unsure how licensing here needs to work, but seeing as it's only a few lines that are exact or near-exact matches (given that you can't do them in literally any other way), I expect this is not a major concern. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy with the design :) It just needs some documentation on macros and PrinterVisitor
, and the implementation of the implementers, but I'm happy about its look. I'd like to know the thoughts of others too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API looks perfect! Missing implementations aside, I think it would be good to have an example on how to implement Visitor
in the module documentation (e.g. https://docs.rs/serde/latest/serde/de/trait.Visitor.html).
Changes look good! It's just missing some clippy fixes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remains addressing the commented out use
s from the value
module. Everything else looks really good. Very nice job!
Ah no, I meant deleting the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good to me. The only thing here is that we might want to feature-gate this, and not compile the full visitor implementation for all cases, since we are not using it in the engine itself, if I'm not mistaken, at least for now.
Style-wise, we usually prefer use
statements to be grouped, and in levels, but we have a bit of a mess, and this can be solved by the nightly cargo fmt
, so that could probably be a separate thing.
In any case, I'm OK to merge this as-is and then add the feature before the 0.17 in a separate PR, for example.
I would offer to maybe replace some internal mechanisms. For example: https://github.com/boa-dev/boa/blob/main/boa_engine/src/syntax/ast/statement/mod.rs#L140 |
Yeah, I was planning on replacing |
For sure. Unless I'm mistaken, the code I linked becomes: pub(crate) fn var_declared_names(&self, vars: &mut FxHashSet<Identifier>) {
struct IdentVisitor<'a> {
vars: &'a mut FxHashSet<Identifier>,
}
impl<'a, 'ast> Visitor<'ast> for IdentVisitor<'a> {
type BreakTy = Infallible;
fn visit_identifier(&mut self, node: &'ast Identifier) -> ControlFlow<Self::BreakTy> {
self.vars.insert(*node);
ControlFlow::Continue(())
}
}
let mut visitor = IdentVisitor { vars };
visitor.visit_statement(self);
} |
Hm, actually, that causes a bunch of errors. I am not sure why. |
Not the correct definition. That includes vars inside functions, which is incorrect since those are part of the var environment of the function. Here's the correct algorithm for https://tc39.es/ecma262/#sec-static-semantics-vardeclarednames EDIT: It also includes all other identifiers, including |
Gonna merge this as it is, and I'll create some PRs to take advantage of the new visitor. bors r+ |
This Pull Request closes no specific issue, but allows for analysis and post-processing passes by both internal and external developers. It changes the following: - Adds a Visitor trait, to be implemented by visitors of a particular node type. - Adds `Type`Visitor traits which offer access to private members of a node. - Adds an example which demonstrates the use of Visitor traits by walking over an AST and printing its contents. At this time, the PR is more of a demonstration of intent rather than a full PR. Once it's in a satisfactory state, I'll mark it as not a draft. Co-authored-by: Addison Crump <[email protected]>
Pull request successfully merged into main. Build succeeded: |
This Pull Request closes no specific issue, but allows for analysis and post-processing passes by both internal and external developers.
It changes the following:
Type
Visitor traits which offer access to private members of a node.At this time, the PR is more of a demonstration of intent rather than a full PR. Once it's in a satisfactory state, I'll mark it as not a draft.