{-# LANGUAGE BangPatterns, CPP, FlexibleContexts, GeneralizedNewtypeDeriving, InstanceSigs,
             RankNTypes, ScopedTypeVariables, TypeFamilies, TypeOperators, UndecidableInstances #-}
-- | A context-free memoizing parser that handles all alternatives in parallel.
module Text.Grampa.ContextFree.SortedMemoizing 
       (ParseFailure(..), ResultList(..), Parser(..),
        longest, peg, terminalPEG)
where

import Control.Applicative
import Control.Monad (Monad(..), MonadPlus(..))
#if MIN_VERSION_base(4,13,0)
import Control.Monad (MonadFail(fail))
#endif
import Data.Either (partitionEithers)
import Data.Functor.Compose (Compose(..))
import Data.List (intercalate)
import Data.List.NonEmpty (NonEmpty((:|)), nonEmpty, toList)
import Data.Monoid (Monoid(mappend, mempty))
import Data.Monoid.Null (MonoidNull(null))
import Data.Monoid.Factorial (FactorialMonoid, splitPrimePrefix)
import Data.Monoid.Textual (TextualMonoid)
import qualified Data.Monoid.Factorial as Factorial
import qualified Data.Monoid.Textual as Textual
import Data.Ord (Down(Down))
import Data.Semigroup (Semigroup((<>)))
import Data.Semigroup.Cancellative (LeftReductive(isPrefixOf))
import Data.String (fromString)
import Debug.Trace (trace)
import Witherable (Filterable(mapMaybe))

import qualified Text.Parser.Char
import Text.Parser.Char (CharParsing)
import Text.Parser.Combinators (Parsing(..))
import Text.Parser.Input.Position (fromEnd)
import Text.Parser.LookAhead (LookAheadParsing(..))

import qualified Rank2

import Text.Grampa.Class (GrammarParsing(..), InputParsing(..), InputCharParsing(..), MultiParsing(..),
                          AmbiguousParsing(..), Ambiguous(Ambiguous), CommittedParsing(..),
                          ConsumedInputParsing(..), DeterministicParsing(..),
                          TailsParsing(parseTails, parseAllTails), ParseResults, ParseFailure(..),
                          FailureDescription(..))
import Text.Grampa.Internal (ResultList(..), ResultsOfLength(..), TraceableParsing(..),
                             emptyFailure, expected, expectedInput, replaceExpected, erroneous)
import qualified Text.Grampa.PEG.Backtrack.Measured as Backtrack

import Prelude hiding (iterate, null, showList, span, takeWhile)

-- | Parser for a context-free grammar with packrat-like sharing of parse results. It does not support left-recursive
-- grammars.
newtype Parser g s r = Parser{forall (g :: (* -> *) -> *) s r.
Parser g s r -> [(s, g (ResultList g s))] -> ResultList g s r
applyParser :: [(s, g (ResultList g s))] -> ResultList g s r}

instance Functor (Parser g i) where
   fmap :: forall a b. (a -> b) -> Parser g i a -> Parser g i b
fmap a -> b
f (Parser [(i, g (ResultList g i))] -> ResultList g i a
p) = ([(i, g (ResultList g i))] -> ResultList g i b) -> Parser g i b
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser ((a -> b) -> ResultList g i a -> ResultList g i b
forall a b. (a -> b) -> ResultList g i a -> ResultList g i b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (ResultList g i a -> ResultList g i b)
-> ([(i, g (ResultList g i))] -> ResultList g i a)
-> [(i, g (ResultList g i))]
-> ResultList g i b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(i, g (ResultList g i))] -> ResultList g i a
p)
   {-# INLINE fmap #-}

instance Ord s => Applicative (Parser g s) where
   pure :: forall a. a -> Parser g s a
pure a
a = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (\[(s, g (ResultList g s))]
rest-> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty a -> ResultsOfLength g s a
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
rest (a
aa -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty)
   Parser [(s, g (ResultList g s))] -> ResultList g s (a -> b)
p <*> :: forall a b. Parser g s (a -> b) -> Parser g s a -> Parser g s b
<*> Parser [(s, g (ResultList g s))] -> ResultList g s a
q = ([(s, g (ResultList g s))] -> ResultList g s b) -> Parser g s b
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s b
r where
      r :: [(s, g (ResultList g s))] -> ResultList g s b
r [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))] -> ResultList g s (a -> b)
p [(s, g (ResultList g s))]
rest
               of ResultList [ResultsOfLength g s (a -> b)]
results ParseFailure Pos s
failure -> [ResultsOfLength g s b] -> ParseFailure Pos s -> ResultList g s b
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s b]
forall a. Monoid a => a
mempty ParseFailure Pos s
failure ResultList g s b -> ResultList g s b -> ResultList g s b
forall a. Semigroup a => a -> a -> a
<> (ResultsOfLength g s (a -> b) -> ResultList g s b)
-> [ResultsOfLength g s (a -> b)] -> ResultList g s b
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ResultsOfLength g s (a -> b) -> ResultList g s b
continue [ResultsOfLength g s (a -> b)]
results
      continue :: ResultsOfLength g s (a -> b) -> ResultList g s b
continue (ResultsOfLength Int
l [(s, g (ResultList g s))]
rest' NonEmpty (a -> b)
fs) = ((a -> b) -> ResultList g s b)
-> NonEmpty (a -> b) -> ResultList g s b
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Int -> ResultList g s a -> (a -> b) -> ResultList g s b
forall {g :: (* -> *) -> *} {s} {a} {r}.
Int -> ResultList g s a -> (a -> r) -> ResultList g s r
continue' Int
l (ResultList g s a -> (a -> b) -> ResultList g s b)
-> ResultList g s a -> (a -> b) -> ResultList g s b
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest') NonEmpty (a -> b)
fs
      continue' :: Int -> ResultList g s a -> (a -> r) -> ResultList g s r
continue' Int
l (ResultList [ResultsOfLength g s a]
rs ParseFailure Pos s
failure) a -> r
f = [ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList (Int -> (a -> r) -> ResultsOfLength g s a -> ResultsOfLength g s r
forall {a} {r} {g :: (* -> *) -> *} {s}.
Int -> (a -> r) -> ResultsOfLength g s a -> ResultsOfLength g s r
adjust Int
l a -> r
f (ResultsOfLength g s a -> ResultsOfLength g s r)
-> [ResultsOfLength g s a] -> [ResultsOfLength g s r]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s a]
rs) ParseFailure Pos s
failure
      adjust :: Int -> (a -> r) -> ResultsOfLength g s a -> ResultsOfLength g s r
