summaryrefslogtreecommitdiff
path: root/prism
diff options
context:
space:
mode:
authorTSUYUSATO Kitsune <make.just.on@gmail.com>2023-11-23 00:06:59 +0900
committergit <svn-admin@ruby-lang.org>2023-11-23 14:39:30 +0000
commit7b20dd9f91b4941e4f161dfd2d8b68b33a347a4c (patch)
treef42dd47b085d9f1a43dd10f6c4852b5197b889f5 /prism
parenta31cdc6ee9bfe517e7eaffdca7c8bfeefd0c1df1 (diff)
[ruby/prism] Fix LocalVariableTargetNode depth in patterns
Fix https://github.com/ruby/prism/pull/1821 https://github.com/ruby/prism/commit/7d023a26b4
Diffstat (limited to 'prism')
-rw-r--r--prism/prism.c33
1 files changed, 29 insertions, 4 deletions
diff --git a/prism/prism.c b/prism/prism.c
index 95bfc5a8db..ba93bbce14 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -4137,6 +4137,22 @@ pm_local_variable_target_node_create(pm_parser_t *parser, const pm_token_t *name
}
/**
+ * Allocate and initialize a new LocalVariableTargetNode node with the given depth.
+ */
+static pm_local_variable_target_node_t *
+pm_local_variable_target_node_create_depth(pm_parser_t *parser, const pm_token_t *name, uint32_t depth) {
+ pm_refute_numbered_parameter(parser, name->start, name->end);
+
+ return pm_local_variable_target_node_create_values(
+ parser,
+ &(pm_location_t) { .start = name->start, .end = name->end },
+ pm_parser_constant_id_token(parser, name),
+ depth
+ );
+}
+
+
+/**
* Allocate and initialize a new MatchPredicateNode node.
*/
static pm_match_predicate_node_t *
@@ -13003,8 +13019,13 @@ parse_pattern_primitive(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
case PM_TOKEN_IDENTIFIER:
case PM_TOKEN_METHOD_NAME: {
parser_lex(parser);
- pm_parser_local_add_token(parser, &parser->previous);
- return (pm_node_t *) pm_local_variable_target_node_create(parser, &parser->previous);
+ pm_token_t name = parser->previous;
+ int depth = pm_parser_local_depth(parser, &name);
+ if (depth < 0) {
+ depth = 0;
+ pm_parser_local_add_token(parser, &name);
+ }
+ return (pm_node_t *) pm_local_variable_target_node_create_depth(parser, &name, (uint32_t) depth);
}
case PM_TOKEN_BRACKET_LEFT_ARRAY: {
pm_token_t opening = parser->current;
@@ -13321,9 +13342,13 @@ parse_pattern_primitives(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
expect1(parser, PM_TOKEN_IDENTIFIER, PM_ERR_PATTERN_IDENT_AFTER_HROCKET);
pm_token_t identifier = parser->previous;
- pm_parser_local_add_token(parser, &identifier);
+ int depth = pm_parser_local_depth(parser, &identifier);
+ if (depth < 0) {
+ depth = 0;
+ pm_parser_local_add_token(parser, &identifier);
+ }
- pm_node_t *target = (pm_node_t *) pm_local_variable_target_node_create(parser, &identifier);
+ pm_node_t *target = (pm_node_t *) pm_local_variable_target_node_create_depth(parser, &identifier, (uint32_t) depth);
node = (pm_node_t *) pm_capture_pattern_node_create(parser, node, target, &operator);
}