{-# OPTIONS_GHC -fno-warn-orphans #-}

module Geometry.SvgParser.Common (
      lexeme
    , double
    , char_
    , someMaybeCommaSep
) where



import           Control.Applicative
import           Data.Functor
import           Data.Text                  (Text)
import qualified Text.Megaparsec            as MP
import qualified Text.Megaparsec.Char       as MPC
import qualified Text.Megaparsec.Char.Lexer as MPCLex



instance MP.ShowErrorComponent Text where
    showErrorComponent :: Text -> String
showErrorComponent = Text -> String
forall a. Show a => a -> String
show

-- | Run a parser, and discard any whitespace after it.
lexeme :: Ord err => MP.Parsec err Text a -> MP.Parsec err Text a
lexeme :: forall err a. Ord err => Parsec err Text a -> Parsec err Text a
lexeme  = String
-> ParsecT err Text Identity a -> ParsecT err Text Identity a
forall a.
String
-> ParsecT err Text Identity a -> ParsecT err Text Identity a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
MP.label String
"" (ParsecT err Text Identity a -> ParsecT err Text Identity a)
-> (ParsecT err Text Identity a -> ParsecT err Text Identity a)
-> ParsecT err Text Identity a
-> ParsecT err Text Identity a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParsecT err Text Identity ()
-> ParsecT err Text Identity a -> ParsecT err Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m () -> m a -> m a
MPCLex.lexeme ParsecT err Text Identity ()
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m ()
MPC.space

double :: Ord err => MP.Parsec err Text Double
double :: forall err. Ord err => Parsec err Text Double
double = String
-> ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall a.
String
-> ParsecT err Text Identity a -> ParsecT err Text Identity a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
MP.label String
"number" (ParsecT err Text Identity Double
 -> ParsecT err Text Identity Double)
-> ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall a b. (a -> b) -> a -> b
$ ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall err a. Ord err => Parsec err Text a -> Parsec err Text a
lexeme (ParsecT err Text Identity Double
 -> ParsecT err Text Identity Double)
-> ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall a b. (a -> b) -> a -> b
$ ParsecT err Text Identity ()
-> ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall e s (m :: * -> *) a.
(MonadParsec e s m, Token s ~ Char, Num a) =>
m () -> m a -> m a
MPCLex.signed (() -> ParsecT err Text Identity ()
forall a. a -> ParsecT err Text Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (ParsecT err Text Identity Double
 -> ParsecT err Text Identity Double)
-> ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall a b. (a -> b) -> a -> b
$
    ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall a.
ParsecT err Text Identity a -> ParsecT err Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
MP.try ParsecT err Text Identity Double
forall e s (m :: * -> *) a.
(MonadParsec e s m, Token s ~ Char, RealFloat a) =>
m a
MPCLex.float ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
-> ParsecT err Text Identity Double
forall a.
ParsecT err Text Identity a
-> ParsecT err Text Identity a -> ParsecT err Text Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Integer -> Double)
-> ParsecT err Text Identity Integer
-> ParsecT err Text Identity Double
forall a b.
(a -> b)
-> ParsecT err Text Identity a -> ParsecT err Text Identity b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ParsecT err Text Identity Integer
forall e s (m :: * -> *) a.
(MonadParsec e s m, Token s ~ Char, Num a) =>
m a
MPCLex.decimal

char_ :: Ord err => Char -> MP.Parsec err Text ()
char_ :: forall err. Ord err => Char -> Parsec err Text ()
char_ Char
c = Parsec err Text Char -> Parsec err Text Char
forall err a. Ord err => Parsec err Text a -> Parsec err Text a
lexeme (Token Text -> ParsecT err Text Identity (Token Text)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
MPC.char Char
Token Text
c) Parsec err Text Char -> () -> ParsecT err Text Identity ()
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> ()

someMaybeCommaSep :: Ord err => MP.Parsec err Text a -> MP.Parsec err Text [a]
someMaybeCommaSep :: forall err a. Ord err => Parsec err Text a -> Parsec err Text [a]
someMaybeCommaSep Parsec err Text a
p = Parsec err Text a
-> ParsecT err Text Identity () -> ParsecT err Text Identity [a]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
MP.sepBy1 Parsec err Text a
p (Char -> ParsecT err Text Identity ()
forall err. Ord err => Char -> Parsec err Text ()
char_ Char
',' ParsecT err Text Identity ()
-> ParsecT err Text Identity () -> ParsecT err Text Identity ()
forall a.
ParsecT err Text Identity a
-> ParsecT err Text Identity a -> ParsecT err Text Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT err Text Identity ()
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m ()
MPC.space)