Into AliasGlobalVariableNode and AliasMethodNode. These have different
enough semantics that we feel comfortable splitting them up.

https://github.com/ruby/yarp/commit/c1f3e6d344
This commit is contained in:
Kevin Newton 2023-09-11 11:52:01 -04:00 коммит произвёл git
Родитель 39ee3e22bd
Коммит a98209b8a7
11 изменённых файлов: 86 добавлений и 44 удалений

Просмотреть файл

@ -4,8 +4,12 @@ require_relative "test_helper"
module YARP module YARP
class LocationTest < TestCase class LocationTest < TestCase
def test_AliasNode def test_AliasGlobalVariableNode
assert_location(AliasNode, "alias foo bar") assert_location(AliasGlobalVariableNode, "alias $foo $bar")
end
def test_AliasMethodNode
assert_location(AliasMethodNode, "alias foo bar")
end end
def test_AlternationPatternNode def test_AlternationPatternNode

Просмотреть файл

@ -3,7 +3,7 @@
└── statements: └── statements:
@ StatementsNode (location: (0...199)) @ StatementsNode (location: (0...199))
└── body: (length: 12) └── body: (length: 12)
├── @ AliasNode (location: (0...15)) ├── @ AliasMethodNode (location: (0...15))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (6...10)) │ │ @ SymbolNode (location: (6...10))
│ │ ├── opening_loc: (6...7) = ":" │ │ ├── opening_loc: (6...7) = ":"
@ -17,7 +17,7 @@
│ │ ├── closing_loc: ∅ │ │ ├── closing_loc: ∅
│ │ └── unescaped: "bar" │ │ └── unescaped: "bar"
│ └── keyword_loc: (0...5) = "alias" │ └── keyword_loc: (0...5) = "alias"
├── @ AliasNode (location: (17...38)) ├── @ AliasMethodNode (location: (17...38))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (23...30)) │ │ @ SymbolNode (location: (23...30))
│ │ ├── opening_loc: (23...26) = "%s[" │ │ ├── opening_loc: (23...26) = "%s["
@ -31,7 +31,7 @@
│ │ ├── closing_loc: (37...38) = "]" │ │ ├── closing_loc: (37...38) = "]"
│ │ └── unescaped: "def" │ │ └── unescaped: "def"
│ └── keyword_loc: (17...22) = "alias" │ └── keyword_loc: (17...22) = "alias"
├── @ AliasNode (location: (40...59)) ├── @ AliasMethodNode (location: (40...59))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (46...52)) │ │ @ SymbolNode (location: (46...52))
│ │ ├── opening_loc: (46...48) = ":'" │ │ ├── opening_loc: (46...48) = ":'"
@ -45,7 +45,7 @@
│ │ ├── closing_loc: (58...59) = "'" │ │ ├── closing_loc: (58...59) = "'"
│ │ └── unescaped: "def" │ │ └── unescaped: "def"
│ └── keyword_loc: (40...45) = "alias" │ └── keyword_loc: (40...45) = "alias"
├── @ AliasNode (location: (61...84)) ├── @ AliasMethodNode (location: (61...84))
│ ├── new_name: │ ├── new_name:
│ │ @ InterpolatedSymbolNode (location: (67...77)) │ │ @ InterpolatedSymbolNode (location: (67...77))
│ │ ├── opening_loc: (67...69) = ":\"" │ │ ├── opening_loc: (67...69) = ":\""
@ -71,14 +71,14 @@
│ │ ├── closing_loc: (83...84) = "'" │ │ ├── closing_loc: (83...84) = "'"
│ │ └── unescaped: "def" │ │ └── unescaped: "def"
│ └── keyword_loc: (61...66) = "alias" │ └── keyword_loc: (61...66) = "alias"
├── @ AliasNode (location: (86...97)) ├── @ AliasGlobalVariableNode (location: (86...97))
│ ├── new_name: │ ├── new_name:
│ │ @ GlobalVariableReadNode (location: (92...94)) │ │ @ GlobalVariableReadNode (location: (92...94))
│ │ └── name: :$a │ │ └── name: :$a
│ ├── old_name: │ ├── old_name:
│ │ @ BackReferenceReadNode (location: (95...97)) │ │ @ BackReferenceReadNode (location: (95...97))
│ └── keyword_loc: (86...91) = "alias" │ └── keyword_loc: (86...91) = "alias"
├── @ AliasNode (location: (99...112)) ├── @ AliasMethodNode (location: (99...112))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (105...108)) │ │ @ SymbolNode (location: (105...108))
│ │ ├── opening_loc: ∅ │ │ ├── opening_loc: ∅
@ -92,7 +92,7 @@
│ │ ├── closing_loc: ∅ │ │ ├── closing_loc: ∅
│ │ └── unescaped: "bar" │ │ └── unescaped: "bar"
│ └── keyword_loc: (99...104) = "alias" │ └── keyword_loc: (99...104) = "alias"
├── @ AliasNode (location: (114...129)) ├── @ AliasGlobalVariableNode (location: (114...129))
│ ├── new_name: │ ├── new_name:
│ │ @ GlobalVariableReadNode (location: (120...124)) │ │ @ GlobalVariableReadNode (location: (120...124))
│ │ └── name: :$foo │ │ └── name: :$foo
@ -100,7 +100,7 @@
│ │ @ GlobalVariableReadNode (location: (125...129)) │ │ @ GlobalVariableReadNode (location: (125...129))
│ │ └── name: :$bar │ │ └── name: :$bar
│ └── keyword_loc: (114...119) = "alias" │ └── keyword_loc: (114...119) = "alias"
├── @ AliasNode (location: (131...143)) ├── @ AliasMethodNode (location: (131...143))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (137...140)) │ │ @ SymbolNode (location: (137...140))
│ │ ├── opening_loc: ∅ │ │ ├── opening_loc: ∅
@ -114,7 +114,7 @@
│ │ ├── closing_loc: ∅ │ │ ├── closing_loc: ∅
│ │ └── unescaped: "if" │ │ └── unescaped: "if"
│ └── keyword_loc: (131...136) = "alias" │ └── keyword_loc: (131...136) = "alias"
├── @ AliasNode (location: (145...158)) ├── @ AliasMethodNode (location: (145...158))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (151...154)) │ │ @ SymbolNode (location: (151...154))
│ │ ├── opening_loc: ∅ │ │ ├── opening_loc: ∅
@ -128,7 +128,7 @@
│ │ ├── closing_loc: ∅ │ │ ├── closing_loc: ∅
│ │ └── unescaped: "<=>" │ │ └── unescaped: "<=>"
│ └── keyword_loc: (145...150) = "alias" │ └── keyword_loc: (145...150) = "alias"
├── @ AliasNode (location: (160...175)) ├── @ AliasMethodNode (location: (160...175))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (166...169)) │ │ @ SymbolNode (location: (166...169))
│ │ ├── opening_loc: (166...167) = ":" │ │ ├── opening_loc: (166...167) = ":"
@ -142,7 +142,7 @@
│ │ ├── closing_loc: ∅ │ │ ├── closing_loc: ∅
│ │ └── unescaped: "eql?" │ │ └── unescaped: "eql?"
│ └── keyword_loc: (160...165) = "alias" │ └── keyword_loc: (160...165) = "alias"
├── @ AliasNode (location: (177...186)) ├── @ AliasMethodNode (location: (177...186))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (183...184)) │ │ @ SymbolNode (location: (183...184))
│ │ ├── opening_loc: ∅ │ │ ├── opening_loc: ∅
@ -156,7 +156,7 @@
│ │ ├── closing_loc: ∅ │ │ ├── closing_loc: ∅
│ │ └── unescaped: "B" │ │ └── unescaped: "B"
│ └── keyword_loc: (177...182) = "alias" │ └── keyword_loc: (177...182) = "alias"
└── @ AliasNode (location: (188...199)) └── @ AliasMethodNode (location: (188...199))
├── new_name: ├── new_name:
│ @ SymbolNode (location: (194...196)) │ @ SymbolNode (location: (194...196))
│ ├── opening_loc: (194...195) = ":" │ ├── opening_loc: (194...195) = ":"

