{-# 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
-> IsStrictlySequential -> [Tag] -> TextParser (PGNDatabase x y)
parser IsStrictlySequential
isStrictlySequential IsStrictlySequential
validateMoves [Tag]
identificationTags = Parser Char (PGN x y)
-> Parser Char () -> TextParser (PGNDatabase x y)
forall (p :: * -> *) a z.
(PolyParse p, Show a) =>
p a -> p z -> p [a]
Poly.manyFinally' Parser Char (PGN x y)
parser' (Parser Char () -> TextParser (PGNDatabase x y))
-> Parser Char () -> TextParser (PGNDatabase x y)
forall a b. (a -> b) -> a -> b
$ Parser Char ()
Text.Poly.spaces Parser Char () -> Parser Char () -> Parser Char ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser Char ()
forall t. Parser t ()
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' :: Parser Char (PGN x y)
parser' = IsStrictlySequential
-> IsStrictlySequential -> [Tag] -> Parser Char (PGN x y)
forall x y.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y) =>
IsStrictlySequential
-> IsStrictlySequential -> [Tag] -> TextParser (PGN x y)
ContextualNotation.PGN.parser IsStrictlySequential
isStrictlySequential IsStrictlySequential
validateMoves [Tag]
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 :: Tag
-> IsStrictlySequential
-> IsStrictlySequential
-> [Tag]
-> Tag
-> Either Tag (PGNDatabase x y)
parse Tag
_ IsStrictlySequential
isStrictlySequential IsStrictlySequential
validateMoves [Tag]
identificationTags = PGNDatabase x y -> Either Tag (PGNDatabase x y)
forall a b. b -> Either a b
Right
#else /* Plain */
parse name isStrictlySequential validateMoves identificationTags = either (Left . showString "regarding " . shows name . showString ", ") Right
#endif
(PGNDatabase x y -> Either Tag (PGNDatabase x y))
-> (Tag -> PGNDatabase x y) -> Tag -> Either Tag (PGNDatabase x y)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PGNDatabase x y, Tag) -> PGNDatabase x y
forall a b. (a, b) -> a
fst ((PGNDatabase x y, Tag) -> PGNDatabase x y)
-> (Tag -> (PGNDatabase x y, Tag)) -> Tag -> PGNDatabase x y
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Char (PGNDatabase x y) -> Tag -> (PGNDatabase x y, Tag)
forall t a. Parser t a -> [t] -> (a, [t])
Poly.runParser Parser Char (PGNDatabase x y)
parser'
#else /* Parsec */
parse name isStrictlySequential validateMoves identificationTags = either (Left . showString "failed to parse; " . show) Right . Parsec.parse parser' name
#endif
where
parser' :: Parser Char (PGNDatabase x y)
parser' = IsStrictlySequential
-> IsStrictlySequential -> [Tag] -> Parser Char (PGNDatabase x y)
forall x y.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y) =>
IsStrictlySequential
-> IsStrictlySequential -> [Tag] -> TextParser (PGNDatabase x y)
parser IsStrictlySequential
isStrictlySequential IsStrictlySequential
validateMoves [Tag]
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 :: Tag
-> IsStrictlySequential
-> IsStrictlySequential
-> TextEncoding
-> [Tag]
-> IO (Either Tag (PGNDatabase x y))
parseIO Tag
filePath IsStrictlySequential
isStrictlySequential IsStrictlySequential
validateMoves TextEncoding
textEncoding [Tag]
identificationTags = Tag
-> IOMode
-> (Handle -> IO (Either Tag (PGNDatabase x y)))
-> IO (Either Tag (PGNDatabase x y))
forall r. Tag -> IOMode -> (Handle -> IO r) -> IO r
System.IO.withFile Tag
filePath IOMode
System.IO.ReadMode ((Handle -> IO (Either Tag (PGNDatabase x y)))
-> IO (Either Tag (PGNDatabase x y)))
-> (Handle -> IO (Either Tag (PGNDatabase x y)))
-> IO (Either Tag (PGNDatabase x y))
forall a b. (a -> b) -> a -> b
$ \Handle
fileHandle -> do
Handle -> TextEncoding -> IO ()
System.IO.hSetEncoding Handle
fileHandle TextEncoding
textEncoding
Tag
contents <- Handle -> IO Tag
System.IO.hGetContents Handle
fileHandle
Tag
-> IO (Either Tag (PGNDatabase x y))
-> IO (Either Tag (PGNDatabase x y))
forall a b. NFData a => a -> b -> b
Control.DeepSeq.deepseq Tag
contents (IO (Either Tag (PGNDatabase x y))
-> IO (Either Tag (PGNDatabase x y)))
-> (Either Tag (PGNDatabase x y)
-> IO (Either Tag (PGNDatabase x y)))
-> Either Tag (PGNDatabase x y)
-> IO (Either Tag (PGNDatabase x y))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either Tag (PGNDatabase x y) -> IO (Either Tag (PGNDatabase x y))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Tag (PGNDatabase x y) -> IO (Either Tag (PGNDatabase x y)))
-> Either Tag (PGNDatabase x y)
-> IO (Either Tag (PGNDatabase x y))
forall a b. (a -> b) -> a -> b
$ Tag
-> IsStrictlySequential
-> IsStrictlySequential
-> [Tag]
-> Tag
-> Either Tag (PGNDatabase x y)
forall x y.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y) =>
Tag
-> IsStrictlySequential
-> IsStrictlySequential
-> [Tag]
-> Tag
-> Either Tag (PGNDatabase x y)
parse Tag
filePath IsStrictlySequential
isStrictlySequential IsStrictlySequential
validateMoves [Tag]
identificationTags Tag
contents