Skip to content

Commit

Permalink
Normalize and add error messages for unterminated literals (#5409)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and RX14 committed Dec 19, 2017
1 parent c74c0bd commit 993916a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
10 changes: 8 additions & 2 deletions spec/compiler/parser/parser_spec.cr
Expand Up @@ -1601,8 +1601,14 @@ describe "Parser" do
assert_syntax_error %(case x; when /x/; 2; when /x/; end), "duplicate when /x/ in case"
assert_syntax_error %(case x; when X; 2; when X; end), "duplicate when X in case"

assert_syntax_error "%w(", "Unterminated String array literal"
assert_syntax_error "%i(", "Unterminated Symbol array literal"
assert_syntax_error "%w(", "Unterminated string array literal"
assert_syntax_error "%i(", "Unterminated symbol array literal"
assert_syntax_error "%x(", "Unterminated command literal"
assert_syntax_error "%r(", "Unterminated regular expression"
assert_syntax_error "%q(", "Unterminated string literal"
assert_syntax_error "%Q(", "Unterminated string literal"
assert_syntax_error "<<-HEREDOC", "Unexpected EOF on heredoc identifier"
assert_syntax_error "<<-HEREDOC\n", "Unterminated heredoc"

it "gets corrects of ~" do
node = Parser.parse("\n ~1")
Expand Down
1 change: 1 addition & 0 deletions spec/compiler/parser/to_s_spec.cr
Expand Up @@ -116,4 +116,5 @@ describe "ASTNode#to_s" do
expect_to_s %([(1 + 2)] of Int32)
expect_to_s %(foo(1, (2 + 3), bar: (4 + 5)))
expect_to_s %(if (1 + 2\n3)\n 4\nend)
expect_to_s "%x(whoami)", "`whoami`"
end
20 changes: 11 additions & 9 deletions src/compiler/crystal/syntax/lexer.cr
Expand Up @@ -183,7 +183,7 @@ module Crystal
when ident_part?(char)
here << char
when char == '\0'
raise "unexpected EOF on heredoc identifier"
raise "Unexpected EOF on heredoc identifier"
else
if char == '\'' && has_single_quote
found_closing_single_quote = true
Expand Down Expand Up @@ -1729,7 +1729,7 @@ module Crystal

case current_char
when '\0'
raise_unterminated_quoted string_end
raise_unterminated_quoted delimiter_state
when string_end
next_char
if string_open_count == 0
Expand Down Expand Up @@ -1801,7 +1801,7 @@ module Crystal
char = next_char
case char
when '\0'
raise_unterminated_quoted string_end
raise_unterminated_quoted delimiter_state
when '\n'
incr_line_number
@token.line_number = @line_number
Expand Down Expand Up @@ -1919,12 +1919,14 @@ module Crystal
@token
end

def raise_unterminated_quoted(string_end)
msg = case string_end
when '`' then "unterminated command"
when '/' then "unterminated regular expression"
when String then "unterminated heredoc"
else "unterminated string literal"
def raise_unterminated_quoted(delimiter_state)
msg = case delimiter_state.kind
when :command then "Unterminated command literal"
when :regex then "Unterminated regular expression"
when :heredoc then "Unterminated heredoc"
when :string then "Unterminated string literal"
else
::raise "unreachable"
end
raise msg, @line_number, @column_number
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/syntax/parser.cr
Expand Up @@ -2054,7 +2054,7 @@ module Crystal
next_token
break
else
raise "Unterminated #{elements_type} array literal"
raise "Unterminated #{elements_type.downcase} array literal"
end
end

Expand Down

0 comments on commit 993916a

Please sign in to comment.