Skip to content

Commit 0d3e8cd

Browse files
committed
Extract out parse_write_name
1 parent d3a852d commit 0d3e8cd

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

src/yarp.c

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8115,6 +8115,26 @@ parse_starred_expression(yp_parser_t *parser, yp_binding_power_t binding_power,
81158115
return parse_expression(parser, binding_power, diag_id);
81168116
}
81178117

8118+
// Convert the name of a method into the corresponding write method name. For
8119+
// exmaple, foo would be turned into foo=.
8120+
static void
8121+
parse_write_name(yp_string_t *string) {
8122+
// The method name needs to change. If we previously had
8123+
// foo, we now need foo=. In this case we'll allocate a new
8124+
// owned string, copy the previous method name in, and
8125+
// append an =.
8126+
size_t length = yp_string_length(string);
8127+
uint8_t *name = calloc(length + 1, sizeof(uint8_t));
8128+
if (name == NULL) return;
8129+
8130+
memcpy(name, yp_string_source(string), length);
8131+
name[length] = '=';
8132+
8133+
// Now switch the name to the new string.
8134+
yp_string_free(string);
8135+
yp_string_owned_init(string, name, length + 1);
8136+
}
8137+
81188138
// Convert the given node into a valid target node.
81198139
static yp_node_t *
81208140
parse_target(yp_parser_t *parser, yp_node_t *target) {
@@ -8205,23 +8225,8 @@ parse_target(yp_parser_t *parser, yp_node_t *target) {
82058225
}
82068226

82078227
if (*call->message_loc.start == '_' || parser->encoding.alnum_char(call->message_loc.start, call->message_loc.end - call->message_loc.start)) {
8208-
// The method name needs to change. If we previously had
8209-
// foo, we now need foo=. In this case we'll allocate a new
8210-
// owned string, copy the previous method name in, and
8211-
// append an =.
8212-
size_t length = yp_string_length(&call->name);
8213-
8214-
uint8_t *name = calloc(length + 1, sizeof(uint8_t));
8215-
if (name == NULL) return NULL;
8216-
8217-
memcpy(name, yp_string_source(&call->name), length);
8218-
name[length] = '=';
8219-
8220-
// Now switch the name to the new string.
8221-
yp_string_free(&call->name);
8222-
yp_string_owned_init(&call->name, name, length + 1);
8223-
8224-
return target;
8228+
parse_write_name(&call->name);
8229+
return (yp_node_t *) call;
82258230
}
82268231
}
82278232

@@ -8359,25 +8364,12 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod
83598364
// arguments node, parse the argument, and add it to the list.
83608365
yp_arguments_node_t *arguments = yp_arguments_node_create(parser);
83618366
call->arguments = arguments;
8362-
yp_arguments_node_arguments_append(arguments, value);
8363-
target->location.end = arguments->base.location.end;
8364-
8365-
// The method name needs to change. If we previously had foo, we now
8366-
// need foo=. In this case we'll allocate a new owned string, copy
8367-
// the previous method name in, and append an =.
8368-
size_t length = yp_string_length(&call->name);
8369-
8370-
uint8_t *name = calloc(length + 1, sizeof(uint8_t));
8371-
if (name == NULL) return NULL;
83728367

8373-
memcpy(name, yp_string_source(&call->name), length);
8374-
name[length] = '=';
8375-
8376-
// Now switch the name to the new string.
8377-
yp_string_free(&call->name);
8378-
yp_string_owned_init(&call->name, name, length + 1);
8368+
yp_arguments_node_arguments_append(arguments, value);
8369+
call->base.location.end = arguments->base.location.end;
83798370

8380-
return target;
8371+
parse_write_name(&call->name);
8372+
return (yp_node_t *) call;
83818373
}
83828374
}
83838375

0 commit comments

Comments
 (0)