Skip to content

Commit

Permalink
Add some cuts and other tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sthiele committed Dec 3, 2023
1 parent 4a703f3 commit 37f2104
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ In your code:
```rust
use flatzinc::*;

match flatzinc::model::<ContextError<&str>>(&buf) {
match flatzinc::model::<InputError<&str>>(&buf) {
Ok(result) => println!("{:#?}", result),
Err(e) => {
error!("Failed to parse flatzinc!\n{}", e)
Expand Down
15 changes: 8 additions & 7 deletions examples/fz-parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use anyhow::Result;
use clap::Parser as clapParser;
use clap::Parser;
use flatzinc::statements::parse_statement;
use log::error;
use std::path::PathBuf;
use stderrlog;
use winnow::{error::ContextError, Parser};
use winnow::error::InputError;

/// flatzinc parser
#[derive(clapParser, Debug)]
#[derive(Parser, Debug)]
#[clap(name = "fz-parser")]
struct Opt {
/// Input in flatzinc format
Expand All @@ -29,12 +30,12 @@ fn run() -> Result<()> {

let opt = Opt::parse();
let buf = std::fs::read_to_string(opt.file)?;
let mut parser = flatzinc::statement::<ContextError<&str>, &str>();
for line in buf.lines() {
match parser.parse(line) {
for mut line in buf.lines() {
match parse_statement(&mut line) {
Ok(result) => println!("{:#?}", result),
Err(e) => {
error!("Failed to parse flatzinc!\n{:?}", e)
let y: InputError<&str> = e.into_inner().unwrap();
error!("Failed to parse flatzinc!\n{:?}", y);
}
}
}
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)
}
11 changes: 9 additions & 2 deletions src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use winnow::{
ascii::{multispace0, multispace1},
combinator::alt,
combinator::opt,
combinator::{alt, eof, opt},
error::ParserError,
token::take_till0,
PResult, Parser,
Expand All @@ -11,6 +10,7 @@ use crate::statements::Stmt;

pub fn space_or_comment<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<Stmt, E> {
let s = space_or_comment0(input)?;
eof.parse_next(input)?;
Ok(Stmt::Comment(s.into()))
}
pub fn space_or_comment0<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<&'a str, E> {
Expand All @@ -36,3 +36,10 @@ fn test_comment() {
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<&str>>(&mut input);
assert!(res.is_err());
}
35 changes: 26 additions & 9 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::{eof, separated},
error::{FromExternalError, ParserError},
PResult, Parser,
};

#[derive(PartialEq, Clone, Debug)]
pub struct ConstraintItem {
Expand All @@ -26,7 +24,17 @@ where
+ FromExternalError<&'a str, std::num::ParseFloatError>,
{
space_or_comment0(input)?;
tag("constraint").parse_next(input)?;
"constraint".parse_next(input)?;
let item = constraint_tail(input).map_err(|e| e.cut())?;
Ok(item)
}
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 @@ -39,10 +47,11 @@ where
space_or_comment0(input)?;
';'.parse_next(input)?;
space_or_comment0(input)?;
eof.parse_next(input)?;
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);";
Expand Down Expand Up @@ -171,3 +180,11 @@ fn test_constraint_item_5() {
})
);
}
#[test]
fn test_constraint_item_6() {
use winnow::error::InputError;
let mut input = "constraintX int_lin_le_reif(X_INTRODUCED_22_,[X_INTRODUCED_7_,X_INTRODUCED_8_],,-2,X_INTRODUCED_58_):: defines_var(X_INTRODUCED_58_);";
let res = constraint_item::<InputError<&str>>(&mut input);
assert!(res.is_err());
assert_eq!("Err(Cut(InputError { input: \"X int_lin_le_reif(X_INTRODUCED_22_,[X_INTRODUCED_7_,X_INTRODUCED_8_],,-2,X_INTRODUCED_58_):: defines_var(X_INTRODUCED_58_);\", kind: Slice }))", format!("{:?}", res));
}
12 changes: 6 additions & 6 deletions src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use winnow::{
ascii::multispace1,
combinator::{alt, delimited, fold_repeat, opt, preceded, repeat, separated},
error::{FromExternalError, ParserError},
token::{tag, take_till1, take_while},
token::{take_till1, take_while},
PResult, Parser,
};

Expand All @@ -28,7 +28,7 @@ where
E: FromExternalError<&'a str, std::num::ParseIntError>
+ FromExternalError<&'a str, std::num::ParseFloatError>,
{
tag("::").parse_next(input)?;
"::".parse_next(input)?;
space_or_comment0(input)?;
annotation(input)
}
Expand Down Expand Up @@ -526,7 +526,7 @@ where
{
let lb = int_expr(input)?;
space_or_comment0(input)?;
tag("..").parse_next(input)?;
"..".parse_next(input)?;
space_or_comment0(input)?;
let ub = int_expr(input)?;
Ok(SetLiteralExpr::IntInRange(lb, ub))
Expand All @@ -538,7 +538,7 @@ where
{
let lb = float_expr(input)?;
space_or_comment0(input)?;
tag("..").parse_next(input)?;
"..".parse_next(input)?;
space_or_comment0(input)?;
let ub = float_expr(input)?;
Ok(SetLiteralExpr::BoundedFloat(lb, ub))
Expand Down Expand Up @@ -599,7 +599,7 @@ where
{
let lb = int_literal(input)?;
space_or_comment0(input)?;
tag("..").parse_next(input)?;
"..".parse_next(input)?;
space_or_comment0(input)?;
let ub = int_literal(input)?;
Ok(SetLiteral::IntRange(lb, ub))
Expand All @@ -611,7 +611,7 @@ where
{
let lb = float_literal(input)?;
space_or_comment0(input)?;
tag("..").parse_next(input)?;
"..".parse_next(input)?;
space_or_comment0(input)?;
let ub = float_literal(input)?;
Ok(SetLiteral::BoundedFloat(lb, ub))
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use predicates::{
};
pub use primitive_literals::IndexSet;
pub use solve_items::{Goal, OptimizationType, SolveItem};
pub use statements::{statement, Stmt};
pub use statements::{parse_statement, Stmt};
pub use variables::{declarations::VarDeclItem, types::BasicVarType};

pub mod basic_types;
Expand Down
7 changes: 5 additions & 2 deletions src/parameters/declarations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use winnow::{
combinator::eof,
error::{FromExternalError, ParserError},
PResult, Parser,
};
Expand Down Expand Up @@ -69,7 +70,7 @@ where
space_or_comment0(input)?;
'='.parse_next(input)?;
space_or_comment0(input)?;
match ptype {
let item = match ptype {
ParType::BasicParType(bpt) => match bpt {
BasicParType::BasicType(bt) => match bt {
BasicType::Bool => {
Expand Down Expand Up @@ -134,7 +135,9 @@ where
Ok(ParDeclItem::ArrayOfSet { ix, id, v })
}
},
}
};
eof.parse_next(input)?;
item
}
#[test]
fn test_par_decl_item_1() {
Expand Down
11 changes: 5 additions & 6 deletions src/parameters/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use winnow::{
combinator::alt,
error::{FromExternalError, ParserError},
token::tag,
PResult, Parser,
};

Expand Down Expand Up @@ -31,11 +30,11 @@ fn bpt_basic_type<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<B
// "set" "of" "int"
// Moved this be a basic-var-type basic-par-type
fn bpt_set_of_int<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<BasicParType, E> {
tag("set").parse_next(input)?;
"set".parse_next(input)?;
space_or_comment1(input)?;
tag("of").parse_next(input)?;
"of".parse_next(input)?;
space_or_comment1(input)?;
tag("int").parse_next(input)?;
"int".parse_next(input)?;
Ok(BasicParType::SetOfInt)
}

Expand Down Expand Up @@ -77,15 +76,15 @@ fn array_par_type<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<P
where
E: FromExternalError<&'a str, std::num::ParseIntError>,
{
tag("array").parse_next(input)?;
"array".parse_next(input)?;
space_or_comment1(input)?;
'['.parse_next(input)?;
space_or_comment0(input)?;
let ix = index_set(input)?;
space_or_comment0(input)?;
']'.parse_next(input)?;
space_or_comment1(input)?;
tag("of").parse_next(input)?;
"of".parse_next(input)?;
space_or_comment1(input)?;
let par_type = basic_par_type(input)?;
Ok(ParType::Array { ix, par_type })
Expand Down
17 changes: 13 additions & 4 deletions src/predicates/declarations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use winnow::{
combinator::separated,
combinator::{eof, separated},
error::{FromExternalError, ParserError},
token::tag,
PResult, Parser,
};

Expand All @@ -23,7 +22,17 @@ where
+ FromExternalError<&'a str, std::num::ParseFloatError>,
{
space_or_comment0(input)?;
tag("predicate").parse_next(input)?;
"predicate".parse_next(input)?;
let item = predicate_item_tail(input).map_err(|e| e.cut())?;
Ok(item)
}
pub fn predicate_item_tail<'a, E: ParserError<&'a str>>(
input: &mut &'a str,
) -> PResult<PredicateItem, 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 @@ -32,9 +41,9 @@ where
space_or_comment0(input)?;
';'.parse_next(input)?;
space_or_comment0(input)?;
eof.parse_next(input)?;
Ok(PredicateItem { id, parameters })
}

#[test]
fn test_predicate_item() {
use crate::predicates::types::BasicPredParType;
Expand Down
15 changes: 7 additions & 8 deletions src/predicates/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use winnow::{
combinator::alt,
error::{FromExternalError, ParserError},
token::tag,
PResult, Parser,
};

Expand Down Expand Up @@ -82,13 +81,13 @@ fn bppt_var_set_of_int<'a, E: ParserError<&'a str>>(
input: &mut &'a str,
) -> PResult<BasicPredParType, E> {
space_or_comment0(input)?;
tag("var").parse_next(input)?;
"var".parse_next(input)?;
space_or_comment1(input)?;
tag("set").parse_next(input)?;
"set".parse_next(input)?;
space_or_comment1(input)?;
tag("of").parse_next(input)?;
"of".parse_next(input)?;
space_or_comment1(input)?;
tag("int").parse_next(input)?;
"int".parse_next(input)?;
space_or_comment0(input)?;
Ok(BasicPredParType::VarSetOfInt)
}
Expand Down Expand Up @@ -223,15 +222,15 @@ where
+ FromExternalError<&'a str, std::num::ParseFloatError>,
{
space_or_comment0(input)?;
tag("array").parse_next(input)?;
"array".parse_next(input)?;
space_or_comment1(input)?;
'['.parse_next(input)?;
space_or_comment0(input)?;
let ix = pred_index_set(input)?;
space_or_comment0(input)?;
']'.parse_next(input)?;
space_or_comment1(input)?;
tag("of").parse_next(input)?;
"of".parse_next(input)?;
space_or_comment1(input)?;
let par_type = basic_pred_par_type(input)?;
Ok(PredParType::Array { ix, par_type })
Expand All @@ -251,7 +250,7 @@ where
}

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

Expand Down
Loading

0 comments on commit 37f2104

Please sign in to comment.