ast 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/ast.gemspec +1 -1
- data/lib/ast/node.rb +31 -26
- data/test/helper.rb +2 -2
- data/test/test_ast.rb +7 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa80b25cb36d090155a83e7e24dfb047701f681a
|
4
|
+
data.tar.gz: d93cac6b17f6b9b3f61df96a21fd5383cd53a6c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edd48c34d4d4bb2deb23ce4eda259a7e1454c84a39d2d8b2d90bd489c0cc31f993deea37068aea225ea83470ae9ebb2e0ef603a0efc284b69edc2690456d94d2
|
7
|
+
data.tar.gz: 684ac2d08942b7161f0cc30ffac40605138034907c77047752842dd659cfb817826f21fd8ffd8724a27442f3a5bb1a44fbbd825fb4baef914f7905b853f545e1
|
data/.travis.yml
CHANGED
data/ast.gemspec
CHANGED
data/lib/ast/node.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AST
|
2
4
|
# Node is an immutable class, instances of which represent abstract
|
3
5
|
# syntax tree nodes. It combines semantic information (i.e. anything
|
@@ -42,8 +44,17 @@ module AST
|
|
42
44
|
|
43
45
|
# Returns the children of this node.
|
44
46
|
# The returned value is frozen.
|
47
|
+
# The to_a alias is useful for decomposing nodes concisely.
|
48
|
+
# For example:
|
49
|
+
#
|
50
|
+
# node = s(:gasgn, :$foo, s(:integer, 1))
|
51
|
+
# var_name, value = *node
|
52
|
+
# p var_name # => :$foo
|
53
|
+
# p value # => (integer 1)
|
54
|
+
#
|
45
55
|
# @return [Array]
|
46
56
|
attr_reader :children
|
57
|
+
alias to_a children
|
47
58
|
|
48
59
|
# Returns the precomputed hash value for this node
|
49
60
|
# @return [Fixnum]
|
@@ -167,20 +178,6 @@ module AST
|
|
167
178
|
|
168
179
|
alias << append
|
169
180
|
|
170
|
-
# Returns {#children}. This is very useful in order to decompose nodes
|
171
|
-
# concisely. For example:
|
172
|
-
#
|
173
|
-
# node = s(:gasgn, :$foo, s(:integer, 1))
|
174
|
-
# s
|
175
|
-
# var_name, value = *node
|
176
|
-
# p var_name # => :$foo
|
177
|
-
# p value # => (integer 1)
|
178
|
-
#
|
179
|
-
# @return [Array]
|
180
|
-
def to_a
|
181
|
-
children
|
182
|
-
end
|
183
|
-
|
184
181
|
# Converts `self` to a pretty-printed s-expression.
|
185
182
|
#
|
186
183
|
# @param [Integer] indent Base indentation level.
|
@@ -189,12 +186,8 @@ module AST
|
|
189
186
|
indented = " " * indent
|
190
187
|
sexp = "#{indented}(#{fancy_type}"
|
191
188
|
|
192
|
-
|
193
|
-
child.is_a?(Node)
|
194
|
-
end || children.count
|
195
|
-
|
196
|
-
children.each_with_index do |child, idx|
|
197
|
-
if child.is_a?(Node) && idx >= first_node_child
|
189
|
+
children.each do |child|
|
190
|
+
if child.is_a?(Node)
|
198
191
|
sexp += "\n#{child.to_sexp(indent + 1)}"
|
199
192
|
else
|
200
193
|
sexp += " #{child.inspect}"
|
@@ -217,12 +210,8 @@ module AST
|
|
217
210
|
indented = " " * indent
|
218
211
|
sexp = "#{indented}s(:#{@type}"
|
219
212
|
|
220
|
-
|
221
|
-
child.is_a?(Node)
|
222
|
-
end || children.count
|
223
|
-
|
224
|
-
children.each_with_index do |child, idx|
|
225
|
-
if child.is_a?(Node) && idx >= first_node_child
|
213
|
+
children.each do |child|
|
214
|
+
if child.is_a?(Node)
|
226
215
|
sexp += ",\n#{child.inspect(indent + 1)}"
|
227
216
|
else
|
228
217
|
sexp += ", #{child.inspect}"
|
@@ -238,6 +227,22 @@ module AST
|
|
238
227
|
def to_ast
|
239
228
|
self
|
240
229
|
end
|
230
|
+
|
231
|
+
# Converts `self` to an Array where the first element is the type as a Symbol,
|
232
|
+
# and subsequent elements are the same representation of its children.
|
233
|
+
#
|
234
|
+
# @return [Array<Symbol, [...Array]>]
|
235
|
+
def to_sexp_array
|
236
|
+
children_sexp_arrs = children.map do |child|
|
237
|
+
if child.is_a?(Node)
|
238
|
+
child.to_sexp_array
|
239
|
+
else
|
240
|
+
child
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
[type, *children_sexp_arrs]
|
245
|
+
end
|
241
246
|
|
242
247
|
protected
|
243
248
|
|
data/test/helper.rb
CHANGED
@@ -5,10 +5,10 @@ require 'simplecov'
|
|
5
5
|
require 'coveralls'
|
6
6
|
|
7
7
|
SimpleCov.start do
|
8
|
-
self.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
self.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
9
9
|
SimpleCov::Formatter::HTMLFormatter,
|
10
10
|
Coveralls::SimpleCov::Formatter
|
11
|
-
]
|
11
|
+
])
|
12
12
|
|
13
13
|
# Exclude the testsuite itself.
|
14
14
|
add_filter "/test/"
|
data/test/test_ast.rb
CHANGED
@@ -88,6 +88,13 @@ describe AST::Node do
|
|
88
88
|
it 'should return self in to_ast' do
|
89
89
|
@node.to_ast.should.be.identical_to @node
|
90
90
|
end
|
91
|
+
|
92
|
+
it 'should produce to_sexp_array correctly' do
|
93
|
+
AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).to_sexp_array.should.equal [:a, :sym, [1, 2]]
|
94
|
+
AST::Node.new(:a, [ :sym,
|
95
|
+
AST::Node.new(:b, [ @node, @node ])
|
96
|
+
]).to_sexp_array.should.equal [:a, :sym, [:b, [:node, 0, 1], [:node, 0, 1]]]
|
97
|
+
end
|
91
98
|
|
92
99
|
it 'should only use type and children to compute #hash' do
|
93
100
|
@node.hash.should.equal([@node.type, @node.children, @node.class].hash)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- whitequark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -194,9 +194,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
196
|
rubyforge_project:
|
197
|
-
rubygems_version: 2.5.
|
197
|
+
rubygems_version: 2.5.2.2
|
198
198
|
signing_key:
|
199
199
|
specification_version: 4
|
200
200
|
summary: A library for working with Abstract Syntax Trees.
|
201
201
|
test_files: []
|
202
|
-
has_rdoc:
|