adjust Int
l a -> r
f (ResultsOfLength Int
l' [(s, g (ResultList g s))]
rest' NonEmpty a
as) = Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength (Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l') [(s, g (ResultList g s))]
rest' (a -> r
f (a -> r) -> NonEmpty a -> NonEmpty r
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty a
as)
   {-# INLINABLE pure #-}
   {-# INLINABLE (<*>) #-}

instance Ord s => Alternative (Parser g s) where
   empty :: forall a. Parser g s a
empty = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser ([ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty (ParseFailure Pos s -> ResultList g s a)
-> ([(s, g (ResultList g s))] -> ParseFailure Pos s)
-> [(s, g (ResultList g s))]
-> ResultList g s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pos -> ParseFailure Pos s
forall s. Pos -> ParseFailure Pos s
emptyFailure (Pos -> ParseFailure Pos s)
-> ([(s, g (ResultList g s))] -> Pos)
-> [(s, g (ResultList g s))]
-> ParseFailure Pos s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Pos
forall a. a -> Down a
Down (Int -> Pos)
-> ([(s, g (ResultList g s))] -> Int)
-> [(s, g (ResultList g s))]
-> Pos
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length)
   Parser [(s, g (ResultList g s))] -> ResultList g s a
p <|> :: forall a. Parser g s a -> Parser g s a -> Parser g s a
<|> Parser [(s, g (ResultList g s))] -> ResultList g s a
q = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
r where
      r :: [(s, g (ResultList g s))] -> ResultList g s a
r [(s, g (ResultList g s))]
rest = [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest ResultList g s a -> ResultList g s a -> ResultList g s a
forall a. Semigroup a => a -> a -> a
<> [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest
   {-# INLINE (<|>) #-}
   {-# INLINABLE empty #-}

instance Filterable (Parser g s) where
  mapMaybe :: forall a b. (a -> Maybe b) -> Parser g s a -> Parser g s b
mapMaybe a -> Maybe b
f (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s b) -> Parser g s b
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser ((a -> Maybe b) -> ResultList g s a -> ResultList g s b
forall a b. (a -> Maybe b) -> ResultList g s a -> ResultList g s b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe a -> Maybe b
f (ResultList g s a -> ResultList g s b)
-> ([(s, g (ResultList g s))] -> ResultList g s a)
-> [(s, g (ResultList g s))]
-> ResultList g s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(s, g (ResultList g s))] -> ResultList g s a
p)

instance Ord s => Monad (Parser g s) where
   return :: forall a. a -> Parser g s a
return = a -> Parser g s a
forall a. a -> Parser g s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
   >> :: forall a b. Parser g s a -> Parser g s b -> Parser g s b
(>>) = Parser g s a -> Parser g s b -> Parser g s b
forall a b. Parser g s a -> Parser g s b -> Parser g s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
   Parser [(s, g (ResultList g s))] -> ResultList g s a
p >>= :: forall a b. Parser g s a -> (a -> Parser g s b) -> Parser g s b
>>= a -> Parser g s b
f = ([(s, g (ResultList g s))] -> ResultList g s b) -> Parser g s b
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s b
q where
      q :: [(s, g (ResultList g s))] -> ResultList g s b
q [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest
               of ResultList [ResultsOfLength g s a]
results ParseFailure Pos s
failure -> [ResultsOfLength g s b] -> ParseFailure Pos s -> ResultList g s b
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s b]
forall a. Monoid a => a
mempty ParseFailure Pos s
failure ResultList g s b -> ResultList g s b -> ResultList g s b
forall a. Semigroup a => a -> a -> a
<> (ResultsOfLength g s a -> ResultList g s b)
-> [ResultsOfLength g s a] -> ResultList g s b
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ResultsOfLength g s a -> ResultList g s b
continue [ResultsOfLength g s a]
results
      continue :: ResultsOfLength g s a -> ResultList g s b
continue (ResultsOfLength Int
l [(s, g (ResultList g s))]
rest' NonEmpty a
rs) = (a -> ResultList g s b) -> NonEmpty a -> ResultList g s b
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Int -> ResultList g s b -> ResultList g s b
forall {g :: (* -> *) -> *} {s} {r}.
Int -> ResultList g s r -> ResultList g s r
continue' Int
l (ResultList g s b -> ResultList g s b)
-> (a -> ResultList g s b) -> a -> ResultList g s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Parser g s b -> [(s, g (ResultList g s))] -> ResultList g s b)
-> [(s, g (ResultList g s))] -> Parser g s b -> ResultList g s b
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser g s b -> [(s, g (ResultList g s))] -> ResultList g s b
forall (g :: (* -> *) -> *) s r.
Parser g s r -> [(s, g (ResultList g s))] -> ResultList g s r
applyParser [(s, g (ResultList g s))]
rest' (Parser g s b -> ResultList g s b)
-> (a -> Parser g s b) -> a -> ResultList g s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Parser g s b
f) NonEmpty a
rs
      continue' :: Int -> ResultList g s r -> ResultList g s r
continue' Int
l (ResultList [ResultsOfLength g s r]
rs ParseFailure Pos s
failure) = [ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList (Int -> ResultsOfLength g s r -> ResultsOfLength g s r
forall {g :: (* -> *) -> *} {s} {r}.
Int -> ResultsOfLength g s r -> ResultsOfLength g s r
adjust Int
l (ResultsOfLength g s r -> ResultsOfLength g s r)
-> [ResultsOfLength g s r] -> [ResultsOfLength g s r]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s r]
rs) ParseFailure Pos s
failure
      adjust :: Int -> ResultsOfLength g s r -> ResultsOfLength g s r
adjust Int
l (ResultsOfLength Int
l' [(s, g (ResultList g s))]
rest' NonEmpty r
rs) = Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength (Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l') [(s, g (ResultList g s))]
rest' NonEmpty r
rs

#if MIN_VERSION_base(4,13,0)
instance Ord s => MonadFail (Parser g s) where
#endif
   fail :: forall a. String -> Parser g s a
fail String
msg = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
erroneous (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
msg)

instance Ord s => MonadPlus (Parser g s) where
   mzero :: forall a. Parser g s a
mzero = Parser g s a
forall a. Parser g s a
forall (f :: * -> *) a. Alternative f => f a
empty
   mplus :: forall a. Parser g s a -> Parser g s a -> Parser g s a
mplus = Parser g s a -> Parser g s a -> Parser g s a
forall a. Parser g s a -> Parser g s a -> Parser g s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)

instance (Semigroup x, Ord s) => Semigroup (Parser g s x) where
   <> :: Parser g s x -> Parser g s x -> Parser g s x
(<>) = (x -> x -> x) -> Parser g s x -> Parser g s x -> Parser g s x
forall a b c.
(a -> b -> c) -> Parser g s a -> Parser g s b -> Parser g s c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 x -> x -> x
forall a. Semigroup a => a -> a -> a
(<>)

instance (Monoid x, Ord s) => Monoid (Parser g s x) where
   mempty :: Parser g s x
mempty = x -> Parser g s x
forall a. a -> Parser g s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure x
forall a. Monoid a => a
mempty
   mappend :: Parser g s x -> Parser g s x -> Parser g s x
mappend = Parser g s x -> Parser g s x -> Parser g s x
forall a. Semigroup a => a -> a -> a
(<>)

-- | Memoizing parser guarantees O(n²) performance for grammars with unambiguous productions. Can be wrapped with
-- 'Text.Grampa.ContextFree.SortedMemoizing.LeftRecursive.Fixed' to provide left recursion support.
instance (Ord s, LeftReductive s, FactorialMonoid s) => GrammarParsing (Parser g s) where
   type ParserGrammar (Parser g s) = g
   type GrammarFunctor (Parser g s) = ResultList g s
   parsingResult :: forall a.
ParserInput (Parser g s)
-> GrammarFunctor (Parser g s) a
-> ResultFunctor (Parser g s) (ParserInput (Parser g s), a)
parsingResult ParserInput (Parser g s)
_ = Either (ParseFailure Pos s) [(s, a)]
-> Compose (Either (ParseFailure Pos s)) [] (s, a)
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Either (ParseFailure Pos s) [(s, a)]
 -> Compose (Either (ParseFailure Pos s)) [] (s, a))
-> (ResultList g s a -> Either (ParseFailure Pos s) [(s, a)])
-> ResultList g s a
-> Compose (Either (ParseFailure Pos s)) [] (s, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResultList g s a -> Either (ParseFailure Pos s) [(s, a)]
forall s (g :: (* -> *) -> *) r.
FactorialMonoid s =>
ResultList g s r -> ParseResults s [(s, r)]
fromResultList
   nonTerminal :: (Rank2.Functor g, ParserInput (Parser g s) ~ s) => (g (ResultList g s) -> ResultList g s a) -> Parser g s a
   nonTerminal :: forall a.
(Functor g, ParserInput (Parser g s) ~ s) =>
(g (ResultList g s) -> ResultList g s a) -> Parser g s a
nonTerminal g (ResultList g s) -> ResultList g s a
f = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
p where
      p :: [(s, g (ResultList g s))] -> ResultList g s a
p input :: [(s, g (ResultList g s))]
input@((s
_, g (ResultList g s)
d) : [(s, g (ResultList g s))]
_) = [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
rs' ParseFailure Pos s
failure
         where ResultList [ResultsOfLength g s a]
rs ParseFailure Pos s
failure = g (ResultList g s) -> ResultList g s a
f g (ResultList g s)
d
               rs' :: [ResultsOfLength g s a]
rs' = ResultsOfLength g s a -> ResultsOfLength g s a
sync (ResultsOfLength g s a -> ResultsOfLength g s a)
-> [ResultsOfLength g s a] -> [ResultsOfLength g s a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s a]
rs
               -- in left-recursive grammars the stored input remainder may be wrong, so revert to the current input
               sync :: ResultsOfLength g s a -> ResultsOfLength g s a
sync (ResultsOfLength Int
0 [(s, g (ResultList g s))]
_remainder NonEmpty a
r) = Int
-> [(s, g (ResultList g s))] -> NonEmpty a -> ResultsOfLength g s a
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
input NonEmpty a
r
               sync ResultsOfLength g s a
rol = ResultsOfLength g s a
rol
      p [(s, g (ResultList g s))]
_ = [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected Pos
0 String
"NonTerminal at endOfInput")
   {-# INLINE nonTerminal #-}
   chainRecursive :: forall (g :: (* -> *) -> *) (f :: * -> *) a.
(g ~ ParserGrammar (Parser g s), f ~ GrammarFunctor (Parser g s),
 GrammarConstraint (Parser g s) g) =>
(f a -> g f -> g f) -> Parser g s a -> Parser g s a -> Parser g s a
chainRecursive f a -> g f -> g f
assign (Parser [(s, g (ResultList g s))] -> ResultList g s a
base) (Parser [(s, g (ResultList g s))] -> ResultList g s a
recurse) = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
[(s, g f)] -> ResultList g s a
q
      where q :: [(s, g f)] -> ResultList g s a
q [] = [(s, g (ResultList g s))] -> ResultList g s a
base []
            q ((s
s, g f
d):[(s, g f)]
t) = case [(s, g (ResultList g s))] -> ResultList g s a
base ((s
s, f a -> g f -> g f
assign f a
forall a. Monoid a => a
mempty g f
d) (s, g (ResultList g s))
-> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. a -> [a] -> [a]
: [(s, g (ResultList g s))]
[(s, g f)]
t)
                           of r :: ResultList g s a
r@(ResultList [] ParseFailure Pos s
_) -> ResultList g s a
r
                              ResultList g s a
r -> ResultList g s a -> ResultList g s a -> ResultList g s a
iter ResultList g s a
r ResultList g s a
r
               where iter :: ResultList g s a -> ResultList g s a -> ResultList g s a
iter f a
marginal ResultList g s a
total = case [(s, g (ResultList g s))] -> ResultList g s a
recurse ((s
s, f a -> g f -> g f
assign f a
marginal g f
d) (s, g (ResultList g s))
-> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. a -> [a] -> [a]
: [(s, g (ResultList g s))]
[(s, g f)]
t)
                                           of ResultList [] ParseFailure Pos s
_ -> ResultList g s a
total
                                              ResultList g s a
r -> ResultList g s a -> ResultList g s a -> ResultList g s a
iter ResultList g s a
r (ResultList g s a
total ResultList g s a -> ResultList g s a -> ResultList g s a
forall a. Semigroup a => a -> a -> a
<> ResultList g s a
r)
   chainLongestRecursive :: forall (g :: (* -> *) -> *) (f :: * -> *) a.
(g ~ ParserGrammar (Parser g s), f ~ GrammarFunctor (Parser g s),
 GrammarConstraint (Parser g s) g) =>
(f a -> g f -> g f) -> Parser g s a -> Parser g s a -> Parser g s a
chainLongestRecursive f a -> g f -> g f
assign (Parser [(s, g (ResultList g s))] -> ResultList g s a
base) (Parser [(s, g (ResultList g s))] -> ResultList g s a
recurse) = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
[(s, g f)] -> ResultList g s a
q
      where q :: [(s, g f)] -> ResultList g s a
q [] = [(s, g (ResultList g s))] -> ResultList g s a
base []
            q ((s
s, g f
d):[(s, g f)]
t) = case [(s, g (ResultList g s))] -> ResultList g s a
base ((s
s, f a -> g f -> g f
assign f a
forall a. Monoid a => a
mempty g f
d) (s, g (ResultList g s))
-> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. a -> [a] -> [a]
: [(s, g (ResultList g s))]
[(s, g f)]
t)
                           of r :: ResultList g s a
r@(ResultList [] ParseFailure Pos s
_) -> ResultList g s a
r
                              ResultList g s a
r -> ResultList g s a -> f a
iter ResultList g s a
r
               where iter :: ResultList g s a -> f a
iter f a
r = case [(s, g (ResultList g s))] -> ResultList g s a
recurse ((s
s, f a -> g f -> g f
assign f a
r g f
d) (s, g (ResultList g s))
-> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. a -> [a] -> [a]
: [(s, g (ResultList g s))]
[(s, g f)]
t)
                              of ResultList [] ParseFailure Pos s
_ -> f a
r
                                 ResultList g s a
r' -> ResultList g s a -> f a
iter ResultList g s a
r'

instance (Ord s, LeftReductive s, FactorialMonoid s) => TailsParsing (Parser g s) where
   parseTails :: forall (g :: (* -> *) -> *) r.
GrammarConstraint (Parser g s) g =>
Parser g s r
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> GrammarFunctor (Parser g s) r
parseTails = Parser g s r -> [(s, g (ResultList g s))] -> ResultList g s r
Parser g s r
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> GrammarFunctor (Parser g s) r
forall (g :: (* -> *) -> *) s r.
Parser g s r -> [(s, g (ResultList g s))] -> ResultList g s r
applyParser

-- | Memoizing parser guarantees O(n²) performance for grammars with unambiguous productions. Can be wrapped with
-- 'Text.Grampa.ContextFree.SortedMemoizing.LeftRecursive.Fixed' to provide left recursion support.
--
-- @
-- 'parseComplete' :: ("Rank2".'Rank2.Functor' g, 'FactorialMonoid' s) =>
--                  g (Memoizing.'Parser' g s) -> s -> g ('Compose' ('ParseResults' s) [])
-- @
instance (LeftReductive s, FactorialMonoid s, Ord s) => MultiParsing (Parser g s) where
   type GrammarConstraint (Parser g s) g' = (g ~ g', Rank2.Functor g)
   type ResultFunctor (Parser g s) = Compose (ParseResults s) []
   -- | Returns the list of all possible input prefix parses paired with the remaining input suffix.
   parsePrefix :: forall s (g :: (* -> *) -> *).
(ParserInput (Parser g s) ~ s, GrammarConstraint (Parser g s) g,
 Eq s, FactorialMonoid s) =>
g (Parser g s)
-> s -> g (Compose (ResultFunctor (Parser g s)) ((,) s))
parsePrefix g (Parser g s)
g s
input = (forall a.
 ResultList g s a
 -> Compose (Compose (ParseResults s) []) ((,) s) a)
-> g (ResultList g s)
-> g (Compose (Compose (ParseResults s) []) ((,) s))
forall {k} (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (Compose (ParseResults s) [] (s, a)
-> Compose (Compose (ParseResults s) []) ((,) s) a
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Compose (ParseResults s) [] (s, a)
 -> Compose (Compose (ParseResults s) []) ((,) s) a)
-> (ResultList g s a -> Compose (ParseResults s) [] (s, a))
-> ResultList g s a
-> Compose (Compose (ParseResults s) []) ((,) s) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either (ParseFailure Pos s) [(s, a)]
-> Compose (ParseResults s) [] (s, a)
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Either (ParseFailure Pos s) [(s, a)]
 -> Compose (ParseResults s) [] (s, a))
-> (ResultList g s a -> Either (ParseFailure Pos s) [(s, a)])
-> ResultList g s a
-> Compose (ParseResults s) [] (s, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResultList g s a -> Either (ParseFailure Pos s) [(s, a)]
ResultList g s a -> Either (ParseFailure Pos s) [(s, a)]
forall s (g :: (* -> *) -> *) r.
FactorialMonoid s =>
ResultList g s r -> ParseResults s [(s, r)]
fromResultList) ((s, g (ResultList g s)) -> g (ResultList g s)
forall a b. (a, b) -> b
snd ((s, g (ResultList g s)) -> g (ResultList g s))
-> (s, g (ResultList g s)) -> g (ResultList g s)
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> (s, g (ResultList g s))
forall a. HasCallStack => [a] -> a
head ([(s, g (ResultList g s))] -> (s, g (ResultList g s)))
-> [(s, g (ResultList g s))] -> (s, g (ResultList g s))
forall a b. (a -> b) -> a -> b
$ g (Parser g s) -> s -> [(s, g (ResultList g s))]
forall (g :: (* -> *) -> *) s.
(Functor g, FactorialMonoid s) =>
g (Parser g s) -> s -> [(s, g (ResultList g s))]
parseGrammarTails g (Parser g s)
g (Parser g s)
g s
s
input)
   -- parseComplete :: (ParserInput (Parser g s) ~ s, Rank2.Functor g, Eq s, FactorialMonoid s) =>
   --                  g (Parser g s) -> s -> g (Compose (ParseResults s) [])
   parseComplete :: forall s (g :: (* -> *) -> *).
(ParserInput (Parser g s) ~ s, GrammarConstraint (Parser g s) g,
 Eq s, FactorialMonoid s) =>
g (Parser g s) -> s -> g (ResultFunctor (Parser g s))
parseComplete g (Parser g s)
g s
input = (forall a.
 GrammarFunctor (Parser g s) a -> Compose (ParseResults s) [] a)
-> g (GrammarFunctor (Parser g s))
-> g (Compose (ParseResults s) [])
forall {k} (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (((s, a) -> a
forall a b. (a, b) -> b
snd ((s, a) -> a)
-> Compose (ParseResults s) [] (s, a)
-> Compose (ParseResults s) [] a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Compose (ParseResults s) [] (s, a)
 -> Compose (ParseResults s) [] a)
-> (GrammarFunctor (Parser g s) a
    -> Compose (ParseResults s) [] (s, a))
-> GrammarFunctor (Parser g s) a
-> Compose (ParseResults s) [] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either (ParseFailure Pos s) [(s, a)]
-> Compose (ParseResults s) [] (s, a)
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Either (ParseFailure Pos s) [(s, a)]
 -> Compose (ParseResults s) [] (s, a))
-> (GrammarFunctor (Parser g s) a
    -> Either (ParseFailure Pos s) [(s, a)])
-> GrammarFunctor (Parser g s) a
-> Compose (ParseResults s) [] (s, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GrammarFunctor (Parser g s) a
-> Either (ParseFailure Pos s) [(s, a)]
ResultList g s a -> Either (ParseFailure Pos s) [(s, a)]
forall s (g :: (* -> *) -> *) r.
FactorialMonoid s =>
ResultList g s r -> ParseResults s [(s, r)]
fromResultList)
                              ((ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))
-> g (GrammarFunctor (Parser g s))
forall a b. (a, b) -> b
snd ((ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))
 -> g (GrammarFunctor (Parser g s)))
-> (ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))
-> g (GrammarFunctor (Parser g s))
forall a b. (a -> b) -> a -> b
$ [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> (ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))
forall a. HasCallStack => [a] -> a
head ([(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
 -> (ParserInput (Parser g s), g (GrammarFunctor (Parser g s))))
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> (ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))
forall a b. (a -> b) -> a -> b
$ g (Parser g s)
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
forall (m :: * -> *) (g :: (* -> *) -> *).
(TailsParsing m, GrammarConstraint m g, Functor g) =>
g m
-> [(ParserInput m, g (GrammarFunctor m))]
-> [(ParserInput m, g (GrammarFunctor m))]
forall (g :: (* -> *) -> *).
(GrammarConstraint (Parser g s) g, Functor g) =>
g (Parser g s)
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
parseAllTails g (Parser g s)
close ([(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
 -> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))])
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
-> [(ParserInput (Parser g s), g (GrammarFunctor (Parser g s)))]
forall a b. (a -> b) -> a -> b
$ g (Parser g s) -> s -> [(s, g (ResultList g s))]
forall (g :: (* -> *) -> *) s.
(Functor g, FactorialMonoid s) =>
g (Parser g s) -> s -> [(s, g (ResultList g s))]
parseGrammarTails g (Parser g s)
g (Parser g s)
g s
s
input)
      where close :: g (Parser g s)
close = (forall a. Parser g s a -> Parser g s a)
-> g (Parser g s) -> g (Parser g s)
forall {k} (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (Parser g s a -> Parser g s () -> Parser g s a
forall a b. Parser g s a -> Parser g s b -> Parser g s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser g s ()
forall (m :: * -> *). Parsing m => m ()
eof) g (Parser g s)
g

fromResultList :: FactorialMonoid s => ResultList g s r -> ParseResults s [(s, r)]
fromResultList :: forall s (g :: (* -> *) -> *) r.
FactorialMonoid s =>
ResultList g s r -> ParseResults s [(s, r)]
fromResultList (ResultList [] (ParseFailure Pos
pos FailureDescription s
expected' [String]
erroneous')) =
   ParseFailure Pos s -> Either (ParseFailure Pos s) [(s, r)]
forall a b. a -> Either a b
Left (Pos -> FailureDescription s -> [String] -> ParseFailure Pos s
forall pos s.
pos -> FailureDescription s -> [String] -> ParseFailure pos s
ParseFailure (Pos
pos Pos -> Pos -> Pos
forall a. Num a => a -> a -> a
- Pos
1) FailureDescription s
expected' [String]
erroneous')
fromResultList (ResultList [ResultsOfLength g s r]
rl ParseFailure Pos s
_failure) = [(s, r)] -> Either (ParseFailure Pos s) [(s, r)]
forall a b. b -> Either a b
Right ((ResultsOfLength g s r -> [(s, r)])
-> [ResultsOfLength g s r] -> [(s, r)]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ResultsOfLength g s r -> [(s, r)]
forall {a} {g :: (* -> *) -> *} {a}.
Monoid a =>
ResultsOfLength g a a -> [(a, a)]
f [ResultsOfLength g s r]
rl)
   where f :: ResultsOfLength g a a -> [(a, a)]
f (ResultsOfLength Int
_ ((a
s, g (ResultList g a)
_):[(a, g (ResultList g a))]
_) NonEmpty a
r) = (,) a
s (a -> (a, a)) -> [a] -> [(a, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList NonEmpty a
r
         f (ResultsOfLength Int
_ [] NonEmpty a
r) = (,) a
forall a. Monoid a => a
mempty (a -> (a, a)) -> [a] -> [(a, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList NonEmpty a
r

parseGrammarTails :: (Rank2.Functor g, FactorialMonoid s) => g (Parser g s) -> s -> [(s, g (ResultList g s))]
parseGrammarTails :: forall (g :: (* -> *) -> *) s.
(Functor g, FactorialMonoid s) =>
g (Parser g s) -> s -> [(s, g (ResultList g s))]
parseGrammarTails g (Parser g s)
g s
input = (s -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))])
-> [(s, g (ResultList g s))] -> [s] -> [(s, g (ResultList g s))]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr s -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
parseTail [] (s -> [s]
forall m. FactorialMonoid m => m -> [m]
Factorial.tails s
input)
   where parseTail :: s -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
parseTail s
s [(s, g (ResultList g s))]
parsedTail = [(s, g (ResultList g s))]
parsed
            where parsed :: [(s, g (ResultList g s))]
parsed = (s
s,g (ResultList g s)
d)(s, g (ResultList g s))
-> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. a -> [a] -> [a]
:[(s, g (ResultList g s))]
parsedTail
                  d :: g (ResultList g s)
d      = (forall a. Parser g s a -> ResultList g s a)
-> g (Parser g s) -> g (ResultList g s)
forall {k} (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap ((([(s, g (ResultList g s))] -> ResultList g s a)
-> [(s, g (ResultList g s))] -> ResultList g s a
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))]
parsed) (([(s, g (ResultList g s))] -> ResultList g s a)
 -> ResultList g s a)
-> (Parser g s a -> [(s, g (ResultList g s))] -> ResultList g s a)
-> Parser g s a
-> ResultList g s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser g s a -> [(s, g (ResultList g s))] -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
Parser g s r -> [(s, g (ResultList g s))] -> ResultList g s r
applyParser) g (Parser g s)
g

instance (LeftReductive s, FactorialMonoid s, Ord s) => InputParsing (Parser g s) where
   type ParserInput (Parser g s) = s
   getInput :: Parser g s (ParserInput (Parser g s))
getInput = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
forall {r} {g :: (* -> *) -> *}.
(Ord r, Monoid r) =>
[(r, g (ResultList g r))] -> ResultList g r r
p
      where p :: [(r, g (ResultList g r))] -> ResultList g r r
p rest :: [(r, g (ResultList g r))]
rest@((r
s, g (ResultList g r)
_):[(r, g (ResultList g r))]
_) = [ResultsOfLength g r r] -> ParseFailure Pos r -> ResultList g r r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(r, g (ResultList g r))] -> NonEmpty r -> ResultsOfLength g r r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(r, g (ResultList g r))]
rest (r
sr -> [r] -> NonEmpty r
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos r
forall a. Monoid a => a
mempty
            p [] = [ResultsOfLength g r r] -> ParseFailure Pos r -> ResultList g r r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(r, g (ResultList g r))] -> NonEmpty r -> ResultsOfLength g r r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [] (r
forall a. Monoid a => a
memptyr -> [r] -> NonEmpty r
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos r
forall a. Monoid a => a
mempty
   anyToken :: Parser g s (ParserInput (Parser g s))
anyToken = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
forall {r} {g :: (* -> *) -> *}.
(FactorialMonoid r, Ord r) =>
[(r, g (ResultList g r))] -> ResultList g r r
p
      where p :: [(r, g (ResultList g r))] -> ResultList g r r
p rest :: [(r, g (ResultList g r))]
rest@((r
s, g (ResultList g r)
_):[(r, g (ResultList g r))]
t) = case r -> Maybe (r, r)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix r
s
                                of Just (r
first, r
_) -> [ResultsOfLength g r r] -> ParseFailure Pos r -> ResultList g r r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(r, g (ResultList g r))] -> NonEmpty r -> ResultsOfLength g r r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
1 [(r, g (ResultList g r))]
t (r
firstr -> [r] -> NonEmpty r
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos r
forall a. Monoid a => a
mempty
                                   Maybe (r, r)
_ -> [ResultsOfLength g r r] -> ParseFailure Pos r -> ResultList g r r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g r r]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos r
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(r, g (ResultList g r))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(r, g (ResultList g r))]
rest) String
"anyToken")
            p [] = [ResultsOfLength g r r] -> ParseFailure Pos r -> ResultList g r r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g r r]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos r
