{-# LANGUAGE Safe #-}
module Text.ParserCombinators.Parsec.Utils(
GeneralizedToken, GeneralizedTokenParser,
togtok, tokeng, satisfyg, oneOfg, noneOfg,
specificg, allg,
notMatching
) where
import safe Text.ParserCombinators.Parsec
( (<?>),
(<|>),
getPosition,
many,
token,
unexpected,
try,
SourcePos,
GenParser )
type GeneralizedToken a = (SourcePos, a)
type GeneralizedTokenParser a st b = GenParser (GeneralizedToken a) st b
togtok :: a -> GenParser b st (GeneralizedToken a)
togtok :: forall a b st. a -> GenParser b st (GeneralizedToken a)
togtok a
tok = do
SourcePos
x <- forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
forall (m :: * -> *) a. Monad m => a -> m a
return (SourcePos
x, a
tok)
tokeng :: (Show a) => (a -> Maybe b) -> GeneralizedTokenParser a st b
tokeng :: forall a b st.
Show a =>
(a -> Maybe b) -> GeneralizedTokenParser a st b
tokeng a -> Maybe b
test =
forall s t a u.
Stream s Identity t =>
(t -> String) -> (t -> SourcePos) -> (t -> Maybe a) -> Parsec s u a
token (forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) (forall a b. (a, b) -> a
fst) (a -> Maybe b
test forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd)
satisfyg :: (Show a) => (a -> Bool) -> GeneralizedTokenParser a st a
satisfyg :: forall a st. Show a => (a -> Bool) -> GeneralizedTokenParser a st a
satisfyg a -> Bool
test = forall a b st.
Show a =>
(a -> Maybe b) -> GeneralizedTokenParser a st b
tokeng (\a
t -> if a -> Bool
test a
t then forall a. a -> Maybe a
Just a
t else forall a. Maybe a
Nothing)
oneOfg :: (Eq a, Show a) => [a] -> GeneralizedTokenParser a st a
oneOfg :: forall a st. (Eq a, Show a) => [a] -> GeneralizedTokenParser a st a
oneOfg [a]
i = forall a st. Show a => (a -> Bool) -> GeneralizedTokenParser a st a
satisfyg (\a
x -> forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
x [a]
i)
allg :: (Show a) => GeneralizedTokenParser a st [a]
allg :: forall a st. Show a => GeneralizedTokenParser a st [a]
allg = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many forall a b. (a -> b) -> a -> b
$ forall a st. Show a => (a -> Bool) -> GeneralizedTokenParser a st a
satisfyg (\a
_ -> Bool
True)
noneOfg :: (Eq a, Show a) => [a] -> GeneralizedTokenParser a st a
noneOfg :: forall a st. (Eq a, Show a) => [a] -> GeneralizedTokenParser a st a
noneOfg [a]
l = forall a st. Show a => (a -> Bool) -> GeneralizedTokenParser a st a
satisfyg (\a
x -> Bool -> Bool
not (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
x [a]
l))
specificg :: (Eq a, Show a) => a -> GeneralizedTokenParser a st a
specificg :: forall a st. (Eq a, Show a) => a -> GeneralizedTokenParser a st a
specificg a
i = forall a st. Show a => (a -> Bool) -> GeneralizedTokenParser a st a
satisfyg (forall a. Eq a => a -> a -> Bool
== a
i) forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> forall a. Show a => a -> String
show a
i
notMatching :: GenParser a b c -> String -> GenParser a b ()
notMatching :: forall a b c. GenParser a b c -> String -> GenParser a b ()
notMatching GenParser a b c
p String
errormsg =
let maybeRead :: ParsecT [a] b Identity (Maybe c)
maybeRead = forall tok st a. GenParser tok st a -> GenParser tok st a
try (do
c
x <- GenParser a b c
p
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just c
x)
)
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
workerFunc :: ParsecT [a] b Identity ()
workerFunc = do
Maybe c
x <- ParsecT [a] b Identity (Maybe c)
maybeRead
case Maybe c
x of
Maybe c
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just c
_ -> forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected String
errormsg
in
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT [a] b Identity ()
workerFunc