{-# LANGUAGE OverloadedStrings #-}

module SimpleParser.Examples.Sexp
  ( Sexp (..)
  , SexpF (..)
  , Atom (..)
  , SexpLabel (..)
  , SexpParserC
  , SexpParserM
  , sexpParser
  , recSexpParser
  ) where

import Control.Monad (void)
import Data.Char (isDigit, isSpace)
import Data.Foldable (asum)
import Data.Scientific (Scientific)
import Data.Sequence (Seq)
import Data.Text (Text)
import Data.Void (Void)
import SimpleParser (Chunked (..), EmbedTextLabel (..), ExplainLabel (..), Parser, TextLabel, TextualStream,
                     betweenParser, commitParser, decimalParser, escapedStringParser, lexemeParser, matchToken,
                     onEmptyParser, orParser, packChunk, satisfyToken, scientificParser, sepByParser,
                     signedNumStartPred, signedParser, spaceParser, takeTokensWhile)

data Atom =
    AtomIdent !Text
  | AtomString !Text
  | AtomInt !Integer
  | AtomFloat !Scientific
  deriving (Atom -> Atom -> Bool
(Atom -> Atom -> Bool) -> (Atom -> Atom -> Bool) -> Eq Atom
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Atom -> Atom -> Bool
$c/= :: Atom -> Atom -> Bool
== :: Atom -> Atom -> Bool
$c== :: Atom -> Atom -> Bool
Eq, Int -> Atom -> ShowS
[Atom] -> ShowS
Atom -> String
(Int -> Atom -> ShowS)
-> (Atom -> String) -> ([Atom] -> ShowS) -> Show Atom
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Atom] -> ShowS
$cshowList :: [Atom] -> ShowS
show :: Atom -> String
$cshow :: Atom -> String
showsPrec :: Int -> Atom -> ShowS
$cshowsPrec :: Int -> Atom -> ShowS
Show)

