Skip to content
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

Closed
wants to merge 34 commits into from

Conversation

addisoncrump
Copy link
Contributor

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 TypeVisitor 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.

@addisoncrump
Copy link
Contributor Author

cc: @jedel1043

@codecov
Copy link

codecov bot commented Oct 29, 2022

Codecov Report

Merging #2392 (468ecb9) into main (c72e4c2) will decrease coverage by 1.25%.
The diff coverage is 0.00%.

❗ Current head 468ecb9 differs from pull request most recent head 58aa397. Consider uploading reports for the commit 58aa397 to get more accurate results

@@            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     
Impacted Files Coverage Δ
boa_engine/src/syntax/ast/declaration/mod.rs 55.31% <0.00%> (-23.47%) ⬇️
boa_engine/src/syntax/ast/declaration/variable.rs 27.27% <0.00%> (-12.73%) ⬇️
boa_engine/src/syntax/ast/expression/access.rs 17.64% <0.00%> (-5.43%) ⬇️
boa_engine/src/syntax/ast/expression/await.rs 7.14% <0.00%> (-1.20%) ⬇️
boa_engine/src/syntax/ast/expression/call.rs 21.95% <0.00%> (-11.39%) ⬇️
boa_engine/src/syntax/ast/expression/identifier.rs 14.28% <0.00%> (-1.51%) ⬇️
..._engine/src/syntax/ast/expression/literal/array.rs 25.80% <0.00%> (-6.20%) ⬇️
...oa_engine/src/syntax/ast/expression/literal/mod.rs 23.33% <0.00%> (-5.84%) ⬇️
...ne/src/syntax/ast/expression/literal/object/mod.rs 46.93% <0.00%> (-6.55%) ⬇️
...gine/src/syntax/ast/expression/literal/template.rs 8.10% <0.00%> (-3.90%) ⬇️
... and 43 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

pub use equality::*;
pub use hash::*;
// pub use equality::*;
// pub use hash::*;
Copy link
Contributor Author

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.

Copy link
Member

@jedel1043 jedel1043 Nov 1, 2022

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

@addisoncrump
Copy link
Contributor Author

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.

Copy link
Member

@Razican Razican left a 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.

Copy link
Member

@jedel1043 jedel1043 left a 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).

boa_examples/Cargo.toml Outdated Show resolved Hide resolved
@addisoncrump addisoncrump marked this pull request as ready for review November 1, 2022 20:26
@jedel1043
Copy link
Member

Changes look good! It's just missing some clippy fixes.

Copy link
Member

@jedel1043 jedel1043 left a 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 uses from the value module. Everything else looks really good. Very nice job!

@jedel1043
Copy link
Member

Ah no, I meant deleting the uses altogether, since those modules don't export any new types.

@jedel1043 jedel1043 added API ast Issue surrounding the abstract syntax tree enhancement New feature or request labels Nov 2, 2022
@jedel1043 jedel1043 added this to the v0.17.0 milestone Nov 2, 2022
Copy link
Member

@Razican Razican left a 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.

@addisoncrump
Copy link
Contributor Author

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

@jedel1043
Copy link
Member

Yeah, I was planning on replacing contains and contains_arguments with a visitor

@addisoncrump
Copy link
Contributor Author

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);
    }

@addisoncrump
Copy link
Contributor Author

Hm, actually, that causes a bunch of errors. I am not sure why.

@jedel1043
Copy link
Member

jedel1043 commented Nov 2, 2022

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 var_declared_names:

https://tc39.es/ecma262/#sec-static-semantics-vardeclarednames

EDIT: It also includes all other identifiers, including let, const and undeclared variables, which should be excluded.

@jedel1043
Copy link
Member

Gonna merge this as it is, and I'll create some PRs to take advantage of the new visitor.

bors r+

bors bot pushed a commit that referenced this pull request Nov 2, 2022
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 <addison.crump@cispa.de>
@bors
Copy link

bors bot commented Nov 2, 2022

Pull request successfully merged into main.

Build succeeded:

@bors bors bot changed the title Implement AST Visitor pattern (attempt #3) [Merged by Bors] - Implement AST Visitor pattern (attempt #3) Nov 2, 2022
@bors bors bot closed this Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API ast Issue surrounding the abstract syntax tree enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants