-- | Parsing utilities
module Music.Theory.Parse where

import Data.Maybe {- base -}

import qualified Text.Parsec as P {- parsec -}
import qualified Text.Parsec.String as P {- parsec -}

-- | A 'Char' parser.
type P a = P.GenParser Char () a

-- | Boolean 'P' for given 'Char'.
is_char :: Char -> P Bool
is_char :: Char -> P Bool
is_char = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
P.optionMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
P.char

-- | Parse 'Integral'.
parse_int :: Integral i => P i
parse_int :: forall i. Integral i => P i
parse_int = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Num a => Integer -> a
fromInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Read a => [Char] -> a
read) (forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
P.many1 forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.digit)

run_parser :: P t -> String -> Either P.ParseError t
run_parser :: forall t. P t -> [Char] -> Either ParseError t
run_parser P t
p = forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> [Char] -> s -> Either ParseError a
P.runParser P t
p () [Char]
""

run_parser_maybe :: P t -> String -> Maybe t
run_parser_maybe :: forall t. P t -> [Char] -> Maybe t
run_parser_maybe P t
p = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. P t -> [Char] -> Either ParseError t
run_parser P t
p

run_parser_error :: P c -> String -> c
run_parser_error :: forall c. P c -> [Char] -> c
run_parser_error P c
p = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. HasCallStack => [Char] -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> [Char]
show) forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. P t -> [Char] -> Either ParseError t
run_parser P c
p