{-# LANGUAGE AllowAmbiguousTypes, ConstraintKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor,
             FlexibleContexts, FlexibleInstances, OverloadedStrings, RankNTypes, ScopedTypeVariables, TypeApplications,
             TypeFamilies, TypeSynonymInstances, UndecidableInstances #-}
module Text.Grampa.Class (MultiParsing(..), GrammarParsing(..),
                          AmbiguousParsing(..), DeterministicParsing(..), InputParsing(..), InputCharParsing(..),
                          ConsumedInputParsing(..), LexicalParsing(..), TailsParsing(..),
                          ParseResults, ParseFailure(..), Expected(..),
                          Ambiguous(..), completeParser) where

import Control.Applicative (Alternative(empty, many, some), optional, liftA2, (<|>))
import Data.ByteString (ByteString, singleton)
import Data.Char (isAlphaNum, isLetter, isSpace)
import Data.Functor (void)
import Data.Functor.Classes (Show1(..))
import Data.Functor.Compose (Compose(..))
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.Data (Data)
import Data.Typeable (Typeable)
import Data.Monoid (Monoid(mempty, mappend))
import qualified Data.Monoid.Null as Null
import Data.Monoid.Null (MonoidNull)
import qualified Data.Monoid.Factorial as Factorial
import Data.Monoid.Factorial (FactorialMonoid)
import Data.Monoid.Textual (TextualMonoid)
import Data.Semigroup (Semigroup((<>)))
import qualified Data.Semigroup.Cancellative as Cancellative
import Text.ParserCombinators.ReadP (ReadP)
import Text.Parser.Combinators (Parsing((<?>), eof, notFollowedBy, try), skipMany, unexpected)
import Text.Parser.Char (CharParsing)
import Text.Parser.LookAhead (LookAheadParsing(lookAhead))
import Text.Parser.Token (TokenParsing)
import Text.Parser.Deterministic (DeterministicParsing(..))
import Text.Parser.Input (ConsumedInputParsing(..), InputParsing(..), InputCharParsing(..))
import qualified Text.Parser.Char
import Data.Kind (Constraint)

import qualified Data.Attoparsec.ByteString as Attoparsec
import qualified Text.ParserCombinators.ReadP as ReadP

import qualified Rank2

import Prelude hiding (takeWhile)

type ParseResults s = Either (ParseFailure s)

-- | A 'ParseFailure' contains the offset of the parse failure and the list of things expected at that offset.
data ParseFailure s = ParseFailure Int [Expected s] deriving (ParseFailure s -> ParseFailure s -> Bool
(ParseFailure s -> ParseFailure s -> Bool)
-> (ParseFailure s -> ParseFailure s -> Bool)
-> Eq (ParseFailure s)
forall s. Eq s => ParseFailure s -> ParseFailure s -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParseFailure s -> ParseFailure s -> Bool
$c/= :: forall s. Eq s => ParseFailure s -> ParseFailure s -> Bool
== :: ParseFailure s -> ParseFailure s -> Bool
$c== :: forall s. Eq s => ParseFailure s -> ParseFailure s -> Bool
Eq, Int -> ParseFailure s -> ShowS
[ParseFailure s] -> ShowS
ParseFailure s -> String
(Int -> ParseFailure s -> ShowS)
-> (ParseFailure s -> String)
-> ([ParseFailure s] -> ShowS)
-> Show (ParseFailure s)
forall s. Show s => Int -> ParseFailure s -> ShowS
forall s. Show s => [ParseFailure s] -> ShowS
forall s. Show s => ParseFailure s -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseFailure s] -> ShowS
$cshowList :: forall s. Show s => [ParseFailure s] -> ShowS
show :: ParseFailure s -> String
$cshow :: forall s. Show s => ParseFailure s -> String
showsPrec :: Int -> ParseFailure s -> ShowS
$cshowsPrec :: forall s. Show s => Int -> ParseFailure s -> ShowS
Show)
data Expected s = Expected String
                | ExpectedInput s
                deriving (a -> Expected b -> Expected a
(a -> b) -> Expected a -> Expected b
(forall a b. (a -> b) -> Expected a -> Expected b)
-> (forall a b. a -> Expected b -> Expected a) -> Functor Expected
forall a b. a -> Expected b -> Expected a
forall a b. (a -> b) -> Expected a -> Expected b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Expected b -> Expected a
$c<$ :: forall a b. a -> Expected b -> Expected a
fmap :: (a -> b) -> Expected a -> Expected b
$cfmap :: forall a b. (a -> b) -> Expected a -> Expected b
Functor, Expected s -> Expected s -> Bool
(Expected s -> Expected s -> Bool)
-> (Expected s -> Expected s -> Bool) -> Eq (Expected s)
forall s. Eq s => Expected s -> Expected s -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Expected s -> Expected s -> Bool
$c/= :: forall s. Eq s => Expected s -> Expected s -> Bool
== :: Expected s -> Expected s -> Bool
$c== :: forall s. Eq s => Expected s -> Expected s -> Bool
Eq, Eq (Expected s)
Eq (Expected s) =>
(Expected s -> Expected s -> Ordering)
-> (Expected s -> Expected s -> Bool)
-> (Expected s -> Expected s -> Bool)
-> (Expected s -> Expected s -> Bool)
-> (Expected s -> Expected s -> Bool)
-> (Expected s -> Expected s -> Expected s)
-> (Expected s -> Expected s -> Expected s)
-> Ord (Expected s)
Expected s -> Expected s -> Bool
Expected s -> Expected s -> Ordering
Expected s -> Expected s -> Expected s
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall s. Ord s => Eq (Expected s)
forall s. Ord s => Expected s -> Expected s -> Bool
forall s. Ord s => Expected s -> Expected s -> Ordering
forall s. Ord s => Expected s -> Expected s -> Expected s
min :: Expected s -> Expected s -> Expected s
$cmin :: forall s. Ord s => Expected s -> Expected s -> Expected s
max :: Expected s -> Expected s -> Expected s
$cmax :: forall s. Ord s => Expected s -> Expected s -> Expected s
>= :: Expected s -> Expected s -> Bool
$c>= :: forall s. Ord s => Expected s -> Expected s -> Bool
> :: Expected s -> Expected s -> Bool
$c> :: forall s. Ord s => Expected s -> Expected s -> Bool
<= :: Expected s -> Expected s -> Bool
$c<= :: forall s. Ord s => Expected s -> Expected s -> Bool
< :: Expected s -> Expected s -> Bool
$c< :: forall s. Ord s => Expected s -> Expected s -> Bool
compare :: Expected s -> Expected s -> Ordering
$ccompare :: forall s. Ord s => Expected s -> Expected s -> Ordering
$cp1Ord :: forall s. Ord s => Eq (Expected s)
Ord, ReadPrec [Expected s]
ReadPrec (Expected s)
Int -> ReadS (Expected s)
ReadS [Expected s]
(Int -> ReadS (Expected s))
-> ReadS [Expected s]
-> ReadPrec (Expected s)
-> ReadPrec [Expected s]
-> Read (Expected s)
forall s. Read s => ReadPrec [Expected s]
forall s. Read s => ReadPrec (Expected s)
forall s. Read s => Int -> ReadS (Expected s)
forall s. Read s => ReadS [Expected s]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Expected s]
$creadListPrec :: forall s. Read s => ReadPrec [Expected s]
readPrec :: ReadPrec (Expected s)
$creadPrec :: forall s. Read s => ReadPrec (Expected s)
readList :: ReadS [Expected s]
$creadList :: forall s. Read s => ReadS [Expected s]
readsPrec :: Int -> ReadS (Expected s)
$creadsPrec :: forall s. Read s => Int -> ReadS (Expected s)
Read, Int -> Expected s -> ShowS
[Expected s] -> ShowS
Expected s -> String
(Int -> Expected s -> ShowS)
-> (Expected s -> String)
-> ([Expected s] -> ShowS)
-> Show (Expected s)
forall s. Show s => Int -> Expected s -> ShowS
forall s. Show s => [Expected s] -> ShowS
forall s. Show s => Expected s -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Expected s] -> ShowS
$cshowList :: forall s. Show s => [Expected s] -> ShowS
show :: Expected s -> String
$cshow :: forall s. Show s => Expected s -> String
showsPrec :: Int -> Expected s -> ShowS
$cshowsPrec :: forall s. Show s => Int -> Expected s -> ShowS
Show)

