{-# LANGUAGE CPP #-}
module BishBosh.ContextualNotation.PGNDatabase(
PGNDatabase,
parseIO
) where
import qualified BishBosh.ContextualNotation.PGN as ContextualNotation.PGN
import qualified BishBosh.ContextualNotation.StandardAlgebraic as ContextualNotation.StandardAlgebraic
import qualified BishBosh.Types as T
import qualified Control.DeepSeq
import qualified System.FilePath
import qualified System.IO
#ifdef USE_POLYPARSE
import qualified BishBosh.Text.Poly as Text.Poly
#if USE_POLYPARSE == 1
import qualified Text.ParserCombinators.Poly.Lazy as Poly
#else /* Plain */
import qualified Text.ParserCombinators.Poly.Plain as Poly
#endif
#else /* Parsec */
import qualified Text.ParserCombinators.Parsec as Parsec
import Text.ParserCombinators.Parsec((<?>))
#endif
type PGNDatabase x y = [ContextualNotation.PGN.PGN x y]
parser :: (
Enum x,
Enum y,
Ord x,
Ord y,
Show x,
Show y
)
=> ContextualNotation.PGN.IsStrictlySequential
-> ContextualNotation.StandardAlgebraic.ValidateMoves
-> [ContextualNotation.PGN.Tag]
#ifdef USE_POLYPARSE
-> Text.Poly.TextParser (PGNDatabase x y)
{-# SPECIALISE parser :: ContextualNotation.PGN.IsStrictlySequential -> ContextualNotation.StandardAlgebraic.ValidateMoves -> [ContextualNotation.PGN.Tag] -> Text.Poly.TextParser (PGNDatabase T.X T.Y) #-}
parser isStrictlySequential validateMoves identificationTags = Poly.manyFinally' parser' $ Text.Poly.spaces >> Poly.eof
#else /* Parsec */
-> Parsec.Parser (PGNDatabase x y)
{-# SPECIALISE parser :: ContextualNotation.PGN.IsStrictlySequential -> ContextualNotation.StandardAlgebraic.ValidateMoves -> [ContextualNotation.PGN.Tag] -> Parsec.Parser (PGNDatabase T.X T.Y) #-}
parser isStrictlySequential validateMoves identificationTags = Parsec.manyTill parser' (Parsec.try $ Parsec.spaces >> Parsec.try Parsec.eof) <?> "PGN-database"
#endif
where
parser' = ContextualNotation.PGN.parser isStrictlySequential validateMoves identificationTags
parse :: (
Enum x,
Enum y,
Ord x,
Ord y,
Show x,
Show y
)
=> String
-> ContextualNotation.PGN.IsStrictlySequential
-> ContextualNotation.StandardAlgebraic.ValidateMoves
-> [ContextualNotation.PGN.Tag]
-> String
-> Either String (PGNDatabase x y)
{-# SPECIALISE parse :: String -> ContextualNotation.PGN.IsStrictlySequential -> ContextualNotation.StandardAlgebraic.ValidateMoves -> [ContextualNotation.PGN.Tag] -> String -> Either String (PGNDatabase T.X T.Y) #-}
#ifdef USE_POLYPARSE
#if USE_POLYPARSE == 1
parse _ isStrictlySequential validateMoves identificationTags = Right
#else /* Plain */
parse name isStrictlySequential validateMoves identificationTags = either (Left . showString "regarding " . shows name . showString ", ") Right
#endif
. fst . Poly.runParser parser'
#else /* Parsec */
parse name isStrictlySequential validateMoves identificationTags = either (Left . showString "failed to parse; " . show) Right . Parsec.parse parser' name
#endif
where
parser' = parser isStrictlySequential validateMoves identificationTags
parseIO :: (
Enum x,
Enum y,
Ord x,
Ord y,
Show x,
Show y
)
=> System.FilePath.FilePath
-> ContextualNotation.PGN.IsStrictlySequential
-> ContextualNotation.StandardAlgebraic.ValidateMoves
-> System.IO.TextEncoding
-> [ContextualNotation.PGN.Tag]
-> IO (Either String (PGNDatabase x y))
{-# SPECIALISE parseIO :: System.FilePath.FilePath -> ContextualNotation.PGN.IsStrictlySequential -> ContextualNotation.StandardAlgebraic.ValidateMoves -> System.IO.TextEncoding -> [ContextualNotation.PGN.Tag] -> IO (Either String (PGNDatabase T.X T.Y)) #-}
parseIO filePath isStrictlySequential validateMoves textEncoding identificationTags = System.IO.withFile filePath System.IO.ReadMode $ \fileHandle -> do
System.IO.hSetEncoding fileHandle textEncoding
contents <- System.IO.hGetContents fileHandle
Control.DeepSeq.deepseq contents . return $ parse filePath isStrictlySequential validateMoves identificationTags contents