decode 0.17.3 → 0.18.2

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: a838fd5f42db321e8afe7627740faca8a0bce85e66016b1a9f5eac4c1b800e13
4
- data.tar.gz: 2d2e88c8dfbddaf2507f8b94ef7b3678247640ff48768dd8971afe4bca32a579
3
+ metadata.gz: 73f08a729f4aab1c447325886ac2f07a76c3e0b12a4ac9fadd0da81343fb24fe
4
+ data.tar.gz: 0fa743ec70c64044f12d56b1660fd8e2d2dfb5f6aeeb7ee49fbbe8325eca724d
5
5
  SHA512:
6
- metadata.gz: 2fd7f4dc09a37c14d87750e3cf12ed22f6352877a382ab6a08f8571b48d774761fa2deb6dcc7382bb43868361774816d018c086f3ae2e0b0908a89c8e9704154
7
- data.tar.gz: e6b60875db3b53417736599852a9354eabbdc08503b3dda594bd364df7c98fa84702303917f27c7850054ab40d0f5c25b018a0a9919bf07a9e7d922cd8b480fb
6
+ metadata.gz: 65c43bcc6ead3b9ab8b67a5a203910520e81837a8654dedb86f3fd92010803e9762fc200b14cb724d4862871fc8d4f87ee39d2aee2eaa5abbe23b34b2a1cea19
7
+ data.tar.gz: 8aa62d064fd628fd5a8fa16dfcd6464072e7d7b271be4c81f66b504905b725aec59892ffcbf8d30835328ce7f42903a44f2ffbd437f80bdc7c6bb89cbd19952e
checksums.yaml.gz.sig CHANGED
Binary file
@@ -18,6 +18,8 @@
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 'location'
22
+
21
23
  module Decode
22
24
  # A symbol with attached documentation.
23
25
  class Definition
@@ -38,10 +40,12 @@ module Decode
38
40
  @qualified_name = nil
39
41
  end
40
42
 
41
- def to_s
43
+ def inspect
42
44
  "\#<#{self.class} #{qualified_name}>"
43
45
  end
44
46
 
47
+ alias to_s inspect
48
+
45
49
  # The symbol name.
46
50
  # e.g. `:Decode`.
47
51
  # @attribute [Symbol]
@@ -168,5 +172,12 @@ module Decode
168
172
  @documentation ||= Documentation.new(@comments, @language)
169
173
  end
170
174
  end
175
+
176
+ # The location of the definition.
177
+ #
178
+ # @returns [Location | Nil] A {Location} instance if this definition has a location.
179
+ def location
180
+ nil
181
+ end
171
182
  end
172
183
  end
@@ -70,25 +70,25 @@ module Decode
70
70
  end
71
71
 
72
72
  # Parse the input yielding definitions.
73
- # @parameter input [File] The input file which contains the source code.
73
+ # @parameter source [Source] The input source file which contains the source code.
74
74
  # @yields {|definition| ...} Receives the definitions extracted from the source code.
75
75
  # @parameter definition [Definition] The source code definition including methods, classes, etc.
76
76
  # @returns [Enumerator(Segment)] If no block given.
77
- def definitions_for(input, &block)
77
+ def definitions_for(source, &block)
78
78
  if parser = self.parser
79
- parser.definitions_for(input, &block)
79
+ parser.definitions_for(source, &block)
80
80
  end
81
81
  end
82
82
 
83
83
  # Parse the input yielding segments.
84
84
  # Segments are constructed from a block of top level comments followed by a block of code.
85
- # @parameter input [File] The input file which contains the source code.
85
+ # @parameter source [Source] The input source file which contains the source code.
86
86
  # @yields {|segment| ...}
87
87
  # @parameter segment [Segment]
88
88
  # @returns [Enumerator(Segment)] If no block given.
89
- def segments_for(input, &block)
89
+ def segments_for(source, &block)
90
90
  if parser = self.parser
91
- parser.segments_for(input, &block)
91
+ parser.segments_for(source, &block)
92
92
  end
93
93
  end
94
94
  end
@@ -69,6 +69,10 @@ module Decode
69
69
 
70
70
  # A Ruby-specific singleton class.
71
71
  class Singleton < Definition
72
+ def nested_name
73
+ "::class"
74
+ end
75
+
72
76
  # A singleton class is a container for other definitions.
73
77
  # @returns [Boolean]
74
78
  def container?
@@ -82,14 +86,40 @@ module Decode
82
86
  end
83
87
 
84
88
  # The short form of the class.
85
- # e.g. `class << (self)`.
89
+ # e.g. `class << self`.
86
90
  def short_form
87
91
  "class << #{@name}"
88
92
  end
89
93
 
90
94
  # The long form is the same as the short form.
91
95
  alias long_form short_form
96
+
97
+ def path_name
98
+ [:class]
99
+ end
100
+
101
+ # The lexical scope as an array of names.
102
+ # e.g. `[:Decode, :Definition]`
103
+ # @returns [Array]
104
+ def path
105
+ if @path
106
+ # Cached version:
107
+ @path
108
+ else
109
+ @path = [*self.absolute_path, *self.path_name]
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ def absolute_path
116
+ if @parent
117
+ @parent.path
118
+ else
119
+ @name.to_s.split('::').map(&:to_sym)
120
+ end
121
+ end
92
122
  end
