Skip to content

Add an AST visitor/walker - #1954

Closed
addisoncrump wants to merge 11 commits into
boa-dev:mainfrom
addisoncrump:ast-walking
Closed

addisoncrump wants to merge 11 commits into
boa-dev:mainfrom
addisoncrump:ast-walking

Conversation

@addisoncrump

Copy link
Copy Markdown
Contributor

This pull request seeks to develop an AST visitor/walker so that we can simplify a lot of the context-sensitive parsing.

@codecov

codecov Bot commented Mar 17, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #1954 (cc1be71) into main (48d8b42) will decrease coverage by 2.05%.
The diff coverage is 1.43%.

@@            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     
Impacted Files Coverage Δ
boa_engine/src/syntax/ast/node/array/mod.rs 33.33% <ø> (ø)
boa_engine/src/syntax/ast/node/await_expr/mod.rs 28.57% <ø> (ø)
boa_engine/src/syntax/ast/node/block/mod.rs 50.00% <ø> (ø)
boa_engine/src/syntax/ast/node/call/mod.rs 69.23% <ø> (ø)
.../syntax/ast/node/conditional/conditional_op/mod.rs 64.28% <ø> (ø)
...x/ast/node/declaration/async_generator_decl/mod.rs 13.33% <0.00%> (ø)
...x/ast/node/declaration/async_generator_expr/mod.rs 5.55% <0.00%> (ø)
.../src/syntax/ast/node/declaration/class_decl/mod.rs 70.33% <ø> (ø)
.../syntax/ast/node/declaration/generator_decl/mod.rs 9.52% <0.00%> (ø)
.../syntax/ast/node/declaration/generator_expr/mod.rs 5.55% <0.00%> (ø)
... and 35 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 48d8b42...cc1be71. Read the comment docs.

@addisoncrump
addisoncrump marked this pull request as ready for review March 18, 2022 03:04
@Razican Razican added this to the v0.15.0 milestone Mar 18, 2022
@Razican Razican linked an issue Mar 18, 2022 that may be closed by this pull request
@Razican Razican added C-Parser Issues surrounding the parser A-Internal Changes that don't modify execution behaviour C-AST Issue surrounding the abstract syntax tree and removed C-Parser Issues surrounding the parser labels Mar 18, 2022

@raskad raskad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jedel1043

Copy link
Copy Markdown
Member

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.

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:

  • you need to ensure non-breaking changes for the users (which we don't, since the AST is currently not exposed to our users) or
  • you need to maintain certain invariants that the type system cannot maintain by itself (and I think there are very few AST types that have that requirement). We could instead find those types, use setters and getters for them, and default to pub(crate) for everything else.

@jedel1043 jedel1043 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good! Documenting how to use the trait would be good though.
Also, did you measure the performance of the fuzzer with this?

@Razican Razican modified the milestones: v0.15.0, v0.16.0 Jun 1, 2022
@addisoncrump

Copy link
Copy Markdown
Contributor Author

@jedel1043 please confirm requested changes :)

@jedel1043 jedel1043 added the A-Enhancement New feature or request label Jul 6, 2022
@Razican
Razican requested review from jedel1043 and raskad July 7, 2022 11:39
@addisoncrump
addisoncrump marked this pull request as draft July 7, 2022 18:00

@Razican Razican left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to expose this?

pub mod template;
pub mod throw;
pub mod try_node;
pub mod visit;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +33 to +34
/// use boa_engine::syntax::ast::node::visit::Visitor;
/// use boa_engine::Context;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually prefer scoped imports:

Suggested change
/// 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// impl<'ast> Visitor<'ast> for NameReplacer {
///
/// impl<'ast> Visitor<'ast> for NameReplacer {

/// use boa_engine::Context;
/// use boa_interner::Sym;
///
/// struct NameReplacer {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
}
}

Comment on lines +84 to +85
type Output;
type Error;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types would need some documentation.

}
}

fn get_default_ok() -> Result<Self::Output, Self::Error>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function, being mandatory for external types to implement, needs documentation on how to use it, and what it's supposed to do.

@Razican Razican modified the milestones: v0.16.0, v0.17.0 Sep 19, 2022
@addisoncrump

Copy link
Copy Markdown
Contributor Author

Closing as the internal API has changed dramatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Enhancement New feature or request A-Internal Changes that don't modify execution behaviour C-AST Issue surrounding the abstract syntax tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catching lexer/parser bugs with fuzzed input

4 participants