module Language.LBNF.CFtoAbstract (absRules,absTokens) where
import Language.Haskell.TH
import Language.LBNF.CF
absRules :: CF -> Q [Dec]
absRules cf0 = sequence $
map (prData $ map mkName $ derivations cf0) $ cf2data cf0
absTokens :: CF -> Q [Dec]
absTokens cf0 = sequence $
map (prSpecialData (map mkName $ derivations cf0) cf0) (specialCats cf0)
fixname :: String -> TypeQ
fixname ('[':xs) = appT listT $ conT $ mkName $ init xs
fixname xs = conT $ mkName xs
prData :: [Name] -> Data -> Q Dec
prData deriv (cat,rules) =
dataD (return []) (mkName cat) [] (map cons rules) deriv where
cons (fun,cats) = normalC (mkName fun) $ either (map typ) (const str) cats
typ = strictType notStrict . fixname
str = [typ "String"]
prSpecialData :: [Name] -> CF -> Cat -> Q Dec
prSpecialData deriv cf cat =
newtypeD (return []) (mkName cat) [] con deriv where
con = normalC (mkName cat) $ [typ]
typ = strictType notStrict $ contentSpec cf cat
contentSpec :: CF -> Cat -> Q Type
contentSpec cf cat = if isPositionCat cf cat
then [t|((Int,Int),String)|]
else [t|String|]