This commit is contained in:
Taylor C. Richberger 2020-04-20 10:02:26 -06:00
parent 1ce4b54417
commit 9121497d17
2 changed files with 71 additions and 27 deletions

View File

@ -11,6 +11,7 @@ reqwest = "0.10"
serde_json = "1" serde_json = "1"
toml = "0.5" toml = "0.5"
nom = "5" nom = "5"
rand = "0.7"
[dependencies.serde] [dependencies.serde]
version = "1" version = "1"

View File

@ -1,13 +1,10 @@
use nom::{ use nom::{
alt, alt,
tag,
complete,
named,
many0,
IResult,
bytes::complete::{tag, take_while}, bytes::complete::{tag, take_while},
character::complete::digit1, character::complete::digit1,
sequence::tuple complete, many0, named,
sequence::tuple,
tag, IResult,
}; };
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -18,10 +15,7 @@ struct Dice {
impl Dice { impl Dice {
fn new(count: u32, sides: u32) -> Dice { fn new(count: u32, sides: u32) -> Dice {
Dice { Dice { count, sides }
count,
sides,
}
} }
} }
@ -58,7 +52,10 @@ fn eat_whitespace(input: &str) -> IResult<&str, ()> {
// Parse a dice expression. Does not eat whitespace // Parse a dice expression. Does not eat whitespace
fn parse_dice(input: &str) -> IResult<&str, Dice> { fn parse_dice(input: &str) -> IResult<&str, Dice> {
let (input, (count, _, sides)) = tuple((digit1, tag("d"), digit1))(input)?; let (input, (count, _, sides)) = tuple((digit1, tag("d"), digit1))(input)?;
Ok((input, Dice::new(count.parse().unwrap(), sides.parse().unwrap()))) Ok((
input,
Dice::new(count.parse().unwrap(), sides.parse().unwrap()),
))
} }
// Parse a single digit expression. Does not eat whitespace // Parse a single digit expression. Does not eat whitespace
@ -136,29 +133,75 @@ mod tests {
#[test] #[test]
fn element_test() { fn element_test() {
assert_eq!(parse_element(" \t\n\r\n 8d7 \n"), Ok((" \n", Element::Dice(Dice::new(8, 7))))); assert_eq!(
assert_eq!(parse_element(" \t\n\r\n 8 \n"), Ok((" \n", Element::Bonus(8)))); parse_element(" \t\n\r\n 8d7 \n"),
Ok((" \n", Element::Dice(Dice::new(8, 7))))
);
assert_eq!(
parse_element(" \t\n\r\n 8 \n"),
Ok((" \n", Element::Bonus(8)))
);
} }
#[test] #[test]
fn signed_element_test() { fn signed_element_test() {
assert_eq!(parse_signed_element("+ 7"), Ok(("", SignedElement::Positive(Element::Bonus(7))))); assert_eq!(
assert_eq!(parse_signed_element(" \t\n\r\n- 8 \n"), Ok((" \n", SignedElement::Negative(Element::Bonus(8))))); parse_signed_element("+ 7"),
assert_eq!(parse_signed_element(" \t\n\r\n- 8d4 \n"), Ok((" \n", SignedElement::Negative(Element::Dice(Dice::new(8, 4)))))); Ok(("", SignedElement::Positive(Element::Bonus(7))))
assert_eq!(parse_signed_element(" \t\n\r\n+ 8d4 \n"), Ok((" \n", SignedElement::Positive(Element::Dice(Dice::new(8, 4)))))); );
assert_eq!(
parse_signed_element(" \t\n\r\n- 8 \n"),
Ok((" \n", SignedElement::Negative(Element::Bonus(8))))
);
assert_eq!(
parse_signed_element(" \t\n\r\n- 8d4 \n"),
Ok((
" \n",
SignedElement::Negative(Element::Dice(Dice::new(8, 4)))
))
);
assert_eq!(
parse_signed_element(" \t\n\r\n+ 8d4 \n"),
Ok((
" \n",
SignedElement::Positive(Element::Dice(Dice::new(8, 4)))
))
);
} }
#[test] #[test]
fn element_expression_test() { fn element_expression_test() {
assert_eq!(parse_element_expression("8d4"), Ok(("", ElementExpression(vec![SignedElement::Positive(Element::Dice(Dice::new(8, 4)))])))); assert_eq!(
assert_eq!(parse_element_expression(" - 8d4 \n "), Ok((" \n ", ElementExpression(vec![SignedElement::Negative(Element::Dice(Dice::new(8, 4)))])))); parse_element_expression("8d4"),
assert_eq!(parse_element_expression("\t3d4 + 7 - 5 - 6d12 + 1d1 + 53 1d5 "), Ok((" 1d5 ", ElementExpression(vec![ Ok((
SignedElement::Positive(Element::Dice(Dice::new(3, 4))), "",
SignedElement::Positive(Element::Bonus(7)), ElementExpression(vec![SignedElement::Positive(Element::Dice(Dice::new(
SignedElement::Negative(Element::Bonus(5)), 8, 4
SignedElement::Negative(Element::Dice(Dice::new(6, 12))), )))])
SignedElement::Positive(Element::Dice(Dice::new(1, 1))), ))
SignedElement::Positive(Element::Bonus(53)), );
])))); assert_eq!(
parse_element_expression(" - 8d4 \n "),
Ok((
" \n ",
ElementExpression(vec![SignedElement::Negative(Element::Dice(Dice::new(
8, 4
)))])
))
);
assert_eq!(
parse_element_expression("\t3d4 + 7 - 5 - 6d12 + 1d1 + 53 1d5 "),
Ok((
" 1d5 ",
ElementExpression(vec![
SignedElement::Positive(Element::Dice(Dice::new(3, 4))),
SignedElement::Positive(Element::Bonus(7)),
SignedElement::Negative(Element::Bonus(5)),
SignedElement::Negative(Element::Dice(Dice::new(6, 12))),
SignedElement::Positive(Element::Dice(Dice::new(1, 1))),
SignedElement::Positive(Element::Bonus(53)),
])
))
);
} }
} }