ExDiceRoller v0.3.0-alpha ExDiceRoller.Parser

Functionality for parsing ExDiceRoller.Tokenizer.tokens/0.

Link to this section Summary

Functions

Converts a series of tokens provided by tokenize/1 and parses them into an expression structure. This expression structure is what’s used by the dice rolling functions to calculate rolls. The BNF grammar definition file is located at src/dice_parser.yrl

Link to this section Types

Link to this type expression()
expression() ::
  {:digit, list()}
  | {{:operator, list()}, expression(), expression()}
  | {:roll, expression(), expression()}
  | {:var, String.t()}

Link to this section Functions

Converts a series of tokens provided by tokenize/1 and parses them into an expression structure. This expression structure is what’s used by the dice rolling functions to calculate rolls. The BNF grammar definition file is located at src/dice_parser.yrl.

iex> {:ok, tokens} = ExDiceRoller.tokenize("2d8 + (1+2)")
{:ok,
[
  {:digit, 1, '2'},
  {:roll, 1, 'd'},
  {:digit, 1, '8'},
  {:basic_operator, 1, '+'},
  {:"(", 1, '('},
  {:digit, 1, '1'},
  {:basic_operator, 1, '+'},
  {:digit, 1, '2'},
  {:")", 1, ')'}
]}
iex> {:ok, _} = ExDiceRoller.parse(tokens)
{:ok,
{{:operator, '+'}, {:roll, {:digit, '2'}, {:digit, '8'}},
  {{:operator, '+'}, {:digit, '1'}, {:digit, '2'}}}}