From deae1024053f45c3aa9376f64d27e9caf656c17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Wed, 26 Feb 2020 14:25:58 +0100 Subject: [PATCH] Require word boundaries around names --- src/parse/names.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/parse/names.rs b/src/parse/names.rs index df2ba9d..763cf5d 100644 --- a/src/parse/names.rs +++ b/src/parse/names.rs @@ -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() @@ -30,10 +32,14 @@ pub fn function_name(i: &str) -> IResult<&str, &str> { recognize ( - pair + terminated ( - take_while_m_n(1, 1, is_function_name_character_first), - take_while(is_function_name_character_body), + pair + ( + take_while_m_n(1, 1, is_function_name_character_first), + take_while(is_function_name_character_body), + ), + word_boundary, ) )(i) } @@ -47,10 +53,14 @@ pub fn variable_name(i: &str) -> IResult<&str, &str> { recognize ( - pair + terminated ( - take_while_m_n(1, 1, is_variable_name_character_first), - take_while(is_variable_name_character_body), + 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());