Class: GamesDice::Parser

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/games_dice/parser.rb

Overview

Based on the parslet gem, this class defines the dice mini-language used by GamesDice.create

An instance of this class is a parser for the language. There are no user-definable instance variables.

Instance Method Summary collapse

Instance Method Details

#parse(dice_description) ⇒ Hash

Parses a string description in the dice mini-language, and returns data for feeding into GamesDice::Dice constructor.

Parameters:

  • dice_description (String)

    Text to parse e.g. ‘1d6’

Returns:

  • (Hash)

    Analysis of dice_description



74
75
76
77
78
79
80
# File 'lib/games_dice/parser.rb', line 74

def parse dice_description
  dice_description = dice_description.to_s.strip
  # Force first item to start '+' for simpler parse rules
  dice_description = '+' + dice_description unless dice_description =~ /\A[+-]/
  dice_expressions = super( dice_description )
  { :bunches => collect_bunches( dice_expressions ), :offset => collect_offset( dice_expressions ) }
end