[ruby/prism] Split private types
https://github.com/ruby/prism/commit/0209d093ec
This commit is contained in:
parent
e03e9c3644
commit
7556fd937c
@ -169,14 +169,17 @@ module Prism
|
|||||||
class ParametersNode < Node
|
class ParametersNode < Node
|
||||||
# Mirrors the Method#parameters method.
|
# Mirrors the Method#parameters method.
|
||||||
def signature
|
def signature
|
||||||
names = [] #: Array[[:req | :opt | :rest | :keyreq | :key | :keyrest | :block, Symbol] | [:rest | :keyrest | :nokey]]
|
names = [] #: Array[[:req | :opt | :rest | :keyreq | :key | :keyrest | :block, Symbol] | [:req | :rest | :keyrest | :nokey]]
|
||||||
|
|
||||||
requireds.each do |param|
|
requireds.each do |param|
|
||||||
names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
|
names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
|
||||||
end
|
end
|
||||||
|
|
||||||
optionals.each { |param| names << [:opt, param.name] }
|
optionals.each { |param| names << [:opt, param.name] }
|
||||||
names << [:rest, rest.name || :*] if rest
|
|
||||||
|
if rest && rest.is_a?(RestParameterNode)
|
||||||
|
names << [:rest, rest.name || :*]
|
||||||
|
end
|
||||||
|
|
||||||
posts.each do |param|
|
posts.each do |param|
|
||||||
names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
|
names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
|
||||||
|
@ -216,6 +216,7 @@ module Prism
|
|||||||
else
|
else
|
||||||
source = directive.source
|
source = directive.source
|
||||||
end
|
end
|
||||||
|
# @type var source_width: Integer
|
||||||
" #{source.ljust(source_width)} #{directive.describe}"
|
" #{source.ljust(source_width)} #{directive.describe}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ module Prism
|
|||||||
# Perform a byteslice on the source code using the given byte offset and
|
# Perform a byteslice on the source code using the given byte offset and
|
||||||
# byte length.
|
# byte length.
|
||||||
def slice(byte_offset, length)
|
def slice(byte_offset, length)
|
||||||
source.byteslice(byte_offset, length)
|
source.byteslice(byte_offset, length) or raise
|
||||||
end
|
end
|
||||||
|
|
||||||
# Binary search through the offsets to find the line number for the given
|
# Binary search through the offsets to find the line number for the given
|
||||||
@ -52,7 +52,7 @@ module Prism
|
|||||||
|
|
||||||
# Return the character offset for the given byte offset.
|
# Return the character offset for the given byte offset.
|
||||||
def character_offset(byte_offset)
|
def character_offset(byte_offset)
|
||||||
source.byteslice(0, byte_offset).length
|
(source.byteslice(0, byte_offset) or raise).length
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the column number in characters for the given byte offset.
|
# Return the column number in characters for the given byte offset.
|
||||||
@ -157,12 +157,8 @@ module Prism
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Create a new location object with the given options.
|
# Create a new location object with the given options.
|
||||||
def copy(**options)
|
def copy(source: self.source, start_offset: self.start_offset, length: self.length)
|
||||||
Location.new(
|
Location.new(source, start_offset, length)
|
||||||
options.fetch(:source) { source },
|
|
||||||
options.fetch(:start_offset) { start_offset },
|
|
||||||
options.fetch(:length) { length }
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a string representation of this location.
|
# Returns a string representation of this location.
|
||||||
|
@ -150,6 +150,7 @@ module Prism
|
|||||||
target_end = target.end_offset
|
target_end = target.end_offset
|
||||||
|
|
||||||
if target.encloses?(comment)
|
if target.encloses?(comment)
|
||||||
|
# @type var target: NodeTarget
|
||||||
# The comment is completely contained by this target. Abandon the
|
# The comment is completely contained by this target. Abandon the
|
||||||
# binary search at this level.
|
# binary search at this level.
|
||||||
return nearest_targets(target.node, comment)
|
return nearest_targets(target.node, comment)
|
||||||
|
@ -58,7 +58,11 @@ module Prism
|
|||||||
|
|
||||||
# Walk the tree and mark nodes that are on a new line.
|
# Walk the tree and mark nodes that are on a new line.
|
||||||
def mark_newlines!
|
def mark_newlines!
|
||||||
value.accept(Newlines.new(Array.new(1 + source.offsets.size, false)))
|
if ProgramNode === value
|
||||||
|
value.accept(Newlines.new(Array.new(1 + source.offsets.size, false)))
|
||||||
|
else
|
||||||
|
raise "ParseResult does not contain ProgramNode value"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -69,7 +69,14 @@ module Prism
|
|||||||
# nodes.
|
# nodes.
|
||||||
def compile
|
def compile
|
||||||
result = Prism.parse("case nil\nin #{query}\nend")
|
result = Prism.parse("case nil\nin #{query}\nend")
|
||||||
compile_node(result.value.statements.body.last.conditions.last.pattern)
|
|
||||||
|
case_match_node = result.value.statements.body.last
|
||||||
|
raise CompilationError, case_match_node.inspect unless case_match_node.is_a?(CaseMatchNode)
|
||||||
|
|
||||||
|
in_node = case_match_node.conditions.last
|
||||||
|
raise CompilationError, in_node.inspect unless in_node.is_a?(InNode)
|
||||||
|
|
||||||
|
compile_node(in_node.pattern)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Scan the given node and all of its children for nodes that match the
|
# Scan the given node and all of its children for nodes that match the
|
||||||
@ -77,13 +84,14 @@ module Prism
|
|||||||
# matches the pattern. If no block is given, an enumerator will be returned
|
# matches the pattern. If no block is given, an enumerator will be returned
|
||||||
# that will yield each node that matches the pattern.
|
# that will yield each node that matches the pattern.
|
||||||
def scan(root)
|
def scan(root)
|
||||||
return to_enum(__method__, root) unless block_given?
|
return to_enum(__method__ || raise, root) unless block_given?
|
||||||
|
|
||||||
@compiled ||= compile
|
@compiled ||= compile
|
||||||
|
compiled = @compiled #: Proc
|
||||||
queue = [root]
|
queue = [root]
|
||||||
|
|
||||||
while (node = queue.shift)
|
while (node = queue.shift)
|
||||||
yield node if @compiled.call(node)
|
yield node if compiled.call(node)
|
||||||
queue.concat(node.compact_child_nodes)
|
queue.concat(node.compact_child_nodes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -174,7 +182,13 @@ module Prism
|
|||||||
|
|
||||||
preprocessed =
|
preprocessed =
|
||||||
node.elements.to_h do |element|
|
node.elements.to_h do |element|
|
||||||
[element.key.unescaped.to_sym, compile_node(element.value)]
|
key = element.key
|
||||||
|
if key.respond_to?(:unescaped)
|
||||||
|
# @type var key: SymbolNode
|
||||||
|
[key.unescaped.to_sym, compile_node(element.value)]
|
||||||
|
else
|
||||||
|
raise CompilationError, element.inspect
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
compiled_keywords = ->(other) do
|
compiled_keywords = ->(other) do
|
||||||
|
@ -125,19 +125,14 @@ Gem::Specification.new do |spec|
|
|||||||
"sig/manifest.yaml",
|
"sig/manifest.yaml",
|
||||||
"sig/prism.rbs",
|
"sig/prism.rbs",
|
||||||
"sig/prism/compiler.rbs",
|
"sig/prism/compiler.rbs",
|
||||||
"sig/prism/debug.rbs",
|
|
||||||
"sig/prism/desugar_compiler.rbs",
|
|
||||||
"sig/prism/dispatcher.rbs",
|
"sig/prism/dispatcher.rbs",
|
||||||
"sig/prism/dot_visitor.rbs",
|
"sig/prism/dot_visitor.rbs",
|
||||||
"sig/prism/dsl.rbs",
|
"sig/prism/dsl.rbs",
|
||||||
"sig/prism/lex_compat.rbs",
|
|
||||||
"sig/prism/mutation_compiler.rbs",
|
"sig/prism/mutation_compiler.rbs",
|
||||||
"sig/prism/node_ext.rbs",
|
|
||||||
"sig/prism/node_inspector.rbs",
|
|
||||||
"sig/prism/node.rbs",
|
"sig/prism/node.rbs",
|
||||||
|
"sig/prism/node_ext.rbs",
|
||||||
"sig/prism/pack.rbs",
|
"sig/prism/pack.rbs",
|
||||||
"sig/prism/parse_result/comments.rbs",
|
"sig/prism/parse_result.rbs",
|
||||||
"sig/prism/parse_result/newlines.rbs",
|
|
||||||
"sig/prism/pattern.rbs",
|
"sig/prism/pattern.rbs",
|
||||||
"sig/prism/ripper_compat.rbs",
|
"sig/prism/ripper_compat.rbs",
|
||||||
"sig/prism/serialize.rbs",
|
"sig/prism/serialize.rbs",
|
||||||
|
@ -692,7 +692,6 @@ nodes:
|
|||||||
type: constant[]
|
type: constant[]
|
||||||
- name: parameters
|
- name: parameters
|
||||||
type: node?
|
type: node?
|
||||||
kind: BlockParametersNode
|
|
||||||
- name: body
|
- name: body
|
||||||
type: node?
|
type: node?
|
||||||
- name: opening_loc
|
- name: opening_loc
|
||||||
@ -1583,8 +1582,12 @@ nodes:
|
|||||||
type: node?
|
type: node?
|
||||||
- name: elements
|
- name: elements
|
||||||
type: node[]
|
type: node[]
|
||||||
|
kind: AssocNode
|
||||||
- name: rest
|
- name: rest
|
||||||
type: node?
|
type: node?
|
||||||
|
kind:
|
||||||
|
- AssocSplatNode
|
||||||
|
- NoKeywordsParameterNode
|
||||||
- name: opening_loc
|
- name: opening_loc
|
||||||
type: location?
|
type: location?
|
||||||
- name: closing_loc
|
- name: closing_loc
|
||||||
|
@ -17,7 +17,7 @@ module Prism
|
|||||||
if port
|
if port
|
||||||
"<tr><td align=\"left\" colspan=\"2\" port=\"#{name}\">#{name}</td></tr>"
|
"<tr><td align=\"left\" colspan=\"2\" port=\"#{name}\">#{name}</td></tr>"
|
||||||
else
|
else
|
||||||
"<tr><td align=\"left\">#{name}</td><td>#{CGI.escapeHTML(value)}</td></tr>"
|
"<tr><td align=\"left\">#{name}</td><td>#{CGI.escapeHTML(value || raise)}</td></tr>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -182,14 +182,9 @@ module Prism
|
|||||||
}.compact.join(", ") %>] #: Array[Prism::node | Location]
|
}.compact.join(", ") %>] #: Array[Prism::node | Location]
|
||||||
end
|
end
|
||||||
|
|
||||||
# def copy: (**params) -> <%= node.name %>
|
# def copy: (<%= (node.fields.map { |field| "?#{field.name}: #{field.rbs_class}" } + ["?location: Location"]).join(", ") %>) -> <%= node.name %>
|
||||||
def copy(**params)
|
def copy(<%= (node.fields.map(&:name) + ["location"]).map { |field| "#{field}: self.#{field}" }.join(", ") %>)
|
||||||
<%= node.name %>.new(
|
<%= node.name %>.new(<%= ["source", *node.fields.map(&:name), "location"].join(", ") %>)
|
||||||
source,
|
|
||||||
<%- (node.fields.map(&:name) + ["location"]).map do |name| -%>
|
|
||||||
params.fetch(:<%= name %>) { <%= name %> },
|
|
||||||
<%- end -%>
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# def deconstruct: () -> Array[nil | Node]
|
# def deconstruct: () -> Array[nil | Node]
|
||||||
|
@ -99,11 +99,11 @@ module Prism
|
|||||||
end
|
end
|
||||||
|
|
||||||
def specific_kind
|
def specific_kind
|
||||||
@options[:kind] unless @options[:kind].is_a?(Array)
|
options[:kind] unless options[:kind].is_a?(Array)
|
||||||
end
|
end
|
||||||
|
|
||||||
def union_kind
|
def union_kind
|
||||||
options[:kind] if @options[:kind].is_a?(Array)
|
options[:kind] if options[:kind].is_a?(Array)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ module Prism
|
|||||||
if specific_kind
|
if specific_kind
|
||||||
"#{specific_kind}?"
|
"#{specific_kind}?"
|
||||||
elsif union_kind
|
elsif union_kind
|
||||||
[union_kind, "nil"].join(" | ")
|
[*union_kind, "nil"].join(" | ")
|
||||||
else
|
else
|
||||||
"Prism::node?"
|
"Prism::node?"
|
||||||
end
|
end
|
||||||
@ -147,7 +147,13 @@ module Prism
|
|||||||
# references and store them directly on the struct.
|
# references and store them directly on the struct.
|
||||||
class NodeListField < Field
|
class NodeListField < Field
|
||||||
def rbs_class
|
def rbs_class
|
||||||
"Array[Prism::node]"
|
if specific_kind
|
||||||
|
"Array[#{specific_kind}]"
|
||||||
|
elsif union_kind
|
||||||
|
"Array[#{union_kind.join(" | ")}]"
|
||||||
|
else
|
||||||
|
"Array[Prism::node]"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def rbi_class
|
def rbi_class
|
||||||
@ -157,6 +163,15 @@ module Prism
|
|||||||
def java_type
|
def java_type
|
||||||
"Node[]"
|
"Node[]"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: unduplicate with NodeKindField
|
||||||
|
def specific_kind
|
||||||
|
options[:kind] unless options[:kind].is_a?(Array)
|
||||||
|
end
|
||||||
|
|
||||||
|
def union_kind
|
||||||
|
options[:kind] if options[:kind].is_a?(Array)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# This represents a field on a node that is the ID of a string interned
|
# This represents a field on a node that is the ID of a string interned
|
||||||
@ -569,12 +584,12 @@ module Prism
|
|||||||
"src/token_type.c",
|
"src/token_type.c",
|
||||||
"rbi/prism.rbi",
|
"rbi/prism.rbi",
|
||||||
"sig/prism.rbs",
|
"sig/prism.rbs",
|
||||||
"sig/prism/dot_visitor.rbs",
|
|
||||||
"sig/prism/dsl.rbs",
|
"sig/prism/dsl.rbs",
|
||||||
"sig/prism/mutation_compiler.rbs",
|
"sig/prism/mutation_compiler.rbs",
|
||||||
"sig/prism/node.rbs",
|
"sig/prism/node.rbs",
|
||||||
"sig/prism/ripper_compat.rbs",
|
|
||||||
"sig/prism/visitor.rbs",
|
"sig/prism/visitor.rbs",
|
||||||
|
"sig/prism/_private/dot_visitor.rbs",
|
||||||
|
"sig/prism/_private/ripper_compat.rbs",
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user