Просмотреть файл

@ -3,7 +3,7 @@
└── statements: └── statements:
@ StatementsNode (location: (0...15)) @ StatementsNode (location: (0...15))
└── body: (length: 1) └── body: (length: 1)
└── @ AliasNode (location: (0...15)) └── @ AliasGlobalVariableNode (location: (0...15))
├── new_name: ├── new_name:
│ @ GlobalVariableReadNode (location: (6...12)) │ @ GlobalVariableReadNode (location: (6...12))
│ └── name: :$MATCH │ └── name: :$MATCH

Просмотреть файл

@ -3,7 +3,7 @@
└── statements: └── statements:
@ StatementsNode (location: (0...12)) @ StatementsNode (location: (0...12))
└── body: (length: 1) └── body: (length: 1)
└── @ AliasNode (location: (0...12)) └── @ AliasMethodNode (location: (0...12))
├── new_name: ├── new_name:
│ @ SymbolNode (location: (6...8)) │ @ SymbolNode (location: (6...8))
│ ├── opening_loc: ∅ │ ├── opening_loc: ∅

Просмотреть файл

@ -3,7 +3,7 @@
└── statements: └── statements:
@ StatementsNode (location: (0...32)) @ StatementsNode (location: (0...32))
└── body: (length: 2) └── body: (length: 2)
├── @ AliasNode (location: (0...17)) ├── @ AliasMethodNode (location: (0...17))
│ ├── new_name: │ ├── new_name:
│ │ @ SymbolNode (location: (6...11)) │ │ @ SymbolNode (location: (6...11))
│ │ ├── opening_loc: (6...8) = ":\"" │ │ ├── opening_loc: (6...8) = ":\""
@ -17,7 +17,7 @@
│ │ ├── closing_loc: (16...17) = "\"" │ │ ├── closing_loc: (16...17) = "\""
│ │ └── unescaped: ">>" │ │ └── unescaped: ">>"
│ └── keyword_loc: (0...5) = "alias" │ └── keyword_loc: (0...5) = "alias"
└── @ AliasNode (location: (19...32)) └── @ AliasMethodNode (location: (19...32))
├── new_name: ├── new_name:
│ @ SymbolNode (location: (25...28)) │ @ SymbolNode (location: (25...28))
│ ├── opening_loc: (25...26) = ":" │ ├── opening_loc: (25...26) = ":"

