Class: Kaiseki::Stream
Instance Attribute Summary collapse
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
- #column ⇒ Object
- #eof? ⇒ Boolean
- #getc ⇒ Object
- #goto(pos) ⇒ Object
-
#initialize(string) ⇒ Stream
constructor
A new instance of Stream.
- #length ⇒ Object (also: #size)
- #line ⇒ Object
- #lock(&block) ⇒ Object
- #match(regexp) ⇒ Object
- #to_s ⇒ Object
- #to_stream ⇒ Object
Constructor Details
Instance Attribute Details
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
3 4 5 |
# File 'lib/stream.rb', line 3 def pos @pos end |
Instance Method Details
#column ⇒ Object
81 82 83 |
# File 'lib/stream.rb', line 81 def column @pos - @newlines[self.line][0] end |
#eof? ⇒ Boolean
25 26 27 |
# File 'lib/stream.rb', line 25 def eof? @pos == @string.length end |
#getc ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/stream.rb', line 16 def getc if @pos < @string.length @pos += 1 @string[@pos - 1] else nil end end |
#goto(pos) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/stream.rb', line 39 def goto pos if pos < 0 @pos = 0 elsif pos > @string.length @pos = @string.length end end |
#length ⇒ Object Also known as: size
71 72 73 |
# File 'lib/stream.rb', line 71 def length @string.length end |
#line ⇒ Object
77 78 79 |
# File 'lib/stream.rb', line 77 def line bsearch @pos, 0, @newlines.length end |
#lock(&block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/stream.rb', line 47 def lock &block safe_pos = @pos begin catch :PredicateSuccess do return block.call end @pos = safe_pos throw :SkipSuccess rescue ParseError => e @pos = safe_pos e.line ||= self.line e.column ||= self.column raise e end end |
#match(regexp) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/stream.rb', line 29 def match regexp match = @string[@pos..-1].match /\A#{regexp}/ if match @pos += match.to_s.length match else nil end end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/stream.rb', line 63 def to_s @string.dup end |
#to_stream ⇒ Object
67 68 69 |
# File 'lib/stream.rb', line 67 def to_stream self end |