lib/yarp.rb in yarp-0.8.0 vs lib/yarp.rb in yarp-0.9.0

- old
+ new

@@ -35,11 +35,11 @@ # This represents a location in the source. class Location # A Source object that is used to determine more information from the given # offset and length. - private attr_reader :source + protected attr_reader :source # The byte offset from the beginning of the source where this location # starts. attr_reader :start_offset @@ -50,10 +50,20 @@ @source = source @start_offset = start_offset @length = length end + # Create a new location object with the given options. + def copy(**options) + Location.new( + options.fetch(:source) { source }, + options.fetch(:start_offset) { start_offset }, + options.fetch(:length) { length } + ) + end + + # Returns a string representation of this location. def inspect "#<YARP::Location @start_offset=#{@start_offset} @length=#{@length}>" end # The source code that this location represents. @@ -100,10 +110,20 @@ other.is_a?(Location) && other.start_offset == start_offset && other.end_offset == end_offset end + # Returns a new location that stretches from this location to the given + # other location. Raises an error if this location is not before the other + # location or if they don't share the same source. + def join(other) + raise "Incompatible sources" if source != other.source + raise "Incompatible locations" if start_offset > other.start_offset + + Location.new(source, start_offset, other.end_offset - start_offset) + end + def self.null new(0, 0) end end @@ -311,10 +331,34 @@ q.text(")") end 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) end @@ -481,15 +525,17 @@ # but at least this way it's clear it's not meant for consumers. private_constant :Debug end require_relative "yarp/lex_compat" +require_relative "yarp/mutation_visitor" +require_relative "yarp/desugar_visitor" require_relative "yarp/node" require_relative "yarp/ripper_compat" require_relative "yarp/serialize" require_relative "yarp/pack" if RUBY_ENGINE == "ruby" and !ENV["YARP_FFI_BACKEND"] require "yarp/yarp" else - require "yarp/ffi" + require_relative "yarp/ffi" end