module Text.Earley.Derived where
import Control.Applicative hiding (many)
import Data.ListLike(ListLike)
import qualified Data.ListLike as ListLike
import Text.Earley.Grammar
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
list :: Eq t => [t] -> Prod r e t [t]
list = listLike
listLike :: (Eq t, ListLike i t) => i -> Prod r e t i
listLike = ListLike.foldr (liftA2 ListLike.cons . satisfy . (==)) (pure ListLike.empty)
symbol :: Eq t => t -> Prod r e t t
symbol = token
namedSymbol :: Eq t => t -> Prod r e t t
namedSymbol = token
word :: Eq t => [t] -> Prod r e t [t]
word = list