Class: Parser::Source::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/source/range.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_buffer, begin_, end_) ⇒ Range

Returns a new instance of Range.



8
9
10
11
12
13
# File 'lib/parser/source/range.rb', line 8

def initialize(source_buffer, begin_, end_)
  @source_buffer = source_buffer
  @begin, @end = begin_, end_

  freeze
end

Instance Attribute Details

#beginObject (readonly)

Returns the value of attribute begin.



6
7
8
# File 'lib/parser/source/range.rb', line 6

def begin
  @begin
end

#endObject (readonly)

Returns the value of attribute end.



6
7
8
# File 'lib/parser/source/range.rb', line 6

def end
  @end
end

#source_bufferObject (readonly)

Returns the value of attribute source_buffer.



5
6
7
# File 'lib/parser/source/range.rb', line 5

def source_buffer
  @source_buffer
end

Instance Method Details

#begin_columnObject



25
26
27
28
29
# File 'lib/parser/source/range.rb', line 25

def begin_column
  _, column = @source_buffer.decompose_position(@begin)

  column
end

#column_rangeObject



37
38
39
# File 'lib/parser/source/range.rb', line 37

def column_range
  begin_column..end_column
end

#end_columnObject



31
32
33
34
35
# File 'lib/parser/source/range.rb', line 31

def end_column
  _, column = @source_buffer.decompose_position(@end)

  column
end

#inspectObject



60
61
62
# File 'lib/parser/source/range.rb', line 60

def inspect
  "#<Source::Range #{@source_buffer.name} #{@begin}..#{@end}>"
end

#join(other) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/parser/source/range.rb', line 50

def join(other)
  if other.source_buffer == @source_buffer
    Range.new(@source_buffer,
        [@begin, other.begin].min,
        [@end, other.end].max)
  else
    raise ArgumentError, "Cannot join SourceRanges for different SourceFiles"
  end
end

#lineObject



19
20
21
22
23
# File 'lib/parser/source/range.rb', line 19

def line
  line, _ = @source_buffer.decompose_position(@begin)

  line
end

#sizeObject



15
16
17
# File 'lib/parser/source/range.rb', line 15

def size
  @end - @begin + 1
end

#source_lineObject



41
42
43
# File 'lib/parser/source/range.rb', line 41

def source_line
  @source_buffer.source_line(line)
end

#to_sObject



45
46
47
48
# File 'lib/parser/source/range.rb', line 45

def to_s
  line, column = @source_buffer.decompose_position(@begin)
  [@source_buffer.name, line, column + 1].join(':')
end