{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module XMonad.Util.Parser (
Parser,
runParser,
pfail,
eof,
num,
char,
string,
skipSpaces,
get,
look,
gather,
satisfy,
choice,
count,
between,
option,
optionally,
skipMany,
skipMany1,
many1,
sepBy,
sepBy1,
endBy,
endBy1,
munch,
munch1,
chainr,
chainr1,
chainl,
chainl1,
manyTill,
) where
import XMonad.Prelude
import qualified Text.ParserCombinators.ReadP as ReadP
import Data.Coerce (coerce)
import Data.String (IsString (fromString))
import Text.ParserCombinators.ReadP (ReadP, (<++))
newtype Parser a = Parser (ReadP a)
deriving newtype ((forall a b. (a -> b) -> Parser a -> Parser b)
-> (forall a b. a -> Parser b -> Parser a) -> Functor Parser
forall a b. a -> Parser b -> Parser a
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Parser a -> Parser b
fmap :: forall a b. (a -> b) -> Parser a -> Parser b
$c<$ :: forall a b. a -> Parser b -> Parser a
<$ :: forall a b. a -> Parser b -> Parser a
Functor, Functor Parser
Functor Parser =>
(forall a. a -> Parser a)
-> (forall a b. Parser (a -> b) -> Parser a -> Parser b)
-> (forall a b c.
(a -> b -> c) -> Parser a -> Parser b -> Parser c)
-> (forall a b. Parser a -> Parser b -> Parser b)
-> (forall a b. Parser a -> Parser b -> Parser a)
-> Applicative Parser
forall a. a -> Parser a
forall a b. Parser a -> Parser b -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> Parser a
pure :: forall a. a -> Parser a
$c<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
$cliftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
liftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
$c*> :: forall a b. Parser a -> Parser b -> Parser b
*> :: forall a b. Parser a -> Parser b -> Parser b
$c<* :: forall a b. Parser a -> Parser b -> Parser a
<* :: forall a b. Parser a -> Parser b -> Parser a
Applicative, Applicative Parser
Applicative Parser =>
(forall a b. Parser a -> (a -> Parser b) -> Parser b)
-> (forall a b. Parser a -> Parser b -> Parser b)
-> (forall a. a -> Parser a)
-> Monad Parser
forall a. a -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
$c>> :: forall a b. Parser a -> Parser b -> Parser b
>> :: forall a b. Parser a -> Parser b -> Parser b
$creturn :: forall a. a -> Parser a
return :: forall a. a -> Parser a
Monad)
instance Semigroup (Parser a) where
(<>) :: Parser a -> Parser a -> Parser a
<> :: Parser a -> Parser a -> Parser a
(<>) = (ReadP a -> ReadP a -> ReadP a) -> Parser a -> Parser a -> Parser a
forall a b. Coercible a b => a -> b
coerce (forall a. ReadP a -> ReadP a -> ReadP a
(<++) @a)
{-# INLINE (<>) #-}
instance Monoid (Parser a) where
mempty :: Parser a
mempty :: Parser a
mempty = ReadP a -> Parser a
forall a. ReadP a -> Parser a
Parser ReadP a
forall a. ReadP a
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE mempty #-}
instance Alternative Parser where
empty :: Parser a
empty :: forall a. Parser a
empty = Parser a
forall a. Monoid a => a
mempty
{-# INLINE empty #-}
(<|>) :: Parser a -> Parser a -> Parser a
<|> :: forall a. Parser a -> Parser a -> Parser a
(<|>) = Parser a -> Parser a -> Parser a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE (<|>) #-}
instance a ~ String => IsString (Parser a) where
fromString :: String -> Parser a
fromString :: String -> Parser a
fromString = String -> Parser a
String -> Parser String
string
{-# INLINE fromString #-}
runParser :: Parser a -> String -> Maybe a
runParser :: forall a. Parser a -> String -> Maybe a
runParser (Parser ReadP a
p) = ((a, String) -> a) -> Maybe (a, String) -> Maybe a
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, String) -> a
forall a b. (a, b) -> a
fst (Maybe (a, String) -> Maybe a)
-> (String -> Maybe (a, String)) -> String -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(a, String)] -> Maybe (a, String)
forall a. [a] -> Maybe a
listToMaybe ([(a, String)] -> Maybe (a, String))
-> (String -> [(a, String)]) -> String -> Maybe (a, String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadP a -> String -> [(a, String)]
forall a. ReadP a -> ReadS a
ReadP.readP_to_S ReadP a
p
{-# INLINE runParser #-}
pfail :: Parser a
pfail :: forall a. Parser a
pfail = Parser a
forall a. Parser a
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE pfail #-}
get :: Parser Char
get :: Parser Char
get = ReadP Char -> Parser Char
forall a b. Coercible a b => a -> b
coerce ReadP Char
ReadP.get
{-# INLINE get #-}
look :: Parser String
look :: Parser String
look = ReadP String -> Parser String
forall a b. Coercible a b => a -> b
coerce ReadP String
ReadP.look
{-# INLINE look #-}
gather :: forall a. Parser a -> Parser (String, a)
gather :: forall a. Parser a -> Parser (String, a)
gather = (ReadP a -> ReadP (String, a)) -> Parser a -> Parser (String, a)
forall a b. Coercible a b => a -> b
coerce (forall a. ReadP a -> ReadP (String, a)
ReadP.gather @a)
{-# INLINE gather #-}
eof :: Parser ()
eof :: Parser ()
eof = ReadP () -> Parser ()
forall a b. Coercible a b => a -> b
coerce ReadP ()
ReadP.eof
{-# INLINE eof #-}
num :: (Read a, Integral a) => Parser a
num :: forall a. (Read a, Integral a) => Parser a
num = String -> a
forall a. Read a => String -> a
read (String -> a) -> Parser String -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Bool) -> Parser String
munch1 Char -> Bool
isDigit
{-# INLINE num #-}
char :: Char -> Parser Char
char :: Char -> Parser Char
char = (Char -> ReadP Char) -> Char -> Parser Char
forall a b. Coercible a b => a -> b
coerce Char -> ReadP Char
ReadP.char
{-# INLINE char #-}
string :: String -> Parser String
string :: String -> Parser String
string = (String -> ReadP String) -> String -> Parser String
forall a b. Coercible a b => a -> b
coerce String -> ReadP String
ReadP.string
{-# INLINE string #-}
skipSpaces :: Parser ()
skipSpaces :: Parser ()
skipSpaces = ReadP () -> Parser ()
forall a b. Coercible a b => a -> b
coerce ReadP ()
ReadP.skipSpaces
{-# INLINE skipSpaces #-}
satisfy :: (Char -> Bool) -> Parser Char
satisfy :: (Char -> Bool) -> Parser Char
satisfy = ((Char -> Bool) -> ReadP Char) -> (Char -> Bool) -> Parser Char
forall a b. Coercible a b => a -> b
coerce (Char -> Bool) -> ReadP Char
ReadP.satisfy
{-# INLINE satisfy #-}
choice :: [Parser a] -> Parser a
choice :: forall a. [Parser a] -> Parser a
choice = (Parser a -> Parser a -> Parser a)
-> Parser a -> [Parser a] -> Parser a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Parser a -> Parser a -> Parser a
forall a. Semigroup a => a -> a -> a
(<>) Parser a
forall a. Monoid a => a
mempty
{-# INLINE choice #-}
count :: Int -> Parser a -> Parser [a]
count :: forall a. Int -> Parser a -> Parser [a]
count = Int -> Parser a -> Parser [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM
{-# INLINE count #-}
between :: Parser open -> Parser close -> Parser a -> Parser a
between :: forall open close a.
Parser open -> Parser close -> Parser a -> Parser a
between Parser open
open Parser close
close Parser a
p = Parser open
open Parser open -> Parser a -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p Parser a -> Parser close -> Parser a
forall a b. Parser a -> Parser b -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser close
close
{-# INLINE between #-}
option :: a -> Parser a -> Parser a
option :: forall a. a -> Parser a -> Parser a
option a
def Parser a
p = Parser a
p Parser a -> Parser a -> Parser a
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> Parser a
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
def
{-# INLINE option #-}
optionally :: Parser a -> Parser ()
optionally :: forall a. Parser a -> Parser ()
optionally Parser a
p = Parser a -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser a
p Parser () -> Parser () -> Parser ()
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> Parser ()
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE optionally #-}
skipMany :: Parser a -> Parser ()
skipMany :: forall a. Parser a -> Parser ()
skipMany = Parser [a] -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser [a] -> Parser ())
-> (Parser a -> Parser [a]) -> Parser a -> Parser ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser a -> Parser [a]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many
{-# INLINE skipMany #-}
skipMany1 :: Parser a -> Parser ()
skipMany1 :: forall a. Parser a -> Parser ()
skipMany1 Parser a
p = Parser a
p Parser a -> Parser () -> Parser ()
forall a b. Parser a -> Parser b -> Parser b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a -> Parser ()
forall a. Parser a -> Parser ()
skipMany Parser a
p
{-# INLINE skipMany1 #-}
munch :: (Char -> Bool) -> Parser String
munch :: (Char -> Bool) -> Parser String
munch = ((Char -> Bool) -> ReadP String) -> (Char -> Bool) -> Parser String
forall a b. Coercible a b => a -> b
coerce (Char -> Bool) -> ReadP String
ReadP.munch
{-# INLINE munch #-}
munch1 :: (Char -> Bool) -> Parser String
munch1 :: (Char -> Bool) -> Parser String
munch1 = ((Char -> Bool) -> ReadP String) -> (Char -> Bool) -> Parser String
forall a b. Coercible a b => a -> b
coerce (Char -> Bool) -> ReadP String
ReadP.munch1
{-# INLINE munch1 #-}
endBy :: Parser a -> Parser sep -> Parser [a]
endBy :: forall a sep. Parser a -> Parser sep -> Parser [a]
endBy Parser a
p Parser sep
sep = Parser a -> Parser [a]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser a
p Parser a -> Parser sep -> Parser a
forall a b. Parser a -> Parser b -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser sep
sep)
{-# INLINE endBy #-}
endBy1 :: Parser a -> Parser sep -> Parser [a]
endBy1 :: forall a sep. Parser a -> Parser sep -> Parser [a]
endBy1 Parser a
p Parser sep
sep = Parser a -> Parser [a]
forall a. Parser a -> Parser [a]
many1 (Parser a
p Parser a -> Parser sep -> Parser a
forall a b. Parser a -> Parser b -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser sep
sep)
{-# INLINE endBy1 #-}
many1 :: Parser a -> Parser [a]
many1 :: forall a. Parser a -> Parser [a]
many1 = Parser a -> Parser [a]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some
{-# INLINE many1 #-}
sepBy :: Parser a -> Parser sep -> Parser [a]
sepBy :: forall a sep. Parser a -> Parser sep -> Parser [a]
sepBy Parser a
p Parser sep
sep = Parser a -> Parser sep -> Parser [a]
forall a sep. Parser a -> Parser sep -> Parser [a]
sepBy1 Parser a
p Parser sep
sep Parser [a] -> Parser [a] -> Parser [a]
forall a. Semigroup a => a -> a -> a
<> [a] -> Parser [a]
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
{-# INLINE sepBy #-}
sepBy1 :: Parser a -> Parser sep -> Parser [a]
sepBy1 :: forall a sep. Parser a -> Parser sep -> Parser [a]
sepBy1 Parser a
p Parser sep
sep = (a -> [a] -> [a]) -> Parser a -> Parser [a] -> Parser [a]
forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) Parser a
p (Parser a -> Parser [a]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser sep
sep Parser sep -> Parser a -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p))
{-# INLINE sepBy1 #-}
chainr :: Parser a -> Parser (a -> a -> a) -> a -> Parser a
chainr :: forall a. Parser a -> Parser (a -> a -> a) -> a -> Parser a
chainr Parser a
p Parser (a -> a -> a)
op a
x = a -> Parser a -> Parser a
forall a. a -> Parser a -> Parser a
option a
x (Parser a -> Parser (a -> a -> a) -> Parser a
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 Parser a
p Parser (a -> a -> a)
op)
{-# INLINE chainr #-}
chainr1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 Parser a
p Parser (a -> a -> a)
op = Parser a
scan
where
scan :: Parser a
scan :: Parser a
scan = Parser a
p Parser a -> (a -> Parser a) -> Parser a
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Parser a
rest
rest :: a -> Parser a
rest :: a -> Parser a
rest a
x = a -> Parser a -> Parser a
forall a. a -> Parser a -> Parser a
option a
x (Parser a -> Parser a) -> Parser a -> Parser a
forall a b. (a -> b) -> a -> b
$ do a -> a -> a
f <- Parser (a -> a -> a)
op
a -> a -> a
f a
x (a -> a) -> Parser a -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser a
scan
{-# INLINE chainr1 #-}
chainl :: Parser a -> Parser (a -> a -> a) -> a -> Parser a
chainl :: forall a. Parser a -> Parser (a -> a -> a) -> a -> Parser a
chainl Parser a
p Parser (a -> a -> a)
op a
x = a -> Parser a -> Parser a
forall a. a -> Parser a -> Parser a
option a
x (Parser a -> Parser (a -> a -> a) -> Parser a
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 Parser a
p Parser (a -> a -> a)
op)
{-# INLINE chainl #-}
chainl1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 :: forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 Parser a
p Parser (a -> a -> a)
op = Parser a
scan
where
scan :: Parser a
scan :: Parser a
scan = Parser a
p Parser a -> (a -> Parser a) -> Parser a
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Parser a
rest
rest :: a -> Parser a
rest :: a -> Parser a
rest a
x = a -> Parser a -> Parser a
forall a. a -> Parser a -> Parser a
option a
x (Parser a -> Parser a) -> Parser a -> Parser a
forall a b. (a -> b) -> a -> b
$ do a -> a -> a
f <- Parser (a -> a -> a)
op
a
y <- Parser a
p
a -> Parser a
rest (a -> a -> a
f a
x a
y)
{-# INLINE chainl1 #-}
manyTill :: forall a end. Parser a -> Parser end -> Parser [a]
manyTill :: forall a sep. Parser a -> Parser sep -> Parser [a]
manyTill Parser a
p Parser end
end = Parser [a]
scan
where
scan :: Parser [a]
scan :: Parser [a]
scan = Parser end
end Parser end -> [a] -> Parser [a]
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> [] Parser [a] -> Parser [a] -> Parser [a]
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a -> [a] -> [a]) -> Parser a -> Parser [a] -> Parser [a]
forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) Parser a
p Parser [a]
scan
{-# INLINE manyTill #-}