Class: DMTool::Parser
- Inherits:
-
Object
- Object
- DMTool::Parser
- Defined in:
- lib/dmtool/parser.rb
Defined Under Namespace
Classes: DiceString, DieDirective
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
Returns the value of attribute history.
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse!(input) ⇒ Object (also: #parse)
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
3 4 5 |
# File 'lib/dmtool/parser.rb', line 3 def initialize @history = [] end |
Instance Attribute Details
#history ⇒ Object (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 |