From 178794ba5f0548b8d28c106bf8f6aa3b41bcb9b9 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 8 Jun 2024 06:59:11 +0200 Subject: [PATCH] Use Rust macros for converters where possible (#5532) * Use macro for flags * Use if-let intead of .map * Create basic macro for simple types So far it supports flags and optional nodes * Support float and optional strings * Support simple nodes * Support fixed strings * Support variable strings * Use more macros * Convert node list * Add remaining macros * Fix linting --- .../ast_nodes/arrow_function_expression.rs | 24 +- .../src/ast_nodes/assignment_expression.rs | 30 +- .../src/ast_nodes/await_expression.rs | 18 +- .../src/ast_nodes/break_statement.rs | 20 +- .../src/ast_nodes/call_expression.rs | 11 +- .../src/ast_nodes/conditional_expression.rs | 28 +- .../src/ast_nodes/continue_statement.rs | 20 +- .../src/ast_nodes/debugger_statement.rs | 12 +- rust/parse_ast/src/ast_nodes/directive.rs | 21 +- .../src/ast_nodes/do_while_statement.rs | 23 +- .../src/ast_nodes/empty_statement.rs | 12 +- .../src/ast_nodes/export_specifier.rs | 27 +- .../src/ast_nodes/expression_statement.rs | 19 +- .../src/ast_nodes/for_in_statement.rs | 27 +- .../src/ast_nodes/for_of_statement.rs | 36 +- rust/parse_ast/src/ast_nodes/for_statement.rs | 37 +- rust/parse_ast/src/ast_nodes/if_statement.rs | 29 +- .../src/ast_nodes/import_default_specifier.rs | 19 +- .../ast_nodes/import_namespace_specifier.rs | 19 +- .../src/ast_nodes/import_specifier.rs | 25 +- .../src/ast_nodes/labeled_statement.rs | 23 +- .../src/ast_nodes/literal_big_int.rs | 27 +- .../src/ast_nodes/literal_boolean.rs | 21 +- rust/parse_ast/src/ast_nodes/literal_null.rs | 13 +- .../parse_ast/src/ast_nodes/literal_number.rs | 24 +- .../src/ast_nodes/literal_reg_exp.rs | 21 +- .../parse_ast/src/ast_nodes/literal_string.rs | 23 +- .../src/ast_nodes/member_expression.rs | 19 +- .../src/ast_nodes/method_definition.rs | 19 +- .../src/ast_nodes/object_expression.rs | 24 +- .../parse_ast/src/ast_nodes/object_pattern.rs | 24 +- .../src/ast_nodes/private_identifier.rs | 20 +- rust/parse_ast/src/ast_nodes/property.rs | 59 +- .../src/ast_nodes/property_definition.rs | 18 +- .../src/ast_nodes/return_statement.rs | 20 +- .../src/ast_nodes/sequence_expression.rs | 25 +- .../src/ast_nodes/shared/function_node.rs | 23 +- rust/parse_ast/src/ast_nodes/static_block.rs | 24 +- rust/parse_ast/src/ast_nodes/super_element.rs | 14 +- rust/parse_ast/src/ast_nodes/switch_case.rs | 31 +- .../src/ast_nodes/switch_statement.rs | 29 +- .../ast_nodes/tagged_template_expression.rs | 23 +- .../src/ast_nodes/template_element.rs | 34 +- .../src/ast_nodes/this_expression.rs | 13 +- .../src/ast_nodes/throw_statement.rs | 18 +- .../src/ast_nodes/unary_expression.rs | 25 +- .../src/ast_nodes/update_expression.rs | 34 +- .../src/ast_nodes/while_statement.rs | 23 +- .../src/ast_nodes/yield_expression.rs | 29 +- rust/parse_ast/src/convert_ast/converter.rs | 3 +- .../convert_ast/converter/ast_constants.rs | 223 ----- .../src/convert_ast/converter/ast_macros.rs | 889 ++++++++++++++++++ scripts/ast-types.js | 104 +- scripts/generate-ast-converters.js | 1 + scripts/generate-ast-macros.js | 151 +++ scripts/generate-rust-constants.js | 25 +- scripts/helpers.js | 8 + 57 files changed, 1435 insertions(+), 1126 deletions(-) create mode 100644 rust/parse_ast/src/convert_ast/converter/ast_macros.rs create mode 100644 scripts/generate-ast-macros.js diff --git a/rust/parse_ast/src/ast_nodes/arrow_function_expression.rs b/rust/parse_ast/src/ast_nodes/arrow_function_expression.rs index 2b8a692a9..c737e4580 100644 --- a/rust/parse_ast/src/ast_nodes/arrow_function_expression.rs +++ b/rust/parse_ast/src/ast_nodes/arrow_function_expression.rs @@ -2,13 +2,12 @@ use swc_ecma_ast::{ArrowExpr, BlockStmtOrExpr}; use crate::convert_ast::annotations::AnnotationKind; use crate::convert_ast::converter::ast_constants::{ - ARROW_FUNCTION_EXPRESSION_ANNOTATIONS_OFFSET, ARROW_FUNCTION_EXPRESSION_ASYNC_FLAG, - ARROW_FUNCTION_EXPRESSION_BODY_OFFSET, ARROW_FUNCTION_EXPRESSION_EXPRESSION_FLAG, - ARROW_FUNCTION_EXPRESSION_FLAGS_OFFSET, ARROW_FUNCTION_EXPRESSION_GENERATOR_FLAG, + ARROW_FUNCTION_EXPRESSION_ANNOTATIONS_OFFSET, ARROW_FUNCTION_EXPRESSION_BODY_OFFSET, ARROW_FUNCTION_EXPRESSION_PARAMS_OFFSET, ARROW_FUNCTION_EXPRESSION_RESERVED_BYTES, TYPE_ARROW_FUNCTION_EXPRESSION, }; use crate::convert_ast::converter::{convert_annotation, AstConverter}; +use crate::store_arrow_function_expression_flags; impl<'a> AstConverter<'a> { pub fn store_arrow_function_expression(&mut self, arrow_expression: &ArrowExpr) { @@ -33,18 +32,13 @@ impl<'a> AstConverter<'a> { ); } // flags - let mut flags = 0u32; - if arrow_expression.is_async { - flags |= ARROW_FUNCTION_EXPRESSION_ASYNC_FLAG; - } - if arrow_expression.is_generator { - flags |= ARROW_FUNCTION_EXPRESSION_GENERATOR_FLAG; - } - if let BlockStmtOrExpr::Expr(_) = &*arrow_expression.body { - flags |= ARROW_FUNCTION_EXPRESSION_EXPRESSION_FLAG; - } - let flags_position = end_position + ARROW_FUNCTION_EXPRESSION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + store_arrow_function_expression_flags!( + self, + end_position, + async => arrow_expression.is_async, + expression => matches!(&*arrow_expression.body, BlockStmtOrExpr::Expr(_)), + generator => arrow_expression.is_generator + ); // params self.convert_item_list( &arrow_expression.params, diff --git a/rust/parse_ast/src/ast_nodes/assignment_expression.rs b/rust/parse_ast/src/ast_nodes/assignment_expression.rs index 7da1f20df..1b159de3e 100644 --- a/rust/parse_ast/src/ast_nodes/assignment_expression.rs +++ b/rust/parse_ast/src/ast_nodes/assignment_expression.rs @@ -1,10 +1,5 @@ use swc_ecma_ast::{AssignExpr, AssignOp}; -use crate::convert_ast::converter::ast_constants::{ - ASSIGNMENT_EXPRESSION_LEFT_OFFSET, ASSIGNMENT_EXPRESSION_OPERATOR_OFFSET, - ASSIGNMENT_EXPRESSION_RESERVED_BYTES, ASSIGNMENT_EXPRESSION_RIGHT_OFFSET, - TYPE_ASSIGNMENT_EXPRESSION, -}; use crate::convert_ast::converter::string_constants::{ STRING_ADDASSIGN, STRING_ANDASSIGN, STRING_ASSIGN, STRING_BITANDASSIGN, STRING_BITORASSIGN, STRING_BITXORASSIGN, STRING_DIVASSIGN, STRING_EXPASSIGN, STRING_LSHIFTASSIGN, STRING_MODASSIGN, @@ -12,22 +7,14 @@ use crate::convert_ast::converter::string_constants::{ STRING_ZEROFILLRSHIFTASSIGN, }; use crate::convert_ast::converter::AstConverter; +use crate::store_assignment_expression; impl<'a> AstConverter<'a> { pub fn store_assignment_expression(&mut self, assignment_expression: &AssignExpr) { - let end_position = self.add_type_and_start( - &TYPE_ASSIGNMENT_EXPRESSION, - &assignment_expression.span, - ASSIGNMENT_EXPRESSION_RESERVED_BYTES, - false, - ); - // left - self.update_reference_position(end_position + ASSIGNMENT_EXPRESSION_LEFT_OFFSET); - self.convert_pattern_or_expression(&assignment_expression.left); - // operator - let operator_position = end_position + ASSIGNMENT_EXPRESSION_OPERATOR_OFFSET; - self.buffer[operator_position..operator_position + 4].copy_from_slice( - match assignment_expression.op { + store_assignment_expression!( + self, + span => assignment_expression.span, + operator => match assignment_expression.op { AssignOp::Assign => &STRING_ASSIGN, AssignOp::AddAssign => &STRING_ADDASSIGN, AssignOp::SubAssign => &STRING_SUBASSIGN, @@ -45,11 +32,8 @@ impl<'a> AstConverter<'a> { AssignOp::OrAssign => &STRING_ORASSIGN, AssignOp::NullishAssign => &STRING_NULLISHASSIGN, }, + left => [assignment_expression.left, convert_pattern_or_expression], + right => [assignment_expression.right, convert_expression] ); - // right - self.update_reference_position(end_position + ASSIGNMENT_EXPRESSION_RIGHT_OFFSET); - self.convert_expression(&assignment_expression.right); - // end - self.add_end(end_position, &assignment_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/await_expression.rs b/rust/parse_ast/src/ast_nodes/await_expression.rs index e371afca7..820a7c9d1 100644 --- a/rust/parse_ast/src/ast_nodes/await_expression.rs +++ b/rust/parse_ast/src/ast_nodes/await_expression.rs @@ -1,22 +1,14 @@ use swc_ecma_ast::AwaitExpr; -use crate::convert_ast::converter::ast_constants::{ - AWAIT_EXPRESSION_ARGUMENT_OFFSET, AWAIT_EXPRESSION_RESERVED_BYTES, TYPE_AWAIT_EXPRESSION, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_await_expression; impl<'a> AstConverter<'a> { pub fn store_await_expression(&mut self, await_expression: &AwaitExpr) { - let end_position = self.add_type_and_start( - &TYPE_AWAIT_EXPRESSION, - &await_expression.span, - AWAIT_EXPRESSION_RESERVED_BYTES, - false, + store_await_expression!( + self, + span => await_expression.span, + argument => [await_expression.arg, convert_expression] ); - // argument - self.update_reference_position(end_position + AWAIT_EXPRESSION_ARGUMENT_OFFSET); - self.convert_expression(&await_expression.arg); - // end - self.add_end(end_position, &await_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/break_statement.rs b/rust/parse_ast/src/ast_nodes/break_statement.rs index 825f9d4dc..ef54b723d 100644 --- a/rust/parse_ast/src/ast_nodes/break_statement.rs +++ b/rust/parse_ast/src/ast_nodes/break_statement.rs @@ -1,24 +1,14 @@ use swc_ecma_ast::BreakStmt; -use crate::convert_ast::converter::ast_constants::{ - BREAK_STATEMENT_LABEL_OFFSET, BREAK_STATEMENT_RESERVED_BYTES, TYPE_BREAK_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_break_statement; impl<'a> AstConverter<'a> { pub fn store_break_statement(&mut self, break_statement: &BreakStmt) { - let end_position = self.add_type_and_start( - &TYPE_BREAK_STATEMENT, - &break_statement.span, - BREAK_STATEMENT_RESERVED_BYTES, - false, + store_break_statement!( + self, + span => break_statement.span, + label => [break_statement.label, convert_identifier] ); - // label - if let Some(label) = break_statement.label.as_ref() { - self.update_reference_position(end_position + BREAK_STATEMENT_LABEL_OFFSET); - self.convert_identifier(label); - } - // end - self.add_end(end_position, &break_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/call_expression.rs b/rust/parse_ast/src/ast_nodes/call_expression.rs index 9a95347c0..e8d3c5265 100644 --- a/rust/parse_ast/src/ast_nodes/call_expression.rs +++ b/rust/parse_ast/src/ast_nodes/call_expression.rs @@ -4,10 +4,10 @@ use swc_ecma_ast::{Expr, ExprOrSpread, OptCall, Super}; use crate::convert_ast::annotations::AnnotationKind; use crate::convert_ast::converter::ast_constants::{ CALL_EXPRESSION_ANNOTATIONS_OFFSET, CALL_EXPRESSION_ARGUMENTS_OFFSET, - CALL_EXPRESSION_CALLEE_OFFSET, CALL_EXPRESSION_FLAGS_OFFSET, CALL_EXPRESSION_OPTIONAL_FLAG, - CALL_EXPRESSION_RESERVED_BYTES, TYPE_CALL_EXPRESSION, + CALL_EXPRESSION_CALLEE_OFFSET, CALL_EXPRESSION_RESERVED_BYTES, TYPE_CALL_EXPRESSION, }; use crate::convert_ast::converter::{convert_annotation, AstConverter}; +use crate::store_call_expression_flags; impl<'a> AstConverter<'a> { pub fn store_call_expression( @@ -39,12 +39,7 @@ impl<'a> AstConverter<'a> { ); } // flags - let mut flags = 0u32; - if is_optional { - flags |= CALL_EXPRESSION_OPTIONAL_FLAG; - }; - let flags_position = end_position + CALL_EXPRESSION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + store_call_expression_flags!(self, end_position, optional => is_optional); // callee self.update_reference_position(end_position + CALL_EXPRESSION_CALLEE_OFFSET); match callee { diff --git a/rust/parse_ast/src/ast_nodes/conditional_expression.rs b/rust/parse_ast/src/ast_nodes/conditional_expression.rs index b054c3741..402d14981 100644 --- a/rust/parse_ast/src/ast_nodes/conditional_expression.rs +++ b/rust/parse_ast/src/ast_nodes/conditional_expression.rs @@ -1,30 +1,16 @@ use swc_ecma_ast::CondExpr; -use crate::convert_ast::converter::ast_constants::{ - CONDITIONAL_EXPRESSION_ALTERNATE_OFFSET, CONDITIONAL_EXPRESSION_CONSEQUENT_OFFSET, - CONDITIONAL_EXPRESSION_RESERVED_BYTES, CONDITIONAL_EXPRESSION_TEST_OFFSET, - TYPE_CONDITIONAL_EXPRESSION, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_conditional_expression; impl<'a> AstConverter<'a> { pub fn store_conditional_expression(&mut self, conditional_expression: &CondExpr) { - let end_position = self.add_type_and_start( - &TYPE_CONDITIONAL_EXPRESSION, - &conditional_expression.span, - CONDITIONAL_EXPRESSION_RESERVED_BYTES, - false, + store_conditional_expression!( + self, + span => conditional_expression.span, + test => [conditional_expression.test, convert_expression], + consequent => [conditional_expression.cons, convert_expression], + alternate => [conditional_expression.alt, convert_expression] ); - // test - self.update_reference_position(end_position + CONDITIONAL_EXPRESSION_TEST_OFFSET); - self.convert_expression(&conditional_expression.test); - // consequent - self.update_reference_position(end_position + CONDITIONAL_EXPRESSION_CONSEQUENT_OFFSET); - self.convert_expression(&conditional_expression.cons); - // alternate - self.update_reference_position(end_position + CONDITIONAL_EXPRESSION_ALTERNATE_OFFSET); - self.convert_expression(&conditional_expression.alt); - // end - self.add_end(end_position, &conditional_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/continue_statement.rs b/rust/parse_ast/src/ast_nodes/continue_statement.rs index db43e62a7..6614f7910 100644 --- a/rust/parse_ast/src/ast_nodes/continue_statement.rs +++ b/rust/parse_ast/src/ast_nodes/continue_statement.rs @@ -1,24 +1,14 @@ use swc_ecma_ast::ContinueStmt; -use crate::convert_ast::converter::ast_constants::{ - CONTINUE_STATEMENT_LABEL_OFFSET, CONTINUE_STATEMENT_RESERVED_BYTES, TYPE_CONTINUE_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_continue_statement; impl<'a> AstConverter<'a> { pub fn store_continue_statement(&mut self, continue_statement: &ContinueStmt) { - let end_position = self.add_type_and_start( - &TYPE_CONTINUE_STATEMENT, - &continue_statement.span, - CONTINUE_STATEMENT_RESERVED_BYTES, - false, + store_continue_statement!( + self, + span => continue_statement.span, + label => [continue_statement.label, convert_identifier] ); - // label - if let Some(label) = continue_statement.label.as_ref() { - self.update_reference_position(end_position + CONTINUE_STATEMENT_LABEL_OFFSET); - self.convert_identifier(label); - } - // end - self.add_end(end_position, &continue_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/debugger_statement.rs b/rust/parse_ast/src/ast_nodes/debugger_statement.rs index f41fcd08d..ac3ea4680 100644 --- a/rust/parse_ast/src/ast_nodes/debugger_statement.rs +++ b/rust/parse_ast/src/ast_nodes/debugger_statement.rs @@ -1,18 +1,10 @@ use swc_ecma_ast::DebuggerStmt; -use crate::convert_ast::converter::ast_constants::{ - DEBUGGER_STATEMENT_RESERVED_BYTES, TYPE_DEBUGGER_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_debugger_statement; impl<'a> AstConverter<'a> { pub fn store_debugger_statement(&mut self, debugger_statement: &DebuggerStmt) { - let end_position = self.add_type_and_start( - &TYPE_DEBUGGER_STATEMENT, - &debugger_statement.span, - DEBUGGER_STATEMENT_RESERVED_BYTES, - false, - ); - self.add_end(end_position, &debugger_statement.span); + store_debugger_statement!(self, span => debugger_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/directive.rs b/rust/parse_ast/src/ast_nodes/directive.rs index ff8f2eaea..6290f3151 100644 --- a/rust/parse_ast/src/ast_nodes/directive.rs +++ b/rust/parse_ast/src/ast_nodes/directive.rs @@ -1,25 +1,16 @@ use swc_atoms::JsWord; use swc_ecma_ast::ExprStmt; -use crate::convert_ast::converter::ast_constants::{ - DIRECTIVE_DIRECTIVE_OFFSET, DIRECTIVE_EXPRESSION_OFFSET, DIRECTIVE_RESERVED_BYTES, TYPE_DIRECTIVE, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_directive; impl<'a> AstConverter<'a> { pub fn store_directive(&mut self, expression_statement: &ExprStmt, directive: &JsWord) { - let end_position = self.add_type_and_start( - &TYPE_DIRECTIVE, - &expression_statement.span, - DIRECTIVE_RESERVED_BYTES, - false, + store_directive!( + self, + span => expression_statement.span, + directive => directive, + expression => [expression_statement.expr, convert_expression] ); - // directive - self.convert_string(directive, end_position + DIRECTIVE_DIRECTIVE_OFFSET); - // expression - self.update_reference_position(end_position + DIRECTIVE_EXPRESSION_OFFSET); - self.convert_expression(&expression_statement.expr); - // end - self.add_end(end_position, &expression_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/do_while_statement.rs b/rust/parse_ast/src/ast_nodes/do_while_statement.rs index a44fb2a5c..01cb24d33 100644 --- a/rust/parse_ast/src/ast_nodes/do_while_statement.rs +++ b/rust/parse_ast/src/ast_nodes/do_while_statement.rs @@ -1,26 +1,15 @@ use swc_ecma_ast::DoWhileStmt; -use crate::convert_ast::converter::ast_constants::{ - DO_WHILE_STATEMENT_BODY_OFFSET, DO_WHILE_STATEMENT_RESERVED_BYTES, - DO_WHILE_STATEMENT_TEST_OFFSET, TYPE_DO_WHILE_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_do_while_statement; impl<'a> AstConverter<'a> { pub fn store_do_while_statement(&mut self, do_while_statement: &DoWhileStmt) { - let end_position = self.add_type_and_start( - &TYPE_DO_WHILE_STATEMENT, - &do_while_statement.span, - DO_WHILE_STATEMENT_RESERVED_BYTES, - false, + store_do_while_statement!( + self, + span => do_while_statement.span, + body => [do_while_statement.body, convert_statement], + test => [do_while_statement.test, convert_expression] ); - // body - self.update_reference_position(end_position + DO_WHILE_STATEMENT_BODY_OFFSET); - self.convert_statement(&do_while_statement.body); - // test - self.update_reference_position(end_position + DO_WHILE_STATEMENT_TEST_OFFSET); - self.convert_expression(&do_while_statement.test); - // end - self.add_end(end_position, &do_while_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/empty_statement.rs b/rust/parse_ast/src/ast_nodes/empty_statement.rs index dfcd20424..22d9d147b 100644 --- a/rust/parse_ast/src/ast_nodes/empty_statement.rs +++ b/rust/parse_ast/src/ast_nodes/empty_statement.rs @@ -1,18 +1,10 @@ use swc_ecma_ast::EmptyStmt; -use crate::convert_ast::converter::ast_constants::{ - EMPTY_STATEMENT_RESERVED_BYTES, TYPE_EMPTY_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_empty_statement; impl<'a> AstConverter<'a> { pub fn store_empty_statement(&mut self, empty_statement: &EmptyStmt) { - let end_position = self.add_type_and_start( - &TYPE_EMPTY_STATEMENT, - &empty_statement.span, - EMPTY_STATEMENT_RESERVED_BYTES, - false, - ); - self.add_end(end_position, &empty_statement.span) + store_empty_statement!(self, span => empty_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/export_specifier.rs b/rust/parse_ast/src/ast_nodes/export_specifier.rs index 9068d68e6..adb8c09cf 100644 --- a/rust/parse_ast/src/ast_nodes/export_specifier.rs +++ b/rust/parse_ast/src/ast_nodes/export_specifier.rs @@ -1,28 +1,15 @@ use swc_ecma_ast::ExportNamedSpecifier; -use crate::convert_ast::converter::ast_constants::{ - EXPORT_SPECIFIER_EXPORTED_OFFSET, EXPORT_SPECIFIER_LOCAL_OFFSET, EXPORT_SPECIFIER_RESERVED_BYTES, - TYPE_EXPORT_SPECIFIER, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_export_specifier; impl<'a> AstConverter<'a> { - pub fn store_export_named_specifier(&mut self, export_named_specifier: &ExportNamedSpecifier) { - let end_position = self.add_type_and_start( - &TYPE_EXPORT_SPECIFIER, - &export_named_specifier.span, - EXPORT_SPECIFIER_RESERVED_BYTES, - false, + pub fn store_export_specifier(&mut self, export_named_specifier: &ExportNamedSpecifier) { + store_export_specifier!( + self, + span => &export_named_specifier.span, + local => [export_named_specifier.orig, convert_module_export_name], + exported => [export_named_specifier.exported, convert_module_export_name] ); - // local - self.update_reference_position(end_position + EXPORT_SPECIFIER_LOCAL_OFFSET); - self.convert_module_export_name(&export_named_specifier.orig); - // exported - if let Some(exported) = export_named_specifier.exported.as_ref() { - self.update_reference_position(end_position + EXPORT_SPECIFIER_EXPORTED_OFFSET); - self.convert_module_export_name(exported); - } - // end - self.add_end(end_position, &export_named_specifier.span); } } diff --git a/rust/parse_ast/src/ast_nodes/expression_statement.rs b/rust/parse_ast/src/ast_nodes/expression_statement.rs index 98f9853c7..7fed6d836 100644 --- a/rust/parse_ast/src/ast_nodes/expression_statement.rs +++ b/rust/parse_ast/src/ast_nodes/expression_statement.rs @@ -1,23 +1,14 @@ use swc_ecma_ast::ExprStmt; -use crate::convert_ast::converter::ast_constants::{ - EXPRESSION_STATEMENT_EXPRESSION_OFFSET, EXPRESSION_STATEMENT_RESERVED_BYTES, - TYPE_EXPRESSION_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_expression_statement; impl<'a> AstConverter<'a> { pub fn store_expression_statement(&mut self, expression_statement: &ExprStmt) { - let end_position = self.add_type_and_start( - &TYPE_EXPRESSION_STATEMENT, - &expression_statement.span, - EXPRESSION_STATEMENT_RESERVED_BYTES, - false, + store_expression_statement!( + self, + span => &expression_statement.span, + expression => [expression_statement.expr, convert_expression] ); - // expression - self.update_reference_position(end_position + EXPRESSION_STATEMENT_EXPRESSION_OFFSET); - self.convert_expression(&expression_statement.expr); - // end - self.add_end(end_position, &expression_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/for_in_statement.rs b/rust/parse_ast/src/ast_nodes/for_in_statement.rs index 2cb79ea98..72bf9e3dd 100644 --- a/rust/parse_ast/src/ast_nodes/for_in_statement.rs +++ b/rust/parse_ast/src/ast_nodes/for_in_statement.rs @@ -1,29 +1,16 @@ use swc_ecma_ast::ForInStmt; -use crate::convert_ast::converter::ast_constants::{ - FOR_IN_STATEMENT_BODY_OFFSET, FOR_IN_STATEMENT_LEFT_OFFSET, FOR_IN_STATEMENT_RESERVED_BYTES, - FOR_IN_STATEMENT_RIGHT_OFFSET, TYPE_FOR_IN_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_for_in_statement; impl<'a> AstConverter<'a> { pub fn store_for_in_statement(&mut self, for_in_statement: &ForInStmt) { - let end_position = self.add_type_and_start( - &TYPE_FOR_IN_STATEMENT, - &for_in_statement.span, - FOR_IN_STATEMENT_RESERVED_BYTES, - false, + store_for_in_statement!( + self, + span => &for_in_statement.span, + left => [for_in_statement.left, convert_for_head], + right => [for_in_statement.right, convert_expression], + body => [for_in_statement.body, convert_statement] ); - // left - self.update_reference_position(end_position + FOR_IN_STATEMENT_LEFT_OFFSET); - self.convert_for_head(&for_in_statement.left); - // right - self.update_reference_position(end_position + FOR_IN_STATEMENT_RIGHT_OFFSET); - self.convert_expression(&for_in_statement.right); - // body - self.update_reference_position(end_position + FOR_IN_STATEMENT_BODY_OFFSET); - self.convert_statement(&for_in_statement.body); - // end - self.add_end(end_position, &for_in_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/for_of_statement.rs b/rust/parse_ast/src/ast_nodes/for_of_statement.rs index b6c593ad4..a57e69cfe 100644 --- a/rust/parse_ast/src/ast_nodes/for_of_statement.rs +++ b/rust/parse_ast/src/ast_nodes/for_of_statement.rs @@ -1,37 +1,17 @@ use swc_ecma_ast::ForOfStmt; -use crate::convert_ast::converter::ast_constants::{ - FOR_OF_STATEMENT_AWAIT_FLAG, FOR_OF_STATEMENT_BODY_OFFSET, FOR_OF_STATEMENT_FLAGS_OFFSET, - FOR_OF_STATEMENT_LEFT_OFFSET, FOR_OF_STATEMENT_RESERVED_BYTES, FOR_OF_STATEMENT_RIGHT_OFFSET, - TYPE_FOR_OF_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::{store_for_of_statement, store_for_of_statement_flags}; impl<'a> AstConverter<'a> { pub fn store_for_of_statement(&mut self, for_of_statement: &ForOfStmt) { - let end_position = self.add_type_and_start( - &TYPE_FOR_OF_STATEMENT, - &for_of_statement.span, - FOR_OF_STATEMENT_RESERVED_BYTES, - false, + store_for_of_statement!( + self, + span => &for_of_statement.span, + await => for_of_statement.is_await, + left => [for_of_statement.left, convert_for_head], + right => [for_of_statement.right, convert_expression], + body => [for_of_statement.body, convert_statement] ); - // flags - let mut flags = 0u32; - if for_of_statement.is_await { - flags |= FOR_OF_STATEMENT_AWAIT_FLAG; - }; - let flags_position = end_position + FOR_OF_STATEMENT_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); - // left - self.update_reference_position(end_position + FOR_OF_STATEMENT_LEFT_OFFSET); - self.convert_for_head(&for_of_statement.left); - // right - self.update_reference_position(end_position + FOR_OF_STATEMENT_RIGHT_OFFSET); - self.convert_expression(&for_of_statement.right); - // body - self.update_reference_position(end_position + FOR_OF_STATEMENT_BODY_OFFSET); - self.convert_statement(&for_of_statement.body); - // end - self.add_end(end_position, &for_of_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/for_statement.rs b/rust/parse_ast/src/ast_nodes/for_statement.rs index 654308d80..1d16325e7 100644 --- a/rust/parse_ast/src/ast_nodes/for_statement.rs +++ b/rust/parse_ast/src/ast_nodes/for_statement.rs @@ -1,38 +1,17 @@ use swc_ecma_ast::ForStmt; -use crate::convert_ast::converter::ast_constants::{ - FOR_STATEMENT_BODY_OFFSET, FOR_STATEMENT_INIT_OFFSET, FOR_STATEMENT_RESERVED_BYTES, - FOR_STATEMENT_TEST_OFFSET, FOR_STATEMENT_UPDATE_OFFSET, TYPE_FOR_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_for_statement; impl<'a> AstConverter<'a> { pub fn store_for_statement(&mut self, for_statement: &ForStmt) { - let end_position = self.add_type_and_start( - &TYPE_FOR_STATEMENT, - &for_statement.span, - FOR_STATEMENT_RESERVED_BYTES, - false, + store_for_statement!( + self, + span => &for_statement.span, + init => [for_statement.init, convert_variable_declaration_or_expression], + test => [for_statement.test, convert_expression], + update => [for_statement.update, convert_expression], + body => [for_statement.body, convert_statement] ); - // init - if let Some(init) = for_statement.init.as_ref() { - self.update_reference_position(end_position + FOR_STATEMENT_INIT_OFFSET); - self.convert_variable_declaration_or_expression(init); - } - // test - if let Some(test) = for_statement.test.as_ref() { - self.update_reference_position(end_position + FOR_STATEMENT_TEST_OFFSET); - self.convert_expression(test); - } - // update - if let Some(update) = for_statement.update.as_ref() { - self.update_reference_position(end_position + FOR_STATEMENT_UPDATE_OFFSET); - self.convert_expression(update); - } - // body - self.update_reference_position(end_position + FOR_STATEMENT_BODY_OFFSET); - self.convert_statement(&for_statement.body); - // end - self.add_end(end_position, &for_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/if_statement.rs b/rust/parse_ast/src/ast_nodes/if_statement.rs index e68c2debd..e093372cb 100644 --- a/rust/parse_ast/src/ast_nodes/if_statement.rs +++ b/rust/parse_ast/src/ast_nodes/if_statement.rs @@ -1,31 +1,16 @@ use swc_ecma_ast::IfStmt; -use crate::convert_ast::converter::ast_constants::{ - IF_STATEMENT_ALTERNATE_OFFSET, IF_STATEMENT_CONSEQUENT_OFFSET, IF_STATEMENT_RESERVED_BYTES, - IF_STATEMENT_TEST_OFFSET, TYPE_IF_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_if_statement; impl<'a> AstConverter<'a> { pub fn store_if_statement(&mut self, if_statement: &IfStmt) { - let end_position = self.add_type_and_start( - &TYPE_IF_STATEMENT, - &if_statement.span, - IF_STATEMENT_RESERVED_BYTES, - false, + store_if_statement!( + self, + span => &if_statement.span, + test => [if_statement.test, convert_expression], + consequent => [if_statement.cons, convert_statement], + alternate => [if_statement.alt, convert_statement] ); - // test - self.update_reference_position(end_position + IF_STATEMENT_TEST_OFFSET); - self.convert_expression(&if_statement.test); - // consequent - self.update_reference_position(end_position + IF_STATEMENT_CONSEQUENT_OFFSET); - self.convert_statement(&if_statement.cons); - // alternate - if let Some(alt) = if_statement.alt.as_ref() { - self.update_reference_position(end_position + IF_STATEMENT_ALTERNATE_OFFSET); - self.convert_statement(alt); - } - // end - self.add_end(end_position, &if_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/import_default_specifier.rs b/rust/parse_ast/src/ast_nodes/import_default_specifier.rs index 86bac1411..689bd5f4e 100644 --- a/rust/parse_ast/src/ast_nodes/import_default_specifier.rs +++ b/rust/parse_ast/src/ast_nodes/import_default_specifier.rs @@ -1,26 +1,17 @@ use swc_ecma_ast::ImportDefaultSpecifier; -use crate::convert_ast::converter::ast_constants::{ - IMPORT_DEFAULT_SPECIFIER_LOCAL_OFFSET, IMPORT_DEFAULT_SPECIFIER_RESERVED_BYTES, - TYPE_IMPORT_DEFAULT_SPECIFIER, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_import_default_specifier; impl<'a> AstConverter<'a> { pub fn store_import_default_specifier( &mut self, import_default_specifier: &ImportDefaultSpecifier, ) { - let end_position = self.add_type_and_start( - &TYPE_IMPORT_DEFAULT_SPECIFIER, - &import_default_specifier.span, - IMPORT_DEFAULT_SPECIFIER_RESERVED_BYTES, - false, + store_import_default_specifier!( + self, + span => &import_default_specifier.span, + local => [import_default_specifier.local, convert_identifier] ); - // local - self.update_reference_position(end_position + IMPORT_DEFAULT_SPECIFIER_LOCAL_OFFSET); - self.convert_identifier(&import_default_specifier.local); - // end - self.add_end(end_position, &import_default_specifier.span); } } diff --git a/rust/parse_ast/src/ast_nodes/import_namespace_specifier.rs b/rust/parse_ast/src/ast_nodes/import_namespace_specifier.rs index 9060f92d8..f7ffe93f3 100644 --- a/rust/parse_ast/src/ast_nodes/import_namespace_specifier.rs +++ b/rust/parse_ast/src/ast_nodes/import_namespace_specifier.rs @@ -1,26 +1,17 @@ use swc_ecma_ast::ImportStarAsSpecifier; -use crate::convert_ast::converter::ast_constants::{ - IMPORT_NAMESPACE_SPECIFIER_LOCAL_OFFSET, IMPORT_NAMESPACE_SPECIFIER_RESERVED_BYTES, - TYPE_IMPORT_NAMESPACE_SPECIFIER, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_import_namespace_specifier; impl<'a> AstConverter<'a> { pub fn store_import_namespace_specifier( &mut self, import_namespace_specifier: &ImportStarAsSpecifier, ) { - let end_position = self.add_type_and_start( - &TYPE_IMPORT_NAMESPACE_SPECIFIER, - &import_namespace_specifier.span, - IMPORT_NAMESPACE_SPECIFIER_RESERVED_BYTES, - false, + store_import_namespace_specifier!( + self, + span => &import_namespace_specifier.span, + local => [import_namespace_specifier.local, convert_identifier] ); - // local - self.update_reference_position(end_position + IMPORT_NAMESPACE_SPECIFIER_LOCAL_OFFSET); - self.convert_identifier(&import_namespace_specifier.local); - // end - self.add_end(end_position, &import_namespace_specifier.span); } } diff --git a/rust/parse_ast/src/ast_nodes/import_specifier.rs b/rust/parse_ast/src/ast_nodes/import_specifier.rs index 3370f2048..1d02bf067 100644 --- a/rust/parse_ast/src/ast_nodes/import_specifier.rs +++ b/rust/parse_ast/src/ast_nodes/import_specifier.rs @@ -1,28 +1,15 @@ use swc_ecma_ast::ImportNamedSpecifier; -use crate::convert_ast::converter::ast_constants::{ - IMPORT_SPECIFIER_IMPORTED_OFFSET, IMPORT_SPECIFIER_LOCAL_OFFSET, IMPORT_SPECIFIER_RESERVED_BYTES, - TYPE_IMPORT_SPECIFIER, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_import_specifier; impl<'a> AstConverter<'a> { pub fn store_import_specifier(&mut self, import_named_specifier: &ImportNamedSpecifier) { - let end_position = self.add_type_and_start( - &TYPE_IMPORT_SPECIFIER, - &import_named_specifier.span, - IMPORT_SPECIFIER_RESERVED_BYTES, - false, + store_import_specifier!( + self, + span => &import_named_specifier.span, + imported => [import_named_specifier.imported, convert_module_export_name], + local => [import_named_specifier.local, convert_identifier] ); - // imported - if let Some(imported) = import_named_specifier.imported.as_ref() { - self.update_reference_position(end_position + IMPORT_SPECIFIER_IMPORTED_OFFSET); - self.convert_module_export_name(imported); - } - // local - self.update_reference_position(end_position + IMPORT_SPECIFIER_LOCAL_OFFSET); - self.convert_identifier(&import_named_specifier.local); - // end - self.add_end(end_position, &import_named_specifier.span); } } diff --git a/rust/parse_ast/src/ast_nodes/labeled_statement.rs b/rust/parse_ast/src/ast_nodes/labeled_statement.rs index 4ba845952..0f2bd3cd0 100644 --- a/rust/parse_ast/src/ast_nodes/labeled_statement.rs +++ b/rust/parse_ast/src/ast_nodes/labeled_statement.rs @@ -1,26 +1,15 @@ use swc_ecma_ast::LabeledStmt; -use crate::convert_ast::converter::ast_constants::{ - LABELED_STATEMENT_BODY_OFFSET, LABELED_STATEMENT_LABEL_OFFSET, LABELED_STATEMENT_RESERVED_BYTES, - TYPE_LABELED_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_labeled_statement; impl<'a> AstConverter<'a> { pub fn store_labeled_statement(&mut self, labeled_statement: &LabeledStmt) { - let end_position = self.add_type_and_start( - &TYPE_LABELED_STATEMENT, - &labeled_statement.span, - LABELED_STATEMENT_RESERVED_BYTES, - false, + store_labeled_statement!( + self, + span => &labeled_statement.span, + label => [labeled_statement.label, convert_identifier], + body => [labeled_statement.body, convert_statement] ); - // label - self.update_reference_position(end_position + LABELED_STATEMENT_LABEL_OFFSET); - self.convert_identifier(&labeled_statement.label); - // body - self.update_reference_position(end_position + LABELED_STATEMENT_BODY_OFFSET); - self.convert_statement(&labeled_statement.body); - // end - self.add_end(end_position, &labeled_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/literal_big_int.rs b/rust/parse_ast/src/ast_nodes/literal_big_int.rs index 56f040cbf..5a98704fd 100644 --- a/rust/parse_ast/src/ast_nodes/literal_big_int.rs +++ b/rust/parse_ast/src/ast_nodes/literal_big_int.rs @@ -1,30 +1,15 @@ use swc_ecma_ast::BigInt; -use crate::convert_ast::converter::ast_constants::{ - LITERAL_BIG_INT_BIGINT_OFFSET, LITERAL_BIG_INT_RAW_OFFSET, LITERAL_BIG_INT_RESERVED_BYTES, - TYPE_LITERAL_BIG_INT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_literal_big_int; impl<'a> AstConverter<'a> { pub fn store_literal_bigint(&mut self, bigint: &BigInt) { - let end_position = self.add_type_and_start( - &TYPE_LITERAL_BIG_INT, - &bigint.span, - LITERAL_BIG_INT_RESERVED_BYTES, - false, + store_literal_big_int!( + self, + span => &bigint.span, + bigint => &bigint.value.to_str_radix(10), + raw => bigint.raw.as_ref().unwrap() ); - // bigint - self.convert_string( - &bigint.value.to_str_radix(10), - end_position + LITERAL_BIG_INT_BIGINT_OFFSET, - ); - // raw - self.convert_string( - bigint.raw.as_ref().unwrap(), - end_position + LITERAL_BIG_INT_RAW_OFFSET, - ); - // end - self.add_end(end_position, &bigint.span); } } diff --git a/rust/parse_ast/src/ast_nodes/literal_boolean.rs b/rust/parse_ast/src/ast_nodes/literal_boolean.rs index 1a31da921..9c84f9dfb 100644 --- a/rust/parse_ast/src/ast_nodes/literal_boolean.rs +++ b/rust/parse_ast/src/ast_nodes/literal_boolean.rs @@ -1,25 +1,14 @@ use swc_ecma_ast::Bool; -use crate::convert_ast::converter::ast_constants::{ - LITERAL_BOOLEAN_FLAGS_OFFSET, LITERAL_BOOLEAN_RESERVED_BYTES, LITERAL_BOOLEAN_VALUE_FLAG, - TYPE_LITERAL_BOOLEAN, -}; use crate::convert_ast::converter::AstConverter; +use crate::{store_literal_boolean, store_literal_boolean_flags}; impl<'a> AstConverter<'a> { pub fn store_literal_boolean(&mut self, literal: &Bool) { - let end_position = self.add_type_and_start( - &TYPE_LITERAL_BOOLEAN, - &literal.span, - LITERAL_BOOLEAN_RESERVED_BYTES, - false, + store_literal_boolean!( + self, + span => &literal.span, + value => literal.value ); - let mut flags = 0u32; - if literal.value { - flags |= LITERAL_BOOLEAN_VALUE_FLAG - }; - let flags_position = end_position + LITERAL_BOOLEAN_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); - self.add_end(end_position, &literal.span); } } diff --git a/rust/parse_ast/src/ast_nodes/literal_null.rs b/rust/parse_ast/src/ast_nodes/literal_null.rs index 546cedff2..9c46f838c 100644 --- a/rust/parse_ast/src/ast_nodes/literal_null.rs +++ b/rust/parse_ast/src/ast_nodes/literal_null.rs @@ -1,18 +1,13 @@ use swc_ecma_ast::Null; -use crate::convert_ast::converter::ast_constants::{ - LITERAL_NULL_RESERVED_BYTES, TYPE_LITERAL_NULL, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_literal_null; impl<'a> AstConverter<'a> { pub fn store_literal_null(&mut self, literal: &Null) { - let end_position = self.add_type_and_start( - &TYPE_LITERAL_NULL, - &literal.span, - LITERAL_NULL_RESERVED_BYTES, - false, + store_literal_null!( + self, + span => &literal.span ); - self.add_end(end_position, &literal.span); } } diff --git a/rust/parse_ast/src/ast_nodes/literal_number.rs b/rust/parse_ast/src/ast_nodes/literal_number.rs index b9a0a7300..64cc5fa32 100644 --- a/rust/parse_ast/src/ast_nodes/literal_number.rs +++ b/rust/parse_ast/src/ast_nodes/literal_number.rs @@ -1,27 +1,15 @@ use swc_ecma_ast::Number; -use crate::convert_ast::converter::ast_constants::{ - LITERAL_NUMBER_RAW_OFFSET, LITERAL_NUMBER_RESERVED_BYTES, LITERAL_NUMBER_VALUE_OFFSET, - TYPE_LITERAL_NUMBER, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_literal_number; impl<'a> AstConverter<'a> { pub fn store_literal_number(&mut self, literal: &Number) { - let end_position = self.add_type_and_start( - &TYPE_LITERAL_NUMBER, - &literal.span, - LITERAL_NUMBER_RESERVED_BYTES, - false, + store_literal_number!( + self, + span => &literal.span, + raw => literal.raw, + value => literal.value ); - // value, needs to be little endian as we are reading via a DataView - let value_position = end_position + LITERAL_NUMBER_VALUE_OFFSET; - self.buffer[value_position..value_position + 8].copy_from_slice(&literal.value.to_le_bytes()); - // raw - if let Some(raw) = literal.raw.as_ref() { - self.convert_string(raw, end_position + LITERAL_NUMBER_RAW_OFFSET); - } - // end - self.add_end(end_position, &literal.span); } } diff --git a/rust/parse_ast/src/ast_nodes/literal_reg_exp.rs b/rust/parse_ast/src/ast_nodes/literal_reg_exp.rs index 67c288fb7..3e730e4f4 100644 --- a/rust/parse_ast/src/ast_nodes/literal_reg_exp.rs +++ b/rust/parse_ast/src/ast_nodes/literal_reg_exp.rs @@ -1,24 +1,15 @@ use swc_ecma_ast::Regex; -use crate::convert_ast::converter::ast_constants::{ - LITERAL_REG_EXP_FLAGS_OFFSET, LITERAL_REG_EXP_PATTERN_OFFSET, LITERAL_REG_EXP_RESERVED_BYTES, - TYPE_LITERAL_REG_EXP, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_literal_reg_exp; impl<'a> AstConverter<'a> { pub fn store_literal_regex(&mut self, regex: &Regex) { - let end_position = self.add_type_and_start( - &TYPE_LITERAL_REG_EXP, - ®ex.span, - LITERAL_REG_EXP_RESERVED_BYTES, - false, + store_literal_reg_exp!( + self, + span => ®ex.span, + flags => ®ex.flags, + pattern => ®ex.exp ); - // flags - self.convert_string(®ex.flags, end_position + LITERAL_REG_EXP_FLAGS_OFFSET); - // pattern - self.convert_string(®ex.exp, end_position + LITERAL_REG_EXP_PATTERN_OFFSET); - // end - self.add_end(end_position, ®ex.span); } } diff --git a/rust/parse_ast/src/ast_nodes/literal_string.rs b/rust/parse_ast/src/ast_nodes/literal_string.rs index 4f5b70ee9..0b475512e 100644 --- a/rust/parse_ast/src/ast_nodes/literal_string.rs +++ b/rust/parse_ast/src/ast_nodes/literal_string.rs @@ -1,26 +1,15 @@ use swc_ecma_ast::Str; -use crate::convert_ast::converter::ast_constants::{ - LITERAL_STRING_RAW_OFFSET, LITERAL_STRING_RESERVED_BYTES, LITERAL_STRING_VALUE_OFFSET, - TYPE_LITERAL_STRING, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_literal_string; impl<'a> AstConverter<'a> { pub fn store_literal_string(&mut self, literal: &Str) { - let end_position = self.add_type_and_start( - &TYPE_LITERAL_STRING, - &literal.span, - LITERAL_STRING_RESERVED_BYTES, - false, + store_literal_string!( + self, + span => &literal.span, + value => &literal.value, + raw => &literal.raw ); - // value - self.convert_string(&literal.value, end_position + LITERAL_STRING_VALUE_OFFSET); - // raw - if let Some(raw) = literal.raw.as_ref() { - self.convert_string(raw, end_position + LITERAL_STRING_RAW_OFFSET); - } - // end - self.add_end(end_position, &literal.span); } } diff --git a/rust/parse_ast/src/ast_nodes/member_expression.rs b/rust/parse_ast/src/ast_nodes/member_expression.rs index ce64abd66..7c660bf46 100644 --- a/rust/parse_ast/src/ast_nodes/member_expression.rs +++ b/rust/parse_ast/src/ast_nodes/member_expression.rs @@ -5,11 +5,11 @@ use swc_ecma_ast::{ }; use crate::convert_ast::converter::ast_constants::{ - MEMBER_EXPRESSION_COMPUTED_FLAG, MEMBER_EXPRESSION_FLAGS_OFFSET, MEMBER_EXPRESSION_OBJECT_OFFSET, - MEMBER_EXPRESSION_OPTIONAL_FLAG, MEMBER_EXPRESSION_PROPERTY_OFFSET, + MEMBER_EXPRESSION_OBJECT_OFFSET, MEMBER_EXPRESSION_PROPERTY_OFFSET, MEMBER_EXPRESSION_RESERVED_BYTES, TYPE_MEMBER_EXPRESSION, }; use crate::convert_ast::converter::AstConverter; +use crate::store_member_expression_flags; impl<'a> AstConverter<'a> { pub fn store_member_expression( @@ -44,15 +44,12 @@ impl<'a> AstConverter<'a> { ExpressionOrSuper::Super(super_token) => self.store_super_element(super_token), } // flags - let mut flags = 0u32; - if is_optional { - flags |= MEMBER_EXPRESSION_OPTIONAL_FLAG; - } - if matches!(property, MemberOrSuperProp::Computed(_)) { - flags |= MEMBER_EXPRESSION_COMPUTED_FLAG; - } - let flags_position = end_position + MEMBER_EXPRESSION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + store_member_expression_flags!( + self, + end_position, + computed => matches!(property, MemberOrSuperProp::Computed(_)), + optional => is_optional + ); // property self.update_reference_position(end_position + MEMBER_EXPRESSION_PROPERTY_OFFSET); match property { diff --git a/rust/parse_ast/src/ast_nodes/method_definition.rs b/rust/parse_ast/src/ast_nodes/method_definition.rs index 86c30a88c..0872b9146 100644 --- a/rust/parse_ast/src/ast_nodes/method_definition.rs +++ b/rust/parse_ast/src/ast_nodes/method_definition.rs @@ -6,14 +6,14 @@ use swc_ecma_ast::{ use crate::convert_ast::converter::analyze_code::find_first_occurrence_outside_comment; use crate::convert_ast::converter::ast_constants::{ - METHOD_DEFINITION_COMPUTED_FLAG, METHOD_DEFINITION_FLAGS_OFFSET, METHOD_DEFINITION_KEY_OFFSET, - METHOD_DEFINITION_KIND_OFFSET, METHOD_DEFINITION_RESERVED_BYTES, METHOD_DEFINITION_STATIC_FLAG, + METHOD_DEFINITION_KEY_OFFSET, METHOD_DEFINITION_KIND_OFFSET, METHOD_DEFINITION_RESERVED_BYTES, METHOD_DEFINITION_VALUE_OFFSET, TYPE_FUNCTION_EXPRESSION, TYPE_METHOD_DEFINITION, }; use crate::convert_ast::converter::string_constants::{ STRING_CONSTRUCTOR, STRING_GET, STRING_METHOD, STRING_SET, }; use crate::convert_ast::converter::AstConverter; +use crate::store_method_definition_flags; impl<'a> AstConverter<'a> { pub fn store_method_definition( @@ -32,15 +32,7 @@ impl<'a> AstConverter<'a> { false, ); // flags - let mut flags = 0u32; - if is_static { - flags |= METHOD_DEFINITION_STATIC_FLAG - }; - if is_computed { - flags |= METHOD_DEFINITION_COMPUTED_FLAG; - } - let flags_position = end_position + METHOD_DEFINITION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + store_method_definition_flags!(self, end_position, static => is_static, computed => is_computed); // kind let kind_position = end_position + METHOD_DEFINITION_KIND_OFFSET; self.buffer[kind_position..kind_position + 4].copy_from_slice(match kind { @@ -89,9 +81,8 @@ impl<'a> AstConverter<'a> { // key self.update_reference_position(end_position + METHOD_DEFINITION_KEY_OFFSET); self.convert_property_name(&constructor.key); - // flags, method definitions are neither static nor computed - let flags_position = end_position + METHOD_DEFINITION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&0u32.to_ne_bytes()); + // flags + store_method_definition_flags!(self, end_position, static => false, computed => false); // kind let kind_position = end_position + METHOD_DEFINITION_KIND_OFFSET; self.buffer[kind_position..kind_position + 4].copy_from_slice(&STRING_CONSTRUCTOR); diff --git a/rust/parse_ast/src/ast_nodes/object_expression.rs b/rust/parse_ast/src/ast_nodes/object_expression.rs index e0fcec6e1..72e07f702 100644 --- a/rust/parse_ast/src/ast_nodes/object_expression.rs +++ b/rust/parse_ast/src/ast_nodes/object_expression.rs @@ -1,28 +1,14 @@ use swc_ecma_ast::ObjectLit; -use crate::convert_ast::converter::ast_constants::{ - OBJECT_EXPRESSION_PROPERTIES_OFFSET, OBJECT_EXPRESSION_RESERVED_BYTES, TYPE_OBJECT_EXPRESSION, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_object_expression; impl<'a> AstConverter<'a> { pub fn store_object_expression(&mut self, object_literal: &ObjectLit) { - let end_position = self.add_type_and_start( - &TYPE_OBJECT_EXPRESSION, - &object_literal.span, - OBJECT_EXPRESSION_RESERVED_BYTES, - false, + store_object_expression!( + self, + span => &object_literal.span, + properties => [object_literal.props, convert_property_or_spread] ); - // properties - self.convert_item_list( - &object_literal.props, - end_position + OBJECT_EXPRESSION_PROPERTIES_OFFSET, - |ast_converter, property_or_spread| { - ast_converter.convert_property_or_spread(property_or_spread); - true - }, - ); - // end - self.add_end(end_position, &object_literal.span); } } diff --git a/rust/parse_ast/src/ast_nodes/object_pattern.rs b/rust/parse_ast/src/ast_nodes/object_pattern.rs index 71c3803eb..22a958bd9 100644 --- a/rust/parse_ast/src/ast_nodes/object_pattern.rs +++ b/rust/parse_ast/src/ast_nodes/object_pattern.rs @@ -1,28 +1,14 @@ use swc_ecma_ast::ObjectPat; -use crate::convert_ast::converter::ast_constants::{ - OBJECT_PATTERN_PROPERTIES_OFFSET, OBJECT_PATTERN_RESERVED_BYTES, TYPE_OBJECT_PATTERN, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_object_pattern; impl<'a> AstConverter<'a> { pub fn store_object_pattern(&mut self, object_pattern: &ObjectPat) { - let end_position = self.add_type_and_start( - &TYPE_OBJECT_PATTERN, - &object_pattern.span, - OBJECT_PATTERN_RESERVED_BYTES, - false, + store_object_pattern!( + self, + span => &object_pattern.span, + properties => [object_pattern.props, convert_object_pattern_property] ); - // properties - self.convert_item_list( - &object_pattern.props, - end_position + OBJECT_PATTERN_PROPERTIES_OFFSET, - |ast_converter, object_pattern_property| { - ast_converter.convert_object_pattern_property(object_pattern_property); - true - }, - ); - // end - self.add_end(end_position, &object_pattern.span); } } diff --git a/rust/parse_ast/src/ast_nodes/private_identifier.rs b/rust/parse_ast/src/ast_nodes/private_identifier.rs index 9a288de0e..0af703eb6 100644 --- a/rust/parse_ast/src/ast_nodes/private_identifier.rs +++ b/rust/parse_ast/src/ast_nodes/private_identifier.rs @@ -1,24 +1,14 @@ use swc_ecma_ast::PrivateName; -use crate::convert_ast::converter::ast_constants::{ - PRIVATE_IDENTIFIER_NAME_OFFSET, PRIVATE_IDENTIFIER_RESERVED_BYTES, TYPE_PRIVATE_IDENTIFIER, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_private_identifier; impl<'a> AstConverter<'a> { pub fn store_private_identifier(&mut self, private_name: &PrivateName) { - let end_position = self.add_type_and_start( - &TYPE_PRIVATE_IDENTIFIER, - &private_name.span, - PRIVATE_IDENTIFIER_RESERVED_BYTES, - false, + store_private_identifier!( + self, + span => &private_name.span, + name => &private_name.id.sym ); - // name - self.convert_string( - &private_name.id.sym, - end_position + PRIVATE_IDENTIFIER_NAME_OFFSET, - ); - // end - self.add_end(end_position, &private_name.span); } } diff --git a/rust/parse_ast/src/ast_nodes/property.rs b/rust/parse_ast/src/ast_nodes/property.rs index 8fd244ed2..f2c9b866d 100644 --- a/rust/parse_ast/src/ast_nodes/property.rs +++ b/rust/parse_ast/src/ast_nodes/property.rs @@ -7,12 +7,12 @@ use swc_ecma_ast::{ use crate::ast_nodes::assignment_pattern::PatternOrIdentifier; use crate::convert_ast::converter::analyze_code::find_first_occurrence_outside_comment; use crate::convert_ast::converter::ast_constants::{ - PROPERTY_COMPUTED_FLAG, PROPERTY_FLAGS_OFFSET, PROPERTY_KEY_OFFSET, PROPERTY_KIND_OFFSET, - PROPERTY_METHOD_FLAG, PROPERTY_RESERVED_BYTES, PROPERTY_SHORTHAND_FLAG, PROPERTY_VALUE_OFFSET, + PROPERTY_KEY_OFFSET, PROPERTY_KIND_OFFSET, PROPERTY_RESERVED_BYTES, PROPERTY_VALUE_OFFSET, TYPE_FUNCTION_EXPRESSION, TYPE_PROPERTY, }; use crate::convert_ast::converter::string_constants::{STRING_GET, STRING_INIT, STRING_SET}; use crate::convert_ast::converter::AstConverter; +use crate::store_property_flags; impl<'a> AstConverter<'a> { pub fn convert_property(&mut self, property: &Prop) { @@ -57,13 +57,14 @@ impl<'a> AstConverter<'a> { // key self.update_reference_position(end_position + PROPERTY_KEY_OFFSET); self.convert_property_name(property_name); - // flags, method and shorthand are always false - let mut flags = 0u32; - if matches!(property_name, PropName::Computed(_)) { - flags |= PROPERTY_COMPUTED_FLAG - }; - let flags_position = end_position + PROPERTY_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + // flags + store_property_flags!( + self, + end_position, + method => false, + shorthand => false, + computed => matches!(property_name, PropName::Computed(_)) + ); // kind let kind_position = end_position + PROPERTY_KIND_OFFSET; self.buffer[kind_position..kind_position + 4].copy_from_slice(&STRING_INIT); @@ -91,13 +92,14 @@ impl<'a> AstConverter<'a> { self.update_reference_position(end_position + PROPERTY_KEY_OFFSET); self.convert_property_name(key); let key_end = key.span().hi.0 - 1; - // flags, method and shorthand are always false - let mut flags = 0u32; - if matches!(key, PropName::Computed(_)) { - flags |= PROPERTY_COMPUTED_FLAG; - }; - let flags_position = end_position + PROPERTY_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + // flags + store_property_flags!( + self, + end_position, + method => false, + shorthand => false, + computed => matches!(key, PropName::Computed(_)) + ); // kind let kind_position = end_position + PROPERTY_KIND_OFFSET; self.buffer[kind_position..kind_position + 4].copy_from_slice(kind); @@ -135,13 +137,14 @@ impl<'a> AstConverter<'a> { self.convert_property_name(&method_property.key); let key_end = &method_property.key.span().hi.0 - 1; let function_start = find_first_occurrence_outside_comment(self.code, b'(', key_end); - // flags, shorthand is always false - let mut flags = PROPERTY_METHOD_FLAG; - if matches!(&method_property.key, PropName::Computed(_)) { - flags |= PROPERTY_COMPUTED_FLAG - }; - let flags_position = end_position + PROPERTY_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + // flags + store_property_flags!( + self, + end_position, + method => true, + shorthand => false, + computed => matches!(&method_property.key, PropName::Computed(_)) + ); // kind let kind_position = end_position + PROPERTY_KIND_OFFSET; self.buffer[kind_position..kind_position + 4].copy_from_slice(&STRING_INIT); @@ -192,9 +195,13 @@ impl<'a> AstConverter<'a> { } } // flags - let flags_position = end_position + PROPERTY_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4] - .copy_from_slice(&PROPERTY_SHORTHAND_FLAG.to_ne_bytes()); + store_property_flags!( + self, + end_position, + method => false, + shorthand => true, + computed => false + ); // kind let kind_position = end_position + PROPERTY_KIND_OFFSET; self.buffer[kind_position..kind_position + 4].copy_from_slice(&STRING_INIT); diff --git a/rust/parse_ast/src/ast_nodes/property_definition.rs b/rust/parse_ast/src/ast_nodes/property_definition.rs index ee94196a0..e98060cdf 100644 --- a/rust/parse_ast/src/ast_nodes/property_definition.rs +++ b/rust/parse_ast/src/ast_nodes/property_definition.rs @@ -3,11 +3,11 @@ use swc_ecma_ast::{ClassProp, Expr, PrivateProp, PropName}; use crate::ast_nodes::method_definition::PropOrPrivateName; use crate::convert_ast::converter::ast_constants::{ - PROPERTY_DEFINITION_COMPUTED_FLAG, PROPERTY_DEFINITION_FLAGS_OFFSET, PROPERTY_DEFINITION_KEY_OFFSET, PROPERTY_DEFINITION_RESERVED_BYTES, - PROPERTY_DEFINITION_STATIC_FLAG, PROPERTY_DEFINITION_VALUE_OFFSET, TYPE_PROPERTY_DEFINITION, + PROPERTY_DEFINITION_VALUE_OFFSET, TYPE_PROPERTY_DEFINITION, }; use crate::convert_ast::converter::AstConverter; +use crate::store_property_definition_flags; impl<'a> AstConverter<'a> { pub fn store_property_definition( @@ -33,20 +33,12 @@ impl<'a> AstConverter<'a> { PropOrPrivateName::PrivateName(private_name) => self.store_private_identifier(private_name), } // flags - let mut flags = 0u32; - if is_static { - flags |= PROPERTY_DEFINITION_STATIC_FLAG; - } - if is_computed { - flags |= PROPERTY_DEFINITION_COMPUTED_FLAG; - } - let flags_position = end_position + PROPERTY_DEFINITION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + store_property_definition_flags!(self, end_position, static => is_static, computed => is_computed); // value - value.map(|expression| { + if let Some(expression) = value { self.update_reference_position(end_position + PROPERTY_DEFINITION_VALUE_OFFSET); self.convert_expression(expression); - }); + } // end self.add_end(end_position, span); } diff --git a/rust/parse_ast/src/ast_nodes/return_statement.rs b/rust/parse_ast/src/ast_nodes/return_statement.rs index 59a1f7778..5a0870bd5 100644 --- a/rust/parse_ast/src/ast_nodes/return_statement.rs +++ b/rust/parse_ast/src/ast_nodes/return_statement.rs @@ -1,24 +1,14 @@ use swc_ecma_ast::ReturnStmt; -use crate::convert_ast::converter::ast_constants::{ - RETURN_STATEMENT_ARGUMENT_OFFSET, RETURN_STATEMENT_RESERVED_BYTES, TYPE_RETURN_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_return_statement; impl<'a> AstConverter<'a> { pub fn store_return_statement(&mut self, return_statement: &ReturnStmt) { - let end_position = self.add_type_and_start( - &TYPE_RETURN_STATEMENT, - &return_statement.span, - RETURN_STATEMENT_RESERVED_BYTES, - false, + store_return_statement!( + self, + span => return_statement.span, + argument => [return_statement.arg, convert_expression] ); - // argument - if let Some(argument) = return_statement.arg.as_ref() { - self.update_reference_position(end_position + RETURN_STATEMENT_ARGUMENT_OFFSET); - self.convert_expression(argument) - } - // end - self.add_end(end_position, &return_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/sequence_expression.rs b/rust/parse_ast/src/ast_nodes/sequence_expression.rs index 6109fb83c..fa69c151e 100644 --- a/rust/parse_ast/src/ast_nodes/sequence_expression.rs +++ b/rust/parse_ast/src/ast_nodes/sequence_expression.rs @@ -1,29 +1,14 @@ use swc_ecma_ast::SeqExpr; -use crate::convert_ast::converter::ast_constants::{ - SEQUENCE_EXPRESSION_EXPRESSIONS_OFFSET, SEQUENCE_EXPRESSION_RESERVED_BYTES, - TYPE_SEQUENCE_EXPRESSION, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_sequence_expression; impl<'a> AstConverter<'a> { pub fn store_sequence_expression(&mut self, sequence_expression: &SeqExpr) { - let end_position = self.add_type_and_start( - &TYPE_SEQUENCE_EXPRESSION, - &sequence_expression.span, - SEQUENCE_EXPRESSION_RESERVED_BYTES, - false, + store_sequence_expression!( + self, + span => &sequence_expression.span, + expressions => [sequence_expression.exprs, convert_expression] ); - // expressions - self.convert_item_list( - &sequence_expression.exprs, - end_position + SEQUENCE_EXPRESSION_EXPRESSIONS_OFFSET, - |ast_converter, expression| { - ast_converter.convert_expression(expression); - true - }, - ); - // end - self.add_end(end_position, &sequence_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/shared/function_node.rs b/rust/parse_ast/src/ast_nodes/shared/function_node.rs index 2e98f0b7b..34d868e5f 100644 --- a/rust/parse_ast/src/ast_nodes/shared/function_node.rs +++ b/rust/parse_ast/src/ast_nodes/shared/function_node.rs @@ -2,12 +2,12 @@ use swc_ecma_ast::{BlockStmt, Function, Ident, Pat}; use crate::convert_ast::annotations::AnnotationKind; use crate::convert_ast::converter::ast_constants::{ - FUNCTION_DECLARATION_ANNOTATIONS_OFFSET, FUNCTION_DECLARATION_ASYNC_FLAG, - FUNCTION_DECLARATION_BODY_OFFSET, FUNCTION_DECLARATION_FLAGS_OFFSET, - FUNCTION_DECLARATION_GENERATOR_FLAG, FUNCTION_DECLARATION_ID_OFFSET, - FUNCTION_DECLARATION_PARAMS_OFFSET, FUNCTION_DECLARATION_RESERVED_BYTES, + FUNCTION_DECLARATION_ANNOTATIONS_OFFSET, FUNCTION_DECLARATION_BODY_OFFSET, + FUNCTION_DECLARATION_ID_OFFSET, FUNCTION_DECLARATION_PARAMS_OFFSET, + FUNCTION_DECLARATION_RESERVED_BYTES, }; use crate::convert_ast::converter::{convert_annotation, AstConverter}; +use crate::store_function_declaration_flags; impl<'a> AstConverter<'a> { pub(crate) fn convert_function( @@ -46,15 +46,12 @@ impl<'a> AstConverter<'a> { let end_position = self.add_type_and_explicit_start(node_type, start, FUNCTION_DECLARATION_RESERVED_BYTES); // flags - let mut flags = 0u32; - if is_async { - flags |= FUNCTION_DECLARATION_ASYNC_FLAG - }; - if is_generator { - flags |= FUNCTION_DECLARATION_GENERATOR_FLAG; - } - let flags_position = end_position + FUNCTION_DECLARATION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + store_function_declaration_flags!( + self, + end_position, + async => is_async, + generator => is_generator + ); // annotations if observe_annotations { let annotations = self diff --git a/rust/parse_ast/src/ast_nodes/static_block.rs b/rust/parse_ast/src/ast_nodes/static_block.rs index d2cc58682..43d858cc3 100644 --- a/rust/parse_ast/src/ast_nodes/static_block.rs +++ b/rust/parse_ast/src/ast_nodes/static_block.rs @@ -1,28 +1,14 @@ use swc_ecma_ast::StaticBlock; -use crate::convert_ast::converter::ast_constants::{ - STATIC_BLOCK_BODY_OFFSET, STATIC_BLOCK_RESERVED_BYTES, TYPE_STATIC_BLOCK, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_static_block; impl<'a> AstConverter<'a> { pub fn store_static_block(&mut self, static_block: &StaticBlock) { - let end_position = self.add_type_and_start( - &TYPE_STATIC_BLOCK, - &static_block.span, - STATIC_BLOCK_RESERVED_BYTES, - false, + store_static_block!( + self, + span => &static_block.span, + body => [static_block.body.stmts, convert_statement] ); - // body - self.convert_item_list( - &static_block.body.stmts, - end_position + STATIC_BLOCK_BODY_OFFSET, - |ast_converter, statement| { - ast_converter.convert_statement(statement); - true - }, - ); - // end - self.add_end(end_position, &static_block.span); } } diff --git a/rust/parse_ast/src/ast_nodes/super_element.rs b/rust/parse_ast/src/ast_nodes/super_element.rs index db3a9cbda..cb5df0820 100644 --- a/rust/parse_ast/src/ast_nodes/super_element.rs +++ b/rust/parse_ast/src/ast_nodes/super_element.rs @@ -1,19 +1,13 @@ use swc_ecma_ast::Super; -use crate::convert_ast::converter::ast_constants::{ - SUPER_ELEMENT_RESERVED_BYTES, TYPE_SUPER_ELEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_super_element; impl<'a> AstConverter<'a> { pub fn store_super_element(&mut self, super_token: &Super) { - let end_position = self.add_type_and_start( - &TYPE_SUPER_ELEMENT, - &super_token.span, - SUPER_ELEMENT_RESERVED_BYTES, - false, + store_super_element!( + self, + span => &super_token.span ); - // end - self.add_end(end_position, &super_token.span); } } diff --git a/rust/parse_ast/src/ast_nodes/switch_case.rs b/rust/parse_ast/src/ast_nodes/switch_case.rs index 153d15cc9..e1a278674 100644 --- a/rust/parse_ast/src/ast_nodes/switch_case.rs +++ b/rust/parse_ast/src/ast_nodes/switch_case.rs @@ -1,34 +1,15 @@ use swc_ecma_ast::SwitchCase; -use crate::convert_ast::converter::ast_constants::{ - SWITCH_CASE_CONSEQUENT_OFFSET, SWITCH_CASE_RESERVED_BYTES, SWITCH_CASE_TEST_OFFSET, - TYPE_SWITCH_CASE, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_switch_case; impl<'a> AstConverter<'a> { pub fn store_switch_case(&mut self, switch_case: &SwitchCase) { - let end_position = self.add_type_and_start( - &TYPE_SWITCH_CASE, - &switch_case.span, - SWITCH_CASE_RESERVED_BYTES, - false, + store_switch_case!( + self, + span => &switch_case.span, + test => [switch_case.test, convert_expression], + consequent => [switch_case.cons, convert_statement] ); - // test - if let Some(expression) = switch_case.test.as_ref() { - self.update_reference_position(end_position + SWITCH_CASE_TEST_OFFSET); - self.convert_expression(expression) - } - // consequent - self.convert_item_list( - &switch_case.cons, - end_position + SWITCH_CASE_CONSEQUENT_OFFSET, - |ast_converter, statement| { - ast_converter.convert_statement(statement); - true - }, - ); - // end - self.add_end(end_position, &switch_case.span); } } diff --git a/rust/parse_ast/src/ast_nodes/switch_statement.rs b/rust/parse_ast/src/ast_nodes/switch_statement.rs index e86812db1..2c6ba5684 100644 --- a/rust/parse_ast/src/ast_nodes/switch_statement.rs +++ b/rust/parse_ast/src/ast_nodes/switch_statement.rs @@ -1,32 +1,15 @@ use swc_ecma_ast::SwitchStmt; -use crate::convert_ast::converter::ast_constants::{ - SWITCH_STATEMENT_CASES_OFFSET, SWITCH_STATEMENT_DISCRIMINANT_OFFSET, - SWITCH_STATEMENT_RESERVED_BYTES, TYPE_SWITCH_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_switch_statement; impl<'a> AstConverter<'a> { pub fn store_switch_statement(&mut self, switch_statement: &SwitchStmt) { - let end_position = self.add_type_and_start( - &TYPE_SWITCH_STATEMENT, - &switch_statement.span, - SWITCH_STATEMENT_RESERVED_BYTES, - false, + store_switch_statement!( + self, + span => &switch_statement.span, + discriminant => [switch_statement.discriminant, convert_expression], + cases => [switch_statement.cases, store_switch_case] ); - // discriminant - self.update_reference_position(end_position + SWITCH_STATEMENT_DISCRIMINANT_OFFSET); - self.convert_expression(&switch_statement.discriminant); - // cases - self.convert_item_list( - &switch_statement.cases, - end_position + SWITCH_STATEMENT_CASES_OFFSET, - |ast_converter, switch_case| { - ast_converter.store_switch_case(switch_case); - true - }, - ); - // end - self.add_end(end_position, &switch_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/tagged_template_expression.rs b/rust/parse_ast/src/ast_nodes/tagged_template_expression.rs index 40160c7d1..c7cdcacfa 100644 --- a/rust/parse_ast/src/ast_nodes/tagged_template_expression.rs +++ b/rust/parse_ast/src/ast_nodes/tagged_template_expression.rs @@ -1,26 +1,15 @@ use swc_ecma_ast::TaggedTpl; -use crate::convert_ast::converter::ast_constants::{ - TAGGED_TEMPLATE_EXPRESSION_QUASI_OFFSET, TAGGED_TEMPLATE_EXPRESSION_RESERVED_BYTES, - TAGGED_TEMPLATE_EXPRESSION_TAG_OFFSET, TYPE_TAGGED_TEMPLATE_EXPRESSION, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_tagged_template_expression; impl<'a> AstConverter<'a> { pub fn store_tagged_template_expression(&mut self, tagged_template: &TaggedTpl) { - let end_position = self.add_type_and_start( - &TYPE_TAGGED_TEMPLATE_EXPRESSION, - &tagged_template.span, - TAGGED_TEMPLATE_EXPRESSION_RESERVED_BYTES, - false, + store_tagged_template_expression!( + self, + span => &tagged_template.span, + tag => [tagged_template.tag, convert_expression], + quasi => [tagged_template.tpl, store_template_literal] ); - // tag - self.update_reference_position(end_position + TAGGED_TEMPLATE_EXPRESSION_TAG_OFFSET); - self.convert_expression(&tagged_template.tag); - // quasi - self.update_reference_position(end_position + TAGGED_TEMPLATE_EXPRESSION_QUASI_OFFSET); - self.store_template_literal(&tagged_template.tpl); - // end - self.add_end(end_position, &tagged_template.span); } } diff --git a/rust/parse_ast/src/ast_nodes/template_element.rs b/rust/parse_ast/src/ast_nodes/template_element.rs index 093764afc..3b9d385fb 100644 --- a/rust/parse_ast/src/ast_nodes/template_element.rs +++ b/rust/parse_ast/src/ast_nodes/template_element.rs @@ -1,36 +1,16 @@ use swc_ecma_ast::TplElement; -use crate::convert_ast::converter::ast_constants::{ - TEMPLATE_ELEMENT_COOKED_OFFSET, TEMPLATE_ELEMENT_FLAGS_OFFSET, TEMPLATE_ELEMENT_RAW_OFFSET, - TEMPLATE_ELEMENT_RESERVED_BYTES, TEMPLATE_ELEMENT_TAIL_FLAG, TYPE_TEMPLATE_ELEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::{store_template_element, store_template_element_flags}; impl<'a> AstConverter<'a> { pub fn store_template_element(&mut self, template_element: &TplElement) { - let end_position = self.add_type_and_start( - &TYPE_TEMPLATE_ELEMENT, - &template_element.span, - TEMPLATE_ELEMENT_RESERVED_BYTES, - false, + store_template_element!( + self, + span => &template_element.span, + tail => template_element.tail, + cooked => template_element.cooked.as_ref(), + raw => &template_element.raw ); - // flags - let mut flags = 0u32; - if template_element.tail { - flags |= TEMPLATE_ELEMENT_TAIL_FLAG - } - let flags_position = end_position + TEMPLATE_ELEMENT_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); - // raw - self.convert_string( - &template_element.raw, - end_position + TEMPLATE_ELEMENT_RAW_OFFSET, - ); - // cooked - if let Some(cooked) = template_element.cooked.as_ref() { - self.convert_string(cooked, end_position + TEMPLATE_ELEMENT_COOKED_OFFSET); - } - // end - self.add_end(end_position, &template_element.span); } } diff --git a/rust/parse_ast/src/ast_nodes/this_expression.rs b/rust/parse_ast/src/ast_nodes/this_expression.rs index 21273f629..37c0a8b86 100644 --- a/rust/parse_ast/src/ast_nodes/this_expression.rs +++ b/rust/parse_ast/src/ast_nodes/this_expression.rs @@ -1,19 +1,10 @@ use swc_ecma_ast::ThisExpr; -use crate::convert_ast::converter::ast_constants::{ - THIS_EXPRESSION_RESERVED_BYTES, TYPE_THIS_EXPRESSION, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_this_expression; impl<'a> AstConverter<'a> { pub fn store_this_expression(&mut self, this_expression: &ThisExpr) { - let end_position = self.add_type_and_start( - &TYPE_THIS_EXPRESSION, - &this_expression.span, - THIS_EXPRESSION_RESERVED_BYTES, - false, - ); - // end - self.add_end(end_position, &this_expression.span); + store_this_expression!(self, span => this_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/throw_statement.rs b/rust/parse_ast/src/ast_nodes/throw_statement.rs index badb056cb..32fe92397 100644 --- a/rust/parse_ast/src/ast_nodes/throw_statement.rs +++ b/rust/parse_ast/src/ast_nodes/throw_statement.rs @@ -1,22 +1,14 @@ use swc_ecma_ast::ThrowStmt; -use crate::convert_ast::converter::ast_constants::{ - THROW_STATEMENT_ARGUMENT_OFFSET, THROW_STATEMENT_RESERVED_BYTES, TYPE_THROW_STATEMENT, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_throw_statement; impl<'a> AstConverter<'a> { pub fn store_throw_statement(&mut self, throw_statement: &ThrowStmt) { - let end_position = self.add_type_and_start( - &TYPE_THROW_STATEMENT, - &throw_statement.span, - THROW_STATEMENT_RESERVED_BYTES, - false, + store_throw_statement!( + self, + span => &throw_statement.span, + argument => [throw_statement.arg, convert_expression] ); - // argument - self.update_reference_position(end_position + THROW_STATEMENT_ARGUMENT_OFFSET); - self.convert_expression(&throw_statement.arg); - // end - self.add_end(end_position, &throw_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/unary_expression.rs b/rust/parse_ast/src/ast_nodes/unary_expression.rs index 805a6bd18..291410285 100644 --- a/rust/parse_ast/src/ast_nodes/unary_expression.rs +++ b/rust/parse_ast/src/ast_nodes/unary_expression.rs @@ -1,29 +1,17 @@ use swc_ecma_ast::{UnaryExpr, UnaryOp}; -use crate::convert_ast::converter::ast_constants::{ - TYPE_UNARY_EXPRESSION, UNARY_EXPRESSION_ARGUMENT_OFFSET, UNARY_EXPRESSION_OPERATOR_OFFSET, - UNARY_EXPRESSION_RESERVED_BYTES, -}; use crate::convert_ast::converter::string_constants::{ STRING_BANG, STRING_DELETE, STRING_MINUS, STRING_PLUS, STRING_TILDE, STRING_TYPEOF, STRING_VOID, }; use crate::convert_ast::converter::AstConverter; +use crate::store_unary_expression; impl<'a> AstConverter<'a> { pub fn store_unary_expression(&mut self, unary_expression: &UnaryExpr) { - let end_position = self.add_type_and_start( - &TYPE_UNARY_EXPRESSION, - &unary_expression.span, - UNARY_EXPRESSION_RESERVED_BYTES, - false, - ); - // argument - self.update_reference_position(end_position + UNARY_EXPRESSION_ARGUMENT_OFFSET); - self.convert_expression(&unary_expression.arg); - // operator - let operator_position = end_position + UNARY_EXPRESSION_OPERATOR_OFFSET; - self.buffer[operator_position..operator_position + 4].copy_from_slice( - match unary_expression.op { + store_unary_expression!( + self, + span => &unary_expression.span, + operator => match unary_expression.op { UnaryOp::Minus => &STRING_MINUS, UnaryOp::Plus => &STRING_PLUS, UnaryOp::Bang => &STRING_BANG, @@ -32,8 +20,7 @@ impl<'a> AstConverter<'a> { UnaryOp::Void => &STRING_VOID, UnaryOp::Delete => &STRING_DELETE, }, + argument => [unary_expression.arg, convert_expression] ); - // end - self.add_end(end_position, &unary_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/update_expression.rs b/rust/parse_ast/src/ast_nodes/update_expression.rs index e2952b7ab..cbf59a3f5 100644 --- a/rust/parse_ast/src/ast_nodes/update_expression.rs +++ b/rust/parse_ast/src/ast_nodes/update_expression.rs @@ -1,40 +1,20 @@ use swc_ecma_ast::{UpdateExpr, UpdateOp}; -use crate::convert_ast::converter::ast_constants::{ - TYPE_UPDATE_EXPRESSION, UPDATE_EXPRESSION_ARGUMENT_OFFSET, UPDATE_EXPRESSION_FLAGS_OFFSET, - UPDATE_EXPRESSION_OPERATOR_OFFSET, UPDATE_EXPRESSION_PREFIX_FLAG, - UPDATE_EXPRESSION_RESERVED_BYTES, -}; use crate::convert_ast::converter::string_constants::{STRING_MINUSMINUS, STRING_PLUSPLUS}; use crate::convert_ast::converter::AstConverter; +use crate::{store_update_expression, store_update_expression_flags}; impl<'a> AstConverter<'a> { pub fn store_update_expression(&mut self, update_expression: &UpdateExpr) { - let end_position = self.add_type_and_start( - &TYPE_UPDATE_EXPRESSION, - &update_expression.span, - UPDATE_EXPRESSION_RESERVED_BYTES, - false, - ); - // argument - self.update_reference_position(end_position + UPDATE_EXPRESSION_ARGUMENT_OFFSET); - self.convert_expression(&update_expression.arg); - // flags - let mut flags = 0u32; - if update_expression.prefix { - flags |= UPDATE_EXPRESSION_PREFIX_FLAG; - } - let flags_position = end_position + UPDATE_EXPRESSION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); - // operator - let operator_position = end_position + UPDATE_EXPRESSION_OPERATOR_OFFSET; - self.buffer[operator_position..operator_position + 4].copy_from_slice( - match update_expression.op { + store_update_expression!( + self, + span => &update_expression.span, + prefix => update_expression.prefix, + operator => match update_expression.op { UpdateOp::PlusPlus => &STRING_PLUSPLUS, UpdateOp::MinusMinus => &STRING_MINUSMINUS, }, + argument => [update_expression.arg, convert_expression] ); - // end - self.add_end(end_position, &update_expression.span); } } diff --git a/rust/parse_ast/src/ast_nodes/while_statement.rs b/rust/parse_ast/src/ast_nodes/while_statement.rs index f7f5c8e09..d13525355 100644 --- a/rust/parse_ast/src/ast_nodes/while_statement.rs +++ b/rust/parse_ast/src/ast_nodes/while_statement.rs @@ -1,26 +1,15 @@ use swc_ecma_ast::WhileStmt; -use crate::convert_ast::converter::ast_constants::{ - TYPE_WHILE_STATEMENT, WHILE_STATEMENT_BODY_OFFSET, WHILE_STATEMENT_RESERVED_BYTES, - WHILE_STATEMENT_TEST_OFFSET, -}; use crate::convert_ast::converter::AstConverter; +use crate::store_while_statement; impl<'a> AstConverter<'a> { pub fn store_while_statement(&mut self, while_statement: &WhileStmt) { - let end_position = self.add_type_and_start( - &TYPE_WHILE_STATEMENT, - &while_statement.span, - WHILE_STATEMENT_RESERVED_BYTES, - false, + store_while_statement!( + self, + span => &while_statement.span, + test => [while_statement.test, convert_expression], + body => [while_statement.body, convert_statement] ); - // test - self.update_reference_position(end_position + WHILE_STATEMENT_TEST_OFFSET); - self.convert_expression(&while_statement.test); - // body - self.update_reference_position(end_position + WHILE_STATEMENT_BODY_OFFSET); - self.convert_statement(&while_statement.body); - // end - self.add_end(end_position, &while_statement.span); } } diff --git a/rust/parse_ast/src/ast_nodes/yield_expression.rs b/rust/parse_ast/src/ast_nodes/yield_expression.rs index dea9b60cc..dbd4d3f9a 100644 --- a/rust/parse_ast/src/ast_nodes/yield_expression.rs +++ b/rust/parse_ast/src/ast_nodes/yield_expression.rs @@ -1,32 +1,15 @@ use swc_ecma_ast::YieldExpr; -use crate::convert_ast::converter::ast_constants::{ - TYPE_YIELD_EXPRESSION, YIELD_EXPRESSION_ARGUMENT_OFFSET, YIELD_EXPRESSION_DELEGATE_FLAG, - YIELD_EXPRESSION_FLAGS_OFFSET, YIELD_EXPRESSION_RESERVED_BYTES, -}; use crate::convert_ast::converter::AstConverter; +use crate::{store_yield_expression, store_yield_expression_flags}; impl<'a> AstConverter<'a> { pub fn store_yield_expression(&mut self, yield_expression: &YieldExpr) { - let end_position = self.add_type_and_start( - &TYPE_YIELD_EXPRESSION, - &yield_expression.span, - YIELD_EXPRESSION_RESERVED_BYTES, - false, + store_yield_expression!( + self, + span => yield_expression.span, + delegate => yield_expression.delegate, + argument => [yield_expression.arg, convert_expression] ); - // flags - let mut flags = 0u32; - if yield_expression.delegate { - flags |= YIELD_EXPRESSION_DELEGATE_FLAG; - } - let flags_position = end_position + YIELD_EXPRESSION_FLAGS_OFFSET; - self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); - // argument - if let Some(expression) = yield_expression.arg.as_ref() { - self.update_reference_position(end_position + YIELD_EXPRESSION_ARGUMENT_OFFSET); - self.convert_expression(expression) - } - // end - self.add_end(end_position, &yield_expression.span); } } diff --git a/rust/parse_ast/src/convert_ast/converter.rs b/rust/parse_ast/src/convert_ast/converter.rs index 89897f6fb..b82cf23e1 100644 --- a/rust/parse_ast/src/convert_ast/converter.rs +++ b/rust/parse_ast/src/convert_ast/converter.rs @@ -24,6 +24,7 @@ pub mod string_constants; mod utf16_positions; pub mod ast_constants; +mod ast_macros; pub struct AstConverter<'a> { pub buffer: Vec, @@ -267,7 +268,7 @@ impl<'a> AstConverter<'a> { pub(crate) fn convert_export_specifier(&mut self, export_specifier: &ExportSpecifier) { match export_specifier { ExportSpecifier::Named(export_named_specifier) => { - self.store_export_named_specifier(export_named_specifier) + self.store_export_specifier(export_named_specifier) } ExportSpecifier::Namespace(_) => unimplemented!("Cannot convert ExportSpecifier::Namespace"), ExportSpecifier::Default(_) => unimplemented!("Cannot convert ExportSpecifier::Default"), diff --git a/rust/parse_ast/src/convert_ast/converter/ast_constants.rs b/rust/parse_ast/src/convert_ast/converter/ast_constants.rs index 011154f5a..2048f4721 100644 --- a/rust/parse_ast/src/convert_ast/converter/ast_constants.rs +++ b/rust/parse_ast/src/convert_ast/converter/ast_constants.rs @@ -6,80 +6,38 @@ pub const TYPE_PARSE_ERROR: [u8; 4] = 1u32.to_ne_bytes(); pub const TYPE_ARRAY_EXPRESSION: [u8; 4] = 2u32.to_ne_bytes(); pub const TYPE_ARRAY_PATTERN: [u8; 4] = 3u32.to_ne_bytes(); pub const TYPE_ARROW_FUNCTION_EXPRESSION: [u8; 4] = 4u32.to_ne_bytes(); -pub const TYPE_ASSIGNMENT_EXPRESSION: [u8; 4] = 5u32.to_ne_bytes(); pub const TYPE_ASSIGNMENT_PATTERN: [u8; 4] = 6u32.to_ne_bytes(); -pub const TYPE_AWAIT_EXPRESSION: [u8; 4] = 7u32.to_ne_bytes(); pub const TYPE_BINARY_EXPRESSION: [u8; 4] = 8u32.to_ne_bytes(); pub const TYPE_BLOCK_STATEMENT: [u8; 4] = 9u32.to_ne_bytes(); -pub const TYPE_BREAK_STATEMENT: [u8; 4] = 10u32.to_ne_bytes(); pub const TYPE_CALL_EXPRESSION: [u8; 4] = 11u32.to_ne_bytes(); pub const TYPE_CATCH_CLAUSE: [u8; 4] = 12u32.to_ne_bytes(); pub const TYPE_CHAIN_EXPRESSION: [u8; 4] = 13u32.to_ne_bytes(); pub const TYPE_CLASS_BODY: [u8; 4] = 14u32.to_ne_bytes(); pub const TYPE_CLASS_DECLARATION: [u8; 4] = 15u32.to_ne_bytes(); pub const TYPE_CLASS_EXPRESSION: [u8; 4] = 16u32.to_ne_bytes(); -pub const TYPE_CONDITIONAL_EXPRESSION: [u8; 4] = 17u32.to_ne_bytes(); -pub const TYPE_CONTINUE_STATEMENT: [u8; 4] = 18u32.to_ne_bytes(); -pub const TYPE_DEBUGGER_STATEMENT: [u8; 4] = 19u32.to_ne_bytes(); -pub const TYPE_DIRECTIVE: [u8; 4] = 20u32.to_ne_bytes(); -pub const TYPE_DO_WHILE_STATEMENT: [u8; 4] = 21u32.to_ne_bytes(); -pub const TYPE_EMPTY_STATEMENT: [u8; 4] = 22u32.to_ne_bytes(); pub const TYPE_EXPORT_ALL_DECLARATION: [u8; 4] = 23u32.to_ne_bytes(); pub const TYPE_EXPORT_DEFAULT_DECLARATION: [u8; 4] = 24u32.to_ne_bytes(); pub const TYPE_EXPORT_NAMED_DECLARATION: [u8; 4] = 25u32.to_ne_bytes(); -pub const TYPE_EXPORT_SPECIFIER: [u8; 4] = 26u32.to_ne_bytes(); -pub const TYPE_EXPRESSION_STATEMENT: [u8; 4] = 27u32.to_ne_bytes(); -pub const TYPE_FOR_IN_STATEMENT: [u8; 4] = 28u32.to_ne_bytes(); -pub const TYPE_FOR_OF_STATEMENT: [u8; 4] = 29u32.to_ne_bytes(); -pub const TYPE_FOR_STATEMENT: [u8; 4] = 30u32.to_ne_bytes(); pub const TYPE_FUNCTION_DECLARATION: [u8; 4] = 31u32.to_ne_bytes(); pub const TYPE_FUNCTION_EXPRESSION: [u8; 4] = 32u32.to_ne_bytes(); pub const TYPE_IDENTIFIER: [u8; 4] = 33u32.to_ne_bytes(); -pub const TYPE_IF_STATEMENT: [u8; 4] = 34u32.to_ne_bytes(); pub const TYPE_IMPORT_ATTRIBUTE: [u8; 4] = 35u32.to_ne_bytes(); pub const TYPE_IMPORT_DECLARATION: [u8; 4] = 36u32.to_ne_bytes(); -pub const TYPE_IMPORT_DEFAULT_SPECIFIER: [u8; 4] = 37u32.to_ne_bytes(); pub const TYPE_IMPORT_EXPRESSION: [u8; 4] = 38u32.to_ne_bytes(); -pub const TYPE_IMPORT_NAMESPACE_SPECIFIER: [u8; 4] = 39u32.to_ne_bytes(); -pub const TYPE_IMPORT_SPECIFIER: [u8; 4] = 40u32.to_ne_bytes(); -pub const TYPE_LABELED_STATEMENT: [u8; 4] = 41u32.to_ne_bytes(); -pub const TYPE_LITERAL_BIG_INT: [u8; 4] = 42u32.to_ne_bytes(); -pub const TYPE_LITERAL_BOOLEAN: [u8; 4] = 43u32.to_ne_bytes(); -pub const TYPE_LITERAL_NULL: [u8; 4] = 44u32.to_ne_bytes(); -pub const TYPE_LITERAL_NUMBER: [u8; 4] = 45u32.to_ne_bytes(); -pub const TYPE_LITERAL_REG_EXP: [u8; 4] = 46u32.to_ne_bytes(); -pub const TYPE_LITERAL_STRING: [u8; 4] = 47u32.to_ne_bytes(); pub const TYPE_LOGICAL_EXPRESSION: [u8; 4] = 48u32.to_ne_bytes(); pub const TYPE_MEMBER_EXPRESSION: [u8; 4] = 49u32.to_ne_bytes(); pub const TYPE_META_PROPERTY: [u8; 4] = 50u32.to_ne_bytes(); pub const TYPE_METHOD_DEFINITION: [u8; 4] = 51u32.to_ne_bytes(); pub const TYPE_NEW_EXPRESSION: [u8; 4] = 52u32.to_ne_bytes(); -pub const TYPE_OBJECT_EXPRESSION: [u8; 4] = 53u32.to_ne_bytes(); -pub const TYPE_OBJECT_PATTERN: [u8; 4] = 54u32.to_ne_bytes(); -pub const TYPE_PRIVATE_IDENTIFIER: [u8; 4] = 55u32.to_ne_bytes(); pub const TYPE_PROGRAM: [u8; 4] = 56u32.to_ne_bytes(); pub const TYPE_PROPERTY: [u8; 4] = 57u32.to_ne_bytes(); pub const TYPE_PROPERTY_DEFINITION: [u8; 4] = 58u32.to_ne_bytes(); pub const TYPE_REST_ELEMENT: [u8; 4] = 59u32.to_ne_bytes(); -pub const TYPE_RETURN_STATEMENT: [u8; 4] = 60u32.to_ne_bytes(); -pub const TYPE_SEQUENCE_EXPRESSION: [u8; 4] = 61u32.to_ne_bytes(); pub const TYPE_SPREAD_ELEMENT: [u8; 4] = 62u32.to_ne_bytes(); -pub const TYPE_STATIC_BLOCK: [u8; 4] = 63u32.to_ne_bytes(); -pub const TYPE_SUPER_ELEMENT: [u8; 4] = 64u32.to_ne_bytes(); -pub const TYPE_SWITCH_CASE: [u8; 4] = 65u32.to_ne_bytes(); -pub const TYPE_SWITCH_STATEMENT: [u8; 4] = 66u32.to_ne_bytes(); -pub const TYPE_TAGGED_TEMPLATE_EXPRESSION: [u8; 4] = 67u32.to_ne_bytes(); -pub const TYPE_TEMPLATE_ELEMENT: [u8; 4] = 68u32.to_ne_bytes(); pub const TYPE_TEMPLATE_LITERAL: [u8; 4] = 69u32.to_ne_bytes(); -pub const TYPE_THIS_EXPRESSION: [u8; 4] = 70u32.to_ne_bytes(); -pub const TYPE_THROW_STATEMENT: [u8; 4] = 71u32.to_ne_bytes(); pub const TYPE_TRY_STATEMENT: [u8; 4] = 72u32.to_ne_bytes(); -pub const TYPE_UNARY_EXPRESSION: [u8; 4] = 73u32.to_ne_bytes(); -pub const TYPE_UPDATE_EXPRESSION: [u8; 4] = 74u32.to_ne_bytes(); pub const TYPE_VARIABLE_DECLARATION: [u8; 4] = 75u32.to_ne_bytes(); pub const TYPE_VARIABLE_DECLARATOR: [u8; 4] = 76u32.to_ne_bytes(); -pub const TYPE_WHILE_STATEMENT: [u8; 4] = 77u32.to_ne_bytes(); -pub const TYPE_YIELD_EXPRESSION: [u8; 4] = 78u32.to_ne_bytes(); pub const PANIC_ERROR_RESERVED_BYTES: usize = 8; pub const PANIC_ERROR_MESSAGE_OFFSET: usize = 4; @@ -94,26 +52,14 @@ pub const ARRAY_PATTERN_RESERVED_BYTES: usize = 8; pub const ARRAY_PATTERN_ELEMENTS_OFFSET: usize = 4; pub const ARROW_FUNCTION_EXPRESSION_RESERVED_BYTES: usize = 20; -pub const ARROW_FUNCTION_EXPRESSION_FLAGS_OFFSET: usize = 4; -pub const ARROW_FUNCTION_EXPRESSION_ASYNC_FLAG: u32 = 1; -pub const ARROW_FUNCTION_EXPRESSION_EXPRESSION_FLAG: u32 = 2; -pub const ARROW_FUNCTION_EXPRESSION_GENERATOR_FLAG: u32 = 4; pub const ARROW_FUNCTION_EXPRESSION_ANNOTATIONS_OFFSET: usize = 8; pub const ARROW_FUNCTION_EXPRESSION_PARAMS_OFFSET: usize = 12; pub const ARROW_FUNCTION_EXPRESSION_BODY_OFFSET: usize = 16; -pub const ASSIGNMENT_EXPRESSION_RESERVED_BYTES: usize = 16; -pub const ASSIGNMENT_EXPRESSION_OPERATOR_OFFSET: usize = 4; -pub const ASSIGNMENT_EXPRESSION_LEFT_OFFSET: usize = 8; -pub const ASSIGNMENT_EXPRESSION_RIGHT_OFFSET: usize = 12; - pub const ASSIGNMENT_PATTERN_RESERVED_BYTES: usize = 12; pub const ASSIGNMENT_PATTERN_LEFT_OFFSET: usize = 4; pub const ASSIGNMENT_PATTERN_RIGHT_OFFSET: usize = 8; -pub const AWAIT_EXPRESSION_RESERVED_BYTES: usize = 8; -pub const AWAIT_EXPRESSION_ARGUMENT_OFFSET: usize = 4; - pub const BINARY_EXPRESSION_RESERVED_BYTES: usize = 16; pub const BINARY_EXPRESSION_OPERATOR_OFFSET: usize = 4; pub const BINARY_EXPRESSION_LEFT_OFFSET: usize = 8; @@ -122,12 +68,7 @@ pub const BINARY_EXPRESSION_RIGHT_OFFSET: usize = 12; pub const BLOCK_STATEMENT_RESERVED_BYTES: usize = 8; pub const BLOCK_STATEMENT_BODY_OFFSET: usize = 4; -pub const BREAK_STATEMENT_RESERVED_BYTES: usize = 8; -pub const BREAK_STATEMENT_LABEL_OFFSET: usize = 4; - pub const CALL_EXPRESSION_RESERVED_BYTES: usize = 20; -pub const CALL_EXPRESSION_FLAGS_OFFSET: usize = 4; -pub const CALL_EXPRESSION_OPTIONAL_FLAG: u32 = 1; pub const CALL_EXPRESSION_ANNOTATIONS_OFFSET: usize = 8; pub const CALL_EXPRESSION_CALLEE_OFFSET: usize = 12; pub const CALL_EXPRESSION_ARGUMENTS_OFFSET: usize = 16; @@ -147,26 +88,6 @@ pub const CLASS_DECLARATION_ID_OFFSET: usize = 4; pub const CLASS_DECLARATION_SUPER_CLASS_OFFSET: usize = 8; pub const CLASS_DECLARATION_BODY_OFFSET: usize = 12; -pub const CONDITIONAL_EXPRESSION_RESERVED_BYTES: usize = 16; -pub const CONDITIONAL_EXPRESSION_TEST_OFFSET: usize = 4; -pub const CONDITIONAL_EXPRESSION_CONSEQUENT_OFFSET: usize = 8; -pub const CONDITIONAL_EXPRESSION_ALTERNATE_OFFSET: usize = 12; - -pub const CONTINUE_STATEMENT_RESERVED_BYTES: usize = 8; -pub const CONTINUE_STATEMENT_LABEL_OFFSET: usize = 4; - -pub const DEBUGGER_STATEMENT_RESERVED_BYTES: usize = 4; - -pub const DIRECTIVE_RESERVED_BYTES: usize = 12; -pub const DIRECTIVE_DIRECTIVE_OFFSET: usize = 4; -pub const DIRECTIVE_EXPRESSION_OFFSET: usize = 8; - -pub const DO_WHILE_STATEMENT_RESERVED_BYTES: usize = 12; -pub const DO_WHILE_STATEMENT_BODY_OFFSET: usize = 4; -pub const DO_WHILE_STATEMENT_TEST_OFFSET: usize = 8; - -pub const EMPTY_STATEMENT_RESERVED_BYTES: usize = 4; - pub const EXPORT_ALL_DECLARATION_RESERVED_BYTES: usize = 16; pub const EXPORT_ALL_DECLARATION_EXPORTED_OFFSET: usize = 4; pub const EXPORT_ALL_DECLARATION_SOURCE_OFFSET: usize = 8; @@ -181,35 +102,7 @@ pub const EXPORT_NAMED_DECLARATION_SOURCE_OFFSET: usize = 8; pub const EXPORT_NAMED_DECLARATION_ATTRIBUTES_OFFSET: usize = 12; pub const EXPORT_NAMED_DECLARATION_DECLARATION_OFFSET: usize = 16; -pub const EXPORT_SPECIFIER_RESERVED_BYTES: usize = 12; -pub const EXPORT_SPECIFIER_LOCAL_OFFSET: usize = 4; -pub const EXPORT_SPECIFIER_EXPORTED_OFFSET: usize = 8; - -pub const EXPRESSION_STATEMENT_RESERVED_BYTES: usize = 8; -pub const EXPRESSION_STATEMENT_EXPRESSION_OFFSET: usize = 4; - -pub const FOR_IN_STATEMENT_RESERVED_BYTES: usize = 16; -pub const FOR_IN_STATEMENT_LEFT_OFFSET: usize = 4; -pub const FOR_IN_STATEMENT_RIGHT_OFFSET: usize = 8; -pub const FOR_IN_STATEMENT_BODY_OFFSET: usize = 12; - -pub const FOR_OF_STATEMENT_RESERVED_BYTES: usize = 20; -pub const FOR_OF_STATEMENT_FLAGS_OFFSET: usize = 4; -pub const FOR_OF_STATEMENT_AWAIT_FLAG: u32 = 1; -pub const FOR_OF_STATEMENT_LEFT_OFFSET: usize = 8; -pub const FOR_OF_STATEMENT_RIGHT_OFFSET: usize = 12; -pub const FOR_OF_STATEMENT_BODY_OFFSET: usize = 16; - -pub const FOR_STATEMENT_RESERVED_BYTES: usize = 20; -pub const FOR_STATEMENT_INIT_OFFSET: usize = 4; -pub const FOR_STATEMENT_TEST_OFFSET: usize = 8; -pub const FOR_STATEMENT_UPDATE_OFFSET: usize = 12; -pub const FOR_STATEMENT_BODY_OFFSET: usize = 16; - pub const FUNCTION_DECLARATION_RESERVED_BYTES: usize = 24; -pub const FUNCTION_DECLARATION_FLAGS_OFFSET: usize = 4; -pub const FUNCTION_DECLARATION_ASYNC_FLAG: u32 = 1; -pub const FUNCTION_DECLARATION_GENERATOR_FLAG: u32 = 2; pub const FUNCTION_DECLARATION_ANNOTATIONS_OFFSET: usize = 8; pub const FUNCTION_DECLARATION_ID_OFFSET: usize = 12; pub const FUNCTION_DECLARATION_PARAMS_OFFSET: usize = 16; @@ -218,11 +111,6 @@ pub const FUNCTION_DECLARATION_BODY_OFFSET: usize = 20; pub const IDENTIFIER_RESERVED_BYTES: usize = 8; pub const IDENTIFIER_NAME_OFFSET: usize = 4; -pub const IF_STATEMENT_RESERVED_BYTES: usize = 16; -pub const IF_STATEMENT_TEST_OFFSET: usize = 4; -pub const IF_STATEMENT_CONSEQUENT_OFFSET: usize = 8; -pub const IF_STATEMENT_ALTERNATE_OFFSET: usize = 12; - pub const IMPORT_ATTRIBUTE_RESERVED_BYTES: usize = 12; pub const IMPORT_ATTRIBUTE_KEY_OFFSET: usize = 4; pub const IMPORT_ATTRIBUTE_VALUE_OFFSET: usize = 8; @@ -232,50 +120,11 @@ pub const IMPORT_DECLARATION_SPECIFIERS_OFFSET: usize = 4; pub const IMPORT_DECLARATION_SOURCE_OFFSET: usize = 8; pub const IMPORT_DECLARATION_ATTRIBUTES_OFFSET: usize = 12; -pub const IMPORT_DEFAULT_SPECIFIER_RESERVED_BYTES: usize = 8; -pub const IMPORT_DEFAULT_SPECIFIER_LOCAL_OFFSET: usize = 4; - pub const IMPORT_EXPRESSION_RESERVED_BYTES: usize = 12; pub const IMPORT_EXPRESSION_SOURCE_OFFSET: usize = 4; pub const IMPORT_EXPRESSION_OPTIONS_OFFSET: usize = 8; -pub const IMPORT_NAMESPACE_SPECIFIER_RESERVED_BYTES: usize = 8; -pub const IMPORT_NAMESPACE_SPECIFIER_LOCAL_OFFSET: usize = 4; - -pub const IMPORT_SPECIFIER_RESERVED_BYTES: usize = 12; -pub const IMPORT_SPECIFIER_IMPORTED_OFFSET: usize = 4; -pub const IMPORT_SPECIFIER_LOCAL_OFFSET: usize = 8; - -pub const LABELED_STATEMENT_RESERVED_BYTES: usize = 12; -pub const LABELED_STATEMENT_LABEL_OFFSET: usize = 4; -pub const LABELED_STATEMENT_BODY_OFFSET: usize = 8; - -pub const LITERAL_BIG_INT_RESERVED_BYTES: usize = 12; -pub const LITERAL_BIG_INT_BIGINT_OFFSET: usize = 4; -pub const LITERAL_BIG_INT_RAW_OFFSET: usize = 8; - -pub const LITERAL_BOOLEAN_RESERVED_BYTES: usize = 8; -pub const LITERAL_BOOLEAN_FLAGS_OFFSET: usize = 4; -pub const LITERAL_BOOLEAN_VALUE_FLAG: u32 = 1; - -pub const LITERAL_NULL_RESERVED_BYTES: usize = 4; - -pub const LITERAL_NUMBER_RESERVED_BYTES: usize = 16; -pub const LITERAL_NUMBER_RAW_OFFSET: usize = 4; -pub const LITERAL_NUMBER_VALUE_OFFSET: usize = 8; - -pub const LITERAL_REG_EXP_RESERVED_BYTES: usize = 12; -pub const LITERAL_REG_EXP_FLAGS_OFFSET: usize = 4; -pub const LITERAL_REG_EXP_PATTERN_OFFSET: usize = 8; - -pub const LITERAL_STRING_RESERVED_BYTES: usize = 12; -pub const LITERAL_STRING_VALUE_OFFSET: usize = 4; -pub const LITERAL_STRING_RAW_OFFSET: usize = 8; - pub const MEMBER_EXPRESSION_RESERVED_BYTES: usize = 16; -pub const MEMBER_EXPRESSION_FLAGS_OFFSET: usize = 4; -pub const MEMBER_EXPRESSION_COMPUTED_FLAG: u32 = 1; -pub const MEMBER_EXPRESSION_OPTIONAL_FLAG: u32 = 2; pub const MEMBER_EXPRESSION_OBJECT_OFFSET: usize = 8; pub const MEMBER_EXPRESSION_PROPERTY_OFFSET: usize = 12; @@ -284,9 +133,6 @@ pub const META_PROPERTY_META_OFFSET: usize = 4; pub const META_PROPERTY_PROPERTY_OFFSET: usize = 8; pub const METHOD_DEFINITION_RESERVED_BYTES: usize = 20; -pub const METHOD_DEFINITION_FLAGS_OFFSET: usize = 4; -pub const METHOD_DEFINITION_STATIC_FLAG: u32 = 1; -pub const METHOD_DEFINITION_COMPUTED_FLAG: u32 = 2; pub const METHOD_DEFINITION_KEY_OFFSET: usize = 8; pub const METHOD_DEFINITION_VALUE_OFFSET: usize = 12; pub const METHOD_DEFINITION_KIND_OFFSET: usize = 16; @@ -296,94 +142,34 @@ pub const NEW_EXPRESSION_ANNOTATIONS_OFFSET: usize = 4; pub const NEW_EXPRESSION_CALLEE_OFFSET: usize = 8; pub const NEW_EXPRESSION_ARGUMENTS_OFFSET: usize = 12; -pub const OBJECT_EXPRESSION_RESERVED_BYTES: usize = 8; -pub const OBJECT_EXPRESSION_PROPERTIES_OFFSET: usize = 4; - -pub const OBJECT_PATTERN_RESERVED_BYTES: usize = 8; -pub const OBJECT_PATTERN_PROPERTIES_OFFSET: usize = 4; - -pub const PRIVATE_IDENTIFIER_RESERVED_BYTES: usize = 8; -pub const PRIVATE_IDENTIFIER_NAME_OFFSET: usize = 4; - pub const PROGRAM_RESERVED_BYTES: usize = 12; pub const PROGRAM_BODY_OFFSET: usize = 4; pub const PROGRAM_INVALID_ANNOTATIONS_OFFSET: usize = 8; pub const PROPERTY_RESERVED_BYTES: usize = 20; -pub const PROPERTY_FLAGS_OFFSET: usize = 4; -pub const PROPERTY_METHOD_FLAG: u32 = 1; -pub const PROPERTY_SHORTHAND_FLAG: u32 = 2; -pub const PROPERTY_COMPUTED_FLAG: u32 = 4; pub const PROPERTY_KEY_OFFSET: usize = 8; pub const PROPERTY_VALUE_OFFSET: usize = 12; pub const PROPERTY_KIND_OFFSET: usize = 16; pub const PROPERTY_DEFINITION_RESERVED_BYTES: usize = 16; -pub const PROPERTY_DEFINITION_FLAGS_OFFSET: usize = 4; -pub const PROPERTY_DEFINITION_STATIC_FLAG: u32 = 1; -pub const PROPERTY_DEFINITION_COMPUTED_FLAG: u32 = 2; pub const PROPERTY_DEFINITION_KEY_OFFSET: usize = 8; pub const PROPERTY_DEFINITION_VALUE_OFFSET: usize = 12; pub const REST_ELEMENT_RESERVED_BYTES: usize = 8; pub const REST_ELEMENT_ARGUMENT_OFFSET: usize = 4; -pub const RETURN_STATEMENT_RESERVED_BYTES: usize = 8; -pub const RETURN_STATEMENT_ARGUMENT_OFFSET: usize = 4; - -pub const SEQUENCE_EXPRESSION_RESERVED_BYTES: usize = 8; -pub const SEQUENCE_EXPRESSION_EXPRESSIONS_OFFSET: usize = 4; - pub const SPREAD_ELEMENT_RESERVED_BYTES: usize = 8; pub const SPREAD_ELEMENT_ARGUMENT_OFFSET: usize = 4; -pub const STATIC_BLOCK_RESERVED_BYTES: usize = 8; -pub const STATIC_BLOCK_BODY_OFFSET: usize = 4; - -pub const SUPER_ELEMENT_RESERVED_BYTES: usize = 4; - -pub const SWITCH_CASE_RESERVED_BYTES: usize = 12; -pub const SWITCH_CASE_TEST_OFFSET: usize = 4; -pub const SWITCH_CASE_CONSEQUENT_OFFSET: usize = 8; - -pub const SWITCH_STATEMENT_RESERVED_BYTES: usize = 12; -pub const SWITCH_STATEMENT_DISCRIMINANT_OFFSET: usize = 4; -pub const SWITCH_STATEMENT_CASES_OFFSET: usize = 8; - -pub const TAGGED_TEMPLATE_EXPRESSION_RESERVED_BYTES: usize = 12; -pub const TAGGED_TEMPLATE_EXPRESSION_TAG_OFFSET: usize = 4; -pub const TAGGED_TEMPLATE_EXPRESSION_QUASI_OFFSET: usize = 8; - -pub const TEMPLATE_ELEMENT_RESERVED_BYTES: usize = 16; -pub const TEMPLATE_ELEMENT_FLAGS_OFFSET: usize = 4; -pub const TEMPLATE_ELEMENT_TAIL_FLAG: u32 = 1; -pub const TEMPLATE_ELEMENT_COOKED_OFFSET: usize = 8; -pub const TEMPLATE_ELEMENT_RAW_OFFSET: usize = 12; - pub const TEMPLATE_LITERAL_RESERVED_BYTES: usize = 12; pub const TEMPLATE_LITERAL_QUASIS_OFFSET: usize = 4; pub const TEMPLATE_LITERAL_EXPRESSIONS_OFFSET: usize = 8; -pub const THIS_EXPRESSION_RESERVED_BYTES: usize = 4; - -pub const THROW_STATEMENT_RESERVED_BYTES: usize = 8; -pub const THROW_STATEMENT_ARGUMENT_OFFSET: usize = 4; - pub const TRY_STATEMENT_RESERVED_BYTES: usize = 16; pub const TRY_STATEMENT_BLOCK_OFFSET: usize = 4; pub const TRY_STATEMENT_HANDLER_OFFSET: usize = 8; pub const TRY_STATEMENT_FINALIZER_OFFSET: usize = 12; -pub const UNARY_EXPRESSION_RESERVED_BYTES: usize = 12; -pub const UNARY_EXPRESSION_OPERATOR_OFFSET: usize = 4; -pub const UNARY_EXPRESSION_ARGUMENT_OFFSET: usize = 8; - -pub const UPDATE_EXPRESSION_RESERVED_BYTES: usize = 16; -pub const UPDATE_EXPRESSION_FLAGS_OFFSET: usize = 4; -pub const UPDATE_EXPRESSION_PREFIX_FLAG: u32 = 1; -pub const UPDATE_EXPRESSION_OPERATOR_OFFSET: usize = 8; -pub const UPDATE_EXPRESSION_ARGUMENT_OFFSET: usize = 12; - pub const VARIABLE_DECLARATION_RESERVED_BYTES: usize = 12; pub const VARIABLE_DECLARATION_KIND_OFFSET: usize = 4; pub const VARIABLE_DECLARATION_DECLARATIONS_OFFSET: usize = 8; @@ -391,12 +177,3 @@ pub const VARIABLE_DECLARATION_DECLARATIONS_OFFSET: usize = 8; pub const VARIABLE_DECLARATOR_RESERVED_BYTES: usize = 12; pub const VARIABLE_DECLARATOR_ID_OFFSET: usize = 4; pub const VARIABLE_DECLARATOR_INIT_OFFSET: usize = 8; - -pub const WHILE_STATEMENT_RESERVED_BYTES: usize = 12; -pub const WHILE_STATEMENT_TEST_OFFSET: usize = 4; -pub const WHILE_STATEMENT_BODY_OFFSET: usize = 8; - -pub const YIELD_EXPRESSION_RESERVED_BYTES: usize = 12; -pub const YIELD_EXPRESSION_FLAGS_OFFSET: usize = 4; -pub const YIELD_EXPRESSION_DELEGATE_FLAG: u32 = 1; -pub const YIELD_EXPRESSION_ARGUMENT_OFFSET: usize = 8; diff --git a/rust/parse_ast/src/convert_ast/converter/ast_macros.rs b/rust/parse_ast/src/convert_ast/converter/ast_macros.rs new file mode 100644 index 000000000..dee54ed74 --- /dev/null +++ b/rust/parse_ast/src/convert_ast/converter/ast_macros.rs @@ -0,0 +1,889 @@ +// This file is generated by scripts/generate-ast-macros.js. +// Do not edit this file directly. + +#[macro_export] +macro_rules! store_assignment_expression { + ($self:expr, span => $span:expr, operator => $operator_value:expr, left => [$left_value:expr, $left_converter:ident], right => [$right_value:expr, $right_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&5u32.to_ne_bytes(), &$span, 16, false); + // operator + let operator_position = end_position + 4; + $self.buffer[operator_position..operator_position + 4].copy_from_slice($operator_value); + // left + $self.update_reference_position(end_position + 8); + $self.$left_converter(&$left_value); + // right + $self.update_reference_position(end_position + 12); + $self.$right_converter(&$right_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_await_expression { + ($self:expr, span => $span:expr, argument => [$argument_value:expr, $argument_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&7u32.to_ne_bytes(), &$span, 8, false); + // argument + $self.update_reference_position(end_position + 4); + $self.$argument_converter(&$argument_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_break_statement { + ($self:expr, span => $span:expr, label => [$label_value:expr, $label_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&10u32.to_ne_bytes(), &$span, 8, false); + // label + if let Some(value) = $label_value.as_ref() { + $self.update_reference_position(end_position + 4); + $self.$label_converter(value); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_conditional_expression { + ($self:expr, span => $span:expr, test => [$test_value:expr, $test_converter:ident], consequent => [$consequent_value:expr, $consequent_converter:ident], alternate => [$alternate_value:expr, $alternate_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&17u32.to_ne_bytes(), &$span, 16, false); + // test + $self.update_reference_position(end_position + 4); + $self.$test_converter(&$test_value); + // consequent + $self.update_reference_position(end_position + 8); + $self.$consequent_converter(&$consequent_value); + // alternate + $self.update_reference_position(end_position + 12); + $self.$alternate_converter(&$alternate_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_continue_statement { + ($self:expr, span => $span:expr, label => [$label_value:expr, $label_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&18u32.to_ne_bytes(), &$span, 8, false); + // label + if let Some(value) = $label_value.as_ref() { + $self.update_reference_position(end_position + 4); + $self.$label_converter(value); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_debugger_statement { + ($self:expr, span => $span:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&19u32.to_ne_bytes(), &$span, 4, false); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_directive { + ($self:expr, span => $span:expr, directive => $directive_value:expr, expression => [$expression_value:expr, $expression_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&20u32.to_ne_bytes(), &$span, 12, false); + // directive + $self.convert_string($directive_value, end_position + 4); + // expression + $self.update_reference_position(end_position + 8); + $self.$expression_converter(&$expression_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_do_while_statement { + ($self:expr, span => $span:expr, body => [$body_value:expr, $body_converter:ident], test => [$test_value:expr, $test_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&21u32.to_ne_bytes(), &$span, 12, false); + // body + $self.update_reference_position(end_position + 4); + $self.$body_converter(&$body_value); + // test + $self.update_reference_position(end_position + 8); + $self.$test_converter(&$test_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_empty_statement { + ($self:expr, span => $span:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&22u32.to_ne_bytes(), &$span, 4, false); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_export_specifier { + ($self:expr, span => $span:expr, local => [$local_value:expr, $local_converter:ident], exported => [$exported_value:expr, $exported_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&26u32.to_ne_bytes(), &$span, 12, false); + // local + $self.update_reference_position(end_position + 4); + $self.$local_converter(&$local_value); + // exported + if let Some(value) = $exported_value.as_ref() { + $self.update_reference_position(end_position + 8); + $self.$exported_converter(value); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_expression_statement { + ($self:expr, span => $span:expr, expression => [$expression_value:expr, $expression_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&27u32.to_ne_bytes(), &$span, 8, false); + // expression + $self.update_reference_position(end_position + 4); + $self.$expression_converter(&$expression_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_for_in_statement { + ($self:expr, span => $span:expr, left => [$left_value:expr, $left_converter:ident], right => [$right_value:expr, $right_converter:ident], body => [$body_value:expr, $body_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&28u32.to_ne_bytes(), &$span, 16, false); + // left + $self.update_reference_position(end_position + 4); + $self.$left_converter(&$left_value); + // right + $self.update_reference_position(end_position + 8); + $self.$right_converter(&$right_value); + // body + $self.update_reference_position(end_position + 12); + $self.$body_converter(&$body_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_for_of_statement { + ($self:expr, span => $span:expr, await => $await_value:expr, left => [$left_value:expr, $left_converter:ident], right => [$right_value:expr, $right_converter:ident], body => [$body_value:expr, $body_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start( + &29u32.to_ne_bytes(), + &$span, + 20, + false, + ); + // flags + store_for_of_statement_flags!($self, end_position, await => $await_value); + // left + $self.update_reference_position(end_position + 8); + $self.$left_converter(&$left_value); + // right + $self.update_reference_position(end_position + 12); + $self.$right_converter(&$right_value); + // body + $self.update_reference_position(end_position + 16); + $self.$body_converter(&$body_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_for_statement { + ($self:expr, span => $span:expr, init => [$init_value:expr, $init_converter:ident], test => [$test_value:expr, $test_converter:ident], update => [$update_value:expr, $update_converter:ident], body => [$body_value:expr, $body_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&30u32.to_ne_bytes(), &$span, 20, false); + // init + if let Some(value) = $init_value.as_ref() { + $self.update_reference_position(end_position + 4); + $self.$init_converter(value); + } + // test + if let Some(value) = $test_value.as_ref() { + $self.update_reference_position(end_position + 8); + $self.$test_converter(value); + } + // update + if let Some(value) = $update_value.as_ref() { + $self.update_reference_position(end_position + 12); + $self.$update_converter(value); + } + // body + $self.update_reference_position(end_position + 16); + $self.$body_converter(&$body_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_if_statement { + ($self:expr, span => $span:expr, test => [$test_value:expr, $test_converter:ident], consequent => [$consequent_value:expr, $consequent_converter:ident], alternate => [$alternate_value:expr, $alternate_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&34u32.to_ne_bytes(), &$span, 16, false); + // test + $self.update_reference_position(end_position + 4); + $self.$test_converter(&$test_value); + // consequent + $self.update_reference_position(end_position + 8); + $self.$consequent_converter(&$consequent_value); + // alternate + if let Some(value) = $alternate_value.as_ref() { + $self.update_reference_position(end_position + 12); + $self.$alternate_converter(value); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_import_default_specifier { + ($self:expr, span => $span:expr, local => [$local_value:expr, $local_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&37u32.to_ne_bytes(), &$span, 8, false); + // local + $self.update_reference_position(end_position + 4); + $self.$local_converter(&$local_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_import_namespace_specifier { + ($self:expr, span => $span:expr, local => [$local_value:expr, $local_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&39u32.to_ne_bytes(), &$span, 8, false); + // local + $self.update_reference_position(end_position + 4); + $self.$local_converter(&$local_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_import_specifier { + ($self:expr, span => $span:expr, imported => [$imported_value:expr, $imported_converter:ident], local => [$local_value:expr, $local_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&40u32.to_ne_bytes(), &$span, 12, false); + // imported + if let Some(value) = $imported_value.as_ref() { + $self.update_reference_position(end_position + 4); + $self.$imported_converter(value); + } + // local + $self.update_reference_position(end_position + 8); + $self.$local_converter(&$local_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_labeled_statement { + ($self:expr, span => $span:expr, label => [$label_value:expr, $label_converter:ident], body => [$body_value:expr, $body_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&41u32.to_ne_bytes(), &$span, 12, false); + // label + $self.update_reference_position(end_position + 4); + $self.$label_converter(&$label_value); + // body + $self.update_reference_position(end_position + 8); + $self.$body_converter(&$body_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_literal_big_int { + ($self:expr, span => $span:expr, bigint => $bigint_value:expr, raw => $raw_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&42u32.to_ne_bytes(), &$span, 12, false); + // bigint + $self.convert_string($bigint_value, end_position + 4); + // raw + $self.convert_string($raw_value, end_position + 8); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_literal_boolean { + ($self:expr, span => $span:expr, value => $value_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start( + &43u32.to_ne_bytes(), + &$span, + 8, + false, + ); + // flags + store_literal_boolean_flags!($self, end_position, value => $value_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_literal_null { + ($self:expr, span => $span:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&44u32.to_ne_bytes(), &$span, 4, false); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_literal_number { + ($self:expr, span => $span:expr, raw => $raw_value:expr, value => $value_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&45u32.to_ne_bytes(), &$span, 16, false); + // raw + if let Some(value) = $raw_value.as_ref() { + $self.convert_string(value, end_position + 4); + } + // value + let value_position = end_position + 8; + $self.buffer[value_position..value_position + 8].copy_from_slice(&$value_value.to_le_bytes()); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_literal_reg_exp { + ($self:expr, span => $span:expr, flags => $flags_value:expr, pattern => $pattern_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&46u32.to_ne_bytes(), &$span, 12, false); + // flags + $self.convert_string($flags_value, end_position + 4); + // pattern + $self.convert_string($pattern_value, end_position + 8); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_literal_string { + ($self:expr, span => $span:expr, value => $value_value:expr, raw => $raw_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&47u32.to_ne_bytes(), &$span, 12, false); + // value + $self.convert_string($value_value, end_position + 4); + // raw + if let Some(value) = $raw_value.as_ref() { + $self.convert_string(value, end_position + 8); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_object_expression { + ($self:expr, span => $span:expr, properties => [$properties_value:expr, $properties_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&53u32.to_ne_bytes(), &$span, 8, false); + // properties + $self.convert_item_list( + &$properties_value, + end_position + 4, + |ast_converter, node| { + ast_converter.$properties_converter(node); + true + }, + ); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_object_pattern { + ($self:expr, span => $span:expr, properties => [$properties_value:expr, $properties_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&54u32.to_ne_bytes(), &$span, 8, false); + // properties + $self.convert_item_list( + &$properties_value, + end_position + 4, + |ast_converter, node| { + ast_converter.$properties_converter(node); + true + }, + ); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_private_identifier { + ($self:expr, span => $span:expr, name => $name_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&55u32.to_ne_bytes(), &$span, 8, false); + // name + $self.convert_string($name_value, end_position + 4); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_return_statement { + ($self:expr, span => $span:expr, argument => [$argument_value:expr, $argument_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&60u32.to_ne_bytes(), &$span, 8, false); + // argument + if let Some(value) = $argument_value.as_ref() { + $self.update_reference_position(end_position + 4); + $self.$argument_converter(value); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_sequence_expression { + ($self:expr, span => $span:expr, expressions => [$expressions_value:expr, $expressions_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&61u32.to_ne_bytes(), &$span, 8, false); + // expressions + $self.convert_item_list( + &$expressions_value, + end_position + 4, + |ast_converter, node| { + ast_converter.$expressions_converter(node); + true + }, + ); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_static_block { + ($self:expr, span => $span:expr, body => [$body_value:expr, $body_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&63u32.to_ne_bytes(), &$span, 8, false); + // body + $self.convert_item_list(&$body_value, end_position + 4, |ast_converter, node| { + ast_converter.$body_converter(node); + true + }); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_super_element { + ($self:expr, span => $span:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&64u32.to_ne_bytes(), &$span, 4, false); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_switch_case { + ($self:expr, span => $span:expr, test => [$test_value:expr, $test_converter:ident], consequent => [$consequent_value:expr, $consequent_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&65u32.to_ne_bytes(), &$span, 12, false); + // test + if let Some(value) = $test_value.as_ref() { + $self.update_reference_position(end_position + 4); + $self.$test_converter(value); + } + // consequent + $self.convert_item_list( + &$consequent_value, + end_position + 8, + |ast_converter, node| { + ast_converter.$consequent_converter(node); + true + }, + ); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_switch_statement { + ($self:expr, span => $span:expr, discriminant => [$discriminant_value:expr, $discriminant_converter:ident], cases => [$cases_value:expr, $cases_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&66u32.to_ne_bytes(), &$span, 12, false); + // discriminant + $self.update_reference_position(end_position + 4); + $self.$discriminant_converter(&$discriminant_value); + // cases + $self.convert_item_list(&$cases_value, end_position + 8, |ast_converter, node| { + ast_converter.$cases_converter(node); + true + }); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_tagged_template_expression { + ($self:expr, span => $span:expr, tag => [$tag_value:expr, $tag_converter:ident], quasi => [$quasi_value:expr, $quasi_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&67u32.to_ne_bytes(), &$span, 12, false); + // tag + $self.update_reference_position(end_position + 4); + $self.$tag_converter(&$tag_value); + // quasi + $self.update_reference_position(end_position + 8); + $self.$quasi_converter(&$quasi_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_template_element { + ($self:expr, span => $span:expr, tail => $tail_value:expr, cooked => $cooked_value:expr, raw => $raw_value:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start( + &68u32.to_ne_bytes(), + &$span, + 16, + false, + ); + // flags + store_template_element_flags!($self, end_position, tail => $tail_value); + // cooked + if let Some(value) = $cooked_value.as_ref() { + $self.convert_string(value, end_position + 8); + } + // raw + $self.convert_string($raw_value, end_position + 12); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_this_expression { + ($self:expr, span => $span:expr) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&70u32.to_ne_bytes(), &$span, 4, false); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_throw_statement { + ($self:expr, span => $span:expr, argument => [$argument_value:expr, $argument_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&71u32.to_ne_bytes(), &$span, 8, false); + // argument + $self.update_reference_position(end_position + 4); + $self.$argument_converter(&$argument_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_unary_expression { + ($self:expr, span => $span:expr, operator => $operator_value:expr, argument => [$argument_value:expr, $argument_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&73u32.to_ne_bytes(), &$span, 12, false); + // operator + let operator_position = end_position + 4; + $self.buffer[operator_position..operator_position + 4].copy_from_slice($operator_value); + // argument + $self.update_reference_position(end_position + 8); + $self.$argument_converter(&$argument_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_update_expression { + ($self:expr, span => $span:expr, prefix => $prefix_value:expr, operator => $operator_value:expr, argument => [$argument_value:expr, $argument_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start( + &74u32.to_ne_bytes(), + &$span, + 16, + false, + ); + // flags + store_update_expression_flags!($self, end_position, prefix => $prefix_value); + // operator + let operator_position = end_position + 8; + $self.buffer[operator_position..operator_position + 4].copy_from_slice($operator_value); + // argument + $self.update_reference_position(end_position + 12); + $self.$argument_converter(&$argument_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_while_statement { + ($self:expr, span => $span:expr, test => [$test_value:expr, $test_converter:ident], body => [$body_value:expr, $body_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start(&77u32.to_ne_bytes(), &$span, 12, false); + // test + $self.update_reference_position(end_position + 4); + $self.$test_converter(&$test_value); + // body + $self.update_reference_position(end_position + 8); + $self.$body_converter(&$body_value); + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_yield_expression { + ($self:expr, span => $span:expr, delegate => $delegate_value:expr, argument => [$argument_value:expr, $argument_converter:ident]) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start( + &78u32.to_ne_bytes(), + &$span, + 12, + false, + ); + // flags + store_yield_expression_flags!($self, end_position, delegate => $delegate_value); + // argument + if let Some(value) = $argument_value.as_ref() { + $self.update_reference_position(end_position + 8); + $self.$argument_converter(value); + } + // end + $self.add_end(end_position, &$span); + }; +} + +#[macro_export] +macro_rules! store_arrow_function_expression_flags { + ($self:expr, $end_position:expr, async => $async_value:expr, expression => $expression_value:expr, generator => $generator_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $async_value { + flags |= 1; + } + if $expression_value { + flags |= 2; + } + if $generator_value { + flags |= 4; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_call_expression_flags { + ($self:expr, $end_position:expr, optional => $optional_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $optional_value { + flags |= 1; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_for_of_statement_flags { + ($self:expr, $end_position:expr, await => $await_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $await_value { + flags |= 1; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_function_declaration_flags { + ($self:expr, $end_position:expr, async => $async_value:expr, generator => $generator_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $async_value { + flags |= 1; + } + if $generator_value { + flags |= 2; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_literal_boolean_flags { + ($self:expr, $end_position:expr, value => $value_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $value_value { + flags |= 1; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_member_expression_flags { + ($self:expr, $end_position:expr, computed => $computed_value:expr, optional => $optional_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $computed_value { + flags |= 1; + } + if $optional_value { + flags |= 2; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_method_definition_flags { + ($self:expr, $end_position:expr, static => $static_value:expr, computed => $computed_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $static_value { + flags |= 1; + } + if $computed_value { + flags |= 2; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_property_flags { + ($self:expr, $end_position:expr, method => $method_value:expr, shorthand => $shorthand_value:expr, computed => $computed_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $method_value { + flags |= 1; + } + if $shorthand_value { + flags |= 2; + } + if $computed_value { + flags |= 4; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_property_definition_flags { + ($self:expr, $end_position:expr, static => $static_value:expr, computed => $computed_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $static_value { + flags |= 1; + } + if $computed_value { + flags |= 2; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_template_element_flags { + ($self:expr, $end_position:expr, tail => $tail_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $tail_value { + flags |= 1; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_update_expression_flags { + ($self:expr, $end_position:expr, prefix => $prefix_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $prefix_value { + flags |= 1; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +#[macro_export] +macro_rules! store_yield_expression_flags { + ($self:expr, $end_position:expr, delegate => $delegate_value:expr) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + if $delegate_value { + flags |= 1; + } + let flags_position = $end_position + 4; + $self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); + }; +} diff --git a/scripts/ast-types.js b/scripts/ast-types.js index 8191fa3b1..6a42eb586 100644 --- a/scripts/ast-types.js +++ b/scripts/ast-types.js @@ -45,24 +45,29 @@ * postProcessFields?: Record, // If this is specified, the field will be extracted into a variable and this code is injected after the field is assigned * scopes?: Record // If the field gets a parent scope other than node.scope * scriptedFields?: Record // If fields are parsed via custom logic, $position references the node position + * useMacro?: boolean // Generate a Rust macro instead of separate constants * }} NodeDescription */ /** @type {Record} */ export const AST_NODES = { PanicError: { estreeType: "{ type: 'PanicError', message: string }", - fields: [['message', 'String']] + fields: [['message', 'String']], + useMacro: false }, ParseError: { estreeType: "{ type: 'ParseError', message: string }", - fields: [['message', 'String']] + fields: [['message', 'String']], + useMacro: false }, // eslint-disable-next-line sort-keys ArrayExpression: { - fields: [['elements', 'NodeList']] + fields: [['elements', 'NodeList']], + useMacro: false }, ArrayPattern: { - fields: [['elements', 'NodeList']] + fields: [['elements', 'NodeList']], + useMacro: false }, ArrowFunctionExpression: { fields: [ @@ -91,7 +96,8 @@ export const AST_NODES = { }, scopes: { body: 'scope.bodyScope' - } + }, + useMacro: false }, AssignmentExpression: { fields: [ @@ -107,7 +113,8 @@ export const AST_NODES = { fields: [ ['left', 'Node'], ['right', 'Node'] - ] + ], + useMacro: false }, AwaitExpression: { fields: [['argument', 'Node']] @@ -120,10 +127,12 @@ export const AST_NODES = { ], fieldTypes: { operator: 'estree.BinaryOperator' - } + }, + useMacro: false }, BlockStatement: { - fields: [['body', 'NodeList']] + fields: [['body', 'NodeList']], + useMacro: false }, BreakStatement: { fields: [['label', 'OptionalNode']] @@ -136,6 +145,7 @@ export const AST_NODES = { ['arguments', 'NodeList'] ], flags: ['optional'], + useMacro: false, variableNames: { arguments: 'callArguments' } @@ -150,10 +160,12 @@ export const AST_NODES = { }, scopes: { body: 'scope.bodyScope' - } + }, + useMacro: false }, ChainExpression: { - fields: [['expression', 'Node']] + fields: [['expression', 'Node']], + useMacro: false }, ClassBody: { fields: [['body', 'NodeList']], @@ -175,7 +187,8 @@ export const AST_NODES = { ); } }` - } + }, + useMacro: false }, ClassDeclaration: { fields: [ @@ -185,13 +198,15 @@ export const AST_NODES = { ], scopes: { id: 'scope.parent as ChildScope' - } + }, + useMacro: false }, ClassExpression: { hasSameFieldsAs: 'ClassDeclaration', scopes: { id: 'scope' - } + }, + useMacro: false }, ConditionalExpression: { fields: [ @@ -225,10 +240,12 @@ export const AST_NODES = { ['exported', 'OptionalNode'], ['source', 'Node'], ['attributes', 'NodeList'] - ] + ], + useMacro: false }, ExportDefaultDeclaration: { - fields: [['declaration', 'Node']] + fields: [['declaration', 'Node']], + useMacro: false }, ExportNamedDeclaration: { estreeType: 'estree.ExportNamedDeclaration & { attributes: ImportAttributeNode[] }', @@ -237,7 +254,8 @@ export const AST_NODES = { ['source', 'OptionalNode'], ['attributes', 'NodeList'], ['declaration', 'OptionalNode'] - ] + ], + useMacro: false }, ExportSpecifier: { fields: [ @@ -306,16 +324,19 @@ export const AST_NODES = { scopes: { body: 'scope.bodyScope', id: 'scope.parent as ChildScope' - } + }, + useMacro: false }, FunctionExpression: { hasSameFieldsAs: 'FunctionDeclaration', scopes: { id: 'node.idScope' - } + }, + useMacro: false }, Identifier: { - fields: [['name', 'String']] + fields: [['name', 'String']], + useMacro: false }, IfStatement: { fields: [ @@ -334,7 +355,8 @@ export const AST_NODES = { fields: [ ['key', 'Node'], ['value', 'Node'] - ] + ], + useMacro: false }, ImportDeclaration: { estreeType: 'estree.ImportDeclaration & { attributes: ImportAttributeNode[] }', @@ -342,7 +364,8 @@ export const AST_NODES = { ['specifiers', 'NodeList'], ['source', 'Node'], ['attributes', 'NodeList'] - ] + ], + useMacro: false }, ImportDefaultSpecifier: { fields: [['local', 'Node']] @@ -356,7 +379,8 @@ export const AST_NODES = { scriptedFields: { source: `node.source = convertNode(node, scope, $position, buffer, readString); node.sourceAstNode = convertJsonNode($position, buffer, readString);` - } + }, + useMacro: false }, ImportNamespaceSpecifier: { fields: [['local', 'Node']] @@ -442,20 +466,23 @@ export const AST_NODES = { fieldTypes: { operator: 'estree.LogicalOperator' }, - hasSameFieldsAs: 'BinaryExpression' + hasSameFieldsAs: 'BinaryExpression', + useMacro: false }, MemberExpression: { fields: [ ['object', 'Node'], ['property', 'Node'] ], - flags: ['computed', 'optional'] + flags: ['computed', 'optional'], + useMacro: false }, MetaProperty: { fields: [ ['meta', 'Node'], ['property', 'Node'] - ] + ], + useMacro: false }, MethodDefinition: { fields: [ @@ -468,6 +495,7 @@ export const AST_NODES = { }, // "static" needs to come first as ClassBody depends on it flags: ['static', 'computed'], + useMacro: false, variableNames: { static: 'isStatic' } @@ -478,6 +506,7 @@ export const AST_NODES = { ['callee', 'Node'], ['arguments', 'NodeList'] ], + useMacro: false, variableNames: { arguments: 'callArguments' } @@ -498,7 +527,8 @@ export const AST_NODES = { ], fixed: { sourceType: 'module' - } + }, + useMacro: false }, Property: { fields: [ @@ -510,7 +540,8 @@ export const AST_NODES = { flags: ['method', 'shorthand', 'computed'], optionalFallback: { key: 'value' - } + }, + useMacro: false }, PropertyDefinition: { fields: [ @@ -519,12 +550,14 @@ export const AST_NODES = { ], // "static" needs to come first as ClassBody depends on it flags: ['static', 'computed'], + useMacro: false, variableNames: { static: 'isStatic' } }, RestElement: { - fields: [['argument', 'Node']] + fields: [['argument', 'Node']], + useMacro: false }, ReturnStatement: { fields: [['argument', 'OptionalNode']] @@ -533,7 +566,8 @@ export const AST_NODES = { fields: [['expressions', 'NodeList']] }, SpreadElement: { - fields: [['argument', 'Node']] + fields: [['argument', 'Node']], + useMacro: false }, StaticBlock: { fields: [['body', 'NodeList']] @@ -579,7 +613,8 @@ export const AST_NODES = { fields: [ ['quasis', 'NodeList'], ['expressions', 'NodeList'] - ] + ], + useMacro: false }, ThisExpression: {}, ThrowStatement: { @@ -590,7 +625,8 @@ export const AST_NODES = { ['block', 'Node'], ['handler', 'OptionalNode'], ['finalizer', 'OptionalNode'] - ] + ], + useMacro: false }, UnaryExpression: { fields: [ @@ -621,13 +657,15 @@ export const AST_NODES = { ], fieldTypes: { kind: "estree.VariableDeclaration['kind']" - } + }, + useMacro: false }, VariableDeclarator: { fields: [ ['id', 'Node'], ['init', 'OptionalNode'] - ] + ], + useMacro: false }, WhileStatement: { fields: [ diff --git a/scripts/generate-ast-converters.js b/scripts/generate-ast-converters.js index 75b6c455f..b3fc7e704 100644 --- a/scripts/generate-ast-converters.js +++ b/scripts/generate-ast-converters.js @@ -6,4 +6,5 @@ import './generate-child-node-keys.js'; import './generate-node-index.js'; import './generate-node-types.js'; import './generate-rust-constants.js'; +import './generate-ast-macros.js'; import './generate-string-constants.js'; diff --git a/scripts/generate-ast-macros.js b/scripts/generate-ast-macros.js new file mode 100644 index 000000000..1407be46a --- /dev/null +++ b/scripts/generate-ast-macros.js @@ -0,0 +1,151 @@ +import { writeFile } from 'node:fs/promises'; +import { astNodeNamesWithFieldOrder } from './ast-types.js'; +import { generateNotEditFilesComment, lintRustFile, toSnakeCase } from './helpers.js'; + +const BYTES_PER_U32 = 4; + +const notEditFilesComment = generateNotEditFilesComment(import.meta.url); + +const astMacrosFile = new URL( + '../rust/parse_ast/src/convert_ast/converter/ast_macros.rs', + import.meta.url +); + +const astMacros = astNodeNamesWithFieldOrder + .map(({ name, fields, node: { flags, useMacro } }, nodeIndex) => { + if (useMacro === false) { + return ''; + } + let reservedBytes = BYTES_PER_U32; + let valuesInput = ''; + let flagConverter = ''; + if (flags?.length) { + reservedBytes += BYTES_PER_U32; + let flagsInput = ''; + for (const flag of flags) { + valuesInput += `, ${flag} => $${flag}_value:expr`; + flagsInput += `, ${flag} => $${flag}_value`; + } + flagConverter = ` + // flags + store_${toSnakeCase(name)}_flags!($self, end_position${flagsInput});`; + } + let fieldConverters = ''; + for (const [fieldName, fieldType] of fields) { + fieldConverters += ` + // ${fieldName}`; + switch (fieldType) { + case 'FixedString': { + valuesInput += `, ${fieldName} => $${fieldName}_value:expr`; + fieldConverters += ` + let ${fieldName}_position = end_position + ${reservedBytes}; + $self.buffer[${fieldName}_position..${fieldName}_position + ${BYTES_PER_U32}].copy_from_slice($${fieldName}_value);`; + break; + } + case 'Float': { + valuesInput += `, ${fieldName} => $${fieldName}_value:expr`; + fieldConverters += ` + let ${fieldName}_position = end_position + ${reservedBytes}; + $self.buffer[${fieldName}_position..${fieldName}_position + ${2 * BYTES_PER_U32}].copy_from_slice(&$${fieldName}_value.to_le_bytes());`; + reservedBytes += BYTES_PER_U32; + break; + } + case 'Node': { + valuesInput += `, ${fieldName} => [$${fieldName}_value:expr, $${fieldName}_converter:ident]`; + fieldConverters += ` + $self.update_reference_position(end_position + ${reservedBytes}); + $self.$${fieldName}_converter(&$${fieldName}_value);`; + break; + } + case 'NodeList': { + valuesInput += `, ${fieldName} => [$${fieldName}_value:expr, $${fieldName}_converter:ident]`; + fieldConverters += ` + $self.convert_item_list( + &$${fieldName}_value, + end_position + ${reservedBytes}, + |ast_converter, node| { + ast_converter.$${fieldName}_converter(node); + true + } + );`; + break; + } + case 'OptionalNode': { + valuesInput += `, ${fieldName} => [$${fieldName}_value:expr, $${fieldName}_converter:ident]`; + fieldConverters += ` + if let Some(value) = $${fieldName}_value.as_ref() { + $self.update_reference_position(end_position + ${reservedBytes}); + $self.$${fieldName}_converter(value); + }`; + break; + } + case 'OptionalString': { + valuesInput += `, ${fieldName} => $${fieldName}_value:expr`; + fieldConverters += ` + if let Some(value) = $${fieldName}_value.as_ref() { + $self.convert_string(value, end_position + ${reservedBytes}); + }`; + break; + } + case 'String': { + valuesInput += `, ${fieldName} => $${fieldName}_value:expr`; + fieldConverters += ` + $self.convert_string($${fieldName}_value, end_position + ${reservedBytes});`; + break; + } + default: { + throw new Error(`Unhandled field type ${fieldType}`); + } + } + reservedBytes += BYTES_PER_U32; + } + return `#[macro_export] +macro_rules! store_${toSnakeCase(name)} { + ($self:expr, span => $span:expr${valuesInput}) => { + let _: &mut AstConverter = $self; + let end_position = $self.add_type_and_start( + &${nodeIndex}u32.to_ne_bytes(), + &$span, + ${reservedBytes}, + false, + );${flagConverter}${fieldConverters} + // end + $self.add_end(end_position, &$span); + }; +} + +`; + }) + .join(''); + +const flagMacros = astNodeNamesWithFieldOrder + .map(({ name, originalNode: { flags, hasSameFieldsAs } }) => { + if (hasSameFieldsAs || !flags?.length) { + return ''; + } + const valuesInput = flags.map(flag => `${flag} => $${flag}_value:expr`).join(', '); + const setFlags = flags + .map((flag, index) => `if $${flag}_value { flags |= ${1 << index}; }`) + .join('\n'); + return `#[macro_export] +macro_rules! store_${toSnakeCase(name)}_flags { + ($self:expr, $end_position:expr, ${valuesInput}) => { + let _: &mut AstConverter = $self; + let _: usize = $end_position; + let mut flags = 0u32; + ${setFlags} + let flags_position = $end_position + ${BYTES_PER_U32}; + $self.buffer[flags_position..flags_position + ${BYTES_PER_U32}].copy_from_slice(&flags.to_ne_bytes()); + }; +} + +`; + }) + .join(''); + +const astConstants = `${notEditFilesComment} +${astMacros} +${flagMacros}`; + +await writeFile(astMacrosFile, astConstants); +await lintRustFile(astMacrosFile); diff --git a/scripts/generate-rust-constants.js b/scripts/generate-rust-constants.js index ae74a631e..5c99fffe6 100644 --- a/scripts/generate-rust-constants.js +++ b/scripts/generate-rust-constants.js @@ -11,21 +11,18 @@ const astConstantsFile = new URL( import.meta.url ); -// Nodes with suffix _SIMPLE have at most one variable child that does not need -// an indirect reference (e.g. one non-optional AST Node or a list of AST Nodes) -// and can use "add_type_and_start_simple" - const nodeTypes = astNodeNamesWithFieldOrder - .map( - ({ name }, index) => - `pub const TYPE_${toScreamingSnakeCase(name)}: [u8; 4] = ${index}u32.to_ne_bytes();` + .map(({ name, node: { useMacro } }, index) => + useMacro === false + ? `pub const TYPE_${toScreamingSnakeCase(name)}: [u8; 4] = ${index}u32.to_ne_bytes();\n` + : '' ) - .join('\n'); + .join(''); const reservedBytesAndOffsets = astNodeNamesWithFieldOrder - .map(({ name, fields }) => { + .map(({ name, fields, node: { useMacro } }) => { const { flags, hasSameFieldsAs } = AST_NODES[name]; - if (hasSameFieldsAs) { + if (hasSameFieldsAs || useMacro !== false) { return ''; } /** @type {string[]} */ @@ -36,15 +33,7 @@ const reservedBytesAndOffsets = astNodeNamesWithFieldOrder // - non-inlined fields let reservedBytes = BYTES_PER_U32; if (flags) { - lines.push(`pub const ${toScreamingSnakeCase(name)}_FLAGS_OFFSET: usize = ${reservedBytes};`); reservedBytes += BYTES_PER_U32; - for (const [index, flag] of flags.entries()) { - lines.push( - `pub const ${toScreamingSnakeCase(name)}_${toScreamingSnakeCase(flag)}_FLAG: u32 = ${ - 1 << index - };` - ); - } } for (const [fieldName, fieldType] of fields) { lines.push( diff --git a/scripts/helpers.js b/scripts/helpers.js index aa9143dbf..4953bd128 100644 --- a/scripts/helpers.js +++ b/scripts/helpers.js @@ -106,6 +106,14 @@ export function firstLetterLowercase(string) { return string[0].toLowerCase() + string.slice(1); } +/** + * @param {string} string + * @returns {string} + */ +export function toSnakeCase(string) { + return string.replace(/(?