Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: allow call &.@var #7754

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,9 @@ describe Crystal::Formatter do
assert_format "foo &.[]=(1, 2)"
assert_format "foo &.[]=( 1, 2 )", "foo &.[]=(1, 2)"

assert_format "foo &.@bar"
assert_format "foo(&.@bar)"

assert_format "foo.[]"
assert_format "foo.[1]"
assert_format "foo.[] = 1"
Expand Down
1 change: 1 addition & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ module Crystal
it_parses "foo(&.responds_to?(:foo))", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], RespondsTo.new(Var.new("__arg0"), "foo")))
it_parses "foo &.each {\n}", Call.new(nil, "foo", block: Block.new(["__arg0".var], Call.new("__arg0".var, "each", block: Block.new)))
it_parses "foo &.each do\nend", Call.new(nil, "foo", block: Block.new(["__arg0".var], Call.new("__arg0".var, "each", block: Block.new)))
it_parses "foo &.@bar", Call.new(nil, "foo", block: Block.new(["__arg0".var], ReadInstanceVar.new("__arg0".var, "@bar")))

it_parses "foo(&.as(T))", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Cast.new(Var.new("__arg0"), "T".path)))
it_parses "foo(&.as(T).bar)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Cast.new(Var.new("__arg0"), "T".path), "bar")))
Expand Down
151 changes: 83 additions & 68 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1425,79 +1425,19 @@ module Crystal
skip_space
else
next_token_skip_space
end

location = @token.location

check AtomicWithMethodCheck

if @token.value == :is_a?
call = parse_is_a(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :as
call = parse_as(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :as?
call = parse_as?(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :responds_to?
call = parse_responds_to(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :nil?
call = parse_nil?(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.type == :"["
call = parse_atomic_method_suffix obj, location

if @token.type == :"=" && call.is_a?(Call)
next_token_skip_space
exp = parse_op_assign
call.name = "#{call.name}="
call.args << exp
end
else
# At this point we want to attach the "do" to the next call
old_stop_on_do = @stop_on_do
@stop_on_do = false
call = parse_var_or_call(force_call: true).at(location)

if call.is_a?(Call)
call.obj = obj
else
raise "BUG: #{call} should be a call"
end

call = call.as(Call)

if @token.type == :"="
next_token_skip_space
if @token.type == :"("
next_token_skip_space
exp = parse_op_assign
check :")"
next_token_skip_space
call.name = "#{call.name}="
call.args = [exp] of ASTNode
call = parse_atomic_method_suffix call, location
else
exp = parse_op_assign
call.name = "#{call.name}="
call.args = [exp] of ASTNode
end
else
call = parse_atomic_method_suffix call, location
if @token.type == :INSTANCE_VAR
ivar_name = @token.value.to_s
end_location = token_end_location
next_token

if @token.type == :"=" && call.is_a?(Call) && call.name == "[]"
next_token_skip_space
exp = parse_op_assign
call.name = "#{call.name}="
call.args << exp
end
call = ReadInstanceVar.new(obj, ivar_name).at(location)
call.end_location = end_location
end

@stop_on_do = old_stop_on_do
end

call ||= parse_call_block_arg_after_dot(obj)

block = Block.new([Var.new(block_arg_name)], call).at(location)
else
block_arg = parse_op_assign
Expand All @@ -1516,6 +1456,81 @@ module Crystal
CallArgs.new args, block, block_arg, named_args, false, end_location, has_parentheses: check_paren
end

def parse_call_block_arg_after_dot(obj)
location = @token.location

check AtomicWithMethodCheck

if @token.value == :is_a?
call = parse_is_a(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :as
call = parse_as(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :as?
call = parse_as?(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :responds_to?
call = parse_responds_to(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.value == :nil?
call = parse_nil?(obj).at(location)
call = parse_atomic_method_suffix_special(call, location)
elsif @token.type == :"["
call = parse_atomic_method_suffix obj, location

if @token.type == :"=" && call.is_a?(Call)
next_token_skip_space
exp = parse_op_assign
call.name = "#{call.name}="
call.args << exp
end
else
# At this point we want to attach the "do" to the next call
old_stop_on_do = @stop_on_do
@stop_on_do = false
call = parse_var_or_call(force_call: true).at(location)

if call.is_a?(Call)
call.obj = obj
else
raise "BUG: #{call} should be a call"
end

call = call.as(Call)

if @token.type == :"="
next_token_skip_space
if @token.type == :"("
next_token_skip_space
exp = parse_op_assign
check :")"
next_token_skip_space
call.name = "#{call.name}="
call.args = [exp] of ASTNode
call = parse_atomic_method_suffix call, location
else
exp = parse_op_assign
call.name = "#{call.name}="
call.args = [exp] of ASTNode
end
else
call = parse_atomic_method_suffix call, location

if @token.type == :"=" && call.is_a?(Call) && call.name == "[]"
next_token_skip_space
exp = parse_op_assign
call.name = "#{call.name}="
call.args << exp
end
end

@stop_on_do = old_stop_on_do
end

call
end

def parse_class_def(is_abstract = false, is_struct = false, doc = nil)
@type_nest += 1

Expand Down
10 changes: 9 additions & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2809,8 +2809,16 @@ module Crystal
clear_object(body)
accept body
end
when ReadInstanceVar
if body.obj.is_a?(Var)
call = Call.new(nil, body.name)
accept call
else
clear_object(body)
accept body
end
else
raise "BUG: expected Call, IsA or RespondsTo as &. argument, at #{node.location}, not #{body.class}"
raise "BUG: unexpected node for &. argument, at #{node.location}, not #{body.class}"
end
end

Expand Down