Move special integer parser to separate module

This commit is contained in:
Patrick Lühne 2020-03-27 03:09:48 +01:00
parent 6f86cd40d7
commit 7d78a504b1
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
3 changed files with 68 additions and 62 deletions

View File

@ -5,7 +5,7 @@ mod names;
mod terms; mod terms;
pub(crate) use helpers::word_boundary; pub(crate) use helpers::word_boundary;
pub(crate) use literals::{boolean, integer}; pub(crate) use literals::{boolean, integer, special_integer};
pub use names::{function_or_predicate_name, variable_name}; pub use names::{function_or_predicate_name, variable_name};
pub use terms::term; pub use terms::term;
pub use formulas::formula; pub use formulas::formula;

View File

@ -72,9 +72,45 @@ pub fn integer(i: &str) -> IResult<&str, i32>
)(i) )(i)
} }
fn infimum(i: &str) -> IResult<&str, crate::SpecialInteger>
{
map
(
terminated
(
tag("#inf"),
word_boundary,
),
|_| crate::SpecialInteger::Infimum,
)(i)
}
fn supremum(i: &str) -> IResult<&str, crate::SpecialInteger>
{
map
(
terminated
(
tag("#sup"),
word_boundary,
),
|_| crate::SpecialInteger::Supremum,
)(i)
}
pub fn special_integer(i: &str) -> IResult<&str, crate::SpecialInteger>
{
alt
((
infimum,
supremum,
))(i)
}
#[cfg(test)] #[cfg(test)]
mod tests mod tests
{ {
use crate::SpecialInteger;
use crate::parse::*; use crate::parse::*;
#[test] #[test]
@ -117,4 +153,28 @@ mod tests
assert!(integer("-").is_err()); assert!(integer("-").is_err());
assert!(integer(" ").is_err()); assert!(integer(" ").is_err());
} }
#[test]
fn parse_special_integer()
{
assert_eq!(special_integer("#inf"), Ok(("", SpecialInteger::Infimum)));
assert_eq!(special_integer("#sup"), Ok(("", SpecialInteger::Supremum)));
assert_eq!(special_integer("#inf #sup"), Ok((" #sup", SpecialInteger::Infimum)));
assert_eq!(special_integer("#sup #inf"), Ok((" #inf", SpecialInteger::Supremum)));
assert_eq!(special_integer("#inf,"), Ok((",", SpecialInteger::Infimum)));
assert_eq!(special_integer("#sup,"), Ok((",", SpecialInteger::Supremum)));
assert!(special_integer("#inf0").is_err());
assert!(special_integer("#sup0").is_err());
assert!(special_integer("#infimum").is_err());
assert!(special_integer("#supremum").is_err());
assert!(special_integer("inf").is_err());
assert!(special_integer("sup").is_err());
assert!(special_integer("0").is_err());
assert!(special_integer("10000").is_err());
assert!(special_integer("-10000").is_err());
assert!(special_integer("-").is_err());
assert!(special_integer("+").is_err());
assert!(special_integer("a").is_err());
assert!(special_integer(" ").is_err());
}
} }

View File

@ -9,7 +9,8 @@ use nom::
sequence::{delimited, pair, preceded, terminated}, sequence::{delimited, pair, preceded, terminated},
}; };
use super::{Declarations, boolean, function_or_predicate_name, integer, variable_name, word_boundary}; use super::{Declarations, boolean, function_or_predicate_name, integer, special_integer,
variable_name, word_boundary};
fn negative<'i>(i: &'i str, d: &Declarations) -> IResult<&'i str, crate::Term> fn negative<'i>(i: &'i str, d: &Declarations) -> IResult<&'i str, crate::Term>
{ {
@ -57,41 +58,6 @@ fn absolute_value<'i>(i: &'i str, d: &Declarations) -> IResult<&'i str, crate::T
)(i) )(i)
} }
fn infimum(i: &str) -> IResult<&str, crate::Term>
{
map
(
terminated
(
tag("#inf"),
word_boundary,
),
|_| crate::Term::infimum(),
)(i)
}
fn supremum(i: &str) -> IResult<&str, crate::Term>
{
map
(
terminated
(
tag("#sup"),
word_boundary,
),
|_| crate::Term::supremum(),
)(i)
}
pub fn special_integer(i: &str) -> IResult<&str, crate::Term>
{
alt
((
infimum,
supremum,
))(i)
}
pub fn string(i: &str) -> IResult<&str, crate::Term> pub fn string(i: &str) -> IResult<&str, crate::Term>
{ {
map map
@ -260,7 +226,11 @@ fn term_precedence_0<'a>(i: &'a str, d: &Declarations) -> IResult<&'a str, crate
boolean, boolean,
crate::Term::Boolean, crate::Term::Boolean,
), ),
special_integer, map
(
special_integer,
crate::Term::SpecialInteger,
),
map map
( (
integer, integer,
@ -766,30 +736,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)"); 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_special_integer()
{
assert_eq!(special_integer("#inf"), Ok(("", Term::infimum())));
assert_eq!(special_integer("#sup"), Ok(("", Term::supremum())));
assert_eq!(special_integer("#inf #sup"), Ok((" #sup", Term::infimum())));
assert_eq!(special_integer("#sup #inf"), Ok((" #inf", Term::supremum())));
assert_eq!(special_integer("#inf,"), Ok((",", Term::infimum())));
assert_eq!(special_integer("#sup,"), Ok((",", Term::supremum())));
assert!(special_integer("#inf0").is_err());
assert!(special_integer("#sup0").is_err());
assert!(special_integer("#infimum").is_err());
assert!(special_integer("#supremum").is_err());
assert!(special_integer("inf").is_err());
assert!(special_integer("sup").is_err());
assert!(special_integer("0").is_err());
assert!(special_integer("10000").is_err());
assert!(special_integer("-10000").is_err());
assert!(special_integer("-").is_err());
assert!(special_integer("+").is_err());
assert!(special_integer("a").is_err());
assert!(special_integer(" ").is_err());
}
#[test] #[test]
fn parse_string() fn parse_string()
{ {