Class: EDN::CharStream

Inherits:
Object
  • Object
show all
Defined in:
lib/edn/char_stream.rb

Instance Method Summary collapse

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

#advanceObject


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

Returns:

  • (Boolean)

25
26
27
# File 'lib/edn/char_stream.rb', line 25

def alpha?(c=current)
  /[a-zA-Z]/ =~ c
end

#currentObject


11
12
13
14
# File 'lib/edn/char_stream.rb', line 11

def current
  return @current if @current
  advance
end

#digit?(c = current) ⇒ Boolean

Returns:

  • (Boolean)

21
22
23
# File 'lib/edn/char_stream.rb', line 21

def digit?(c=current)
  /[0-9]/ =~ c
end

#eof?(c = current) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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, error_message=nil)
  if current == expected
    advance
    return expected
  elsif error_message
    raise error_message
  end
  nil
end

#skip_to_eolObject


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_wsObject


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

Returns:

  • (Boolean)

33
34
35
# File 'lib/edn/char_stream.rb', line 33

def ws?(c=current)
  /[ \t\r\n,]/ =~ c
end