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

St/wip #35

Merged
merged 16 commits into from
Mar 25, 2024
5 changes: 3 additions & 2 deletions .github/workflows/minizinc-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
matrix:
os: [ubuntu-latest]
clingconversion: [5.2.1]
minizincversion: [2.8.0]
minizincversion: [2.8.3]
fzn2lpversion: [v0.1.4]

steps:
- uses: actions/checkout@v4
- uses: conda-incubator/setup-miniconda@v2
- uses: conda-incubator/setup-miniconda@v3
with:
auto-update-conda: true
python-version: ${{ matrix.python-version }}
Expand Down Expand Up @@ -97,6 +97,7 @@ jobs:
run: sed -i 's/"gecode", "cbc", "chuffed"/"flatzingo"/' libminizinc/tests/minizinc_testing/spec.py

- name: Run libminizinc tests
shell: bash -l {0}
run: |
cd libminizinc/tests
pytest -k "not test_output_checker and not test_var_set_element and not bug269 and not bug347 and not test-search1 and not test_vis_ann and not test_vis_custom" --driver=../../MiniZincIDE-${{ matrix.minizincversion }}-bundle-linux-x86_64/bin
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
exclude = ["/.github"]

[dependencies]
winnow = { version = "0.5.19", features = ["alloc"] }
winnow = "0.6"

[dev-dependencies]
clap = { version = "4.2", features = ["derive"] }
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ flatzinc = "0.3.20-dev"
In your code:

```rust
use flatzinc::*;
use flatzinc::Stmt;

match flatzinc::model::<ContextError<&str>>(&buf) {
match <Stmt as std::str::FromStr>::from_str(&mut line) {
Ok(result) => println!("{:#?}", result),
Err(e) => {
error!("Failed to parse flatzinc!\n{}", e)
error!("Failed to parse flatzinc statement:\n{}", e);
}
}
```
Expand Down
6 changes: 3 additions & 3 deletions examples/fz-parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use clap::Parser;
use flatzinc::Stmt;
use log::error;
use std::path::PathBuf;
use stderrlog;
use winnow::error::ContextError;