forall s. Pos -> String -> ParseFailure Pos s
expected Pos
0 String
"anyToken")
   satisfy :: (ParserInput (Parser g s) -> Bool)
-> Parser g s (ParserInput (Parser g s))
satisfy ParserInput (Parser g s) -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
t) =
               case s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix s
s
               of Just (s
first, s
_) | ParserInput (Parser g s) -> Bool
predicate s
ParserInput (Parser g s)
first -> [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
1 [(s, g (ResultList g s))]
t (s
firsts -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                  Maybe (s, s)
_ -> [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"satisfy")
            p [] = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected Pos
0 String
"satisfy")
   scan :: forall state.
state
-> (state -> ParserInput (Parser g s) -> Maybe state)
-> Parser g s (ParserInput (Parser g s))
scan state
s0 state -> ParserInput (Parser g s) -> Maybe state
f = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (state -> [(s, g (ResultList g s))] -> ResultList g s s
p state
s0)
      where p :: state -> [(s, g (ResultList g s))] -> ResultList g s s
p state
s rest :: [(s, g (ResultList g s))]
rest@((s
i, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_) = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
prefixs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
               where (s
prefix, s
_, state
_) = state -> (state -> s -> Maybe state) -> s -> (s, s, state)
forall s. s -> (s -> s -> Maybe s) -> s -> (s, s, s)
forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe' state
s state -> s -> Maybe state
state -> ParserInput (Parser g s) -> Maybe state
f s
i
                     l :: Int
l = s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
prefix
            p state
_ [] = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [] (s
forall a. Monoid a => a
memptys -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
   take :: Int -> Parser g s (ParserInput (Parser g s))
take Int
0 = Parser g s s
Parser g s (ParserInput (Parser g s))
forall a. Monoid a => a
mempty
   take Int
n = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_)
               | s
x <- Int -> s -> s
forall m. FactorialMonoid m => Int -> m -> m
Factorial.take Int
n s
s, Int
l <- s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
x, Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n =
                    [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
xs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
            p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) (String -> ParseFailure Pos s) -> String -> ParseFailure Pos s
forall a b. (a -> b) -> a -> b
$ String
"take " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n)
   takeWhile :: (ParserInput (Parser g s) -> Bool)
-> Parser g s (ParserInput (Parser g s))
takeWhile ParserInput (Parser g s) -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_)
               | s
