module Language.Dung.Input
(
parseAF, pAF
)
where
import Language.Dung.AF
import Text.Parsec
import Text.Parsec.String (Parser)
import Text.Parsec.Char (char, string)
import qualified Text.Parsec.Token as P
import Text.Parsec.Language(haskellStyle)
import Text.Parsec.Error(errorMessages, messageString)
import Data.Either (partitionEithers)
lexer :: P.TokenParser ()
lexer = P.makeTokenParser haskellStyle
whiteSpace :: Parser ()
whiteSpace = P.whiteSpace lexer
identifier :: Parser String
identifier = P.identifier lexer
stringLiteral :: Parser String
stringLiteral = P.stringLiteral lexer
argName :: Parser String
argName = try identifier <|> stringLiteral
pArgument :: Parser String
pArgument = do
string "arg("
arg <- argName
string ")."
whiteSpace
return arg
pAttack :: Parser (String, String)
pAttack = do
string "at"
string "t(" <|> string "k("
arg1 <- argName
char ','
whiteSpace
arg2 <- argName
string ")."
return (arg1, arg2)
pArgOrAttack :: Parser (Either String (String, String))
pArgOrAttack = try (do arg <- pArgument
whiteSpace
return $ Left arg)
<|>
do atk <- pAttack
whiteSpace
return $ Right atk
pAF :: Parser (DungAF String)
pAF = do
ps <- many1 pArgOrAttack
eof
let (args, atks) = partitionEithers ps
return $ AF args atks
parseAF :: String -> Either ParseError (DungAF String)
parseAF = parse pAF ""