Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
implementing...
Browse files Browse the repository at this point in the history
  • Loading branch information
euglena1215 committed Jun 10, 2024
1 parent e97d206 commit c00b76d
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ Style/StringLiterals:

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Layout/LeadingCommentSpace:
Enabled: false
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ gem "rake", "~> 13.0"

gem "minitest", "~> 5.16"

gem "rubocop", "~> 1.21"
gem "rbs-inline"
gem "rubocop", "~> 1.21"
gem "steep"
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ namespace :rbs do
task setup: %i[clean inline inline_data]

task :clean do
sh 'rm', '-rf', 'sig/generated'
sh "rm", "-rf", "sig/generated"
end

task :install do
sh 'bundle', 'exec', 'rbs', 'install'
sh "bundle", "exec", "rbs", "install"
end

task :inline do
sh 'bundle', 'exec', 'rbs-inline', 'lib', '--opt-out', '--output'
sh "bundle", "exec", "rbs-inline", "lib", "--opt-out", "--output"
end

task :inline_data do
sh 'bundle', 'exec', 'ruby', 'exe/rbs-inline-data', 'lib'
sh "bundle", "exec", "ruby", "exe/rbs-inline-data", "lib", "--output"
end
end
4 changes: 3 additions & 1 deletion Steepfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

D = Steep::Diagnostic

target :lib do
signature "sig"

check "lib"

configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
end
5 changes: 3 additions & 2 deletions exe/rbs-inline-data
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rbs_inline_data'
require 'rbs_inline_data/cli'
require "rbs_inline_data"
require "rbs_inline_data/cli"

RbsInlineData::CLI.new.run(ARGV)
23 changes: 15 additions & 8 deletions lib/rbs_inline_data/cli.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# frozen_string_literal: true

require 'rbs_inline_data/parser'
require 'rbs_inline_data/writer'
require "optparse"

require "rbs_inline_data/parser"
require "rbs_inline_data/writer"

module RbsInlineData
class CLI
#:: (Array[String]) -> void
def run(args)
unless args.size == 1
raise ArgumentError, "Usage: rbs_inline_data <path/file>"
end
# @type var output_path: Pathname?
output_path = nil

OptionParser.new do |opts|
opts.on("--output", "Output to stdout instead of writing to files") do
output_path = Pathname("sig/generated/data")
end
end.parse!(args)

targets = Pathname.glob(args[0]).flat_map do |path|
if path.directory?
Expand All @@ -22,10 +29,10 @@ def run(args)
targets.sort!
targets.uniq!

targets.each do |file|
result = Prism.parse_file(file.to_s)
targets.each do |target|
result = Prism.parse_file(target.to_s)
definitions = Parser.parse(result)
Writer.write(file, definitions)
Writer.write(definitions, output_path ? (output_path + target).sub_ext(".rbs") : nil)
end
end
end
Expand Down
30 changes: 21 additions & 9 deletions lib/rbs_inline_data/parser.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
require 'prism'
# frozen_string_literal: true

require "prism"

module RbsInlineData
class Parser < Prism::Visitor
# @rbs skip
TypedDefinition = Data.define(
:class_name, #:: String
:fields, #:: Array[TypedField]
:fields #:: Array[TypedField]
)
# @rbs skip
TypedField = Data.define(
:field_name, #:: String
:type, #:: String
:type #:: String
)

# @rbs @definitions: Array[RbsInlineData::Parser::TypedDefinition]
Expand All @@ -26,7 +28,7 @@ def initialize(definitions)
def self.parse(result)
# @type var definitions: Array[RbsInlineData::Parser::TypedDefinition]
definitions = []
instance = self.new(definitions)
instance = new(definitions)
result.value.accept(instance)
definitions
end
Expand Down Expand Up @@ -80,15 +82,25 @@ def extract_definition(node)
_, class_name, field_text = source.match(/\A([a-zA-Z]+) = Data\.define\(([\n\s\w\W]+)\)\z/).to_a
return nil if field_text.nil? || class_name.nil?

class_name = @surronding_class_or_module.join("::") + "::" + class_name
class_name = "#{@surronding_class_or_module.join("::")}::#{class_name}"

