Class: RLSL::BaseTranslator::CodeScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/rlsl/base_translator/code_scanner.rb

Defined Under Namespace

Classes: Segment

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ CodeScanner

Returns a new instance of CodeScanner.



12
13
14
# File 'lib/rlsl/base_translator/code_scanner.rb', line 12

def initialize(code)
  @code = code.to_s
end

Instance Method Details

#each_segment {|Segment.new(type: :code, text: buffer)| ... } ⇒ Object

Yields:

  • (Segment.new(type: :code, text: buffer))


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rlsl/base_translator/code_scanner.rb', line 16

def each_segment
  return enum_for(:each_segment) unless block_given?

  buffer = +""
  index = 0

  while index < @code.length
    preserved, next_index = read_preserved_segment(index)
    unless preserved
      buffer << @code[index]
      index += 1
      next
    end

    yield Segment.new(type: :code, text: buffer) unless buffer.empty?
    buffer = +""
    yield Segment.new(type: preserved[:type], text: preserved[:text])
    index = next_index
  end

  yield Segment.new(type: :code, text: buffer) unless buffer.empty?
end