Recognize variables better

This commit is contained in:
Patrick Lühne 2019-11-05 12:54:15 -06:00
parent 8a7bd651b2
commit 683236f4a8
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 30 additions and 71 deletions

View File

@ -58,12 +58,6 @@ impl std::fmt::Debug for crate::VariableDeclaration
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
match &self.domain
{
crate::Domain::Program => write!(format, "X")?,
crate::Domain::Integer => write!(format, "N")?,
};
write!(format, "{}", &self.name)
}
}

View File

@ -4,7 +4,7 @@ use nom::
bytes::complete::{take_while, take_while_m_n, is_not},
character::complete::{digit1, multispace0, space1, not_line_ending, line_ending},
sequence::{preceded, delimited, pair, terminated},
combinator::{map, map_res},
combinator::{map, map_res, recognize},
multi::{many0, many0_count, separated_list},
branch::alt,
bytes::complete::tag,
@ -95,6 +95,11 @@ fn is_lowercase_alphanumeric(c: char) -> bool
c.is_alphanumeric() && c.is_lowercase()
}
fn is_uppercase_alphanumeric(c: char) -> bool
{
c.is_alphanumeric() && c.is_uppercase()
}
fn symbolic_identifier(i: &str) -> IResult<&str, String>
{
let (i, symbolic_identifier) = map
@ -138,17 +143,20 @@ fn string(i: &str) -> IResult<&str, crate::Term>
)(i)
}
fn program_variable_identifier(i: &str) -> IResult<&str, String>
fn variable_identifier(i: &str) -> IResult<&str, String>
{
map
(
delimited
(
whitespace0,
preceded
recognize
(
tag("X"),
take_while(char::is_alphanumeric)
pair
(
take_while_m_n(1, 1, is_uppercase_alphanumeric),
take_while(char::is_alphanumeric)
)
),
whitespace0
),
@ -156,76 +164,33 @@ fn program_variable_identifier(i: &str) -> IResult<&str, String>
)(i)
}
fn integer_variable_identifier(i: &str) -> IResult<&str, String>
{
map
(
delimited
(
whitespace0,
preceded
(
tag("N"),
take_while(char::is_alphanumeric)
),
whitespace0
),
|s: &str| s.to_string()
)(i)
}
fn program_variable_declaration(i: &str) -> IResult<&str, crate::VariableDeclaration>
{
map
(
program_variable_identifier,
|s| crate::VariableDeclaration{name: s, domain: crate::Domain::Program}
)(i)
}
fn integer_variable_declaration(i: &str) -> IResult<&str, crate::VariableDeclaration>
{
map
(
integer_variable_identifier,
|s| crate::VariableDeclaration{name: s, domain: crate::Domain::Integer}
)(i)
}
fn variable_declaration(i: &str) -> IResult<&str, crate::VariableDeclaration>
{
alt
((
program_variable_declaration,
integer_variable_declaration
))(i)
}
fn program_variable(i: &str) -> IResult<&str, crate::Term>
{
map
(
program_variable_identifier,
|s| crate::Term::Variable(crate::VariableDeclaration{name: s, domain: crate::Domain::Program})
)(i)
}
variable_identifier,
|name|
{
let domain = match name.chars().next()
{
Some('X') | Some('Y') | Some('Z') => crate::Domain::Program,
Some('I') | Some('N') | Some('M') => crate::Domain::Integer,
Some(other) => panic!("variable “{}” starts with character “{}”, which is not allowed", name, other),
None => panic!("unexpected variable name, please report to bug tracker"),
};
fn integer_variable(i: &str) -> IResult<&str, crate::Term>
{
map
(
integer_variable_identifier,
|s| crate::Term::Variable(crate::VariableDeclaration{name: s, domain: crate::Domain::Integer})
crate::VariableDeclaration{name, domain}
}
)(i)
}
fn variable(i: &str) -> IResult<&str, crate::Term>
{
alt
((
program_variable,
integer_variable
))(i)
map
(
variable_declaration,
|variable_declaration| crate::Term::Variable(variable_declaration)
)(i)
}
fn predicate_0_ary(i: &str) -> IResult<&str, crate::Formula>