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

.of_ast_location(ast_location) ⇒ Location?

Parameters:

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

Returns:



22
23
24
25
# 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:) ⇒ Location

Parameters:

  • line (Integer)
  • character (Integer)

Returns:



30
31
32
# 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) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)


35
36
37
38
39
# 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

Instance Method Details

#<=>(another) ⇒ Integer

Parameters:

Returns:

  • (Integer)


94
95
96
97
98
# 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) ⇒ Object

Parameters:

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


58
59
60
61
# 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) ⇒ Object

Parameters:

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


64
65
66
67
# 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) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)


47
48
49
50
# 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) ⇒ Object

Parameters:

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


42
43
44
# 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) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/yoda/parsing/location.rb', line 53

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

#move(row:, column:) ⇒ Location

Parameters:

  • row (Integer)
  • column (Integer)

Returns:



79
80
81
# 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) ⇒ {Symbol => Numerical}

Parameters:

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

Returns:

  • ({Symbol => Numerical})


71
72
73
74
# 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{Symbol => Integer}

Returns:

  • ({Symbol => Integer})


84
85
86
# File 'lib/yoda/parsing/location.rb', line 84

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

#to_sObject



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

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