/// flatzinc parser
#[derive(Parser, Debug)]
Expand All @@ -30,10 +30,10 @@ fn run() -> Result<()> {
let opt = Opt::parse();
let buf = std::fs::read_to_string(opt.file)?;
for mut line in buf.lines() {
match flatzinc::statement::<ContextError<&str>>(&mut line) {
match <Stmt as std::str::FromStr>::from_str(&mut line) {
Ok(result) => println!("{:#?}", result),
Err(e) => {
error!("Failed to parse flatzinc!\n{}", e)
error!("Failed to parse flatzinc statement:\n{}", e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_target_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use winnow::error::ContextError;
fuzz_target!(|data: &[u8]| {
// fuzzed code goes here
if let Ok(mut utf8_str) = std::str::from_utf8(data) {
match flatzinc::statement::<ContextError<&str>>(&mut utf8_str) {
match flatzinc::statement::<ContextError>(&mut utf8_str) {
Ok(result) => println!("{:#?}", result),
Err(e) => print!("Failed to parse flatzinc: {:?}", e),
}
Expand Down
8 changes: 4 additions & 4 deletions src/basic_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use winnow::{combinator::alt, error::ParserError, token::tag, PResult, Parser};
use winnow::{combinator::alt, error::ParserError, PResult, Parser};

#[derive(PartialEq, Clone, Debug)]
pub enum BasicType {
Expand All @@ -13,16 +13,16 @@ pub fn basic_type<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<B
}

fn bool<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<BasicType, E> {
tag("bool").parse_next(input)?;
"bool".parse_next(input)?;
Ok(BasicType::Bool)
}

fn int<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<BasicType, E> {
tag("int").parse_next(input)?;
"int".parse_next(input)?;
Ok(BasicType::Int)
}

fn float<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<BasicType, E> {
tag("float").parse_next(input)?;
"float".parse_next(input)?;
Ok(BasicType::Float)
}
16 changes: 11 additions & 5 deletions src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use winnow::{
ascii::{multispace0, multispace1},
combinator::alt,
combinator::opt,
combinator::{alt, opt},
error::ParserError,
token::take_till0,
token::take_till,
PResult, Parser,
};

Expand All @@ -22,7 +21,7 @@ pub fn space_or_comment1<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PR
fn comment<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<&'a str, E> {
multispace0.parse_next(input)?;
'%'.parse_next(input)?;
let string = take_till0(|c| c == '\n').parse_next(input)?;
let string = take_till(0.., |c| c == '\n').parse_next(input)?;
multispace0.parse_next(input)?;
opt(comment).parse_next(input)?;
Ok(string)
Expand All @@ -32,7 +31,14 @@ fn test_comment() {
use winnow::error::ContextError;
let mut input = "% Comments can have anyth!ng in it really <3";
assert_eq!(
comment::<ContextError<&str>>(&mut input),
comment::<ContextError>(&mut input),
Ok(" Comments can have anyth!ng in it really <3".into())
);
}
#[test]
fn test_comment2() {
use winnow::error::ContextError;
let mut input = "5 % Comments can have anyth!ng in it really <3";
let res = comment::<ContextError>(&mut input);
assert!(res.is_err());
}
51 changes: 34 additions & 17 deletions src/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use winnow::{
combinator::separated,
error::{FromExternalError, ParserError},
token::tag,
PResult, Parser,
};

use crate::{
comments::{space_or_comment0, space_or_comment1},
expressions::{annotations, expr, Annotation, Expr},
primitive_literals::identifier,
};
use winnow::{
combinator::{cut_err, separated},
error::{AddContext, FromExternalError, ParserError, StrContext},
PResult, Parser,
};

#[derive(PartialEq, Clone, Debug)]
pub struct ConstraintItem {
Expand All @@ -23,10 +21,21 @@ pub fn constraint_item<'a, E: ParserError<&'a str>>(
) -> PResult<ConstraintItem, E>
where
E: FromExternalError<&'a str, std::num::ParseIntError>
+ FromExternalError<&'a str, std::num::ParseFloatError>,
+ FromExternalError<&'a str, std::num::ParseFloatError>
+ AddContext<&'a str, StrContext>,
{
space_or_comment0(input)?;
tag("constraint").parse_next(input)?;
"constraint".parse_next(input)?;
cut_err(constraint_tail.context(StrContext::Label("Error while parsing constraint")))
.parse_next(input)
}
pub fn constraint_tail<'a, E: ParserError<&'a str>>(
input: &mut &'a str,
) -> PResult<ConstraintItem, E>
where
E: FromExternalError<&'a str, std::num::ParseIntError>
+ FromExternalError<&'a str, std::num::ParseFloatError>,
{
space_or_comment1(input)?;
let id = identifier(input)?;
'('.parse_next(input)?;
Expand All @@ -42,12 +51,12 @@ where
Ok(ConstraintItem { id, exprs, annos })
}
#[test]
fn test_constraint_item() {
fn test_constraint_item_1() {
use crate::{AnnExpr, Annotation, Expr, IntExpr, SetLiteralExpr};
use winnow::error::ContextError;
let mut input = "constraint set_in_reif(X_26,1..2,X_52):: defines_var(X_52);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "set_in_reif".to_string(),
exprs: vec![
Expand All @@ -63,7 +72,7 @@ fn test_constraint_item() {
);
let mut input = "constraint array_var_int_element(INT01, w, 2);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "array_var_int_element".to_string(),
exprs: vec![
Expand All @@ -76,7 +85,7 @@ fn test_constraint_item() {
);
let mut input = "constraint array_var_int_element(INT01, w, 2.0);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "array_var_int_element".to_string(),
exprs: vec![
Expand All @@ -94,7 +103,7 @@ fn test_constraint_item_2() {
use winnow::error::ContextError;
let mut input = "constraint int_lin_eq([-1, 1], [INT01, p], -3);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "int_lin_eq".to_string(),
exprs: vec![
Expand All @@ -115,7 +124,7 @@ fn test_constraint_item_3() {
use winnow::error::ContextError;
let mut input = "constraint float_lin_eq(X_139,[X_27,X_28,X_29],1.0);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "float_lin_eq".to_string(),
exprs: vec![
Expand All @@ -137,7 +146,7 @@ fn test_constraint_item_4() {
use winnow::error::ContextError;
let mut input = "constraint array_bool_or([X_43,X_44],true);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "array_bool_or".to_string(),
exprs: vec![
Expand All @@ -157,7 +166,7 @@ fn test_constraint_item_5() {
use winnow::error::ContextError;
let mut input = "constraint bool_clause([],[X_81,X_77]);";
assert_eq!(
constraint_item::<ContextError<&str>>(&mut input),
constraint_item::<ContextError>(&mut input),
Ok(ConstraintItem {
id: "bool_clause".to_string(),
exprs: vec![
Expand All @@ -171,3 +180,11 @@ fn test_constraint_item_5() {
})
);
}
#[test]
fn test_constraint_item_6() {
use winnow::error::ContextError;
let mut input = "constraintX int_lin_le_reif(X_INTRODUCED_22_,,-2,X_INTRODUCED_58_):: defines_var(X_INTRODUCED_58_);";
let res = constraint_item::<ContextError>(&mut input);
assert!(res.is_err());
assert_eq!("Parsing Failure: ContextError { context: [Label(\"Error while parsing constraint\")], cause: None }", res.unwrap_err().to_string());
}
Loading