Move integer parser to separate module
This commit is contained in:
parent
29ea4578e4
commit
6f86cd40d7
@ -5,7 +5,7 @@ mod names;
|
||||
mod terms;
|
||||
|
||||
pub(crate) use helpers::word_boundary;
|
||||
pub(crate) use literals::boolean;
|
||||
pub(crate) use literals::{boolean, integer};
|
||||
pub use names::{function_or_predicate_name, variable_name};
|
||||
pub use terms::term;
|
||||
pub use formulas::formula;
|
||||
|
@ -3,8 +3,9 @@ use nom::
|
||||
IResult,
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
combinator::map,
|
||||
sequence::terminated,
|
||||
character::complete::digit1,
|
||||
combinator::{map, map_res, opt, recognize},
|
||||
sequence::{pair, terminated},
|
||||
};
|
||||
|
||||
use super::word_boundary;
|
||||
@ -44,6 +45,33 @@ pub fn boolean(i: &str) -> IResult<&str, bool>
|
||||
))(i)
|
||||
}
|
||||
|
||||
pub fn integer(i: &str) -> IResult<&str, i32>
|
||||
{
|
||||
map_res
|
||||
(
|
||||
recognize
|
||||
(
|
||||
terminated
|
||||
(
|
||||
pair
|
||||
(
|
||||
opt
|
||||
(
|
||||
alt
|
||||
((
|
||||
tag("-"),
|
||||
tag("+"),
|
||||
))
|
||||
),
|
||||
digit1,
|
||||
),
|
||||
word_boundary,
|
||||
)
|
||||
),
|
||||
std::str::FromStr::from_str,
|
||||
)(i)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
@ -66,4 +94,27 @@ mod tests
|
||||
assert!(boolean("-").is_err());
|
||||
assert!(boolean(" ").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_integer()
|
||||
{
|
||||
assert_eq!(integer("0"), Ok(("", 0)));
|
||||
assert_eq!(integer("10000"), Ok(("", 10000)));
|
||||
assert_eq!(integer("+10000"), Ok(("", 10000)));
|
||||
assert_eq!(integer("-10000"), Ok(("", -10000)));
|
||||
assert_eq!(integer("0 42"), Ok((" 42", 0)));
|
||||
assert_eq!(integer("10000 42"), Ok((" 42", 10000)));
|
||||
assert_eq!(integer("+10000 42"), Ok((" 42", 10000)));
|
||||
assert_eq!(integer("-10000 42"), Ok((" 42", -10000)));
|
||||
assert_eq!(integer("10000,"), Ok((",", 10000)));
|
||||
assert_eq!(integer("+10000,"), Ok((",", 10000)));
|
||||
assert_eq!(integer("-10000,"), Ok((",", -10000)));
|
||||
assert!(integer("10000a").is_err());
|
||||
assert!(integer("+10000a").is_err());
|
||||
assert!(integer("-10000a").is_err());
|
||||
assert!(integer("1.5").is_err());
|
||||
assert!(integer("a").is_err());
|
||||
assert!(integer("-").is_err());
|
||||
assert!(integer(" ").is_err());
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,13 @@ use nom::
|
||||
IResult,
|
||||
branch::alt,
|
||||
bytes::complete::{escaped_transform, tag},
|
||||
character::complete::{digit1, multispace0, none_of},
|
||||
combinator::{map, map_res, opt, recognize},
|
||||
character::complete::{multispace0, none_of},
|
||||
combinator::{map, opt},
|
||||
multi::{many1, separated_list},
|
||||
sequence::{delimited, pair, preceded, terminated},
|
||||
};
|
||||
|
||||
use super::{Declarations, boolean, function_or_predicate_name, variable_name, word_boundary};
|
||||
use super::{Declarations, boolean, function_or_predicate_name, integer, variable_name, word_boundary};
|
||||
|
||||
fn negative<'i>(i: &'i str, d: &Declarations) -> IResult<&'i str, crate::Term>
|
||||
{
|
||||
@ -57,37 +57,6 @@ fn absolute_value<'i>(i: &'i str, d: &Declarations) -> IResult<&'i str, crate::T
|
||||
)(i)
|
||||
}
|
||||
|
||||
pub fn integer(i: &str) -> IResult<&str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
map_res
|
||||
(
|
||||
recognize
|
||||
(
|
||||
terminated
|
||||
(
|
||||
pair
|
||||
(
|
||||
opt
|
||||
(
|
||||
alt
|
||||
((
|
||||
tag("-"),
|
||||
tag("+"),
|
||||
))
|
||||
),
|
||||
digit1,
|
||||
),
|
||||
word_boundary,
|
||||
)
|
||||
),
|
||||
std::str::FromStr::from_str,
|
||||
),
|
||||
crate::Term::integer,
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn infimum(i: &str) -> IResult<&str, crate::Term>
|
||||
{
|
||||
map
|
||||
@ -292,7 +261,11 @@ fn term_precedence_0<'a>(i: &'a str, d: &Declarations) -> IResult<&'a str, crate
|
||||
crate::Term::Boolean,
|
||||
),
|
||||
special_integer,
|
||||
integer,
|
||||
map
|
||||
(
|
||||
integer,
|
||||
crate::Term::Integer,
|
||||
),
|
||||
map
|
||||
(
|
||||
|i| function(i, d),
|
||||
@ -793,29 +766,6 @@ mod tests
|
||||
assert_eq!(format_term("f( \n a \n + \n b \n , \n c \n + \n d \n)"), "f(a + b, c + d)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_integer()
|
||||
{
|
||||
assert_eq!(integer("0"), Ok(("", Term::integer(0))));
|
||||
assert_eq!(integer("10000"), Ok(("", Term::integer(10000))));
|
||||
assert_eq!(integer("+10000"), Ok(("", Term::integer(10000))));
|
||||
assert_eq!(integer("-10000"), Ok(("", Term::integer(-10000))));
|
||||
assert_eq!(integer("0 42"), Ok((" 42", Term::integer(0))));
|
||||
assert_eq!(integer("10000 42"), Ok((" 42", Term::integer(10000))));
|
||||
assert_eq!(integer("+10000 42"), Ok((" 42", Term::integer(10000))));
|
||||
assert_eq!(integer("-10000 42"), Ok((" 42", Term::integer(-10000))));
|
||||
assert_eq!(integer("10000,"), Ok((",", Term::integer(10000))));
|
||||
assert_eq!(integer("+10000,"), Ok((",", Term::integer(10000))));
|
||||
assert_eq!(integer("-10000,"), Ok((",", Term::integer(-10000))));
|
||||
assert!(integer("10000a").is_err());
|
||||
assert!(integer("+10000a").is_err());
|
||||
assert!(integer("-10000a").is_err());
|
||||
assert!(integer("1.5").is_err());
|
||||
assert!(integer("a").is_err());
|
||||
assert!(integer("-").is_err());
|
||||
assert!(integer(" ").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_special_integer()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user