Class: Steep::AST::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/ast/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer:, start_pos:, end_pos:) ⇒ Location

Returns a new instance of Location.



8
9
10
11
12
# File 'lib/steep/ast/location.rb', line 8

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



4
5
6
# File 'lib/steep/ast/location.rb', line 4

def buffer
  @buffer
end

#end_posObject (readonly)

Returns the value of attribute end_pos.



6
7
8
# File 'lib/steep/ast/location.rb', line 6

def end_pos
  @end_pos
end

#start_posObject (readonly)

Returns the value of attribute start_pos.



5
6
7
# File 'lib/steep/ast/location.rb', line 5

def start_pos
  @start_pos
end

Class Method Details

.concat(*locations) ⇒ Object



69
70
71
72
73
# File 'lib/steep/ast/location.rb', line 69

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

Instance Method Details

#+(other) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/steep/ast/location.rb', line 61

def +(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)
end

#==(other) ⇒ Object



54
55
56
57
58
59
# File 'lib/steep/ast/location.rb', line 54

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

#end_columnObject



34
35
36
# File 'lib/steep/ast/location.rb', line 34

def end_column
  end_loc[1]
end

#end_lineObject



30
31
32
# File 'lib/steep/ast/location.rb', line 30

def end_line
  end_loc[0]
end

#end_locObject



42
43
44
# File 'lib/steep/ast/location.rb', line 42

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

#inspectObject



14
15
16
# File 'lib/steep/ast/location.rb', line 14

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

#nameObject



18
19
20
# File 'lib/steep/ast/location.rb', line 18

def name
  buffer.name
end

#pred?(loc) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/steep/ast/location.rb', line 75

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

#sourceObject



46
47
48
# File 'lib/steep/ast/location.rb', line 46

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

#start_columnObject



26
27
28
# File 'lib/steep/ast/location.rb', line 26

def start_column
  start_loc[1]
end

#start_lineObject



22
23
24
# File 'lib/steep/ast/location.rb', line 22

def start_line
  start_loc[0]
end

#start_locObject



38
39
40
# File 'lib/steep/ast/location.rb', line 38

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

#to_sObject



50
51
52
# File 'lib/steep/ast/location.rb', line 50

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