rip-parser 0.1.1 → 0.1.2
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/bin/rspec +17 -0
- data/source/rip/parser/about.rb +1 -1
- data/source/rip/parser/node.rb +16 -12
- data/source/rip/parser/rules/expression.rb +2 -0
- data/source/rip/parser/utilities/normalizer.rb +13 -38
- data/spec/unit/rip/parser/node_spec.rb +12 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aafa69ef5c9c30357b11e2ac9860ba212e97cde9
|
4
|
+
data.tar.gz: 9819d13beb2ba122291d5832b7f1a4f4d355f9dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e9f90dd03e880efce9d5379991fd3ce392448203d38db6f11cc49a052a00a03ee70455224d9e0d6577032badbc9fe721eba4cdce704239a2aa03cb74af99e35
|
7
|
+
data.tar.gz: 66eecd03bfe5b04f926cba4645b59d10a8be60d8132c500c6b0dfd3e9069d37ba844b96638c01b7876f29b13b86fbd980e8ceb527d459e88a1a60932376e9711
|
data/bin/rspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rspec' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/source/rip/parser/about.rb
CHANGED
data/source/rip/parser/node.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
module Rip::Parser
|
2
2
|
class Node
|
3
|
-
include Enumerable
|
4
|
-
|
5
3
|
attr_reader :location
|
4
|
+
attr_reader :parent
|
6
5
|
attr_reader :type
|
7
6
|
attr_reader :extra
|
8
7
|
|
9
|
-
def initialize(location:, type:, **extra)
|
8
|
+
def initialize(location:, parent: nil, type:, **extra)
|
10
9
|
@location = location
|
10
|
+
@parent = parent
|
11
11
|
@type = type
|
12
12
|
@extra = extra.inject({}) do |memo, (key, value)|
|
13
|
-
memo.merge(key => self.class.try_convert(value))
|
13
|
+
memo.merge(key => self.class.try_convert(value, self))
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -26,10 +26,6 @@ module Rip::Parser
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def each(&block)
|
30
|
-
to_h.each(&block)
|
31
|
-
end
|
32
|
-
|
33
29
|
def key?(key)
|
34
30
|
extra.key?(key.to_sym)
|
35
31
|
end
|
@@ -38,6 +34,10 @@ module Rip::Parser
|
|
38
34
|
extra.keys
|
39
35
|
end
|
40
36
|
|
37
|
+
def values
|
38
|
+
extra.values
|
39
|
+
end
|
40
|
+
|
41
41
|
def length
|
42
42
|
location.length
|
43
43
|
end
|
@@ -74,7 +74,7 @@ module Rip::Parser
|
|
74
74
|
[ key, _value ]
|
75
75
|
end.to_h
|
76
76
|
|
77
|
-
callback.call(merge(_extra))
|
77
|
+
callback.call(merge(_extra.merge(parent: parent)))
|
78
78
|
end
|
79
79
|
|
80
80
|
private
|
@@ -94,12 +94,16 @@ module Rip::Parser
|
|
94
94
|
key?(name) || name.to_s.end_with?('?')
|
95
95
|
end
|
96
96
|
|
97
|
-
def self.try_convert(value)
|
97
|
+
def self.try_convert(value, parent)
|
98
98
|
case value
|
99
99
|
when Array
|
100
|
-
value.map
|
100
|
+
value.map do |v|
|
101
|
+
try_convert(v, parent)
|
102
|
+
end
|
101
103
|
when Hash
|
102
|
-
new(value)
|
104
|
+
new(value.merge(parent: parent))
|
105
|
+
when self
|
106
|
+
new(value.to_h.merge(parent: parent))
|
103
107
|
else
|
104
108
|
value
|
105
109
|
end
|
@@ -8,45 +8,20 @@ module Rip::Parser::Utilities
|
|
8
8
|
|
9
9
|
def self.apply(origin, raw_tree)
|
10
10
|
new.apply(raw_tree, origin: origin).tap do |tree|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
validate_branches(branch, origin)
|
25
|
-
end
|
26
|
-
|
27
|
-
if tree.key?(:expression_chain)
|
28
|
-
shape = tree[:expression_chain].map do |key, value|
|
29
|
-
[ key, value.class ]
|
30
|
-
end.to_h
|
31
|
-
warn shape
|
32
|
-
raise Rip::Parser::NormalizeError.new('Unhandled expression_chain node', origin, tree)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.validate_leaves(tree, origin)
|
38
|
-
case tree
|
39
|
-
when Array
|
40
|
-
tree.each do |branch|
|
41
|
-
validate_leaves(branch, origin)
|
42
|
-
end
|
43
|
-
when Rip::Parser::Node
|
44
|
-
tree.each do |_, branch|
|
45
|
-
validate_leaves(branch, origin)
|
11
|
+
tree.traverse do |node|
|
12
|
+
case
|
13
|
+
when node.values.any? { |leaf| leaf.is_a?(Parslet::Slice) }
|
14
|
+
raise Rip::Parser::NormalizeError.new('Unconverted parslet slice', origin, node)
|
15
|
+
when node.key?(:expression_chain)
|
16
|
+
shape = node.expression_chain.map do |key, value|
|
17
|
+
[ key, value.class ]
|
18
|
+
end.to_h
|
19
|
+
warn shape
|
20
|
+
raise Rip::Parser::NormalizeError.new('Unhandled expression_chain node', origin, node)
|
21
|
+
else
|
22
|
+
node
|
23
|
+
end
|
46
24
|
end
|
47
|
-
when Parslet::Slice
|
48
|
-
warn tree
|
49
|
-
raise Rip::Parser::NormalizeError.new('Unconverted parslet slice', origin, tree)
|
50
25
|
end
|
51
26
|
end
|
52
27
|
|
@@ -27,14 +27,6 @@ RSpec.describe Rip::Parser::Node do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
describe '#each' do
|
31
|
-
specify do
|
32
|
-
expect do |x|
|
33
|
-
node.each(&x)
|
34
|
-
end.to yield_successive_args([ :location, location ], [ :type, :test ], [ :answer, 42 ])
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
30
|
describe '#key?' do
|
39
31
|
specify { expect(node.key?(:answer)).to be(true) }
|
40
32
|
specify { expect(node.key?(:foo)).to be(false) }
|
@@ -46,6 +38,10 @@ RSpec.describe Rip::Parser::Node do
|
|
46
38
|
specify { expect(node.keys).to match_array([ :answer ]) }
|
47
39
|
end
|
48
40
|
|
41
|
+
describe '#values' do
|
42
|
+
specify { expect(node.values).to match_array([ 42 ]) }
|
43
|
+
end
|
44
|
+
|
49
45
|
describe '#length' do
|
50
46
|
specify { expect(node.length).to eq(node.location.length) }
|
51
47
|
end
|
@@ -148,6 +144,7 @@ RSpec.describe Rip::Parser::Node do
|
|
148
144
|
|
149
145
|
context 'nesting' do
|
150
146
|
let(:root) { Rip::Parser::Node.new(location: location, type: :root, other: node) }
|
147
|
+
let(:root_with_hash) { Rip::Parser::Node.new(location: location, type: :root, other: node.to_h) }
|
151
148
|
|
152
149
|
specify { expect(root.other).to eq(node) }
|
153
150
|
specify { expect(root.other.answer).to eq(42) }
|
@@ -158,6 +155,11 @@ RSpec.describe Rip::Parser::Node do
|
|
158
155
|
specify { expect(root).to respond_to(:root?) }
|
159
156
|
specify { expect(root).to respond_to(:test?) }
|
160
157
|
|
158
|
+
specify { expect(root.parent).to be_nil }
|
159
|
+
specify { expect(root.other.parent).to eq(root) }
|
160
|
+
|
161
|
+
specify { expect(root_with_hash.other.parent).to eq(root) }
|
162
|
+
|
161
163
|
context 'nested collection' do
|
162
164
|
let(:nodes) do
|
163
165
|
[
|
@@ -175,6 +177,8 @@ RSpec.describe Rip::Parser::Node do
|
|
175
177
|
|
176
178
|
specify { expect(filtered.count).to eq(1) }
|
177
179
|
specify { expect(filtered.first.type).to eq(:special) }
|
180
|
+
|
181
|
+
specify { expect(root.others.sample.parent).to eq(root) }
|
178
182
|
end
|
179
183
|
end
|
180
184
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rip-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Ingram
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08
|
11
|
+
date: 2016-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parslet
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- bin/console
|
98
|
+
- bin/rspec
|
98
99
|
- bin/setup
|
99
100
|
- legacy/normalizer.rb
|
100
101
|
- legacy/parser_spec.rb
|