diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/nom/tests/arithmetic.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/nom/tests/arithmetic.rs')
-rw-r--r-- | third_party/rust/nom/tests/arithmetic.rs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/third_party/rust/nom/tests/arithmetic.rs b/third_party/rust/nom/tests/arithmetic.rs new file mode 100644 index 0000000000..b3c38931df --- /dev/null +++ b/third_party/rust/nom/tests/arithmetic.rs @@ -0,0 +1,109 @@ +extern crate nom; + + +use nom::{ + IResult, + branch::alt, + combinator::map_res, + character::complete::char, + bytes::complete::tag, + character::complete::{digit1 as digit, space0 as space}, + multi::fold_many0, + sequence::{delimited, pair} +}; + +// Parser definition + +use std::str::FromStr; + +// We parse any expr surrounded by parens, ignoring all whitespaces around those +fn parens(i: &str) -> IResult<&str, i64> { + delimited( + space, + delimited( + tag("("), + expr, + tag(")") + ), + space + )(i) +} + +// We transform an integer string into a i64, ignoring surrounding whitespaces +// We look for a digit suite, and try to convert it. +// If either str::from_utf8 or FromStr::from_str fail, +// we fallback to the parens parser defined above +fn factor(i: &str) -> IResult<&str, i64> { + alt(( + map_res(delimited(space, digit, space), FromStr::from_str), + parens + ))(i) +} + +// We read an initial factor and for each time we find +// a * or / operator followed by another factor, we do +// the math by folding everything +fn term(i: &str) -> IResult<&str, i64> { + let (i, init) = factor(i)?; + + fold_many0( + pair(alt((char('*'), char('/'))), factor), + init, + |acc, (op, val): (char, i64)| { + if op == '*' { acc * val } else { acc / val } + } + )(i) +} + +fn expr(i: &str) -> IResult<&str, i64> { + let (i, init) = term(i)?; + + fold_many0( + pair(alt((char('+'), char('-'))), term), + init, + |acc, (op, val): (char, i64)| { + if op == '+' { acc + val } else { acc - val } + } + )(i) +} + +#[test] +fn factor_test() { + assert_eq!(factor("3"), Ok(("", 3))); + assert_eq!(factor(" 12"), Ok(("", 12))); + assert_eq!(factor("537 "), Ok(("", 537))); + assert_eq!(factor(" 24 "), Ok(("", 24))); +} + +#[test] +fn term_test() { + assert_eq!(term(" 12 *2 / 3"), Ok(("", 8))); + assert_eq!( + term(" 2* 3 *2 *2 / 3"), + Ok(("", 8)) + ); + assert_eq!(term(" 48 / 3/2"), Ok(("", 8))); +} + +#[test] +fn expr_test() { + assert_eq!(expr(" 1 + 2 "), Ok(("", 3))); + assert_eq!( + expr(" 12 + 6 - 4+ 3"), + Ok(("", 17)) + ); + assert_eq!(expr(" 1 + 2*3 + 4"), Ok(("", 11))); +} + +#[test] +fn parens_test() { + assert_eq!(expr(" ( 2 )"), Ok(("", 2))); + assert_eq!( + expr(" 2* ( 3 + 4 ) "), + Ok(("", 14)) + ); + assert_eq!( + expr(" 2*2 / ( 5 - 1) + 3"), + Ok(("", 4)) + ); +} |