diff options
author | Kevin Newton <kddnewton@gmail.com> | 2023-09-05 13:53:37 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-09-06 13:00:23 +0000 |
commit | 4c9a036606afff049b1184b7a7c6d9d8a3a50307 (patch) | |
tree | 6a0d1023e0bb34375222bb44dc72468a36374f71 /yarp | |
parent | 6778d2c582d8f17b81b9a8894bd3b2c152050bd3 (diff) |
[ruby/yarp] Add constants and constants
https://github.com/ruby/yarp/commit/d7eaa89bc3
Diffstat (limited to 'yarp')
-rw-r--r-- | yarp/config.yml | 14 | ||||
-rw-r--r-- | yarp/yarp.c | 57 |
2 files changed, 47 insertions, 24 deletions
diff --git a/yarp/config.yml b/yarp/config.yml index a53b7aaa86..a8841bed4c 100644 --- a/yarp/config.yml +++ b/yarp/config.yml @@ -867,6 +867,8 @@ nodes: ^^^^^^^^^ - name: ConstantAndWriteNode fields: + - name: name + type: constant - name: name_loc type: location - name: operator_loc @@ -880,6 +882,8 @@ nodes: ^^^^^^^^^^^^^^^^ - name: ConstantOperatorWriteNode fields: + - name: name + type: constant - name: name_loc type: location - name: operator_loc @@ -895,6 +899,8 @@ nodes: ^^^^^^^^^^^^^^^ - name: ConstantOrWriteNode fields: + - name: name + type: constant - name: name_loc type: location - name: operator_loc @@ -997,12 +1003,18 @@ nodes: ::Foo::Bar = 1 ^^^^^^^^^^^^^^ - name: ConstantReadNode + fields: + - name: name + type: constant comment: | Represents referencing a constant. Foo ^^^ - name: ConstantTargetNode + fields: + - name: name + type: constant comment: | Represents writing to a constant in a context that doesn't have an explicit value. @@ -1010,6 +1022,8 @@ nodes: ^^^ ^^^ - name: ConstantWriteNode fields: + - name: name + type: constant - name: name_loc type: location - name: value diff --git a/yarp/yarp.c b/yarp/yarp.c index 8079bb55f6..df1cec913c 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -1747,7 +1747,7 @@ yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) .type = YP_CLASS_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }, - .name = yp_parser_constant_id_location(parser, token->start, token->end) + .name = yp_parser_constant_id_token(parser, token) }; return node; @@ -1885,8 +1885,7 @@ yp_constant_path_write_node_create(yp_parser_t *parser, yp_constant_path_node_t // Allocate and initialize a new ConstantAndWriteNode node. static yp_constant_and_write_node_t * -yp_constant_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { - assert(YP_NODE_TYPE_P(target, YP_CONSTANT_READ_NODE)); +yp_constant_and_write_node_create(yp_parser_t *parser, yp_constant_read_node_t *target, const yp_token_t *operator, yp_node_t *value) { assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL); yp_constant_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_constant_and_write_node_t); @@ -1894,11 +1893,12 @@ yp_constant_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const { .type = YP_CONSTANT_AND_WRITE_NODE, .location = { - .start = target->location.start, + .start = target->base.location.start, .end = value->location.end } }, - .name_loc = target->location, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .value = value }; @@ -1908,18 +1908,19 @@ yp_constant_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const // 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_create(yp_parser_t *parser, yp_constant_read_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_CONSTANT_OPERATOR_WRITE_NODE, .location = { - .start = target->location.start, + .start = target->base.location.start, .end = value->location.end } }, - .name_loc = target->location, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .value = value, .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) @@ -1930,8 +1931,7 @@ yp_constant_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, c // Allocate and initialize a new ConstantOrWriteNode node. static yp_constant_or_write_node_t * -yp_constant_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { - assert(YP_NODE_TYPE_P(target, YP_CONSTANT_READ_NODE)); +yp_constant_or_write_node_create(yp_parser_t *parser, yp_constant_read_node_t *target, const yp_token_t *operator, yp_node_t *value) { assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL); yp_constant_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_constant_or_write_node_t); @@ -1939,11 +1939,12 @@ yp_constant_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const y { .type = YP_CONSTANT_OR_WRITE_NODE, .location = { - .start = target->location.start, + .start = target->base.location.start, .end = value->location.end } }, - .name_loc = target->location, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .value = value }; @@ -1955,26 +1956,34 @@ yp_constant_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const y static yp_constant_read_node_t * yp_constant_read_node_create(yp_parser_t *parser, const yp_token_t *name) { assert(name->type == YP_TOKEN_CONSTANT || name->type == YP_TOKEN_MISSING); - yp_constant_read_node_t *node = YP_ALLOC_NODE(parser, yp_constant_read_node_t); - *node = (yp_constant_read_node_t) {{ .type = YP_CONSTANT_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(name) }}; + + *node = (yp_constant_read_node_t) { + { + .type = YP_CONSTANT_READ_NODE, + .location = YP_LOCATION_TOKEN_VALUE(name) + }, + .name = yp_parser_constant_id_token(parser, name) + }; + return node; } // Allocate a new ConstantWriteNode node. static yp_constant_write_node_t * -yp_constant_write_node_create(yp_parser_t *parser, yp_location_t *name_loc, const yp_token_t *operator, yp_node_t *value) { +yp_constant_write_node_create(yp_parser_t *parser, yp_constant_read_node_t *target, const yp_token_t *operator, yp_node_t *value) { yp_constant_write_node_t *node = YP_ALLOC_NODE(parser, yp_constant_write_node_t); *node = (yp_constant_write_node_t) { { .type = YP_CONSTANT_WRITE_NODE, .location = { - .start = name_loc->start, + .start = target->base.location.start, .end = value->location.end - }, + } }, - .name_loc = *name_loc, + .name = target->name, + .name_loc = target->base.location, .operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator), .value = value }; @@ -2478,7 +2487,7 @@ yp_global_variable_read_node_create(yp_parser_t *parser, const yp_token_t *name) .type = YP_GLOBAL_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(name), }, - .name = yp_parser_constant_id_location(parser, name->start, name->end) + .name = yp_parser_constant_id_token(parser, name) }; return node; @@ -2840,7 +2849,7 @@ yp_instance_variable_read_node_create(yp_parser_t *parser, const yp_token_t *tok .type = YP_INSTANCE_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }, - .name = yp_parser_constant_id_location(parser, token->start, token->end) + .name = yp_parser_constant_id_token(parser, token) }; return node; @@ -8167,7 +8176,7 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod case YP_CONSTANT_PATH_NODE: return (yp_node_t *) yp_constant_path_write_node_create(parser, (yp_constant_path_node_t *) target, operator, value); case YP_CONSTANT_READ_NODE: { - yp_constant_write_node_t *node = yp_constant_write_node_create(parser, &target->location, operator, value); + yp_constant_write_node_t *node = yp_constant_write_node_create(parser, (yp_constant_read_node_t *) target, operator, value); yp_node_destroy(parser, target); return (yp_node_t *) node; } @@ -13005,7 +13014,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t parser_lex(parser); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&="); - yp_node_t *result = (yp_node_t *) yp_constant_and_write_node_create(parser, node, &token, value); + yp_node_t *result = (yp_node_t *) yp_constant_and_write_node_create(parser, (yp_constant_read_node_t *) node, &token, value); yp_node_destroy(parser, node); return result; @@ -13106,7 +13115,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t parser_lex(parser); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||="); - yp_node_t *result = (yp_node_t *) yp_constant_or_write_node_create(parser, node, &token, value); + yp_node_t *result = (yp_node_t *) yp_constant_or_write_node_create(parser, (yp_constant_read_node_t *) node, &token, value); yp_node_destroy(parser, node); return result; @@ -13217,7 +13226,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t parser_lex(parser); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator."); - yp_node_t *result = (yp_node_t *) yp_constant_operator_write_node_create(parser, node, &token, value); + yp_node_t *result = (yp_node_t *) yp_constant_operator_write_node_create(parser, (yp_constant_read_node_t *) node, &token, value); yp_node_destroy(parser, node); return result; |