module AutoProof.Internal.Utils.Parser.Char
( charIf,
char,
anyChar,
spaces,
string,
oneOf,
)
where
import AutoProof.Internal.Utils.Parser.Types (Parser (Parser))
import Control.Applicative (Alternative (empty, many, (<|>)))
import Data.Char (isSpace)
charIf :: (Char -> Bool) -> Parser Char
charIf :: (Char -> Bool) -> Parser Char
charIf p :: Char -> Bool
p = (String -> Maybe (String, Char)) -> Parser Char
forall a. (String -> Maybe (String, a)) -> Parser a
Parser String -> Maybe (String, Char)
f
where
f :: String -> Maybe (String, Char)
f (c :: Char
c : cs :: String
cs) | Char -> Bool
p Char
c = (String, Char) -> Maybe (String, Char)
forall a. a -> Maybe a
Just (String
cs, Char
c)
f _ = Maybe (String, Char)
forall a. Maybe a
Nothing
char :: Char -> Parser Char
char :: Char -> Parser Char
char c :: Char
c = (Char -> Bool) -> Parser Char
charIf (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c)
anyChar :: Parser Char
anyChar :: Parser Char
anyChar = (Char -> Bool) -> Parser Char
charIf (Bool -> Char -> Bool
forall a b. a -> b -> a
const Bool
True)
oneOf :: [Char] -> Parser Char
oneOf :: String -> Parser Char
oneOf = (Char -> Parser Char -> Parser Char)
-> Parser Char -> String -> Parser Char
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Parser Char -> Parser Char -> Parser Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (Parser Char -> Parser Char -> Parser Char)
-> (Char -> Parser Char) -> Char -> Parser Char -> Parser Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Parser Char
char) Parser Char
forall (f :: * -> *) a. Alternative f => f a
empty
string :: String -> Parser String
string :: String -> Parser String
string = (Char -> Parser Char) -> String -> Parser String
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Char -> Parser Char
char
spaces :: Parser String
spaces :: Parser String
spaces = Parser Char -> Parser String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ((Char -> Bool) -> Parser Char
charIf Char -> Bool
isSpace)