Disallow reserved keywords as names

This commit is contained in:
Patrick Lühne 2020-03-27 04:37:37 +01:00
parent c127bc5eea
commit a7dd4d2fe9
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -30,6 +30,7 @@ fn is_variable_name_character_body(c: char) -> bool
pub fn function_or_predicate_name(i: &str) -> IResult<&str, &str> pub fn function_or_predicate_name(i: &str) -> IResult<&str, &str>
{ {
let (i, name) =
recognize recognize
( (
terminated terminated
@ -41,7 +42,20 @@ pub fn function_or_predicate_name(i: &str) -> IResult<&str, &str>
), ),
word_boundary, word_boundary,
) )
)(i) )(i)?;
match name
{
"and"
| "exists"
| "false"
| "forall"
| "not"
| "or"
| "true"
=> Err(nom::Err::Error((i, nom::error::ErrorKind::Verify))),
name => Ok((i, name)),
}
} }
pub fn variable_name(i: &str) -> IResult<&str, &str> pub fn variable_name(i: &str) -> IResult<&str, &str>
@ -80,6 +94,22 @@ mod tests
assert!(function_or_predicate_name("_ rest").is_err()); assert!(function_or_predicate_name("_ rest").is_err());
assert!(function_or_predicate_name("_predicate_123 rest").is_err()); assert!(function_or_predicate_name("_predicate_123 rest").is_err());
assert!(function_or_predicate_name(" ").is_err()); assert!(function_or_predicate_name(" ").is_err());
// Keywords arent valid names
assert!(function_or_predicate_name("and rest").is_err());
assert!(function_or_predicate_name("exists rest").is_err());
assert!(function_or_predicate_name("false rest").is_err());
assert!(function_or_predicate_name("forall rest").is_err());
assert!(function_or_predicate_name("or rest").is_err());
assert!(function_or_predicate_name("not rest").is_err());
assert!(function_or_predicate_name("true rest").is_err());
// Names that start with keywords are fine though
assert!(function_or_predicate_name("anda rest").is_ok());
assert!(function_or_predicate_name("existsa rest").is_ok());
assert!(function_or_predicate_name("falsea rest").is_ok());
assert!(function_or_predicate_name("foralla rest").is_ok());
assert!(function_or_predicate_name("ora rest").is_ok());
assert!(function_or_predicate_name("nota rest").is_ok());
assert!(function_or_predicate_name("truea rest").is_ok());
} }
#[test] #[test]