data SexpF a =
    SexpAtom !Atom
  | SexpList !(Seq a)
  deriving (SexpF a -> SexpF a -> Bool
(SexpF a -> SexpF a -> Bool)
-> (SexpF a -> SexpF a -> Bool) -> Eq (SexpF a)
forall a. Eq a => SexpF a -> SexpF a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SexpF a -> SexpF a -> Bool
$c/= :: forall a. Eq a => SexpF a -> SexpF a -> Bool
== :: SexpF a -> SexpF a -> Bool
$c== :: forall a. Eq a => SexpF a -> SexpF a -> Bool
Eq, Int -> SexpF a -> ShowS
[SexpF a] -> ShowS
SexpF a -> String
(Int -> SexpF a -> ShowS)
-> (SexpF a -> String) -> ([SexpF a] -> ShowS) -> Show (SexpF a)
forall a. Show a => Int -> SexpF a -> ShowS
forall a. Show a => [SexpF a] -> ShowS
forall a. Show a => SexpF a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SexpF a] -> ShowS
$cshowList :: forall a. Show a => [SexpF a] -> ShowS
show :: SexpF a -> String
$cshow :: forall a. Show a => SexpF a -> String
showsPrec :: Int -> SexpF a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> SexpF a -> ShowS
Show, a -> SexpF b -> SexpF a
(a -> b) -> SexpF a -> SexpF b
(forall a b. (a -> b) -> SexpF a -> SexpF b)
-> (forall a b. a -> SexpF b -> SexpF a) -> Functor SexpF
forall a b. a -> SexpF b -> SexpF a
forall a b. (a -> b) -> SexpF a -> SexpF b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SexpF b -> SexpF a
$c<$ :: forall a b. a -> SexpF b -> SexpF a
fmap :: (a -> b) -> SexpF a -> SexpF b
$cfmap :: forall a b. (a -> b) -> SexpF a -> SexpF b
Functor, SexpF a -> Bool
(a -> m) -> SexpF a -> m
(a -> b -> b) -> b -> SexpF a -> b
(forall m. Monoid m => SexpF m -> m)
-> (forall m a. Monoid m => (a -> m) -> SexpF a -> m)
-> (forall m a. Monoid m => (a -> m) -> SexpF a -> m)
-> (forall a b. (a -> b -> b) -> b -> SexpF a -> b)
-> (forall a b. (a -> b -> b) -> b -> SexpF a -> b)
-> (forall b a. (b -> a -> b) -> b -> SexpF a -> b)
-> (forall b a. (b -> a -> b) -> b -> SexpF a -> b)
-> (forall a. (a -> a -> a) -> SexpF a -> a)
-> (forall a. (a -> a -> a) -> SexpF a -> a)
-> (forall a. SexpF a -> [a])
-> (forall a. SexpF a -> Bool)
-> (forall a. SexpF a -> Int)
-> (forall a. Eq a => a -> SexpF a -> Bool)
-> (forall a. Ord a => SexpF a -> a)
-> (forall a. Ord a => SexpF a -> a)
-> (forall a. Num a => SexpF a -> a)
-> (forall a. Num a => SexpF a -> a)
-> Foldable SexpF
forall a. Eq a => a -> SexpF a -> Bool
forall a. Num a => SexpF a -> a
forall a. Ord a => SexpF a -> a
forall m. Monoid m => SexpF m -> m
forall a. SexpF a -> Bool
forall a. SexpF a -> Int
forall a. SexpF a -> [a]
forall a. (a -> a -> a) -> SexpF a -> a
forall m a. Monoid m => (a -> m) -> SexpF a -> m
forall b a. (b -> a -> b) -> b -> SexpF a -> b
forall a b. (a -> b -> b) -> b -> SexpF a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: SexpF a -> a
$cproduct :: forall a. Num a => SexpF a -> a
sum :: SexpF a -> a
$csum :: forall a. Num a => SexpF a -> a
minimum :: SexpF a -> a
$cminimum :: forall a. Ord a => SexpF a -> a
maximum :: SexpF a -> a
$cmaximum :: forall a. Ord a => SexpF a -> a
elem :: a -> SexpF a -> Bool
$celem :: forall a. Eq a => a -> SexpF a -> Bool
length :: SexpF a -> Int
$clength :: forall a. SexpF a -> Int
null :: SexpF a -> Bool
$cnull :: forall a. SexpF a -> Bool
toList :: SexpF a -> [a]
$ctoList :: forall a. SexpF a -> [a]
foldl1 :: (a -> a -> a) -> SexpF a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SexpF a -> a
foldr1 :: (a -> a -> a) -> SexpF a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SexpF a -> a
foldl' :: (b -> a -> b) -> b -> SexpF a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SexpF a -> b
foldl :: (b -> a -> b) -> b -> SexpF a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SexpF a -> b
foldr' :: (a -> b -> b) -> b -> SexpF a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SexpF a -> b
foldr :: (a -> b -> b) -> b -> SexpF a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SexpF a -> b
foldMap' :: (a -> m) -> SexpF a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SexpF a -> m
foldMap :: (a -> m) -> SexpF a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SexpF a -> m
fold :: SexpF m -> m
$cfold :: forall m. Monoid m => SexpF m -> m
Foldable, Functor SexpF
Foldable SexpF
Functor SexpF
-> Foldable SexpF
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> SexpF a -> f (SexpF b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    SexpF (f a) -> f (SexpF a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> SexpF a -> m (SexpF b))
-> (forall (m :: * -> *) a. Monad m => SexpF (m a) -> m (SexpF a))
-> Traversable SexpF
(a -> f b) -> SexpF a -> f (SexpF b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => SexpF (m a) -> m (SexpF a)
forall (f :: * -> *) a. Applicative f => SexpF (f a) -> f (SexpF a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SexpF a -> m (SexpF b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SexpF a -> f (SexpF b)
sequence :: SexpF (m a) -> m (SexpF a)
$csequence :: forall (m :: * -> *) a. Monad m => SexpF (m a) -> m (SexpF a)
mapM :: (a -> m b) -> SexpF a -> m (SexpF b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SexpF a -> m (SexpF b)
sequenceA :: SexpF (f a) -> f (SexpF a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => SexpF (f a) -> f (SexpF a)
traverse :: (a -> f b) -> SexpF a -> f (SexpF b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SexpF a -> f (SexpF b)
$cp2Traversable :: Foldable SexpF
$cp1Traversable :: Functor SexpF
Traversable)

data SexpLabel =
    SexpLabelIdentStart
  | SexpLabelEmbedText !TextLabel
  deriving (SexpLabel -> SexpLabel -> Bool
(SexpLabel -> SexpLabel -> Bool)
-> (SexpLabel -> SexpLabel -> Bool) -> Eq SexpLabel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SexpLabel -> SexpLabel -> Bool
$c/= :: SexpLabel -> SexpLabel -> Bool
== :: SexpLabel -> SexpLabel -> Bool
$c== :: SexpLabel -> SexpLabel -> Bool
Eq, Int -> SexpLabel -> ShowS
[SexpLabel] -> ShowS
SexpLabel -> String
(Int -> SexpLabel -> ShowS)
-> (SexpLabel -> String)
-> ([SexpLabel] -> ShowS)
-> Show SexpLabel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SexpLabel] -> ShowS
$cshowList :: [SexpLabel] -> ShowS
show :: SexpLabel -> String
$cshow :: SexpLabel -> String
showsPrec :: Int -> SexpLabel -> ShowS
$cshowsPrec :: Int -> SexpLabel -> ShowS
Show)

instance ExplainLabel SexpLabel where
  explainLabel :: SexpLabel -> Builder
explainLabel SexpLabel
sl =
    case SexpLabel
sl of
      SexpLabel
SexpLabelIdentStart -> Builder
"start of identifier"
      SexpLabelEmbedText TextLabel
tl -> TextLabel -> Builder
forall l. ExplainLabel l => l -> Builder
explainLabel TextLabel
tl

instance EmbedTextLabel SexpLabel where
  embedTextLabel :: TextLabel -> SexpLabel
embedTextLabel = TextLabel -> SexpLabel
SexpLabelEmbedText

newtype Sexp = Sexp { Sexp -> SexpF Sexp
unSexp :: SexpF Sexp }
  deriving (Sexp -> Sexp -> Bool
(Sexp -> Sexp -> Bool) -> (Sexp -> Sexp -> Bool) -> Eq Sexp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Sexp -> Sexp -> Bool
$c/= :: Sexp -> Sexp -> Bool
== :: Sexp -> Sexp -> Bool
$c== :: Sexp -> Sexp -> Bool
Eq, Int -> Sexp -> ShowS
[Sexp] -> ShowS
Sexp -> String
(Int -> Sexp -> ShowS)
-> (Sexp -> String) -> ([Sexp] -> ShowS) -> Show Sexp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Sexp] -> ShowS
$cshowList :: [Sexp] -> ShowS
show :: Sexp -> String
$cshow :: Sexp -> String
showsPrec :: Int -> Sexp -> ShowS
$cshowsPrec :: Int -> Sexp -> ShowS
Show)

type SexpParserC s = TextualStream s

type SexpParserM s a = Parser SexpLabel s Void a

sexpParser :: SexpParserC s => SexpParserM s Sexp
sexpParser :: SexpParserM s Sexp
sexpParser = let p :: SexpParserM s Sexp
p = (SexpF Sexp -> Sexp)
-> ParserT SexpLabel s Void Identity (SexpF Sexp)
-> SexpParserM s Sexp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SexpF Sexp -> Sexp
Sexp (SexpParserM s Sexp
-> ParserT SexpLabel s Void Identity (SexpF Sexp)
forall s a.
SexpParserC s =>
SexpParserM s a -> SexpParserM s (SexpF a)
recSexpParser SexpParserM s Sexp
p) in SexpParserM s Sexp
p

recSexpParser :: SexpParserC s => SexpParserM s a -> SexpParserM s (SexpF a)
recSexpParser :: SexpParserM s a -> SexpParserM s (SexpF a)
recSexpParser SexpParserM s a
root = SexpParserM s (SexpF a)
-> SexpParserM s (SexpF a) -> SexpParserM s (SexpF a)
forall l s e a. Parser l s e a -> Parser l s e a -> Parser l s e a
onEmptyParser (SexpParserM s (SexpF a)
-> SexpParserM s (SexpF a) -> SexpParserM s (SexpF a)
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m a -> ParserT l s e m a -> ParserT l s e m a
orParser SexpParserM s (SexpF a)
lp SexpParserM s (SexpF a)
forall a. ParserT SexpLabel s Void Identity (SexpF a)
ap) (String -> SexpParserM s (SexpF a)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"failed to parse sexp document") where
  lp :: SexpParserM s (SexpF a)
lp = ParserT SexpLabel s Void Identity ()
-> SexpParserM s (SexpF a) -> SexpParserM s (SexpF a)
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
commitParser ParserT SexpLabel s Void Identity ()
forall s. SexpParserC s => SexpParserM s ()
openParenP ((Seq a -> SexpF a)
-> ParserT SexpLabel s Void Identity (Seq a)
-> SexpParserM s (SexpF a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Seq a -> SexpF a
forall a. Seq a -> SexpF a
SexpList (SexpParserM s a -> ParserT SexpLabel s Void Identity (Seq a)
forall s a.
SexpParserC s =>
SexpParserM s a -> SexpParserM s (Seq a)
listP SexpParserM s a
root))
  ap :: ParserT SexpLabel s Void Identity (SexpF a)
ap = (Atom -> SexpF a)
-> ParserT SexpLabel s Void Identity Atom
-> ParserT SexpLabel s Void Identity (SexpF a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Atom -> SexpF a
forall a. Atom -> SexpF a
SexpAtom ParserT SexpLabel s Void Identity Atom
forall s. SexpParserC s => SexpParserM s Atom
atomP

nonDelimPred :: Char -> Bool
nonDelimPred :: Char -> Bool
nonDelimPred Char
c = Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'(' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
')' Bool -> Bool -> Bool
&& Bool -> Bool
not (Char -> Bool
isSpace Char
c)

identStartPred :: Char -> Bool
identStartPred :: Char -> Bool
identStartPred Char
c = Bool -> Bool
not (Char -> Bool
isDigit Char
c) Bool -> Bool -> Bool
&& Char -> Bool
identContPred Char
c

identContPred :: Char -> Bool
identContPred :: Char -> Bool
identContPred Char
c = Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'"' Bool -> Bool -> Bool
&& Char -> Bool
nonDelimPred Char
c

stringP :: SexpParserC s => SexpParserM s Text
stringP :: SexpParserM s Text
stringP = (Chunk s -> Text)
-> ParserT SexpLabel s Void Identity (Chunk s)
-> SexpParserM s Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Chunk s -> Text
forall chunk. TextualChunked chunk => chunk -> Text
packChunk (Char -> ParserT SexpLabel s Void Identity (Chunk s)
forall s (m :: * -> *) l e.
(Stream s, Token s ~ Char, Monad m) =>
Char -> ParserT l s e m (Chunk s)
escapedStringParser Char
'"')

identifierP :: SexpParserC s => SexpParserM s Text
identifierP :: SexpParserM s Text
identifierP = do
  Char
x <- Maybe SexpLabel
-> (Token s -> Bool) -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m) =>
Maybe l -> (Token s -> Bool) -> ParserT l s e m (Token s)
satisfyToken (SexpLabel -> Maybe SexpLabel
forall a. a -> Maybe a
Just SexpLabel
SexpLabelIdentStart) Char -> Bool
Token s -> Bool
identStartPred
  Chunk s
xs <- (Token s -> Bool) -> ParserT SexpLabel s Void Identity (Chunk s)
forall s (m :: * -> *) l e.
(Stream s, Monad m) =>
(Token s -> Bool) -> ParserT l s e m (Chunk s)
takeTokensWhile Char -> Bool
Token s -> Bool
identContPred
  Text -> SexpParserM s Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Chunk s -> Text
forall chunk. TextualChunked chunk => chunk -> Text
packChunk (Char -> Chunk s -> Chunk s
forall chunk token. Chunked chunk token => token -> chunk -> chunk
consChunk Char
x Chunk s
xs))

spaceP :: SexpParserC s => SexpParserM s ()
spaceP :: SexpParserM s ()
spaceP = SexpParserM s ()
forall s (m :: * -> *) l e.
(Stream s, Token s ~ Char, Monad m) =>
ParserT l s e m ()
spaceParser

lexP :: SexpParserC s => SexpParserM s a -> SexpParserM s a
lexP :: SexpParserM s a -> SexpParserM s a
lexP = ParserT SexpLabel s Void Identity ()
-> SexpParserM s a -> SexpParserM s a
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
lexemeParser ParserT SexpLabel s Void Identity ()
forall s. SexpParserC s => SexpParserM s ()
spaceP

openParenP :: SexpParserC s => SexpParserM s ()
openParenP :: SexpParserM s ()
openParenP = SexpParserM s () -> SexpParserM s ()
forall s a. SexpParserC s => SexpParserM s a -> SexpParserM s a
lexP (ParserT SexpLabel s Void Identity Char -> SexpParserM s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Token s -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m, Eq (Token s)) =>
Token s -> ParserT l s e m (Token s)
matchToken Char
Token s
'('))

closeParenP :: SexpParserC s => SexpParserM s ()
closeParenP :: SexpParserM s ()
closeParenP = SexpParserM s () -> SexpParserM s ()
forall s a. SexpParserC s => SexpParserM s a -> SexpParserM s a
lexP (ParserT SexpLabel s Void Identity Char -> SexpParserM s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Token s -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m, Eq (Token s)) =>
Token s -> ParserT l s e m (Token s)
matchToken Char
Token s
')'))

