Skip to content

Commit 26a4c97

Browse files
author
Ary Borenszweig
committedJan 20, 2017
Keyword arguments in [] operators. Fixes #3918
1 parent 6952e76 commit 26a4c97

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed
 

‎spec/compiler/parser/parser_spec.cr

+4
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ describe "Parser" do
340340
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)])
341341
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)])
342342

343+
it_parses "x[a: 1, b: 2]", Call.new("x".call, "[]", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
344+
it_parses "x[{1}]", Call.new("x".call, "[]", TupleLiteral.new([1.int32] of ASTNode))
345+
it_parses "x[+ 1]", Call.new("x".call, "[]", Call.new(1.int32, "+"))
346+
343347
it_parses "foo(a: 1, &block)", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)], block_arg: "block".call)
344348
it_parses "foo a: 1, &block", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)], block_arg: "block".call)
345349
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)

‎src/compiler/crystal/syntax/parser.cr

+13-20
Original file line numberDiff line numberDiff line change
@@ -692,23 +692,16 @@ module Crystal
692692

693693
column_number = @token.column_number
694694
next_token_skip_space_or_newline
695-
args = [] of ASTNode
696-
while true
697-
args << parse_single_arg
698-
skip_space_or_newline
699-
case @token.type
700-
when :","
701-
next_token_skip_space_or_newline
702-
if @token.type == :"]"
703-
next_token
704-
break
705-
end
706-
when :"]"
707-
next_token
708-
break
709-
else
710-
unexpected_token
711-
end
695+
call_args = preserve_stop_on_do { parse_call_args_space_consumed check_plus_and_minus: false, allow_curly: true, end_token: :"]" }
696+
skip_space_or_newline
697+
check :"]"
698+
next_token
699+
700+
if call_args
701+
args = call_args.args
702+
block = call_args.block
703+
block_arg = call_args.block_arg
704+
named_args = call_args.named_args
712705
end
713706

714707
if @token.type == :"?"
@@ -719,7 +712,7 @@ module Crystal
719712
skip_space
720713
end
721714

722-
atomic = Call.new(atomic, method_name, args, name_column_number: column_number).at(location)
715+
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)
723716
atomic.name_size = 0 if atomic.is_a?(Call)
724717
atomic
725718
else
@@ -3890,7 +3883,7 @@ module Crystal
38903883
end
38913884
end
38923885

3893-
def parse_call_args_space_consumed(check_plus_and_minus = true, allow_curly = false, control = false)
3886+
def parse_call_args_space_consumed(check_plus_and_minus = true, allow_curly = false, control = false, end_token = :")")
38943887
if @token.keyword?(:end) && !next_comes_colon_space?
38953888
return nil
38963889
end
@@ -3942,7 +3935,7 @@ module Crystal
39423935
# never to `return`.
39433936
@stop_on_do = true unless control
39443937

3945-
while @token.type != :NEWLINE && @token.type != :";" && @token.type != :EOF && @token.type != :")" && @token.type != :":" && !end_token?
3938+
while @token.type != :NEWLINE && @token.type != :";" && @token.type != :EOF && @token.type != end_token && @token.type != :":" && !end_token?
39463939
if call_block_arg_follows?
39473940
return parse_call_block_arg(args, false)
39483941
end

0 commit comments

Comments
 (0)
Please sign in to comment.