summaryrefslogtreecommitdiff
path: root/yarp
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-09-11 11:52:01 -0400
committergit <svn-admin@ruby-lang.org>2023-09-12 15:44:25 +0000
commita98209b8a70345714ac5f3028e0591f3ee50bba7 (patch)
treea9eccc1a3c2604c58eeb90e5c300c3d9145b3468 /yarp
parent39ee3e22bd3d071c1c283b6b8dbd1af413342fb1 (diff)
[ruby/yarp] Split AliasNode
Into AliasGlobalVariableNode and AliasMethodNode. These have different enough semantics that we feel comfortable splitting them up. https://github.com/ruby/yarp/commit/c1f3e6d344
Diffstat (limited to 'yarp')
-rw-r--r--yarp/config.yml17
-rw-r--r--yarp/yarp.c55
-rw-r--r--yarp/yarp.h4
3 files changed, 57 insertions, 19 deletions
diff --git a/yarp/config.yml b/yarp/config.yml
index 411c11540a..7210ad7959 100644
--- a/yarp/config.yml
+++ b/yarp/config.yml
@@ -370,7 +370,7 @@ flags:
- name: ONCE
comment: "o - only interpolates values into the regular expression once"
nodes:
- - name: AliasNode
+ - name: AliasGlobalVariableNode
fields:
- name: new_name
type: node
@@ -379,7 +379,20 @@ nodes:
- name: keyword_loc
type: location
comment: |
- Represents the use of the `alias` keyword.
+ Represents the use of the `alias` keyword to alias a global variable.
+
+ alias $foo $bar
+ ^^^^^^^^^^^^^^^
+ - name: AliasMethodNode
+ fields:
+ - name: new_name
+ type: node
+ - name: old_name
+ type: node
+ - name: keyword_loc
+ type: location
+ comment: |
+ Represents the use of the `alias` keyword to alias a method.
alias foo bar
^^^^^^^^^^^^^
diff --git a/yarp/yarp.c b/yarp/yarp.c
index 28d71f69c4..6379ee50c5 100644
--- a/yarp/yarp.c
+++ b/yarp/yarp.c
@@ -735,15 +735,37 @@ yp_missing_node_create(yp_parser_t *parser, const uint8_t *start, const uint8_t
return node;
}
-// Allocate and initialize a new alias node.
-static yp_alias_node_t *
-yp_alias_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *new_name, yp_node_t *old_name) {
+// Allocate and initialize a new AliasGlobalVariableNode node.
+static yp_alias_global_variable_node_t *
+yp_alias_global_variable_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *new_name, yp_node_t *old_name) {
assert(keyword->type == YP_TOKEN_KEYWORD_ALIAS);
- yp_alias_node_t *node = YP_ALLOC_NODE(parser, yp_alias_node_t);
+ yp_alias_global_variable_node_t *node = YP_ALLOC_NODE(parser, yp_alias_global_variable_node_t);
- *node = (yp_alias_node_t) {
+ *node = (yp_alias_global_variable_node_t) {
{
- .type = YP_ALIAS_NODE,
+ .type = YP_ALIAS_GLOBAL_VARIABLE_NODE,
+ .location = {
+ .start = keyword->start,
+ .end = old_name->location.end
+ },
+ },
+ .new_name = new_name,
+ .old_name = old_name,
+ .keyword_loc = YP_LOCATION_TOKEN_VALUE(keyword)
+ };
+
+ return node;
+}
+
+// Allocate and initialize a new AliasMethodNode node.
+static yp_alias_method_node_t *
+yp_alias_method_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *new_name, yp_node_t *old_name) {
+ assert(keyword->type == YP_TOKEN_KEYWORD_ALIAS);
+ yp_alias_method_node_t *node = YP_ALLOC_NODE(parser, yp_alias_method_node_t);
+
+ *node = (yp_alias_method_node_t) {
+ {
+ .type = YP_ALIAS_METHOD_NODE,
.location = {
.start = keyword->start,
.end = old_name->location.end
@@ -11413,13 +11435,6 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
yp_node_t *old_name = parse_alias_argument(parser, false);
switch (YP_NODE_TYPE(new_name)) {
- case YP_SYMBOL_NODE:
- case YP_INTERPOLATED_SYMBOL_NODE: {
- if (!YP_NODE_TYPE_P(old_name, YP_SYMBOL_NODE) && !YP_NODE_TYPE_P(old_name, YP_INTERPOLATED_SYMBOL_NODE)) {
- yp_diagnostic_list_append(&parser->error_list, old_name->location.start, old_name->location.end, YP_ERR_ALIAS_ARGUMENT);
- }
- break;
- }
case YP_BACK_REFERENCE_READ_NODE:
case YP_NUMBERED_REFERENCE_READ_NODE:
case YP_GLOBAL_VARIABLE_READ_NODE: {
@@ -11430,13 +11445,19 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
} else {
yp_diagnostic_list_append(&parser->error_list, old_name->location.start, old_name->location.end, YP_ERR_ALIAS_ARGUMENT);
}
- break;
+
+ return (yp_node_t *) yp_alias_global_variable_node_create(parser, &keyword, new_name, old_name);
}
+ case YP_SYMBOL_NODE:
+ case YP_INTERPOLATED_SYMBOL_NODE: {
+ if (!YP_NODE_TYPE_P(old_name, YP_SYMBOL_NODE) && !YP_NODE_TYPE_P(old_name, YP_INTERPOLATED_SYMBOL_NODE)) {
+ yp_diagnostic_list_append(&parser->error_list, old_name->location.start, old_name->location.end, YP_ERR_ALIAS_ARGUMENT);
+ }
+ }
+ /* fallthrough */
default:
- break;
+ return (yp_node_t *) yp_alias_method_node_create(parser, &keyword, new_name, old_name);
}
-
- return (yp_node_t *) yp_alias_node_create(parser, &keyword, new_name, old_name);
}
case YP_TOKEN_KEYWORD_CASE: {
parser_lex(parser);
diff --git a/yarp/yarp.h b/yarp/yarp.h
index 378efe0c93..6e1db00748 100644
--- a/yarp/yarp.h
+++ b/yarp/yarp.h
@@ -28,6 +28,10 @@
#include <strings.h>
#endif
+// These aliases are for migration purposes.
+#define YP_ALIAS_NODE YP_ALIAS_METHOD_NODE
+#define yp_alias_node_t yp_alias_method_node_t
+
void yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer);
void yp_print_node(yp_parser_t *parser, yp_node_t *node);