Class: Akaza::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/akaza/parser.rb

Constant Summary collapse

EOF =
Class.new(StandardError)
SPACE =
" "
TAB =
"\t"
NL =
"\n"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Parser

Returns a new instance of Parser.



13
14
15
# File 'lib/akaza/parser.rb', line 13

def initialize(code)
  @io = StringIO.new(code)
end

Class Method Details

.parse(code) ⇒ Object



9
10
11
# File 'lib/akaza/parser.rb', line 9

def self.parse(code)
  self.new(code).parse
end

Instance Method Details

#parseObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/akaza/parser.rb', line 17

def parse
  commands = []

  loop do
    case c = nextc
    when SPACE
      commands << parse_stack
    when NL
      commands << parse_flow
    when TAB
      case nextc
      when SPACE
        commands << parse_calc
      when TAB
        commands << parse_heap
      when NL
        commands << parse_io
      end
    else
      raise "unreachable: #{c}"
    end
  rescue EOF
    return commands
  end
end