From 988b0108fbc0caac2758e410edb8b5fcf6a62aad Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 18 Aug 2023 15:09:38 -0400 Subject: [PATCH] [ruby/yarp] Consolidate OperatorAndWrite and OperatorOrWrite nodes https://github.com/ruby/yarp/commit/9e680a7598 --- test/yarp/location_test.rb | 41 ++++++++++ yarp/config.yml | 111 +++++++++++++++++++++++++++ yarp/yarp.c | 152 +++++++++++++++++++++++++++++++++++++ 3 files changed, 304 insertions(+) diff --git a/test/yarp/location_test.rb b/test/yarp/location_test.rb index 703c8e24f0..3902b2036c 100644 --- a/test/yarp/location_test.rb +++ b/test/yarp/location_test.rb @@ -209,6 +209,13 @@ module YARP assert_location(ClassNode, "class Foo < Bar end") end +<<<<<<< HEAD:test/yarp/location_test.rb +======= + def test_ClassVariableOperatorWriteNode + assert_location(ClassVariableOperatorWriteNode, "@@foo += bar") + end + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):test/location_test.rb def test_ClassVariableReadNode assert_location(ClassVariableReadNode, "@@foo") end @@ -229,6 +236,17 @@ module YARP assert_location(ConstantPathWriteNode, "::Foo::Bar = baz") end +<<<<<<< HEAD:test/yarp/location_test.rb +======= + def test_ConstantPathOperatorWriteNode + assert_location(ConstantPathOperatorWriteNode, "Parent::Child += bar") + end + + def test_ConstantOperatorWriteNode + assert_location(ConstantOperatorWriteNode, "Foo += bar") + end + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):test/location_test.rb def test_ConstantReadNode assert_location(ConstantReadNode, "Foo") assert_location(ConstantReadNode, "Foo::Bar", 5...8, &:child) @@ -302,6 +320,13 @@ module YARP assert_location(ForwardingSuperNode, "super {}") end +<<<<<<< HEAD:test/yarp/location_test.rb +======= + def test_GlobalVariableOperatorWriteNode + assert_location(GlobalVariableOperatorWriteNode, "$foo += bar") + end + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):test/location_test.rb def test_GlobalVariableReadNode assert_location(GlobalVariableReadNode, "$foo") end @@ -336,6 +361,13 @@ module YARP end end +<<<<<<< HEAD:test/yarp/location_test.rb +======= + def test_InstanceVariableOperatorWriteNode + assert_location(InstanceVariableOperatorWriteNode, "@foo += bar") + end + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):test/location_test.rb def test_InstanceVariableReadNode assert_location(InstanceVariableReadNode, "@foo") end @@ -402,6 +434,15 @@ module YARP assert_location(LambdaNode, "-> do foo end") end +<<<<<<< HEAD:test/yarp/location_test.rb +======= + def test_LocalVariableOperatorWriteNode + assert_location(LocalVariableOperatorWriteNode, "foo += bar") + assert_location(LocalVariableOperatorWriteNode, "foo = 1; foo += bar", 9...19) + end + + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):test/location_test.rb def test_LocalVariableReadNode assert_location(LocalVariableReadNode, "foo = 1; foo", 9...12) end diff --git a/yarp/config.yml b/yarp/config.yml index 8315b7af75..8e4518e70c 100644 --- a/yarp/config.yml +++ b/yarp/config.yml @@ -736,6 +736,24 @@ nodes: class Foo end ^^^^^^^^^^^^^ +<<<<<<< HEAD:yarp/config.yml +======= + - name: ClassVariableOperatorWriteNode + child_nodes: + - name: name_loc + type: location + - name: operator_loc + type: location + - name: value + type: node + - name: operator + type: constant + comment: | + Represents assigning to a class variable using an operator that isn't `=`. + + @@target += value + ^^^^^^^^^^^^^^^^^ +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):config.yml - name: ClassVariableReadNode comment: | Represents referencing a class variable. @@ -755,6 +773,24 @@ nodes: @@foo = 1 ^^^^^^^^^ +<<<<<<< HEAD:yarp/config.yml +======= + - name: ConstantOperatorWriteNode + child_nodes: + - name: name_loc + type: location + - name: operator_loc + type: location + - name: value + type: node + - name: operator + type: constant + comment: | + Represents assigning to a constant using an operator that isn't `=`. + + Target += value + ^^^^^^^^^^^^^^^ +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):config.yml - name: ConstantPathNode child_nodes: - name: parent @@ -768,6 +804,25 @@ nodes: Foo::Bar ^^^^^^^^ +<<<<<<< HEAD:yarp/config.yml +======= + - name: ConstantPathOperatorWriteNode + child_nodes: + - name: target + type: node + kind: ConstantPathNode + - name: operator_loc + type: location + - name: value + type: node + - name: operator + type: constant + comment: | + Represents assigning to a constant path using an operator that isn't `=`. + + Parent::Child += value + ^^^^^^^^^^^^^^^^^^^^^^ +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):config.yml - name: ConstantPathWriteNode child_nodes: - name: target @@ -1010,6 +1065,24 @@ nodes: super ^^^^^ +<<<<<<< HEAD:yarp/config.yml +======= + - name: GlobalVariableOperatorWriteNode + child_nodes: + - name: name_loc + type: location + - name: operator_loc + type: location + - name: value + type: node + - name: operator + type: constant + comment: | + Represents assigning to a global variable using an operator that isn't `=`. + + $target += value + ^^^^^^^^^^^^^^^^ +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):config.yml - name: GlobalVariableReadNode comment: | Represents referencing a global variable. @@ -1109,6 +1182,24 @@ nodes: case a; in b then c end ^^^^^^^^^^^ +<<<<<<< HEAD:yarp/config.yml +======= + - name: InstanceVariableOperatorWriteNode + child_nodes: + - name: name_loc + type: location + - name: operator_loc + type: location + - name: value + type: node + - name: operator + type: constant + comment: | + Represents assigning to an instance variable using an operator that isn't `=`. + + @target += value + ^^^^^^^^^^^^^^^^ +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):config.yml - name: InstanceVariableReadNode comment: | Represents referencing an instance variable. @@ -1246,6 +1337,26 @@ nodes: ->(value) { value * 2 } ^^^^^^^^^^^^^^^^^^^^^^^ +<<<<<<< HEAD:yarp/config.yml +======= + - name: LocalVariableOperatorWriteNode + child_nodes: + - name: name_loc + type: location + - name: operator_loc + type: location + - name: value + type: node + - name: constant_id + type: constant + - name: operator_id + type: constant + comment: | + Represents assigning to a local variable using an operator that isn't `=`. + + target += value + ^^^^^^^^^^^^^^^ +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):config.yml - name: LocalVariableReadNode child_nodes: - name: constant_id diff --git a/yarp/yarp.c b/yarp/yarp.c index 419d3ddada..a4e7697876 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -1506,6 +1506,31 @@ yp_class_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const y return node; } +<<<<<<< HEAD:yarp/yarp.c +======= +// Allocate and initialize a new ClassVariableOperatorWriteNode node. +static yp_class_variable_operator_write_node_t * +yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { + yp_class_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_operator_write_node_t); + + *node = (yp_class_variable_operator_write_node_t) { + { + .type = YP_NODE_CLASS_VARIABLE_OPERATOR_WRITE_NODE, + .location = { + .start = target->location.start, + .end = value->location.end + } + }, + .name_loc = target->location, + .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), + .value = value, + .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) + }; + + return node; +} + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):src/yarp.c // Allocate and initialize a new ClassVariableReadNode node. static yp_class_variable_read_node_t * yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) { @@ -1536,6 +1561,31 @@ yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp return node; } +<<<<<<< HEAD:yarp/yarp.c +======= +// Allocate and initialize a new ConstantPathOperatorWriteNode node. +static yp_constant_path_operator_write_node_t * +yp_constant_path_operator_write_node_create(yp_parser_t *parser, yp_constant_path_node_t *target, const yp_token_t *operator, yp_node_t *value) { + yp_constant_path_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_constant_path_operator_write_node_t); + + *node = (yp_constant_path_operator_write_node_t) { + { + .type = YP_NODE_CONSTANT_PATH_OPERATOR_WRITE_NODE, + .location = { + .start = target->base.location.start, + .end = value->location.end + } + }, + .target = target, + .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), + .value = value, + .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) + }; + + return node; +} + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):src/yarp.c // Allocate and initialize a new ConstantPathNode node. static yp_constant_path_node_t * yp_constant_path_node_create(yp_parser_t *parser, yp_node_t *parent, const yp_token_t *delimiter, yp_node_t *child) { @@ -1578,6 +1628,31 @@ yp_constant_path_write_node_create(yp_parser_t *parser, yp_constant_path_node_t return node; } +<<<<<<< HEAD:yarp/yarp.c +======= +// Allocate and initialize a new ConstantOperatorWriteNode node. +static yp_constant_operator_write_node_t * +yp_constant_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { + yp_constant_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_constant_operator_write_node_t); + + *node = (yp_constant_operator_write_node_t) { + { + .type = YP_NODE_CONSTANT_OPERATOR_WRITE_NODE, + .location = { + .start = target->location.start, + .end = value->location.end + } + }, + .name_loc = target->location, + .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), + .value = value, + .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) + }; + + return node; +} + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):src/yarp.c // Allocate and initialize a new ConstantReadNode node. static yp_constant_read_node_t * yp_constant_read_node_create(yp_parser_t *parser, const yp_token_t *name) { @@ -2012,6 +2087,31 @@ yp_hash_pattern_node_node_list_create(yp_parser_t *parser, yp_node_list_t *assoc return node; } +<<<<<<< HEAD:yarp/yarp.c +======= +// Allocate and initialize a new GlobalVariableOperatorWriteNode node. +static yp_global_variable_operator_write_node_t * +yp_global_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { + yp_global_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_global_variable_operator_write_node_t); + + *node = (yp_global_variable_operator_write_node_t) { + { + .type = YP_NODE_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE, + .location = { + .start = target->location.start, + .end = value->location.end + } + }, + .name_loc = target->location, + .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), + .value = value, + .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) + }; + + return node; +} + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):src/yarp.c // Allocate a new GlobalVariableReadNode node. static yp_global_variable_read_node_t * yp_global_variable_read_node_create(yp_parser_t *parser, const yp_token_t *name) { @@ -2302,6 +2402,31 @@ yp_in_node_create(yp_parser_t *parser, yp_node_t *pattern, yp_statements_node_t return node; } +<<<<<<< HEAD:yarp/yarp.c +======= +// Allocate and initialize a new InstanceVariableOperatorWriteNode node. +static yp_instance_variable_operator_write_node_t * +yp_instance_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { + yp_instance_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_instance_variable_operator_write_node_t); + + *node = (yp_instance_variable_operator_write_node_t) { + { + .type = YP_NODE_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE, + .location = { + .start = target->location.start, + .end = value->location.end + } + }, + .name_loc = target->location, + .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), + .value = value, + .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) + }; + + return node; +} + +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):src/yarp.c // Allocate and initialize a new InstanceVariableReadNode node. static yp_instance_variable_read_node_t * yp_instance_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) { @@ -2569,7 +2694,34 @@ yp_lambda_node_create( .locals = *locals, .opening_loc = YP_LOCATION_TOKEN_VALUE(opening), .parameters = parameters, +<<<<<<< HEAD:yarp/yarp.c .body = body +======= + .statements = statements + }; + + return node; +} + +// Allocate and initialize a new LocalVariableOperatorWriteNode node. +static yp_local_variable_operator_write_node_t * +yp_local_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t constant_id) { + yp_local_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_operator_write_node_t); + + *node = (yp_local_variable_operator_write_node_t) { + { + .type = YP_NODE_LOCAL_VARIABLE_OPERATOR_WRITE_NODE, + .location = { + .start = target->location.start, + .end = value->location.end + } + }, + .name_loc = target->location, + .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), + .value = value, + .constant_id = constant_id, + .operator_id = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) +>>>>>>> 9e680a7598 (Consolidate OperatorAndWrite and OperatorOrWrite nodes):src/yarp.c }; return node;