Skip to content

Commit

Permalink
Fix parsing of empty array with space/newline
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and ysbaddaden committed May 19, 2018
1 parent 20e7d36 commit 4b8159c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion spec/compiler/parser/parser_spec.cr
Expand Up @@ -82,7 +82,8 @@ describe "Parser" do
it_parses %(:"\\\\foo"), "\\foo".symbol
it_parses %(:"\\\"foo"), "\"foo".symbol
it_parses %(:"\\\"foo\\\""), "\"foo\"".symbol
it_parses %(:"\\a\\b\\n\\r\\t\\v\\f\\e"), "\a\b\n\r\t\v\f\e".symbol
# TODO: uncomment after 0.24.2
# it_parses %(:"\\a\\b\\n\\r\\t\\v\\f\\e"), "\a\b\n\r\t\v\f\e".symbol
it_parses %(:"\\u{61}"), "a".symbol

it_parses "[1, 2]", ([1.int32, 2.int32] of ASTNode).array
Expand Down Expand Up @@ -1560,7 +1561,9 @@ describe "Parser" do

assert_syntax_error "def Foo(Int32).bar;end"

assert_syntax_error "[\n]", "for empty arrays use '[] of ElementType'"
assert_syntax_error "[1 1]"
assert_syntax_error "{\n}", "for empty hashes use '{} of KeyType => ValueType'"
assert_syntax_error "{1 => 2 3 => 4}"
assert_syntax_error "{1 => 2, 3 => 4 5 => 6}"
assert_syntax_error "{a: 1 b: 2}"
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Expand Up @@ -2164,6 +2164,9 @@ module Crystal
end

def parse_array_literal
line = @line_number
column = @token.column_number

slash_is_regex!

exps = [] of ASTNode
Expand Down Expand Up @@ -2199,6 +2202,8 @@ module Crystal
next_token_skip_space_or_newline
of = parse_single_type
end_location = of.end_location
elsif exps.size == 0
raise "for empty arrays use '[] of ElementType'", line, column
end

ArrayLiteral.new(exps, of).at_end(end_location)
Expand Down

0 comments on commit 4b8159c

Please sign in to comment.