Class: StringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/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/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/parser/string.rb', line 3

def eos
  @eos
end

#iObject (readonly)

Returns the value of attribute i.



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

def i
  @i
end

#lenObject (readonly)

Returns the value of attribute len.



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

def len
  @len
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

Instance Method Details

#eos?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/parser/string.rb', line 36

def eos?
  @eos
end

#grabObject



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

def grab
  return nil if @eos
  char = @line[@i]
  @i += 1
  check_eos
  char
end

#next!Object



27
28
29
# File 'lib/parser/string.rb', line 27

def next!
  @line[@i + 1]
end

#peekObject



40
41
42
43
# File 'lib/parser/string.rb', line 40

def peek
  return nil if @eos
  @line[@i]
end

#prevObject



31
32
33
34
# File 'lib/parser/string.rb', line 31

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

#skip_spacesObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/parser/string.rb', line 45

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

#ungrabObject



22
23
24
25
# File 'lib/parser/string.rb', line 22

def ungrab
  @i -= 1
  check_eos
end