Skip to content

Commit 988ac82

Browse files
committed
Fix up more error messages to more closely match parse.y
1 parent 63e03a5 commit 988ac82

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

bin/prism

+19-10
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,32 @@ module Prism
162162
# bin/prism error [name] [source]
163163
def error(argv)
164164
name = argv.shift
165-
source, _ = read_source(argv)
166-
167-
result = Prism.parse(source)
168-
raise "Expected #{source.inspect} to have errors" if result.success?
169165

166+
source = nil
170167
filepath = File.expand_path("../test/prism/errors/#{name}.txt", __dir__)
171168

172-
if File.file?(filepath)
173-
counter = 1
169+
if argv.empty?
170+
raise "Expected #{filepath} to exist" unless File.file?(filepath)
174171

175-
begin
176-
current = "#{File.dirname(filepath)}/#{File.basename(filepath, ".txt")}_#{counter += 1}.txt"
177-
end while File.file?(current)
172+
source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)
173+
source = source.lines.grep_v(/^\s*\^/).join.gsub(/\n*\z/, "")
174+
else
175+
if File.file?(filepath)
176+
counter = 1
178177

179-
filepath = current
178+
begin
179+
current = "#{File.dirname(filepath)}/#{File.basename(filepath, ".txt")}_#{counter += 1}.txt"
180+
end while File.file?(current)
181+
182+
filepath = current
183+
end
184+
185+
source, _ = read_source(argv)
180186
end
181187

188+
result = Prism.parse(source)
189+
raise "Expected #{source.inspect} to have errors" if result.success?
190+
182191
File.write(filepath, result.errors_format)
183192
end
184193

src/prism.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -15421,7 +15421,7 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept
1542115421
// then we have a trailing comma where we need to check whether it is
1542215422
// allowed or not.
1542315423
if (parser->previous.type == PM_TOKEN_COMMA && !match1(parser, PM_TOKEN_SEMICOLON)) {
15424-
pm_parser_err_previous(parser, PM_ERR_EXPECT_ARGUMENT);
15424+
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->previous, PM_ERR_EXPECT_ARGUMENT, pm_token_type_human(parser->current.type));
1542515425
}
1542615426

1542715427
pm_accepts_block_stack_pop(parser);
@@ -17649,7 +17649,8 @@ pm_parser_err_prefix(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
1764917649
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->previous, diag_id, human, parser->previous.start[0]);
1765017650
break;
1765117651
}
17652-
case PM_ERR_UNARY_DISALLOWED: {
17652+
case PM_ERR_UNARY_DISALLOWED:
17653+
case PM_ERR_EXPECT_ARGUMENT: {
1765317654
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, diag_id, pm_token_type_human(parser->current.type));
1765417655
break;
1765517656
}
@@ -17999,7 +18000,13 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1799918000
}
1800018001

1800118002
accept1(parser, PM_TOKEN_NEWLINE);
18002-
expect1(parser, PM_TOKEN_BRACKET_RIGHT, PM_ERR_ARRAY_TERM);
18003+
18004+
if (!accept1(parser, PM_TOKEN_BRACKET_RIGHT)) {
18005+
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, PM_ERR_ARRAY_TERM, pm_token_type_human(parser->current.type));
18006+
parser->previous.start = parser->previous.end;
18007+
parser->previous.type = PM_TOKEN_MISSING;
18008+
}
18009+
1800318010
pm_array_node_close_set(array, &parser->previous);
1800418011
pm_accepts_block_stack_pop(parser);
1800518012

templates/src/diagnostic.c.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
113113
[PM_ERR_ARRAY_EXPRESSION] = { "expected an expression for the array element", PM_ERROR_LEVEL_SYNTAX },
114114
[PM_ERR_ARRAY_EXPRESSION_AFTER_STAR] = { "expected an expression after `*` in the array", PM_ERROR_LEVEL_SYNTAX },
115115
[PM_ERR_ARRAY_SEPARATOR] = { "unexpected %s; expected a `,` separator for the array elements", PM_ERROR_LEVEL_SYNTAX },
116-
[PM_ERR_ARRAY_TERM] = { "expected a `]` to close the array", PM_ERROR_LEVEL_SYNTAX },
116+
[PM_ERR_ARRAY_TERM] = { "unexpected %s; expected a `]` to close the array", PM_ERROR_LEVEL_SYNTAX },
117117
[PM_ERR_BEGIN_LONELY_ELSE] = { "unexpected `else` in `begin` block; else without rescue is useless", PM_ERROR_LEVEL_SYNTAX },
118118
[PM_ERR_BEGIN_TERM] = { "expected an `end` to close the `begin` statement", PM_ERROR_LEVEL_SYNTAX },
119119
[PM_ERR_BEGIN_UPCASE_BRACE] = { "expected a `{` after `BEGIN`", PM_ERROR_LEVEL_SYNTAX },
@@ -170,7 +170,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
170170
[PM_ERR_ESCAPE_INVALID_UNICODE_LONG] = { "invalid Unicode escape sequence; maximum length is 6 digits", PM_ERROR_LEVEL_SYNTAX },
171171
[PM_ERR_ESCAPE_INVALID_UNICODE_SHORT] = { "too short escape sequence: %.*s", PM_ERROR_LEVEL_SYNTAX },
172172
[PM_ERR_ESCAPE_INVALID_UNICODE_TERM] = { "unterminated Unicode escape", PM_ERROR_LEVEL_SYNTAX },
173-
[PM_ERR_EXPECT_ARGUMENT] = { "expected an argument", PM_ERROR_LEVEL_SYNTAX },
173+
[PM_ERR_EXPECT_ARGUMENT] = { "unexpected %s; expected an argument", PM_ERROR_LEVEL_SYNTAX },
174174
[PM_ERR_EXPECT_EOL_AFTER_STATEMENT] = { "unexpected %s, expecting end-of-input", PM_ERROR_LEVEL_SYNTAX },
175175
[PM_ERR_EXPECT_EXPRESSION_AFTER_AMPAMPEQ] = { "expected an expression after `&&=`", PM_ERROR_LEVEL_SYNTAX },
176176
[PM_ERR_EXPECT_EXPRESSION_AFTER_PIPEPIPEEQ] = { "expected an expression after `||=`", PM_ERROR_LEVEL_SYNTAX },

test/prism/errors/break_1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
break 1,;
2-
^ expected an argument
2+
^ unexpected ';'; expected an argument
33
^~~~~~~~ Invalid break
44

test/prism/errors/next_1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
next 1,;
2-
^ expected an argument
2+
^ unexpected ';'; expected an argument
33
^~~~~~~ Invalid next
44

test/prism/errors/return_1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
return 1,;
2-
^ expected an argument
2+
^ unexpected ';'; expected an argument
33

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
foo 1,
2-
^ expected an argument
2+
^ unexpected end-of-input; expected an argument
33

0 commit comments

Comments
 (0)