intP :: SexpParserC s => SexpParserM s Integer
intP :: SexpParserM s Integer
intP = ParserT SexpLabel s Void Identity ()
-> SexpParserM s Integer -> SexpParserM s Integer
forall s (m :: * -> *) a l e.
(Stream s, Token s ~ Char, Monad m, Num a) =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
signedParser (() -> ParserT SexpLabel s Void Identity ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) SexpParserM s Integer
forall l s (m :: * -> *) a e.
(EmbedTextLabel l, Stream s, Token s ~ Char, Monad m, Num a) =>
ParserT l s e m a
decimalParser

floatP :: SexpParserC s => SexpParserM s Scientific
floatP :: SexpParserM s Scientific
floatP = ParserT SexpLabel s Void Identity ()
-> SexpParserM s Scientific -> SexpParserM s Scientific
forall s (m :: * -> *) a l e.
(Stream s, Token s ~ Char, Monad m, Num a) =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
signedParser (() -> ParserT SexpLabel s Void Identity ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) SexpParserM s Scientific
forall l s (m :: * -> *) e.
(EmbedTextLabel l, Stream s, Token s ~ Char, Monad m) =>
ParserT l s e m Scientific
scientificParser

atomP :: SexpParserC s => SexpParserM s Atom
atomP :: SexpParserM s Atom
atomP = SexpParserM s Atom -> SexpParserM s Atom
forall s a. SexpParserC s => SexpParserM s a -> SexpParserM s a
lexP (SexpParserM s Atom -> SexpParserM s Atom)
-> SexpParserM s Atom -> SexpParserM s Atom
forall a b. (a -> b) -> a -> b
$ [SexpParserM s Atom] -> SexpParserM s Atom
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
  [ ParserT SexpLabel s Void Identity ()
-> SexpParserM s Atom -> SexpParserM s Atom
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
commitParser (ParserT SexpLabel s Void Identity Char
-> ParserT SexpLabel s Void Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Token s -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m, Eq (Token s)) =>
Token s -> ParserT l s e m (Token s)
matchToken Char
Token s
'"')) ((Text -> Atom)
-> ParserT SexpLabel s Void Identity Text -> SexpParserM s Atom
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Atom
AtomString ParserT SexpLabel s Void Identity Text
forall s. SexpParserC s => SexpParserM s Text
stringP)
  , ParserT SexpLabel s Void Identity ()