-- | An 'Ambiguous' parse result, produced by the 'ambiguous' combinator, contains a 'NonEmpty' list of
-- alternative results.
newtype Ambiguous a = Ambiguous{Ambiguous a -> NonEmpty a
getAmbiguous :: NonEmpty a} deriving (Typeable (Ambiguous a)
DataType
Constr
Typeable (Ambiguous a) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Ambiguous a))
-> (Ambiguous a -> Constr)
-> (Ambiguous a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Ambiguous a)))
-> ((forall b. Data b => b -> b) -> Ambiguous a -> Ambiguous a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Ambiguous a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Ambiguous a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a))
-> Data (Ambiguous a)
Ambiguous a -> DataType
Ambiguous a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a))
(forall b. Data b => b -> b) -> Ambiguous a -> Ambiguous a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ambiguous a)
forall a. Data a => Typeable (Ambiguous a)
forall a. Data a => Ambiguous a -> DataType
forall a. Data a => Ambiguous a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> Ambiguous a -> Ambiguous a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Ambiguous a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Ambiguous a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ambiguous a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Ambiguous a))
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Ambiguous a -> u
forall u. (forall d. Data d => d -> u) -> Ambiguous a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ambiguous a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Ambiguous a))
$cAmbiguous :: Constr
$tAmbiguous :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
gmapMp :: (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
gmapM :: (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Ambiguous a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Ambiguous a -> u
gmapQ :: (forall d. Data d => d -> u) -> Ambiguous a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Ambiguous a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r
gmapT :: (forall b. Data b => b -> b) -> Ambiguous a -> Ambiguous a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Ambiguous a -> Ambiguous a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Ambiguous a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Ambiguous a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a))
dataTypeOf :: Ambiguous a -> DataType
$cdataTypeOf :: forall a. Data a => Ambiguous a -> DataType
toConstr :: Ambiguous a -> Constr
$ctoConstr :: forall a. Data a => Ambiguous a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ambiguous a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ambiguous a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a)
$cp1Data :: forall a. Data a => Typeable (Ambiguous a)
Data, Ambiguous a -> Ambiguous a -> Bool
(Ambiguous a -> Ambiguous a -> Bool)
-> (Ambiguous a -> Ambiguous a -> Bool) -> Eq (Ambiguous a)
forall a. Eq a => Ambiguous a -> Ambiguous a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Ambiguous a -> Ambiguous a -> Bool
$c/= :: forall a. Eq a => Ambiguous a -> Ambiguous a -> Bool
== :: Ambiguous a -> Ambiguous a -> Bool
$c== :: forall a. Eq a => Ambiguous a -> Ambiguous a -> Bool
Eq, Eq (Ambiguous a)
Eq (Ambiguous a) =>
(Ambiguous a -> Ambiguous a -> Ordering)
-> (Ambiguous a -> Ambiguous a -> Bool)
-> (Ambiguous a -> Ambiguous a -> Bool)
-> (Ambiguous a -> Ambiguous a -> Bool)
-> (Ambiguous a -> Ambiguous a -> Bool)
-> (Ambiguous a -> Ambiguous a -> Ambiguous a)
-> (Ambiguous a -> Ambiguous a -> Ambiguous a)
-> Ord (Ambiguous a)
Ambiguous a -> Ambiguous a -> Bool
Ambiguous a -> Ambiguous a -> Ordering
Ambiguous a -> Ambiguous a -> Ambiguous a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Ambiguous a)
forall a. Ord a => Ambiguous a -> Ambiguous a -> Bool
forall a. Ord a => Ambiguous a -> Ambiguous a -> Ordering
forall a. Ord a => Ambiguous a -> Ambiguous a -> Ambiguous a
min :: Ambiguous a -> Ambiguous a -> Ambiguous a
$cmin :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Ambiguous a
max :: Ambiguous a -> Ambiguous a -> Ambiguous a
$cmax :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Ambiguous a
>= :: Ambiguous a -> Ambiguous a -> Bool
$c>= :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Bool
> :: Ambiguous a -> Ambiguous a -> Bool
$c> :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Bool
<= :: Ambiguous a -> Ambiguous a -> Bool
$c<= :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Bool
< :: Ambiguous a -> Ambiguous a -> Bool
$c< :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Bool
compare :: Ambiguous a -> Ambiguous a -> Ordering
$ccompare :: forall a. Ord a => Ambiguous a -> Ambiguous a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (Ambiguous a)
Ord, Int -> Ambiguous a -> ShowS
[Ambiguous a] -> ShowS
Ambiguous a -> String
(Int -> Ambiguous a -> ShowS)
-> (Ambiguous a -> String)
-> ([Ambiguous a] -> ShowS)
-> Show (Ambiguous a)
forall a. Show a => Int -> Ambiguous a -> ShowS
forall a. Show a => [Ambiguous a] -> ShowS
forall a. Show a => Ambiguous a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Ambiguous a] -> ShowS
$cshowList :: forall a. Show a => [Ambiguous a] -> ShowS
show :: Ambiguous a -> String
$cshow :: forall a. Show a => Ambiguous a -> String
showsPrec :: Int -> Ambiguous a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Ambiguous a -> ShowS
Show, Typeable)

