ExDiceRoller v1.0.0-rc.2 ExDiceRoller.Parser View Source
Functionality for parsing ExDiceRoller.Tokenizer.tokens/0
.
iex> {:ok, tokens} = ExDiceRoller.Tokenizer.tokenize("2d3+9-(ydz)d(31+x)/(3d8+2)")
{:ok,
[
{:int, 1, '2'},
{:roll, 1, 'd'},
{:int, 1, '3'},
{:basic_operator, 1, '+'},
{:int, 1, '9'},
{:basic_operator, 1, '-'},
{:"(", 1, '('},
{:var, 1, 'y'},
{:roll, 1, 'd'},
{:var, 1, 'z'},
{:")", 1, ')'},
{:roll, 1, 'd'},
{:"(", 1, '('},
{:int, 1, '31'},
{:basic_operator, 1, '+'},
{:var, 1, 'x'},
{:")", 1, ')'},
{:complex_operator, 1, '/'},
{:"(", 1, '('},
{:int, 1, '3'},
{:roll, 1, 'd'},
{:int, 1, '8'},
{:basic_operator, 1, '+'},
{:int, 1, '2'},
{:")", 1, ')'}
]}
iex> ExDiceRoller.Parser.parse(tokens)
{:ok,
{{:operator, '-'},
{{:operator, '+'}, {:roll, 2, 3}, 9},
{{:operator, '/'},
{:roll, {:roll, {:var, 'y'}, {:var, 'z'}},
{{:operator, '+'}, 31, {:var, 'x'}}},
{{:operator, '+'}, {:roll, 3, 8}, 2}}}}
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()
View Source
expression() :: number() | {{:operator, charlist()}, expression(), expression()} | {:roll, expression(), expression()} | {:var, charlist()} | {:sep, expression(), expression()}
Link to this section Functions
Link to this function
parse(tokens)
View Source
parse(ExDiceRoller.Tokenizer.tokens()) :: {:ok, expression()}
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,
[
{:int, 1, '2'},
{:roll, 1, 'd'},
{:int, 1, '8'},
{:basic_operator, 1, '+'},
{:"(", 1, '('},
{:int, 1, '1'},
{:basic_operator, 1, '+'},
{:int, 1, '2'},
{:")", 1, ')'}
]}
iex> {:ok, _} = ExDiceRoller.parse(tokens)
{:ok,
{{:operator, '+'}, {:roll, 2, 8},
{{:operator, '+'}, 1, 2}}}