Navigation Menu

Skip to content

Commit

Permalink
Parser: allow to use trailing comma after named arguments in index no…
Browse files Browse the repository at this point in the history
…tation (#4784)

Fix #4782

For example, current compiler cannot compile this:

    a = [1, 2, 3]
    a[
      index: 1,
    ]
    # Syntax error in foo.cr:4: expected named argument, not ]

Because the compiler does not allow comma after named arguments in index
notation. This fixes it so above is now working.
  • Loading branch information
makenowjust authored and RX14 committed Aug 16, 2017
1 parent dc877a8 commit 53c4431
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions spec/compiler/parser/parser_spec.cr
Expand Up @@ -341,6 +341,7 @@ describe "Parser" do
it_parses "x.foo a: 1, b: 2 ", Call.new("x".call, "foo", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])

it_parses "x[a: 1, b: 2]", Call.new("x".call, "[]", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "x[a: 1, b: 2,]", Call.new("x".call, "[]", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "x[{1}]", Call.new("x".call, "[]", TupleLiteral.new([1.int32] of ASTNode))
it_parses "x[+ 1]", Call.new("x".call, "[]", Call.new(1.int32, "+"))

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/syntax/parser.cr
Expand Up @@ -4089,7 +4089,7 @@ module Crystal
skip_space_or_newline if allow_newline
if @token.type == :","
next_token_skip_space_or_newline
if @token.type == :")" || @token.type == :"&"
if @token.type == :")" || @token.type == :"&" || @token.type == :"]"
break
end
else
Expand Down

0 comments on commit 53c4431

Please sign in to comment.