code_breaker 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +33 -0
- data/README.md +4 -6
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/code_breaker.gemspec +16 -16
- data/lib/code_breaker.rb +0 -1
- data/lib/code_breaker/parsable/assignments.rb +46 -36
- data/lib/code_breaker/parsable/data_types.rb +44 -37
- data/lib/code_breaker/parsable/keywords.rb +65 -72
- data/lib/code_breaker/parsable/language_elements.rb +28 -26
- data/lib/code_breaker/parsable/node.rb +20 -16
- data/lib/code_breaker/parsable/ranges.rb +8 -7
- data/lib/code_breaker/parsable/variable_types.rb +13 -11
- data/lib/code_breaker/parsable/wrappers.rb +6 -12
- data/lib/code_breaker/parser.rb +2 -3
- data/lib/code_breaker/version.rb +1 -1
- metadata +14 -29
- data/.travis.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 34e130c3a963be818c41a997843620e65a3e89aa4daa4aad2d0ffdb8aa380087
|
4
|
+
data.tar.gz: e55c8809962f90a39ff8b5ad73f7cb1b3b45d2572f8954578fefabfe8ba00051
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42ed1c04674bb3049df6c4a1805cda4e17594ca84f99d48ceb159a9d96580d0061bb9d4c774bb33b0f77dcdb106ab145c616aaaa2a456e0825f2ac3ae213e847
|
7
|
+
data.tar.gz: dbb5fe81fa8690056f738496add777f80ae6aab7ab3d36fac9834096cd9ded694bd8ebf744fbfb2926ca83b37039231d23b15679249a922f98a39d5f86932dd1
|
@@ -0,0 +1,33 @@
|
|
1
|
+
name: Ruby CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ main, develop ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ main, develop ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
strategy:
|
12
|
+
fail-fast: false
|
13
|
+
matrix:
|
14
|
+
os: [ ubuntu-latest, macos-latest, windows-latest ]
|
15
|
+
ruby: [ 2.5, 2.6, 2.7, ruby-head ]
|
16
|
+
runs-on: ${{ matrix.os }}
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- uses: ruby/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
- uses: actions/cache@v2
|
23
|
+
with:
|
24
|
+
path: vendor/bundle
|
25
|
+
key: ${{ matrix.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
26
|
+
restore-keys: |
|
27
|
+
${{ matrix.os }}-gems-
|
28
|
+
- name: Install dependencies
|
29
|
+
run: |
|
30
|
+
gem install bundler
|
31
|
+
bundle install --jobs 4 --retry 3
|
32
|
+
- name: Run Tests
|
33
|
+
run: bundle exec rake default
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# CodeBreaker
|
2
2
|
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://github.com/daigaku-ruby/code_breaker/workflows/Ruby%20CI/badge.svg)](https://github.com/daigaku-ruby/code_breaker/workflows/Ruby%20CI/badge.svg)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/code_breaker.svg)](http://badge.fury.io/rb/code_breaker)
|
5
5
|
|
6
6
|
CodeBreaker breaks a line of Ruby code into its receiver classes and the methods
|
@@ -13,7 +13,7 @@ sum = Rational(2, 3) + 4
|
|
13
13
|
```
|
14
14
|
|
15
15
|
By breaking down this line into the receiver classes and called methods you can
|
16
|
-
check e.g. whether a code snippet assigns the sum of a Rational and
|
16
|
+
check e.g. whether a code snippet assigns the sum of a Rational and an Integer to
|
17
17
|
a variable with the name `sum`.
|
18
18
|
|
19
19
|
Testing global code snippets might be important for testing the code of simple
|
@@ -44,7 +44,7 @@ require 'code_breaker'
|
|
44
44
|
|
45
45
|
code_snippet = 'crazy_number = Rational(3, 5) + 42 - Complex(2.3, 6.4) * 1.2'
|
46
46
|
CodeBreaker.parse(code_snippet)
|
47
|
-
# => {:lvasgn=>[:crazy_number, [Rational, :+,
|
47
|
+
# => {:lvasgn=>[:crazy_number, [Rational, :+, Integer, :-, Complex, :*, Float]]}
|
48
48
|
|
49
49
|
code_snippet = '"hello" + "World"'
|
50
50
|
CodeBreaker.parse(code_snippet)
|
@@ -79,10 +79,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
79
79
|
|
80
80
|
## Contributing
|
81
81
|
|
82
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/daigaku-ruby/code_breaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org)
|
83
|
-
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/daigaku-ruby/code_breaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant code of conduct](http://contributor-covenant.org/version/1/2/0).
|
84
83
|
|
85
84
|
## License
|
86
85
|
|
87
86
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
88
|
-
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'code_breaker'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "code_breaker"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/code_breaker.gemspec
CHANGED
@@ -4,33 +4,33 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'code_breaker/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'code_breaker'
|
8
8
|
spec.version = CodeBreaker::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Paul Götze']
|
10
|
+
spec.email = ['paul.christoph.goetze@gmail.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
12
|
+
spec.summary = 'Breaking a Ruby code snippet into a sequence of classes and their connecting methods.'
|
13
|
+
spec.description = 'Breaking a Ruby code snippet into a sequence of classes and their connecting methods.'
|
14
|
+
spec.homepage = 'https://github.com/daigaku-ruby/code_breaker'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
18
|
# delete this section to allow pushing this gem to any host.
|
19
19
|
if spec.respond_to?(:metadata)
|
20
|
-
spec.metadata['allowed_push_host'] =
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
21
|
else
|
22
|
-
raise
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
23
|
end
|
24
24
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
-
spec.bindir =
|
26
|
+
spec.bindir = 'exe'
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = [
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
spec.required_ruby_version = '~> 2.5'
|
29
30
|
|
30
|
-
spec.add_dependency
|
31
|
-
spec.add_dependency "activesupport", "~> 4"
|
31
|
+
spec.add_dependency 'parser', '~> 2.7'
|
32
32
|
|
33
|
-
spec.add_development_dependency
|
34
|
-
spec.add_development_dependency
|
35
|
-
spec.add_development_dependency
|
33
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
34
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.9'
|
36
36
|
end
|
data/lib/code_breaker.rb
CHANGED
@@ -1,50 +1,60 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module Assignments
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
alias :parse_ivasgn_node :parse_as_hash # instance variable assignment
|
13
|
-
alias :parse_cvasgn_node :parse_as_hash # class variable assignment
|
14
|
-
alias :parse_gvasgn_node :parse_as_hash # global variable assignment
|
15
|
-
alias :parse_op_asgn_node :parse_as_hash # operation assignment
|
16
|
-
alias :parse_or_asgn_node :parse_as_hash # or assignment
|
17
|
-
alias :parse_and_asgn_node :parse_as_hash # and assignment
|
18
|
-
|
19
|
-
# multiple assignment
|
20
|
-
def parse_masgn_node(node)
|
21
|
-
lhs, rhs = parse_children(node)
|
22
|
-
|
23
|
-
values = if rhs.kind_of?(Hash) && rhs.has_key?(:array)
|
24
|
-
rhs[:array]
|
25
|
-
else
|
26
|
-
[rhs] + (1...lhs.count).map { NilClass }
|
27
|
-
end
|
28
|
-
|
29
|
-
{ node.type => { lhs => values } }
|
30
|
-
end
|
6
|
+
# local variable assignment
|
7
|
+
alias_method :parse_lvasgn_node, :parse_as_hash
|
31
8
|
|
32
|
-
|
33
|
-
|
34
|
-
parse_children(node).map(&:values).flatten
|
35
|
-
end
|
9
|
+
# instance variable assignment
|
10
|
+
alias_method :parse_ivasgn_node, :parse_as_hash
|
36
11
|
|
37
|
-
|
38
|
-
|
39
|
-
name = node.children[1]
|
40
|
-
children = node.children[2]
|
41
|
-
value = children.nil? ? name : [name, parse(node.children[2])]
|
12
|
+
# class variable assignment
|
13
|
+
alias_method :parse_cvasgn_node, :parse_as_hash
|
42
14
|
|
43
|
-
|
44
|
-
|
15
|
+
# global variable assignment
|
16
|
+
alias_method :parse_gvasgn_node, :parse_as_hash
|
17
|
+
|
18
|
+
# operation assignment
|
19
|
+
alias_method :parse_op_asgn_node, :parse_as_hash
|
20
|
+
|
21
|
+
# or assignment
|
22
|
+
alias_method :parse_or_asgn_node, :parse_as_hash
|
23
|
+
|
24
|
+
# and assignment
|
25
|
+
alias_method :parse_and_asgn_node, :parse_as_hash
|
26
|
+
|
27
|
+
# multiple assignment
|
28
|
+
def parse_masgn_node(node)
|
29
|
+
lhs, rhs = parse_children(node)
|
30
|
+
values = multiple_assignment_values(lhs, rhs)
|
45
31
|
|
32
|
+
{ node.type => { lhs => values } }
|
46
33
|
end
|
47
34
|
|
35
|
+
# multiple left hand side
|
36
|
+
def parse_mlhs_node(node)
|
37
|
+
parse_children(node).map(&:values).flatten
|
38
|
+
end
|
39
|
+
|
40
|
+
# constant assignment
|
41
|
+
def parse_casgn_node(node)
|
42
|
+
name = node.children[1]
|
43
|
+
children = node.children[2]
|
44
|
+
value = children.nil? ? name : [name, parse(node.children[2])]
|
45
|
+
|
46
|
+
{ node.type => value }
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def multiple_assignment_values(lhs, rhs)
|
52
|
+
if rhs.is_a?(Hash) && rhs.key?(:array)
|
53
|
+
rhs[:array]
|
54
|
+
else
|
55
|
+
[rhs] + (1...lhs.count).map { NilClass }
|
56
|
+
end
|
57
|
+
end
|
48
58
|
end
|
49
59
|
end
|
50
60
|
end
|
@@ -1,57 +1,64 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module DataTypes
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
6
|
+
def parse_nil_node(node)
|
7
|
+
NilClass
|
8
|
+
end
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
def parse_true_node(node)
|
11
|
+
TrueClass
|
12
|
+
end
|
18
13
|
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
def parse_false_node(node)
|
15
|
+
FalseClass
|
16
|
+
end
|
22
17
|
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
def parse_str_node(node)
|
19
|
+
String
|
20
|
+
end
|
26
21
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
22
|
+
# interpolated executed string
|
23
|
+
def parse_xstr_node(node)
|
24
|
+
{ node.type => parse_children(node).first }
|
25
|
+
end
|
31
26
|
|
32
|
-
|
33
|
-
|
27
|
+
# interpolated string
|
28
|
+
def parse_dstr_node(node)
|
29
|
+
values = parse_as_hash(node)[node.type].map do |value|
|
30
|
+
if value.is_a?(Array)
|
31
|
+
value.flatten(1)
|
32
|
+
else
|
33
|
+
value
|
34
|
+
end
|
34
35
|
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
end
|
37
|
+
{ node.type => values }
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
def parse_sym_node(node)
|
41
|
+
Symbol
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
def parse_float_node(node)
|
45
|
+
Float
|
46
|
+
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
def parse_regexp_node(node)
|
49
|
+
Regexp
|
50
|
+
end
|
51
51
|
|
52
|
-
|
53
|
-
|
52
|
+
def parse_int_node(node)
|
53
|
+
node.children[0].class
|
54
54
|
end
|
55
|
+
|
56
|
+
def parse_pair_node(node)
|
57
|
+
{ parse(node.children[0]) => parse(node.children[1]) }
|
58
|
+
end
|
59
|
+
|
60
|
+
alias_method :parse_hash_node, :parse_as_hash
|
61
|
+
alias_method :parse_array_node, :parse_as_hash
|
55
62
|
end
|
56
63
|
end
|
57
64
|
end
|
@@ -1,102 +1,95 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module Keywords
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
alias :parse_resbody_node :parse_as_hash
|
6
|
+
alias_method :parse_or_node, :parse_as_hash
|
7
|
+
alias_method :parse_and_node, :parse_as_hash
|
8
|
+
alias_method :parse_def_node, :parse_as_hash
|
9
|
+
alias_method :parse_yield_node, :parse_as_hash
|
10
|
+
alias_method :parse_rescue_node, :parse_as_hash
|
11
|
+
alias_method :parse_resbody_node, :parse_as_hash
|
17
12
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
alias_method :parse_break_node, :parse_as_node_type
|
14
|
+
alias_method :parse_next_node, :parse_as_node_type
|
15
|
+
alias_method :parse_retry_node, :parse_as_node_type
|
16
|
+
alias_method :parse_self_node, :parse_as_node_type
|
22
17
|
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
def parse_loop_node(node)
|
19
|
+
condition = node.children[0]
|
20
|
+
body = node.children[1]
|
26
21
|
|
27
|
-
|
28
|
-
|
22
|
+
{ node.type => parse(condition), do: parse(body) }
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
25
|
+
alias_method :parse_while_node, :parse_loop_node
|
26
|
+
alias_method :parse_until_node, :parse_loop_node
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
def parse_for_node(node)
|
29
|
+
variable = node.children[0]
|
30
|
+
range = node.children[1]
|
31
|
+
body = node.children[2]
|
37
32
|
|
38
|
-
|
39
|
-
|
33
|
+
{ node.type => parse(variable), in: parse(range), do: parse(body) }
|
34
|
+
end
|
40
35
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
def parse_if_node(node)
|
37
|
+
condition = node.children[0]
|
38
|
+
if_body = node.children[1]
|
39
|
+
else_body = node.children[2]
|
45
40
|
|
46
|
-
|
47
|
-
|
41
|
+
clause = { node.type => parse(condition), then: parse(if_body) }
|
42
|
+
clause[:else] = parse(else_body) if else_body
|
48
43
|
|
49
|
-
|
50
|
-
|
44
|
+
clause
|
45
|
+
end
|
51
46
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
47
|
+
def parse_module_node(node)
|
48
|
+
name = parse(node.children[0])
|
49
|
+
body = node.children[1].nil? ? nil : parse(node.children[1])
|
50
|
+
value = body ? [name, body] : [name]
|
56
51
|
|
57
|
-
|
58
|
-
|
52
|
+
{ node.type => value }
|
53
|
+
end
|
59
54
|
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
def parse_return_node(node)
|
56
|
+
children = parse_children(node)
|
57
|
+
values = children.length == 1 ? children[0] : children
|
63
58
|
|
64
|
-
|
65
|
-
|
59
|
+
{ node.type => values }
|
60
|
+
end
|
66
61
|
|
67
|
-
|
68
|
-
|
62
|
+
def parse_kwbegin_node(node)
|
63
|
+
rescue_given = node.children.first.type == :rescue
|
69
64
|
|
70
|
-
|
71
|
-
|
65
|
+
if rescue_given
|
66
|
+
rescue_part = parse(node.children.first)[:rescue]
|
72
67
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
68
|
+
{
|
69
|
+
begin: rescue_part.first,
|
70
|
+
rescue: rescue_part.last[:resbody].first
|
71
|
+
}
|
72
|
+
else
|
73
|
+
{ begin: parse(node.children.last) }
|
80
74
|
end
|
75
|
+
end
|
81
76
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
77
|
+
def parse_case_node(node)
|
78
|
+
case_part = parse(node.children.first)
|
79
|
+
when_parts = node.children[1...-1].map { |child| parse(child) }
|
80
|
+
else_part = parse(node.children.last)
|
86
81
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
82
|
+
statement = { case: when_parts.unshift(case_part) }
|
83
|
+
statement[:case] << { else: else_part } if else_part
|
84
|
+
statement
|
85
|
+
end
|
91
86
|
|
92
|
-
|
93
|
-
|
94
|
-
|
87
|
+
def parse_when_node(node)
|
88
|
+
when_part = parse(node.children[0])
|
89
|
+
then_part = parse(node.children[1])
|
95
90
|
|
96
|
-
|
97
|
-
end
|
91
|
+
{ when: when_part, then: then_part }
|
98
92
|
end
|
99
|
-
|
100
93
|
end
|
101
94
|
end
|
102
95
|
end
|
@@ -1,34 +1,36 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module LanguageElements
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
6
|
+
alias_method :parse_block_node, :parse_as_hash
|
7
|
+
alias_method :parse_args_node, :parse_as_hash
|
8
|
+
alias_method :parse_arg_node, :parse_as_last_child_hash
|
9
|
+
alias_method :parse_blockarg_node, :parse_as_last_child_hash
|
10
|
+
alias_method :parse_restarg_node, :parse_as_last_child_hash
|
11
|
+
|
12
|
+
# optional argument
|
13
|
+
alias_method :parse_optarg_node, :parse_as_hash
|
14
|
+
|
15
|
+
# keyword argument
|
16
|
+
alias_method :parse_kwarg_node, :parse_as_last_child_hash
|
17
|
+
|
18
|
+
# optional keyword argument
|
19
|
+
alias_method :parse_kwoptarg_node, :parse_as_hash
|
20
|
+
|
21
|
+
# keyword rest argument
|
22
|
+
alias_method :parse_kwrestarg_node, :parse_as_last_child_hash
|
23
|
+
|
24
|
+
def parse_block_pass_node(node)
|
25
|
+
{ node.type => node.children.first.children.last }
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_splat_node(node)
|
29
|
+
children = parse_children(node).flatten(1)
|
30
|
+
values = children.length == 1 ? children[0] : children
|
31
|
+
|
32
|
+
{ node.type => values }
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
34
|
-
end
|
36
|
+
end
|
@@ -1,21 +1,22 @@
|
|
1
1
|
module CodeBreaker
|
2
2
|
module Parsable
|
3
3
|
module Node
|
4
|
-
|
5
4
|
def parse(node)
|
6
|
-
|
7
|
-
|
8
|
-
if node.kind_of?(Symbol)
|
5
|
+
if node.is_a?(Symbol)
|
9
6
|
node
|
7
|
+
elsif node.nil?
|
8
|
+
parse_nil_node(node)
|
10
9
|
else
|
11
10
|
send("parse_#{node.type}_node", node)
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
15
|
-
def parse_children(node)
|
16
|
-
node.children
|
17
|
-
|
18
|
-
|
14
|
+
def parse_children(node, compact: true)
|
15
|
+
children = node.children
|
16
|
+
children = children.compact if compact
|
17
|
+
|
18
|
+
children.each_with_object([]) do |child, nodes|
|
19
|
+
nodes << parse(child)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
@@ -32,21 +33,24 @@ module CodeBreaker
|
|
32
33
|
end
|
33
34
|
|
34
35
|
def method_missing(method, *args, &block)
|
35
|
-
|
36
|
+
matches = method.to_s.match(/^parse_(.+)_node$/)
|
37
|
+
node_type = matches ? matches.captures.first : []
|
36
38
|
|
37
39
|
if node_type.empty?
|
38
40
|
super
|
39
41
|
else
|
40
|
-
|
41
|
-
"Breaking the node type \"#{node_type}\" is not yet implemented.",
|
42
|
-
"You can open an issue on this in the project's Github repo under:",
|
43
|
-
"https://github.com/daigaku-ruby/code_breaker/issues/new\n"
|
44
|
-
].join("\n")
|
45
|
-
|
46
|
-
raise NotImplementedError, message
|
42
|
+
raise NotImplementedError, not_implemented_message(node_type)
|
47
43
|
end
|
48
44
|
end
|
49
45
|
|
46
|
+
def not_implemented_message(node_type)
|
47
|
+
[
|
48
|
+
%(Breaking the node type "#{node_type}" is not yet implemented.),
|
49
|
+
'You can open an issue on this in the project’s Github repo under:',
|
50
|
+
'https://github.com/daigaku-ruby/code_breaker/issues/new',
|
51
|
+
''
|
52
|
+
].join("\n")
|
53
|
+
end
|
50
54
|
end
|
51
55
|
end
|
52
56
|
end
|
@@ -1,15 +1,16 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module Ranges
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
# inclusive range a..b, a.., ..b
|
7
|
+
def parse_irange_node(node)
|
8
|
+
{ node.type => parse_children(node, compact: false) }
|
9
|
+
end
|
10
|
+
|
11
|
+
# exclusive range a...b, a..., ...b
|
12
|
+
def parse_erange_node(node)
|
13
|
+
{ node.type => parse_children(node, compact: false) }
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -1,19 +1,21 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module VariableTypes
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
6
|
+
alias_method :parse_const_node, :parse_as_last_child_hash
|
7
|
+
|
8
|
+
# local variable
|
9
|
+
alias_method :parse_lvar_node, :parse_as_last_child_hash
|
10
|
+
|
11
|
+
# instance variable
|
12
|
+
alias_method :parse_ivar_node, :parse_as_last_child_hash
|
13
|
+
|
14
|
+
# class variable
|
15
|
+
alias_method :parse_cvar_node, :parse_as_last_child_hash
|
16
|
+
|
17
|
+
# global variable
|
18
|
+
alias_method :parse_gvar_node, :parse_as_last_child_hash
|
17
19
|
end
|
18
20
|
end
|
19
21
|
end
|
@@ -1,23 +1,17 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module CodeBreaker
|
4
2
|
module Parsable
|
5
3
|
module Wrappers
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
8
4
|
include Parsable::Node
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
return node.children[1].to_s.constantize
|
14
|
-
end
|
15
|
-
|
16
|
-
parse_children(node).flatten(1)
|
6
|
+
def parse_send_node(node)
|
7
|
+
if [:Rational, :Complex].include?(node.children[1])
|
8
|
+
return Kernel.const_get(node.children[1].to_s)
|
17
9
|
end
|
18
10
|
|
19
|
-
|
11
|
+
parse_children(node).flatten(1)
|
20
12
|
end
|
13
|
+
|
14
|
+
alias_method :parse_begin_node, :parse_children
|
21
15
|
end
|
22
16
|
end
|
23
17
|
end
|
data/lib/code_breaker/parser.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'code_breaker/parsable'
|
2
2
|
require 'parser/current'
|
3
|
-
require 'active_support/inflector'
|
4
3
|
|
5
4
|
module CodeBreaker
|
6
5
|
class Parser
|
@@ -21,12 +20,12 @@ module CodeBreaker
|
|
21
20
|
def run
|
22
21
|
unless @output
|
23
22
|
ast = ::Parser::CurrentRuby.parse(input)
|
24
|
-
@output = parse(ast.loc.node)
|
23
|
+
@output = ast.nil? ? [] : parse(ast.loc.node)
|
25
24
|
end
|
26
25
|
|
27
26
|
@output
|
28
27
|
end
|
29
28
|
|
30
|
-
alias
|
29
|
+
alias output run
|
31
30
|
end
|
32
31
|
end
|
data/lib/code_breaker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_breaker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Götze
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -16,70 +16,56 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '2.
|
19
|
+
version: '2.7'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '2.
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: activesupport
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '4'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '4'
|
26
|
+
version: '2.7'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: bundler
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
44
30
|
requirements:
|
45
31
|
- - "~>"
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
33
|
+
version: '2.0'
|
48
34
|
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
38
|
- - "~>"
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
40
|
+
version: '2.0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rake
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
45
|
- - "~>"
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
47
|
+
version: '13.0'
|
62
48
|
type: :development
|
63
49
|
prerelease: false
|
64
50
|
version_requirements: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
52
|
- - "~>"
|
67
53
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
54
|
+
version: '13.0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: rspec
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
59
|
- - "~>"
|
74
60
|
- !ruby/object:Gem::Version
|
75
|
-
version: '3.
|
61
|
+
version: '3.9'
|
76
62
|
type: :development
|
77
63
|
prerelease: false
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
79
65
|
requirements:
|
80
66
|
- - "~>"
|
81
67
|
- !ruby/object:Gem::Version
|
82
|
-
version: '3.
|
68
|
+
version: '3.9'
|
83
69
|
description: Breaking a Ruby code snippet into a sequence of classes and their connecting
|
84
70
|
methods.
|
85
71
|
email:
|
@@ -88,9 +74,9 @@ executables: []
|
|
88
74
|
extensions: []
|
89
75
|
extra_rdoc_files: []
|
90
76
|
files:
|
77
|
+
- ".github/workflows/main.yml"
|
91
78
|
- ".gitignore"
|
92
79
|
- ".rspec"
|
93
|
-
- ".travis.yml"
|
94
80
|
- CODE_OF_CONDUCT.md
|
95
81
|
- Gemfile
|
96
82
|
- LICENSE.txt
|
@@ -122,17 +108,16 @@ require_paths:
|
|
122
108
|
- lib
|
123
109
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
110
|
requirements:
|
125
|
-
- - "
|
111
|
+
- - "~>"
|
126
112
|
- !ruby/object:Gem::Version
|
127
|
-
version: '
|
113
|
+
version: '2.5'
|
128
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
115
|
requirements:
|
130
116
|
- - ">="
|
131
117
|
- !ruby/object:Gem::Version
|
132
118
|
version: '0'
|
133
119
|
requirements: []
|
134
|
-
|
135
|
-
rubygems_version: 2.4.6
|
120
|
+
rubygems_version: 3.1.3
|
136
121
|
signing_key:
|
137
122
|
specification_version: 4
|
138
123
|
summary: Breaking a Ruby code snippet into a sequence of classes and their connecting
|
data/.travis.yml
DELETED