Skip to content

Commit

Permalink
[ruby/prism] Consolidate integer fields into a single reflection class
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Apr 22, 2024
1 parent f77618c commit aa5b53d
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions prism/templates/lib/prism/reflection.rb.erb
Expand Up @@ -65,14 +65,16 @@ module Prism
class OptionalLocationField < Field
end

# A uint8 field represents an unsigned 8-bit integer value on a node. It
# resolves to an Integer in Ruby.
class UInt8Field < Field
# An integer field represents an integer value. It is used to represent the
# value of an integer literal, the depth of local variables, and the number
# of a numbered reference. It resolves to an Integer in Ruby.
class IntegerField < Field
end

# A uint32 field represents an unsigned 32-bit integer value on a node. It
# resolves to an Integer in Ruby.
class UInt32Field < Field
# A float field represents a double-precision floating point value. It is
# used exclusively to represent the value of a floating point literal. It
# resolves to a Float in Ruby.
class FloatField < Field
end

# A flags field represents a bitset of flags on a node. It resolves to an
Expand All @@ -90,18 +92,6 @@ module Prism
end
end

# An integer field represents an arbitrarily-sized integer value. It is used
# exclusively to represent the value of an integer literal. It resolves to
# an Integer in Ruby.
class IntegerField < Field
end

# A double field represents a double-precision floating point value. It is
# used exclusively to represent the value of a floating point literal. It
# resolves to a Float in Ruby.
class DoubleField < Field
end

# Returns the fields for the given node.
def self.fields_for(node)
case node.type
Expand All @@ -127,17 +117,13 @@ module Prism
"LocationField.new(:#{field.name})"
when Prism::Template::OptionalLocationField
"OptionalLocationField.new(:#{field.name})"
when Prism::Template::UInt8Field
"UInt8Field.new(:#{field.name})"
when Prism::Template::UInt32Field
"UInt32Field.new(:#{field.name})"
when Prism::Template::UInt8Field, Prism::Template::UInt32Field, Prism::Template::IntegerField
"Integer.new(:#{field.name})"
when Prism::Template::DoubleField
"FloatField.new(:#{field.name})"
when Prism::Template::FlagsField
found = flags.find { |flag| flag.name == field.kind }.tap { |found| raise "Expected to find #{field.kind}" unless found }
"FlagsField.new(:#{field.name}, [#{found.values.map { |value| ":#{value.name.downcase}?" }.join(", ")}])"
when Prism::Template::IntegerField
"IntegerField.new(:#{field.name})"
when Prism::Template::DoubleField
"DoubleField.new(:#{field.name})"
else
raise field.class.name
end
Expand Down

0 comments on commit aa5b53d

Please sign in to comment.