Class: EDN::CharStream
- Inherits:
-
Object
- Object
- EDN::CharStream
- Defined in:
- lib/edn/char_stream.rb
Instance Method Summary collapse
- #advance ⇒ Object
- #alpha?(c = current) ⇒ Boolean
- #current ⇒ Object
- #digit?(c = current) ⇒ Boolean
- #eof?(c = current) ⇒ Boolean
- #gather(pattern) ⇒ Object
-
#initialize(io = $stdin) ⇒ CharStream
constructor
A new instance of CharStream.
- #newline?(c = current) ⇒ Boolean
- #repeat(pattern, &block) ⇒ Object
- #skip_past(expected, error_message = nil) ⇒ Object
- #skip_to_eol ⇒ Object
- #skip_ws ⇒ Object
- #ws?(c = current) ⇒ Boolean
Constructor Details
#initialize(io = $stdin) ⇒ CharStream
Returns a new instance of CharStream.
6 7 8 9 |
# File 'lib/edn/char_stream.rb', line 6 def initialize(io=$stdin) @io = io @current = nil end |
Instance Method Details
#advance ⇒ Object
16 17 18 19 |
# File 'lib/edn/char_stream.rb', line 16 def advance return @current if @current == :eof @current = @io.getc || :eof end |
#alpha?(c = current) ⇒ Boolean
25 26 27 |
# File 'lib/edn/char_stream.rb', line 25 def alpha?(c=current) /[a-zA-Z]/ =~ c end |
#current ⇒ Object
11 12 13 14 |
# File 'lib/edn/char_stream.rb', line 11 def current return @current if @current advance end |
#digit?(c = current) ⇒ Boolean
21 22 23 |
# File 'lib/edn/char_stream.rb', line 21 def digit?(c=current) /[0-9]/ =~ c end |
#eof?(c = current) ⇒ Boolean
29 30 31 |
# File 'lib/edn/char_stream.rb', line 29 def eof?(c=current) c == :eof end |
#gather(pattern) ⇒ Object
50 51 52 53 54 |
# File 'lib/edn/char_stream.rb', line 50 def gather(pattern) repeat(pattern) do |result, ch| result << ch end end |
#newline?(c = current) ⇒ Boolean
37 38 39 |
# File 'lib/edn/char_stream.rb', line 37 def newline?(c=current) /[\n\r]/ =~ c end |
#repeat(pattern, &block) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/edn/char_stream.rb', line 41 def repeat(pattern, &block) result = nil while current =~ pattern result ||= '' result = block.call(result, current) end result end |
#skip_past(expected, error_message = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/edn/char_stream.rb', line 56 def skip_past(expected, =nil) if current == expected advance return expected elsif raise end nil end |
#skip_to_eol ⇒ Object
66 67 68 69 70 |
# File 'lib/edn/char_stream.rb', line 66 def skip_to_eol until current == :eof || newline? advance end end |
#skip_ws ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/edn/char_stream.rb', line 72 def skip_ws while current != :eof if ws?(current) advance elsif current == ';' skip_to_eol else break end end end |
#ws?(c = current) ⇒ Boolean
33 34 35 |
# File 'lib/edn/char_stream.rb', line 33 def ws?(c=current) /[ \t\r\n,]/ =~ c end |