Skip to content

Commit

Permalink
Use nested structs in NodeKind
Browse files Browse the repository at this point in the history
Summary:
In order to allow use of `Default` and more restrictive types
in parameters in the future, nest a struct in each of the variants
of `enum NodeKind` instead of having fields be accessed directly.

Reviewed By: tmikov

Differential Revision: D31114633

fbshipit-source-id: 514292a6051c0a8ff1707dfd18b2e42275c2d580
  • Loading branch information
avp authored and facebook-github-bot committed Sep 23, 2021
1 parent 0232b87 commit fc28e1c
Show file tree
Hide file tree
Showing 10 changed files with 888 additions and 837 deletions.
8 changes: 4 additions & 4 deletions unsupported/juno/src/ast/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

use super::{
AssignmentExpressionOperator, BinaryExpressionOperator, Context, ExportKind, ImportKind,
LogicalExpressionOperator, MethodDefinitionKind, NodeKind, NodeLabel, NodeList, NodePtr,
NodeString, PropertyKind, UnaryExpressionOperator, UpdateExpressionOperator,
VariableDeclarationKind,
LogicalExpressionOperator, MethodDefinitionKind, NodeLabel, NodeList, NodePtr, NodeString,
PropertyKind, UnaryExpressionOperator, UpdateExpressionOperator, VariableDeclarationKind,
};
use std::io::{self, Write};
use support::{case::ascii_snake_to_camel, json::*};
Expand All @@ -31,12 +30,13 @@ macro_rules! gen_dumper {
$(,)?
}) => {
fn dump_node<W: Write>(ctx: &Context, node: NodePtr, emitter: &mut JSONEmitter<W>) {
use crate::ast::*;
emitter.open_dict();
emitter.emit_key("type");
emitter.emit_string(node.get(ctx).kind.name());
match &node.get(ctx).kind {
$(
NodeKind::$kind $({$($field),*})? => {
NodeKind::$kind($kind {$($($field),*)?}) => {
$($(
emitter.emit_key(&ascii_snake_to_camel(stringify!($field)));
$field.dump(ctx, emitter);
Expand Down
16 changes: 11 additions & 5 deletions unsupported/juno/src/ast/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ macro_rules! gen_nodekind_enum {
#[derive(Debug)]
pub enum NodeKind {
// Create each field in the enum.
$($kind($kind),)*
}

$(
#[derive(Debug)]
pub struct $kind {
// Create each field in the struct.
$(
$kind $({
$($field : $type),*
})?
),*
$(pub $field : $type),*
)?
}
)*

impl NodeKind {
/// Visit the child fields of this kind.
/// `node` is the node for which this is the kind.
pub fn visit_children< V: Visitor>(&self, ctx: &Context, visitor: &mut V, node: NodePtr) {
match self {
$(
Self::$kind $({$($field),*})? => {
Self::$kind($kind {$($($field),*)?}) => {
$($(
$field.visit(ctx, visitor, node);
)*)?
Expand Down
2 changes: 1 addition & 1 deletion unsupported/juno/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ mod dump;
mod kind;
mod validate;

pub use kind::NodeKind;
use kind::NodeVariant;

pub use dump::{dump_json, Pretty};
pub use kind::*;
pub use validate::{validate_tree, ValidationError};

/// The storage for AST nodes.
Expand Down
23 changes: 11 additions & 12 deletions unsupported/juno/src/ast/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

use super::{
AssignmentExpressionOperator, BinaryExpressionOperator, Context, ExportKind, ImportKind,
LogicalExpressionOperator, MethodDefinitionKind, NodeKind, NodeLabel, NodeList, NodePtr,
NodeString, NodeVariant, PropertyKind, UnaryExpressionOperator, UpdateExpressionOperator,
VariableDeclarationKind, Visitor,
kind::*, AssignmentExpressionOperator, BinaryExpressionOperator, Context, ExportKind,
ImportKind, LogicalExpressionOperator, MethodDefinitionKind, NodeKind, NodeLabel, NodeList,
NodePtr, NodeString, NodeVariant, PropertyKind, UnaryExpressionOperator,
UpdateExpressionOperator, VariableDeclarationKind, Visitor,
};

macro_rules! gen_validate_fn {
Expand All @@ -29,7 +29,7 @@ macro_rules! gen_validate_fn {
fn validate_node(ctx: &Context, node: NodePtr) -> Result<(), ValidationError> {
match &node.get(ctx).kind {
$(
NodeKind::$kind $({$($field),*})? => {
NodeKind::$kind($kind {$($($field),*)?}) => {
// Run the validation for each child.
// Use `true &&` to make it work when there's no children.
$($(
Expand Down Expand Up @@ -153,34 +153,33 @@ fn instanceof(subtype: NodeVariant, supertype: NodeVariant) -> bool {
/// Custom validation function for constraints which can't be expressed
/// using just the inheritance structure in NodeKind.
fn validate_custom(ctx: &Context, node: NodePtr) -> Result<(), ValidationError> {
use NodeKind::*;
match &node.get(ctx).kind {
MemberExpression {
NodeKind::MemberExpression(MemberExpression {
property,
object: _,
computed,
}
| OptionalMemberExpression {
})
| NodeKind::OptionalMemberExpression(OptionalMemberExpression {
property,
object: _,
computed,
optional: _,
} => {
}) => {
if *computed {
property.validate_child(ctx, node, &[NodeVariant::Expression])?;
} else {
property.validate_child(ctx, node, &[NodeVariant::Identifier])?;
}
}

Property {
NodeKind::Property(Property {
key,
value,
kind,
computed,
method,
shorthand,
} => {
}) => {
if *computed && *shorthand {
return Err(ValidationError {
node,
Expand Down
Loading

0 comments on commit fc28e1c

Please sign in to comment.