module Text.GrammarCombinators.Utils.EnumTokens (
enumRuleTokens,
enumTokens,
enumAllTokens
) where
import Text.GrammarCombinators.Base
import Text.GrammarCombinators.Utils.IsReachable
import Data.Enumerable (enumerate)
newtype EnumTokensRule (phi :: * -> *) (r :: * -> *) t v = ETR {
unETR :: [t]
}
instance ProductionRule (EnumTokensRule phi r t) where
die = ETR []
endOfInput = ETR []
a ||| b = ETR $ unETR a ++ unETR b
a >>> b = ETR $ unETR a ++ unETR b
instance LiftableProductionRule (EnumTokensRule phi r t) where
epsilonL v _ = epsilon v
instance EpsProductionRule (EnumTokensRule phi r t) where
epsilon _ = ETR []
instance (Token t) => TokenProductionRule (EnumTokensRule phi r t) t where
token t = ETR [t]
anyToken = ETR enumerate
instance (ShowFam phi) => RecProductionRule (EnumTokensRule phi r t) phi r where
ref _ = ETR []
instance (ShowFam phi) => LoopProductionRule (EnumTokensRule phi r t) phi r where
manyRef _ = ETR []
many1Ref _ = ETR []
enumRuleTokens :: (Domain phi, Token t) =>
ExtendedContextFreeRule phi r t v -> [t]
enumRuleTokens rule = unETR rule
enumTokens' :: forall phi t r rr . (Token t, Domain phi) =>
((forall ix. phi ix -> [t] -> [t]) -> [t] -> [t]) ->
GExtendedContextFreeGrammar phi t r rr -> [t]
enumTokens' fold' grammar =
let
addRuleTokens :: forall ix. phi ix -> [t] -> [t]
addRuleTokens idx b = enumRuleTokens (grammar idx) ++ b
in fold' addRuleTokens []
enumTokens :: (Token t, Domain phi) => GExtendedContextFreeGrammar phi t r rr -> phi ix -> [t]
enumTokens g idx = enumTokens' (foldReachable g idx) g
enumAllTokens :: (Token t, Domain phi) => GExtendedContextFreeGrammar phi t r rr -> [t]
enumAllTokens = enumTokens' foldFam