Skip to content

Commit d398e7d

Browse files
committed
Better error message on invalid def
1 parent 60928bf commit d398e7d

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ errors:
6060
- DEF_ENDLESS
6161
- DEF_ENDLESS_SETTER
6262
- DEF_NAME
63-
- DEF_NAME_AFTER_RECEIVER
6463
- DEF_PARAMS_TERM
6564
- DEF_PARAMS_TERM_PAREN
6665
- DEF_RECEIVER
@@ -97,6 +96,7 @@ errors:
9796
- EXPECT_EXPRESSION_AFTER_STAR
9897
- EXPECT_IDENT_REQ_PARAMETER
9998
- EXPECT_LPAREN_REQ_PARAMETER
99+
- EXPECT_MESSAGE
100100
- EXPECT_RBRACKET
101101
- EXPECT_RPAREN
102102
- EXPECT_RPAREN_AFTER_MULTI

src/prism.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15281,6 +15281,7 @@ parse_method_definition_name(pm_parser_t *parser) {
1528115281
parser_lex(parser);
1528215282
return parser->previous;
1528315283
default:
15284+
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, PM_ERR_DEF_NAME, pm_token_type_human(parser->current.type));
1528415285
return (pm_token_t) { .type = PM_TOKEN_MISSING, .start = parser->current.start, .end = parser->current.end };
1528515286
}
1528615287
}
@@ -17722,7 +17723,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1772217723

1772317724
pm_node_t *receiver = NULL;
1772417725
pm_token_t operator = not_provided(parser);
17725-
pm_token_t name = (pm_token_t) { .type = PM_TOKEN_MISSING, .start = def_keyword.end, .end = def_keyword.end };
17726+
pm_token_t name;
1772617727

1772717728
// This context is necessary for lexing `...` in a bare params
1772817729
// correctly. It must be pushed before lexing the first param, so it
@@ -17804,7 +17805,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1780417805
receiver = (pm_node_t *) pm_true_node_create(parser, &identifier);
1780517806
break;
1780617807
case PM_TOKEN_KEYWORD_FALSE:
17807-
receiver = (pm_node_t *)pm_false_node_create(parser, &identifier);
17808+
receiver = (pm_node_t *) pm_false_node_create(parser, &identifier);
1780817809
break;
1780917810
case PM_TOKEN_KEYWORD___FILE__:
1781017811
receiver = (pm_node_t *) pm_source_file_node_create(parser, &identifier);
@@ -17826,9 +17827,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1782617827
break;
1782717828
}
1782817829
case PM_TOKEN_PARENTHESIS_LEFT: {
17829-
// The current context is `PM_CONTEXT_DEF_PARAMS`, however the inner expression
17830-
// of this parenthesis should not be processed under this context.
17831-
// Thus, the context is popped here.
17830+
// The current context is `PM_CONTEXT_DEF_PARAMS`, however
17831+
// the inner expression of this parenthesis should not be
17832+
// processed under this context. Thus, the context is popped
17833+
// here.
1783217834
context_pop(parser);
1783317835
parser_lex(parser);
1783417836

@@ -17845,7 +17847,8 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1784517847
operator = parser->previous;
1784617848
receiver = (pm_node_t *) pm_parentheses_node_create(parser, &lparen, expression, &rparen);
1784717849

17848-
// To push `PM_CONTEXT_DEF_PARAMS` again is for the same reason as described the above.
17850+
// To push `PM_CONTEXT_DEF_PARAMS` again is for the same
17851+
// reason as described the above.
1784917852
pm_parser_scope_push(parser, true);
1785017853
context_push(parser, PM_CONTEXT_DEF_PARAMS);
1785117854
name = parse_method_definition_name(parser);
@@ -17857,12 +17860,6 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1785717860
break;
1785817861
}
1785917862

17860-
// If, after all that, we were unable to find a method name, add an
17861-
// error to the error list.
17862-
if (name.type == PM_TOKEN_MISSING) {
17863-
pm_parser_err_previous(parser, PM_ERR_DEF_NAME);
17864-
}
17865-
1786617863
pm_token_t lparen;
1786717864
pm_token_t rparen;
1786817865
pm_parameters_node_t *params;
@@ -19856,7 +19853,7 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
1985619853
break;
1985719854
}
1985819855
default: {
19859-
pm_parser_err_current(parser, PM_ERR_DEF_NAME);
19856+
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, PM_ERR_EXPECT_MESSAGE, pm_token_type_human(parser->current.type));
1986019857
message = (pm_token_t) { .type = PM_TOKEN_MISSING, .start = parser->previous.end, .end = parser->previous.end };
1986119858
}
1986219859
}

