Expand description
Parser generator functions and implementations of str::FromStr
for all dice and expression data structures.
Requires the parse
feature (enabled by default).
The parser generators generate parsers for parsing dice, dice modifiers, modifier conditions, and full mathematical
dice expressions (such as 4d8 + 2d6x - 3
) from strings. They’re all made with [chumsky] and are almost entirely
zero-copy. A parser can be used directly by calling [Parser::parse()
] on it.
§Examples
§Parsing Dice
use tyche::Dice;
let dice: Dice = "6d8x".parse()?;
assert_eq!(dice, Dice::builder().count(6).sides(8).explode(None, true).build());
§Parsing expressions
use tyche::{Dice, Expr};
let expr: Expr = "6d8x + 4d6 - 3".parse()?;
assert_eq!(
expr,
Expr::Sub(
Box::new(Expr::Add(
Box::new(Expr::Dice(
Dice::builder().count(6).sides(8).explode(None, true).build()
)),
Box::new(Expr::Dice(Dice::new(4, 6))),
)),
Box::new(Expr::Num(3)),
)
);
Structs§
- Error
- Error that can occur while parsing a string into a dice or expression-related structure via
alloc::str::FromStr
.
Traits§
- GenParser
- Trait to allow convenient access to a parser generator for any implementing type
Functions§
- condition
- Generates a parser that specifically handles dice modifier conditions like “<3”, “>=5”, “=1”, “1”, etc. and expects end of input
- condition_
part - Generates a parser that specifically handles dice modifier conditions like “<3”, “>=5”, “=1”, “1”, etc.
- dice
- Generates a parser that specifically handles dice with or without modifiers like “d20”, “2d20kh”, “8d6x”, etc. and expects end of input
- dice_
part - Generates a parser that specifically handles dice with or without modifiers like “d20”, “2d20kh”, “8d6x”, etc.
- expr
- Generates a parser that handles full expressions including mathematical operations, grouping with parentheses, dice, etc. and expects end of input
- expr_
part - Generates a parser that handles full expressions including mathematical operations, grouping with parentheses, dice, etc.
- modifier
- Generates a parser that specifically handles dice modifiers with conditions like “r1”, “xo>4”, “kh”, etc. and expects end of input
- modifier_
list - Generates a parser that specifically handles dice modifier lists with conditions like “r1kh4”, “r1xo>4kh4”, etc. and expects end of input
- modifier_
list_ part - Generates a parser that specifically handles dice modifier lists with conditions like “r1kh4”, “r1xo>4kh4”, etc.
- modifier_
part - Generates a parser that specifically handles dice modifiers with conditions like “r1”, “xo>4”, “kh”, etc.