ast-navigator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2be55ad8331e21211a9c0cc73920f14750fb2c2
4
+ data.tar.gz: b165047e2a37bfef73a6ffe5ea37023cc05f96e3
5
+ SHA512:
6
+ metadata.gz: 88f2ea7c6a193b92960471b556a28c237835b1c9dc07e663e86e66fd085ea114bab5e9c7756ede8a63832250896543fd9ce2d1f2eece9728a6b9bb1cd4fe3a52
7
+ data.tar.gz: d7c12d814e4ad802c707fbed91b9c973c8d9a7cd8390a33556fb4f6b0261851d2dfe6c67dc5259e3fb0e0e9b879384437829e8b1415e3363e4f0d52097cf2da4
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ast-navigator.gemspec
4
+ gemspec
@@ -0,0 +1,53 @@
1
+ # AST::Navigator
2
+
3
+ `AST::Navigator` provides some utility functions for `AST::Node` objects, including *parent(node)*, *ancestors(node)*, *siblings(node)*.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ast-navigator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ast-navigator
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ require "ast/navigator"
25
+
26
+ navigator = AST::Navigator.new()
27
+
28
+ # Add root node to navigator
29
+ navigator.add_root_node(node)
30
+
31
+ # Returns parent node or nil
32
+ navigator.parent(node.children[0])
33
+
34
+ # Returns array of ancestor nodes
35
+ navigator.ancestors(node.children[0].children[1].children[3])
36
+
37
+ # Yields child node of given node, non AST::Node children will be ignored
38
+ navigator.each_child(node) do ... end
39
+
40
+ # Yields sibling nodes of given node, non AST::Node siblings will be ignored
41
+ navigator.each_sibling(node) do ... end
42
+ ```
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/soutaro/ast-navigator.
53
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ast/navigator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ast-navigator"
8
+ spec.version = AST::Navigator::VERSION
9
+ spec.authors = ["Soutaro Matsumoto"]
10
+ spec.email = ["matsumoto@soutaro.com"]
11
+
12
+ spec.summary = %q{Utility functions for AST::Node}
13
+ spec.description = %q{Utility functions for AST::Node}
14
+ spec.homepage = "https://github.com/soutaro/ast-navigator"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "ast", "~> 2.3.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ast/navigator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,102 @@
1
+ require "ast/navigator/version"
2
+
3
+ require "set"
4
+
5
+ module AST
6
+ class Navigator
7
+ attr_reader :parents
8
+ attr_reader :roots
9
+
10
+ def initialize()
11
+ @parents = {}
12
+ @roots = {}
13
+ end
14
+
15
+ def add_root_node(node)
16
+ roots[node.__id__] = node
17
+
18
+ node.children.each do |child|
19
+ if child.is_a?(AST::Node)
20
+ add_node(child, node)
21
+ end
22
+ end
23
+ end
24
+
25
+ def parent(node)
26
+ parents[node.__id__]
27
+ end
28
+
29
+ def ancestors(node)
30
+ ancestors = []
31
+
32
+ current_node = node
33
+ while current_node
34
+ ancestors.unshift current_node
35
+ current_node = parent(current_node)
36
+ end
37
+
38
+ ancestors
39
+ end
40
+
41
+ def each_subnode(node, &block)
42
+ if block_given?
43
+ each_child(node) do |child|
44
+ yield child
45
+ each_subnode child, &block
46
+ end
47
+ else
48
+ enum_for :each_subnode, node
49
+ end
50
+ end
51
+
52
+ def root?(node)
53
+ roots.key?(node.__id__)
54
+ end
55
+
56
+ def member?(node)
57
+ parent(node) || root?(node)
58
+ end
59
+
60
+ def each_root(&block)
61
+ if block_given?
62
+ roots.values.each(&block)
63
+ else
64
+ enum_for :each_root
65
+ end
66
+ end
67
+
68
+ def each_child(node, &block)
69
+ if block_given?
70
+ node.children.select {|child| child.is_a?(AST::Node) }.each(&block)
71
+ else
72
+ enum_for :each_child, node
73
+ end
74
+ end
75
+
76
+ def each_sibling(node, &block)
77
+ if block_given?
78
+ parent = parent(node)
79
+
80
+ if parent
81
+ each_child(parent, &block)
82
+ else
83
+ []
84
+ end
85
+ else
86
+ enum_for :each_sibling, node
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def add_node(node, parent)
93
+ parents[node.__id__] = parent
94
+
95
+ node.children.each do |child|
96
+ if child.is_a?(AST::Node)
97
+ add_node(child, node)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,5 @@
1
+ module AST
2
+ class Navigator
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ast-navigator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Soutaro Matsumoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ast
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Utility functions for AST::Node
70
+ email:
71
+ - matsumoto@soutaro.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - ast-navigator.gemspec
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/ast/navigator.rb
85
+ - lib/ast/navigator/version.rb
86
+ homepage: https://github.com/soutaro/ast-navigator
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.8
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Utility functions for AST::Node
109
+ test_files: []