Class: ABNF::Compiler::TextStream
- Inherits:
-
Object
- Object
- ABNF::Compiler::TextStream
- Defined in:
- lib/abnf/compiler/text_stream.rb
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #==(other_stream) ⇒ Object
- #branch(&block) ⇒ Object
- #eof? ⇒ Boolean
-
#initialize(text) ⇒ TextStream
constructor
A new instance of TextStream.
- #match(regexp) ⇒ Object
- #next_character ⇒ Object
- #position ⇒ Object
- #position=(new_position) ⇒ Object (also: #seek)
- #rest ⇒ Object
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
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
4 5 6 |
# File 'lib/abnf/compiler/text_stream.rb', line 4 def stack @stack end |
#text ⇒ Object (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
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_character ⇒ Object
41 42 43 |
# File 'lib/abnf/compiler/text_stream.rb', line 41 def next_character text[position] end |
#position ⇒ Object
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 |
#rest ⇒ Object
54 55 56 |
# File 'lib/abnf/compiler/text_stream.rb', line 54 def rest text[position..-1] end |