Class: ABNF::Compiler::TextStream

Inherits:
Object
  • Object
show all
Defined in:
lib/abnf/compiler/text_stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ TextStream

Returns a new instance of TextStream.



7
8
9
10
# File 'lib/abnf/compiler/text_stream.rb', line 7

def initialize text
  @text = text
  @stack = [0]
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



4
5
6
# File 'lib/abnf/compiler/text_stream.rb', line 4

def stack
  @stack
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/abnf/compiler/text_stream.rb', line 5

def text
  @text
end

Instance Method Details

#==(other_stream) ⇒ Object



12
13
14
15
# File 'lib/abnf/compiler/text_stream.rb', line 12

def == other_stream
  return unless other_stream.is_a? self.class
  text == other_stream.text and stack == other_stream.stack
end

#branch(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/abnf/compiler/text_stream.rb', line 17

def branch &block
  next_position = position

  stack << position
  if block.()
    next_position = position
  end
  stack.pop

  self.position = next_position
end

#eof?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/abnf/compiler/text_stream.rb', line 29

def eof?
  position == text.size
end

#match(regexp) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/abnf/compiler/text_stream.rb', line 33

def match regexp
  match_data = regexp.match rest
  return unless match_data
  return unless match_data.pre_match.empty?
  self.position += match_data.to_s.size
  match_data
end

#next_characterObject



41
42
43
# File 'lib/abnf/compiler/text_stream.rb', line 41

def next_character
  text[position]
end

#positionObject



50
51
52
# File 'lib/abnf/compiler/text_stream.rb', line 50

def position
  stack[-1]
end

#position=(new_position) ⇒ Object Also known as: seek



45
46
47
# File 'lib/abnf/compiler/text_stream.rb', line 45

def position= new_position
  stack[-1] = new_position
end

#restObject



54
55
56
# File 'lib/abnf/compiler/text_stream.rb', line 54

def rest
  text[position..-1]
end