module Data.Katydid.Relapse.Relapse
( parse
, parseWithUDFs
, Grammar
, validate
, filter
)
where
import Prelude hiding ( filter )
import Control.Monad.State ( runState )
import Control.Monad ( filterM )
import Data.Katydid.Parser.Parser
import qualified Data.Katydid.Relapse.Parser as Parser
import qualified Data.Katydid.Relapse.Ast as Ast
import qualified Data.Katydid.Relapse.MemDerive
as MemDerive
import qualified Data.Katydid.Relapse.Smart as Smart
import qualified Data.Katydid.Relapse.Exprs as Exprs
newtype Grammar = Grammar Smart.Grammar
parse :: String -> Either String Grammar
parse grammarString = do
parsed <- Parser.parseGrammar grammarString
Grammar <$> Smart.compile parsed
parseWithUDFs :: Exprs.MkFunc -> String -> Either String Grammar
parseWithUDFs userLib grammarString = do
parsed <- Parser.parseGrammarWithUDFs userLib grammarString
Grammar <$> Smart.compile parsed
validate :: Tree t => Grammar -> [t] -> Bool
validate g tree = case filter g [tree] of
[] -> False
_ -> True
filter :: Tree t => Grammar -> [[t]] -> [[t]]
filter (Grammar g) trees =
let start = Smart.lookupMain g
f = filterM (MemDerive.validate g start) trees
(r, _) = runState f MemDerive.newMem
in r