Skip to content

Commit

Permalink
Keyword arguments in [] operators. Fixes #3918
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jan 20, 2017
1 parent 6952e76 commit 26a4c97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
4 changes: 4 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Expand Up @@ -340,6 +340,10 @@ 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.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[{1}]", Call.new("x".call, "[]", TupleLiteral.new([1.int32] of ASTNode))
it_parses "x[+ 1]", Call.new("x".call, "[]", Call.new(1.int32, "+"))

it_parses "foo(a: 1, &block)", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)], block_arg: "block".call)
it_parses "foo a: 1, &block", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)], block_arg: "block".call)
it_parses "foo a: b(1) do\nend", Call.new(nil, "foo", named_args: [NamedArgument.new("a", Call.new(nil, "b", 1.int32))], block: Block.new)
Expand Down
33 changes: 13 additions & 20 deletions src/compiler/crystal/syntax/parser.cr
Expand Up @@ -692,23 +692,16 @@ module Crystal

column_number = @token.column_number
next_token_skip_space_or_newline
args = [] of ASTNode
while true
args << parse_single_arg
skip_space_or_newline
case @token.type
when :","
next_token_skip_space_or_newline
if @token.type == :"]"
next_token
break
end
when :"]"
next_token
break
else
unexpected_token
end
call_args = preserve_stop_on_do { parse_call_args_space_consumed check_plus_and_minus: false, allow_curly: true, end_token: :"]" }
skip_space_or_newline
check :"]"
next_token

if call_args
args = call_args.args
block = call_args.block
block_arg = call_args.block_arg
named_args = call_args.named_args
end

if @token.type == :"?"
Expand All @@ -719,7 +712,7 @@ module Crystal
skip_space
end

atomic = Call.new(atomic, method_name, args, name_column_number: column_number).at(location)
atomic = Call.new(atomic, method_name, args: (args || [] of ASTNode), block: block, block_arg: block_arg, named_args: named_args, name_column_number: column_number).at(location)
atomic.name_size = 0 if atomic.is_a?(Call)
atomic
else
Expand Down Expand Up @@ -3890,7 +3883,7 @@ module Crystal
end
end

def parse_call_args_space_consumed(check_plus_and_minus = true, allow_curly = false, control = false)
def parse_call_args_space_consumed(check_plus_and_minus = true, allow_curly = false, control = false, end_token = :")")
if @token.keyword?(:end) && !next_comes_colon_space?
return nil
end
Expand Down Expand Up @@ -3942,7 +3935,7 @@ module Crystal
# never to `return`.
@stop_on_do = true unless control

while @token.type != :NEWLINE && @token.type != :";" && @token.type != :EOF && @token.type != :")" && @token.type != :":" && !end_token?
while @token.type != :NEWLINE && @token.type != :";" && @token.type != :EOF && @token.type != end_token && @token.type != :":" && !end_token?
if call_block_arg_follows?
return parse_call_block_arg(args, false)
end
Expand Down

0 comments on commit 26a4c97

Please sign in to comment.