Skip to content

Commit

Permalink
Merge pull request #63 from pocke/support-_-for-method-name
Browse files Browse the repository at this point in the history
Allow underscore for method name with receiver
  • Loading branch information
soutaro authored Apr 5, 2019
2 parents 9ac65fd + c02a853 commit d4edd76
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/querly/pattern/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ keyword: LIDENT | UIDENT
constant: UIDENT { result = [val[0]] }
| UIDENT COLONCOLON constant { result = [val[0]] + val[2] }

send: LIDENT block { result = val[1] != nil ? Expr::Send.new(receiver: nil, name: val[0], args: Argument::AnySeq.new, block: val[1]) : Expr::Vcall.new(name: val[0]) }
send: LIDENT block { result = val[1] != nil ? Expr::Send.new(receiver: nil, name: val[0], block: val[1]) : Expr::Vcall.new(name: val[0]) }
| UIDENT block { result = Expr::Send.new(receiver: nil, name: val[0], block: val[1]) }
| method_name { result = Expr::Send.new(receiver: nil, name: val[0], block: nil) }
| method_name_or_ident LPAREN args RPAREN block { result = Expr::Send.new(receiver: nil,
Expand All @@ -83,18 +83,15 @@ send: LIDENT block { result = val[1] != nil ? Expr::Send.new(receiver: nil, name
name: val[1],
args: Argument::AnySeq.new,
block: val[2]) }
| receiver method_name_or_ident block { result = Expr::Send.new(receiver: val[0],
name: val[1],
args: Argument::AnySeq.new,
block: val[2]) }
| receiver method_name_or_ident LPAREN args RPAREN block { result = Expr::Send.new(receiver: val[0],
name: val[1],
args: val[3],
block: val[5]) }
| receiver method_name_or_ident LPAREN args RPAREN block { result = Expr::Send.new(receiver: val[0],
name: val[1],
args: val[3],
block: val[5]) }
| receiver UNDERBAR block { result = Expr::Send.new(receiver: val[0], name: /.+/, block: val[2]) }
| receiver UNDERBAR LPAREN args RPAREN block { result = Expr::Send.new(receiver: val[0],
name: /.+/,
args: val[3],
block: val[5]) }

receiver: expr DOT { result = val[0] }
| expr DOTDOTDOT { result = Expr::ReceiverContext.new(receiver: val[0]) }
Expand Down
24 changes: 24 additions & 0 deletions test/pattern_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ def test_send
assert_equal [:F], parse_expr("_.F").name
end

def test_any_method
recv = E::Vcall.new(name: :a)
assert_equal E::Send.new(receiver: recv,
name: /.+/,
args: A::AnySeq.new,
block: nil), parse_expr("a._")
assert_equal E::Send.new(receiver: recv,
name: /.+/,
args: A::AnySeq.new,
block: true), parse_expr("a._{}")
assert_equal E::Send.new(receiver: recv,
name: /.+/,
args: A::AnySeq.new,
block: false), parse_expr("a._!{}")
assert_equal E::Send.new(receiver: recv,
name: /.+/,
args: nil,
block: nil), parse_expr("a._()")
assert_equal E::Send.new(receiver: recv,
name: /.+/,
args: A::Expr.new(expr: E::Vcall.new(name: :b), tail: nil),
block: nil), parse_expr("a._(b)")
end

def test_method_name
assert_equal [:f!], parse_expr("f!()").name
assert_equal [:f=], parse_expr("f=(3)").name
Expand Down
18 changes: 18 additions & 0 deletions test/pattern_test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ def test_call_with_any_receiver
assert_equal ruby("bar.foo"), nodes.first
end

def test_call_any_method
nodes = query_pattern("foo._", "foo; foo.bar; foo.baz")
assert_equal 2, nodes.size
assert_equal [ruby("foo.bar"), ruby("foo.baz")], nodes
end

def test_call_any_method_with_args
nodes = query_pattern("foo._(baz)", "foo.bar(baz); foo.bar(bar)")
assert_equal 1, nodes.size
assert_equal ruby("foo.bar(baz)"), nodes.first
end

def test_call_any_method_with_block
nodes = query_pattern("foo._{}", "foo.bar; foo.baz{}")
assert_equal 1, nodes.size
assert_equal ruby("foo.baz{}"), nodes.first
end

def test_vcall
# Vcall pattern matches with local variable
nodes = query_pattern("foo", "foo = 1; foo.bar")
Expand Down

0 comments on commit d4edd76

Please sign in to comment.