ruby_mod_kit 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -8
- data/lib/ruby_mod_kit/cli.rb +10 -1
- data/lib/ruby_mod_kit/core_ext/eval.rb +30 -0
- data/lib/ruby_mod_kit/core_ext/load.rb +66 -0
- data/lib/ruby_mod_kit/core_ext.rb +7 -0
- data/lib/ruby_mod_kit/memo/{class.rb → def_parent.rb} +5 -5
- data/lib/ruby_mod_kit/memo.rb +9 -9
- data/lib/ruby_mod_kit/mission/fix_parse_error.rb +12 -9
- data/lib/ruby_mod_kit/mission/overload.rb +1 -1
- data/lib/ruby_mod_kit/mission/type_attr.rb +9 -9
- data/lib/ruby_mod_kit/node/{class_node.rb → def_parent_node.rb} +5 -5
- data/lib/ruby_mod_kit/node.rb +6 -6
- data/lib/ruby_mod_kit/offset_diff.rb +12 -6
- data/lib/ruby_mod_kit/version.rb +1 -1
- data/lib/ruby_mod_kit.rb +26 -8
- data/ruby_mod_kit.gemspec +1 -6
- data/sig/generated/ruby_mod_kit/core_ext/eval.rbs +14 -0
- data/sig/generated/ruby_mod_kit/core_ext/load.rbs +30 -0
- data/sig/generated/ruby_mod_kit/memo/{class.rbs → def_parent.rbs} +5 -5
- data/sig/generated/ruby_mod_kit/memo.rbs +5 -5
- data/sig/generated/ruby_mod_kit/mission/fix_parse_error.rbs +2 -2
- data/sig/generated/ruby_mod_kit/node/{class_node.rbs → def_parent_node.rbs} +6 -6
- data/sig/generated/ruby_mod_kit/node.rbs +2 -2
- data/sig/generated/ruby_mod_kit/offset_diff.rbs +3 -1
- data/sig/generated/ruby_mod_kit.rbs +9 -2
- data/sig/thor.rbs +2 -0
- metadata +11 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83923be4714ddc71e6c09823f58468b46717815164235dc5437d495c62cd6b7c
|
4
|
+
data.tar.gz: d7a12ed80c113de570b7a1a30c39a0f31d13803c140c1b3c1fe946ec26dde484
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffbd1485194583c50a78287cbb779437d1bfe8991e31e4eca113b3ec0af2d06682e42ab652bed96b7bd9edd6595116c94ffac3c34693b4575696a5ba89e48e39
|
7
|
+
data.tar.gz: 8d5cd3e6989aa21225e9bdaeae4ab68658670bbbb727f5a6f965e3b5e83323a1d79b5debb79861b00fc141bb57f3630a678c5caa386583cb7a4fa0d8eefaaf79
|
data/README.md
CHANGED
@@ -20,20 +20,26 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
20
20
|
#### `transpile`
|
21
21
|
|
22
22
|
You can get transpiled ruby script from .rbm file by `ruby_mod_kit transpile` command.
|
23
|
+
`transpile` command outputs script to stdout by default. `--output` can change output target.
|
24
|
+
`--output=.rb` is a special pattern that means "Output the original script file name with the extension changed to .rb".
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
These below have the same meaning: "Output ruby script to stdout".
|
27
|
+
|
28
|
+
$ ruby_mod_kit transpile path/to/script.rbm
|
29
|
+
$ ruby_mod_kit transpile --output=- path/to/script.rbm
|
30
|
+
$ ruby_mod_kit transpile --output=/dev/stdout path/to/script.rbm
|
31
|
+
|
32
|
+
These below have the same meaning: "Output ruby script to `path/to/script.rb`".
|
33
|
+
|
34
|
+
$ ruby_mod_kit transpile path/to/script.rbm > path/to/script.rb
|
35
|
+
$ ruby_mod_kit transpile --output=path/to/script.rb path/to/script.rbm
|
36
|
+
$ ruby_mod_kit transpile --output=.rb path/to/script.rbm
|
27
37
|
|
28
38
|
#### `exec`
|
29
39
|
|
30
40
|
You can run transpiled ruby script by `ruby_mod_kit exec` command.
|
31
41
|
|
32
|
-
|
33
|
-
ruby_mod_kit transpile path/to/script.rbm
|
34
|
-
```
|
35
|
-
|
36
|
-
The command also creates/updates ruby script before running.
|
42
|
+
$ ruby_mod_kit exec path/to/script.rbm
|
37
43
|
|
38
44
|
## Feature
|
39
45
|
|
data/lib/ruby_mod_kit/cli.rb
CHANGED
@@ -16,11 +16,20 @@ module RubyModKit
|
|
16
16
|
end
|
17
17
|
|
18
18
|
desc "transpile", "transpile rbm files"
|
19
|
+
method_option :output, type: :string
|
19
20
|
# @rbs *args: String
|
20
21
|
# @rbs return: void
|
21
22
|
def transpile(*args)
|
23
|
+
output = case options[:output]
|
24
|
+
when nil, "-", "/dev/stdout"
|
25
|
+
$stdout
|
26
|
+
when ".rb"
|
27
|
+
nil
|
28
|
+
else
|
29
|
+
options[:output]
|
30
|
+
end
|
22
31
|
args.each do |path|
|
23
|
-
RubyModKit.transpile_file(path)
|
32
|
+
RubyModKit.transpile_file(path, output: output || RubyModKit.rb_path(path))
|
24
33
|
end
|
25
34
|
end
|
26
35
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# rbs_inline: enabled
|
4
|
+
|
5
|
+
require "ruby_mod_kit"
|
6
|
+
|
7
|
+
module RubyModKit
|
8
|
+
module CoreExt
|
9
|
+
# the extension for eval
|
10
|
+
module Eval
|
11
|
+
module_function
|
12
|
+
|
13
|
+
# @rbs expr: String
|
14
|
+
# @rbs *rest: Object
|
15
|
+
# @rbs transpile: bool
|
16
|
+
# @rbs return: Object
|
17
|
+
def eval(expr, *rest, transpile: true)
|
18
|
+
if transpile
|
19
|
+
fname = rest[1].is_a?(String) ? rest[1] : "(eval)"
|
20
|
+
expr = RubyModKit.transpile(expr, filename: fname)
|
21
|
+
end
|
22
|
+
|
23
|
+
case rest
|
24
|
+
in [] | [Binding] | [Binding, String] | [Binding, String, Integer]
|
25
|
+
super(expr, *rest)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# rbs_inline: enabled
|
4
|
+
|
5
|
+
require "ruby_mod_kit"
|
6
|
+
require "ruby_mod_kit/core_ext/eval"
|
7
|
+
|
8
|
+
module RubyModKit
|
9
|
+
module CoreExt
|
10
|
+
# the extension for load/require
|
11
|
+
module Load
|
12
|
+
LOADABLE_EXTS = %w[.rb .rbm .so .o .dll].freeze #: Array[String]
|
13
|
+
|
14
|
+
module_function
|
15
|
+
|
16
|
+
# @rbs path: String
|
17
|
+
# @rbs wrap: bool
|
18
|
+
# @rbs return: void
|
19
|
+
def load(path, wrap = false) # rubocop:disable Style/OptionalBooleanParameter
|
20
|
+
return super unless path.end_with?(".rbm")
|
21
|
+
|
22
|
+
b = wrap ? binding : TOPLEVEL_BINDING
|
23
|
+
RubyModKit::CoreExt::Eval.eval(File.read(path), b, path)
|
24
|
+
end
|
25
|
+
|
26
|
+
# @rbs path: String
|
27
|
+
# @rbs return: void
|
28
|
+
def require(path)
|
29
|
+
require_path = Load.require_path(path)
|
30
|
+
return super unless require_path&.end_with?(".rbm")
|
31
|
+
return if Load.loaded_features.include?(require_path)
|
32
|
+
|
33
|
+
Load.loaded_features << require_path
|
34
|
+
load(require_path)
|
35
|
+
end
|
36
|
+
|
37
|
+
class << self
|
38
|
+
# @rbs return: Array[String]
|
39
|
+
def loaded_features
|
40
|
+
$LOADED_FEATURES
|
41
|
+
end
|
42
|
+
|
43
|
+
# @rbs return: Array[String]
|
44
|
+
def load_path
|
45
|
+
$LOAD_PATH
|
46
|
+
end
|
47
|
+
|
48
|
+
# @rbs path: String
|
49
|
+
# @rbs expanded: bool
|
50
|
+
# @rbs return: String | nil
|
51
|
+
def require_path(path, expanded: false)
|
52
|
+
if !expanded && !File.absolute_path?(path)
|
53
|
+
return load_path.each.lazy.map { require_path(File.join(_1, path), expanded: true) }.find(&:itself)
|
54
|
+
end
|
55
|
+
|
56
|
+
pathes = if path.end_with?(*LOADABLE_EXTS)
|
57
|
+
[path]
|
58
|
+
else
|
59
|
+
LOADABLE_EXTS.map { "#{path}#{_1}" }
|
60
|
+
end
|
61
|
+
pathes.find { File.exist?(_1) }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -4,17 +4,17 @@
|
|
4
4
|
|
5
5
|
module RubyModKit
|
6
6
|
class Memo
|
7
|
-
# The memo for
|
8
|
-
class
|
7
|
+
# The memo for def parent (class, module)
|
8
|
+
class DefParent < OffsetMemo
|
9
9
|
# @rbs @ivars_memo: Hash[Symbol, Memo::Ivar]
|
10
10
|
|
11
11
|
attr_reader :ivars_memo #: Hash[Symbol, Memo::Ivar]
|
12
12
|
|
13
|
-
# @rbs
|
13
|
+
# @rbs def_parent_node: Node::DefParentNode
|
14
14
|
# @rbs return: void
|
15
|
-
def initialize(
|
15
|
+
def initialize(def_parent_node)
|
16
16
|
@ivars_memo = {}
|
17
|
-
super(
|
17
|
+
super(def_parent_node.offset)
|
18
18
|
end
|
19
19
|
|
20
20
|
# @rbs name: Symbol
|
data/lib/ruby_mod_kit/memo.rb
CHANGED
@@ -5,17 +5,17 @@
|
|
5
5
|
module RubyModKit
|
6
6
|
# The class of transpiler generation.
|
7
7
|
class Memo
|
8
|
-
# @rbs @
|
8
|
+
# @rbs @def_parents_memo: Hash[Integer, Memo::DefParent]
|
9
9
|
# @rbs @methods_memo: Hash[Integer, Memo::Method]
|
10
10
|
# @rbs @parameters_memo: Hash[Integer, Memo::Parameter]
|
11
11
|
|
12
|
-
attr_reader :
|
12
|
+
attr_reader :def_parents_memo #: Hash[Integer, Memo::DefParent]
|
13
13
|
attr_reader :methods_memo #: Hash[Integer, Memo::Method]
|
14
14
|
attr_reader :parameters_memo #: Hash[Integer, Memo::Parameter]
|
15
15
|
|
16
16
|
# @rbs return: void
|
17
17
|
def initialize
|
18
|
-
@
|
18
|
+
@def_parents_memo = {}
|
19
19
|
@methods_memo = {}
|
20
20
|
@parameters_memo = {}
|
21
21
|
end
|
@@ -23,7 +23,7 @@ module RubyModKit
|
|
23
23
|
# @rbs offset_diff: OffsetDiff
|
24
24
|
# @rbs return: void
|
25
25
|
def succ(offset_diff)
|
26
|
-
[@methods_memo, @parameters_memo, @
|
26
|
+
[@methods_memo, @parameters_memo, @def_parents_memo].each do |offset_node_memo|
|
27
27
|
new_offset_node_memo = {}
|
28
28
|
offset_node_memo.each_value do |node_memo|
|
29
29
|
node_memo.succ(offset_diff)
|
@@ -34,10 +34,10 @@ module RubyModKit
|
|
34
34
|
self
|
35
35
|
end
|
36
36
|
|
37
|
-
# @rbs
|
38
|
-
# @rbs return: Memo::
|
39
|
-
def
|
40
|
-
@
|
37
|
+
# @rbs def_parent_node: Node::DefParentNode
|
38
|
+
# @rbs return: Memo::DefParent
|
39
|
+
def def_parent_memo(def_parent_node)
|
40
|
+
@def_parents_memo[def_parent_node.offset] ||= Memo::DefParent.new(def_parent_node)
|
41
41
|
end
|
42
42
|
|
43
43
|
# @rbs def_node: Node::DefNode
|
@@ -67,7 +67,7 @@ module RubyModKit
|
|
67
67
|
end
|
68
68
|
|
69
69
|
require_relative "memo/offset_memo"
|
70
|
-
require_relative "memo/
|
70
|
+
require_relative "memo/def_parent"
|
71
71
|
require_relative "memo/ivar"
|
72
72
|
require_relative "memo/method"
|
73
73
|
require_relative "memo/parameter"
|
@@ -58,9 +58,12 @@ module RubyModKit
|
|
58
58
|
parse_result.errors.each do |parse_error|
|
59
59
|
message << "\n" unless message.empty?
|
60
60
|
message << "#{generation.name}:#{parse_error.location.start_line}:#{parse_error.message} "
|
61
|
-
message << "(#{parse_error.type})
|
62
|
-
|
63
|
-
|
61
|
+
message << "(#{parse_error.type})"
|
62
|
+
line = parse_result.source.lines[parse_error.location.start_line - 1]
|
63
|
+
if line
|
64
|
+
message << "\n#{line.chomp}\n"
|
65
|
+
message << "#{" " * parse_error.location.start_column}^#{"~" * [parse_error.location.length - 1, 0].max}"
|
66
|
+
end
|
64
67
|
end
|
65
68
|
raise RubyModKit::SyntaxError, message
|
66
69
|
end
|
@@ -108,8 +111,8 @@ module RubyModKit
|
|
108
111
|
|
109
112
|
return unless parameter_memo.untyped?
|
110
113
|
|
111
|
-
|
112
|
-
ivar_memo_type = memo.
|
114
|
+
def_parent_node = root_node.def_parent_node_at(parse_error.location.start_offset) || return
|
115
|
+
ivar_memo_type = memo.def_parent_memo(def_parent_node).ivar_memo(name.to_sym).type || return
|
113
116
|
parameter_memo.type = ivar_memo_type
|
114
117
|
end
|
115
118
|
|
@@ -156,7 +159,7 @@ module RubyModKit
|
|
156
159
|
case parent_node
|
157
160
|
when Node::DefNode
|
158
161
|
fix_unexpected_colon_in_def(parse_error, generation, root_node, parent_node, memo)
|
159
|
-
when Node::
|
162
|
+
when Node::DefParentNode
|
160
163
|
fix_unexpected_colon_in_module(parse_error, generation, parent_node, memo)
|
161
164
|
end
|
162
165
|
end
|
@@ -186,10 +189,10 @@ module RubyModKit
|
|
186
189
|
|
187
190
|
# @rbs parse_error: Prism::ParseError
|
188
191
|
# @rbs generation: Generation
|
189
|
-
# @rbs
|
192
|
+
# @rbs def_parent_node: Node::DefParentNode
|
190
193
|
# @rbs memo: Memo
|
191
194
|
# @rbs return: void
|
192
|
-
def fix_unexpected_colon_in_module(parse_error, generation,
|
195
|
+
def fix_unexpected_colon_in_module(parse_error, generation, def_parent_node, memo)
|
193
196
|
line = generation.line(parse_error)
|
194
197
|
line_offset = generation.src_offset(parse_error) || return
|
195
198
|
attr_patterns = %i[attr_reader reader getter attr_writer writer setter attr_accessor accessor property]
|
@@ -202,7 +205,7 @@ module RubyModKit
|
|
202
205
|
type = ::Regexp.last_match(4)
|
203
206
|
return if !length || !indent || !ivar_name || !type
|
204
207
|
|
205
|
-
ivar_memo = memo.
|
208
|
+
ivar_memo = memo.def_parent_memo(def_parent_node).ivar_memo(ivar_name.to_sym)
|
206
209
|
ivar_memo.type = type
|
207
210
|
ivar_memo.attr_kind = attr_kind if attr_kind
|
208
211
|
|
@@ -22,7 +22,7 @@ module RubyModKit
|
|
22
22
|
# @rbs return: bool
|
23
23
|
def perform(generation, root_node, parse_result, memo)
|
24
24
|
method_memo_groups = memo.methods_memo.each_value.group_by do |method_memo|
|
25
|
-
[root_node.
|
25
|
+
[root_node.def_parent_node_at(method_memo.offset), method_memo.name]
|
26
26
|
end
|
27
27
|
method_memo_groups.each_value do |method_memos|
|
28
28
|
next if method_memos.length <= 1
|
@@ -17,11 +17,11 @@ module RubyModKit
|
|
17
17
|
# @rbs memo: Memo
|
18
18
|
# @rbs return: bool
|
19
19
|
def perform(generation, root_node, _parse_result, memo)
|
20
|
-
memo.
|
21
|
-
ivars_memo =
|
22
|
-
|
20
|
+
memo.def_parents_memo.each_value do |def_parent_memo|
|
21
|
+
ivars_memo = def_parent_memo.ivars_memo.dup
|
22
|
+
def_parent_node = root_node.def_parent_node_at(def_parent_memo.offset) || raise(RubyModKit::Error)
|
23
23
|
attr_adding_line = 0
|
24
|
-
|
24
|
+
def_parent_node.body_node&.children&.each do |call_node|
|
25
25
|
break if ivars_memo.empty?
|
26
26
|
next unless call_node.is_a?(Node::CallNode)
|
27
27
|
next unless %i[attr_reader attr_writer attr_accessor].include?(call_node.name)
|
@@ -42,7 +42,7 @@ module RubyModKit
|
|
42
42
|
|
43
43
|
add_first_separator_line = false
|
44
44
|
if attr_adding_line == 0
|
45
|
-
attr_adding_line =
|
45
|
+
attr_adding_line = def_parent_node.location.start_line
|
46
46
|
prev_line = nil
|
47
47
|
while generation.line(attr_adding_line) =~ /\A\s*#.*|\A$/
|
48
48
|
prev_line = ::Regexp.last_match(0)
|
@@ -54,12 +54,12 @@ module RubyModKit
|
|
54
54
|
add_separator_line = line != "\n" && line !~ /\A\s*end$/
|
55
55
|
offset = generation.src_offset(attr_adding_line) || next
|
56
56
|
|
57
|
-
if
|
58
|
-
first_line = generation.line(
|
57
|
+
if def_parent_node.body_node
|
58
|
+
first_line = generation.line(def_parent_node.body_node)
|
59
59
|
indent = first_line[/\A\s*/] || raise(RubyModKit::Error)
|
60
60
|
else
|
61
|
-
|
62
|
-
indent = " #{
|
61
|
+
def_parent_line = generation.line(def_parent_node)
|
62
|
+
indent = " #{def_parent_line[/\A\s*/]}"
|
63
63
|
end
|
64
64
|
|
65
65
|
generation[offset, 0] = "\n" if add_first_separator_line
|
@@ -5,22 +5,22 @@
|
|
5
5
|
module RubyModKit
|
6
6
|
class Node
|
7
7
|
# Transpiler program node
|
8
|
-
class
|
9
|
-
# @rbs @prism_node: Prism::ClassNode
|
8
|
+
class DefParentNode < Node
|
9
|
+
# @rbs @prism_node: Prism::ClassNode | Prism::ModuleNode
|
10
10
|
# @rbs @body_node: nil | Node::StatementsNode
|
11
11
|
# @rbs @parent: Node
|
12
12
|
|
13
|
-
attr_reader :prism_node #: Prism::ClassNode
|
13
|
+
attr_reader :prism_node #: Prism::ClassNode | Prism::ModuleNode
|
14
14
|
attr_reader :body_node #: nil | Node::StatementsNode
|
15
15
|
attr_reader :parent #: Node
|
16
16
|
|
17
|
-
# @rbs prism_node: Prism::ClassNode
|
17
|
+
# @rbs prism_node: Prism::ClassNode | Prism::ModuleNode
|
18
18
|
# @rbs parent: Node
|
19
19
|
# @rbs return: void
|
20
20
|
def initialize(prism_node, parent:)
|
21
21
|
@prism_node = prism_node
|
22
22
|
@parent = parent
|
23
|
-
raise RubyModKit::Error
|
23
|
+
raise RubyModKit::Error if !prism_node.is_a?(Prism::ClassNode) && !prism_node.is_a?(Prism::ModuleNode)
|
24
24
|
|
25
25
|
super()
|
26
26
|
end
|
data/lib/ruby_mod_kit/node.rb
CHANGED
@@ -31,8 +31,8 @@ module RubyModKit
|
|
31
31
|
case prism_node
|
32
32
|
when Prism::SymbolNode
|
33
33
|
Node::SymbolNode.new(prism_node, parent: self)
|
34
|
-
when Prism::ClassNode
|
35
|
-
Node::
|
34
|
+
when Prism::ClassNode, Prism::ModuleNode
|
35
|
+
Node::DefParentNode.new(prism_node, parent: self)
|
36
36
|
when Prism::DefNode
|
37
37
|
Node::DefNode.new(prism_node, parent: self)
|
38
38
|
when Prism::RequiredParameterNode, Prism::OptionalKeywordParameterNode,
|
@@ -99,10 +99,10 @@ module RubyModKit
|
|
99
99
|
end
|
100
100
|
|
101
101
|
# @rbs offset: Integer
|
102
|
-
# @rbs return: Node::
|
103
|
-
def
|
102
|
+
# @rbs return: Node::DefParentNode | nil
|
103
|
+
def def_parent_node_at(offset)
|
104
104
|
node = node_at(offset) || return
|
105
|
-
[node, *node.ancestors].each { return _1 if _1.is_a?(Node::
|
105
|
+
[node, *node.ancestors].each { return _1 if _1.is_a?(Node::DefParentNode) }
|
106
106
|
nil
|
107
107
|
end
|
108
108
|
|
@@ -155,7 +155,7 @@ module RubyModKit
|
|
155
155
|
end
|
156
156
|
|
157
157
|
require_relative "node/symbol_node"
|
158
|
-
require_relative "node/
|
158
|
+
require_relative "node/def_parent_node"
|
159
159
|
require_relative "node/call_node"
|
160
160
|
require_relative "node/def_node"
|
161
161
|
require_relative "node/parameter_node"
|
@@ -2,23 +2,24 @@
|
|
2
2
|
|
3
3
|
# rbs_inline: enabled
|
4
4
|
|
5
|
-
require "sorted_set"
|
6
|
-
|
7
5
|
module RubyModKit
|
8
6
|
# The class of offset differences.
|
9
7
|
class OffsetDiff
|
10
|
-
# @rbs @diffs:
|
8
|
+
# @rbs @diffs: Hash[Integer, Integer]
|
9
|
+
# @rbs @offsets: Array[Integer]
|
11
10
|
|
12
11
|
# @rbs return: void
|
13
12
|
def initialize
|
14
|
-
@diffs =
|
13
|
+
@diffs = {}
|
14
|
+
@offsets = []
|
15
15
|
end
|
16
16
|
|
17
17
|
# @rbs src_offset: Integer
|
18
18
|
# @rbs return: Integer
|
19
19
|
def [](src_offset)
|
20
20
|
dst_offset = src_offset
|
21
|
-
@
|
21
|
+
@offsets.each do |offset|
|
22
|
+
diff = @diffs[offset]
|
22
23
|
break if offset > src_offset
|
23
24
|
break if offset == src_offset && diff < 0
|
24
25
|
|
@@ -31,7 +32,12 @@ module RubyModKit
|
|
31
32
|
# @rbs new_diff: Integer
|
32
33
|
# @rbs return: void
|
33
34
|
def insert(src_offset, new_diff)
|
34
|
-
@diffs
|
35
|
+
if @diffs[src_offset]
|
36
|
+
@diffs[src_offset] += new_diff
|
37
|
+
else
|
38
|
+
@diffs[src_offset] = new_diff
|
39
|
+
@offsets.insert(@offsets.bsearch_index { _1 > src_offset } || -1, src_offset)
|
40
|
+
end
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
data/lib/ruby_mod_kit/version.rb
CHANGED
data/lib/ruby_mod_kit.rb
CHANGED
@@ -13,19 +13,29 @@ module RubyModKit
|
|
13
13
|
class << self
|
14
14
|
# @rbs file: String
|
15
15
|
# @rbs *args: String
|
16
|
+
# @rbs output: String | nil
|
16
17
|
# @rbs return: void
|
17
|
-
def execute_file(file, *args)
|
18
|
-
|
19
|
-
|
18
|
+
def execute_file(file, *args, output: nil)
|
19
|
+
rb_script = transpile_file(file, output: output)
|
20
|
+
if output
|
21
|
+
execute_rb_file(output, *args)
|
22
|
+
else
|
23
|
+
execute_rb(rb_script, *args)
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
# @rbs file: String
|
28
|
+
# @rbs output: String | IO | nil
|
23
29
|
# @rbs return: String
|
24
|
-
def transpile_file(file)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
def transpile_file(file, output: nil)
|
31
|
+
rb_script = transpile(File.read(file), filename: file)
|
32
|
+
case output
|
33
|
+
when IO
|
34
|
+
output.write(rb_script)
|
35
|
+
when String
|
36
|
+
File.write(output, rb_script)
|
37
|
+
end
|
38
|
+
rb_script
|
29
39
|
end
|
30
40
|
|
31
41
|
# @rbs src: String
|
@@ -47,6 +57,14 @@ module RubyModKit
|
|
47
57
|
def rb_path(path)
|
48
58
|
path.sub(/(?:\.rbm)?$/, ".rb")
|
49
59
|
end
|
60
|
+
|
61
|
+
# @rbs rb_script: String
|
62
|
+
# @rbs *args: String
|
63
|
+
# @rbs return: void
|
64
|
+
def execute_rb(rb_script, *args)
|
65
|
+
ARGV.replace(args)
|
66
|
+
eval(rb_script, TOPLEVEL_BINDING) # rubocop:disable Security/Eval
|
67
|
+
end
|
50
68
|
end
|
51
69
|
end
|
52
70
|
|
data/ruby_mod_kit.gemspec
CHANGED
@@ -4,14 +4,10 @@ require_relative "lib/ruby_mod_kit/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "ruby_mod_kit"
|
7
|
+
spec.version = RubyModKit::VERSION
|
7
8
|
spec.authors = ["wanabe"]
|
8
9
|
spec.email = ["s.wanabe@gmail.com"]
|
9
10
|
|
10
|
-
version = RubyModKit::VERSION
|
11
|
-
tag_version = `git tag --points-at HEAD`.chomp.sub("v", "")
|
12
|
-
version += "-#{`git rev-parse --short HEAD`}" if version != tag_version
|
13
|
-
spec.version = version
|
14
|
-
|
15
11
|
spec.summary = "ruby_mod_kit"
|
16
12
|
spec.description = "ruby_mod_kit"
|
17
13
|
spec.homepage = "https://github.com/wanabe/ruby_mod_kit"
|
@@ -43,7 +39,6 @@ Gem::Specification.new do |spec|
|
|
43
39
|
|
44
40
|
# Uncomment to register a new dependency of your gem
|
45
41
|
spec.add_dependency "prism", "~> 1.0"
|
46
|
-
spec.add_dependency "sorted_set", "~> 1.0"
|
47
42
|
spec.add_dependency "thor", "~> 1.3"
|
48
43
|
|
49
44
|
# For more information and examples about making a new gem, check out our
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Generated from lib/ruby_mod_kit/core_ext/eval.rb with RBS::Inline
|
2
|
+
|
3
|
+
module RubyModKit
|
4
|
+
module CoreExt
|
5
|
+
# the extension for eval
|
6
|
+
module Eval
|
7
|
+
# @rbs expr: String
|
8
|
+
# @rbs *rest: Object
|
9
|
+
# @rbs transpile: bool
|
10
|
+
# @rbs return: Object
|
11
|
+
def self?.eval: (String expr, *Object rest, ?transpile: bool) -> Object
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Generated from lib/ruby_mod_kit/core_ext/load.rb with RBS::Inline
|
2
|
+
|
3
|
+
module RubyModKit
|
4
|
+
module CoreExt
|
5
|
+
# the extension for load/require
|
6
|
+
module Load
|
7
|
+
LOADABLE_EXTS: Array[String]
|
8
|
+
|
9
|
+
# @rbs path: String
|
10
|
+
# @rbs wrap: bool
|
11
|
+
# @rbs return: void
|
12
|
+
def self?.load: (String path, ?bool wrap) -> void
|
13
|
+
|
14
|
+
# @rbs path: String
|
15
|
+
# @rbs return: void
|
16
|
+
def self?.require: (String path) -> void
|
17
|
+
|
18
|
+
# @rbs return: Array[String]
|
19
|
+
def self.loaded_features: () -> Array[String]
|
20
|
+
|
21
|
+
# @rbs return: Array[String]
|
22
|
+
def self.load_path: () -> Array[String]
|
23
|
+
|
24
|
+
# @rbs path: String
|
25
|
+
# @rbs expanded: bool
|
26
|
+
# @rbs return: String | nil
|
27
|
+
def self.require_path: (String path, ?expanded: bool) -> (String | nil)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
|
-
# Generated from lib/ruby_mod_kit/memo/
|
1
|
+
# Generated from lib/ruby_mod_kit/memo/def_parent.rb with RBS::Inline
|
2
2
|
|
3
3
|
module RubyModKit
|
4
4
|
class Memo
|
5
|
-
# The memo for
|
6
|
-
class
|
5
|
+
# The memo for def parent (class, module)
|
6
|
+
class DefParent < OffsetMemo
|
7
7
|
@ivars_memo: Hash[Symbol, Memo::Ivar]
|
8
8
|
|
9
9
|
attr_reader ivars_memo: Hash[Symbol, Memo::Ivar]
|
10
10
|
|
11
|
-
# @rbs
|
11
|
+
# @rbs def_parent_node: Node::DefParentNode
|
12
12
|
# @rbs return: void
|
13
|
-
def initialize: (Node::
|
13
|
+
def initialize: (Node::DefParentNode def_parent_node) -> void
|
14
14
|
|
15
15
|
# @rbs name: Symbol
|
16
16
|
# @rbs return: Memo::Ivar
|
@@ -3,13 +3,13 @@
|
|
3
3
|
module RubyModKit
|
4
4
|
# The class of transpiler generation.
|
5
5
|
class Memo
|
6
|
-
@
|
6
|
+
@def_parents_memo: Hash[Integer, Memo::DefParent]
|
7
7
|
|
8
8
|
@methods_memo: Hash[Integer, Memo::Method]
|
9
9
|
|
10
10
|
@parameters_memo: Hash[Integer, Memo::Parameter]
|
11
11
|
|
12
|
-
attr_reader
|
12
|
+
attr_reader def_parents_memo: Hash[Integer, Memo::DefParent]
|
13
13
|
|
14
14
|
attr_reader methods_memo: Hash[Integer, Memo::Method]
|
15
15
|
|
@@ -22,9 +22,9 @@ module RubyModKit
|
|
22
22
|
# @rbs return: void
|
23
23
|
def succ: (OffsetDiff offset_diff) -> void
|
24
24
|
|
25
|
-
# @rbs
|
26
|
-
# @rbs return: Memo::
|
27
|
-
def
|
25
|
+
# @rbs def_parent_node: Node::DefParentNode
|
26
|
+
# @rbs return: Memo::DefParent
|
27
|
+
def def_parent_memo: (Node::DefParentNode def_parent_node) -> Memo::DefParent
|
28
28
|
|
29
29
|
# @rbs def_node: Node::DefNode
|
30
30
|
# @rbs return: Memo::Method
|
@@ -64,10 +64,10 @@ module RubyModKit
|
|
64
64
|
|
65
65
|
# @rbs parse_error: Prism::ParseError
|
66
66
|
# @rbs generation: Generation
|
67
|
-
# @rbs
|
67
|
+
# @rbs def_parent_node: Node::DefParentNode
|
68
68
|
# @rbs memo: Memo
|
69
69
|
# @rbs return: void
|
70
|
-
def fix_unexpected_colon_in_module: (Prism::ParseError parse_error, Generation generation, Node::
|
70
|
+
def fix_unexpected_colon_in_module: (Prism::ParseError parse_error, Generation generation, Node::DefParentNode def_parent_node, Memo memo) -> void
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
# Generated from lib/ruby_mod_kit/node/
|
1
|
+
# Generated from lib/ruby_mod_kit/node/def_parent_node.rb with RBS::Inline
|
2
2
|
|
3
3
|
module RubyModKit
|
4
4
|
class Node
|
5
5
|
# Transpiler program node
|
6
|
-
class
|
7
|
-
@prism_node: Prism::ClassNode
|
6
|
+
class DefParentNode < Node
|
7
|
+
@prism_node: Prism::ClassNode | Prism::ModuleNode
|
8
8
|
|
9
9
|
@body_node: nil | Node::StatementsNode
|
10
10
|
|
11
11
|
@parent: Node
|
12
12
|
|
13
|
-
attr_reader prism_node: Prism::ClassNode
|
13
|
+
attr_reader prism_node: Prism::ClassNode | Prism::ModuleNode
|
14
14
|
|
15
15
|
attr_reader body_node: nil | Node::StatementsNode
|
16
16
|
|
17
17
|
attr_reader parent: Node
|
18
18
|
|
19
|
-
# @rbs prism_node: Prism::ClassNode
|
19
|
+
# @rbs prism_node: Prism::ClassNode | Prism::ModuleNode
|
20
20
|
# @rbs parent: Node
|
21
21
|
# @rbs return: void
|
22
|
-
def initialize: (Prism::ClassNode prism_node, parent: Node) -> void
|
22
|
+
def initialize: (Prism::ClassNode | Prism::ModuleNode prism_node, parent: Node) -> void
|
23
23
|
|
24
24
|
# @rbs child_prism_node: Prism::Node
|
25
25
|
# @rbs return: Node
|
@@ -44,8 +44,8 @@ module RubyModKit
|
|
44
44
|
def parameter_node_at: (Integer offset) -> (Node::ParameterNode | nil)
|
45
45
|
|
46
46
|
# @rbs offset: Integer
|
47
|
-
# @rbs return: Node::
|
48
|
-
def
|
47
|
+
# @rbs return: Node::DefParentNode | nil
|
48
|
+
def def_parent_node_at: (Integer offset) -> (Node::DefParentNode | nil)
|
49
49
|
|
50
50
|
# @rbs offset: Integer
|
51
51
|
# @rbs return: bool
|
@@ -10,12 +10,14 @@ module RubyModKit
|
|
10
10
|
|
11
11
|
# @rbs file: String
|
12
12
|
# @rbs *args: String
|
13
|
+
# @rbs output: String | nil
|
13
14
|
# @rbs return: void
|
14
|
-
def self.execute_file: (String file, *String args) -> void
|
15
|
+
def self.execute_file: (String file, *String args, ?output: String | nil) -> void
|
15
16
|
|
16
17
|
# @rbs file: String
|
18
|
+
# @rbs output: String | IO | nil
|
17
19
|
# @rbs return: String
|
18
|
-
def self.transpile_file: (String file) -> String
|
20
|
+
def self.transpile_file: (String file, ?output: String | IO | nil) -> String
|
19
21
|
|
20
22
|
# @rbs src: String
|
21
23
|
# @rbs filename: String | nil
|
@@ -30,4 +32,9 @@ module RubyModKit
|
|
30
32
|
# @rbs path: String
|
31
33
|
# @rbs return: String
|
32
34
|
def self.rb_path: (String path) -> String
|
35
|
+
|
36
|
+
# @rbs rb_script: String
|
37
|
+
# @rbs *args: String
|
38
|
+
# @rbs return: void
|
39
|
+
def self.execute_rb: (String rb_script, *String args) -> void
|
33
40
|
end
|
data/sig/thor.rbs
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_mod_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wanabe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: prism
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: sorted_set
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: thor
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,9 +53,12 @@ files:
|
|
67
53
|
- exe/ruby_mod_kit
|
68
54
|
- lib/ruby_mod_kit.rb
|
69
55
|
- lib/ruby_mod_kit/cli.rb
|
56
|
+
- lib/ruby_mod_kit/core_ext.rb
|
57
|
+
- lib/ruby_mod_kit/core_ext/eval.rb
|
58
|
+
- lib/ruby_mod_kit/core_ext/load.rb
|
70
59
|
- lib/ruby_mod_kit/generation.rb
|
71
60
|
- lib/ruby_mod_kit/memo.rb
|
72
|
-
- lib/ruby_mod_kit/memo/
|
61
|
+
- lib/ruby_mod_kit/memo/def_parent.rb
|
73
62
|
- lib/ruby_mod_kit/memo/ivar.rb
|
74
63
|
- lib/ruby_mod_kit/memo/method.rb
|
75
64
|
- lib/ruby_mod_kit/memo/offset_memo.rb
|
@@ -83,8 +72,8 @@ files:
|
|
83
72
|
- lib/ruby_mod_kit/mission/type_return.rb
|
84
73
|
- lib/ruby_mod_kit/node.rb
|
85
74
|
- lib/ruby_mod_kit/node/call_node.rb
|
86
|
-
- lib/ruby_mod_kit/node/class_node.rb
|
87
75
|
- lib/ruby_mod_kit/node/def_node.rb
|
76
|
+
- lib/ruby_mod_kit/node/def_parent_node.rb
|
88
77
|
- lib/ruby_mod_kit/node/parameter_node.rb
|
89
78
|
- lib/ruby_mod_kit/node/program_node.rb
|
90
79
|
- lib/ruby_mod_kit/node/statements_node.rb
|
@@ -97,9 +86,11 @@ files:
|
|
97
86
|
- sig/generated/examples/user.rbs
|
98
87
|
- sig/generated/ruby_mod_kit.rbs
|
99
88
|
- sig/generated/ruby_mod_kit/cli.rbs
|
89
|
+
- sig/generated/ruby_mod_kit/core_ext/eval.rbs
|
90
|
+
- sig/generated/ruby_mod_kit/core_ext/load.rbs
|
100
91
|
- sig/generated/ruby_mod_kit/generation.rbs
|
101
92
|
- sig/generated/ruby_mod_kit/memo.rbs
|
102
|
-
- sig/generated/ruby_mod_kit/memo/
|
93
|
+
- sig/generated/ruby_mod_kit/memo/def_parent.rbs
|
103
94
|
- sig/generated/ruby_mod_kit/memo/ivar.rbs
|
104
95
|
- sig/generated/ruby_mod_kit/memo/located.rbs
|
105
96
|
- sig/generated/ruby_mod_kit/memo/method.rbs
|
@@ -115,8 +106,8 @@ files:
|
|
115
106
|
- sig/generated/ruby_mod_kit/mission/type_return.rbs
|
116
107
|
- sig/generated/ruby_mod_kit/node.rbs
|
117
108
|
- sig/generated/ruby_mod_kit/node/call_node.rbs
|
118
|
-
- sig/generated/ruby_mod_kit/node/class_node.rbs
|
119
109
|
- sig/generated/ruby_mod_kit/node/def_node.rbs
|
110
|
+
- sig/generated/ruby_mod_kit/node/def_parent_node.rbs
|
120
111
|
- sig/generated/ruby_mod_kit/node/parameter_node.rbs
|
121
112
|
- sig/generated/ruby_mod_kit/node/program_node.rbs
|
122
113
|
- sig/generated/ruby_mod_kit/node/statements_node.rbs
|