decode 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 519235a3a7f2db0117c0f725a101a602189d77d2a5be2154d925e7a76a7ade53
4
- data.tar.gz: 82e6a4a252e12d01b82054011bfcec2c5fb0c2bad3996467e388c9b93370d4cb
3
+ metadata.gz: c0f1e26c4b0aad982ae69ec4240f058d101c92f15b125f12e17b0a59e7f1a8f2
4
+ data.tar.gz: 552744c2f9ab8477a01eafd7042f5332325a779d94a29d8a4ec03df387a9174b
5
5
  SHA512:
6
- metadata.gz: 60c0fda6909175c3717d762aadccf3f7e8a7dd8582bc390fd1082c5b3d71d5e901224d9072700e7feb4e41052a7f45aa6619522e5a9e65cb7882fee26fef7081
7
- data.tar.gz: 413727720678c4b956539331bed678d7d8e18ebc8a23a10f6e6f4cec3bfa9cb0a2b849c3a48af23efdafa32340ad179553bb8e82ded52cc15e4f858e11637af1
6
+ metadata.gz: fe2a85001a162ca96082bd05788d7b57bae41af6aed065c72db26ef2c1d10d0b7c7e76836cf236a640bb7fdba84c66977e387e0fab449536c6915e3ec2413262
7
+ data.tar.gz: a55480e4354fd41c4bb6e5197180289ab1b29671f65988b67935f001016fd731d083adfbd9edcd079fb2a50894ed2a5ba0b728722c54f66f378609009e20033a
@@ -0,0 +1,69 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'symbol'
22
+
23
+ module Decode
24
+ class Definition < Symbol
25
+ def initialize(kind, name, comments, **options)
26
+ super(kind, name, **options)
27
+
28
+ @comments = comments
29
+ @documentation = nil
30
+ end
31
+
32
+ attr :comments
33
+
34
+ # A short form of the definition, e.g. `def short_form`.
35
+ # @return [String | nil]
36
+ def short_form
37
+ end
38
+
39
+ # A long form of the definition, e.g. `def initialize(kind, name, comments, **options)`.
40
+ # @return [String | nil]
41
+ def long_form
42
+ end
43
+
44
+ # The full text of the definition.
45
+ # @return [String | nil]
46
+ def text
47
+ end
48
+
49
+ # Whether this definition can contain nested definitions.
50
+ # @return [Boolean]
51
+ def container?
52
+ false
53
+ end
54
+
55
+ # Whether this represents a single entity to be documented (along with it's contents).
56
+ # @return [Boolean]
57
+ def nested?
58
+ container?
59
+ end
60
+
61
+ # An interface for accsssing the documentation of the definition.
62
+ # @return [Documentation | nil] A `Documentation` if this definition has comments.
63
+ def documentation
64
+ if @comments&.any?
65
+ @documentation ||= Documentation.new(@comments)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,63 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'definition'
22
+
23
+ module Decode
24
+ module Language
25
+ module Ruby
26
+ class Class < Definition
27
+ def container?
28
+ true
29
+ end
30
+
31
+ def short_form
32
+ "class #{@name}"
33
+ end
34
+
35
+ def long_form
36
+ if super_node = @node.children[1]
37
+ @node.location.keyword.join(
38
+ super_node.location.name
39
+ ).source
40
+ else
41
+ self.short_form
42
+ end
43
+ end
44
+ end
45
+
46
+ class Singleton < Definition
47
+ def container?
48
+ true
49
+ end
50
+
51
+ def nested?
52
+ false
53
+ end
54
+
55
+ def short_form
56
+ "class << #{@name}"
57
+ end
58
+
59
+ alias long_form short_form
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'definition'
22
+
23
+ module Decode
24
+ module Language
25
+ module Ruby
26
+ class Constant < Definition
27
+ def short_form
28
+ @node.location.name.source
29
+ end
30
+
31
+ def long_form
32
+ if @node.location.line == @node.location.last_line
33
+ @node.location.expression.source
34
+ elsif @node.children[2].type == :array
35
+ "#{@name} = [...]"
36
+ elsif @node.children[2].type == :hash
37
+ "#{@name} = {...}"
38
+ else
39
+ self.short_form
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,41 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative '../../definition'
22
+
23
+ module Decode
24
+ module Language
25
+ module Ruby
26
+ class Definition < Decode::Definition
27
+ def initialize(kind, name, comments, node, **options)
28
+ super(kind, name, comments, **options)
29
+
30
+ @node = node
31
+ end
32
+
33
+ attr :node
34
+
35
+ def text
36
+ @node.location.expression.source
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'method'
22
+
23
+ module Decode
24
+ module Language
25
+ module Ruby
26
+ class Function < Method
27
+ def arguments_node
28
+ if node = @node.children[2]
29
+ if node.location.expression
30
+ return node
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'definition'
22
+
23
+ module Decode
24
+ module Language
25
+ module Ruby
26
+ class Method < Definition
27
+ def short_form
28
+ @node.location.keyword.join(@node.location.name).source
29
+ end
30
+
31
+ def arguments_node
32
+ if node = @node.children[1]
33
+ if node.location.expression
34
+ return node
35
+ end
36
+ end
37
+ end
38
+
39
+ def long_form
40
+ if arguments_node = self.arguments_node
41
+ @node.location.keyword.join(
42
+ arguments_node.location.expression
43
+ ).source
44
+ else
45
+ self.short_form
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,39 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'definition'
22
+
23
+ module Decode
24
+ module Language
25
+ module Ruby
26
+ class Module < Definition
27
+ def container?
28
+ true
29
+ end
30
+
31
+ def short_form
32
+ "module #{@name}"
33
+ end
34
+
35
+ alias long_form short_form
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,143 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'parser/current'
22
+
23
+ require_relative 'class'
24
+ require_relative 'constant'
25
+ require_relative 'function'
26
+ require_relative 'method'
27
+ require_relative 'module'
28
+
29
+ module Decode
30
+ module Language
31
+ module Ruby
32
+ class Parser
33
+ def initialize(parser = ::Parser::CurrentRuby.new)
34
+ @parser = parser
35
+ end
36
+
37
+ def parse(input, &block)
38
+ buffer = ::Parser::Source::Buffer.new('(input)')
39
+ buffer.source = input.read
40
+
41
+ top, comments = @parser.parse_with_comments(buffer)
42
+
43
+ walk(top, comments, &block)
44
+ end
45
+
46
+ def extract_comments_for(node, comments)
47
+ prefix = []
48
+
49
+ while comment = comments.first
50
+ break if comment.location.line >= node.location.line
51
+
52
+ if last_comment = prefix.last
53
+ if last_comment.location.line != (comment.location.line - 1)
54
+ prefix.clear
55
+ end
56
+ end
57
+
58
+ prefix << comments.shift
59
+ end
60
+
61
+ # The last comment must butt up against the node:
62
+ if comment = prefix.last
63
+ if comment.location.line == (node.location.line - 1)
64
+ return prefix.map do |comment|
65
+ comment.text.sub(/\A\#\s?/, '')
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ # Walk over the syntax tree and extract relevant definitions with their associated comments.
72
+ def walk(node, comments, parent = nil, &block)
73
+ case node.type
74
+ when :begin
75
+ node.children.each do |child|
76
+ walk(child, comments, parent, &block)
77
+ end
78
+ when :module
79
+ definition = Module.new(
80
+ :module, node.children[0].children[1],
81
+ extract_comments_for(node, comments), node,
82
+ parent: parent, language: Ruby
83
+ )
84
+
85
+ yield definition
86
+
87
+ if children = node.children[1]
88
+ walk(children, comments, definition, &block)
89
+ end
90
+ when :class
91
+ definition = Class.new(
92
+ :class, node.children[0].children[1],
93
+ extract_comments_for(node, comments), node,
94
+ parent: parent, language: Ruby
95
+ )
96
+
97
+ yield definition
98
+
99
+ if children = node.children[2]
100
+ walk(children, comments, definition, &block)
101
+ end
102
+ when :sclass
103
+ definition = Singleton.new(
104
+ :class, node.children[0],
105
+ extract_comments_for(node, comments), node,
106
+ parent: parent, language: Ruby
107
+ )
108
+
109
+ yield definition
110
+
111
+ if children = node.children[1]
112
+ walk(children, comments, definition, &block)
113
+ end
114
+ when :def
115
+ definition = Method.new(
116
+ :def, node.children[0],
117
+ extract_comments_for(node, comments), node,
118
+ parent: parent, language: Ruby
119
+ )
120
+
121
+ yield definition
122
+ when :defs
123
+ definition = Function.new(
124
+ :defs, node.children[1],
125
+ extract_comments_for(node, comments), node,
126
+ parent: parent, language: Ruby
127
+ )
128
+
129
+ yield definition
130
+ when :casgn
131
+ definition = Constant.new(
132
+ :constant, node.children[1],
133
+ extract_comments_for(node, comments), node,
134
+ parent: parent, language: Ruby
135
+ )
136
+
137
+ yield definition
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,76 @@
1
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Decode
22
+ module Language
23
+ module Ruby
24
+ class Reference
25
+ KIND = {
26
+ ':' => :def,
27
+ '.' => :defs,
28
+ }.freeze
29
+
30
+ def initialize(value)
31
+ @value = value
32
+
33
+ @path = nil
34
+ @kind = nil
35
+ end
36
+
37
+ def absolute?
38
+ @value.start_with?('::')
39
+ end
40
+
41
+ METHOD = /\A(?<scope>.*?)?(?<kind>:|\.)(?<name>.+?)\z/
42
+
43
+ def path
44
+ if @path.nil?
45
+ @path = @value.split(/::/)
46
+
47
+ if last = @path.pop
48
+ if match = last.match(METHOD)
49
+ @kind = KIND[match[:kind]]
50
+
51
+ if scope = match[:scope]
52
+ @path << scope
53
+ end
54
+
55
+ @path << match[:name]
56
+ else
57
+ @path << last
58
+ end
59
+ end
60
+
61
+ @path = @path.map(&:to_sym)
62
+ @path.freeze
63
+ end
64
+
65
+ return @path
66
+ end
67
+
68
+ def kind
69
+ self.path
70
+
71
+ return @kind
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -18,13 +18,16 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
- require_relative '../symbol'
22
-
23
- require 'parser/current'
21
+ require_relative 'ruby/reference'
22
+ require_relative 'ruby/parser'
24
23
 
25
24
  module Decode
26
25
  module Language
27
- class Ruby
26
+ module Ruby
27
+ def self.parse(input, &block)
28
+ Parser.new.parse(input, &block)
29
+ end
30
+
28
31
  # The symbol which is used to separate the specified definition from the parent scope.
29
32
  PREFIX = {
30
33
  class: '::',
@@ -34,58 +37,7 @@ module Decode
34
37
  defs: '.',
35
38
  }.freeze
36
39
 
37
- KIND = {
38
- ':' => :def,
39
- '.' => :defs,
40
- }.freeze
41
-
42
- class Reference
43
- def initialize(value)
44
- @value = value
45
-
46
- @path = nil
47
- @kind = nil
48
- end
49
-
50
- def absolute?
51
- @value.start_with?('::')
52
- end
53
-
54
- METHOD = /\A(?<scope>.*?)?(?<kind>:|\.)(?<name>.+?)\z/
55
-
56
- def path
57
- if @path.nil?
58
- @path = @value.split(/::/)
59
-
60
- if last = @path.pop
61
- if match = last.match(METHOD)
62
- @kind = KIND[match[:kind]]
63
-
64
- if scope = match[:scope]
65
- @path << scope
66
- end
67
-
68
- @path << match[:name]
69
- else
70
- @path << last
71
- end
72
- end
73
-
74
- @path = @path.map(&:to_sym)
75
- @path.freeze
76
- end
77
-
78
- return @path
79
- end
80
-
81
- def kind
82
- self.path
83
-
84
- return @kind
85
- end
86
- end
87
-
88
- def join(symbols, absolute = true)
40
+ def self.join(symbols, absolute = true)
89
41
  buffer = String.new
90
42
 
91
43
  symbols.each do |symbol|
@@ -100,104 +52,6 @@ module Decode
100
52
 
101
53
  return buffer
102
54
  end
103
-
104
- def parse(input, &block)
105
- parser = ::Parser::CurrentRuby.new
106
-
107
- buffer = ::Parser::Source::Buffer.new('(input)')
108
- buffer.source = input.read
109
-
110
- top, comments = parser.parse_with_comments(buffer)
111
-
112
- walk(top, comments, &block)
113
- end
114
-
115
- def extract_comments_for(node, comments)
116
- prefix = []
117
-
118
- while comment = comments.first
119
- break if comment.location.line >= node.location.line
120
-
121
- if last_comment = prefix.last
122
- if last_comment.location.line != (comment.location.line - 1)
123
- prefix.clear
124
- end
125
- end
126
-
127
- prefix << comments.shift
128
- end
129
-
130
- # The last comment must butt up against the node:
131
- if comment = prefix.last
132
- if comment.location.line == (node.location.line - 1)
133
- return prefix.map do |comment|
134
- comment.text.sub(/\A\#\s?/, '')
135
- end
136
- end
137
- end
138
- end
139
-
140
- # Walk over the syntax tree and extract relevant definitions with their associated comments.
141
- def walk(node, comments, parent = nil, &block)
142
- case node.type
143
- when :begin
144
- node.children.each do |child|
145
- walk(child, comments, parent, &block)
146
- end
147
- when :class
148
- definition = Definition.new(
149
- :class, node.children[0].children[1],
150
- node, extract_comments_for(node, comments),
151
- parent: parent, language: self
152
- )
153
-
154
- yield definition
155
-
156
- walk(node.children[2], comments, definition, &block)
157
- when :module
158
- definition = Definition.new(
159
- :module, node.children[0].children[1],
160
- node, extract_comments_for(node, comments),
161
- parent: parent, language: self
162
- )
163
-
164
- yield definition
165
-
166
- walk(node.children[1], comments, definition, &block)
167
- when :def
168
- definition = Definition.new(
169
- :def, node.children[0],
170
- node, extract_comments_for(node, comments),
171
- parent: parent, language: self
172
- )
173
-
174
- yield definition
175
-
176
- # if body = node.children[2]
177
- # walk(body, comments, definition, &block)
178
- # end
179
- when :defs
180
- definition = Definition.new(
181
- :defs, node.children[1],
182
- node, extract_comments_for(node, comments),
183
- parent: parent, language: self
184
- )
185
-
186
- yield definition
187
-
188
- # if body = node.children[2]
189
- # walk(body, comments, definition, &block)
190
- # end
191
- when :casgn
192
- definition = Definition.new(
193
- :constant, node.children[1],
194
- node, extract_comments_for(node, comments),
195
- parent: parent, language: self
196
- )
197
-
198
- yield definition
199
- end
200
- end
201
55
  end
202
56
  end
203
57
  end
data/lib/decode/source.rb CHANGED
@@ -24,7 +24,7 @@ module Decode
24
24
  class Source
25
25
  def initialize(path, language = nil)
26
26
  @path = path
27
- @language = language || Language.detect(path).new
27
+ @language = language || Language.detect(path)
28
28
  end
29
29
 
30
30
  def parse(&block)
data/lib/decode/symbol.rb CHANGED
@@ -65,26 +65,4 @@ module Decode
65
65
  self.path.map(&:name)
66
66
  end
67
67
  end
68
-
69
- class Definition < Symbol
70
- def initialize(kind, name, node, comments, **options)
71
- super(kind, name, **options)
72
-
73
- @node = node
74
- @comments = comments
75
- @documentation = nil
76
- end
77
-
78
- def text
79
- @node.location.expression.source
80
- end
81
-
82
- attr :comments
83
-
84
- def documentation
85
- if @comments&.any?
86
- @documentation ||= Documentation.new(@comments)
87
- end
88
- end
89
- end
90
68
  end
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Decode
22
- VERSION = "0.4.0"
22
+ VERSION = "0.5.0"
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
11
+ date: 2020-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -122,10 +122,19 @@ files:
122
122
  - decode.gemspec
123
123
  - gems.rb
124
124
  - lib/decode.rb
125
+ - lib/decode/definition.rb
125
126
  - lib/decode/documentation.rb
126
127
  - lib/decode/index.rb
127
128
  - lib/decode/language.rb
128
129
  - lib/decode/language/ruby.rb
130
+ - lib/decode/language/ruby/class.rb
131
+ - lib/decode/language/ruby/constant.rb
132
+ - lib/decode/language/ruby/definition.rb
133
+ - lib/decode/language/ruby/function.rb
134
+ - lib/decode/language/ruby/method.rb
135
+ - lib/decode/language/ruby/module.rb
136
+ - lib/decode/language/ruby/parser.rb
137
+ - lib/decode/language/ruby/reference.rb
129
138
  - lib/decode/source.rb
130
139
  - lib/decode/symbol.rb
131
140
  - lib/decode/trie.rb