Skip to content

Commit

Permalink
Merge pull request #1560 from haldun/haldun/begin-at-toplevel
Browse files Browse the repository at this point in the history
Do not allow BEGIN except the toplevel
  • Loading branch information
kddnewton authored Sep 22, 2023
2 parents 1c98514 + e6f124a commit c7231f1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/yarp/diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef enum {
YP_ERR_BEGIN_TERM,
YP_ERR_BEGIN_UPCASE_BRACE,
YP_ERR_BEGIN_UPCASE_TERM,
YP_ERR_BEGIN_UPCASE_TOPLEVEL,
YP_ERR_BLOCK_PARAM_LOCAL_VARIABLE,
YP_ERR_BLOCK_PARAM_PIPE_TERM,
YP_ERR_BLOCK_TERM_BRACE,
Expand Down
1 change: 1 addition & 0 deletions src/diagnostic.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static const char* const diagnostic_messages[YP_DIAGNOSTIC_ID_LEN] = {
[YP_ERR_BEGIN_TERM] = "Expected an `end` to close the `begin` statement",
[YP_ERR_BEGIN_UPCASE_BRACE] = "Expected a `{` after `BEGIN`",
[YP_ERR_BEGIN_UPCASE_TERM] = "Expected a `}` to close the `BEGIN` statement",
[YP_ERR_BEGIN_UPCASE_TOPLEVEL] = "BEGIN is permitted only at toplevel",
[YP_ERR_BLOCK_PARAM_LOCAL_VARIABLE] = "Expected a local variable name in the block parameters",
[YP_ERR_BLOCK_PARAM_PIPE_TERM] = "Expected the block parameters to end with `|`",
[YP_ERR_BLOCK_TERM_BRACE] = "Expected a block beginning with `{` to end with `}`",
Expand Down
4 changes: 4 additions & 0 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12144,6 +12144,10 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
yp_statements_node_t *statements = parse_statements(parser, YP_CONTEXT_PREEXE);

expect1(parser, YP_TOKEN_BRACE_RIGHT, YP_ERR_BEGIN_UPCASE_TERM);
yp_context_t context = parser->current_context->context;
if ((context != YP_CONTEXT_MAIN) && (context != YP_CONTEXT_PREEXE)) {
yp_diagnostic_list_append(&parser->error_list, keyword.start, keyword.end, YP_ERR_BEGIN_UPCASE_TOPLEVEL);
}
return (yp_node_t *) yp_pre_execution_node_create(parser, &keyword, &opening, statements, &parser->previous);
}
case YP_TOKEN_KEYWORD_BREAK:
Expand Down
7 changes: 7 additions & 0 deletions test/yarp/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,13 @@ def test_alnum_delimiters
assert_error_messages "%sXfooX", error_messages
end

def test_begin_at_toplevel
source = "def foo; BEGIN {}; end"
assert_errors expression(source), source, [
["BEGIN is permitted only at toplevel", 9..14],
]
end

def test_numbered_parameters_in_block_arguments
source = "foo { |_1| }"
assert_errors expression(source), source, [
Expand Down

0 comments on commit c7231f1

Please sign in to comment.