module PP.Grammar
( To
, InputGrammar(..)
, rules'
) where
import Control.Exception
import Data.Either
import PP.Rule (Rule)
import qualified Text.Parsec as P
type To ast = Either P.ParseError ast
class (Eq ast, Show ast) => InputGrammar ast where
parser :: P.Parsec String () ast
parseAst :: String -> To ast
parseAst = P.parse parser ""
stringify :: ast -> String
stringify = show
rules :: ast -> [Rule]
lexify :: ast -> ast
lexify = id
rules' :: (InputGrammar ast) => ast -> IO (Either String [Rule])
rules' ast = do
a <- try (evaluate $ rules ast) :: IO (Either SomeException [Rule])
case a of
Left e -> return $ Left $ head $ lines $ displayException e
Right r -> return $ Right r