Просмотреть файл

@ -3,7 +3,7 @@
└── statements: └── statements:
@ StatementsNode (location: (0...31)) @ StatementsNode (location: (0...31))
└── body: (length: 2) └── body: (length: 2)
├── @ AliasNode (location: (0...15)) ├── @ AliasGlobalVariableNode (location: (0...15))
│ ├── new_name: │ ├── new_name:
│ │ @ GlobalVariableReadNode (location: (6...10)) │ │ @ GlobalVariableReadNode (location: (6...10))
│ │ └── name: :$foo │ │ └── name: :$foo
@ -11,7 +11,7 @@
│ │ @ GlobalVariableReadNode (location: (11...15)) │ │ @ GlobalVariableReadNode (location: (11...15))
│ │ └── name: :$bar │ │ └── name: :$bar
│ └── keyword_loc: (0...5) = "alias" │ └── keyword_loc: (0...5) = "alias"
└── @ AliasNode (location: (16...31)) └── @ AliasMethodNode (location: (16...31))
├── new_name: ├── new_name:
│ @ SymbolNode (location: (22...26)) │ @ SymbolNode (location: (22...26))
│ ├── opening_loc: (22...23) = ":" │ ├── opening_loc: (22...23) = ":"

Просмотреть файл

@ -3,7 +3,7 @@
└── statements: └── statements:
@ StatementsNode (location: (0...14)) @ StatementsNode (location: (0...14))
└── body: (length: 1) └── body: (length: 1)
└── @ AliasNode (location: (0...14)) └── @ AliasMethodNode (location: (0...14))
├── new_name: ├── new_name:
│ @ SymbolNode (location: (6...10)) │ @ SymbolNode (location: (6...10))
│ ├── opening_loc: (6...7) = ":" │ ├── opening_loc: (6...7) = ":"