x <- (s -> Bool) -> s -> s
forall m. FactorialMonoid m => (m -> Bool) -> m -> m
Factorial.takeWhile s -> Bool
ParserInput (Parser g s) -> Bool
predicate s
s, Int
l <- s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
x =
                    [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
xs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
            p [] = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [] (s
forall a. Monoid a => a
memptys -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
   takeWhile1 :: (ParserInput (Parser g s) -> Bool)
-> Parser g s (ParserInput (Parser g s))
takeWhile1 ParserInput (Parser g s) -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_)
               | s
x <- (s -> Bool) -> s -> s
forall m. FactorialMonoid m => (m -> Bool) -> m -> m
Factorial.takeWhile s -> Bool
ParserInput (Parser g s) -> Bool
predicate s
s, Int
l <- s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
x, Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 =
                    [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
xs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
            p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"takeWhile1")
   string :: ParserInput (Parser g s) -> Parser g s (ParserInput (Parser g s))
string ParserInput (Parser g s)
s = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p where
      p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s', g (ResultList g s)
_) : [(s, g (ResultList g s))]
_)
         | s
ParserInput (Parser g s)
s s -> s -> Bool
forall m. LeftReductive m => m -> m -> Bool
`isPrefixOf` s
s' = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall m. FactorialMonoid m => Int -> m -> m
Factorial.drop Int
l [(s, g (ResultList g s))]
rest) (s
ParserInput (Parser g s)
ss -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
      p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> s -> ParseFailure Pos s
forall s. Pos -> s -> ParseFailure Pos s
expectedInput (Int -> Pos
fromEnd (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) s
ParserInput (Parser g s)
s)
      l :: Int
l = s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
ParserInput (Parser g s)
s
   notSatisfy :: (ParserInput (Parser g s) -> Bool) -> Parser g s ()
notSatisfy ParserInput (Parser g s) -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s ()) -> Parser g s ()
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s ()
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s ()
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
_)
               | Just (s
first, s
_) <- s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix s
s, 
                 ParserInput (Parser g s) -> Bool
