Skip to content

Commit

Permalink
Merge pull request #1187 from ruby/constant-write-node
Browse files Browse the repository at this point in the history
Add constant write node
  • Loading branch information
kddnewton authored Aug 2, 2023
2 parents ff7d7a0 + a62f2f0 commit 3c8a8f9
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 30 deletions.
22 changes: 19 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -881,19 +881,35 @@ nodes:
- name: value
type: node?
comment: |
Represents writing to a constant.
Represents writing to a constant path.
Foo = 1
^^^^^^^
::Foo = 1
^^^^^^^^^
Foo::Bar = 1
^^^^^^^^^^^^
::Foo::Bar = 1
^^^^^^^^^^^^^^
- name: ConstantReadNode
comment: |
Represents referencing a constant.
Foo
^^^
- name: ConstantWriteNode
child_nodes:
- name: name_loc
type: location
- name: value
type: node?
- name: operator_loc
type: location?
comment: |
Represents writing to a constant.
Foo = 1
^^^^^^^
- name: DefNode
child_nodes:
- name: name_loc
Expand Down
24 changes: 23 additions & 1 deletion src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,27 @@ yp_constant_read_node_create(yp_parser_t *parser, const yp_token_t *name) {
return node;
}

// Allocate a new ConstantWriteNode node.
static yp_constant_write_node_t *
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_NODE_CONSTANT_WRITE_NODE,
.location = {
.start = target->base.location.start,
.end = value != NULL ? value->location.end : target->base.location.end
},
},
.name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) target),
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
.value = value
};

return node;
}

// Allocate and initialize a new DefNode node.
static yp_def_node_t *
yp_def_node_create(
Expand Down Expand Up @@ -7560,8 +7581,9 @@ parse_target(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_no
return (yp_node_t *) write_node;
}
case YP_NODE_CONSTANT_PATH_NODE:
case YP_NODE_CONSTANT_READ_NODE:
return (yp_node_t *) yp_constant_path_write_node_create(parser, target, operator, value);
case YP_NODE_CONSTANT_READ_NODE:
return (yp_node_t *) yp_constant_write_node_create(parser, (yp_constant_read_node_t *) target, operator, value);
case YP_NODE_BACK_REFERENCE_READ_NODE:
case YP_NODE_NUMBERED_REFERENCE_READ_NODE:
yp_diagnostic_list_append(&parser->error_list, target->location.start, target->location.end, "Can't set variable");
Expand Down
6 changes: 1 addition & 5 deletions test/snapshots/constants.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/snapshots/methods.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/snapshots/unparser/corpus/literal/assignment.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/snapshots/variables.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions test/snapshots/whitequark/casgn_unscoped.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/snapshots/whitequark/parser_bug_490.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c8a8f9

Please sign in to comment.