rip-parser 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/source/rip/parser/about.rb +1 -1
- data/source/rip/parser/node.rb +26 -20
- data/spec/unit/rip/parser/node_spec.rb +46 -23
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5b0926d469ebb45500d16f544f14c4e4d52363d
|
4
|
+
data.tar.gz: 2b8690bb493360e6fa32fd75f6e6761b12d3a2d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef663c6e862950bdcf357402ac070fb3c371fba4f6780518040023f91b4850c6e5fd85e67183a22d3779e4511838927f03d930cca8f87a8ec3705bc57343722a
|
7
|
+
data.tar.gz: fcfa4fd0a5580c5ddd308a53be565a4650b418fda6490c5b7ae71edcbc28ebdc0066512043fa3abc44673b2b460a4bcb6aeb71dc1df655a63afd8b9286846de7
|
data/source/rip/parser/about.rb
CHANGED
data/source/rip/parser/node.rb
CHANGED
@@ -46,20 +46,35 @@ module Rip::Parser
|
|
46
46
|
self.class.new(extra.merge(other.to_h).merge(location: location, type: other[:type] || type))
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
49
|
+
def to_h(include_location: true)
|
50
50
|
_extra = extra.map do |key, value|
|
51
|
-
[ key, self.class.
|
51
|
+
[ key, self.class.try_convert_to_h(value, include_location) ]
|
52
52
|
end.to_h
|
53
53
|
|
54
|
-
|
54
|
+
if include_location
|
55
|
+
{ location: location, type: type }
|
56
|
+
else
|
57
|
+
{ type: type }
|
58
|
+
end.merge(_extra)
|
55
59
|
end
|
56
60
|
|
57
|
-
def
|
61
|
+
def traverse(&callback)
|
58
62
|
_extra = extra.map do |key, value|
|
59
|
-
|
63
|
+
_value = case value
|
64
|
+
when Array
|
65
|
+
value.map do |v|
|
66
|
+
v.traverse(&callback)
|
67
|
+
end
|
68
|
+
when self.class
|
69
|
+
value.traverse(&callback)
|
70
|
+
else
|
71
|
+
value
|
72
|
+
end
|
73
|
+
|
74
|
+
[ key, _value ]
|
60
75
|
end.to_h
|
61
76
|
|
62
|
-
|
77
|
+
callback.call(merge(_extra))
|
63
78
|
end
|
64
79
|
|
65
80
|
private
|
@@ -90,23 +105,14 @@ module Rip::Parser
|
|
90
105
|
end
|
91
106
|
end
|
92
107
|
|
93
|
-
def self.try_convert_to_h(value)
|
94
|
-
case value
|
95
|
-
when Array
|
96
|
-
value.map(&method(:try_convert_to_h))
|
97
|
-
when self
|
98
|
-
value.to_h
|
99
|
-
else
|
100
|
-
value
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def self.try_convert_s_expression(value)
|
108
|
+
def self.try_convert_to_h(value, include_location)
|
105
109
|
case value
|
106
110
|
when Array
|
107
|
-
value.map
|
111
|
+
value.map do |v|
|
112
|
+
try_convert_to_h(v, include_location)
|
113
|
+
end
|
108
114
|
when self
|
109
|
-
value.
|
115
|
+
value.to_h(include_location: include_location)
|
110
116
|
else
|
111
117
|
value
|
112
118
|
end
|
@@ -62,29 +62,6 @@ RSpec.describe Rip::Parser::Node do
|
|
62
62
|
specify { expect(node.merge(other).to_h).to eq(location: location, type: :other_test, answer: 42, foo: :bar) }
|
63
63
|
end
|
64
64
|
|
65
|
-
describe '#s_expression' do
|
66
|
-
let(:tree) { Rip::Parser::Node.new(location: location, type: :root, children: [ node ]) }
|
67
|
-
|
68
|
-
let(:expected) do
|
69
|
-
{
|
70
|
-
type: :root,
|
71
|
-
children: [
|
72
|
-
{
|
73
|
-
type: :test,
|
74
|
-
answer: 42
|
75
|
-
}
|
76
|
-
]
|
77
|
-
}
|
78
|
-
end
|
79
|
-
|
80
|
-
specify { expect(tree.s_expression).to eq(expected) }
|
81
|
-
specify { expect(tree.s_expression).to be_a(Hash) }
|
82
|
-
|
83
|
-
specify { expect(tree.s_expression.keys).to eq([ :type, :children ]) }
|
84
|
-
|
85
|
-
specify { expect(tree.s_expression[:children].first).to be_a(Hash) }
|
86
|
-
end
|
87
|
-
|
88
65
|
describe '#to_h' do
|
89
66
|
specify { expect(node.to_h.keys).to eq([ :location, :type, :answer ]) }
|
90
67
|
|
@@ -106,6 +83,52 @@ RSpec.describe Rip::Parser::Node do
|
|
106
83
|
specify { expect(tree.to_h).to eq(expected) }
|
107
84
|
specify { expect(tree.to_h[:other]).to be_a(Hash) }
|
108
85
|
end
|
86
|
+
|
87
|
+
context 'include_location: false' do
|
88
|
+
let(:tree) { Rip::Parser::Node.new(location: location, type: :root, children: [ node ]) }
|
89
|
+
|
90
|
+
let(:expected) do
|
91
|
+
{
|
92
|
+
type: :root,
|
93
|
+
children: [
|
94
|
+
{
|
95
|
+
type: :test,
|
96
|
+
answer: 42
|
97
|
+
}
|
98
|
+
]
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
specify { expect(tree.to_h(include_location: false).keys).to eq([ :type, :children ]) }
|
103
|
+
|
104
|
+
specify { expect(tree.to_h(include_location: false)[:children].first.keys).to eq([ :type, :answer ]) }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#traverse' do
|
109
|
+
let(:node_counts) { Hash.new { |h, k| h[k] = 0 } }
|
110
|
+
|
111
|
+
let(:tree) do
|
112
|
+
Rip::Parser::Node.new(
|
113
|
+
location: location,
|
114
|
+
type: :root,
|
115
|
+
answer: 42,
|
116
|
+
nested: [
|
117
|
+
{ location: location, type: :leaf, data: :aaa },
|
118
|
+
{ location: location, type: :leaf, data: :bbb },
|
119
|
+
{ location: location, type: :leaf, data: :ccc }
|
120
|
+
],
|
121
|
+
aux: {
|
122
|
+
location: location,
|
123
|
+
type: :auxiliary,
|
124
|
+
whatever: :anything
|
125
|
+
}
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
before { tree.traverse { |node| node_counts[node.type] += 1 } }
|
130
|
+
|
131
|
+
specify { expect(node_counts).to eq(root: 1, leaf: 3, auxiliary: 1) }
|
109
132
|
end
|
110
133
|
|
111
134
|
describe '.new' do
|