Просмотреть файл

@ -3,14 +3,14 @@
└── statements: └── statements:
@ StatementsNode (location: (0...24)) @ StatementsNode (location: (0...24))
└── body: (length: 2) └── body: (length: 2)
├── @ AliasNode (location: (0...11)) ├── @ AliasGlobalVariableNode (location: (0...11))
│ ├── new_name: │ ├── new_name:
│ │ @ GlobalVariableReadNode (location: (6...8)) │ │ @ GlobalVariableReadNode (location: (6...8))
│ │ └── name: :$a │ │ └── name: :$a
│ ├── old_name: │ ├── old_name:
│ │ @ BackReferenceReadNode (location: (9...11)) │ │ @ BackReferenceReadNode (location: (9...11))
│ └── keyword_loc: (0...5) = "alias" │ └── keyword_loc: (0...5) = "alias"
└── @ AliasNode (location: (13...24)) └── @ AliasGlobalVariableNode (location: (13...24))
├── new_name: ├── new_name:
│ @ GlobalVariableReadNode (location: (19...21)) │ @ GlobalVariableReadNode (location: (19...21))
│ └── name: :$a │ └── name: :$a

Просмотреть файл

@ -370,7 +370,7 @@ flags:
- name: ONCE - name: ONCE
comment: "o - only interpolates values into the regular expression once" comment: "o - only interpolates values into the regular expression once"
nodes: nodes:
- name: AliasNode - name: AliasGlobalVariableNode
fields: fields:
- name: new_name - name: new_name
type: node type: node
@ -379,7 +379,20 @@ nodes:
- name: keyword_loc - name: keyword_loc
type: location type: location
comment: | 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 alias foo bar
^^^^^^^^^^^^^ ^^^^^^^^^^^^^

Просмотреть файл

@ -735,15 +735,37 @@ yp_missing_node_create(yp_parser_t *parser, const uint8_t *start, const uint8_t
return node; return node;
} }
// Allocate and initialize a new alias node. // Allocate and initialize a new AliasGlobalVariableNode node.
static yp_alias_node_t * static yp_alias_global_variable_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) { 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); 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 = { .location = {
.start = keyword->start, .start = keyword->start,
.end = old_name->location.end .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); yp_node_t *old_name = parse_alias_argument(parser, false);
switch (YP_NODE_TYPE(new_name)) { 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_BACK_REFERENCE_READ_NODE:
case YP_NUMBERED_REFERENCE_READ_NODE: case YP_NUMBERED_REFERENCE_READ_NODE:
case YP_GLOBAL_VARIABLE_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 { } else {
yp_diagnostic_list_append(&parser->error_list, old_name->location.start, old_name->location.end, YP_ERR_ALIAS_ARGUMENT); yp_diagnostic_list_append(&parser->error_list, old_name->location.start, old_name->location.end, YP_ERR_ALIAS_ARGUMENT);
} }
break;
}
default:
break;
}
return (yp_node_t *) yp_alias_node_create(parser, &keyword, new_name, old_name); 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:
return (yp_node_t *) yp_alias_method_node_create(parser, &keyword, new_name, old_name);
}
} }
case YP_TOKEN_KEYWORD_CASE: { case YP_TOKEN_KEYWORD_CASE: {
parser_lex(parser); parser_lex(parser);

Просмотреть файл

@ -28,6 +28,10 @@
#include <strings.h> #include <strings.h>
#endif #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_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); void yp_print_node(yp_parser_t *parser, yp_node_t *node);