module Language.Carneades.Input
(
parseCAES, pCAES
)
where
import Language.Carneades.CarneadesDSL
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)
import Debug.Trace
import Data.Maybe(fromMaybe)
lexer :: P.TokenParser ()
lexer = P.makeTokenParser
(haskellStyle
{ P.reservedNames = ["Scintilla", "Preponderance", "ClearAndConvincing",
"BeyondReasonableDoubt", "DialecticalValidity",
"scintilla", "preponderance", "clear_and_convincing",
"beyond_reasonable_doubt", "dialectical_validity"]
}
)
whiteSpace :: Parser ()
whiteSpace = P.whiteSpace lexer
identifier :: Parser String
identifier = P.identifier lexer
stringLiteral :: Parser String
stringLiteral = P.stringLiteral lexer
symbol :: String -> Parser String
symbol = P.symbol lexer
float :: Parser Double
float = P.float lexer
data Argument' = Arg' String ([PropLiteral], [PropLiteral], PropLiteral)
deriving (Eq, Show)
type Weight' = (String, Double)
type Standard' = (String, PSName)
argName :: Parser String
argName = try identifier <|> stringLiteral
pProposition :: Parser PropLiteral
pProposition = do
p <- argName
whiteSpace
return (mkProp p)
pPropositions :: Parser [PropLiteral]
pPropositions = do
char '[' >> whiteSpace
ps <- pProposition `sepBy` (symbol "," >> whiteSpace)
char ']' >> whiteSpace
return ps
pArgument :: Parser Argument'
pArgument = do
try (string "argument") <|> string "arg"
whiteSpace
name <- argName
prems <- pPropositions
excs <- pPropositions
c <- pProposition
return (Arg' name (prems, excs, c))
pWeight :: Parser Weight'
pWeight = do
string "weight" >> whiteSpace
name <- argName
weight <- float
return (name, weight)
pAssumptions :: Parser Assumptions
pAssumptions = do
string "assumptions" >> whiteSpace
pPropositions
pPSName :: Parser PSName
pPSName = try ((try (string "Scintilla") <|>
string "scintilla")
>> return Scintilla)
<|> try ((try (string "Preponderance") <|>
string "preponderance")
>> return Preponderance)
<|> try ((try (string "ClearAndConvincing") <|>
string "clear_and_convincing")
>> return ClearAndConvincing)
<|> try ((try (string "BeyondReasonableDoubt") <|>
string "beyond_reasonable_doubt")
>> return BeyondReasonableDoubt)
<|> try ((try (string "DialecticalValidity") <|>
string "dialectical_validity")
>> return DialecticalValidity)
pStandard :: Parser Standard'
pStandard = do
string "standard"
whiteSpace
name <- argName
psName <- pPSName
whiteSpace
return (name, psName)
argToArg :: Argument' -> Argument
argToArg (Arg' _ a) = Arg a
lookupArg :: Argument -> [Argument'] -> Maybe String
lookupArg a [] = Nothing
lookupArg a (Arg' name a' : args)
| a == Arg a' = Just name
| otherwise = lookupArg a args
weightToWeight :: [Argument'] -> [Weight'] -> ArgWeight
weightToWeight args ws arg =
fromMaybe (error $ "no weight assigned to" ++ show arg)
(lookupArg arg args >>= \name -> lookup name ws)
standardToStandard :: [Standard'] -> PropStandard
standardToStandard [] p = error $ "no standard assigned to" ++ show p
standardToStandard ((name, st) : sts) p
| mkProp name == p = st
| otherwise = standardToStandard sts p
pCAES :: Parser CAES
pCAES = do
whiteSpace
args <- many pArgument
weights <- many pWeight
assumps <- pAssumptions
standards <- many pStandard
eof
let weight = weightToWeight args weights
let audience = (assumps, weight)
let standard = standardToStandard standards
let argSet = mkArgSet (map argToArg args)
return (CAES (argSet, audience, standard))
parseCAES :: String -> Either ParseError CAES
parseCAES = parse pCAES ""