93
123
  end
94
124
  end
95
- end
125
+ end
@@ -59,7 +59,13 @@ module Decode
59
59
  return lines.join
60
60
  end
61
61
  end
62
+
63
+ def location
64
+ expression = @node.location.expression
65
+
66
+ Location.new(expression.source_buffer.name, expression.line)
67
+ end
62
68
  end
63
69
  end
64
70
  end
65
- end
71
+ end
@@ -42,9 +42,19 @@ module Decode
42
42
  @language = language
43
43
  end
44
44
 
45
+ # Parse the given source object, can be a string or a Source instance.
46
+ # @parameter source [String | Source] The source to parse.
47
+ private def parse_source(source)
48
+ if source.is_a?(Source)
49
+ ::Parser::CurrentRuby.parse_with_comments(source.read, source.path)
50
+ else
51
+ ::Parser::CurrentRuby.parse_with_comments(source)
52
+ end
53
+ end
54
+
45
55
  # Extract definitions from the given input file.
46
- def definitions_for(input, &block)
47
- top, comments = ::Parser::CurrentRuby.parse_with_comments(input)
56
+ def definitions_for(source, &block)
57
+ top, comments = self.parse_source(source)
48
58
 
49
59
  if top
50
60
  walk_definitions(top, comments, &block)
@@ -109,16 +119,18 @@ module Decode
109
119
  walk_definitions(children, comments, definition, &block)
110
120
  end
111
121
  when :sclass
112
- definition = Singleton.new(
113
- node, node.children[0],
114
- comments: extract_comments_for(node, comments),
115
- parent: parent, language: @language
116
- )
117
-
118
- yield definition
119
-
120
- if children = node.children[1]
121
- walk_definitions(children, comments, definition, &block)
122
+ if name = singleton_name_for(node.children[0])
123
+ definition = Singleton.new(
124
+ node, name,
125
+ comments: extract_comments_for(node, comments),
126
+ parent: parent, language: @language
127
+ )
128
+
129
+ yield definition
130
+
131
+ if children = node.children[1]
132
+ walk_definitions(children, comments, definition, &block)
133
+ end
122
134
  end
123
135
  when :def
124
136
  definition = Method.new(
@@ -219,6 +231,15 @@ module Decode
219
231
  end
220
232
  end
221
233
 
234
+ def singleton_name_for(node)
235
+ case node.type
236
+ when :const
237
+ nested_name_for(node)
238
+ when :self
239
+ :'self'
240
+ end
241
+ end
242
+
222
243
  KIND_ATTRIBUTE = /\A
223
244
  (@(?<kind>attribute)\s+(?<value>.*?))|
224
245
  (@define\s+(?<kind>)\s+(?<value>.*?))
@@ -253,8 +274,8 @@ module Decode
253
274
  end
254
275
 
255
276
  # Extract segments from the given input file.
256
- def segments_for(input, &block)
257
- top, comments = ::Parser::CurrentRuby.parse_with_comments(input)
277
+ def segments_for(source, &block)
278
+ top, comments = self.parse_source(source)
258
279
 
259
280
  # We delete any leading comments:
260
281
  line = 0
@@ -0,0 +1,27 @@
1
+ # Copyright, 2022, 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
+ class Location < Struct.new(:path, :line)
23
+ def to_s
24
+ "#{path}:#{line}"
25
+ end
26
+ end
27
+ end
data/lib/decode/source.rb CHANGED
@@ -50,7 +50,7 @@ module Decode
50
50
  def definitions(&block)
51
51
  return to_enum(:definitions) unless block_given?
52
52
 
53
- @language.definitions_for(self.read, &block)
53
+ @language.definitions_for(self, &block)
54
54
  end
55
55
 
56
56
  # Open the source file and read all segments.
@@ -60,7 +60,7 @@ module Decode
60
60
  def segments(&block)
61
61
  return to_enum(:segments) unless block_given?
62
62
 
63
- @language.segments_for(self.read, &block)
63
+ @language.segments_for(self, &block)
64
64
  end
65
65
 
66
66
  def code(index = nil, relative_to: nil)
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Decode
22
- VERSION = "0.17.3"
22
+ VERSION = "0.18.2"
23
23
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.3
4
+ version: 0.18.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -36,7 +36,7 @@ cert_chain:
36
36
  RAOsIl+HOBTb252nx1kIRN5hqQx272AJCbCjKx8egcUQKffFVVCI0nye09v5CK+a
37
37
  HiLJ8VOFx6w=
38
38
  -----END CERTIFICATE-----
39
- date: 2022-05-13 00:00:00.000000000 Z
39
+ date: 2022-07-08 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: parser
@@ -149,6 +149,7 @@ files:
149
149
  - lib/decode/language/ruby/reference.rb
150
150
  - lib/decode/language/ruby/segment.rb
151
151
  - lib/decode/languages.rb
152
+ - lib/decode/location.rb
152
153
  - lib/decode/scope.rb
153
154
  - lib/decode/segment.rb
154
155
  - lib/decode/source.rb
metadata.gz.sig CHANGED
Binary file