Skip to content

Commit

Permalink
relax the grammer to allow more type of spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Jul 13, 2015
1 parent 6d77605 commit ecd8575
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Data/PseudoBoolean/Attoparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ oneOrMoreSpace = skipMany1 (char ' ')

-- <zeroOrMoreSpace>::= [" " <zeroOrMoreSpace>]
zeroOrMoreSpace :: Parser ()
zeroOrMoreSpace = skipMany (char ' ')
-- zeroOrMoreSpace = skipMany (char ' ')
zeroOrMoreSpace = skipSpace
-- We relax the grammer and allow more type of spacing

eol :: Parser ()
eol = char '\n' >> return ()

semi :: Parser ()
semi = char ';' >> skipMany space
semi = char ';' >> skipSpace
-- We relax the grammer and allow spaces in the beginning of next component.

{-
Expand Down
4 changes: 3 additions & 1 deletion src/Data/PseudoBoolean/Parsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ oneOrMoreSpace = skipMany1 (char ' ')

-- <zeroOrMoreSpace>::= [" " <zeroOrMoreSpace>]
zeroOrMoreSpace :: Stream s m Char => ParsecT s u m ()
zeroOrMoreSpace = skipMany (char ' ')
-- zeroOrMoreSpace = skipMany (char ' ')
zeroOrMoreSpace = spaces
-- We relax the grammer and allow more type of spacing

eol :: Stream s m Char => ParsecT s u m ()
eol = char '\n' >> return ()
Expand Down
49 changes: 48 additions & 1 deletion test/TestPBFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ case_trailing_junk = do
, "foo"
]

case_min_eol = do
isOk (parseOPBString "" opb) @?= True
isOk (parseOPBByteString "" (BSChar8.pack opb)) @?= True
isOk (A.parseOPBByteString (BSChar8.pack opb)) @?= True
where
-- isLeft is available only on base >=4.7.0.0.
isOk :: Either a b -> Bool
isOk (Left _) = False
isOk (Right _) = True

opb = unlines
[ "* #variable= 5 #constraint= 4"
, "*"
, "* this is a dummy instance"
, "*"
, "min: "
, "1 x2 -1 x3 ;"
, "1 x1 +4 x2 -2 x5 >= 2;"
, "-1 x1 +4 x2 -2 x5 >= +3;"
, "12345678901234567890 x4 +4 x3 >= 10;"
, "* an equality constraint"
, "2 x2 +3 x4 +2 x1 +3 x5 = 5;"
]

case_empty_constraints = do
isOk (parseOPBString "" opb) @?= True
isOk (parseOPBByteString "" (BSChar8.pack opb)) @?= True
Expand Down Expand Up @@ -92,7 +116,30 @@ case_wbo_empty_constraints = do
, "soft: 1 ;"
]

case_trailing_spaces = do
case_trailing_spaces_before_eol = do
isOk (parseOPBString "" opb) @?= True
isOk (parseOPBByteString "" (BSChar8.pack opb)) @?= True
isOk (A.parseOPBByteString (BSChar8.pack opb)) @?= True
where
-- isRight is available only on base >=4.7.0.0.
isOk :: Either a b -> Bool
isOk (Left _) = False
isOk (Right _) = True

opb = unlines
[ "* #variable= 5 #constraint= 4"
, "*"
, "* this is a dummy instance"
, "*"
, "min: 1 x2 -1 x3 ;"
, "1 x1 +4 x2 -2 x5 >= 2; "
, "-1 x1 +4 x2 -2 x5 >= +3; "
, "12345678901234567890 x4 +4 x3 >= 10; "
, "* an equality constraint "
, "2 x2 +3 x4 +2 x1 +3 x5 = 5; "
]

case_trailing_spaces_before_eof = do
isOk (parseOPBString "" opb) @?= True
isOk (parseOPBByteString "" (BSChar8.pack opb)) @?= True
isOk (A.parseOPBByteString (BSChar8.pack opb)) @?= True
Expand Down

0 comments on commit ecd8575

Please sign in to comment.