Skip to content

Commit

Permalink
parse* functions fails if the parser does not consume all of the inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Jun 20, 2015
1 parent 44fe69c commit 1a7558b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.1.1.0
-------
* parse* functions fails if the parser does not consume all of the inputs
1 change: 1 addition & 0 deletions pseudo-boolean.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ category: Data, Optimisation, Optimization, Constraints, Logic
build-type: Simple
extra-source-files:
README.md
CHANGELOG.markdown
test/samples/*.opb
test/samples/normalized-1096.cudf.paranoid.opb
test/samples/*.wbo
Expand Down
6 changes: 3 additions & 3 deletions src/Data/PseudoBoolean/Attoparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Data.PseudoBoolean.Attoparsec
) where

import Prelude hiding (sum)
import Control.Applicative ((<|>))
import Control.Applicative ((<|>), (<*))
import Control.Monad
import Data.Attoparsec.ByteString.Char8 hiding (isDigit)
import qualified Data.Attoparsec.ByteString.Lazy as L
Expand Down Expand Up @@ -201,7 +201,7 @@ literal = variablename <|> (char '~' >> liftM negate variablename)

-- | Parse a OPB format string containing pseudo boolean problem.
parseOPBByteString :: BSLazy.ByteString -> Either String Formula
parseOPBByteString s = L.eitherResult $ L.parse formula s
parseOPBByteString s = L.eitherResult $ L.parse (formula <* endOfInput) s

-- | Parse a OPB file containing pseudo boolean problem.
parseOPBFile :: FilePath -> IO (Either String Formula)
Expand Down Expand Up @@ -260,7 +260,7 @@ softconstraint = do

-- | Parse a WBO format string containing weighted boolean optimization problem.
parseWBOByteString :: BSLazy.ByteString -> Either String SoftFormula
parseWBOByteString s = L.eitherResult $ L.parse softformula s
parseWBOByteString s = L.eitherResult $ L.parse (softformula <* endOfInput) s

-- | Parse a WBO file containing weighted boolean optimization problem.
parseWBOFile :: FilePath -> IO (Either String SoftFormula)
Expand Down
13 changes: 7 additions & 6 deletions src/Data/PseudoBoolean/Parsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Data.PseudoBoolean.Parsec
) where

import Prelude hiding (sum)
import Control.Applicative ((<*))
import Control.Monad
import Data.ByteString.Lazy (ByteString)
import Data.Maybe
Expand Down Expand Up @@ -200,15 +201,15 @@ literal = variablename <|> (char '~' >> liftM negate variablename)

-- | Parse a OPB format string containing pseudo boolean problem.
parseOPBString :: SourceName -> String -> Either ParseError Formula
parseOPBString = parse formula
parseOPBString = parse (formula <* eof)

-- | Parse a OPB format lazy bytestring containing pseudo boolean problem.
parseOPBByteString :: SourceName -> ByteString -> Either ParseError Formula
parseOPBByteString = parse formula
parseOPBByteString = parse (formula <* eof)

-- | Parse a OPB file containing pseudo boolean problem.
parseOPBFile :: FilePath -> IO (Either ParseError Formula)
parseOPBFile = ParsecBS.parseFromFile formula
parseOPBFile = ParsecBS.parseFromFile (formula <* eof)


-- <softformula>::= <sequence_of_comments> <softheader> <sequence_of_comments_or_constraints>
Expand Down Expand Up @@ -262,12 +263,12 @@ softconstraint = do

-- | Parse a WBO format string containing weighted boolean optimization problem.
parseWBOString :: SourceName -> String -> Either ParseError SoftFormula
parseWBOString = parse softformula
parseWBOString = parse (softformula <* eof)

-- | Parse a WBO format lazy bytestring containing pseudo boolean problem.
parseWBOByteString :: SourceName -> ByteString -> Either ParseError SoftFormula
parseWBOByteString = parse softformula
parseWBOByteString = parse (softformula <* eof)

-- | Parse a WBO file containing weighted boolean optimization problem.
parseWBOFile :: FilePath -> IO (Either ParseError SoftFormula)
parseWBOFile = ParsecBS.parseFromFile softformula
parseWBOFile = ParsecBS.parseFromFile (softformula <* eof)
20 changes: 20 additions & 0 deletions test/TestPBFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# OPTIONS_GHC -Wall #-}
module Main (main) where

import Data.Either
import qualified Data.ByteString.Lazy.Char8 as BSChar8
import System.IO
import System.IO.Temp
Expand Down Expand Up @@ -37,6 +38,25 @@ case_normalized_mds_50_10_4 = checkOPBFile "test/samples/normalized-mds_50_10_4.
case_normalized_opt_market_split_4_30_2 = checkOPBFile "test/samples/normalized-opt-market-split_4_30_2.opb"
case_pigeonhole_5_4 = checkOPBFile "test/samples/pigeonhole_5_4.opb"

case_trailing_junk = do
isLeft (parseOPBString "" trailingJunk) @?= True
isLeft (parseOPBByteString "" (BSChar8.pack trailingJunk)) @?= True
isLeft (A.parseOPBByteString (BSChar8.pack trailingJunk)) @?= True
where
trailingJunk = 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;"
, "foo"
]

case_readUnsignedInteger_maxBound_bug :: IO ()
case_readUnsignedInteger_maxBound_bug =
readUnsignedInteger "006666666666666667" @?= 6666666666666667
Expand Down

0 comments on commit 1a7558b

Please sign in to comment.