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: refactor 'foo.%` call parsing #10351

Merged
merged 1 commit into from
Feb 14, 2021
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
4 changes: 4 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ describe Crystal::Formatter do
assert_format "foo(1, ) do\nend", "foo(1) do\nend"
assert_format "foo {;1}", "foo { 1 }"
assert_format "foo {;;1}", "foo { 1 }"
assert_format "foo.%(bar)"
assert_format "foo.% bar"
assert_format "foo.bar(&.%(baz))"
assert_format "foo.bar(&.% baz)"

assert_format "foo.bar\n.baz", "foo.bar\n .baz"
assert_format "foo.bar.baz\n.qux", "foo.bar.baz\n .qux"
Expand Down
25 changes: 10 additions & 15 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -637,23 +637,18 @@ module Crystal

@wants_regex = false

if current_char == '%'
next_char
@token.type = :"%"
@token.column_number += 1
skip_space_or_newline
else
next_token_skip_space_or_newline
@wants_def_or_macro_name = true
next_token_skip_space_or_newline
@wants_def_or_macro_name = false

if @token.type == :INSTANCE_VAR
ivar_name = @token.value.to_s
end_location = token_end_location
next_token_skip_space
if @token.type == :INSTANCE_VAR
ivar_name = @token.value.to_s
end_location = token_end_location
next_token_skip_space

atomic = ReadInstanceVar.new(atomic, ivar_name).at(location)
atomic.end_location = end_location
next
end
atomic = ReadInstanceVar.new(atomic, ivar_name).at(location)
atomic.end_location = end_location
next
end

check AtomicWithMethodCheck
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2555,7 +2555,9 @@ module Crystal
return false
end

@lexer.wants_def_or_macro_name = true
next_token
@lexer.wants_def_or_macro_name = false
skip_space
if (@token.type == :NEWLINE) || @wrote_newline
base_indent = @indent + 2
Expand Down Expand Up @@ -2973,8 +2975,10 @@ module Crystal
write " " if needs_space
write_token :"&"
skip_space_or_newline
write_token :"."
skip_space_or_newline
write :"."
@lexer.wants_def_or_macro_name = true
next_token_skip_space_or_newline
@lexer.wants_def_or_macro_name = false

body = node.body
case body
Expand Down