-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding Binary, Generals and Integer parsing fn
refactor: Updating the infinity implementation refactor: Adding Nom helper functions
- Loading branch information
Showing
5 changed files
with
266 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,145 @@ | ||
use nom::{bytes::complete::take_while1, IResult}; | ||
use nom::{ | ||
branch::alt, | ||
bytes::complete::{tag, tag_no_case, take_while1}, | ||
character::complete::{char, multispace0, space0}, | ||
combinator::{map, opt}, | ||
error::ErrorKind, | ||
multi::many0, | ||
sequence::{preceded, tuple}, | ||
IResult, | ||
}; | ||
|
||
use crate::nom::decoder::is_valid_lp_char; | ||
use crate::nom::{ | ||
decoder::{is_valid_lp_char, number::parse_num_value}, | ||
model::VariableType, | ||
}; | ||
|
||
const SECTION_HEADERS: [&str; 12] = | ||
["integers", "integer", "general", "generals", "gen", "binaries", "binary", "bin", "bounds", "bound", "sos", "end"]; | ||
|
||
#[inline] | ||
pub fn parse_variable(input: &str) -> IResult<&str, &str> { | ||
take_while1(is_valid_lp_char)(input) | ||
} | ||
|
||
#[inline] | ||
pub fn single_bound(input: &str) -> IResult<&str, (&str, VariableType)> { | ||
preceded( | ||
multispace0, | ||
alt(( | ||
// Free variable: `x1 free` | ||
map(tuple((parse_variable, preceded(space0, tag_no_case("free")))), |(var_name, _)| (var_name, VariableType::Free)), | ||
// Double bound: `0 <= x1 <= 5` | ||
map( | ||
tuple(( | ||
parse_num_value, | ||
preceded(space0, alt((tag("<="), tag("<")))), | ||
preceded(space0, parse_variable), | ||
preceded(space0, alt((tag("<="), tag("<")))), | ||
preceded(space0, parse_num_value), | ||
)), | ||
|(lower, _, var_name, _, upper)| (var_name, VariableType::DoubleBound(lower, upper)), | ||
), | ||
// Lower bound: `x1 >= 5` or `5 <= x1` | ||
alt(( | ||
map(tuple((parse_variable, preceded(space0, tag(">=")), preceded(space0, parse_num_value))), |(var_name, _, bound)| { | ||
(var_name, VariableType::LowerBound(bound)) | ||
}), | ||
map(tuple((parse_num_value, preceded(space0, tag("<=")), preceded(space0, parse_variable))), |(bound, _, var_name)| { | ||
(var_name, VariableType::LowerBound(bound)) | ||
}), | ||
)), | ||
// Upper bound: `x1 <= 5` or `5 >= x1` | ||
alt(( | ||
map(tuple((parse_variable, preceded(space0, tag("<=")), preceded(space0, parse_num_value))), |(var_name, _, bound)| { | ||
(var_name, VariableType::UpperBound(bound)) | ||
}), | ||
map(tuple((parse_num_value, preceded(space0, tag(">=")), preceded(space0, parse_variable))), |(bound, _, var_name)| { | ||
(var_name, VariableType::UpperBound(bound)) | ||
}), | ||
)), | ||
)), | ||
)(input) | ||
} | ||
|
||
#[inline] | ||
pub fn parse_bounds_section(input: &str) -> IResult<&str, Vec<(&str, VariableType)>> { | ||
preceded(tuple((multispace0, tag_no_case("bounds"), opt(preceded(space0, char(':'))), multispace0)), many0(single_bound))(input) | ||
} | ||
|
||
#[inline] | ||
fn is_section_header(input: &str) -> bool { | ||
let lower_input = input.trim().to_lowercase(); | ||
SECTION_HEADERS.iter().any(|&header| lower_input.starts_with(header)) | ||
} | ||
|
||
#[inline] | ||
fn variable_not_header(input: &str) -> IResult<&str, &str> { | ||
let (input, _) = multispace0(input)?; | ||
if is_section_header(input) { | ||
return Err(nom::Err::Error(nom::error::Error::new(input, ErrorKind::Not))); | ||
} | ||
parse_variable(input) | ||
} | ||
|
||
#[inline] | ||
pub fn parse_variable_list(input: &str) -> IResult<&str, Vec<&str>> { | ||
many0(variable_not_header)(input) | ||
} | ||
|
||
#[inline] | ||
pub fn parse_generals_section(input: &str) -> IResult<&str, Vec<&str>> { | ||
if input.is_empty() || input == "\n" { | ||
return Ok(("", vec![])); | ||
} | ||
preceded( | ||
tuple((multispace0, alt((tag_no_case("generals"), tag_no_case("general"))), opt(preceded(space0, char(':'))), multispace0)), | ||
parse_variable_list, | ||
)(input) | ||
} | ||
|
||
#[inline] | ||
pub fn parse_integer_section(input: &str) -> IResult<&str, Vec<&str>> { | ||
if input.is_empty() || input == "\n" { | ||
return Ok(("", vec![])); | ||
} | ||
preceded( | ||
tuple((multispace0, alt((tag_no_case("integers"), tag_no_case("integer"))), opt(preceded(space0, char(':'))), multispace0)), | ||
parse_variable_list, | ||
)(input) | ||
} | ||
|
||
#[inline] | ||
pub fn parse_binary_section(input: &str) -> IResult<&str, Vec<&str>> { | ||
if input.is_empty() || input == "\n" { | ||
return Ok(("", vec![])); | ||
} | ||
preceded( | ||
tuple(( | ||
multispace0, | ||
alt((tag_no_case("binaries"), tag_no_case("binary"), tag_no_case("bin"))), | ||
opt(preceded(space0, char(':'))), | ||
multispace0, | ||
)), | ||
parse_variable_list, | ||
)(input) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::nom::decoder::variable::parse_bounds_section; | ||
|
||
#[test] | ||
fn test_bounds() { | ||
let input = " | ||
bounds | ||
x1 free | ||
x2 >= 1 | ||
x2 >= inf | ||
100 <= x2dfsdf <= -1 | ||
-infinity <= qwer < +inf"; | ||
let (remaining, bounds) = parse_bounds_section(input).unwrap(); | ||
assert_eq!(remaining, ""); | ||
assert_eq!(bounds.len(), 5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters