Skip to content

Commit

Permalink
[ruby/yarp] Check whether the conditional predicate is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
haldun authored and matzbot committed Sep 20, 2023
1 parent 639971a commit 0a630fa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions test/yarp/errors_test.rb
Expand Up @@ -1302,6 +1302,14 @@ def test_alnum_delimiters
assert_error_messages "%sXfooX", error_messages
end

def test_conditional_predicate_closed
source = "if 0 0; end\nunless 0 0; end"
assert_errors expression(source), source, [
["Expected `then` or `;` or '\n" + "'", 5..6],
["Expected `then` or `;` or '\n" + "'", 21..22],
]
end

private

def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby")
Expand Down
1 change: 1 addition & 0 deletions yarp/diagnostic.c
Expand Up @@ -93,6 +93,7 @@ static const char* const diagnostic_messages[YP_DIAGNOSTIC_ID_LEN] = {
[YP_ERR_CLASS_TERM] = "Expected an `end` to close the `class` statement",
[YP_ERR_CONDITIONAL_ELSIF_PREDICATE] = "Expected a predicate expression for the `elsif` statement",
[YP_ERR_CONDITIONAL_IF_PREDICATE] = "Expected a predicate expression for the `if` statement",
[YP_ERR_CONDITIONAL_PREDICATE_TERM] = "Expected `then` or `;` or '\n'",
[YP_ERR_CONDITIONAL_TERM] = "Expected an `end` to close the conditional clause",
[YP_ERR_CONDITIONAL_TERM_ELSE] = "Expected an `end` to close the `else` clause",
[YP_ERR_CONDITIONAL_UNLESS_PREDICATE] = "Expected a predicate expression for the `unless` statement",
Expand Down
1 change: 1 addition & 0 deletions yarp/diagnostic.h
Expand Up @@ -58,6 +58,7 @@ typedef enum {
YP_ERR_CLASS_TERM,
YP_ERR_CONDITIONAL_ELSIF_PREDICATE,
YP_ERR_CONDITIONAL_IF_PREDICATE,
YP_ERR_CONDITIONAL_PREDICATE_TERM,
YP_ERR_CONDITIONAL_TERM,
YP_ERR_CONDITIONAL_TERM_ELSE,
YP_ERR_CONDITIONAL_UNLESS_PREDICATE,
Expand Down
7 changes: 5 additions & 2 deletions yarp/yarp.c
Expand Up @@ -9814,8 +9814,11 @@ parse_conditional(yp_parser_t *parser, yp_context_t context) {
yp_node_t *predicate = parse_expression(parser, YP_BINDING_POWER_MODIFIER, error_id);

// Predicates are closed by a term, a "then", or a term and then a "then".
accept2(parser, YP_TOKEN_NEWLINE, YP_TOKEN_SEMICOLON);
accept1(parser, YP_TOKEN_KEYWORD_THEN);
bool predicate_closed = accept2(parser, YP_TOKEN_NEWLINE, YP_TOKEN_SEMICOLON);
predicate_closed |= accept1(parser, YP_TOKEN_KEYWORD_THEN);
if (!predicate_closed) {
yp_diagnostic_list_append(&parser->error_list, parser->current.start, parser->current.end, YP_ERR_CONDITIONAL_PREDICATE_TERM);
}

context_pop(parser);
yp_statements_node_t *statements = NULL;
Expand Down

0 comments on commit 0a630fa

Please sign in to comment.