{-# LANGUAGE OverloadedStrings #-}
module ELynx.Import.Nexus
( Block (..),
nexus,
)
where
import Data.Attoparsec.ByteString.Char8
import qualified Data.ByteString.Char8 as BS
data Block a = Block
{ name :: BS.ByteString,
parser :: Parser a
}
nexus :: Block a -> Parser a
nexus b = start *> block b <* endOfInput <?> "nexus"
start :: Parser ()
start = (<?> "start") $ do
_ <- string "#NEXUS"
_ <- skipWhile isSpace
return ()
block :: Block a -> Parser a
block b = beginB (name b) *> parser b <* endB <?> "block"
beginB :: BS.ByteString -> Parser ()
beginB n = (<?> "begin") $ do
_ <- string "BEGIN"
_ <- skipWhile isSpace
_ <- string n
_ <- char ';'
_ <- skipWhile isSpace
return ()
endB :: Parser ()
endB = (<?> "end") $ do
_ <- string "END;"
_ <- skipWhile isSpace
return ()