Class: Parser::Source::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_cover/parser_ext/range.rb

Instance Method Summary collapse

Instance Method Details

#lstrip(pattern = /\s*/) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/deep_cover/parser_ext/range.rb', line 18

def lstrip(pattern = /\s*/)
  if (match = /^#{pattern}/.match(source))
    with(begin_pos: @begin_pos + match[0].length)
  else
    self
  end
end

#rstrip(pattern = /\s*/) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/deep_cover/parser_ext/range.rb', line 26

def rstrip(pattern = /\s*/)
  if (match = /#{pattern}$/.match(source))
    with(end_pos: @end_pos - match[0].length)
  else
    self
  end
end

#split(*inner_ranges) ⇒ Object

(1…10).split(2…3, 6…8) => [1…2, 3…6, 7…10] Assumes inner_ranges are exclusive, and included in self



10
11
12
13
14
15
16
# File 'lib/deep_cover/parser_ext/range.rb', line 10

def split(*inner_ranges)
  inner_ranges.sort_by!(&:begin_pos)
  [self.begin, *inner_ranges, self.end]
    .each_cons(2)
    .map { |i, j| with(begin_pos: i.end_pos, end_pos: j.begin_pos) }
    .reject(&:empty?)
end

#strip(pattern = /\s*/) ⇒ Object



34
35
36
# File 'lib/deep_cover/parser_ext/range.rb', line 34

def strip(pattern = /\s*/)
  lstrip(pattern).rstrip(pattern)
end

#with(begin_pos: @begin_pos, end_pos: @end_pos) ⇒ Object



4
5
6
# File 'lib/deep_cover/parser_ext/range.rb', line 4

def with(begin_pos: @begin_pos, end_pos: @end_pos)
  Parser::Source::Range.new(@source_buffer, begin_pos, end_pos)
end