jievro-ast-visitor 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba46c8025f0a193bae74b31d80c59dc589f0c892
4
+ data.tar.gz: b93b8187c12e522844755314396e39ba965ced05
5
+ SHA512:
6
+ metadata.gz: fba46b3f142cd8a439992d06c9c0fa0265011638da4df2967ec98be2248aebb9f7866244f4dfdeb8dda31dd7084ac2c386c7673a202102b7f6dc7ab2e2f7f2db
7
+ data.tar.gz: 6b72a0e1502da98d82d9de81c707bffec55d963f1387c34e1ca3fd97b569010ea89e53b27afa8e74d1f4a8686eff4053581c5f9ca7181ea8eddfe0a0efab85f4
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /vendor/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+
3
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jievro-ast-visitor (0.0.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.4.2)
11
+ rspec (3.2.0)
12
+ rspec-core (~> 3.2.0)
13
+ rspec-expectations (~> 3.2.0)
14
+ rspec-mocks (~> 3.2.0)
15
+ rspec-core (3.2.3)
16
+ rspec-support (~> 3.2.0)
17
+ rspec-expectations (3.2.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.2.0)
20
+ rspec-mocks (3.2.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.2.0)
23
+ rspec-support (3.2.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.6)
30
+ jievro-ast-visitor!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.2)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Stefano Azzalini
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # AstVisitor
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:test) do |t|
6
+ t.ruby_opts = %w(-I lib -I spec)
7
+ t.rspec_opts = '--color --format progress'
8
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'ast_visitor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'jievro-ast-visitor'
8
+ spec.version = Jievro::AstVisitor::VERSION
9
+ spec.authors = ['Stefano Azzalini']
10
+ spec.email = 'hi@jievro.io'
11
+ spec.homepage = 'http://jievro.io'
12
+ spec.summary = %q{jievro AST visitor}
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.required_ruby_version = '~> 2.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.2'
25
+ end
@@ -0,0 +1,33 @@
1
+ module Jievro
2
+ module AstVisitor
3
+ class Visitor
4
+ def visit(ast, preorder = nil, postorder = nil)
5
+ visit_node(ast, nil, nil, nil, preorder, postorder)
6
+ end
7
+
8
+ private
9
+
10
+ def visit_node(node, parent, property, index, preorder, postorder)
11
+
12
+ unless preorder.nil?
13
+ preorder.call(node, parent, property, index)
14
+ end
15
+
16
+ node.keys.each { |p|
17
+ child = node[p]
18
+ if child.kind_of?(Array)
19
+ child.each_with_index { |e, i|
20
+ visit_node(e, node, p, i, preorder, postorder)
21
+ }
22
+ elsif child.kind_of?(Hash)
23
+ visit_node(child, node, p, nil, preorder, postorder)
24
+ end
25
+ }
26
+
27
+ unless postorder.nil?
28
+ postorder.call(node, parent, property, index)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module Jievro
2
+ module AstVisitor
3
+ VERSION = '0.0.3'
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'ast_visitor/ast_visitor'
2
+ require 'ast_visitor/version'
@@ -0,0 +1,77 @@
1
+ require 'rspec'
2
+ require 'ast_visitor/ast_visitor'
3
+
4
+ describe 'AstVisitor' do
5
+
6
+ before(:each) do
7
+
8
+ @visitor = Jievro::AstVisitor::Visitor.new
9
+
10
+ @ast = {
11
+ k1: 'v1',
12
+ k2: {
13
+ k3: 'v3',
14
+ k4: {
15
+ # left empty
16
+ },
17
+ k5: {
18
+ k6: [
19
+ {
20
+ k7: 'v7',
21
+ k8: 'v8'
22
+ },
23
+ {
24
+ k10: 'v10'
25
+ },
26
+ {
27
+ k11: {
28
+ k12: 'v12'
29
+ }
30
+ }
31
+ ]
32
+ }
33
+ }
34
+ }
35
+
36
+ @hooked = []
37
+ @hook = lambda { |node, parent, property, index|
38
+ @hooked.push [node.keys, parent.nil? ? nil : parent.keys, property, index]
39
+ }
40
+ end
41
+
42
+ it 'should visit in preorder' do
43
+
44
+ @visitor.visit(@ast, @hook)
45
+
46
+ expected = [
47
+ [[:k1, :k2], nil, nil, nil],
48
+ [[:k3, :k4, :k5], [:k1, :k2], :k2, nil],
49
+ [[], [:k3, :k4, :k5], :k4, nil],
50
+ [[:k6], [:k3, :k4, :k5], :k5, nil],
51
+ [[:k7, :k8], [:k6], :k6, 0],
52
+ [[:k10], [:k6], :k6, 1],
53
+ [[:k11], [:k6], :k6, 2],
54
+ [[:k12], [:k11], :k11, nil]
55
+ ]
56
+
57
+ expect(@hooked).to match(expected)
58
+ end
59
+
60
+ it 'should visit in postorder' do
61
+
62
+ @visitor.visit(@ast, nil, @hook)
63
+
64
+ expected = [
65
+ [[], [:k3, :k4, :k5], :k4, nil],
66
+ [[:k7, :k8], [:k6], :k6, 0],
67
+ [[:k10], [:k6], :k6, 1],
68
+ [[:k12], [:k11], :k11, nil],
69
+ [[:k11], [:k6], :k6, 2],
70
+ [[:k6], [:k3, :k4, :k5], :k5, nil],
71
+ [[:k3, :k4, :k5], [:k1, :k2], :k2, nil],
72
+ [[:k1, :k2], nil, nil, nil]
73
+ ]
74
+
75
+ expect(@hooked).to match(expected)
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jievro-ast-visitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Stefano Azzalini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description:
56
+ email: hi@jievro.io
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - ".travis.yml"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - ast_visitor.gemspec
69
+ - lib/ast_visitor.rb
70
+ - lib/ast_visitor/ast_visitor.rb
71
+ - lib/ast_visitor/version.rb
72
+ - spec/ast_visitor_spec.rb
73
+ homepage: http://jievro.io
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '2.0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: jievro AST visitor
97
+ test_files:
98
+ - spec/ast_visitor_spec.rb