Skip to content

Commit

Permalink
wip: node unions
Browse files Browse the repository at this point in the history
  • Loading branch information
nixme authored and kddnewton committed Feb 24, 2024
1 parent 5d4ced8 commit 99a7167
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
32 changes: 32 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ nodes:
type: constant[]
- name: parameters
type: node?
kind: BlockParametersNode
- name: body
type: node?
- name: opening_loc
Expand Down Expand Up @@ -727,6 +728,7 @@ nodes:
kind: ParametersNode
- name: locals
type: node[]
kind: BlockLocalVariableNode
- name: opening_loc
type: location?
- name: closing_loc
Expand Down Expand Up @@ -1134,6 +1136,9 @@ nodes:
type: node?
- name: child
type: node
kind:
- ConstantReadNode
- MissingNode
- name: delimiter_loc
type: location
comment: |
Expand Down Expand Up @@ -1177,6 +1182,9 @@ nodes:
type: node?
- name: child
type: node
kind:
- ConstantReadNode
- MissingNode
- name: delimiter_loc
type: location
comment: |
Expand Down Expand Up @@ -1617,6 +1625,10 @@ nodes:
fields:
- name: numeric
type: node
kind:
- FloatNode
- IntegerNode
- RationalNode
comment: |
Represents an imaginary number literal.
Expand Down Expand Up @@ -2370,16 +2382,36 @@ nodes:
fields:
- name: requireds
type: node[]
kind:
- RequiredParameterNode
- MultiTargetNode
- name: optionals
type: node[]
kind: OptionalParameterNode
- name: rest
type: node?
kind:
- RestParameterNode
- ImplicitRestNode # Only in block parameters
- name: posts
type: node[]
kind:
- RequiredParameterNode
- MultiTargetNode
# On parsing error of `f(**kwargs, ...)` or `f(**nil, ...)`, the keyword_rest value is moved here:
- KeywordRestParameterNode
- NoKeywordsParameterNode
- name: keywords
type: node[]
kind:
- RequiredKeywordParameterNode
- OptionalKeywordParameterNode
- name: keyword_rest
type: node?
kind:
- KeywordRestParameterNode
- ForwardingParameterNode
- NoKeywordsParameterNode
- name: block
type: node?
kind: BlockParameterNode
Expand Down
34 changes: 27 additions & 7 deletions templates/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,49 @@ def should_be_serialized?
# node and not just a generic node.
class NodeKindField < Field
def c_type
if options[:kind]
"pm_#{options[:kind].gsub(/(?<=.)[A-Z]/, "_\\0").downcase}"
if specific_kind
"pm_#{specific_kind.gsub(/(?<=.)[A-Z]/, "_\\0").downcase}"
else
"pm_node"
end
end

def ruby_type
options[:kind] || "Node"
specific_kind || "Node"
end

def java_type
options[:kind] || "Node"
specific_kind || "Node"
end

def java_cast
if options[:kind]
if specific_kind
"(Nodes.#{options[:kind]}) "
else
""
end
end

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

# This represents a field on a node that is itself a node. We pass them as
# references and store them as references.
class NodeField < NodeKindField
def rbs_class
options[:kind] || "Prism::node"
if specific_kind
specific_kind
elsif union_kind
union_kind.join(" | ")
else
"Prism::node"
end
end

def rbi_class
Expand All @@ -115,7 +129,13 @@ def rbi_class
# optionally null. We pass them as references and store them as references.
class OptionalNodeField < NodeKindField
def rbs_class
"#{options[:kind] || "Prism::node"}?"
if specific_kind
"#{specific_kind}?"
elsif union_kind
[union_kind, "nil"].join(" | ")
else
"Prism::node?"
end
end

def rbi_class
Expand Down

0 comments on commit 99a7167

Please sign in to comment.