-> SexpParserM s Atom -> SexpParserM s Atom
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
commitParser (ParserT SexpLabel s Void Identity Char
-> ParserT SexpLabel s Void Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Maybe SexpLabel
-> (Token s -> Bool) -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m) =>
Maybe l -> (Token s -> Bool) -> ParserT l s e m (Token s)
satisfyToken Maybe SexpLabel
forall a. Maybe a
Nothing Char -> Bool
Token s -> Bool
signedNumStartPred)) ((Integer -> Atom)
-> ParserT SexpLabel s Void Identity Integer -> SexpParserM s Atom
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Atom
AtomInt ParserT SexpLabel s Void Identity Integer
forall s. SexpParserC s => SexpParserM s Integer
intP)
  , ParserT SexpLabel s Void Identity ()
-> SexpParserM s Atom -> SexpParserM s Atom
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
commitParser (ParserT SexpLabel s Void Identity Char
-> ParserT SexpLabel s Void Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Maybe SexpLabel
-> (Token s -> Bool) -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m) =>
Maybe l -> (Token s -> Bool) -> ParserT l s e m (Token s)
satisfyToken Maybe SexpLabel
forall a. Maybe a
Nothing Char -> Bool
Token s -> Bool
signedNumStartPred)) ((Scientific -> Atom)
-> ParserT SexpLabel s Void Identity Scientific
-> SexpParserM s Atom
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Scientific -> Atom
AtomFloat ParserT SexpLabel s Void Identity Scientific
forall s. SexpParserC s => SexpParserM s Scientific
floatP)
  , ParserT SexpLabel s Void Identity ()
