diff options
author | Kevin Newton <kddnewton@gmail.com> | 2023-09-11 11:31:52 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-09-11 16:18:32 +0000 |
commit | 719f83446613f1e18f1a38cff0e00e7be88bbe14 (patch) | |
tree | 4d8a9bb90a13b24f93296f579d367393c2675822 /lib | |
parent | 05a853c2f21f60f9e1c544c2d0709f10de453571 (diff) |
[ruby/yarp] Mark flags as private
The flags integer is an implementation detail. We want people to
use the query methods to access the individual fields so we are
freed from having to maintain a specific order. As such, this
commit changes the Ruby API to mark all flags fields as private
attr_readers.
The only one that has a clear use case is returning the set of
options given to regular expressions, to mirror the Regexp#options
API. So, to support this use case, this commit introduces
RegularExpressionNode#options and InterpolatedRegularExpressionNode#options.
These APIs provide back the same integer so that they can be used
interchangeably.
https://github.com/ruby/yarp/commit/4e6d5dd99f
Diffstat (limited to 'lib')
-rw-r--r-- | lib/yarp.rb | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/lib/yarp.rb b/lib/yarp.rb index f878e719cc..2442ad3354 100644 --- a/lib/yarp.rb +++ b/lib/yarp.rb @@ -389,30 +389,6 @@ module YARP end end - class FloatNode < Node - def value - Float(slice) - end - end - - class ImaginaryNode < Node - def value - Complex(0, numeric.value) - end - end - - class IntegerNode < Node - def value - Integer(slice) - end - end - - class RationalNode < Node - def value - Rational(slice.chomp("r")) - end - end - # Load the serialized AST using the source as a reference into a tree. def self.load(source, serialized) Serialize.load(source, serialized) @@ -599,3 +575,57 @@ if RUBY_ENGINE == "ruby" and !ENV["YARP_FFI_BACKEND"] else require_relative "yarp/ffi" end + +# Reopening the YARP module after yarp/node is required so that constant +# reflection APIs will find the constants defined in the node file before these. +# This block is meant to contain extra APIs we define on YARP nodes that aren't +# templated and are meant as convenience methods. +module YARP + class FloatNode < Node + # Returns the value of the node as a Ruby Float. + def value + Float(slice) + end + end + + class ImaginaryNode < Node + # Returns the value of the node as a Ruby Complex. + def value + Complex(0, numeric.value) + end + end + + class IntegerNode < Node + # Returns the value of the node as a Ruby Integer. + def value + Integer(slice) + end + end + + class InterpolatedRegularExpressionNode < Node + # Returns a numeric value that represents the flags that were used to create + # the regular expression. This mirrors the Regexp#options method in Ruby. + # Note that this is effectively masking only the three common flags that are + # used in Ruby, and does not include the full set of flags like encoding. + def options + flags & 0b111 + end + end + + class RationalNode < Node + # Returns the value of the node as a Ruby Rational. + def value + Rational(slice.chomp("r")) + end + end + + class RegularExpressionNode < Node + # Returns a numeric value that represents the flags that were used to create + # the regular expression. This mirrors the Regexp#options method in Ruby. + # Note that this is effectively masking only the three common flags that are + # used in Ruby, and does not include the full set of flags like encoding. + def options + flags & 0b111 + end + end +end |