Class: Yoda::Parsing::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/yoda/parsing/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row:, column:) ⇒ Location

Returns a new instance of Location.

Parameters:

  • row (Integer)

    1-indexed row number.

  • column (Integer)

    0-indexed column number.



15
16
17
18
# File 'lib/yoda/parsing/location.rb', line 15

def initialize(row:, column:)
  @row = row
  @column = column
end

Instance Attribute Details

#columnInteger (readonly)

Returns 0-indexed column number.

Returns:

  • (Integer)

    0-indexed column number.



11
12
13
# File 'lib/yoda/parsing/location.rb', line 11

def column
  @column
end

#rowInteger (readonly)

TODO:

Make this 0-indexed.

Returns 1-indexed column number.

Returns:

  • (Integer)

    1-indexed column number.



8
9
10
# File 'lib/yoda/parsing/location.rb', line 8

def row
  @row
end

Class Method Details

.first_columnObject



24
25
26
# File 'lib/yoda/parsing/location.rb', line 24

def self.first_column
  0
end

.first_rowObject



20
21
22
# File 'lib/yoda/parsing/location.rb', line 20

def self.first_row
  1
end

.of_ast_location(ast_location) ⇒ Location?

Parameters:

  • ast_location (Parser::Source::Map, Parser::Source::Range)

Returns:



31
32
33
34
# File 'lib/yoda/parsing/location.rb', line 31

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:) ⇒ Location

Parameters:

  • line (Integer)
  • character (Integer)

Returns:



39
40
41
# File 'lib/yoda/parsing/location.rb', line 39

def self.of_language_server_protocol_position(line:, character:)
  new(row: line + 1, column: character)
end

.valid_location?(location) ⇒ Boolean

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map, Object)

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/yoda/parsing/location.rb', line 44

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

Instance Method Details

#<=>(another) ⇒ Integer

Parameters:

Returns:

  • (Integer)


103
104
105
106
107
# File 'lib/yoda/parsing/location.rb', line 103

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) ⇒ Object

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map)


67
68
69
70
# File 'lib/yoda/parsing/location.rb', line 67

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) ⇒ Object

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map)


73
74
75
76
# File 'lib/yoda/parsing/location.rb', line 73

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) ⇒ Boolean

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map)

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/yoda/parsing/location.rb', line 56

def included?(location)
  return false unless self.class.valid_location?(location)
  after_begin(location) && before_last(location)
end

#index_of(source) ⇒ Object

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map)


51
52
53
# File 'lib/yoda/parsing/location.rb', line 51

def index_of(source)
  (source.split("\n").slice(0, row - 1) || []).map(&:length).reduce(0, &:+) + column
end

#later_than?(location) ⇒ Boolean

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map)

Returns:

  • (Boolean)


62
63
64
# File 'lib/yoda/parsing/location.rb', line 62

def later_than?(location)
  move(row: 0, column: -1).after_begin(location)
end

#move(row:, column:) ⇒ Location

Parameters:

  • row (Integer)
  • column (Integer)

Returns:



88
89
90
# File 'lib/yoda/parsing/location.rb', line 88

def move(row:, column:)
  self.class.new(row: @row + row, column: @column + column)
end

#offset_from_begin(location) ⇒ {Symbol => Numerical}

Parameters:

  • location (Parser::Source::Range, Parser::Source::Map)

Returns:

  • ({Symbol => Numerical})


80
81
82
83
# File 'lib/yoda/parsing/location.rb', line 80

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{Symbol => Integer}

Returns:

  • ({Symbol => Integer})


93
94
95
# File 'lib/yoda/parsing/location.rb', line 93

def to_language_server_protocol_range
  { line: row - 1, character: column }
end

#to_sObject



97
98
99
# File 'lib/yoda/parsing/location.rb', line 97

def to_s
  "(#{row}, #{column})"
end