Skip to content

Commit a62f2f0

Browse files
committed
Add constant write node
1 parent 7d0c39f commit a62f2f0

File tree

8 files changed

+60
-30
lines changed

8 files changed

+60
-30
lines changed

config.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,19 +873,35 @@ nodes:
873873
- name: value
874874
type: node?
875875
comment: |
876-
Represents writing to a constant.
876+
Represents writing to a constant path.
877877
878-
Foo = 1
879-
^^^^^^^
878+
::Foo = 1
879+
^^^^^^^^^
880880
881881
Foo::Bar = 1
882882
^^^^^^^^^^^^
883+
884+
::Foo::Bar = 1
885+
^^^^^^^^^^^^^^
883886
- name: ConstantReadNode
884887
comment: |
885888
Represents referencing a constant.
886889
887890
Foo
888891
^^^
892+
- name: ConstantWriteNode
893+
child_nodes:
894+
- name: name_loc
895+
type: location
896+
- name: value
897+
type: node?
898+
- name: operator_loc
899+
type: location?
900+
comment: |
901+
Represents writing to a constant.
902+
903+
Foo = 1
904+
^^^^^^^
889905
- name: DefNode
890906
child_nodes:
891907
- name: name_loc

src/yarp.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,27 @@ yp_constant_read_node_create(yp_parser_t *parser, const yp_token_t *name) {
17001700
return node;
17011701
}
17021702

1703+
// Allocate a new ConstantWriteNode node.
1704+
static yp_constant_write_node_t *
1705+
yp_constant_write_node_create(yp_parser_t *parser, yp_constant_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
1706+
yp_constant_write_node_t *node = YP_ALLOC_NODE(parser, yp_constant_write_node_t);
1707+
1708+
*node = (yp_constant_write_node_t) {
1709+
{
1710+
.type = YP_NODE_CONSTANT_WRITE_NODE,
1711+
.location = {
1712+
.start = target->base.location.start,
1713+
.end = value != NULL ? value->location.end : target->base.location.end
1714+
},
1715+
},
1716+
.name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) target),
1717+
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
1718+
.value = value
1719+
};
1720+
1721+
return node;
1722+
}
1723+
17031724
// Allocate and initialize a new DefNode node.
17041725
static yp_def_node_t *
17051726
yp_def_node_create(
@@ -7540,8 +7561,9 @@ parse_target(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_no
75407561
return (yp_node_t *) write_node;
75417562
}
75427563
case YP_NODE_CONSTANT_PATH_NODE:
7543-
case YP_NODE_CONSTANT_READ_NODE:
75447564
return (yp_node_t *) yp_constant_path_write_node_create(parser, target, operator, value);
7565+
case YP_NODE_CONSTANT_READ_NODE:
7566+
return (yp_node_t *) yp_constant_write_node_create(parser, (yp_constant_read_node_t *) target, operator, value);
75457567
case YP_NODE_BACK_REFERENCE_READ_NODE:
75467568
case YP_NODE_NUMBERED_REFERENCE_READ_NODE:
75477569
yp_diagnostic_list_append(&parser->error_list, target->location.start, target->location.end, "Can't set variable");

test/snapshots/constants.txt

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/methods.txt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/unparser/corpus/literal/assignment.txt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/variables.txt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/whitequark/casgn_unscoped.txt

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/whitequark/parser_bug_490.txt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)