Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow BEGIN except the toplevel #1560

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -12145,6 +12145,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