Implement name parsing
This commit is contained in:
parent
91918cf645
commit
0c057211ed
115
src/parse.rs
115
src/parse.rs
@ -1,3 +1,8 @@
|
||||
mod names;
|
||||
|
||||
pub use names::{function_name, predicate_name, variable_name};
|
||||
|
||||
/*
|
||||
use nom::
|
||||
{
|
||||
IResult,
|
||||
@ -14,7 +19,7 @@ struct Declarations
|
||||
{
|
||||
//function_declarations:,
|
||||
//predicate_declarations:,
|
||||
variable_declaration_stack: VariableDeclarationStack,
|
||||
//variable_declaration_stack: VariableDeclarationStack,
|
||||
}
|
||||
|
||||
enum TermOperator
|
||||
@ -24,103 +29,6 @@ enum TermOperator
|
||||
Multiply,
|
||||
}
|
||||
|
||||
fn infimum<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited(multispace0, tag("#inf"), multispace0),
|
||||
|_| crate::Term::SpecialInteger(crate::SpecialInteger::Infimum)
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn supremum<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited(multispace0, tag("#sup"), multispace0),
|
||||
|_| crate::Term::SpecialInteger(crate::SpecialInteger::Supremum)
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn integer<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
map_res
|
||||
(
|
||||
delimited(multispace0, digit1, multispace0),
|
||||
std::str::FromStr::from_str
|
||||
),
|
||||
crate::Term::Integer
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn is_lowercase_alphanumeric(c: char) -> bool
|
||||
{
|
||||
c.is_alphanumeric() && c.is_lowercase()
|
||||
}
|
||||
|
||||
fn symbolic_identifier<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, String>
|
||||
{
|
||||
let (i, symbolic_identifier) = map
|
||||
(
|
||||
pair
|
||||
(
|
||||
take_while_m_n(1, 1, is_lowercase_alphanumeric),
|
||||
take_while(char::is_alphanumeric)
|
||||
),
|
||||
|(s0, s1)| format!("{}{}", s0, s1)
|
||||
)(i)?;
|
||||
|
||||
Ok((i, symbolic_identifier))
|
||||
}
|
||||
|
||||
fn symbolic<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited(multispace0, |i| symbolic_identifier(i, declarations), multispace0),
|
||||
crate::Term::Symbolic
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn string<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited
|
||||
(
|
||||
multispace0,
|
||||
delimited
|
||||
(
|
||||
tag("\""),
|
||||
is_not("\""),
|
||||
tag("\""),
|
||||
),
|
||||
multispace0
|
||||
),
|
||||
|s: &str| crate::Term::String(s.to_string())
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn variable_identifier<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, String>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited
|
||||
(
|
||||
multispace0,
|
||||
preceded
|
||||
(
|
||||
take_while_m_n(1, 1, |c| char::is_alphabetic(c) && char::is_uppercase(c)),
|
||||
take_while(char::is_alphanumeric)
|
||||
),
|
||||
multispace0
|
||||
),
|
||||
|s: &str| s.to_string()
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn variable_declaration<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::VariableDeclaration>
|
||||
{
|
||||
map
|
||||
@ -128,8 +36,9 @@ fn variable_declaration<'a>(i: &'a str, declarations: &Declarations) -> IResult<
|
||||
|i| variable_identifier(i, declarations),
|
||||
|s| crate::VariableDeclaration{name: s},
|
||||
)(i)
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
fn program_variable<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
map
|
||||
@ -331,8 +240,9 @@ fn comparison<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, c
|
||||
not_equal
|
||||
))(i)
|
||||
}
|
||||
*/
|
||||
|
||||
fn term_parenthesized<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
/*fn term_parenthesized<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Term>
|
||||
{
|
||||
delimited
|
||||
(
|
||||
@ -439,7 +349,9 @@ pub fn term<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, cra
|
||||
{
|
||||
term_precedence_3(i)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
fn formula_parenthesized<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str, crate::Formula>
|
||||
{
|
||||
delimited
|
||||
@ -625,7 +537,9 @@ pub fn formulas<'a>(i: &'a str, declarations: &Declarations) -> IResult<&'a str,
|
||||
{
|
||||
many0(formula)(i)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
@ -1078,3 +992,4 @@ mod tests
|
||||
))));
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
104
src/parse/names.rs
Normal file
104
src/parse/names.rs
Normal file
@ -0,0 +1,104 @@
|
||||
use nom::
|
||||
{
|
||||
IResult,
|
||||
bytes::complete::{take_while, take_while_m_n},
|
||||
combinator::recognize,
|
||||
sequence::pair,
|
||||
};
|
||||
|
||||
fn is_function_name_character_first(c: char) -> bool
|
||||
{
|
||||
c.is_alphabetic() && c.is_lowercase()
|
||||
}
|
||||
|
||||
fn is_function_name_character_body(c: char) -> bool
|
||||
{
|
||||
c.is_alphanumeric() || c == '_'
|
||||
}
|
||||
|
||||
fn is_variable_name_character_first(c: char) -> bool
|
||||
{
|
||||
c.is_alphabetic() && c.is_uppercase()
|
||||
}
|
||||
|
||||
fn is_variable_name_character_body(c: char) -> bool
|
||||
{
|
||||
c.is_alphanumeric() || c == '_'
|
||||
}
|
||||
|
||||
pub fn function_name(i: &str) -> IResult<&str, &str>
|
||||
{
|
||||
recognize
|
||||
(
|
||||
pair
|
||||
(
|
||||
take_while_m_n(1, 1, is_function_name_character_first),
|
||||
take_while(is_function_name_character_body),
|
||||
)
|
||||
)(i)
|
||||
}
|
||||
|
||||
pub fn predicate_name(i: &str) -> IResult<&str, &str>
|
||||
{
|
||||
function_name(i)
|
||||
}
|
||||
|
||||
pub fn variable_name(i: &str) -> IResult<&str, &str>
|
||||
{
|
||||
recognize
|
||||
(
|
||||
pair
|
||||
(
|
||||
take_while_m_n(1, 1, is_variable_name_character_first),
|
||||
take_while(is_variable_name_character_body),
|
||||
)
|
||||
)(i)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
use crate::parse::*;
|
||||
|
||||
#[test]
|
||||
fn parse_function_name()
|
||||
{
|
||||
assert_eq!(function_name("f rest"), Ok((" rest", "f")));
|
||||
assert_eq!(function_name("function_123 rest"), Ok((" rest", "function_123")));
|
||||
assert!(function_name("0 rest").is_err());
|
||||
assert!(function_name("123_asd rest").is_err());
|
||||
assert!(function_name("F rest").is_err());
|
||||
assert!(function_name("Function_123 rest").is_err());
|
||||
assert!(function_name("_ rest").is_err());
|
||||
assert!(function_name("_function_123 rest").is_err());
|
||||
assert!(function_name(" ").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_predicate_name()
|
||||
{
|
||||
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());
|
||||
assert!(predicate_name("P rest").is_err());
|
||||
assert!(predicate_name("Predicate_123 rest").is_err());
|
||||
assert!(predicate_name("_ rest").is_err());
|
||||
assert!(predicate_name("_predicate_123 rest").is_err());
|
||||
assert!(predicate_name(" ").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_variable_name()
|
||||
{
|
||||
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());
|
||||
assert!(variable_name("x Rest").is_err());
|
||||
assert!(variable_name("variable_123 Rest").is_err());
|
||||
assert!(variable_name("_ Rest").is_err());
|
||||
assert!(variable_name("_variable_123 Rest").is_err());
|
||||
assert!(variable_name(" ").is_err());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user