class Yoda::Parsing::Location
Attributes
@return [Integer] 0-indexed column number.
@todo Make this 0-indexed. @return [Integer] 1-indexed column number.
Public Class Methods
@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
@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
@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
@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
@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
@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
@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
@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
@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
@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
@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
@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
@return [{Symbol => Integer}]
# File lib/yoda/parsing/location.rb, line 84 def to_language_server_protocol_range { line: row - 1, character: column } end
# File lib/yoda/parsing/location.rb, line 88 def to_s "(#{row}, #{column})" end