Skip to content

Commit

Permalink
Parser: comma is required between block args (#7343)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and sdogruyol committed Jan 29, 2019
1 parent 34619bf commit fb39000
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 11 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ module Crystal
it_parses "foo { 1 }", Call.new(nil, "foo", block: Block.new(body: 1.int32))
it_parses "foo { |a| 1 }", Call.new(nil, "foo", block: Block.new(["a".var], 1.int32))
it_parses "foo { |a, b| 1 }", Call.new(nil, "foo", block: Block.new(["a".var, "b".var], 1.int32))
it_parses "foo { |a, b, | 1 }", Call.new(nil, "foo", block: Block.new(["a".var, "b".var], 1.int32))
it_parses "1.foo do; 1; end", Call.new(1.int32, "foo", block: Block.new(body: 1.int32))
it_parses "a b() {}", Call.new(nil, "a", Call.new(nil, "b", block: Block.new))

Expand All @@ -591,6 +592,16 @@ module Crystal
"c".var,
] of ASTNode)))

it_parses "foo { |(_, c, )| c }", Call.new(nil, "foo",
block: Block.new(["__arg0".var],
Expressions.new([
Assign.new("c".var, Call.new("__arg0".var, "[]", 1.int32)),
"c".var,
] of ASTNode)))

assert_syntax_error "foo { |a b| }", "expecting ',' or '|', not b"
assert_syntax_error "foo { |(a b)| }", "expecting ',' or ')', not b"

it_parses "1 ? 2 : 3", If.new(1.int32, 2.int32, 3.int32)
it_parses "1 ? a : b", If.new(1.int32, "a".call, "b".call)
it_parses "1 ? a : b ? c : 3", If.new(1.int32, "a".call, If.new("b".call, "c".call, 3.int32))
Expand Down
21 changes: 15 additions & 6 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4014,7 +4014,7 @@ module Crystal
next_token_skip_space
if @token.type == :"|"
next_token_skip_space_or_newline
while @token.type != :"|"
while true
if @token.type == :"*"
if splat_index
raise "splat block argument already specified", @token
Expand Down Expand Up @@ -4064,12 +4064,14 @@ module Crystal
end

next_token_skip_space_or_newline
if @token.type == :","
case @token.type
when :","
next_token_skip_space_or_newline
end

if @token.type == :")"
break if @token.type == :")"
when :")"
break
else
raise "expecting ',' or ')', not #{@token}", @token
end

i += 1
Expand All @@ -4084,8 +4086,15 @@ module Crystal
block_args << var

next_token_skip_space_or_newline
if @token.type == :","

case @token.type
when :","
next_token_skip_space_or_newline
break if @token.type == :"|"
when :"|"
break
else
raise "expecting ',' or '|', not #{@token}", @token
end

arg_index += 1
Expand Down

0 comments on commit fb39000

Please sign in to comment.