class Yoda::Parsing::Location

Attributes

column[R]

@return [Integer] 0-indexed column number.

row[R]

@todo Make this 0-indexed. @return [Integer] 1-indexed column number.

Public Class Methods

new(row:, column:) click to toggle source

@param row [Integer] 1-indexed row number. @param column [Integer] 0-indexed column number.

# File lib/yoda/parsing/location.rb, line 15
def initialize(row:, column:)
  @row = row
  @column = column
end
of_ast_location(ast_location) click to toggle source

@param ast_location [Parser::Source::Map, Parser::Source::Range] @return [Location, nil]

# File lib/yoda/parsing/location.rb, line 22
def self.of_ast_location(ast_location)
  return nil unless valid_location?(ast_location)
  Location.new(row: ast_location.line, column: ast_location.column)
end
of_language_server_protocol_position(line:, character:) click to toggle source

@param line [Integer] @param character [Integer] @return [Location]

# File lib/yoda/parsing/location.rb, line 30
def self.of_language_server_protocol_position(line:, character:)
  new(row: line + 1, column: character)
end
valid_location?(location) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map, Object]

# File lib/yoda/parsing/location.rb, line 35
def self.valid_location?(location)
  return false if !location.is_a?(::Parser::Source::Range) && !location.is_a?(::Parser::Source::Map)
  return false if location.is_a?(::Parser::Source::Map) && !location.expression
  true
end

Public Instance Methods

<=>(another) click to toggle source

@param another [Location] @return [Integer]

# File lib/yoda/parsing/location.rb, line 94
def <=>(another)
  return 0 if row == another.row && column == another.column
  return 1 if (row == another.row && column >= another.column) || row > another.row
  -1
end
after_begin(location) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map]

# File lib/yoda/parsing/location.rb, line 58
def after_begin(location)
  return false unless self.class.valid_location?(location)
  (location.line == row && location.column <= column) || location.line < row
end
before_last(location) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map]

# File lib/yoda/parsing/location.rb, line 64
def before_last(location)
  return false unless self.class.valid_location?(location)
  (location.last_line == row && column <= location.last_column ) || row < location.last_line
end
included?(location) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map]

# File lib/yoda/parsing/location.rb, line 47
def included?(location)
  return false unless self.class.valid_location?(location)
  after_begin(location) && before_last(location)
end
index_of(source) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map]

# File lib/yoda/parsing/location.rb, line 42
def index_of(source)
  (source.split("\n").slice(0, row - 1) || []).map(&:length).reduce(0, &:+) + column
end
later_than?(location) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map]

# File lib/yoda/parsing/location.rb, line 53
def later_than?(location)
  move(row: 0, column: -1).after_begin(location)
end
move(row:, column:) click to toggle source

@param row [Integer] @param column [Integer] @return [Location]

# File lib/yoda/parsing/location.rb, line 79
def move(row:, column:)
  self.class.new(row: @row + row, column: @column + column)
end
offset_from_begin(location) click to toggle source

@param location [Parser::Source::Range, Parser::Source::Map] @return [{Symbol => Numerical}]

# File lib/yoda/parsing/location.rb, line 71
def offset_from_begin(location)
  fail ArgumentError, location unless self.class.valid_location?(location)
  { line: row - location.line, column: column - location.column }
end
to_language_server_protocol_range() click to toggle source

@return [{Symbol => Integer}]

# File lib/yoda/parsing/location.rb, line 84
def to_language_server_protocol_range
  { line: row - 1, character: column }
end
to_s() click to toggle source
# File lib/yoda/parsing/location.rb, line 88
def to_s
  "(#{row}, #{column})"
end