-> SexpParserM s Atom -> SexpParserM s Atom
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
commitParser (ParserT SexpLabel s Void Identity Char
-> ParserT SexpLabel s Void Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Maybe SexpLabel
-> (Token s -> Bool) -> ParserT SexpLabel s Void Identity (Token s)
forall s (m :: * -> *) l e.
(Stream s, Monad m) =>
Maybe l -> (Token s -> Bool) -> ParserT l s e m (Token s)
satisfyToken Maybe SexpLabel
forall a. Maybe a
Nothing Char -> Bool
Token s -> Bool
identStartPred)) ((Text -> Atom)
-> ParserT SexpLabel s Void Identity Text -> SexpParserM s Atom
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Atom
AtomIdent ParserT SexpLabel s Void Identity Text
forall s. SexpParserC s => SexpParserM s Text
identifierP)
  ]

listP :: SexpParserC s => SexpParserM s a -> SexpParserM s (Seq a)
listP :: SexpParserM s a -> SexpParserM s (Seq a)
listP SexpParserM s a
root = SexpParserM s (Seq a) -> SexpParserM s (Seq a)
forall s a. SexpParserC s => SexpParserM s a -> SexpParserM s a
lexP (ParserT SexpLabel s Void Identity ()
-> ParserT SexpLabel s Void Identity ()
-> SexpParserM s (Seq a)
-> SexpParserM s (Seq a)
forall (m :: * -> *) l s e a.
Monad m =>
ParserT l s e m ()
-> ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a
betweenParser ParserT SexpLabel s Void Identity ()
forall s. SexpParserC s => SexpParserM s ()
openParenP ParserT SexpLabel s Void Identity ()
forall s. SexpParserC s => SexpParserM s ()
closeParenP (SexpParserM s a
-> ParserT SexpLabel s Void Identity () -> SexpParserM s (Seq a)
forall seq elem (m :: * -> *) l s e.
(Chunked seq elem, Monad m) =>
ParserT l s e m elem -> ParserT l s e m () -> ParserT l s e m seq
sepByParser SexpParserM s a
root ParserT SexpLabel s Void Identity ()
forall s. SexpParserC s => SexpParserM s ()
spaceP))