Class: DMTool::Parser

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

Defined Under Namespace

Classes: DiceString, DieDirective

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



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

def initialize
  @history = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



2
3
4
# File 'lib/dmtool/parser.rb', line 2

def history
  @history
end

Instance Method Details

#parse!(input) ⇒ Object Also known as: parse



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dmtool/parser.rb', line 7

def parse!(input)
  history.push input
  command, remainder = input.split(/ /, 2).map(&:prep)
  dice_string, directives_string = remainder.to_s.split(',', 2).map(&:prep)

  if respond_to?("cmd_#{command}", true)
    ret = send("cmd_#{command}", dice_string, directives_string)
    ret = "[#{ret.join ', '}]" if ret.is_a? Array
    return "#{ret}"
  end
  raise ParserError.new "Command #{command} not found"
end