Skip to content

Commit

Permalink
Resync yarp (#8498)
Browse files Browse the repository at this point in the history
* [YARP] Reject numbered parameters in block parameters

* [YARP] Do not allow BEGIN except the toplevel

---------

Co-authored-by: Haldun Bayhantopcu <haldun@github.com>
  • Loading branch information
jemmaissroff and haldun committed Sep 22, 2023
1 parent c54e225 commit 9abaf39
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
14 changes: 14 additions & 0 deletions test/yarp/errors_test.rb
Expand Up @@ -1302,6 +1302,20 @@ 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, [
["Token reserved for a numbered parameter", 7..9],
]
end

def test_conditional_predicate_closed
source = "if 0 0; end\nunless 0 0; end"
assert_errors expression(source), source, [
Expand Down
1 change: 1 addition & 0 deletions yarp/diagnostic.c
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
1 change: 1 addition & 0 deletions yarp/diagnostic.h
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
19 changes: 14 additions & 5 deletions yarp/yarp.c
Expand Up @@ -4818,10 +4818,20 @@ yp_parser_local_add_owned(yp_parser_t *parser, const uint8_t *start, size_t leng
if (constant_id != 0) yp_parser_local_add(parser, constant_id);
}

static inline bool
token_is_numbered_parameter(const uint8_t *start, const uint8_t *end) {
return (end - start == 2) && (start[0] == '_') && (start[1] != '0') && (yp_char_is_decimal_digit(start[1]));
}

// Add a parameter name to the current scope and check whether the name of the
// parameter is unique or not.
static void
yp_parser_parameter_name_check(yp_parser_t *parser, yp_token_t *name) {
// We want to check whether the parameter name is a numbered parameter or not.
if (token_is_numbered_parameter(name->start, name->end)) {
yp_diagnostic_list_append(&parser->error_list, name->start, name->end, YP_ERR_PARAMETER_NUMBERED_RESERVED);
}

// We want to ignore any parameter name that starts with an underscore.
if ((*name->start == '_')) return;

Expand Down Expand Up @@ -4902,11 +4912,6 @@ char_is_global_name_punctuation(const uint8_t b) {
return (yp_global_name_punctuation_hash[(i - 0x20) / 32] >> (i % 32)) & 1;
}

static inline bool
token_is_numbered_parameter(const uint8_t *start, const uint8_t *end) {
return (end - start == 2) && (start[0] == '_') && (start[1] != '0') && (yp_char_is_decimal_digit(start[1]));
}

static inline bool
token_is_setter_name(yp_token_t *token) {
return (
Expand Down Expand Up @@ -12155,6 +12160,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

0 comments on commit 9abaf39

Please sign in to comment.