Skip to content

Commit

Permalink
Check value expressions on creating a node
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Nov 14, 2023
1 parent be72c1d commit aeaa411
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ pm_alternation_pattern_node_create(pm_parser_t *parser, pm_node_t *left, pm_node
*/
static pm_and_node_t *
pm_and_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) {
pm_assert_value_expression(parser, left);

pm_and_node_t *node = PM_ALLOC_NODE(parser, pm_and_node_t);

*node = (pm_and_node_t) {
Expand Down Expand Up @@ -1584,6 +1586,8 @@ pm_call_node_create(pm_parser_t *parser) {
*/
static pm_call_node_t *
pm_call_node_aref_create(pm_parser_t *parser, pm_node_t *receiver, pm_arguments_t *arguments) {
pm_assert_value_expression(parser, receiver);

pm_call_node_t *node = pm_call_node_create(parser);

node->base.location.start = receiver->location.start;
Expand Down Expand Up @@ -1611,6 +1615,9 @@ pm_call_node_aref_create(pm_parser_t *parser, pm_node_t *receiver, pm_arguments_
*/
static pm_call_node_t *
pm_call_node_binary_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *operator, pm_node_t *argument) {
pm_assert_value_expression(parser, receiver);
pm_assert_value_expression(parser, argument);

pm_call_node_t *node = pm_call_node_create(parser);

node->base.location.start = MIN(receiver->location.start, argument->location.start);
Expand All @@ -1632,6 +1639,8 @@ pm_call_node_binary_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t
*/
static pm_call_node_t *
pm_call_node_call_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *operator, pm_token_t *message, pm_arguments_t *arguments) {
pm_assert_value_expression(parser, receiver);

pm_call_node_t *node = pm_call_node_create(parser);

node->base.location.start = receiver->location.start;
Expand Down Expand Up @@ -1695,6 +1704,8 @@ pm_call_node_fcall_create(pm_parser_t *parser, pm_token_t *message, pm_arguments
*/
static pm_call_node_t *
pm_call_node_not_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *message, pm_arguments_t *arguments) {
pm_assert_value_expression(parser, receiver);

pm_call_node_t *node = pm_call_node_create(parser);

node->base.location.start = message->start;
Expand All @@ -1719,6 +1730,8 @@ pm_call_node_not_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *me
*/
static pm_call_node_t *
pm_call_node_shorthand_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *operator, pm_arguments_t *arguments) {
pm_assert_value_expression(parser, receiver);

pm_call_node_t *node = pm_call_node_create(parser);

node->base.location.start = receiver->location.start;
Expand Down Expand Up @@ -1748,6 +1761,8 @@ pm_call_node_shorthand_create(pm_parser_t *parser, pm_node_t *receiver, pm_token
*/
static pm_call_node_t *
pm_call_node_unary_create(pm_parser_t *parser, pm_token_t *operator, pm_node_t *receiver, const char *name) {
pm_assert_value_expression(parser, receiver);

pm_call_node_t *node = pm_call_node_create(parser);

node->base.location.start = operator->start;
Expand Down Expand Up @@ -3243,6 +3258,7 @@ pm_if_node_modifier_create(pm_parser_t *parser, pm_node_t *statement, const pm_t
*/
static pm_if_node_t *
pm_if_node_ternary_create(pm_parser_t *parser, pm_node_t *predicate, pm_node_t *true_expression, const pm_token_t *colon, pm_node_t *false_expression) {
pm_assert_value_expression(parser, predicate);
pm_conditional_predicate(predicate);

pm_statements_node_t *if_statements = pm_statements_node_create(parser);
Expand Down Expand Up @@ -4000,6 +4016,8 @@ pm_local_variable_target_node_create(pm_parser_t *parser, const pm_token_t *name
*/
static pm_match_predicate_node_t *
pm_match_predicate_node_create(pm_parser_t *parser, pm_node_t *value, pm_node_t *pattern, const pm_token_t *operator) {
pm_assert_value_expression(parser, value);

pm_match_predicate_node_t *node = PM_ALLOC_NODE(parser, pm_match_predicate_node_t);

*node = (pm_match_predicate_node_t) {
Expand All @@ -4023,6 +4041,8 @@ pm_match_predicate_node_create(pm_parser_t *parser, pm_node_t *value, pm_node_t
*/
static pm_match_required_node_t *
pm_match_required_node_create(pm_parser_t *parser, pm_node_t *value, pm_node_t *pattern, const pm_token_t *operator) {
pm_assert_value_expression(parser, value);

pm_match_required_node_t *node = PM_ALLOC_NODE(parser, pm_match_required_node_t);

*node = (pm_match_required_node_t) {
Expand Down Expand Up @@ -4296,6 +4316,8 @@ pm_optional_parameter_node_create(pm_parser_t *parser, const pm_token_t *name, c
*/
static pm_or_node_t *
pm_or_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) {
pm_assert_value_expression(parser, left);

pm_or_node_t *node = PM_ALLOC_NODE(parser, pm_or_node_t);

*node = (pm_or_node_t) {
Expand Down Expand Up @@ -4566,6 +4588,9 @@ pm_pre_execution_node_create(pm_parser_t *parser, const pm_token_t *keyword, con
*/
static pm_range_node_t *
pm_range_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) {
pm_assert_value_expression(parser, left);
pm_assert_value_expression(parser, right);

pm_range_node_t *node = PM_ALLOC_NODE(parser, pm_range_node_t);
pm_node_flags_t flags = 0;

Expand Down
92 changes: 84 additions & 8 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1517,13 +1517,75 @@ def test_void_value_expression_in_modifier
1 unless (return)
1 while (return)
1 until (return)
(return) => a
(return) in a
RUBY
message = 'Unexpected void value expression'
assert_errors expression(source), source, [
[message, 6..12],
[message, 24..30],
[message, 41..47],
[message, 58..64],
[message, 67..73],
[message, 81..87]
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_expression
source = <<~RUBY
(return) ? 1 : 1
(return)..1
1..(return)
(return)...1
1...(return)
(..(return))
(...(return))
((return)..)
((return)...)
RUBY
message = 'Unexpected void value expression'
assert_errors expression(source), source, [
[message, 1..7],
[message, 18..24],
[message, 33..39],
[message, 42..48],
[message, 59..65],
[message, 71..77],
[message, 85..91],
[message, 96..102],
[message, 109..115]
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_hash
source = <<~RUBY
{ return => 1 }
{ 1 => return }
{ a: return }
{ **return }
RUBY
message = 'Unexpected void value expression'
assert_errors expression(source), source, [
[message, 2..8],
[message, 23..29],
[message, 37..43],
[message, 50..56],
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_call
source = <<~RUBY
(return).foo
(return).(1)
(return)[1]
(return)[1] = 2
RUBY
message = 'Unexpected void value expression'
assert_errors expression(source), source, [
[message, 1..7],
[message, 14..20],
[message, 27..33],
[message, 39..45],
], compare_ripper: false # Ripper does not check 'void value expression'.
end

Expand Down Expand Up @@ -1551,19 +1613,33 @@ def test_void_value_expression_in_arguments
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_hash
def test_void_value_expression_in_unary_call
source = <<~RUBY
{ return => 1 }
{ 1 => return }
{ a: return }
{ **return }
+(return)
not return
RUBY
message = 'Unexpected void value expression'
assert_errors expression(source), source, [
[message, 2..8],
[message, 23..29],
[message, 37..43],
[message, 50..56],
[message, 14..20],
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_binary_call
source = <<~RUBY
1 + (return)
(return) + 1
1 and (return)
(return) and 1
1 or (return)
(return) or 1
RUBY
message = 'Unexpected void value expression'
assert_errors expression(source), source, [
[message, 5..11],
[message, 14..20],
[message, 42..48],
[message, 71..77],
], compare_ripper: false # Ripper does not check 'void value expression'.
end

Expand Down

0 comments on commit aeaa411

Please sign in to comment.