Skip to content

Commit

Permalink
Merge branch 'master' into parser_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption committed Feb 23, 2025
2 parents 359941d + 0c18a2d commit ac2b7cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,51 @@ module Crystal
it_parses "1.x; foo do\nend", [Call.new(1.int32, "x"), Call.new("foo", block: Block.new)] of ASTNode
it_parses "x = 1; foo.bar x do\nend", [Assign.new("x".var, 1.int32), Call.new("foo".call, "bar", ["x".var] of ASTNode, Block.new)]

describe "block associativity" do
describe "surprise one: binds to second-to-the-right (#15303)" do
it_parses "a b c d e do; end", Call.new("a", Call.new("b", Call.new("c", Call.new("d", Call.new("e"), block: Block.new))))
it_parses "a b c d e {}", Call.new("a", Call.new("b", Call.new("c", Call.new("d", Call.new("e", block: Block.new)))))
end

describe "surprise two: block chains bind right-to-left starting from second-to-the-right (#15303)" do
it_parses "a b c d e do 1 end do 2 end { 3 } do 4 end", Call.new("a",
Call.new("b",
Call.new("c",
Call.new("d",
Call.new("e"),
block: Block.new(body: 1.int32)
),
block: Block.new(body: 2.int32)
),
block: Block.new(body: 3.int32)
),
block: Block.new(body: 4.int32))
it_parses "a b c d e { 1 } { 2 } do 3 end { 4 }", Call.new("a",
Call.new("b",
Call.new("c",
Call.new("d",
Call.new("e",
block: Block.new(body: 1.int32)
),
block: Block.new(body: 2.int32)
),
block: Block.new(body: 3.int32)
),
block: Block.new(body: 4.int32))
)
end

describe "surprise three: arguments affect block binding (#15303)" do
it_parses "a b c d e 1, 2 do; end", Call.new("a", Call.new("b", Call.new("c", Call.new("d", Call.new("e", 1.int32, 2.int32, block: Block.new)))))
it_parses "a b c d e 1, 2 {}", Call.new("a", Call.new("b", Call.new("c", Call.new("d", Call.new("e", 1.int32, 2.int32, block: Block.new)))))
end

describe "surprise four: parentheses affect block binding (#15303)" do
it_parses "a 1, (2), b do end", Call.new("a", 1.int32, Expressions.new([2.int32] of ASTNode), Call.new("b", block: Block.new))
it_parses "a 1, (2), b {}", Call.new("a", 1.int32, Expressions.new([2.int32] of ASTNode), Call.new("b", block: Block.new))
end
end

it_parses "foo do\n//\nend", Call.new("foo", [] of ASTNode, Block.new(body: regex("")))
it_parses "foo x do\n//\nend", Call.new("foo", ["x".call] of ASTNode, Block.new(body: regex("")))
it_parses "foo(x) do\n//\nend", Call.new("foo", ["x".call] of ASTNode, Block.new(body: regex("")))
Expand Down Expand Up @@ -2514,6 +2559,10 @@ module Crystal
assert_end_location "1 ensure 2"
assert_end_location "foo.bar= *baz"
assert_end_location %("hello "\\\n"world"), line_number: 2, column_number: 7
assert_end_location "foo(&.bar)"
assert_end_location "foo &.bar"
assert_end_location "foo(&bar)"
assert_end_location "foo &bar"
end

assert_syntax_error %({"a" : 1}), "space not allowed between named argument name and ':'"
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1587,12 +1587,12 @@ module Crystal
call ||= parse_call_block_arg_after_dot(obj)

block = Block.new([Var.new(block_arg_name)], call).at(location)
end_location = call.end_location
else
block_arg = parse_op_assign
end_location = block_arg.end_location
end

end_location = token_end_location

if check_paren
skip_space_or_newline
check :OP_RPAREN
Expand Down

0 comments on commit ac2b7cb

Please sign in to comment.