Implement booleans

This commit is contained in:
Patrick Lühne 2020-02-26 14:15:33 +01:00
parent e6a5c20d42
commit 0fc8506164
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 61 additions and 1 deletions

View File

@ -4,7 +4,7 @@ mod terms;
pub(crate) use helpers::word_boundary; pub(crate) use helpers::word_boundary;
pub use names::{function_name, predicate_name, variable_name}; pub use names::{function_name, predicate_name, variable_name};
pub use terms::{integer, special_integer, string}; pub use terms::{boolean, integer, special_integer, string};
/* /*
use nom:: use nom::

View File

@ -10,6 +10,41 @@ use nom::
use super::word_boundary; use super::word_boundary;
fn true_(i: &str) -> IResult<&str, crate::Term>
{
map
(
terminated
(
tag("true"),
word_boundary,
),
|_| crate::Term::true_(),
)(i)
}
fn false_(i: &str) -> IResult<&str, crate::Term>
{
map
(
terminated
(
tag("false"),
word_boundary,
),
|_| crate::Term::false_(),
)(i)
}
pub fn boolean(i: &str) -> IResult<&str, crate::Term>
{
alt
((
true_,
false_,
))(i)
}
pub fn integer(i: &str) -> IResult<&str, crate::Term> pub fn integer(i: &str) -> IResult<&str, crate::Term>
{ {
map map
@ -175,6 +210,24 @@ mod tests
use crate::parse::*; use crate::parse::*;
use crate::Term; use crate::Term;
#[test]
fn parse_boolean()
{
assert_eq!(boolean("true"), Ok(("", Term::true_())));
assert_eq!(boolean("false"), Ok(("", Term::false_())));
assert_eq!(boolean("true false"), Ok((" false", Term::true_())));
assert_eq!(boolean("false true"), Ok((" true", Term::false_())));
assert_eq!(boolean("true,"), Ok((",", Term::true_())));
assert_eq!(boolean("false,"), Ok((",", Term::false_())));
assert!(boolean("truefalse").is_err());
assert!(boolean("falsetrue").is_err());
assert!(boolean("truea").is_err());
assert!(boolean("falsea").is_err());
assert!(boolean("a").is_err());
assert!(boolean("-").is_err());
assert!(boolean(" ").is_err());
}
#[test] #[test]
fn parse_integer() fn parse_integer()
{ {
@ -216,6 +269,9 @@ mod tests
assert!(special_integer("0").is_err()); assert!(special_integer("0").is_err());
assert!(special_integer("10000").is_err()); assert!(special_integer("10000").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()); assert!(special_integer(" ").is_err());
} }
@ -243,5 +299,9 @@ mod tests
assert_eq!(string("\"test 🙂 123\""), Ok(("", Term::string("test 🙂 123".to_string())))); assert_eq!(string("\"test 🙂 123\""), Ok(("", Term::string("test 🙂 123".to_string()))));
assert!(string("\"test 123\"a").is_err()); assert!(string("\"test 123\"a").is_err());
assert!(string("\"test\\i123\"").is_err()); assert!(string("\"test\\i123\"").is_err());
assert!(string("\"test").is_err());
assert!(string("test").is_err());
assert!(string("-").is_err());
assert!(string(" ").is_err());
} }
} }