Sha256: 6f7b5ea5094873cc3b567a005aa3e33e42b81b10f49faa1847d15c5d6cf1ef15

Contents?: true

Size: 1.83 KB

Versions: 35

Compression:

Stored size: 1.83 KB

Contents

module Steep
  module AST
    class Location
      attr_reader :buffer
      attr_reader :start_pos
      attr_reader :end_pos

      def initialize(buffer:, start_pos:, end_pos:)
        @buffer = buffer
        @start_pos = start_pos
        @end_pos = end_pos
      end

      def inspect
        "#<#{self.class}:#{self.__id__} @buffer=#{buffer.name}, @start_pos=#{start_pos}, @end_pos=#{end_pos}, source='#{source.lines.first}', start_line=#{start_line}, start_column=#{start_column}>"
      end

      def name
        buffer.name
      end

      def start_line
        start_loc[0]
      end

      def start_column
        start_loc[1]
      end

      def end_line
        end_loc[0]
      end

      def end_column
        end_loc[1]
      end

      def start_loc
        @start_loc ||= buffer.pos_to_loc(start_pos)
      end

      def end_loc
        @end_loc ||= buffer.pos_to_loc(end_pos)
      end

      def source
        @source ||= buffer.source(start_pos...end_pos)
      end

      def to_s
        "#{start_line}:#{start_column}...#{end_line}:#{end_column}"
      end

      def ==(other)
        other.is_a?(Location) &&
          other.buffer == buffer &&
          other.start_pos == start_pos &&
          other.end_pos == end_pos
      end

      def +(other)
        if other
          raise "Invalid concat: buffer=#{buffer.name}, other.buffer=#{other.buffer.name}" unless other.buffer == buffer

          self.class.new(buffer: buffer,
                         start_pos: start_pos,
                         end_pos: other.end_pos)
        else
          self
        end
      end

      def self.concat(*locations)
        locations.inject {|l1, l2|
          l1 + l2
        }
      end

      def pred?(loc)
        loc.is_a?(Location) &&
          loc.name == name &&
          loc.start_pos == end_pos
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
steep-0.32.0 lib/steep/ast/location.rb
steep-0.31.1 lib/steep/ast/location.rb
steep-0.31.0 lib/steep/ast/location.rb
steep-0.30.0 lib/steep/ast/location.rb
steep-0.29.0 lib/steep/ast/location.rb
steep-0.28.0 lib/steep/ast/location.rb
steep-0.27.0 lib/steep/ast/location.rb
steep-0.25.0 lib/steep/ast/location.rb
steep-0.24.0 lib/steep/ast/location.rb
steep-0.23.0 lib/steep/ast/location.rb
steep-0.22.0 lib/steep/ast/location.rb
steep-0.21.0 lib/steep/ast/location.rb
steep-0.20.0 lib/steep/ast/location.rb
steep-0.19.0 lib/steep/ast/location.rb
steep-0.18.0 lib/steep/ast/location.rb
steep-0.17.1 lib/steep/ast/location.rb
steep-0.17.0 lib/steep/ast/location.rb
steep-0.16.3 lib/steep/ast/location.rb
steep-0.16.2 lib/steep/ast/location.rb
steep-0.16.1 lib/steep/ast/location.rb