{-# LANGUAGE PatternSynonyms #-}
module Parsley.Fold (
many, some, manyN,
skipMany, skipSome, skipManyN,
sepBy, sepBy1, endBy, endBy1, sepEndBy, sepEndBy1,
chainl1, chainr1, chainl, chainr,
infixl1, infixr1, prefix, postfix,
manyr, manyl,
somer, somel
) where
import Prelude hiding (pure, (<*>), (<$>), (*>), (<*))
import Parsley.Alternative ((<|>), option)
import Parsley.Applicative (pure, (<*>), (<$>), (*>), (<*), (<:>), (<**>), void)
import Parsley.Defunctionalized (Defunc(FLIP, ID, COMPOSE, EMPTY, CONS, CONST), pattern FLIP_H, pattern UNIT)
import Parsley.Internal (Parser)
import Parsley.ParserOps (ParserOps)
import Parsley.Register (get, modify, newRegister)
import qualified Parsley.Internal as Internal (loop)
loop :: Parser () -> Parser a -> Parser a
loop :: forall a. Parser () -> Parser a -> Parser a
loop = forall a. Parser () -> Parser a -> Parser a
Internal.loop
prefix :: Parser (a -> a) -> Parser a -> Parser a
prefix :: forall a. Parser (a -> a) -> Parser a -> Parser a
prefix Parser (a -> a)
op Parser a
p = forall a. Parser a -> Parser (a -> a) -> Parser a
postfix (forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure forall a1. Defunc (a1 -> a1)
ID) (forall y x a b c.
((x -> y) ~ ((a -> b -> c) -> b -> a -> c)) =>
Defunc x -> Defunc y
FLIP_H forall b c a1. Defunc ((b -> c) -> (a1 -> b) -> a1 -> c)
COMPOSE forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser (a -> a)
op) forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser a
p
postfix :: Parser a -> Parser (a -> a) -> Parser a
postfix :: forall a. Parser a -> Parser (a -> a) -> Parser a
postfix Parser a
p Parser (a -> a)
op =
forall a b. Parser a -> (forall r. Reg r a -> Parser b) -> Parser b
newRegister Parser a
p forall a b. (a -> b) -> a -> b
$ \Reg r a
r ->
forall a. Parser () -> Parser a -> Parser a
loop (forall r a. Reg r a -> Parser (a -> a) -> Parser ()
modify Reg r a
r Parser (a -> a)
op)
(forall r a. Reg r a -> Parser a
get Reg r a
r)
manyr :: (ParserOps repf, ParserOps repk) => repf (a -> b -> b) -> repk b -> Parser a -> Parser b
manyr :: forall (repf :: Type -> Type) (repk :: Type -> Type) a b.
(ParserOps repf, ParserOps repk) =>
repf (a -> b -> b) -> repk b -> Parser a -> Parser b
manyr repf (a -> b -> b)
f repk b
k Parser a
p = forall a. Parser (a -> a) -> Parser a -> Parser a
prefix (repf (a -> b -> b)
f forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser a
p) (forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure repk b
k)
somer :: (ParserOps repf, ParserOps repk) => repf (a -> b -> b) -> repk b -> Parser a -> Parser b
somer :: forall (repf :: Type -> Type) (repk :: Type -> Type) a b.
(ParserOps repf, ParserOps repk) =>
repf (a -> b -> b) -> repk b -> Parser a -> Parser b
somer repf (a -> b -> b)
f repk b
k Parser a
p = repf (a -> b -> b)
f forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser a
p forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> forall (repf :: Type -> Type) (repk :: Type -> Type) a b.
(ParserOps repf, ParserOps repk) =>
repf (a -> b -> b) -> repk b -> Parser a -> Parser b
manyr repf (a -> b -> b)
f repk b
k Parser a
p
manyl :: (ParserOps repf, ParserOps repk) => repf (b -> a -> b) -> repk b -> Parser a -> Parser b
manyl :: forall (repf :: Type -> Type) (repk :: Type -> Type) b a.
(ParserOps repf, ParserOps repk) =>
repf (b -> a -> b) -> repk b -> Parser a -> Parser b
manyl repf (b -> a -> b)
f repk b
k Parser a
p = forall a. Parser a -> Parser (a -> a) -> Parser a
postfix (forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure repk b
k) ((forall a1 b c. Defunc ((a1 -> b -> c) -> b -> a1 -> c)
FLIP forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure repf (b -> a -> b)
f) forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser a
p)
somel :: (ParserOps repf, ParserOps repk) => repf (b -> a -> b) -> repk b -> Parser a -> Parser b
somel :: forall (repf :: Type -> Type) (repk :: Type -> Type) b a.
(ParserOps repf, ParserOps repk) =>
repf (b -> a -> b) -> repk b -> Parser a -> Parser b
somel repf (b -> a -> b)
f repk b
k Parser a
p = forall a. Parser a -> Parser (a -> a) -> Parser a
postfix (repf (b -> a -> b)
f forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure repk b
k forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser a
p) ((forall a1 b c. Defunc ((a1 -> b -> c) -> b -> a1 -> c)
FLIP forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure repf (b -> a -> b)
f) forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser a
p)
infixl1 :: ParserOps rep => rep (a -> b) -> Parser a -> Parser (b -> a -> b) -> Parser b
infixl1 :: forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser (b -> a -> b) -> Parser b
infixl1 rep (a -> b)
wrap Parser a
p Parser (b -> a -> b)
op = forall a. Parser a -> Parser (a -> a) -> Parser a
postfix (rep (a -> b)
wrap forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser a
p) (forall a1 b c. Defunc ((a1 -> b -> c) -> b -> a1 -> c)
FLIP forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser (b -> a -> b)
op forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser a
p)
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 = forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser (b -> a -> b) -> Parser b
infixl1 forall a1. Defunc (a1 -> a1)
ID
infixr1 :: ParserOps rep => rep (a -> b) -> Parser a -> Parser (a -> b -> b) -> Parser b
infixr1 :: forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser (a -> b -> b) -> Parser b
infixr1 rep (a -> b)
wrap Parser a
p Parser (a -> b -> b)
op = let go :: Parser b
go = Parser a
p forall a b. Parser a -> Parser (a -> b) -> Parser b
<**> (forall a1 b c. Defunc ((a1 -> b -> c) -> b -> a1 -> c)
FLIP forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser (a -> b -> b)
op forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser b
go forall a. Parser a -> Parser a -> Parser a
<|> forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure rep (a -> b)
wrap) in Parser b
go
chainr1 :: Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 = forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser (a -> b -> b) -> Parser b
infixr1 forall a1. Defunc (a1 -> a1)
ID
chainr :: ParserOps rep => Parser a -> Parser (a -> a -> a) -> rep a -> Parser a
chainr :: forall (rep :: Type -> Type) a.
ParserOps rep =>
Parser a -> Parser (a -> a -> a) -> rep a -> Parser a
chainr Parser a
p Parser (a -> a -> a)
op rep a
x = forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option rep a
x (forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 Parser a
p Parser (a -> a -> a)
op)
chainl :: ParserOps rep => Parser a -> Parser (a -> a -> a) -> rep a -> Parser a
chainl :: forall (rep :: Type -> Type) a.
ParserOps rep =>
Parser a -> Parser (a -> a -> a) -> rep a -> Parser a
chainl Parser a
p Parser (a -> a -> a)
op rep a
x = forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option rep a
x (forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 Parser a
p Parser (a -> a -> a)
op)
many :: Parser a -> Parser [a]
many :: forall a. Parser a -> Parser [a]
many = forall (repf :: Type -> Type) (repk :: Type -> Type) a b.
(ParserOps repf, ParserOps repk) =>
repf (a -> b -> b) -> repk b -> Parser a -> Parser b
manyr forall a1. Defunc (a1 -> [a1] -> [a1])
CONS forall a1. Defunc [a1]
EMPTY
manyN :: Int -> Parser a -> Parser [a]
manyN :: forall a. Int -> Parser a -> Parser [a]
manyN Int
n Parser a
p = forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a b. a -> b -> a
const (Parser a
p forall a. Parser a -> Parser [a] -> Parser [a]
<:>)) (forall a. Parser a -> Parser [a]
many Parser a
p) [Int
1..Int
n]
some :: Parser a -> Parser [a]
some :: forall a. Parser a -> Parser [a]
some = forall a. Int -> Parser a -> Parser [a]
manyN Int
1
skipMany :: Parser a -> Parser ()
skipMany :: forall a. Parser a -> Parser ()
skipMany = forall a. Parser a -> Parser ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (repf :: Type -> Type) (repk :: Type -> Type) b a.
(ParserOps repf, ParserOps repk) =>
repf (b -> a -> b) -> repk b -> Parser a -> Parser b
manyl forall a1 b. Defunc (a1 -> b -> a1)
CONST Defunc ()
UNIT
skipManyN :: Int -> Parser a -> Parser ()
skipManyN :: forall a. Int -> Parser a -> Parser ()
skipManyN Int
n Parser a
p = forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a b. a -> b -> a
const (Parser a
p forall a b. Parser a -> Parser b -> Parser b
*>)) (forall a. Parser a -> Parser ()
skipMany Parser a
p) [Int
1..Int
n]
skipSome :: Parser a -> Parser ()
skipSome :: forall a. Parser a -> Parser ()
skipSome = forall a. Int -> Parser a -> Parser ()
skipManyN Int
1
sepBy :: Parser a -> Parser b -> Parser [a]
sepBy :: forall a b. Parser a -> Parser b -> Parser [a]
sepBy Parser a
p Parser b
sep = forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option forall a1. Defunc [a1]
EMPTY (forall a b. Parser a -> Parser b -> Parser [a]
sepBy1 Parser a
p Parser b
sep)
sepBy1 :: Parser a -> Parser b -> Parser [a]
sepBy1 :: forall a b. Parser a -> Parser b -> Parser [a]
sepBy1 Parser a
p Parser b
sep = Parser a
p forall a. Parser a -> Parser [a] -> Parser [a]
<:> forall a. Parser a -> Parser [a]
many (Parser b
sep forall a b. Parser a -> Parser b -> Parser b
*> Parser a
p)
endBy :: Parser a -> Parser b -> Parser [a]
endBy :: forall a b. Parser a -> Parser b -> Parser [a]
endBy Parser a
p Parser b
sep = forall a. Parser a -> Parser [a]
many (Parser a
p forall a b. Parser a -> Parser b -> Parser a
<* Parser b
sep)
endBy1 :: Parser a -> Parser b -> Parser [a]
endBy1 :: forall a b. Parser a -> Parser b -> Parser [a]
endBy1 Parser a
p Parser b
sep = forall a. Parser a -> Parser [a]
some (Parser a
p forall a b. Parser a -> Parser b -> Parser a
<* Parser b
sep)
sepEndBy :: Parser a -> Parser b -> Parser [a]
sepEndBy :: forall a b. Parser a -> Parser b -> Parser [a]
sepEndBy Parser a
p Parser b
sep = forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option forall a1. Defunc [a1]
EMPTY (forall a b. Parser a -> Parser b -> Parser [a]
sepEndBy1 Parser a
p Parser b
sep)
sepEndBy1 :: Parser a -> Parser b -> Parser [a]
sepEndBy1 :: forall a b. Parser a -> Parser b -> Parser [a]
sepEndBy1 Parser a
p Parser b
sep =
let seb1 :: Parser [a]
seb1 = Parser a
p forall a. Parser a -> Parser [a] -> Parser [a]
<:> forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option forall a1. Defunc [a1]
EMPTY (Parser b
sep forall a b. Parser a -> Parser b -> Parser b
*> forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option forall a1. Defunc [a1]
EMPTY Parser [a]
seb1)
in Parser [a]
seb1