instance Show1 Ambiguous where
   liftShowsPrec :: (Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Ambiguous a -> ShowS
liftShowsPrec sp :: Int -> a -> ShowS
sp sl :: [a] -> ShowS
sl d :: Int
d (Ambiguous (h :: a
h :| l :: [a]
l)) t :: String
t
      | Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 5 = "(Ambiguous $ " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> a -> ShowS
sp 0 a
h (" :| " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [a] -> ShowS
sl [a]
l (')' Char -> ShowS
forall a. a -> [a] -> [a]
: String
t))
      | Bool
otherwise = "Ambiguous (" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> a -> ShowS
sp 0 a
h (" :| " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [a] -> ShowS
sl [a]
l (')' Char -> ShowS
forall a. a -> [a] -> [a]
: String
t))

instance Functor Ambiguous where
   fmap :: (a -> b) -> Ambiguous a -> Ambiguous b
fmap f :: a -> b
f (Ambiguous a :: NonEmpty a
a) = NonEmpty b -> Ambiguous b
forall a. NonEmpty a -> Ambiguous a
Ambiguous ((a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f NonEmpty a
a)

instance Applicative Ambiguous where
   pure :: a -> Ambiguous a
pure a :: a
a = NonEmpty a -> Ambiguous a
forall a. NonEmpty a -> Ambiguous a
Ambiguous (a -> NonEmpty a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a)
   Ambiguous f :: NonEmpty (a -> b)
f <*> :: Ambiguous (a -> b) -> Ambiguous a -> Ambiguous b
<*> Ambiguous a :: NonEmpty a
a = NonEmpty b -> Ambiguous b
forall a. NonEmpty a -> Ambiguous a
Ambiguous (NonEmpty (a -> b)
f NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> NonEmpty a
a)

instance Foldable Ambiguous where
   foldMap :: (a -> m) -> Ambiguous a -> m
foldMap f :: a -> m
f (Ambiguous a :: NonEmpty a
a) = (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f NonEmpty a
a

instance Traversable Ambiguous where
   traverse :: (a -> f b) -> Ambiguous a -> f (Ambiguous b)
traverse f :: a -> f b
f (Ambiguous a :: NonEmpty a
a) = NonEmpty b -> Ambiguous b
forall a. NonEmpty a -> Ambiguous a
Ambiguous (NonEmpty b -> Ambiguous b) -> f (NonEmpty b) -> f (Ambiguous b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> NonEmpty a -> f (NonEmpty b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f NonEmpty a
a

instance Semigroup a => Semigroup (Ambiguous a) where
   Ambiguous xs :: NonEmpty a
xs <> :: Ambiguous a -> Ambiguous a -> Ambiguous a
<> Ambiguous ys :: NonEmpty a
ys = NonEmpty a -> Ambiguous a
forall a. NonEmpty a -> Ambiguous a
Ambiguous ((a -> a -> a) -> NonEmpty a -> NonEmpty a -> NonEmpty a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) NonEmpty a
xs NonEmpty a
ys)

instance Monoid a => Monoid (Ambiguous a) where
   mempty :: Ambiguous a
mempty = NonEmpty a -> Ambiguous a
forall a. NonEmpty a -> Ambiguous a
Ambiguous (a
forall a. Monoid a => a
mempty a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [])
   Ambiguous xs :: NonEmpty a
xs mappend :: Ambiguous a -> Ambiguous a -> Ambiguous a
`mappend` Ambiguous ys :: NonEmpty a
ys = NonEmpty a -> Ambiguous a
forall a. NonEmpty a -> Ambiguous a
Ambiguous ((a -> a -> a) -> NonEmpty a -> NonEmpty a -> NonEmpty a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Monoid a => a -> a -> a
mappend NonEmpty a
xs NonEmpty a
ys)

completeParser :: MonoidNull s => Compose (ParseResults s) (Compose [] ((,) s)) r -> Compose (ParseResults s) [] r
completeParser :: Compose (ParseResults s) (Compose [] ((,) s)) r
-> Compose (ParseResults s) [] r
completeParser (Compose (Left failure :: ParseFailure s
failure)) = Either (ParseFailure s) [r] -> Compose (ParseResults s) [] r
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (ParseFailure s -> Either (ParseFailure s) [r]
forall a b. a -> Either a b
Left ParseFailure s
failure)
completeParser (Compose (Right (Compose results :: [(s, r)]
results))) =
   case ((s, r) -> Bool) -> [(s, r)] -> [(s, r)]
forall a. (a -> Bool) -> [a] -> [a]
filter (s -> Bool
forall m. MonoidNull m => m -> Bool
Null.null (s -> Bool) -> ((s, r) -> s) -> (s, r) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s, r) -> s
forall a b. (a, b) -> a
fst) [(s, r)]
results
   of [] -> Either (ParseFailure s) [r] -> Compose (ParseResults s) [] r
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (ParseFailure s -> Either (ParseFailure s) [r]
forall a b. a -> Either a b
Left (ParseFailure s -> Either (ParseFailure s) [r])
-> ParseFailure s -> Either (ParseFailure s) [r]
forall a b. (a -> b) -> a -> b
$ Int -> [Expected s] -> ParseFailure s
forall s. Int -> [Expected s] -> ParseFailure s
ParseFailure 0 [String -> Expected s
forall s. String -> Expected s
Expected "complete parse"])
      completeResults :: [(s, r)]
completeResults -> Either (ParseFailure s) [r] -> Compose (ParseResults s) [] r
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose ([r] -> Either (ParseFailure s) [r]
forall a b. b -> Either a b
Right ([r] -> Either (ParseFailure s) [r])
-> [r] -> Either (ParseFailure s) [r]
forall a b. (a -> b) -> a -> b
$ (s, r) -> r
forall a b. (a, b) -> b
snd ((s, r) -> r) -> [(s, r)] -> [r]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(s, r)]
completeResults)

-- | Choose one of the instances of this class to parse with.
class InputParsing m => MultiParsing m where
   -- | Some parser types produce a single result, others a list of results.
   type ResultFunctor m :: * -> *
   type GrammarConstraint m (g :: (* -> *) -> *) :: Constraint
   type GrammarConstraint m g = Rank2.Functor g
   -- | Given a rank-2 record of parsers and input, produce a record of parses of the complete input.
   parseComplete :: (ParserInput m ~ s, GrammarConstraint m g, Eq s, FactorialMonoid s) =>
                    g m -> s -> g (ResultFunctor m)
   -- | Given a rank-2 record of parsers and input, produce a record of prefix parses paired with the remaining input
   -- suffix.
   parsePrefix :: (ParserInput m ~ s, GrammarConstraint m g, Eq s, FactorialMonoid s) =>
                  g m -> s -> g (Compose (ResultFunctor m) ((,) s))

-- | Parsers that belong to this class can memoize the parse results to avoid exponential performance complexity.
class MultiParsing m => GrammarParsing m where
   -- | The record of grammar productions associated with the parser
   type ParserGrammar m :: (* -> *) -> *
   -- | For internal use by 'notTerminal'
   type GrammarFunctor m :: * -> *
   -- | Converts the intermediate to final parsing result.
   parsingResult :: ParserInput m -> GrammarFunctor m a -> ResultFunctor m (ParserInput m, a)
   -- | Used to reference a grammar production, only necessary from outside the grammar itself
   nonTerminal :: (g ~ ParserGrammar m, GrammarConstraint m g) => (g (GrammarFunctor m) -> GrammarFunctor m a) -> m a
   -- | Construct a grammar whose every production refers to itself.
   selfReferring :: (g ~ ParserGrammar m, GrammarConstraint m g, Rank2.Distributive g) => g m
   -- | Convert a self-referring grammar function to a grammar.
   fixGrammar :: (g ~ ParserGrammar m, GrammarConstraint m g, Rank2.Distributive g) => (g m -> g m) -> g m
   -- | Mark a parser that relies on primitive recursion to prevent an infinite loop in 'fixGrammar'.
   recursive :: m a -> m a

   selfReferring = (forall a. (g (GrammarFunctor m) -> GrammarFunctor m a) -> m a)
-> (g (GrammarFunctor m) -> g (GrammarFunctor m)) -> g m
forall k (g :: (k -> *) -> *) (m :: * -> *) (p :: k -> *)
       (q :: k -> *).
(Distributive g, Functor m) =>
(forall (a :: k). m (p a) -> q a) -> m (g p) -> g q
Rank2.cotraverse forall a. (g (GrammarFunctor m) -> GrammarFunctor m a) -> m a
forall (m :: * -> *) (g :: (* -> *) -> *) a.
(GrammarParsing m, g ~ ParserGrammar m, GrammarConstraint m g) =>
(g (GrammarFunctor m) -> GrammarFunctor m a) -> m a
nonTerminal g (GrammarFunctor m) -> g (GrammarFunctor m)
forall a. a -> a
id
   {-# INLINE selfReferring #-}
   fixGrammar = ((g m -> g m) -> g m -> g m
forall a b. (a -> b) -> a -> b
$ g m
forall (m :: * -> *) (g :: (* -> *) -> *).
(GrammarParsing m, g ~ ParserGrammar m, GrammarConstraint m g,
 Distributive g) =>
g m
selfReferring)
   {-# INLINE fixGrammar #-}
   recursive = m a -> m a
forall a. a -> a
id

class GrammarParsing m => TailsParsing m where
   -- | Parse the tails of the input together with memoized parse results
   parseTails :: GrammarConstraint m g => m r -> [(ParserInput m, g (GrammarFunctor m))] -> GrammarFunctor m r
   parseAllTails :: (GrammarConstraint m g, Rank2.Functor g) =>
                    g m -> [(ParserInput m, g (GrammarFunctor m))] -> [(ParserInput m, g (GrammarFunctor m))]
   parseAllTails _ [] = []
   parseAllTails final :: g m
final parsed :: [(ParserInput m, g (GrammarFunctor m))]
parsed@((s :: ParserInput m
s, _):_) = (ParserInput m
s, g (GrammarFunctor m)
gd)(ParserInput m, g (GrammarFunctor m))
-> [(ParserInput m, g (GrammarFunctor m))]
-> [(ParserInput m, g (GrammarFunctor m))]
forall a. a -> [a] -> [a]
:[(ParserInput m, g (GrammarFunctor m))]
parsed
      where gd :: g (GrammarFunctor m)
gd = (forall a. m a -> GrammarFunctor m a)
-> g m -> g (GrammarFunctor m)
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (m a
-> [(ParserInput m, g (GrammarFunctor m))] -> GrammarFunctor m a
forall (m :: * -> *) (g :: (* -> *) -> *) r.
(TailsParsing m, GrammarConstraint m g) =>
m r
-> [(ParserInput m, g (GrammarFunctor m))] -> GrammarFunctor m r
`parseTails` [(ParserInput m, g (GrammarFunctor m))]
parsed) g m
final

-- | Parsers that can produce alternative parses and collect them into an 'Ambiguous' node
class Alternative m => AmbiguousParsing m where
   -- | Collect all alternative parses of the same length into a 'NonEmpty' list of results.
   ambiguous :: m a -> m (Ambiguous a)

-- | If a grammar is 'Lexical', its parsers can instantiate the 'TokenParsing' class.
class (DeterministicParsing m, InputCharParsing m, TokenParsing m) => LexicalParsing m where
   -- | Always succeeds, consuming all white space and comments
   lexicalWhiteSpace :: m ()
   -- | Consumes all whitespace and comments, failing if there are none
   someLexicalSpace :: m ()
   -- | Consumes a single comment, defaults to 'empty'
   lexicalComment :: m ()
   -- | Consumes a single semicolon and any trailing whitespace, returning the character |';'|. The method can be
   -- overridden for automatic semicolon insertion, but if it succeeds on semicolon or white space input it must
   -- consume it.
   lexicalSemicolon :: m Char
   -- | Applies the argument parser and consumes the trailing 'lexicalWhitespace'
   lexicalToken :: m a -> m a
   -- | Applies the argument parser, determines whether its result is a legal identifier, and consumes the trailing
   -- 'lexicalWhitespace'
   identifierToken :: m (ParserInput m) -> m (ParserInput m)
   -- | Determines whether the given character can start an identifier token, allows only a letter or underscore by
   -- default
   isIdentifierStartChar :: Char -> Bool
   -- | Determines whether the given character can be any part of an identifier token, also allows numbers
   isIdentifierFollowChar :: Char -> Bool
   -- | Parses a valid identifier and consumes the trailing 'lexicalWhitespace'
   identifier :: m (ParserInput m)
   -- | Parses the argument word whole, not followed by any identifier character, and consumes the trailing
   -- 'lexicalWhitespace'
   keyword :: ParserInput m -> m ()

   default identifier :: TextualMonoid (ParserInput m) => m (ParserInput m)
   default keyword :: (Show (ParserInput m), TextualMonoid (ParserInput m)) => ParserInput m -> m ()

   lexicalWhiteSpace = (Char -> Bool) -> m (ParserInput m)
forall (m :: * -> *).
InputCharParsing m =>
(Char -> Bool) -> m (ParserInput m)
takeCharsWhile Char -> Bool
isSpace m (ParserInput m) -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m (ParserInput m) -> m ()
forall (m :: * -> *) a. DeterministicParsing m => m a -> m ()
skipAll (m ()
forall (m :: * -> *). LexicalParsing m => m ()
lexicalComment m () -> m (ParserInput m) -> m (ParserInput m)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> m (ParserInput m)
forall (m :: * -> *).
InputCharParsing m =>
(Char -> Bool) -> m (ParserInput m)
takeCharsWhile Char -> Bool
isSpace)
   someLexicalSpace = (Char -> Bool) -> m (ParserInput m)
forall (m :: * -> *).
InputCharParsing m =>
(Char -> Bool) -> m (ParserInput m)
takeCharsWhile1 Char -> Bool
isSpace m (ParserInput m) -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (m ()
forall (m :: * -> *). LexicalParsing m => m ()
lexicalComment m () -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m ()
forall (m :: * -> *). LexicalParsing m => m ()
lexicalWhiteSpace m () -> m () -> m ()
forall (m :: * -> *) a. DeterministicParsing m => m a -> m a -> m a
<<|> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
                      m () -> m () -> m ()
forall (m :: * -> *) a. DeterministicParsing m => m a -> m a -> m a
<<|> m ()
forall (m :: * -> *). LexicalParsing m => m ()
lexicalComment m () -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m ()
forall (m :: * -> *). LexicalParsing m => m ()
lexicalWhiteSpace
                      m () -> String -> m ()
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> "whitespace"
   lexicalComment = m ()
forall (f :: * -> *) a. Alternative f => f a
empty
   lexicalSemicolon = m Char -> m Char
forall (m :: * -> *) a. LexicalParsing m => m a -> m a
lexicalToken (Char -> m Char
forall (m :: * -> *). CharParsing m => Char -> m Char
Text.Parser.Char.char ';')
   lexicalToken p :: m a
p = m a
p m a -> m () -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* m ()
forall (m :: * -> *). LexicalParsing m => m ()
lexicalWhiteSpace
   isIdentifierStartChar c :: Char
c = Char -> Bool
isLetter Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '_'
   isIdentifierFollowChar c :: Char
c = Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '_'
   identifier = m (ParserInput m) -> m (ParserInput m)
forall (m :: * -> *).
LexicalParsing m =>
m (ParserInput m) -> m (ParserInput m)
identifierToken ((ParserInput m -> ParserInput m -> ParserInput m)
-> m (ParserInput m) -> m (ParserInput m) -> m (ParserInput m)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ParserInput m -> ParserInput m -> ParserInput m
forall a. Monoid a => a -> a -> a
mappend ((Char -> Bool) -> m (ParserInput m)
forall (m :: * -> *).
InputCharParsing m =>
(Char -> Bool) -> m (ParserInput m)
satisfyCharInput (LexicalParsing m => Char -> Bool
forall (m :: * -> *). LexicalParsing m => Char -> Bool
isIdentifierStartChar @m))
                                                ((Char -> Bool) -> m (ParserInput m)
forall (m :: * -> *).
InputCharParsing m =>
(Char -> Bool) -> m (ParserInput m)
takeCharsWhile (LexicalParsing m => Char -> Bool
forall (m :: * -> *). LexicalParsing m => Char -> Bool
isIdentifierFollowChar @m))) m (ParserInput m) -> String -> m (ParserInput m)
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> "an identifier"
   identifierToken = m (ParserInput m) -> m (ParserInput m)
forall (m :: * -> *) a. LexicalParsing m => m a -> m a
lexicalToken
   keyword s :: ParserInput m
s = m () -> m ()
forall (m :: * -> *) a. LexicalParsing m => m a -> m a
lexicalToken (ParserInput m -> m (ParserInput m)
forall (m :: * -> *).
InputParsing m =>
ParserInput m -> m (ParserInput m)
string ParserInput m
s m (ParserInput m) -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> m ()
forall (m :: * -> *). InputCharParsing m => (Char -> Bool) -> m ()
notSatisfyChar (LexicalParsing m => Char -> Bool
forall (m :: * -> *). LexicalParsing m => Char -> Bool
isIdentifierFollowChar @m)) m () -> String -> m ()
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> ("keyword " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ParserInput m -> String
forall a. Show a => a -> String
show ParserInput m
s)