module Text.Earley.Derived where
import Control.Applicative hiding (many)
import Data.ListLike(ListLike)
import qualified Data.ListLike as ListLike
import Text.Earley.Grammar
{-# INLINE satisfy #-}
satisfy :: (t -> Bool) -> Prod r e t t
satisfy p = Terminal f $ Pure id
where
f t | p t = Just t
f _ = Nothing
token :: Eq t => t -> Prod r e t t
token x = satisfy (== x)
namedToken :: Eq t => t -> Prod r t t t
namedToken x = token x <?> x
{-# INLINE list #-}
list :: Eq t => [t] -> Prod r e t [t]
list = listLike
{-# INLINE listLike #-}
listLike :: (Eq t, ListLike i t) => i -> Prod r e t i
listLike = ListLike.foldr (liftA2 ListLike.cons . satisfy . (==)) (pure ListLike.empty)