--------------------------------------------------------------------
-- |
-- Copyright :  © Edward Kmett 2010-2014, Johan Kiviniemi 2013
-- License   :  BSD3
-- Maintainer:  Edward Kmett <ekmett@gmail.com>
-- Stability :  experimental
-- Portability: non-portable
--
-- A trivial, inefficient parser with no support for error reporting.
--------------------------------------------------------------------
module Ersatz.Internal.Parser
  ( Parser
  , runParser
  , sepBy, sepBy1
  , token, string
  , integer, natural
  , eof
  , satisfy
  ) where

import Control.Applicative
import Control.Monad (guard)
import Control.Monad.Trans.State
import Data.Char (isDigit)

type Parser t a = StateT [t] [] a

runParser :: Parser t a -> [t] -> [a]
runParser :: forall t a. Parser t a -> [t] -> [a]
runParser = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT

sepBy :: Parser t a -> Parser t sep -> Parser t [a]
sepBy :: forall t a sep. Parser t a -> Parser t sep -> Parser t [a]
sepBy Parser t a
p Parser t sep
sep = forall t a sep. Parser t a -> Parser t sep -> Parser t [a]
sepBy1 Parser t a
p Parser t sep
sep forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure []

sepBy1 :: Parser t a -> Parser t sep -> Parser t [a]
sepBy1 :: forall t a sep. Parser t a -> Parser t sep -> Parser t [a]
sepBy1 Parser t a
p Parser t sep
sep = (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser t a
p forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser t sep
sep forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t a
p)

token :: Eq t => t -> Parser t t
token :: forall t. Eq t => t -> Parser t t
token t
t = forall t. (t -> Bool) -> Parser t t
satisfy (forall a. Eq a => a -> a -> Bool
== t
t)

string :: Eq t => [t] -> Parser t [t]
string :: forall t. Eq t => [t] -> Parser t [t]
string = forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall t. Eq t => t -> Parser t t
token

integer :: (Num i, Read i) => Parser Char i
integer :: forall i. (Num i, Read i) => Parser Char i
integer = forall n. Num n => Parser Char (n -> n)
negation forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall i. Read i => Parser Char i
natural

negation :: Num n => Parser Char (n -> n)
negation :: forall n. Num n => Parser Char (n -> n)
negation  =  forall a. Num a => a -> a
negate forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall t. Eq t => t -> Parser t t
token Char
'-'
         forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. a -> a
id

natural :: Read i => Parser Char i
natural :: forall i. Read i => Parser Char i
natural = forall a. Read a => [Char] -> a
read forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (forall t. (t -> Bool) -> Parser t t
satisfy Char -> Bool
isDigit)

eof :: Parser t ()
eof :: forall t. Parser t ()
eof = do
  [] <- forall (m :: * -> *) s. Monad m => StateT s m s
get
  forall (m :: * -> *) a. Monad m => a -> m a
return ()

satisfy :: (t -> Bool) -> Parser t t
satisfy :: forall t. (t -> Bool) -> Parser t t
satisfy t -> Bool
f = do
  (t
t:[t]
ts) <- forall (m :: * -> *) s. Monad m => StateT s m s
get
  forall (f :: * -> *). Alternative f => Bool -> f ()
guard (t -> Bool
f t
t)
  t
t forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put [t]
ts