{-# LANGUAGE TypeOperators, FlexibleInstances #-}

-- |
-- This module provides the same functions and combinators as "GLL.Combinators.Interface".
-- The only difference is that the combinators of this module construct only symbol expressions ('SymbExpr'/'BNF').
-- The combinators are therefore easier to use: they are just as freely combined but with simpler types and simpler type-errors.
-- However, the the underlying grammars are binarised, resulting in slower parsing.
module GLL.Combinators.BinaryInterface (
    -- * Elementary parsers
    term_parser, satisfy,
    -- ** Elementary parsers using the 'Token' datatype 
    keychar, keyword, int_lit, float_lit, bool_lit, char_lit, string_lit, alt_id_lit, id_lit, token,
    -- ** Elementary character-level parsers
    char, 
    -- * Elementary combinators
    -- *** Sequencing
    (<**>),
    -- *** Choice
    (<||>),
    -- *** Semantic actions
    (<$$>),
    -- *** Nonterminal introduction
    (<:=>),(<::=>),chooses,chooses_prec,
    -- * Types
    -- ** Grammar (combinator expression) types
    BNF, SymbExpr, toSymb, mkRule,
    -- ** Parseable token types 
    Token(..), Parseable(..), SubsumesToken(..), unlexTokens, unlexToken,  
    -- * Running a parser 
    grammarOf, parse, printParseData, evaluatorWithParseData,
    -- **  Running a parser with options
    parseWithOptions, parseWithParseOptions, printParseDataWithOptions, evaluatorWithParseDataAndOptions,printGrammarData,
    -- *** Possible options
    CombinatorOptions, CombinatorOption, 
             GLL.Combinators.Options.maximumErrors, throwErrors, 
             maximumPivot, maximumPivotAtNt, leftBiased,
    -- **** Parser options
    fullSPPF, allNodes, packedNodesOnly, strictBinarisation, 
      GLL.Parser.noSelectTest,
    -- *** Running a parser with options and explicit failure
    parseWithOptionsAndError, parseWithParseOptionsAndError,
    -- ** Runing a parser to obtain 'ParseResult'.
    parseResult, parseResultWithOptions,ParseResult(..),
    -- ** Builtin lexers.
    default_lexer, 
    -- *** Lexer settings
        lexer, LexerSettings(..), emptyLanguage,
    -- * Derived combinators
    mkNt, 
    -- *** Ignoring semantic results
    (<$$), (**>), (<**),
    -- *** EBNF patterns
    optional, preferably, reluctantly, optionalWithDef,
    multiple, multiple1, multipleSepBy, multipleSepBy1,
      multipleSepBy2, within, parens, braces, brackets, angles,
     -- *** Disambiguation  
            (<:=), (<::=),(<<<**>), (<**>>>), (<<**>), (<<<**), (**>>>), (<**>>),
            longest_match,shortest_match,
            many, many1, some, some1, 
            manySepBy, manySepBy1, manySepBy2, 
              someSepBy, someSepBy1,someSepBy2,
     -- * Memoisation
    memo, newMemoTable, memClear, MemoTable, MemoRef, useMemoisation,
    module GLL.Combinators.Interface
    ) where

import GLL.Combinators.Interface hiding (within, (**>), (<**>), (<**), (<<<**>), (<<<**), (**>>>), (<**>>>), satisfy, (<||>), (<||), (||>), (<$$>), (<$$), (<:=>), (<:=),(<::=>), (<::=), mkNt, manySepBy, manySepBy1, manySepBy2, multiple, multipleSepBy, many, multipleSepBy1, multipleSepBy2, someSepBy, someSepBy1, someSepBy2, some, memo, some1, many1, multiple1, shortest_match, longest_match, (<**>>), (<<**>), angles, braces, brackets, parens, within, optional, optionalWithDef, preferably, reluctantly, chooses, chooses_prec)
import qualified GLL.Combinators.Interface as IF
import GLL.Combinators.Options
import GLL.Combinators.Visit.Join
import GLL.Combinators.Visit.Sem (emptyAncestors)
import GLL.Combinators.Memoisation
import GLL.Combinators.Lexer
import GLL.Types.Grammar
import GLL.Parser hiding (parse, parseWithOptions)
import qualified GLL.Parser as GLL

import GLL.Types.TypeCompose (OO(..))
import Control.Arrow
import qualified Data.Array as A
import qualified Data.IntMap as IM
import qualified Data.Map as M
import Data.Text (pack)
import Data.IORef 
import Data.Time.Clock
import System.IO.Unsafe


infixl 2 <:=>
-- | 
-- Form a rule by giving the name of the left-hand side of the new rule.
-- Use this combinator on recursive non-terminals.
(<:=>) :: (Show t, Ord t) => String -> BNF t a -> BNF t a 
String
n <:=> :: forall t a. (Show t, Ord t) => String -> BNF t a -> BNF t a
<:=> BNF t a
p = String
n forall t (b :: * -> * -> *) a.
(Show t, Ord t, HasAlts b) =>
String -> b t a -> SymbExpr t a
IF.<:=> BNF t a
p
infixl 2 <::=>

-- | 
--  Variant of '<:=>' for recursive non-terminals that have a potentially infinite
--  number of derivations for some input string.
--
--  A non-terminal yields infinitely many derivations  
--  if and only if it is left-recursive and would be
--  left-recursive if all the right-hand sides of the productions of the
--  grammar are reversed.
(<::=>) :: (Show t, Ord t) => String -> BNF t a -> BNF t a
String
n <::=> :: forall t a. (Show t, Ord t) => String -> BNF t a -> BNF t a
<::=> BNF t a
p = String
n forall t (b :: * -> * -> *) a.
(Show t, Ord t, HasAlts b) =>
String -> b t a -> SymbExpr t a
IF.<::=> BNF t a
p

-- | Variant of '<::=>' that can be supplied with a list of alternates
chooses :: (Show t, Ord t) => String -> [BNF t a] -> BNF t a
chooses :: forall t a. (Show t, Ord t) => String -> [BNF t a] -> BNF t a
chooses String
p [BNF t a]
alts = forall t (alt :: * -> * -> *) a.
(Show t, Ord t, IsAltExpr alt) =>
String -> [alt t a] -> SymbExpr t a
IF.chooses String
p [BNF t a]
alts 

-- | Variant of '<::=' that can be supplied with a list of alternates
chooses_prec :: (Show t, Ord t) => String -> [BNF t a] -> BNF t a
chooses_prec :: forall t a. (Show t, Ord t) => String -> [BNF t a] -> BNF t a
chooses_prec String
p [BNF t a]
alts = forall t (alt :: * -> * -> *) a.
(Show t, Ord t, IsAltExpr alt) =>
String -> [alt t a] -> SymbExpr t a
IF.chooses_prec String
p [BNF t a]
alts 

infixl 4 <$$>
-- |
-- Form an 'AltExpr' by mapping some semantic action overy the result
-- of the second argument.
(<$$>) :: (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
a -> b
f <$$> :: forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p' = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (a -> b
f forall t (s :: * -> * -> *) a b.
(Show t, Ord t, IsSymbExpr s) =>
(a -> b) -> s t a -> AltExpr t b
IF.<$$> BNF t a
p')

infixl 4 <**>,<<<**>,<**>>>
-- | 
-- Add a 'SymbExpr' to the right-hand side represented by an 'AltExpr'
-- creating a new 'AltExpr'. 
-- The semantic result of the first argument is applied to the second 
-- as a cross-product. 
(<**>) :: (Show t, Ord t) =>  BNF t (a -> b) -> BNF t a -> BNF t b
BNF t (a -> b)
pl' <**> :: forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> BNF t a
pr' = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (BNF t (a -> b)
pl' forall t (i :: * -> * -> *) (s :: * -> * -> *) a b.
(Show t, Ord t, IsAltExpr i, IsSymbExpr s) =>
i t (a -> b) -> s t a -> AltExpr t b
IF.<**> BNF t a
pr')

-- | Variant of '<**>' that applies longest match on the left operand.
(<**>>>) :: (Show t, Ord t) => BNF t (a -> b) -> BNF t a -> BNF t b
BNF t (a -> b)
pl' <**>>> :: forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**>>> BNF t a
pr' = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (BNF t (a -> b)
pl' forall t (i :: * -> * -> *) (s :: * -> * -> *) a b.
(Show t, Ord t, IsAltExpr i, IsSymbExpr s) =>
i t (a -> b) -> s t a -> AltExpr t b
IF.<**>>> BNF t a
pr')

-- | Variant of '<**>' that applies shortest match on the left operand.
(<<<**>) :: (Show t, Ord t) => BNF t (a -> b) -> BNF t a -> BNF t b
BNF t (a -> b)
pl' <<<**> :: forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<<<**> BNF t a
pr' = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (BNF t (a -> b)
pl' forall t (i :: * -> * -> *) (s :: * -> * -> *) a b.
(Show t, Ord t, IsAltExpr i, IsSymbExpr s) =>
i t (a -> b) -> s t a -> AltExpr t b
IF.<<<**> BNF t a
pr')

infixr 3 <||>
-- |
-- Add an 'AltExpr' to a list of 'AltExpr'
-- The resuling  '[] :. AltExpr' forms the right-hand side of a rule.
(<||>) :: (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
BNF t a
l' <||> :: forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||> BNF t a
r' = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (BNF t a
l' forall t (i :: * -> * -> *) (b :: * -> * -> *) a.
(Show t, Ord t, IsAltExpr i, HasAlts b) =>
i t a -> b t a -> AltExprs t a
IF.<||> BNF t a
r') 

-- |
-- Apply this combinator to an alternative to turn all underlying occurrences
-- of '<**>' (or variants) apply 'longest match'.
longest_match :: (Show t, Ord t) => BNF t a -> BNF t a
longest_match :: forall t a. (Show t, Ord t) => BNF t a -> BNF t a
longest_match BNF t a
isalt = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (forall t (alt :: * -> * -> *) a.
(Show t, Ord t, IsAltExpr alt) =>
alt t a -> AltExpr t a
IF.longest_match BNF t a
isalt)

-- Apply this combinator to an alternative to turn all underlying occurrences
-- of '<**>' (or variants) apply 'shortest match'.
shortest_match :: (Show t, Ord t) => BNF t a -> BNF t a
shortest_match :: forall t a. (Show t, Ord t) => BNF t a -> BNF t a
shortest_match BNF t a
isalt = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (forall t (alt :: * -> * -> *) a.
(Show t, Ord t, IsAltExpr alt) =>
alt t a -> AltExpr t a
IF.shortest_match BNF t a
isalt)

-- | The empty right-hand side that yields its 
--  first argument as a semantic result.
satisfy :: (Show t, Ord t ) => a -> BNF t a
satisfy :: forall t a. (Show t, Ord t) => a -> BNF t a
satisfy a
a = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (forall t a. (Show t, Ord t) => a -> AltExpr t a
IF.satisfy a
a)

-- | 
-- This function memoises a parser, given:
--
-- * A 'MemoRef' pointing to a fresh 'MemoTable', created using 'newMemoTable'.
-- * The 'SymbExpr' to memoise.
--
-- Use 'memo' on those parsers that are expected to derive the same 
-- substring multiple times. If the same combinator expression is used
-- to parse multiple times the 'MemoRef' needs to be cleared using 'memClear'.
--
-- 'memo' relies on 'unsafePerformIO' and is therefore potentially unsafe.
-- The option 'useMemoisation' enables memoisation.
-- It is off by default, even if 'memo' is used in a combinator expression.
memo :: (Ord t, Show t) => MemoRef [a] -> BNF t a -> BNF t a
memo :: forall t a. (Ord t, Show t) => MemoRef [a] -> BNF t a -> BNF t a
memo MemoRef [a]
ref BNF t a
p' = forall t (s :: * -> * -> *) a.
(Ord t, Show t, IsSymbExpr s) =>
MemoRef [a] -> s t a -> SymbExpr t a
IF.memo MemoRef [a]
ref BNF t a
p' 
-- | 
-- Helper function for defining new combinators.
-- Use 'mkNt' to form a new unique non-terminal name based on
-- the symbol of a given 'SymbExpr' and a 'String' that is unique to
-- the newly defined combinator.
mkNt :: (Show t, Ord t) => BNF t a -> String -> String 
mkNt :: forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
str = forall t (s :: * -> * -> *) a.
(Show t, Ord t, IsSymbExpr s) =>
s t a -> String -> String
IF.mkNt BNF t a
p String
str 

-- | 
-- Variant of '<$$>' that ignores the semantic result of its second argument. 
(<$$) :: (Show t, Ord t) => b -> BNF t a -> BNF t b
b
f <$$ :: forall t b a. (Show t, Ord t) => b -> BNF t a -> BNF t b
<$$ BNF t a
p = forall a b. a -> b -> a
const b
f forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p
infixl 4 <$$

-- | 
infixl 4 **>, <<**>, **>>>

-- | 
-- Variant of '<**>' that ignores the semantic result of the first argument.
(**>) :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
BNF t a
l **> :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
**> BNF t b
r = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> a
const forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
l forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> BNF t b
r

-- Variant of '<**>' that applies longest match on its left operand. 
(**>>>) :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
BNF t a
l **>>> :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
**>>> BNF t b
r = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> a
const forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
l forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**>>> BNF t b
r

-- Variant of '<**>' that ignores shortest match on its left operand.
(<<**>) :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
BNF t a
l <<**> :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
<<**>BNF t b
r = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> a
const forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
l forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<<<**> BNF t b
r


infixl 4 <**, <<<**, <**>>
-- | 
-- Variant of '<**>' that ignores the semantic result of the second argument.
(<**) :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
BNF t a
l <** :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<** BNF t b
r = forall a b. a -> b -> a
const forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
l forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> BNF t b
r 

-- | Variant of '<**' that applies longest match on its left operand.
(<**>>) :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
BNF t a
l <**>> :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<**>> BNF t b
r = forall a b. a -> b -> a
const forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
l forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**>>> BNF t b
r 

-- | Variant '<**' that applies shortest match on its left operand
(<<<**) :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
BNF t a
l <<<** :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<<<** BNF t b
r = forall a b. a -> b -> a
const forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
l forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<<<**> BNF t b
r 

-- | 
-- Variant of '<::=>' that prioritises productions from left-to-right (or top-to-bottom).
String
x <::= :: String -> b t a -> SymbExpr t a
<::= b t a
altPs = String
x forall t (b :: * -> * -> *) a.
(Show t, Ord t, HasAlts b) =>
String -> b t a -> SymbExpr t a
IF.<::= b t a
altPs
infixl 2 <::=

-- | 
-- Variant of '<:=>' that prioritises productions from left-to-right (or top-to-bottom).
String
x <:= :: String -> b t a -> SymbExpr t a
<:= b t a
altPs = String
x forall t (b :: * -> * -> *) a.
(Show t, Ord t, HasAlts b) =>
String -> b t a -> SymbExpr t a
IF.<:= b t a
altPs
infixl 2 <:=

-- | Try to apply a parser multiple times (0 or more) with shortest match
-- applied to each occurrence of the parser.
many :: (Show t, Ord t) => BNF t a -> BNF t [a]
many :: forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
many = forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple_ forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
(<<<**>)

-- | Try to apply a parser multiple times (1 or more) with shortest match
-- applied to each occurrence of the parser.
many1 :: (Show t, Ord t) => BNF t a -> BNF t [a]
many1 :: forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
many1 = forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple1_ forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
(<<<**>) 

-- | Try to apply a parser multiple times (0 or more) with longest match
-- applied to each occurrence of the parser.
some :: (Show t, Ord t) => BNF t a -> BNF t [a]
some :: forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
some = forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple_ forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
(<**>>>)

-- | Try to apply a parser multiple times (1 or more) with longest match
-- applied to each occurrence of the parser.
some1 :: (Show t, Ord t) => BNF t a -> BNF t [a]
some1 :: forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
some1 = forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple1_ forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
(<**>>>) 

-- | Try to apply a parser multiple times (0 or more). The results are returned in a list.
-- In the case of ambiguity the largest list is returned.
multiple :: (Show t, Ord t) => BNF t a -> BNF t [a]
multiple :: forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
multiple = forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple_ forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
(<**>)

-- | Try to apply a parser multiple times (1 or more). The results are returned in a list.
-- In the case of ambiguity the largest list is returned.
multiple1 :: (Show t, Ord t) => BNF t a -> BNF t [a]
multiple1 :: forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
multiple1 = forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple1_ forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
(<**>)

-- | Internal
multiple_ :: (BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple_ BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a]
disa BNF t a
p = let fresh :: String
fresh = forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
"*" 
                    in String
fresh forall t a. (Show t, Ord t) => String -> BNF t a -> BNF t a
<::=> ((:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p) BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a]
`disa` ((BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple_ BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a]
disa BNF t a
p) forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||> forall t a. (Show t, Ord t) => a -> BNF t a
satisfy []

-- | Internal
multiple1_ :: (BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple1_ BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a]
disa BNF t a
p = let fresh :: String
fresh = forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
"+"
                     in String
fresh forall t a. (Show t, Ord t) => String -> BNF t a -> BNF t a
<::=> ((:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p) BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a]
`disa` (forall {t} {t} {a} {a}.
(Show t, Ord t, Show t, Ord t) =>
(BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a])
-> BNF t a -> BNF t [a]
multiple_ BNF t ([a] -> [a]) -> BNF t [a] -> BNF t [a]
disa BNF t a
p)

-- | Same as 'many' but with an additional separator.
manySepBy :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
manySepBy :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
manySepBy = forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
many
-- | Same as 'many1' but with an additional separator.
manySepBy1 :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
manySepBy1 :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
manySepBy1 = forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy1 forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
many
-- | Same as 'some1' but with an additional separator.
someSepBy :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
someSepBy :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
someSepBy = forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
some
-- | Same as 'some1' but with an additional separator.
someSepBy1 :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
someSepBy1 :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
someSepBy1 = forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy1 forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
some
-- | Same as 'multiple' but with an additional separator.
multipleSepBy :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
multipleSepBy :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
multipleSepBy = forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
multiple 
-- | Same as 'multiple1' but with an additional separator.
multipleSepBy1 :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
multipleSepBy1 :: forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
multipleSepBy1 = forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy1 forall t a. (Show t, Ord t) => BNF t a -> BNF t [a]
multiple 

sepBy :: (Show t, Ord t) => (BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy :: forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy BNF t a -> BNF t [a]
mult BNF t a
p BNF t b
c = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
mkRule forall a b. (a -> b) -> a -> b
$ forall t a. (Show t, Ord t) => a -> BNF t a
satisfy [] forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||> (:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> BNF t a -> BNF t [a]
mult (BNF t b
c forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
**> BNF t a
p)

sepBy1 :: (Show t, Ord t) => (BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy1 :: forall t a b.
(Show t, Ord t) =>
(BNF t a -> BNF t [a]) -> BNF t a -> BNF t b -> BNF t [a]
sepBy1 BNF t a -> BNF t [a]
mult BNF t a
p BNF t b
c = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
mkRule forall a b. (a -> b) -> a -> b
$ (:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> BNF t a -> BNF t [a]
mult (BNF t b
c forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
**> BNF t a
p)

-- | Like 'multipleSepBy1' but matching at least two occurrences of the 
-- first argument. The returned list is therefore always of at least
-- length 2. At least one separator will be consumed.
multipleSepBy2 :: BNF t a -> BNF t b -> BNF t [a]
multipleSepBy2 BNF t a
p BNF t b
s = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
mkRule forall a b. (a -> b) -> a -> b
$
  (:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<** BNF t b
s forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
multipleSepBy1 BNF t a
p BNF t b
s

-- | Like 'multipleSepBy2' but matching the minimum number of 
-- occurrences of the first argument as possible (at least 2).
someSepBy2 :: BNF t a -> BNF t b -> BNF t [a]
someSepBy2 BNF t a
p BNF t b
s = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
mkRule forall a b. (a -> b) -> a -> b
$
  (:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<** BNF t b
s forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
someSepBy1 BNF t a
p BNF t b
s

-- | Like 'multipleSepBy2' but matching the maximum number of
-- occurrences of the first argument as possible (at least 2).
manySepBy2 :: BNF t a -> BNF t b -> BNF t [a]
manySepBy2 BNF t a
p BNF t b
s = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
mkRule forall a b. (a -> b) -> a -> b
$ 
  (:) forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<** BNF t b
s forall t a b.
(Show t, Ord t) =>
BNF t (a -> b) -> BNF t a -> BNF t b
<**> forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t [a]
manySepBy1 BNF t a
p BNF t b
s

-- | Derive either from the given symbol or the empty string.
optional :: (Show t, Ord t) => BNF t a -> BNF t (Maybe a)
optional :: forall t a. (Show t, Ord t) => BNF t a -> BNF t (Maybe a)
optional BNF t a
p = String
fresh 
  forall t a. (Show t, Ord t) => String -> BNF t a -> BNF t a
<:=>  forall a. a -> Maybe a
Just forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p 
  forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||>  forall t a. (Show t, Ord t) => a -> BNF t a
satisfy forall a. Maybe a
Nothing 
  where fresh :: String
fresh = forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
"?"

-- | Version of 'optional' that prefers to derive from the given symbol,
-- affects only nullable nonterminal symbols
preferably :: (Show t, Ord t) => BNF t a -> BNF t (Maybe a)
preferably :: forall t a. (Show t, Ord t) => BNF t a -> BNF t (Maybe a)
preferably BNF t a
p = String
fresh 
  forall t (b :: * -> * -> *) a.
(Show t, Ord t, HasAlts b) =>
String -> b t a -> SymbExpr t a
<:=   forall a. a -> Maybe a
Just forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p 
  forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||>  forall t a. (Show t, Ord t) => a -> BNF t a
satisfy forall a. Maybe a
Nothing 
  where fresh :: String
fresh = forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
"?"

-- | Version of 'optional' that prefers to derive the empty string from 
-- the given symbol, affects only nullable nonterminal symbols
reluctantly :: (Show t, Ord t) => BNF t a -> BNF t (Maybe a)
reluctantly :: forall t a. (Show t, Ord t) => BNF t a -> BNF t (Maybe a)
reluctantly BNF t a
p = String
fresh 
  forall t (b :: * -> * -> *) a.
(Show t, Ord t, HasAlts b) =>
String -> b t a -> SymbExpr t a
<:=   forall t a. (Show t, Ord t) => a -> BNF t a
satisfy forall a. Maybe a
Nothing  
  forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||>  forall a. a -> Maybe a
Just forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p
  where fresh :: String
fresh = forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
"?"

optionalWithDef :: (Show t, Ord t) => BNF t a -> a -> BNF t a 
optionalWithDef :: forall t a. (Show t, Ord t) => BNF t a -> a -> BNF t a
optionalWithDef BNF t a
p a
def = forall t a. (Show t, Ord t) => BNF t a -> String -> String
mkNt BNF t a
p String
"?" forall t a. (Show t, Ord t) => String -> BNF t a -> BNF t a
<:=> forall a. a -> a
id forall t a b. (Show t, Ord t) => (a -> b) -> BNF t a -> BNF t b
<$$> BNF t a
p forall t a. (Show t, Ord t) => BNF t a -> BNF t a -> BNF t a
<||> forall t a. (Show t, Ord t) => a -> BNF t a
satisfy a
def

-- | Place a piece of BNF /within/ two other BNF fragments, ignoring their semantics.
within :: (Show t, Ord t) => BNF t a -> BNF t b -> BNF t c -> BNF t b
within :: forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within BNF t a
l BNF t b
p BNF t c
r = forall (a :: * -> * -> *) t b.
(IsSymbExpr a, Show t, Ord t) =>
a t b -> SymbExpr t b
IF.toSymb (BNF t a
l forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t b
**> BNF t b
p forall t a b. (Show t, Ord t) => BNF t a -> BNF t b -> BNF t a
<** BNF t c
r)

-- | Place a piece of BNF between the characters '(' and ')'.
parens :: BNF t b -> BNF t b
parens BNF t b
p = forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'(') BNF t b
p (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
')')
-- | Place a piece of BNF between the characters '{' and '}'.
braces :: BNF t b -> BNF t b
braces BNF t b
p = forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'{') BNF t b
p (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'}')
-- | Place a piece of BNF between the characters '[' and ']'.
brackets :: BNF t b -> BNF t b
brackets BNF t b
p = forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'[') BNF t b
p (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
']')
-- | Place a piece of BNF between the characters '<' and '>'.
angles :: BNF t b -> BNF t b
angles BNF t b
p = forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'<') BNF t b
p (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'>')
-- | Place a piece of BNF between two single quotes.
quotes :: BNF t b -> BNF t b
quotes BNF t b
p = forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'\'') BNF t b
p (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'\'')
-- | Place a piece of BNF between two double quotes.
dquotes :: BNF t b -> BNF t b
dquotes BNF t b
p = forall t a b c.
(Show t, Ord t) =>
BNF t a -> BNF t b -> BNF t c -> BNF t b
within (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'"') BNF t b
p (forall t. SubsumesToken t => Char -> SymbExpr t Char
keychar Char
'"')