predicate s
ParserInput (Parser g s)
first = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s ()]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"notSatisfy")
            p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty ()
-> ResultsOfLength g s ()
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
rest (()() -> [()] -> NonEmpty ()
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
   {-# INLINABLE string #-}

instance (InputParsing (Parser g s), FactorialMonoid s) => TraceableParsing (Parser g s) where
   traceInput :: forall a.
(ParserInput (Parser g s) -> String)
-> Parser g s a -> Parser g s a
traceInput ParserInput (Parser g s) -> String
description (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
q
      where q :: [(s, g (ResultList g s))] -> ResultList g s a
q rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
_) = case String -> ResultList g s a -> ResultList g s a
forall a. String -> a -> a
trace (String
"Parsing " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ParserInput (Parser g s) -> String
description s
ParserInput (Parser g s)
s) ([(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest) of
               rl :: ResultList g s a
rl@(ResultList [] ParseFailure Pos s
_) -> String -> ResultList g s a -> ResultList g s a
forall a. String -> a -> a
trace (String
"Failed " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> (s -> s) -> String
descriptionWith s -> s
forall a. a -> a
id) ResultList g s a
rl
               rl :: ResultList g s a
rl@(ResultList [ResultsOfLength g s a]
rs ParseFailure Pos s
_) -> String -> ResultList g s a -> ResultList g s a
forall a. String -> a -> a
trace (String
"Parsed [" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " (ResultsOfLength g s a -> String
describeResult (ResultsOfLength g s a -> String)
-> [ResultsOfLength g s a] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s a]
rs) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"]") ResultList g s a
rl
               where describeResult :: ResultsOfLength g s a -> String
describeResult (ResultsOfLength Int
len [(s, g (ResultList g s))]
_ NonEmpty a
_) = (s -> s) -> String
descriptionWith (Int -> s -> s
forall m. FactorialMonoid m => Int -> m -> m
Factorial.take Int
len)
                     descriptionWith :: (s -> s) -> String
descriptionWith s -> s
f = ParserInput (Parser g s) -> String
description (s -> s
f s
s)
            q [] = [(s, g (ResultList g s))] -> ResultList g s a
p []

instance (Ord s, Show s, TextualMonoid s) => InputCharParsing (Parser g s) where
   satisfyCharInput :: (Char -> Bool) -> Parser g s (ParserInput (Parser g s))
satisfyCharInput Char -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
t) =
               case s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
s
               of Just Char
first | Char -> Bool
predicate Char
first -> [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
1 [(s, g (ResultList g s))]
t (s -> s
forall m. Factorial m => m -> m
Factorial.primePrefix s
ss -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                  Maybe Char
_ -> [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"satisfyCharInput")
            p [] = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected Pos
0 String
"satisfyCharInput")
   scanChars :: forall state.
state
-> (state -> Char -> Maybe state)
-> Parser g s (ParserInput (Parser g s))
scanChars state
s0 state -> Char -> Maybe state
f = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (state -> [(s, g (ResultList g s))] -> ResultList g s s
p state
s0)
      where p :: state -> [(s, g (ResultList g s))] -> ResultList g s s
p state
s rest :: [(s, g (ResultList g s))]
rest@((s
i, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_) = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
prefixs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
               where (s
prefix, s
_, state
_) = state -> (state -> Char -> Maybe state) -> s -> (s, s, state)
forall s. s -> (s -> Char -> Maybe s) -> s -> (s, s, s)
forall t s.
TextualMonoid t =>
s -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe_' state
s state -> Char -> Maybe state
f s
i
                     l :: Int
l = s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
prefix
            p state
_ [] = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [] (s
forall a. Monoid a => a
memptys -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
   takeCharsWhile :: (Char -> Bool) -> Parser g s (ParserInput (Parser g s))
takeCharsWhile Char -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_)
               | s
x <- Bool -> (Char -> Bool) -> s -> s
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> t
Textual.takeWhile_ Bool
False Char -> Bool
predicate s
s, Int
l <- s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
x =
                    [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
xs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
            p [] = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [] (s
forall a. Monoid a => a
memptys -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
   takeCharsWhile1 :: (Char -> Bool) -> Parser g s (ParserInput (Parser g s))
takeCharsWhile1 Char -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s s) -> Parser g s s
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s s
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s s
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_)
               | s
x <- Bool -> (Char -> Bool) -> s -> s
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> t
Textual.takeWhile_ Bool
False Char -> Bool
predicate s
s, Int
l <- s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
x, Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 =
                    [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty s -> ResultsOfLength g s s
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (s
xs -> [s] -> NonEmpty s
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
            p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s s] -> ParseFailure Pos s -> ResultList g s s
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s s]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"takeCharsWhile1")
   notSatisfyChar :: (Char -> Bool) -> Parser g s ()
notSatisfyChar Char -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s ()) -> Parser g s ()
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s ()
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s ()
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
_)
               | Just Char
first <- s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
s, 
                 Char -> Bool
predicate Char
first = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s ()]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"notSatisfyChar")
            p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty ()
-> ResultsOfLength g s ()
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
rest (()() -> [()] -> NonEmpty ()
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty

instance (LeftReductive s, FactorialMonoid s, Ord s) => ConsumedInputParsing (Parser g s) where
   match :: forall a. Parser g s a -> Parser g s (ParserInput (Parser g s), a)
match (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s (s, a))
-> Parser g s (s, a)
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s (s, a)
q
      where q :: [(s, g (ResultList g s))] -> ResultList g s (s, a)
q [] = s -> ResultList g s a -> ResultList g s (s, a)
forall {a} {g :: (* -> *) -> *} {s} {a}.
FactorialMonoid a =>
a -> ResultList g s a -> ResultList g s (a, a)
addConsumed s
forall a. Monoid a => a
mempty ([(s, g (ResultList g s))] -> ResultList g s a
p [])
            q rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_) : [(s, g (ResultList g s))]
