Skip to content

Commit

Permalink
Merge 726c2eb into 2a878a2
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Jan 18, 2021
2 parents 2a878a2 + 726c2eb commit 6f390e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ def inspect(indent=0)
def to_ast
self
end

# Converts `self` to an Array where the first element is the type as a Symbol,
# and subsequent elements are the same representation of its children.
# and subsequent elements are the same representation of its children.
#
# @return [Array<Symbol, [...Array]>]
def to_sexp_array
Expand All @@ -246,6 +246,14 @@ def to_sexp_array
[type, *children_sexp_arrs]
end

# Enables matching for Node, where type is the first element
# and the children are remaining items.
#
# @return [Array]
def deconstruct
[type, *children]
end

protected

# Returns `@type` with all underscores replaced by dashes. This allows
Expand Down
18 changes: 17 additions & 1 deletion test/test_ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def initialize(*)
it 'should return self in to_ast' do
@node.to_ast.should.be.identical_to @node
end

it 'should produce to_sexp_array correctly' do
AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).to_sexp_array.should.equal [:a, :sym, [1, 2]]
AST::Node.new(:a, [ :sym,
Expand Down Expand Up @@ -175,6 +175,22 @@ def obj.children
rescue SyntaxError
# Running on 1.8, ignore.
end

begin
eval <<-CODE
it 'should be matchable' do
baz = s(:baz, s(:bar, 1), 2)
r = case baz
in [:baz, [:bar, val], Integer] then val
else
:no_match
end
r.should.equal 1
end
CODE
rescue SyntaxError
# Running on < 2.7, ignore.
end
end

describe AST::Processor do
Expand Down

0 comments on commit 6f390e5

Please sign in to comment.