Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nom to 8.0.0 #256

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ flate2 = { version = "1.0.35", features = [
git2 = "0.20.0"
glob = "0.3.2"
itertools = "0.14.0"
nom = "7.1.3"
nom = "8.0.0"
termcolor = "1.4.1"
thiserror = "2.0.11"
rand = "0.9.0"
Expand Down
76 changes: 42 additions & 34 deletions src/instances/fio/dimacs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use nom::{
combinator::{all_consuming, map, map_res, recognize, success},
error::{context, Error as NomError},
multi::separated_list0,
sequence::{pair, terminated, tuple},
IResult,
sequence::terminated,
IResult, Parser,
};
use std::{
convert::TryFrom,
Expand Down Expand Up @@ -294,22 +294,24 @@ where
fn parse_p_line(input: &str) -> IResult<&str, Preamble> {
let (input, _) = context(
"p line does not start with 'p'",
terminated::<_, _, _, NomError<_>, _, _>(tag("p"), multispace1),
)(input)?;
terminated::<_, _, NomError<_>, _, _>(tag("p"), multispace1),
)
.parse(input)?;
let (input, id_token) = context(
"invalid id token in p line",
alt((
terminated::<_, _, _, NomError<_>, _, _>(tag("cnf"), multispace1),
terminated::<_, _, NomError<_>, _, _>(tag("cnf"), multispace1),
#[cfg(feature = "optimization")]
terminated(tag("wcnf"), multispace1),
)),
)(input)?;
)
.parse(input)?;
//.with_context(|| format!("invalid id token in '{}'", input))?;
if id_token == "cnf" {
// Is CNF file
let (input, (n_vars, _, n_clauses)) = context(
"failed to parse number of variables and clauses",
tuple::<_, _, NomError<_>, _>((
(
context(
"number of vars does not fit usize",
map_res(u64, usize::try_from),
Expand All @@ -319,16 +321,17 @@ fn parse_p_line(input: &str) -> IResult<&str, Preamble> {
"number of clauses does not fit usize",
map_res(u64, usize::try_from),
),
)),
)(input)?;
),
)
.parse(input)?;
return Ok((input, Preamble::Cnf { n_vars, n_clauses }));
}
#[cfg(feature = "optimization")]
if id_token == "wcnf" {
// Is WCNF file
let (input, (n_vars, _, n_clauses, _, top)) = context(
"failed to parse number of variables, clauses, and top",
tuple::<_, _, NomError<_>, _>((
(
context(
"number of vars does not fit usize",
map_res(u64, usize::try_from),
Expand All @@ -340,8 +343,9 @@ fn parse_p_line(input: &str) -> IResult<&str, Preamble> {
),
multispace1,
context("top does not fit usize", map_res(u64, usize::try_from)),
)),
)(input)?;
),
)
.parse(input)?;
return Ok((
input,
Preamble::WcnfPre22 {
Expand Down Expand Up @@ -383,7 +387,7 @@ fn parse_wcnf_pre22_line(input: &str) -> IResult<&str, Option<(usize, Clause)>>
} else {
// Line is not a comment
let (input, (weight, clause)) =
separated_pair(parse_weight, multispace1, parse_clause)(input)?;
separated_pair(parse_weight, multispace1, parse_clause).parse(input)?;
Ok((input, Some((weight, clause))))
}
}
Expand All @@ -406,7 +410,7 @@ fn parse_mcnf_line(input: &str) -> IResult<&str, McnfDataLine> {
Err(_) =>
// Line is not a comment
{
match terminated(tag::<_, _, NomError<_>>("h"), multispace1)(input) {
match terminated(tag::<_, _, NomError<_>>("h"), multispace1).parse(input) {
Ok((input, _)) => {
// Hard clause
let (input, clause) = parse_clause(input)?;
Expand All @@ -416,19 +420,19 @@ fn parse_mcnf_line(input: &str) -> IResult<&str, McnfDataLine> {
// Soft clause
if let Ok((input, _)) = tag::<_, _, NomError<_>>("o")(input) {
// MCNF soft (explicit obj index)
let (input, (idx, _, weight, _, clause)) =
tuple((
parse_idx,
multispace1,
parse_weight,
multispace1,
parse_clause,
))(input)?;
let (input, (idx, _, weight, _, clause)) = (
parse_idx,
multispace1,
parse_weight,
multispace1,
parse_clause,
)
.parse(input)?;
Ok((input, Some((Some((idx, weight)), clause))))
} else {
// WCNF soft (implicit obj index of 1)
let (input, (weight, clause)) =
separated_pair(parse_weight, multispace1, parse_clause)(input)?;
separated_pair(parse_weight, multispace1, parse_clause).parse(input)?;
Ok((input, Some((Some((1, weight)), clause))))
}
}
Expand All @@ -445,7 +449,8 @@ fn parse_clause(input: &str) -> IResult<&str, Clause> {
terminated(separated_list0(multispace1, parse_lit), parse_clause_ending),
Clause::from_iter,
),
)(input)
)
.parse(input)
}

#[cfg(feature = "optimization")]
Expand All @@ -457,7 +462,8 @@ fn parse_weight(input: &str) -> IResult<&str, usize> {
context("expected number for weight", u64),
TryInto::try_into,
),
)(input)
)
.parse(input)
}

#[cfg(feature = "optimization")]
Expand All @@ -471,29 +477,31 @@ fn parse_idx(input: &str) -> IResult<&str, usize> {
}
w.try_into().map_err(|_| ())
}),
)(input)
)
.parse(input)
}

/// Nom-like parser for literal
fn parse_lit(input: &str) -> IResult<&str, Lit> {
context("invalid ipasir literal", map_res(i32, Lit::from_ipasir))(input)
context("invalid ipasir literal", map_res(i32, Lit::from_ipasir)).parse(input)
}

/// Parses the end of a clause
/// A '0' followed by a line-break, as well as a '0' followed by
/// white-space or only a line-break are treated as valid clause endings.
/// This is more lean than the file format spec.
fn parse_clause_ending(input: &str) -> IResult<&str, &str> {
recognize(pair(
recognize((
multispace0,
alt((
recognize(all_consuming(success(""))),
recognize(all_consuming(tag("0"))),
recognize(terminated(tag("0"), line_ending)),
recognize(terminated(tag("0"), multispace1)),
recognize(line_ending),
all_consuming(success("")),
all_consuming(tag("0")),
terminated(tag("0"), line_ending),
terminated(tag("0"), multispace1),
line_ending,
)),
))(input)
))
.parse(input)
}

/// Writes a CNF to a DIMACS CNF file
Expand Down
Loading
Loading