{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module XMonad.Util.Parser (
Parser,
runParser,
eof,
num,
char,
string,
skipSpaces,
get,
look,
satisfy,
choice,
many1,
sepBy,
sepBy1,
endBy,
endBy1,
munch,
munch1,
pfail
) 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
<$ :: forall a b. a -> Parser b -> Parser a
$c<$ :: forall a b. a -> Parser b -> Parser a
fmap :: forall a b. (a -> b) -> Parser a -> Parser b
$cfmap :: forall a b. (a -> b) -> Parser a -> Parser b
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
<* :: forall a b. Parser a -> Parser b -> Parser a
$c<* :: forall a b. Parser a -> Parser b -> Parser a
*> :: forall a b. Parser a -> Parser b -> Parser b
$c*> :: forall a b. Parser a -> Parser b -> Parser b
liftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
$cliftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
$c<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
pure :: forall a. a -> Parser a
$cpure :: forall a. a -> 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
return :: forall a. a -> Parser a
$creturn :: forall a. a -> Parser a
>> :: forall a b. Parser a -> Parser b -> Parser b
$c>> :: forall a b. Parser a -> Parser b -> Parser b
>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
$c>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
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
coerce (forall a. ReadP a -> ReadP a -> ReadP a
(<++) @a)
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 (f :: * -> *) a. Alternative f => f a
empty
instance Alternative Parser where
empty :: Parser a
empty :: forall a. Parser a
empty = Parser a
forall a. Monoid a => a
mempty
(<|>) :: 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
(<>)
instance a ~ String => IsString (Parser a) where
fromString :: String -> Parser a
fromString :: String -> Parser a
fromString = String -> Parser a
String -> Parser String
string
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 (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
pfail :: Parser a
pfail :: forall a. Parser a
pfail = Parser a
forall (f :: * -> *) a. Alternative f => f a
empty
get :: Parser Char
get :: Parser Char
get = ReadP Char -> Parser Char
coerce ReadP Char
ReadP.get
look :: Parser String
look :: Parser String
look = ReadP String -> Parser String
coerce ReadP String
ReadP.look
eof :: Parser ()
eof :: Parser ()
eof = ReadP () -> Parser ()
coerce ReadP ()
ReadP.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
{-# SPECIALISE num :: Parser Word #-}
{-# SPECIALISE num :: Parser Int #-}
{-# SPECIALISE num :: Parser Integer #-}
char :: Char -> Parser Char
char :: Char -> Parser Char
char = (Char -> ReadP Char) -> Char -> Parser Char
coerce Char -> ReadP Char
ReadP.char
string :: String -> Parser String
string :: String -> Parser String
string = (String -> ReadP String) -> String -> Parser String
coerce String -> ReadP String
ReadP.string
skipSpaces :: Parser ()
skipSpaces :: Parser ()
skipSpaces = ReadP () -> Parser ()
coerce ReadP ()
ReadP.skipSpaces
satisfy :: (Char -> Bool) -> Parser Char
satisfy :: (Char -> Bool) -> Parser Char
satisfy = ((Char -> Bool) -> ReadP Char) -> (Char -> Bool) -> Parser Char
coerce (Char -> Bool) -> ReadP Char
ReadP.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 (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
munch :: (Char -> Bool) -> Parser String
munch :: (Char -> Bool) -> Parser String
munch = ((Char -> Bool) -> ReadP String) -> (Char -> Bool) -> Parser String
coerce (Char -> Bool) -> ReadP String
ReadP.munch
munch1 :: (Char -> Bool) -> Parser String
munch1 :: (Char -> Bool) -> Parser String
munch1 = ((Char -> Bool) -> ReadP String) -> (Char -> Bool) -> Parser String
coerce (Char -> Bool) -> ReadP String
ReadP.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 (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser a
p Parser a -> Parser sep -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser sep
sep)
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 (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser sep
sep)
many1 :: Parser a -> Parser [a]
many1 :: forall a. Parser a -> Parser [a]
many1 Parser a
p = (a -> [a] -> [a]) -> Parser a -> Parser [a] -> Parser [a]
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 (f :: * -> *) a. Alternative f => f a -> f [a]
many Parser a
p)
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 (f :: * -> *) a. Applicative f => a -> f a
pure []
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 (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) Parser a
p (Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser sep
sep Parser sep -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p))