Class: StringParser

Inherits:
Object show all
Defined in:
lib/livetext/parser/string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ StringParser

Returns a new instance of StringParser.

Raises:

  • (NilValue)


5
6
7
8
9
10
11
12
# File 'lib/livetext/parser/string.rb', line 5

def initialize(line)
  raise NilValue if line.nil?
  raise ExpectedString unless String === line
  @line = line
  @len = @line.length
  @eos = @len == 0 ? true : false
  @i = 0
end

Instance Attribute Details

#eosObject (readonly)

Returns the value of attribute eos.



3
4
5
# File 'lib/livetext/parser/string.rb', line 3

def eos
  @eos
end

#iObject (readonly)

Returns the value of attribute i.



3
4
5
# File 'lib/livetext/parser/string.rb', line 3

def i
  @i
end

#lenObject (readonly)

Returns the value of attribute len.



3
4
5
# File 'lib/livetext/parser/string.rb', line 3

def len
  @len
end

#lineObject (readonly)

Returns the value of attribute line.



3
4
5
# File 'lib/livetext/parser/string.rb', line 3

def line
  @line
end

Instance Method Details

#eos?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/livetext/parser/string.rb', line 43

def eos?
  @eos
end

#grab(n = 1) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/livetext/parser/string.rb', line 14

def grab(n = 1)
  raise "n <= 0 for #grab" if n <= 0
  return nil if @eos
  i2 = @i + n - 1
  char = @line[@i..i2]
  @i += n
  check_eos
  char
end

#lookaheadObject



29
30
31
32
# File 'lib/livetext/parser/string.rb', line 29

def lookahead
  # Get rid of this?
  @line[@i + 1]
end

#peek(n = 1) ⇒ Object



47
48
49
50
51
52
# File 'lib/livetext/parser/string.rb', line 47

def peek(n = 1)
  raise "n <= 0 for #grab" if n <= 0
  return nil if @eos
  i2 = @i + n - 1
  @line[@i..i2]
end

#prevObject



38
39
40
41
# File 'lib/livetext/parser/string.rb', line 38

def prev
  return nil if @i <= 0
  @line[@i-1]
end

#remainderObject



34
35
36
# File 'lib/livetext/parser/string.rb', line 34

def remainder
  @line[@i..-1]
end

#skip_spacesObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/livetext/parser/string.rb', line 54

def skip_spaces
  char = nil
  loop do
    char = peek
    break if eos?
    break if char != " "
    char = grab
  end
  char
end

#ungrabObject



24
25
26
27
# File 'lib/livetext/parser/string.rb', line 24

def ungrab
  @i -= 1
  check_eos
end