_) = s -> ResultList g s a -> ResultList g s (s, a)
forall {a} {g :: (* -> *) -> *} {s} {a}.
FactorialMonoid a =>
a -> ResultList g s a -> ResultList g s (a, a)
addConsumed s
s ([(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest)
            addConsumed :: a -> ResultList g s a -> ResultList g s (a, a)
addConsumed a
input (ResultList [ResultsOfLength g s a]
rl ParseFailure Pos s
failure) = [ResultsOfLength g s (a, a)]
-> ParseFailure Pos s -> ResultList g s (a, a)
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList (ResultsOfLength g s a -> ResultsOfLength g s (a, a)
add1 (ResultsOfLength g s a -> ResultsOfLength g s (a, a))
-> [ResultsOfLength g s a] -> [ResultsOfLength g s (a, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s a]
rl) ParseFailure Pos s
failure
               where add1 :: ResultsOfLength g s a -> ResultsOfLength g s (a, a)
add1 (ResultsOfLength Int
l [(s, g (ResultList g s))]
t NonEmpty a
rs) = Int
-> [(s, g (ResultList g s))]
-> NonEmpty (a, a)
-> ResultsOfLength g s (a, a)
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l [(s, g (ResultList g s))]
t ((,) (Int -> a -> a
forall m. FactorialMonoid m => Int -> m -> m
Factorial.take Int
l a
input) (a -> (a, a)) -> NonEmpty a -> NonEmpty (a, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty a
rs)

instance (MonoidNull s, Ord s) => Parsing (Parser g s) where
   try :: forall a. Parser g s a -> Parser g s a
try (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
q
      where q :: [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest = ResultList g s a -> ResultList g s a
rewindFailure ([(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest)
               where rewindFailure :: ResultList g s a -> ResultList g s a
rewindFailure (ResultList [ResultsOfLength g s a]
rl ParseFailure Pos s
_) = [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
rl (Pos -> ParseFailure Pos s
forall s. Pos -> ParseFailure Pos s
emptyFailure (Pos -> ParseFailure Pos s) -> Pos -> ParseFailure Pos s
forall a b. (a -> b) -> a -> b
$ Int -> Pos
fromEnd (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest)
   Parser [(s, g (ResultList g s))] -> ResultList g s a
p <?> :: forall a. Parser g s a -> String -> Parser g s a
<?> String
msg  = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
q
      where q :: [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest = ResultList g s a -> ResultList g s a
replaceFailure ([(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest)
               where replaceFailure :: ResultList g s a -> ResultList g s a
replaceFailure (ResultList [] ParseFailure Pos s
f) = [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [] (Pos -> String -> ParseFailure Pos s -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s -> ParseFailure Pos s
replaceExpected (Int -> Pos
fromEnd (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
msg ParseFailure Pos s
f)
                     replaceFailure ResultList g s a
rl = ResultList g s a
rl
   notFollowedBy :: forall a. Show a => Parser g s a -> Parser g s ()
notFollowedBy (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s ()) -> Parser g s ()
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (\[(s, g (ResultList g s))]
input-> [(s, g (ResultList g s))] -> ResultList g s a -> ResultList g s ()
forall {s} {g :: (* -> *) -> *} {g :: (* -> *) -> *} {s} {r}.
Ord s =>
[(s, g (ResultList g s))] -> ResultList g s r -> ResultList g s ()
rewind [(s, g (ResultList g s))]
input ([(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
input))
      where rewind :: [(s, g (ResultList g s))] -> ResultList g s r -> ResultList g s ()
rewind [(s, g (ResultList g s))]
t (ResultList [] ParseFailure Pos s
_) = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty ()
-> ResultsOfLength g s ()
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
t (()() -> [()] -> NonEmpty ()
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
            rewind [(s, g (ResultList g s))]
t ResultList{} = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s ()]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
t) String
"notFollowedBy")
   skipMany :: forall a. Parser g s a -> Parser g s ()
skipMany Parser g s a
p = Parser g s ()
go
      where go :: Parser g s ()
go = () -> Parser g s ()
forall a. a -> Parser g s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure () Parser g s () -> Parser g s () -> Parser g s ()
forall a. Parser g s a -> Parser g s a -> Parser g s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser g s a -> Parser g s a
forall a. Parser g s a -> Parser g s a
forall (m :: * -> *) a. Parsing m => m a -> m a
try Parser g s a
p Parser g s a -> Parser g s () -> Parser g s ()
forall a b. Parser g s a -> Parser g s b -> Parser g s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser g s ()
go
   unexpected :: forall a. String -> Parser g s a
unexpected String
msg = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (\[(s, g (ResultList g s))]
t-> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty (ParseFailure Pos s -> ResultList g s a)
-> ParseFailure Pos s -> ResultList g s a
forall a b. (a -> b) -> a -> b
$ Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
erroneous (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
t) String
msg)
   eof :: Parser g s ()
eof = ([(s, g (ResultList g s))] -> ResultList g s ()) -> Parser g s ()
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s ()
forall {s} {g :: (* -> *) -> *}.
(MonoidNull s, Ord s) =>
[(s, g (ResultList g s))] -> ResultList g s ()
f
      where f :: [(s, g (ResultList g s))] -> ResultList g s ()
f rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
_)
               | s -> Bool
forall m. MonoidNull m => m -> Bool
null s
s = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty ()
-> ResultsOfLength g s ()
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
rest (()() -> [()] -> NonEmpty ()
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
               | Bool
otherwise = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s ()]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"end of input")
            f [] = [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty ()
-> ResultsOfLength g s ()
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [] (()() -> [()] -> NonEmpty ()
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty

instance (MonoidNull s, Ord s) => DeterministicParsing (Parser g s) where
   Parser [(s, g (ResultList g s))] -> ResultList g s a
p <<|> :: forall a. Parser g s a -> Parser g s a -> Parser g s a
<<|> Parser [(s, g (ResultList g s))] -> ResultList g s a
q = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
r where
      r :: [(s, g (ResultList g s))] -> ResultList g s a
r [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest
               of rl :: ResultList g s a
rl@(ResultList [] ParseFailure Pos s
_failure) -> ResultList g s a
rl ResultList g s a -> ResultList g s a -> ResultList g s a
forall a. Semigroup a => a -> a -> a
<> [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest
                  ResultList g s a
rl -> ResultList g s a
rl
   takeSome :: forall a. Parser g s a -> Parser g s [a]
takeSome Parser g s a
p = (:) (a -> [a] -> [a]) -> Parser g s a -> Parser g s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser g s a
p Parser g s ([a] -> [a]) -> Parser g s [a] -> Parser g s [a]
forall a b. Parser g s (a -> b) -> Parser g s a -> Parser g s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser g s a -> Parser g s [a]
forall a. Parser g s a -> Parser g s [a]
forall (m :: * -> *) a. DeterministicParsing m => m a -> m [a]
takeMany Parser g s a
p
   takeMany :: forall a. Parser g s a -> Parser g s [a]
takeMany (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s [a]) -> Parser g s [a]
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (Int
-> ([a] -> [a]) -> [(s, g (ResultList g s))] -> ResultList g s [a]
q Int
0 [a] -> [a]
forall a. a -> a
id) where
      q :: Int
-> ([a] -> [a]) -> [(s, g (ResultList g s))] -> ResultList g s [a]
q !Int
len [a] -> [a]
acc [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest
                        of ResultList [] ParseFailure Pos s
_failure -> [ResultsOfLength g s [a]]
-> ParseFailure Pos s -> ResultList g s [a]
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty [a]
-> ResultsOfLength g s [a]
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
len [(s, g (ResultList g s))]
rest ([a] -> [a]
acc [] [a] -> [[a]] -> NonEmpty [a]
forall a. a -> [a] -> NonEmpty a
:| [])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                           ResultList [ResultsOfLength g s a]
rl ParseFailure Pos s
_ -> (ResultsOfLength g s a -> ResultList g s [a])
-> [ResultsOfLength g s a] -> ResultList g s [a]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ResultsOfLength g s a -> ResultList g s [a]
continue [ResultsOfLength g s a]
rl
         where continue :: ResultsOfLength g s a -> ResultList g s [a]
continue (ResultsOfLength Int
len' [(s, g (ResultList g s))]
rest' NonEmpty a
results) = (a -> ResultList g s [a]) -> NonEmpty a -> ResultList g s [a]
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\a
r-> Int
-> ([a] -> [a]) -> [(s, g (ResultList g s))] -> ResultList g s [a]
q (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len') ([a] -> [a]
acc ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
ra -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) [(s, g (ResultList g s))]
rest') NonEmpty a
results
   skipAll :: forall a. Parser g s a -> Parser g s ()
skipAll (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s ()) -> Parser g s ()
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (Int -> [(s, g (ResultList g s))] -> ResultList g s ()
q Int
0) where
      q :: Int -> [(s, g (ResultList g s))] -> ResultList g s ()
q !Int
len [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest
                    of ResultList [] ParseFailure Pos s
_failure -> [ResultsOfLength g s ()] -> ParseFailure Pos s -> ResultList g s ()
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty ()
-> ResultsOfLength g s ()
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
len [(s, g (ResultList g s))]
rest (()() -> [()] -> NonEmpty ()
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                       ResultList [ResultsOfLength g s a]
rl ParseFailure Pos s
_failure -> (ResultsOfLength g s a -> ResultList g s ())
-> [ResultsOfLength g s a] -> ResultList g s ()
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ResultsOfLength g s a -> ResultList g s ()
continue [ResultsOfLength g s a]
rl
         where continue :: ResultsOfLength g s a -> ResultList g s ()
continue (ResultsOfLength Int
len' [(s, g (ResultList g s))]
rest' NonEmpty a
_) = Int -> [(s, g (ResultList g s))] -> ResultList g s ()
q (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len') [(s, g (ResultList g s))]
rest'

instance (MonoidNull s, Ord s) => LookAheadParsing (Parser g s) where
   lookAhead :: forall a. Parser g s a -> Parser g s a
lookAhead (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser (\[(s, g (ResultList g s))]
input-> [(s, g (ResultList g s))] -> ResultList g s a -> ResultList g s a
forall {s} {g :: (* -> *) -> *} {r}.
[(s, g (ResultList g s))] -> ResultList g s r -> ResultList g s r
rewind [(s, g (ResultList g s))]
input ([(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
input))
      where rewind :: [(s, g (ResultList g s))] -> ResultList g s r -> ResultList g s r
rewind [(s, g (ResultList g s))]
_ rl :: ResultList g s r
rl@(ResultList [] ParseFailure Pos s
_) = ResultList g s r
rl
            rewind [(s, g (ResultList g s))]
t (ResultList [ResultsOfLength g s r]
rl ParseFailure Pos s
failure) = [ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
t (NonEmpty r -> ResultsOfLength g s r)
-> NonEmpty r -> ResultsOfLength g s r
forall a b. (a -> b) -> a -> b
$ (NonEmpty r -> NonEmpty r -> NonEmpty r)
-> [NonEmpty r] -> NonEmpty r
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 NonEmpty r -> NonEmpty r -> NonEmpty r
forall a. Semigroup a => a -> a -> a
(<>) (ResultsOfLength g s r -> NonEmpty r
forall {g :: (* -> *) -> *} {s} {r}.
ResultsOfLength g s r -> NonEmpty r
results (ResultsOfLength g s r -> NonEmpty r)
-> [ResultsOfLength g s r] -> [NonEmpty r]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s r]
rl)] ParseFailure Pos s
failure
            results :: ResultsOfLength g s r -> NonEmpty r
results (ResultsOfLength Int
_ [(s, g (ResultList g s))]
_ NonEmpty r
r) = NonEmpty r
r

instance (Ord s, Show s, TextualMonoid s) => CharParsing (Parser g s) where
   satisfy :: (Char -> Bool) -> Parser g s Char
satisfy Char -> Bool
predicate = ([(s, g (ResultList g s))] -> ResultList g s Char)
-> Parser g s Char
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s Char
p
      where p :: [(s, g (ResultList g s))] -> ResultList g s Char
p rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
t) =
               case s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
s
               of Just Char
first | Char -> Bool
predicate Char
first -> [ResultsOfLength g s Char]
-> ParseFailure Pos s -> ResultList g s Char
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty Char
-> ResultsOfLength g s Char
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
1 [(s, g (ResultList g s))]
t (Char
firstChar -> String -> NonEmpty Char
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                  Maybe Char
_ -> [ResultsOfLength g s Char]
-> ParseFailure Pos s -> ResultList g s Char
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s Char]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected (Int -> Pos
forall a. a -> Down a
Down (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ [(s, g (ResultList g s))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(s, g (ResultList g s))]
rest) String
"Char.satisfy")
            p [] = [ResultsOfLength g s Char]
-> ParseFailure Pos s -> ResultList g s Char
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s Char]
forall a. Monoid a => a
mempty (Pos -> String -> ParseFailure Pos s
forall s. Pos -> String -> ParseFailure Pos s
expected Pos
0 String
"Char.satisfy")
   string :: String -> Parser g s String
string String
s = (s -> String) -> s -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
Textual.toString (String -> s -> String
forall a. HasCallStack => String -> a
error String
"unexpected non-character") (s -> String) -> Parser g s s -> Parser g s String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserInput (Parser g s) -> Parser g s (ParserInput (Parser g s))
forall (m :: * -> *).
InputParsing m =>
ParserInput m -> m (ParserInput m)
string (String -> s
forall a. IsString a => String -> a
fromString String
s)
   text :: Text -> Parser g s Text
text Text
t = (String -> Text
forall a. IsString a => String -> a
fromString (String -> Text) -> (s -> String) -> s -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> String) -> s -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
Textual.toString (String -> s -> String
forall a. HasCallStack => String -> a
error String
"unexpected non-character")) (s -> Text) -> Parser g s s -> Parser g s Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserInput (Parser g s) -> Parser g s (ParserInput (Parser g s))
forall (m :: * -> *).
InputParsing m =>
ParserInput m -> m (ParserInput m)
string (Text -> s
forall t. TextualMonoid t => Text -> t
Textual.fromText Text
t)

instance Ord s => AmbiguousParsing (Parser g s) where
   ambiguous :: forall a. Parser g s a -> Parser g s (Ambiguous a)
ambiguous (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))] -> ResultList g s (Ambiguous a))
-> Parser g s (Ambiguous a)
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s (Ambiguous a)
q
      where q :: [(s, g (ResultList g s))] -> ResultList g s (Ambiguous a)
q [(s, g (ResultList g s))]
rest | ResultList [ResultsOfLength g s a]
rs ParseFailure Pos s
failure <- [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest = [ResultsOfLength g s (Ambiguous a)]
-> ParseFailure Pos s -> ResultList g s (Ambiguous a)
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList (ResultsOfLength g s a -> ResultsOfLength g s (Ambiguous a)
forall {g :: (* -> *) -> *} {s} {a}.
ResultsOfLength g s a -> ResultsOfLength g s (Ambiguous a)
groupByLength (ResultsOfLength g s a -> ResultsOfLength g s (Ambiguous a))
-> [ResultsOfLength g s a] -> [ResultsOfLength g s (Ambiguous a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s a]
rs) ParseFailure Pos s
failure
            groupByLength :: ResultsOfLength g s a -> ResultsOfLength g s (Ambiguous a)
groupByLength (ResultsOfLength Int
l [(s, g (ResultList g s))]
rest NonEmpty a
rs) = Int
-> [(s, g (ResultList g s))]
-> NonEmpty (Ambiguous a)
-> ResultsOfLength g s (Ambiguous a)
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l [(s, g (ResultList g s))]
rest (NonEmpty a -> Ambiguous a
forall a. NonEmpty a -> Ambiguous a
Ambiguous NonEmpty a
rs Ambiguous a -> [Ambiguous a] -> NonEmpty (Ambiguous a)
forall a. a -> [a] -> NonEmpty a
:| [])

instance Ord s => CommittedParsing (Parser g s) where
   type CommittedResults (Parser g s) = ParseResults s
   commit :: forall a.
Parser g s a -> Parser g s (CommittedResults (Parser g s) a)
commit (Parser [(s, g (ResultList g s))] -> ResultList g s a
p) = ([(s, g (ResultList g s))]
 -> ResultList g s (Either (ParseFailure Pos s) a))
-> Parser g s (Either (ParseFailure Pos s) a)
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))]
-> ResultList g s (Either (ParseFailure Pos s) a)
q
      where q :: [(s, g (ResultList g s))]
-> ResultList g s (Either (ParseFailure Pos s) a)
q [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))] -> ResultList g s a
p [(s, g (ResultList g s))]
rest
                     of ResultList [] ParseFailure Pos s
failure -> [ResultsOfLength g s (Either (ParseFailure Pos s) a)]
-> ParseFailure Pos s
-> ResultList g s (Either (ParseFailure Pos s) a)
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))]
-> NonEmpty (Either (ParseFailure Pos s) a)
-> ResultsOfLength g s (Either (ParseFailure Pos s) a)
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
0 [(s, g (ResultList g s))]
rest (ParseFailure Pos s -> Either (ParseFailure Pos s) a
forall a b. a -> Either a b
Left ParseFailure Pos s
failureEither (ParseFailure Pos s) a
-> [Either (ParseFailure Pos s) a]
-> NonEmpty (Either (ParseFailure Pos s) a)
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                        ResultList [ResultsOfLength g s a]
rl ParseFailure Pos s
failure -> [ResultsOfLength g s (Either (ParseFailure Pos s) a)]
-> ParseFailure Pos s
-> ResultList g s (Either (ParseFailure Pos s) a)
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList ((a -> Either (ParseFailure Pos s) a)
-> ResultsOfLength g s a
-> ResultsOfLength g s (Either (ParseFailure Pos s) a)
forall a b.
(a -> b) -> ResultsOfLength g s a -> ResultsOfLength g s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either (ParseFailure Pos s) a
forall a b. b -> Either a b
Right (ResultsOfLength g s a
 -> ResultsOfLength g s (Either (ParseFailure Pos s) a))
-> [ResultsOfLength g s a]
-> [ResultsOfLength g s (Either (ParseFailure Pos s) a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ResultsOfLength g s a]
rl) ParseFailure Pos s
failure
   admit :: forall a.
Parser g s (CommittedResults (Parser g s) a) -> Parser g s a
admit (Parser [(s, g (ResultList g s))]
-> ResultList g s (CommittedResults (Parser g s) a)
p) = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
q
      where q :: [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest = case [(s, g (ResultList g s))]
-> ResultList g s (CommittedResults (Parser g s) a)
p [(s, g (ResultList g s))]
rest
                     of ResultList [] ParseFailure Pos s
failure -> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [] ParseFailure Pos s
failure
                        ResultList [ResultsOfLength g s (CommittedResults (Parser g s) a)]
rl ParseFailure Pos s
failure -> (ResultsOfLength g s (Either (ParseFailure Pos s) a)
 -> ResultList g s a)
-> [ResultsOfLength g s (Either (ParseFailure Pos s) a)]
-> ResultList g s a
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ResultsOfLength g s (Either (ParseFailure Pos s) a)
-> ResultList g s a
forall {s} {g :: (* -> *) -> *} {r}.
Ord s =>
ResultsOfLength g s (Either (ParseFailure Pos s) r)
-> ResultList g s r
expose [ResultsOfLength g s (Either (ParseFailure Pos s) a)]
[ResultsOfLength g s (CommittedResults (Parser g s) a)]
rl ResultList g s a -> ResultList g s a -> ResultList g s a
forall a. Semigroup a => a -> a -> a
<> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [] ParseFailure Pos s
failure
            expose :: ResultsOfLength g s (Either (ParseFailure Pos s) r)
-> ResultList g s r
expose (ResultsOfLength Int
len [(s, g (ResultList g s))]
t NonEmpty (Either (ParseFailure Pos s) r)
rs) = case [r] -> Maybe (NonEmpty r)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [r]
successes of
               Maybe (NonEmpty r)
Nothing -> [ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [] ([ParseFailure Pos s] -> ParseFailure Pos s
forall a. Monoid a => [a] -> a
mconcat [ParseFailure Pos s]
failures)
               Just NonEmpty r
successes' -> [ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
len [(s, g (ResultList g s))]
t NonEmpty r
successes'] ([ParseFailure Pos s] -> ParseFailure Pos s
forall a. Monoid a => [a] -> a
mconcat [ParseFailure Pos s]
failures)
               where ([ParseFailure Pos s]
failures, [r]
successes) = [Either (ParseFailure Pos s) r] -> ([ParseFailure Pos s], [r])
forall a b. [Either a b] -> ([a], [b])
partitionEithers (NonEmpty (Either (ParseFailure Pos s) r)
-> [Either (ParseFailure Pos s) r]
forall a. NonEmpty a -> [a]
toList NonEmpty (Either (ParseFailure Pos s) r)
rs)
        
-- | Turns a context-free parser into a backtracking PEG parser that consumes the longest possible prefix of the list
-- of input tails, opposite of 'peg'
longest :: Parser g s a -> Backtrack.Parser g [(s, g (ResultList g s))] a
longest :: forall (g :: (* -> *) -> *) s a.
Parser g s a -> Parser g [(s, g (ResultList g s))] a
longest Parser g s a
p = ([(s, g (ResultList g s))] -> Result g [(s, g (ResultList g s))] a)
-> Parser g [(s, g (ResultList g s))] a
forall (g :: (* -> *) -> *) s r.
(s -> Result g s r) -> Parser g s r
Backtrack.Parser [(s, g (ResultList g s))] -> Result g [(s, g (ResultList g s))] a
q where
   q :: [(s, g (ResultList g s))] -> Result g [(s, g (ResultList g s))] a
q [(s, g (ResultList g s))]
rest = case Parser g s a -> [(s, g (ResultList g s))] -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
Parser g s r -> [(s, g (ResultList g s))] -> ResultList g s r
applyParser Parser g s a
p [(s, g (ResultList g s))]
rest
            of ResultList [] (ParseFailure Pos
pos (FailureDescription [String]
static [s]
inputs) [String]
errors)
                  -> ParseFailure Pos [(s, g (ResultList g s))]
-> Result g [(s, g (ResultList g s))] a
forall (g :: (* -> *) -> *) s v. ParseFailure Pos s -> Result g s v
Backtrack.NoParse (Pos
-> FailureDescription [(s, g (ResultList g s))]
-> [String]
-> ParseFailure Pos [(s, g (ResultList g s))]
forall pos s.
pos -> FailureDescription s -> [String] -> ParseFailure pos s
ParseFailure Pos
pos ([String]
-> [[(s, g (ResultList g s))]]
-> FailureDescription [(s, g (ResultList g s))]
forall s. [String] -> [s] -> FailureDescription s
FailureDescription [String]
static ([[(s, g (ResultList g s))]]
 -> FailureDescription [(s, g (ResultList g s))])
-> [[(s, g (ResultList g s))]]
-> FailureDescription [(s, g (ResultList g s))]
forall a b. (a -> b) -> a -> b
$ (s -> [(s, g (ResultList g s))])
-> [s] -> [[(s, g (ResultList g s))]]
forall a b. (a -> b) -> [a] -> [b]
map s -> [(s, g (ResultList g s))]
forall {a} {b}. a -> [(a, b)]
wrap [s]
inputs) [String]
errors)
               ResultList [ResultsOfLength g s a]
rs ParseFailure Pos s
_ -> ResultsOfLength g s a -> Result g [(s, g (ResultList g s))] a
forall {g :: (* -> *) -> *} {s} {v} {g :: (* -> *) -> *}.
ResultsOfLength g s v -> Result g [(s, g (ResultList g s))] v
parsed ([ResultsOfLength g s a] -> ResultsOfLength g s a
forall a. HasCallStack => [a] -> a
last [ResultsOfLength g s a]
rs)
   parsed :: ResultsOfLength g s v -> Result g [(s, g (ResultList g s))] v
parsed (ResultsOfLength Int
l [(s, g (ResultList g s))]
s (v
r:|[v]
_)) = Int
-> v
-> [(s, g (ResultList g s))]
-> Result g [(s, g (ResultList g s))] v
forall (g :: (* -> *) -> *) s v. Int -> v -> s -> Result g s v
Backtrack.Parsed Int
l v
r [(s, g (ResultList g s))]
s
   wrap :: a -> [(a, b)]
wrap a
s = [(a
s, String -> b
forall a. HasCallStack => String -> a
error String
"longest")]

-- | Turns a backtracking PEG parser of the list of input tails into a context-free parser, opposite of 'longest'
peg :: Ord s => Backtrack.Parser g [(s, g (ResultList g s))] a -> Parser g s a
peg :: forall s (g :: (* -> *) -> *) a.
Ord s =>
Parser g [(s, g (ResultList g s))] a -> Parser g s a
peg Parser g [(s, g (ResultList g s))] a
p = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
q where
   q :: [(s, g (ResultList g s))] -> ResultList g s a
q [(s, g (ResultList g s))]
rest = case Parser g [(s, g (ResultList g s))] a
-> [(s, g (ResultList g s))]
-> Result g [(s, g (ResultList g s))] a
forall (g :: (* -> *) -> *) s r. Parser g s r -> s -> Result g s r
Backtrack.applyParser Parser g [(s, g (ResultList g s))] a
p [(s, g (ResultList g s))]
rest
            of Backtrack.Parsed Int
l a
result [(s, g (ResultList g s))]
suffix -> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty a -> ResultsOfLength g s a
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l [(s, g (ResultList g s))]
suffix (a
resulta -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
               Backtrack.NoParse (ParseFailure Pos
pos (FailureDescription [String]
static [[(s, g (ResultList g s))]]
inputs) [String]
errors)
                  -> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty (Pos -> FailureDescription s -> [String] -> ParseFailure Pos s
forall pos s.
pos -> FailureDescription s -> [String] -> ParseFailure pos s
ParseFailure Pos
pos ([String] -> [s] -> FailureDescription s
forall s. [String] -> [s] -> FailureDescription s
FailureDescription [String]
static ((s, g (ResultList g s)) -> s
forall a b. (a, b) -> a
fst ((s, g (ResultList g s)) -> s)
-> ([(s, g (ResultList g s))] -> (s, g (ResultList g s)))
-> [(s, g (ResultList g s))]
-> s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(s, g (ResultList g s))] -> (s, g (ResultList g s))
forall a. HasCallStack => [a] -> a
head ([(s, g (ResultList g s))] -> s)
-> [[(s, g (ResultList g s))]] -> [s]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[(s, g (ResultList g s))]]
inputs)) [String]
errors)

-- | Turns a backtracking PEG parser into a context-free parser
terminalPEG :: Monoid s => Ord s => Backtrack.Parser g s a -> Parser g s a
terminalPEG :: forall s (g :: (* -> *) -> *) a.
(Monoid s, Ord s) =>
Parser g s a -> Parser g s a
terminalPEG Parser g s a
p = ([(s, g (ResultList g s))] -> ResultList g s a) -> Parser g s a
forall (g :: (* -> *) -> *) s r.
([(s, g (ResultList g s))] -> ResultList g s r) -> Parser g s r
Parser [(s, g (ResultList g s))] -> ResultList g s a
q where
   q :: [(s, g (ResultList g s))] -> ResultList g s a
q [] = case Parser g s a -> s -> Result g s a
forall (g :: (* -> *) -> *) s r. Parser g s r -> s -> Result g s r
Backtrack.applyParser Parser g s a
p s
forall a. Monoid a => a
mempty
            of Backtrack.Parsed Int
l a
result s
_ -> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty a -> ResultsOfLength g s a
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l [] (a
resulta -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
               Backtrack.NoParse ParseFailure Pos s
failure -> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty ParseFailure Pos s
failure
   q rest :: [(s, g (ResultList g s))]
rest@((s
s, g (ResultList g s)
_):[(s, g (ResultList g s))]
_) = case Parser g s a -> s -> Result g s a
forall (g :: (* -> *) -> *) s r. Parser g s r -> s -> Result g s r
Backtrack.applyParser Parser g s a
p s
s
                       of Backtrack.Parsed Int
l a
result s
_ -> 
                             [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [Int
-> [(s, g (ResultList g s))] -> NonEmpty a -> ResultsOfLength g s a
forall (g :: (* -> *) -> *) s r.
Int
-> [(s, g (ResultList g s))] -> NonEmpty r -> ResultsOfLength g s r
ResultsOfLength Int
l (Int -> [(s, g (ResultList g s))] -> [(s, g (ResultList g s))]
forall a. Int -> [a] -> [a]
drop Int
l [(s, g (ResultList g s))]
rest) (a
resulta -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[])] ParseFailure Pos s
forall a. Monoid a => a
mempty
                          Backtrack.NoParse ParseFailure Pos s
failure -> [ResultsOfLength g s a] -> ParseFailure Pos s -> ResultList g s a
forall (g :: (* -> *) -> *) s r.
[ResultsOfLength g s r] -> ParseFailure Pos s -> ResultList g s r
ResultList [ResultsOfLength g s a]
forall a. Monoid a => a
mempty ParseFailure Pos s
failure