Require word boundaries around names

This commit is contained in:
Patrick Lühne 2020-02-26 14:25:58 +01:00
parent 0fc8506164
commit deae102405
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -3,9 +3,11 @@ use nom::
IResult,
bytes::complete::{take_while, take_while_m_n},
combinator::recognize,
sequence::pair,
sequence::{pair, terminated},
};
use super::word_boundary;
fn is_function_name_character_first(c: char) -> bool
{
c.is_alphabetic() && c.is_lowercase()
@ -29,11 +31,15 @@ fn is_variable_name_character_body(c: char) -> bool
pub fn function_name(i: &str) -> IResult<&str, &str>
{
recognize
(
terminated
(
pair
(
take_while_m_n(1, 1, is_function_name_character_first),
take_while(is_function_name_character_body),
),
word_boundary,
)
)(i)
}
@ -46,11 +52,15 @@ pub fn predicate_name(i: &str) -> IResult<&str, &str>
pub fn variable_name(i: &str) -> IResult<&str, &str>
{
recognize
(
terminated
(
pair
(
take_while_m_n(1, 1, is_variable_name_character_first),
take_while(is_variable_name_character_body),
),
word_boundary,
)
)(i)
}
@ -78,6 +88,7 @@ mod tests
fn parse_predicate_name()
{
assert_eq!(predicate_name("p rest"), Ok((" rest", "p")));
assert_eq!(predicate_name("p, rest"), Ok((", rest", "p")));
assert_eq!(predicate_name("predicate_123 rest"), Ok((" rest", "predicate_123")));
assert!(predicate_name("0 rest").is_err());
assert!(predicate_name("123_asd rest").is_err());
@ -92,6 +103,7 @@ mod tests
fn parse_variable_name()
{
assert_eq!(variable_name("X Rest"), Ok((" Rest", "X")));
assert_eq!(variable_name("X, Rest"), Ok((", Rest", "X")));
assert_eq!(variable_name("Variable_123 Rest"), Ok((" Rest", "Variable_123")));
assert!(variable_name("0 Rest").is_err());
assert!(variable_name("123_Asd Rest").is_err());