summaryrefslogtreecommitdiff
path: root/yarp
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2023-09-15 11:09:18 -0400
committergit <[email protected]>2023-09-15 23:32:05 +0000
commitcb686b9cccf571a70f4ac85bef0ebb2b544fba97 (patch)
treecd7f6eaa679f1d08c1411bd0801691e7123d6019 /yarp
parent4c28a61e835645fefa238536acd6334451fb2dde (diff)
[ruby/yarp] Handle missing clauses in case statement
https://github.com/ruby/yarp/commit/1ad7fba5ef
Diffstat (limited to 'yarp')
-rw-r--r--yarp/diagnostic.c2
-rw-r--r--yarp/diagnostic.h2
-rw-r--r--yarp/yarp.c11
3 files changed, 9 insertions, 6 deletions
diff --git a/yarp/diagnostic.c b/yarp/diagnostic.c
index 9bbc30edee..34da3f97a5 100644
--- a/yarp/diagnostic.c
+++ b/yarp/diagnostic.c
@@ -85,7 +85,7 @@ static const char* const diagnostic_messages[YP_DIAGNOSTIC_ID_LEN] = {
[YP_ERR_CANNOT_PARSE_STRING_PART] = "Cannot parse the string part",
[YP_ERR_CASE_EXPRESSION_AFTER_CASE] = "Expected an expression after `case`",
[YP_ERR_CASE_EXPRESSION_AFTER_WHEN] = "Expected an expression after `when`",
- [YP_ERR_CASE_LONELY_ELSE] = "Unexpected `else` in `case` statement; a `when` clause must precede `else`",
+ [YP_ERR_CASE_MISSING_CONDITIONS] = "Expected a `when` or `in` clause after `case`",
[YP_ERR_CASE_TERM] = "Expected an `end` to close the `case` statement",
[YP_ERR_CLASS_IN_METHOD] = "Unexpected class definition in a method body",
[YP_ERR_CLASS_NAME] = "Expected a constant name after `class`",
diff --git a/yarp/diagnostic.h b/yarp/diagnostic.h
index a4b030adfd..b8d9fe0100 100644
--- a/yarp/diagnostic.h
+++ b/yarp/diagnostic.h
@@ -50,7 +50,7 @@ typedef enum {
YP_ERR_CANNOT_PARSE_STRING_PART,
YP_ERR_CASE_EXPRESSION_AFTER_CASE,
YP_ERR_CASE_EXPRESSION_AFTER_WHEN,
- YP_ERR_CASE_LONELY_ELSE,
+ YP_ERR_CASE_MISSING_CONDITIONS,
YP_ERR_CASE_TERM,
YP_ERR_CLASS_IN_METHOD,
YP_ERR_CLASS_NAME,
diff --git a/yarp/yarp.c b/yarp/yarp.c
index aeb015611b..b250220e7e 100644
--- a/yarp/yarp.c
+++ b/yarp/yarp.c
@@ -11935,6 +11935,7 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
}
if (accept1(parser, YP_TOKEN_KEYWORD_END)) {
+ yp_diagnostic_list_append(&parser->error_list, case_keyword.start, case_keyword.end, YP_ERR_CASE_MISSING_CONDITIONS);
return (yp_node_t *) yp_case_node_create(parser, &case_keyword, predicate, NULL, &parser->previous);
}
@@ -12041,12 +12042,14 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
}
}
+ // If we didn't parse any conditions (in or when) then we need to
+ // indicate that we have an error.
+ if (case_node->conditions.size == 0) {
+ yp_diagnostic_list_append(&parser->error_list, case_keyword.start, case_keyword.end, YP_ERR_CASE_MISSING_CONDITIONS);
+ }
+
accept2(parser, YP_TOKEN_NEWLINE, YP_TOKEN_SEMICOLON);
if (accept1(parser, YP_TOKEN_KEYWORD_ELSE)) {
- if (case_node->conditions.size < 1) {
- yp_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, YP_ERR_CASE_LONELY_ELSE);
- }
-
yp_token_t else_keyword = parser->previous;
yp_else_node_t *else_node;