templates/src/diagnostic.c.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
144144
[PM_ERR_CONSTANT_PATH_COLON_COLON_CONSTANT] = { "expected a constant after the `::` operator", PM_ERROR_LEVEL_SYNTAX },
145145
[PM_ERR_DEF_ENDLESS] = { "could not parse the endless method body", PM_ERROR_LEVEL_SYNTAX },
146146
[PM_ERR_DEF_ENDLESS_SETTER] = { "invalid method name; a setter method cannot be defined in an endless method definition", PM_ERROR_LEVEL_SYNTAX },
147-
[PM_ERR_DEF_NAME] = { "expected a method name", PM_ERROR_LEVEL_SYNTAX },
148-
[PM_ERR_DEF_NAME_AFTER_RECEIVER] = { "expected a method name after the receiver", PM_ERROR_LEVEL_SYNTAX },
147+
[PM_ERR_DEF_NAME] = { "unexpected %s; expected a method name", PM_ERROR_LEVEL_SYNTAX },
149148
[PM_ERR_DEF_PARAMS_TERM] = { "expected a delimiter to close the parameters", PM_ERROR_LEVEL_SYNTAX },
150149
[PM_ERR_DEF_PARAMS_TERM_PAREN] = { "expected a `)` to close the parameters", PM_ERROR_LEVEL_SYNTAX },
151150
[PM_ERR_DEF_RECEIVER] = { "expected a receiver for the method definition", PM_ERROR_LEVEL_SYNTAX },
@@ -181,6 +180,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
181180
[PM_ERR_EXPECT_EXPRESSION_AFTER_STAR] = { "expected an expression after `*`", PM_ERROR_LEVEL_SYNTAX },
182181
[PM_ERR_EXPECT_IDENT_REQ_PARAMETER] = { "expected an identifier for the required parameter", PM_ERROR_LEVEL_SYNTAX },
183182
[PM_ERR_EXPECT_LPAREN_REQ_PARAMETER] = { "expected a `(` to start a required parameter", PM_ERROR_LEVEL_SYNTAX },
183+
[PM_ERR_EXPECT_MESSAGE] = { "unexpected %s; expecting a message to send to the receiver", PM_ERROR_LEVEL_SYNTAX },
184184
[PM_ERR_EXPECT_RBRACKET] = { "expected a matching `]`", PM_ERROR_LEVEL_SYNTAX },
185185
[PM_ERR_EXPECT_RPAREN] = { "expected a matching `)`", PM_ERROR_LEVEL_SYNTAX },
186186
[PM_ERR_EXPECT_RPAREN_AFTER_MULTI] = { "expected a `)` after multiple assignment", PM_ERROR_LEVEL_SYNTAX },

templates/src/token_type.c.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,13 @@ pm_token_type_human(pm_token_type_t token_type) {
326326
case PM_TOKEN_STAR_STAR_EQUAL:
327327
return "'**='";
328328
case PM_TOKEN_STRING_BEGIN:
329-
return "string beginning";
329+
return "string literal";
330330
case PM_TOKEN_STRING_CONTENT:
331331
return "string content";
332332
case PM_TOKEN_STRING_END:
333333
return "string ending";
334334
case PM_TOKEN_SYMBOL_BEGIN:
335-
return "symbol beginning";
335+
return "symbol literal";
336336
case PM_TOKEN_TILDE:
337337
return "'~'";
338338
case PM_TOKEN_UAMPERSAND:

test/prism/errors_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def test_aliasing_global_variable_with_global_number_variable
341341
def test_def_with_expression_receiver_and_no_identifier
342342
assert_errors expression("def (a); end"), "def (a); end", [
343343
["expected a `.` or `::` after the receiver in a method definition", 7..7],
344-
["expected a method name", 7..7]
344+
["unexpected ';'; expected a method name", 7..8]
345345
]
346346
end
347347

0 commit comments

Comments
 (0)