fields = field_text.split("\n").map(&:strip).map do |str|
str.match(/:(\w+), #:: ([\w\[\]]+)/)&.to_a
end.compact.map { |_, field_name, type| TypedField.new(field_name: field_name, type: type) }
fields = field_text.split("\n").map(&:strip).reject(&:empty?).map do |str|
case str
when /:(\w+),? #:: ([\w\[\]]+)/
[::Regexp.last_match(1), ::Regexp.last_match(2)]
when /:(\w+),?/
[::Regexp.last_match(1), "untyped"]
end
end.compact.map do |field_name, type|
TypedField.new(
field_name: field_name,
type: type
)
end

TypedDefinition.new(
class_name: class_name,
fields: fields,
fields: fields
)
end
end
Expand Down
45 changes: 28 additions & 17 deletions lib/rbs_inline_data/writer.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
# frozen_string_literal: true

module RbsInlineData
class Writer
#:: (Pathname, Array[RbsInlineData::Parser::TypedDefinition]) -> void
def self.write(file, definitions)
new(file, definitions).write
#:: (Array[RbsInlineData::Parser::TypedDefinition], Pathname?) -> void
def self.write(definitions, output_path)
new(definitions).write(output_path)
end

# @rbs @file: Pathname
# @rbs @definitions: Array[RbsInlineData::Parser::TypedDefinition]

#:: (Pathname, Array[RbsInlineData::Parser::TypedDefinition]) -> void
def initialize(file, definitions)
@file = file
#:: (Array[RbsInlineData::Parser::TypedDefinition]) -> void
def initialize(definitions)
@definitions = definitions
end

# () -> void
def write
#:: (Pathname?) -> void
def write(output_path)
return if @definitions.empty?

puts "file: #{@file}"
rbs = ""
@definitions.each do |definition|
if output_path
output_path.parent.mkpath unless output_path.parent.directory?
output_path.write(build_rbs(@definitions))
else
puts build_rbs(@definitions)
end
end

private

#:: (Array[RbsInlineData::Parser::TypedDefinition]) -> String
def build_rbs(definitions)
rbs_text = ""
definitions.each do |definition|
source = <<~RBS
class #{definition.class_name}
extend Data::_DataClass
#{definition.fields.map { |field| "attr_reader #{field.field_name}: #{field.type}" }.join("\n ")}
def self.new: (*untyped) -> #{definition.class_name}
| (**untyped) -> #{definition.class_name}
def self.new: (*untyped) -> ::#{definition.class_name}
| (**untyped) -> ::#{definition.class_name}
| ...
end
RBS
rbs += source + "\n"
end

puts rbs
rbs_text += "#{source}\n"
end
rbs_text
end
end
end
2 changes: 1 addition & 1 deletion rbs_inline_data.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "rbs"
spec.add_dependency "prism"
spec.add_dependency "rbs"
end
18 changes: 18 additions & 0 deletions sig/generated/data/lib/rbs_inline_data/parser.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class RbsInlineData::Parser::TypedDefinition
extend Data::_DataClass
attr_reader class_name: String
attr_reader fields: Array[TypedField]
def self.new: (*untyped) -> ::RbsInlineData::Parser::TypedDefinition
| (**untyped) -> ::RbsInlineData::Parser::TypedDefinition
| ...
end

class RbsInlineData::Parser::TypedField
extend Data::_DataClass
attr_reader field_name: String
attr_reader type: String
def self.new: (*untyped) -> ::RbsInlineData::Parser::TypedField
| (**untyped) -> ::RbsInlineData::Parser::TypedField
| ...
end

28 changes: 0 additions & 28 deletions sig/generated/rbs_inline_data/parser.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,6 @@

module RbsInlineData
class Parser < Prism::Visitor
class DefineDataVisitor < Prism::Visitor
@definitions: Array[RbsInlineData::Parser::TypedDefinition]

@surronding_class_or_module: Array[Symbol]

def initialize: (untyped definitions) -> untyped

# @rbs override
def visit_class_node: ...

# @rbs override
def visit_module_node: ...

# @rbs override
def visit_constant_write_node: ...

private

# :: (Prism::ClassNode | Prism::ModuleNode) { (Prism::ClassNode | Prism::ModuleNode) -> void } -> void
def record_surrounding_class_or_module: (Prism::ClassNode | Prism::ModuleNode) { (Prism::ClassNode | Prism::ModuleNode) -> void } -> void

# :: (Prism::ConstantWriteNode) -> bool
def define_data?: (Prism::ConstantWriteNode) -> bool

# :: (Prism::ConstantWriteNode) -> RbsInlineData::Parser::TypedDefinition?
def extract_definition: (Prism::ConstantWriteNode) -> RbsInlineData::Parser::TypedDefinition?
end

@definitions: Array[RbsInlineData::Parser::TypedDefinition]

@surronding_class_or_module: Array[Symbol]
Expand Down
19 changes: 11 additions & 8 deletions sig/generated/rbs_inline_data/writer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

module RbsInlineData
class Writer
# :: (Pathname, Array[RbsInlineData::Parser::TypedDefinition]) -> void
def self.write: (Pathname, Array[RbsInlineData::Parser::TypedDefinition]) -> void

@file: Pathname
# :: (Array[RbsInlineData::Parser::TypedDefinition], Pathname?) -> void
def self.write: (Array[RbsInlineData::Parser::TypedDefinition], Pathname?) -> void

@definitions: Array[RbsInlineData::Parser::TypedDefinition]

# :: (Pathname, Array[RbsInlineData::Parser::TypedDefinition]) -> void
def initialize: (Pathname, Array[RbsInlineData::Parser::TypedDefinition]) -> void
# :: (Array[RbsInlineData::Parser::TypedDefinition]) -> void
def initialize: (Array[RbsInlineData::Parser::TypedDefinition]) -> void

# :: (Pathname?) -> void
def write: (Pathname?) -> void

private

# () -> void
def write: () -> untyped
# :: (Array[RbsInlineData::Parser::TypedDefinition]) -> String
def build_rbs: (Array[RbsInlineData::Parser::TypedDefinition]) -> String
end
end
17 changes: 0 additions & 17 deletions sig/handwritten.rbs

This file was deleted.

0 comments on commit c00b76d

Please sign in to comment.