{-# LANGUAGE CPP #-}
{- |
Module      : Control.Lens.Grammar.Prec
Description : grammars of Haskell types, as Read & Show
Copyright   : (C) 2026 - Eitan Chatav
License     : BSD-style (see the file LICENSE)
Maintainer  : Eitan Chatav <eitan.chatav@gmail.com>
Stability   : provisional
Portability : non-portable

Grammars of Haskell types, equivalent to their stock `Read` & `Show`
instances, by precedence, as `Text.ParserCombinators.ReadPrec` reads, &
`showsPrec` shows: `PrecGrammar`s, run by `PrecGrammatical`s, like
t'Precedence', which `readsPrecG` & `showsPrecG` run.
-}

module Control.Lens.Grammar.Prec
  ( -- * PrecGrammar
    PrecGrammar (..)
  , PrecGrammatical (..)
  , precParens
  , precList
    -- * NotFollowedBy
  , NotFollowedBy (..)
  , eof
  , munch
  , munch1
  , skipSpaces
    -- * Precedence
  , Precedence (..)
  , precG
  , readsPrecG
  , showsPrecG
  ) where

import Control.Applicative
import Control.Monad ((>=>))
import Control.Lens
import Control.Lens.Diopter (skippedMany)
import Control.Lens.Grammar.BackusNaur
import Control.Lens.Grammar.Class
import Control.Lens.Grammar.RegEx
import Control.Lens.Grammar.Symbol
import Control.Lens.Grammar.Token
import Control.Lens.PartialIso
import Control.Lens.Prismoid ((>?*))
import Data.Char (isDigit, isLetter, toUpper)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.List (nub)
import Data.Maybe (isJust)
import Data.Monoid.Disjunctive
import Data.Profunctor.Distributor
import Data.Profunctor.Grammar
import Data.Profunctor.Monoidal
import Data.Profunctor.Monoidal.Cocartesian
import Data.Ratio ((%))
import Data.String (IsString (..))
import Data.Word (Word8, Word16, Word32, Word64)
import Data.Proxy (Proxy (..))
import GHC.Generics hiding (from, to)
import qualified GHC.Generics as Generic
import Numeric (floatToDigits)
import Numeric.Natural (Natural)

{- | A Haskell type's grammar, by precedence, equivalent to its stock `Read`
& `Show` instances, `readsPrec` & `showsPrec`. -}
class PrecGrammar a where
  precGrammar :: PrecGrammatical p => p a a
  default precGrammar
    :: (Generic a, GPrecSum (Rep a), PrecGrammatical p) => p a a
  precGrammar = (a -> Rep a (ZonkAny 0))
-> (Rep a (ZonkAny 0) -> a)
-> Iso a a (Rep a (ZonkAny 0)) (Rep a (ZonkAny 0))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso a -> Rep a (ZonkAny 0)
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
Generic.from Rep a (ZonkAny 0) -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
Generic.to (Exchange
   (Rep a (ZonkAny 0))
   (Rep a (ZonkAny 0))
   (Rep a (ZonkAny 0))
   (Identity (Rep a (ZonkAny 0)))
 -> Exchange (Rep a (ZonkAny 0)) (Rep a (ZonkAny 0)) a (Identity a))
-> p (Rep a (ZonkAny 0)) (Rep a (ZonkAny 0)) -> p a a
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p (Rep a (ZonkAny 0)) (Rep a (ZonkAny 0))
-> p (Rep a (ZonkAny 0)) (Rep a (ZonkAny 0))
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens p (Rep a (ZonkAny 0)) (Rep a (ZonkAny 0))
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecSum f, PrecGrammatical p) =>
p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
p (Rep a x) (Rep a x)
gPrecSum
  {- | Lists of the type, as `readListPrec` & `showList`, in brackets, by
  `precList`, by default, & strings, for `Char`. -}
  precListGrammar :: PrecGrammatical p => p [a] [a]
  precListGrammar = p a a -> p [a] [a]
forall (p :: * -> * -> *) a.
PrecGrammatical p =>
p a a -> p [a] [a]
precList p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p a a
precGrammar

{- | `Grammatical` interpreters of precedence, as
`Text.ParserCombinators.ReadPrec` has, where a grammar runs in a
precedence context, from 0 to 11, as `showsPrec`'s is. -}
class
  ( Grammatical Char p, NotFollowedBy p
  , forall x y. (x ~ (), y ~ ()) => IsString (p x y) )
  => PrecGrammatical p where
    {- | At a precedence, failing in a context above it, as
    `Text.ParserCombinators.ReadPrec.prec` does. -}
    precP :: Int -> p a b -> p a b
    {- | In a context one higher, for arguments, as
    `Text.ParserCombinators.ReadPrec.step` does. -}
    precStep :: p a b -> p a b
    {- | In the context 0, within brackets, as
    `Text.ParserCombinators.ReadPrec.reset` does. -}
    precReset :: p a b -> p a b
    {- | By the context's precedence. -}
    precWith :: (Int -> p a b) -> p a b

{- | Within any parentheses, after whitespace, as `GHC.Read.parens` reads,
& within parentheses where it doesn't print bare, as `showParen` shows. -}
precParens :: PrecGrammatical p => p a b -> p a b
precParens :: forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens p a b
p = p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p a b -> p a b
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParenthesized p a b
p

{- Bare, or within parentheses, without leading whitespace, so that no two
places skip the same whitespace, which `read` skips once, greedily. -}
precParenthesized :: PrecGrammatical p => p a b -> p a b
precParenthesized :: forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParenthesized p a b
p = p a b
p p a b -> p a b -> p a b
forall a. DisjunctiveMonoid a => a -> a -> a
>|< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"(" p () () -> p a b -> p a b
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p a b -> p a b
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precReset (p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens p a b
p) p a b -> p () () -> p a b
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p a b -> p () () -> p a b
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
")"

{- | A list, in brackets, its items separated by commas, after whitespace,
at precedence 0, within any parentheses, as `GHC.Read.list` reads &
`showList` shows. -}
precList :: PrecGrammatical p => p a a -> p [a] [a]
precList :: forall (p :: * -> * -> *) a.
PrecGrammatical p =>
p a a -> p [a] [a]
precList = p [a] [a] -> p [a] [a]
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens (p [a] [a] -> p [a] [a])
-> (p a a -> p [a] [a]) -> p a a -> p [a] [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a a -> p [a] [a]
forall (p :: * -> * -> *) a.
PrecGrammatical p =>
p a a -> p [a] [a]
precBrackets

-- Items in brackets, separated by commas, after whitespace, at precedence 0.
precBrackets :: PrecGrammatical p => p a a -> p [a] [a]
precBrackets :: forall (p :: * -> * -> *) a.
PrecGrammatical p =>
p a a -> p [a] [a]
precBrackets p a a
x = [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"[" p () () -> p [a] [a] -> p [a] [a]
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [p [a] [a]] -> p [a] [a]
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
  [ Market () () () (Identity ()) -> Market () () [a] (Identity [a])
forall a. AsEmpty a => Prism' a ()
Prism' [a] ()
_Empty (Market () () () (Identity ()) -> Market () () [a] (Identity [a]))
-> p () () -> p [a] [a]
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP
  , Market (a, [a]) (a, [a]) (a, [a]) (Identity (a, [a]))
-> Market (a, [a]) (a, [a]) [a] (Identity [a])
forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
Prism [a] [a] (a, [a]) (a, [a])
_Cons (Market (a, [a]) (a, [a]) (a, [a]) (Identity (a, [a]))
 -> Market (a, [a]) (a, [a]) [a] (Identity [a]))
-> p (a, [a]) (a, [a]) -> p [a] [a]
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p a a -> p a a
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precReset p a a
x p a a -> p [a] [a] -> p (a, [a]) (a, [a])
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p a a -> p [a] [a]
forall a b. p a b -> p [a] [b]
forall (p :: * -> * -> *) a b. Distributor p => p a b -> p [a] [b]
manyP (p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"," p () () -> p a a -> p a a
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p a a -> p a a
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precReset p a a
x)
  ] p [a] [a] -> p () () -> p [a] [a]
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p [a] [a] -> p () () -> p [a] [a]
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"]"

{- | Negative lookahead, as `read`'s lexer munches lexemes maximally. -}
class NotFollowedBy p where
  {- | Not followed by a string a pattern matches, consuming nothing, &
  printing nothing; nonterminals match nothing. -}
  notFollowedBy :: RegEx Char -> p () ()
instance (Alternative f, Cons s s Char Char) => NotFollowedBy (Parsor s f) where
  notFollowedBy :: RegEx Char -> Parsor s f () ()
notFollowedBy RegEx Char
rex = (Maybe () -> s -> f ((), s)) -> Parsor s f () ()
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe () -> s -> f ((), s)) -> Parsor s f () ())
-> (Maybe () -> s -> f ((), s)) -> Parsor s f () ()
forall a b. (a -> b) -> a -> b
$ \Maybe ()
mode s
s -> case Maybe ()
mode of
    Maybe ()
Nothing | Bool -> Bool
not ([(Int, s)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (RegEx Char -> (Int, s) -> [(Int, s)]
prefixes RegEx Char
rex (Int
0 :: Int, s
s))) -> f ((), s)
forall a. f a
forall (f :: * -> *) a. Alternative f => f a
empty
    Maybe ()
_ -> ((), s) -> f ((), s)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((), s
s)
    where
      -- the rests after each prefix a pattern matches, counting tokens,
      -- so that repetitions of empty matches end
      prefixes :: RegEx Char -> (Int, s) -> [(Int, s)]
prefixes = \case
        RegEx Char
SeqEmpty -> (Int, s) -> [(Int, s)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        Sequence RegEx Char
x RegEx Char
y [RegEx Char]
zs -> (RegEx Char -> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)])
-> ((Int, s) -> [(Int, s)])
-> [RegEx Char]
-> (Int, s)
-> [(Int, s)]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (((Int, s) -> [(Int, s)])
-> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
(>=>) (((Int, s) -> [(Int, s)])
 -> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)])
-> (RegEx Char -> (Int, s) -> [(Int, s)])
-> RegEx Char
-> ((Int, s) -> [(Int, s)])
-> (Int, s)
-> [(Int, s)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegEx Char -> (Int, s) -> [(Int, s)]
prefixes) (Int, s) -> [(Int, s)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RegEx Char
x RegEx Char -> [RegEx Char] -> [RegEx Char]
forall a. a -> [a] -> [a]
: RegEx Char
y RegEx Char -> [RegEx Char] -> [RegEx Char]
forall a. a -> [a] -> [a]
: [RegEx Char]
zs)
        NonTerminal [Char]
_ -> [(Int, s)] -> (Int, s) -> [(Int, s)]
forall a b. a -> b -> a
const []
        KleeneStar RegEx Char
x -> RegEx Char -> (Int, s) -> [(Int, s)]
star RegEx Char
x
        KleeneOpt RegEx Char
x -> Int -> RegEx Char -> (Int, s) -> [(Int, s)]
upTo Int
1 RegEx Char
x
        KleenePlus RegEx Char
x -> RegEx Char -> (Int, s) -> [(Int, s)]
prefixes RegEx Char
x ((Int, s) -> [(Int, s)])
-> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> RegEx Char -> (Int, s) -> [(Int, s)]
star RegEx Char
x
        KleeneTimes Int
lo Maybe Int
hi RegEx Char
x -> (((Int, s) -> [(Int, s)])
 -> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)])
-> ((Int, s) -> [(Int, s)])
-> [(Int, s) -> [(Int, s)]]
-> (Int, s)
-> [(Int, s)]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Int, s) -> [(Int, s)])
-> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
(>=>) (Int, s) -> [(Int, s)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ((Int, s) -> [(Int, s)]) -> [(Int, s) -> [(Int, s)]]
forall a. Int -> a -> [a]
replicate Int
lo (RegEx Char -> (Int, s) -> [(Int, s)]
prefixes RegEx Char
x))
          ((Int, s) -> [(Int, s)])
-> ((Int, s) -> [(Int, s)]) -> (Int, s) -> [(Int, s)]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ((Int, s) -> [(Int, s)])
-> (Int -> (Int, s) -> [(Int, s)])
-> Maybe Int
-> (Int, s)
-> [(Int, s)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (RegEx Char -> (Int, s) -> [(Int, s)]
star RegEx Char
x) (\Int
m -> Int -> RegEx Char -> (Int, s) -> [(Int, s)]
upTo (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
lo) RegEx Char
x) Maybe Int
hi
        Backtracking RegEx Char
x -> RegEx Char -> (Int, s) -> [(Int, s)]
prefixes RegEx Char
x
        RegExam (Alternate RegEx Char
x RegEx Char
y [RegEx Char]
zs) ->
          \(Int, s)
pos -> (RegEx Char -> [(Int, s)]) -> [RegEx Char] -> [(Int, s)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (RegEx Char -> (Int, s) -> [(Int, s)]
`prefixes` (Int, s)
pos) (RegEx Char
x RegEx Char -> [RegEx Char] -> [RegEx Char]
forall a. a -> [a] -> [a]
: RegEx Char
y RegEx Char -> [RegEx Char] -> [RegEx Char]
forall a. a -> [a] -> [a]
: [RegEx Char]
zs)
        RegExam (OneOf Set Char
toks) -> TokenClass Char -> (Int, s) -> [(Int, s)]
forall {a} {b} {token}.
(Num a, Cons b b token token, Categorized token) =>
TokenClass token -> (a, b) -> [(a, b)]
one (TokenExam Char (TokenClass Char) -> TokenClass Char
forall token.
TokenExam token (TokenClass token) -> TokenClass token
TokenClass (Set Char -> TokenExam Char (TokenClass Char)
forall token alg. Set token -> TokenExam token alg
OneOf Set Char
toks))
        RegExam (NotOneOf Set Char
toks CategoryTest Char
test) -> TokenClass Char -> (Int, s) -> [(Int, s)]
forall {a} {b} {token}.
(Num a, Cons b b token token, Categorized token) =>
TokenClass token -> (a, b) -> [(a, b)]
one (TokenExam Char (TokenClass Char) -> TokenClass Char
forall token.
TokenExam token (TokenClass token) -> TokenClass token
TokenClass (Set Char -> CategoryTest Char -> TokenExam Char (TokenClass Char)
forall token alg.
Set token -> CategoryTest token -> TokenExam token alg
NotOneOf Set Char
toks CategoryTest Char
test))
      one :: TokenClass token -> (a, b) -> [(a, b)]
one TokenClass token
cls (a
n, b
str) = case b -> Maybe (token, b)
forall s a. Cons s s a a => s -> Maybe (a, s)
uncons b
str of
        Just (token
c, b
str') | TokenClass token -> token -> Bool
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass token
cls token
c -> [(a
n a -> a -> a
forall a. Num a => a -> a -> a
+ a
1, b
str')]
        Maybe (token, b)
_ -> []
      star :: RegEx Char -> (Int, s) -> [(Int, s)]
star RegEx Char
x (Int, s)
pos = (Int, s)
pos (Int, s) -> [(Int, s)] -> [(Int, s)]
forall a. a -> [a] -> [a]
:
        [(Int, s)
end | (Int, s)
pos' <- RegEx Char -> (Int, s) -> [(Int, s)]
prefixes RegEx Char
x (Int, s)
pos, (Int, s) -> Int
forall a b. (a, b) -> a
fst (Int, s)
pos' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> (Int, s) -> Int
forall a b. (a, b) -> a
fst (Int, s)
pos, (Int, s)
end <- RegEx Char -> (Int, s) -> [(Int, s)]
star RegEx Char
x (Int, s)
pos']
      upTo :: Int -> RegEx Char -> (Int, s) -> [(Int, s)]
upTo Int
m RegEx Char
x (Int, s)
pos
        | Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = [(Int, s)
pos]
        | Bool
otherwise = (Int, s)
pos (Int, s) -> [(Int, s)] -> [(Int, s)]
forall a. a -> [a] -> [a]
: (RegEx Char -> (Int, s) -> [(Int, s)]
prefixes RegEx Char
x (Int, s)
pos [(Int, s)] -> ((Int, s) -> [(Int, s)]) -> [(Int, s)]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> RegEx Char -> (Int, s) -> [(Int, s)]
upTo (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) RegEx Char
x)
instance Applicative f => NotFollowedBy (Printor s f) where
  notFollowedBy :: RegEx Char -> Printor s f () ()
notFollowedBy RegEx Char
_ = (() -> f ((), s -> s)) -> Printor s f () ()
forall s (f :: * -> *) a b. (a -> f (b, s -> s)) -> Printor s f a b
Printor (\()
_ -> ((), s -> s) -> f ((), s -> s)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((), s -> s
forall a. a -> a
id))
instance NotFollowedBy p => NotFollowedBy (Precedence p) where
  notFollowedBy :: RegEx Char -> Precedence p () ()
notFollowedBy = (Int -> p () ()) -> Precedence p () ()
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p () ()) -> Precedence p () ())
-> (RegEx Char -> Int -> p () ())
-> RegEx Char
-> Precedence p () ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p () () -> Int -> p () ()
forall a b. a -> b -> a
const (p () () -> Int -> p () ())
-> (RegEx Char -> p () ()) -> RegEx Char -> Int -> p () ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy

{- | At the end of input, not followed by any token. -}
eof :: NotFollowedBy p => p () ()
eof :: forall (p :: * -> * -> *). NotFollowedBy p => p () ()
eof = RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy RegEx Char
forall token p. Tokenized token p => p
anyToken

{- | Tokens of a class, as many as follow, as
`Text.ParserCombinators.ReadP.munch` parses, & printing them. -}
munch
  :: (RegGrammatical Char p, NotFollowedBy p)
  => TokenClass Char -> p String String
munch :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
TokenClass Char -> p [Char] [Char]
munch TokenClass Char
cls = p Char Char -> p [Char] [Char]
forall a b. p a b -> p [a] [b]
forall (p :: * -> * -> *) a b. Distributor p => p a b -> p [a] [b]
manyP (TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
cls) p [Char] [Char] -> p () () -> p [Char] [Char]
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
cls)

{- | Tokens of a class, one or more, as many as follow, as
`Text.ParserCombinators.ReadP.munch1` parses, & printing them. -}
munch1
  :: (RegGrammatical Char p, NotFollowedBy p)
  => TokenClass Char -> p String String
munch1 :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
TokenClass Char -> p [Char] [Char]
munch1 TokenClass Char
cls = p Char Char -> p [Char] [Char]
forall a b. p a b -> p [a] [b]
forall (p :: * -> * -> *) a b. Alternator p => p a b -> p [a] [b]
someP (TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
cls) p [Char] [Char] -> p () () -> p [Char] [Char]
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
cls)

{- | Whitespace, by `Data.Char.isSpace`, as many as follow, as
`Text.ParserCombinators.ReadP.skipSpaces` parses, & printing none. -}
skipSpaces :: (RegGrammatical Char p, NotFollowedBy p) => p () ()
skipSpaces :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces = (() -> [Char]) -> ([Char] -> ()) -> p [Char] [Char] -> p () ()
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ([Char] -> () -> [Char]
forall a b. a -> b -> a
const [Char]
"") (() -> [Char] -> ()
forall a b. a -> b -> a
const ()) (TokenClass Char -> p [Char] [Char]
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
TokenClass Char -> p [Char] [Char]
munch TokenClass Char
precSpaceClass)

{- | A grammar in a precedence context, a `PrecGrammatical` interpreter of
any `Grammatical` one, each of whose structures it lifts pointwise. -}
newtype Precedence p a b = Precedence {forall (p :: * -> * -> *) a b. Precedence p a b -> Int -> p a b
runPrecedence :: Int -> p a b}

instance Profunctor p => Profunctor (Precedence p) where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Precedence p b c -> Precedence p a d
dimap a -> b
f c -> d
g (Precedence Int -> p b c
h) = (Int -> p a d) -> Precedence p a d
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((a -> b) -> (c -> d) -> p b c -> p a d
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g (p b c -> p a d) -> (Int -> p b c) -> Int -> p a d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p b c
h)
instance Functor (p a) => Functor (Precedence p a) where
  fmap :: forall a b. (a -> b) -> Precedence p a a -> Precedence p a b
fmap a -> b
f (Precedence Int -> p a a
h) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((a -> b) -> p a a -> p a b
forall a b. (a -> b) -> p a a -> p a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (p a a -> p a b) -> (Int -> p a a) -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a a
h)
instance Applicative (p a) => Applicative (Precedence p a) where
  pure :: forall a. a -> Precedence p a a
pure a
x = (Int -> p a a) -> Precedence p a a
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a a -> Int -> p a a
forall a b. a -> b -> a
const (a -> p a a
forall a. a -> p a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x))
  Precedence Int -> p a (a -> b)
f <*> :: forall a b.
Precedence p a (a -> b) -> Precedence p a a -> Precedence p a b
<*> Precedence Int -> p a a
x = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> Int -> p a (a -> b)
f Int
d p a (a -> b) -> p a a -> p a b
forall a b. p a (a -> b) -> p a a -> p a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> p a a
x Int
d)
instance Alternative (p a) => Alternative (Precedence p a) where
  empty :: forall a. Precedence p a a
empty = (Int -> p a a) -> Precedence p a a
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a a -> Int -> p a a
forall a b. a -> b -> a
const p a a
forall a. p a a
forall (f :: * -> *) a. Alternative f => f a
empty)
  Precedence Int -> p a a
x <|> :: forall a. Precedence p a a -> Precedence p a a -> Precedence p a a
<|> Precedence Int -> p a a
y = (Int -> p a a) -> Precedence p a a
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> Int -> p a a
x Int
d p a a -> p a a -> p a a
forall a. p a a -> p a a -> p a a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Int -> p a a
y Int
d)
  many :: forall a. Precedence p a a -> Precedence p a [a]
many (Precedence Int -> p a a
x) = (Int -> p a [a]) -> Precedence p a [a]
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a a -> p a [a]
forall a. p a a -> p a [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (p a a -> p a [a]) -> (Int -> p a a) -> Int -> p a [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a a
x)
  some :: forall a. Precedence p a a -> Precedence p a [a]
some (Precedence Int -> p a a
x) = (Int -> p a [a]) -> Precedence p a [a]
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a a -> p a [a]
forall a. p a a -> p a [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (p a a -> p a [a]) -> (Int -> p a a) -> Int -> p a [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a a
x)
instance Choice p => Choice (Precedence p) where
  left' :: forall a b c.
Precedence p a b -> Precedence p (Either a c) (Either b c)
left' (Precedence Int -> p a b
h) = (Int -> p (Either a c) (Either b c))
-> Precedence p (Either a c) (Either b c)
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> p (Either a c) (Either b c)
forall a b c. p a b -> p (Either a c) (Either b c)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either a c) (Either b c)
left' (p a b -> p (Either a c) (Either b c))
-> (Int -> p a b) -> Int -> p (Either a c) (Either b c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
h)
  right' :: forall a b c.
Precedence p a b -> Precedence p (Either c a) (Either c b)
right' (Precedence Int -> p a b
h) = (Int -> p (Either c a) (Either c b))
-> Precedence p (Either c a) (Either c b)
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> p (Either c a) (Either c b)
forall a b c. p a b -> p (Either c a) (Either c b)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right' (p a b -> p (Either c a) (Either c b))
-> (Int -> p a b) -> Int -> p (Either c a) (Either c b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
h)
instance DisjunctiveMonoid (p a b) => DisjunctiveMonoid (Precedence p a b) where
  falseP :: Precedence p a b
falseP = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> Int -> p a b
forall a b. a -> b -> a
const p a b
forall a. DisjunctiveMonoid a => a
falseP)
  Precedence Int -> p a b
x >|< :: Precedence p a b -> Precedence p a b -> Precedence p a b
>|< Precedence Int -> p a b
y = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> Int -> p a b
x Int
d p a b -> p a b -> p a b
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Int -> p a b
y Int
d)
instance CocartesianMonoidal p => CocartesianMonoidal (Precedence p) where
  zeroP :: Precedence p Void Void
zeroP = (Int -> p Void Void) -> Precedence p Void Void
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p Void Void -> Int -> p Void Void
forall a b. a -> b -> a
const p Void Void
forall (p :: * -> * -> *). CocartesianMonoidal p => p Void Void
zeroP)
  Precedence Int -> p a b
x >+< :: forall a b c d.
Precedence p a b
-> Precedence p c d -> Precedence p (Either a c) (Either b d)
>+< Precedence Int -> p c d
y = (Int -> p (Either a c) (Either b d))
-> Precedence p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> Int -> p a b
x Int
d p a b -> p c d -> p (Either a c) (Either b d)
forall a b c d. p a b -> p c d -> p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b c d.
CocartesianMonoidal p =>
p a b -> p c d -> p (Either a c) (Either b d)
>+< Int -> p c d
y Int
d)
instance CocartesianChoice p => CocartesianChoice (Precedence p) where
  alternate :: forall a b c d.
Either (Precedence p a b) (Precedence p c d)
-> Precedence p (Either a c) (Either b d)
alternate = \case
    Left (Precedence Int -> p a b
x) -> (Int -> p (Either a c) (Either b d))
-> Precedence p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (Either (p a b) (p c d) -> p (Either a c) (Either b d)
forall a b c d.
Either (p a b) (p c d) -> p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b c d.
CocartesianChoice p =>
Either (p a b) (p c d) -> p (Either a c) (Either b d)
alternate (Either (p a b) (p c d) -> p (Either a c) (Either b d))
-> (Int -> Either (p a b) (p c d))
-> Int
-> p (Either a c) (Either b d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Either (p a b) (p c d)
forall a b. a -> Either a b
Left (p a b -> Either (p a b) (p c d))
-> (Int -> p a b) -> Int -> Either (p a b) (p c d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
    Right (Precedence Int -> p c d
y) -> (Int -> p (Either a c) (Either b d))
-> Precedence p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (Either (p a b) (p c d) -> p (Either a c) (Either b d)
forall a b c d.
Either (p a b) (p c d) -> p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b c d.
CocartesianChoice p =>
Either (p a b) (p c d) -> p (Either a c) (Either b d)
alternate (Either (p a b) (p c d) -> p (Either a c) (Either b d))
-> (Int -> Either (p a b) (p c d))
-> Int
-> p (Either a c) (Either b d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p c d -> Either (p a b) (p c d)
forall a b. b -> Either a b
Right (p c d -> Either (p a b) (p c d))
-> (Int -> p c d) -> Int -> Either (p a b) (p c d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p c d
y)
instance Distributor p => Distributor (Precedence p) where
  optionalP :: forall a b. Precedence p a b -> Precedence p (Maybe a) (Maybe b)
optionalP (Precedence Int -> p a b
x) = (Int -> p (Maybe a) (Maybe b)) -> Precedence p (Maybe a) (Maybe b)
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> p (Maybe a) (Maybe b)
forall a b. p a b -> p (Maybe a) (Maybe b)
forall (p :: * -> * -> *) a b.
Distributor p =>
p a b -> p (Maybe a) (Maybe b)
optionalP (p a b -> p (Maybe a) (Maybe b))
-> (Int -> p a b) -> Int -> p (Maybe a) (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
  manyP :: forall a b. Precedence p a b -> Precedence p [a] [b]
manyP (Precedence Int -> p a b
x) = (Int -> p [a] [b]) -> Precedence p [a] [b]
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> p [a] [b]
forall a b. p a b -> p [a] [b]
forall (p :: * -> * -> *) a b. Distributor p => p a b -> p [a] [b]
manyP (p a b -> p [a] [b]) -> (Int -> p a b) -> Int -> p [a] [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
instance Alternator p => Alternator (Precedence p) where
  someP :: forall a b. Precedence p a b -> Precedence p [a] [b]
someP (Precedence Int -> p a b
x) = (Int -> p [a] [b]) -> Precedence p [a] [b]
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> p [a] [b]
forall a b. p a b -> p [a] [b]
forall (p :: * -> * -> *) a b. Alternator p => p a b -> p [a] [b]
someP (p a b -> p [a] [b]) -> (Int -> p a b) -> Int -> p [a] [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
  optionP :: forall a b.
APrism a b () () -> Precedence p a b -> Precedence p a b
optionP APrism a b () ()
def (Precedence Int -> p a b
x) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (APrism a b () () -> p a b -> p a b
forall a b. APrism a b () () -> p a b -> p a b
forall (p :: * -> * -> *) a b.
Alternator p =>
APrism a b () () -> p a b -> p a b
optionP APrism a b () ()
def (p a b -> p a b) -> (Int -> p a b) -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
  manyTillP :: forall a b.
Precedence p a b -> Precedence p [a] [b] -> Precedence p [a] [b]
manyTillP (Precedence Int -> p a b
x) (Precedence Int -> p [a] [b]
y) = (Int -> p [a] [b]) -> Precedence p [a] [b]
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> p a b -> p [a] [b] -> p [a] [b]
forall a b. p a b -> p [a] [b] -> p [a] [b]
forall (p :: * -> * -> *) a b.
Alternator p =>
p a b -> p [a] [b] -> p [a] [b]
manyTillP (Int -> p a b
x Int
d) (Int -> p [a] [b]
y Int
d))
  quantP :: forall a b.
Int -> Maybe Int -> Precedence p a b -> Precedence p [a] [b]
quantP Int
lo Maybe Int
hi (Precedence Int -> p a b
x) = (Int -> p [a] [b]) -> Precedence p [a] [b]
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (Int -> Maybe Int -> p a b -> p [a] [b]
forall a b. Int -> Maybe Int -> p a b -> p [a] [b]
forall (p :: * -> * -> *) a b.
Alternator p =>
Int -> Maybe Int -> p a b -> p [a] [b]
quantP Int
lo Maybe Int
hi (p a b -> p [a] [b]) -> (Int -> p a b) -> Int -> p [a] [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
instance TerminalSymbol token (p a b) => TerminalSymbol token (Precedence p a b) where
  terminal :: [token] -> Precedence p a b
terminal = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> ([token] -> Int -> p a b) -> [token] -> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> ([token] -> p a b) -> [token] -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [token] -> p a b
forall token s. TerminalSymbol token s => [token] -> s
terminal
instance Tokenized token (p a b) => Tokenized token (Precedence p a b) where
  anyToken :: Precedence p a b
anyToken = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> Int -> p a b
forall a b. a -> b -> a
const p a b
forall token p. Tokenized token p => p
anyToken)
  token :: token -> Precedence p a b
token = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> (token -> Int -> p a b) -> token -> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> (token -> p a b) -> token -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> p a b
forall token p. Tokenized token p => token -> p
token
  oneOf :: forall (f :: * -> *). Foldable f => f token -> Precedence p a b
oneOf = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> (f token -> Int -> p a b) -> f token -> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> (f token -> p a b) -> f token -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f token -> p a b
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f token -> p a b
oneOf
  notOneOf :: forall (f :: * -> *). Foldable f => f token -> Precedence p a b
notOneOf = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> (f token -> Int -> p a b) -> f token -> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> (f token -> p a b) -> f token -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f token -> p a b
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f token -> p a b
notOneOf
  asIn :: Categorize token -> Precedence p a b
asIn = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> (Categorize token -> Int -> p a b)
-> Categorize token
-> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> (Categorize token -> p a b) -> Categorize token -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Categorize token -> p a b
forall token p. Tokenized token p => Categorize token -> p
asIn
  notAsIn :: Categorize token -> Precedence p a b
notAsIn = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> (Categorize token -> Int -> p a b)
-> Categorize token
-> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> (Categorize token -> p a b) -> Categorize token -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Categorize token -> p a b
forall token p. Tokenized token p => Categorize token -> p
notAsIn
instance TokenAlgebra token (p a b) => TokenAlgebra token (Precedence p a b) where
  tokenClass :: TokenClass token -> Precedence p a b
tokenClass = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> (TokenClass token -> Int -> p a b)
-> TokenClass token
-> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> (TokenClass token -> p a b) -> TokenClass token -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TokenClass token -> p a b
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass
instance Tried (p a b) => Tried (Precedence p a b) where
  try :: Precedence p a b -> Precedence p a b
try (Precedence Int -> p a b
x) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> p a b
forall p. Tried p => p -> p
try (p a b -> p a b) -> (Int -> p a b) -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
instance Ruled (p a b) => Ruled (Precedence p a b) where
  rule :: [Char] -> Precedence p a b -> Precedence p a b
rule [Char]
name (Precedence Int -> p a b
x) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ([Char] -> p a b -> p a b
forall p. Ruled p => [Char] -> p -> p
rule [Char]
name (p a b -> p a b) -> (Int -> p a b) -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> p a b
x)
instance BackusNaurForm (p a b) => BackusNaurForm (Precedence p a b)
instance (a ~ (), b ~ (), TerminalSymbol Char (p () ()))
  => IsString (Precedence p a b) where
    fromString :: [Char] -> Precedence p a b
fromString = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence ((Int -> p a b) -> Precedence p a b)
-> ([Char] -> Int -> p a b) -> [Char] -> Precedence p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> Int -> p a b
forall a b. a -> b -> a
const (p a b -> Int -> p a b)
-> ([Char] -> p a b) -> [Char] -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> p a b
forall token s. TerminalSymbol token s => [token] -> s
terminal
instance (Grammatical Char p, NotFollowedBy p)
  => PrecGrammatical (Precedence p) where
    precP :: forall a b. Int -> Precedence p a b -> Precedence p a b
precP Int
n (Precedence Int -> p a b
x) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> if Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n then Int -> p a b
x Int
n else p a b
forall a. DisjunctiveMonoid a => a
falseP)
    precStep :: forall a b. Precedence p a b -> Precedence p a b
precStep (Precedence Int -> p a b
x) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (Int -> p a b
x (Int -> p a b) -> (Int -> Int) -> Int -> p a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
    precReset :: forall a b. Precedence p a b -> Precedence p a b
precReset (Precedence Int -> p a b
x) = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (p a b -> Int -> p a b
forall a b. a -> b -> a
const (Int -> p a b
x Int
0))
    precWith :: forall a b. (Int -> Precedence p a b) -> Precedence p a b
precWith Int -> Precedence p a b
k = (Int -> p a b) -> Precedence p a b
forall (p :: * -> * -> *) a b. (Int -> p a b) -> Precedence p a b
Precedence (\Int
d -> Precedence p a b -> Int -> p a b
forall (p :: * -> * -> *) a b. Precedence p a b -> Int -> p a b
runPrecedence (Int -> Precedence p a b
k Int
d) Int
d)

{- | A type's `PrecGrammar` at a precedence, at any `Grammatical`
interpreter which parses or prints. -}
precG :: forall a p. (PrecGrammar a, Grammatical Char p, NotFollowedBy p) => Int -> p a a
precG :: forall a (p :: * -> * -> *).
(PrecGrammar a, Grammatical Char p, NotFollowedBy p) =>
Int -> p a a
precG = Precedence p a a -> Int -> p a a
forall (p :: * -> * -> *) a b. Precedence p a b -> Int -> p a b
runPrecedence Precedence p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p a a
precGrammar

{- | Read at a precedence, as `readsPrec` does. -}
readsPrecG :: forall a. PrecGrammar a => Int -> ReadS a
readsPrecG :: forall a. PrecGrammar a => Int -> ReadS a
readsPrecG Int
d = Parsor [Char] [] a a -> [Char] -> [(a, [Char])]
forall s (f :: * -> *) a b. Parsor s f a b -> s -> f (b, s)
parseP (Int -> Parsor [Char] [] a a
forall a (p :: * -> * -> *).
(PrecGrammar a, Grammatical Char p, NotFollowedBy p) =>
Int -> p a a
precG Int
d :: Parsor String [] a a)

{- | Show at a precedence, as `showsPrec` does, if it shows. -}
showsPrecG :: forall a. PrecGrammar a => Int -> a -> Maybe ShowS
showsPrecG :: forall a. PrecGrammar a => Int -> a -> Maybe ShowS
showsPrecG Int
d = Printor [Char] Maybe a a -> a -> Maybe ShowS
forall (f :: * -> *) s a b.
Functor f =>
Printor s f a b -> a -> f (s -> s)
printP (Int -> Printor [Char] Maybe a a
forall a (p :: * -> * -> *).
(PrecGrammar a, Grammatical Char p, NotFollowedBy p) =>
Int -> p a a
precG Int
d :: Printor String Maybe a a)

-- Base types --

{- The base types' grammars, equivalent to their `Read` & `Show`
instances: lexemes, within any parentheses, as `read` accepts them, &
negative numbers, which `showsPrec` parenthesizes above 6, & `read`
accepts bare at any precedence. -}

instance PrecGrammar Char where
  precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Char Char
precGrammar = p Char Char -> p Char Char
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens p Char Char
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Char Char
precChar
  -- strings, or brackets, as `read` reads, & strings, as `show` shows
  precListGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p [Char] [Char]
precListGrammar = p [Char] [Char] -> p [Char] [Char]
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens (p [Char] [Char]
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p [Char] [Char]
precString p [Char] [Char] -> p [Char] [Char] -> p [Char] [Char]
forall a. DisjunctiveMonoid a => a -> a -> a
>|< p [Char] [Char] -> p [Char] [Char]
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParenthesized (p Char Char -> p [Char] [Char]
forall (p :: * -> * -> *) a.
PrecGrammatical p =>
p a a -> p [a] [a]
precBrackets p Char Char
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p Char Char
precGrammar))
instance PrecGrammar a => PrecGrammar [a] where
  precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p [a] [a]
precGrammar = p [a] [a]
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p [a] [a]
forall (p :: * -> * -> *). PrecGrammatical p => p [a] [a]
precListGrammar
instance PrecGrammar Bool
instance PrecGrammar Ordering
instance PrecGrammar a => PrecGrammar (Maybe a)
instance (PrecGrammar a, PrecGrammar b) => PrecGrammar (Either a b)
instance PrecGrammar () where
  precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precGrammar = p () () -> p () ()
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"(" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
")")
instance (PrecGrammar a, PrecGrammar b) => PrecGrammar (a, b) where
  precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p (a, b) (a, b)
precGrammar = p (a, b) (a, b) -> p (a, b) (a, b)
forall (p :: * -> * -> *) a. PrecGrammatical p => p a a -> p a a
precTupled (p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p a a -> p b b -> p (a, b) (a, b)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p b b -> p b b
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p b b
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem)
instance (PrecGrammar a, PrecGrammar b, PrecGrammar c) => PrecGrammar (a, b, c) where
  precGrammar :: forall (p :: * -> * -> *).
PrecGrammatical p =>
p (a, b, c) (a, b, c)
precGrammar = p (a, b, c) (a, b, c) -> p (a, b, c) (a, b, c)
forall (p :: * -> * -> *) a. PrecGrammatical p => p a a -> p a a
precTupled (p (a, b, c) (a, b, c) -> p (a, b, c) (a, b, c))
-> p (a, b, c) (a, b, c) -> p (a, b, c) (a, b, c)
forall a b. (a -> b) -> a -> b
$ ((a, b, c) -> (a, (b, c)))
-> ((a, (b, c)) -> (a, b, c))
-> Iso (a, b, c) (a, b, c) (a, (b, c)) (a, (b, c))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(a
a, b
b, c
c) -> (a
a, (b
b, c
c))) (\(a
a, (b
b, c
c)) -> (a
a, b
b, c
c))
    (Exchange
   (a, (b, c)) (a, (b, c)) (a, (b, c)) (Identity (a, (b, c)))
 -> Exchange (a, (b, c)) (a, (b, c)) (a, b, c) (Identity (a, b, c)))
-> p (a, (b, c)) (a, (b, c)) -> p (a, b, c) (a, b, c)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p a a -> p (b, c) (b, c) -> p (a, (b, c)) (a, (b, c))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p b b -> p b b
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p b b
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p b b -> p c c -> p (b, c) (b, c)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p c c -> p c c
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p c c
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem
instance (PrecGrammar a, PrecGrammar b, PrecGrammar c, PrecGrammar d)
  => PrecGrammar (a, b, c, d) where
    precGrammar :: forall (p :: * -> * -> *).
PrecGrammatical p =>
p (a, b, c, d) (a, b, c, d)
precGrammar = p (a, b, c, d) (a, b, c, d) -> p (a, b, c, d) (a, b, c, d)
forall (p :: * -> * -> *) a. PrecGrammatical p => p a a -> p a a
precTupled (p (a, b, c, d) (a, b, c, d) -> p (a, b, c, d) (a, b, c, d))
-> p (a, b, c, d) (a, b, c, d) -> p (a, b, c, d) (a, b, c, d)
forall a b. (a -> b) -> a -> b
$ ((a, b, c, d) -> (a, (b, (c, d))))
-> ((a, (b, (c, d))) -> (a, b, c, d))
-> Iso (a, b, c, d) (a, b, c, d) (a, (b, (c, d))) (a, (b, (c, d)))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
      (\(a
a, b
b, c
c, d
d) -> (a
a, (b
b, (c
c, d
d)))) (\(a
a, (b
b, (c
c, d
d))) -> (a
a, b
b, c
c, d
d))
      (Exchange
   (a, (b, (c, d)))
   (a, (b, (c, d)))
   (a, (b, (c, d)))
   (Identity (a, (b, (c, d))))
 -> Exchange
      (a, (b, (c, d)))
      (a, (b, (c, d)))
      (a, b, c, d)
      (Identity (a, b, c, d)))
-> p (a, (b, (c, d))) (a, (b, (c, d)))
-> p (a, b, c, d) (a, b, c, d)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p a a
-> p (b, (c, d)) (b, (c, d)) -> p (a, (b, (c, d))) (a, (b, (c, d)))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p b b -> p b b
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p b b
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p b b -> p (c, d) (c, d) -> p (b, (c, d)) (b, (c, d))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p c c -> p c c
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p c c
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p c c -> p d d -> p (c, d) (c, d)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p d d -> p d d
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p d d
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem
instance (PrecGrammar a, PrecGrammar b, PrecGrammar c, PrecGrammar d, PrecGrammar e)
  => PrecGrammar (a, b, c, d, e) where
    precGrammar :: forall (p :: * -> * -> *).
PrecGrammatical p =>
p (a, b, c, d, e) (a, b, c, d, e)
precGrammar = p (a, b, c, d, e) (a, b, c, d, e)
-> p (a, b, c, d, e) (a, b, c, d, e)
forall (p :: * -> * -> *) a. PrecGrammatical p => p a a -> p a a
precTupled (p (a, b, c, d, e) (a, b, c, d, e)
 -> p (a, b, c, d, e) (a, b, c, d, e))
-> p (a, b, c, d, e) (a, b, c, d, e)
-> p (a, b, c, d, e) (a, b, c, d, e)
forall a b. (a -> b) -> a -> b
$ ((a, b, c, d, e) -> (a, (b, (c, (d, e)))))
-> ((a, (b, (c, (d, e)))) -> (a, b, c, d, e))
-> Iso
     (a, b, c, d, e)
     (a, b, c, d, e)
     (a, (b, (c, (d, e))))
     (a, (b, (c, (d, e))))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
      (\(a
a, b
b, c
c, d
d, e
e) -> (a
a, (b
b, (c
c, (d
d, e
e)))))
      (\(a
a, (b
b, (c
c, (d
d, e
e)))) -> (a
a, b
b, c
c, d
d, e
e))
      (Exchange
   (a, (b, (c, (d, e))))
   (a, (b, (c, (d, e))))
   (a, (b, (c, (d, e))))
   (Identity (a, (b, (c, (d, e)))))
 -> Exchange
      (a, (b, (c, (d, e))))
      (a, (b, (c, (d, e))))
      (a, b, c, d, e)
      (Identity (a, b, c, d, e)))
-> p (a, (b, (c, (d, e)))) (a, (b, (c, (d, e))))
-> p (a, b, c, d, e) (a, b, c, d, e)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p a a
-> p (b, (c, (d, e))) (b, (c, (d, e)))
-> p (a, (b, (c, (d, e)))) (a, (b, (c, (d, e))))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p b b -> p b b
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p b b
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p b b
-> p (c, (d, e)) (c, (d, e)) -> p (b, (c, (d, e))) (b, (c, (d, e)))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p c c -> p c c
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p c c
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem p c c -> p (d, e) (d, e) -> p (c, (d, e)) (c, (d, e))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p d d -> p d d
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p d d
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem
        p d d -> p e e -> p (d, e) (d, e)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma p () () -> p e e -> p e e
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p e e
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem

-- A tuple, within parentheses, its items at precedence 0, as `read`'s
-- @wrap_tup@ reads & `show` shows.
precTupled :: PrecGrammatical p => p a a -> p a a
precTupled :: forall (p :: * -> * -> *) a. PrecGrammatical p => p a a -> p a a
precTupled p a a
items' = p a a -> p a a
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"(" p () () -> p a a -> p a a
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p a a
items' p a a -> p () () -> p a a
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p a a -> p () () -> p a a
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
")")

precItem :: (PrecGrammar a, PrecGrammatical p) => p a a
precItem :: forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precItem = p a a -> p a a
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precReset p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p a a
precGrammar

precComma :: PrecGrammatical p => p () ()
precComma :: forall (p :: * -> * -> *). PrecGrammatical p => p () ()
precComma = p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
","

-- Generics --

{- A sum of constructors, as derived `Read` & `Show` instances read & show
them, by `GHC.Generics`. -}
class GPrecSum f where
  gPrecSum :: PrecGrammatical p => p (f x) (f x)
instance GPrecSum f => GPrecSum (M1 D c f) where
  gPrecSum :: forall (p :: * -> * -> *) x.
PrecGrammatical p =>
p (M1 D c f x) (M1 D c f x)
gPrecSum = (M1 D c f x -> f x)
-> (f x -> M1 D c f x) -> Iso (M1 D c f x) (M1 D c f x) (f x) (f x)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso M1 D c f x -> f x
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1 f x -> M1 D c f x
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Exchange (f x) (f x) (f x) (Identity (f x))
 -> Exchange (f x) (f x) (M1 D c f x) (Identity (M1 D c f x)))
-> p (f x) (f x) -> p (M1 D c f x) (M1 D c f x)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecSum f, PrecGrammatical p) =>
p (f x) (f x)
forall (p :: * -> * -> *) x. PrecGrammatical p => p (f x) (f x)
gPrecSum
instance (GPrecSum f, GPrecSum g) => GPrecSum (f :+: g) where
  gPrecSum :: forall (p :: * -> * -> *) x.
PrecGrammatical p =>
p ((:+:) f g x) ((:+:) f g x)
gPrecSum = ((:+:) f g x -> Either (f x) (g x))
-> (Either (f x) (g x) -> (:+:) f g x)
-> Iso
     ((:+:) f g x)
     ((:+:) f g x)
     (Either (f x) (g x))
     (Either (f x) (g x))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
    (\case L1 f x
x -> f x -> Either (f x) (g x)
forall a b. a -> Either a b
Left f x
x; R1 g x
y -> g x -> Either (f x) (g x)
forall a b. b -> Either a b
Right g x
y)
    ((f x -> (:+:) f g x)
-> (g x -> (:+:) f g x) -> Either (f x) (g x) -> (:+:) f g x
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either f x -> (:+:) f g x
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 g x -> (:+:) f g x
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1)
    (Exchange
   (Either (f x) (g x))
   (Either (f x) (g x))
   (Either (f x) (g x))
   (Identity (Either (f x) (g x)))
 -> Exchange
      (Either (f x) (g x))
      (Either (f x) (g x))
      ((:+:) f g x)
      (Identity ((:+:) f g x)))
-> p (Either (f x) (g x)) (Either (f x) (g x))
-> p ((:+:) f g x) ((:+:) f g x)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecSum f, PrecGrammatical p) =>
p (f x) (f x)
forall (p :: * -> * -> *) x. PrecGrammatical p => p (f x) (f x)
gPrecSum p (f x) (f x)
-> p (g x) (g x) -> p (Either (f x) (g x)) (Either (f x) (g x))
forall a b c d. p a b -> p c d -> p (Either a c) (Either b d)
forall (p :: * -> * -> *) a b c d.
CocartesianMonoidal p =>
p a b -> p c d -> p (Either a c) (Either b d)
>+< p (g x) (g x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecSum f, PrecGrammatical p) =>
p (f x) (f x)
forall (p :: * -> * -> *) x. PrecGrammatical p => p (g x) (g x)
gPrecSum
instance GPrecSum V1 where
  gPrecSum :: forall (p :: * -> * -> *) x. PrecGrammatical p => p (V1 x) (V1 x)
gPrecSum = p (V1 x) (V1 x)
forall a. DisjunctiveMonoid a => a
falseP
instance (Constructor c, GPrecFields f) => GPrecSum (M1 C c f) where
  gPrecSum :: forall (p :: * -> * -> *) x.
PrecGrammatical p =>
p (M1 C c f x) (M1 C c f x)
gPrecSum = (M1 C c f x -> f x)
-> (f x -> M1 C c f x) -> Iso (M1 C c f x) (M1 C c f x) (f x) (f x)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso M1 C c f x -> f x
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1 f x -> M1 C c f x
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Exchange (f x) (f x) (f x) (Identity (f x))
 -> Exchange (f x) (f x) (M1 C c f x) (Identity (M1 C c f x)))
-> p (f x) (f x) -> p (M1 C c f x) (M1 C c f x)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ case M1 C c f () -> Fixity
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> Fixity
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> Fixity
conFixity M1 C c f ()
con of
    Infix Associativity
_ Int
n -> Int -> p (f x) (f x) -> p (f x) (f x)
forall a b. Int -> p a b -> p a b
forall (p :: * -> * -> *) a b.
PrecGrammatical p =>
Int -> p a b -> p a b
precP Int
n (FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields (p () () -> FieldsStyle p
forall (p :: * -> * -> *). p () () -> FieldsStyle p
InfixFields ([Char] -> p () ()
forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precOperator [Char]
name)))
    Fixity
Prefix
      | M1 C c f () -> Bool
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> Bool
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> Bool
conIsRecord M1 C c f ()
con -> (Int -> p (f x) (f x)) -> p (f x) (f x)
forall a b. (Int -> p a b) -> p a b
forall (p :: * -> * -> *) a b.
PrecGrammatical p =>
(Int -> p a b) -> p a b
precWith ((Int -> p (f x) (f x)) -> p (f x) (f x))
-> (Int -> p (f x) (f x)) -> p (f x) (f x)
forall a b. (a -> b) -> a -> b
$ \Int
d ->
          let body :: p (f x) (f x)
body = [Char] -> p () ()
forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precName [Char]
name p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpaced p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"{" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces
                p () () -> p (f x) (f x) -> p (f x) (f x)
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
RecordFields p (f x) (f x) -> p () () -> p (f x) (f x)
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p (f x) (f x) -> p () () -> p (f x) (f x)
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"}"
          in if Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
10 then p (f x) (f x)
body else if Int
d Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
11 then Market (f x) (f x) (f x) (Identity (f x))
-> Market (f x) (f x) (f x) (Identity (f x))
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market (f x) (f x) (f x) (Identity (f x))
 -> Market (f x) (f x) (f x) (Identity (f x)))
-> p (f x) (f x) -> p (f x) (f x)
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p (f x) (f x)
body else p (f x) (f x)
forall a. DisjunctiveMonoid a => a
falseP
      | Proxy f -> Int
forall (f :: * -> *). GPrecFields f => Proxy f -> Int
gArity (forall {k} (t :: k). Proxy t
forall (t :: * -> *). Proxy t
Proxy @f) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 -> [Char] -> p () ()
forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precName [Char]
name p () () -> p (f x) (f x) -> p (f x) (f x)
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
PrefixFields
      | Bool
otherwise -> Int -> p (f x) (f x) -> p (f x) (f x)
forall a b. Int -> p a b -> p a b
forall (p :: * -> * -> *) a b.
PrecGrammatical p =>
Int -> p a b -> p a b
precP Int
10 ([Char] -> p () ()
forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precName [Char]
name p () () -> p (f x) (f x) -> p (f x) (f x)
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
PrefixFields)
    where
      con :: M1 C c f ()
con = M1 C c f ()
forall a. HasCallStack => a
undefined :: M1 C c f ()
      name :: [Char]
name = M1 C c f () -> [Char]
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> [Char]
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> [Char]
conName M1 C c f ()
con

-- How a constructor's fields are written.
data FieldsStyle p
  = PrefixFields
  | BareField
  | RecordFields
  | InfixFields (p () ())

{- The fields of a constructor: prefix, after spaces, at the next precedence;
in records, named, at precedence 0, separated by commas; or infix, about an
operator, at the next precedence. -}
class GPrecFields f where
  gPrecFields :: PrecGrammatical p => FieldsStyle p -> p (f x) (f x)
  gArity :: Proxy f -> Int
instance GPrecFields U1 where
  gPrecFields :: forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (U1 x) (U1 x)
gPrecFields FieldsStyle p
_ = (U1 x -> ()) -> (() -> U1 x) -> Iso (U1 x) (U1 x) () ()
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (() -> U1 x -> ()
forall a b. a -> b -> a
const ()) (U1 x -> () -> U1 x
forall a b. a -> b -> a
const U1 x
forall k (p :: k). U1 p
U1) (Exchange () () () (Identity ())
 -> Exchange () () (U1 x) (Identity (U1 x)))
-> p () () -> p (U1 x) (U1 x)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP
  gArity :: Proxy U1 -> Int
gArity Proxy U1
_ = Int
0
instance (GPrecFields f, GPrecFields g) => GPrecFields (f :*: g) where
  gPrecFields :: forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p ((:*:) f g x) ((:*:) f g x)
gPrecFields FieldsStyle p
style = ((:*:) f g x -> (f x, g x))
-> ((f x, g x) -> (:*:) f g x)
-> Iso ((:*:) f g x) ((:*:) f g x) (f x, g x) (f x, g x)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(f x
x :*: g x
y) -> (f x
x, g x
y)) ((f x -> g x -> (:*:) f g x) -> (f x, g x) -> (:*:) f g x
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry f x -> g x -> (:*:) f g x
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:)) (Exchange (f x, g x) (f x, g x) (f x, g x) (Identity (f x, g x))
 -> Exchange
      (f x, g x) (f x, g x) ((:*:) f g x) (Identity ((:*:) f g x)))
-> p (f x, g x) (f x, g x) -> p ((:*:) f g x) ((:*:) f g x)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ case FieldsStyle p
style of
    FieldsStyle p
RecordFields ->
      FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
RecordFields p (f x) (f x) -> p (g x) (g x) -> p (f x, g x) (f x, g x)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"," p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpaced p () () -> p (g x) (g x) -> p (g x) (g x)
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* FieldsStyle p -> p (g x) (g x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (g x) (g x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
RecordFields
    InfixFields p () ()
operator -> FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
BareField p (f x) (f x) -> p (g x) (g x) -> p (f x, g x) (f x, g x)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p () ()
operator p () () -> p (g x) (g x) -> p (g x) (g x)
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* FieldsStyle p -> p (g x) (g x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (g x) (g x)
gPrecFields FieldsStyle p
forall (p :: * -> * -> *). FieldsStyle p
BareField
    FieldsStyle p
_ -> FieldsStyle p -> p (f x) (f x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (f x) (f x)
gPrecFields FieldsStyle p
style p (f x) (f x) -> p (g x) (g x) -> p (f x, g x) (f x, g x)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< FieldsStyle p -> p (g x) (g x)
forall (f :: * -> *) (p :: * -> * -> *) x.
(GPrecFields f, PrecGrammatical p) =>
FieldsStyle p -> p (f x) (f x)
forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (g x) (g x)
gPrecFields FieldsStyle p
style
  gArity :: Proxy (f :*: g) -> Int
gArity Proxy (f :*: g)
_ = Proxy f -> Int
forall (f :: * -> *). GPrecFields f => Proxy f -> Int
gArity (forall {k} (t :: k). Proxy t
forall (t :: * -> *). Proxy t
Proxy @f) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Proxy g -> Int
forall (f :: * -> *). GPrecFields f => Proxy f -> Int
gArity (forall {k} (t :: k). Proxy t
forall (t :: * -> *). Proxy t
Proxy @g)
instance (Selector s, PrecGrammar a) => GPrecFields (M1 S s (K1 R a)) where
  gPrecFields :: forall (p :: * -> * -> *) x.
PrecGrammatical p =>
FieldsStyle p -> p (M1 S s (K1 R a) x) (M1 S s (K1 R a) x)
gPrecFields FieldsStyle p
style = (M1 S s (K1 R a) x -> a)
-> (a -> M1 S s (K1 R a) x)
-> Iso (M1 S s (K1 R a) x) (M1 S s (K1 R a) x) a a
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (K1 R a x -> a
forall k i c (p :: k). K1 i c p -> c
unK1 (K1 R a x -> a)
-> (M1 S s (K1 R a) x -> K1 R a x) -> M1 S s (K1 R a) x -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 S s (K1 R a) x -> K1 R a x
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1) (K1 R a x -> M1 S s (K1 R a) x
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (K1 R a x -> M1 S s (K1 R a) x)
-> (a -> K1 R a x) -> a -> M1 S s (K1 R a) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> K1 R a x
forall k i c (p :: k). c -> K1 i c p
K1) (Exchange a a a (Identity a)
 -> Exchange a a (M1 S s (K1 R a) x) (Identity (M1 S s (K1 R a) x)))
-> p a a -> p (M1 S s (K1 R a) x) (M1 S s (K1 R a) x)
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ case FieldsStyle p
style of
    FieldsStyle p
RecordFields -> [Char] -> p () ()
forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precName [Char]
field p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpaced p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precSymbol [Char]
"=" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpace p () () -> p a a -> p a a
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p a a -> p a a
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precReset p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p a a
precGrammar
    FieldsStyle p
BareField -> p a a -> p a a
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precStep p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p a a
precGrammar
    FieldsStyle p
_ -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpace p () () -> p a a -> p a a
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p a a -> p a a
forall a b. p a b -> p a b
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precStep p a a
forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
forall (p :: * -> * -> *). PrecGrammatical p => p a a
precGrammar
    where
      field :: [Char]
field = M1 S s (K1 R a) () -> [Char]
forall {k} (s :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Selector s =>
t s f a -> [Char]
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t s f a -> [Char]
selName (M1 S s (K1 R a) ()
forall a. HasCallStack => a
undefined :: M1 S s (K1 R a) ())
  gArity :: Proxy (M1 S s (K1 R a)) -> Int
gArity Proxy (M1 S s (K1 R a))
_ = Int
1

-- A name, an identifier, or a symbol within parentheses.
precName :: PrecGrammatical p => String -> p () ()
precName :: forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precName [Char]
name
  | [Char] -> Bool
precSymbolic [Char]
name =
      [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"(" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precSymbol [Char]
name p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
")"
  | Bool
otherwise = [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precIdent [Char]
name

-- An infix operator, a symbol, or an identifier within backquotes.
precOperator :: PrecGrammatical p => String -> p () ()
precOperator :: forall (p :: * -> * -> *). PrecGrammatical p => [Char] -> p () ()
precOperator [Char]
name
  | [Char] -> Bool
precSymbolic [Char]
name = p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpaced p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precSymbol [Char]
name p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpace
  | Bool
otherwise = p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpaced p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"`" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precIdent [Char]
name
      p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"`" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpace

precSymbolic :: String -> Bool
precSymbolic :: [Char] -> Bool
precSymbolic = \case
  Char
c : [Char]
_ -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':' Bool -> Bool -> Bool
|| TokenClass Char -> Char -> Bool
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
precSymbolClass Char
c
  [] -> Bool
False
instance PrecGrammar Natural where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Natural Natural
precGrammar = p Natural Natural -> p Natural Natural
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens p Natural Natural
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Natural Natural
precNatural
instance PrecGrammar Integer where
  precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Integer Integer
precGrammar = p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens ((Int -> p Integer Integer) -> p Integer Integer
forall a b. (Int -> p a b) -> p a b
forall (p :: * -> * -> *) a b.
PrecGrammatical p =>
(Int -> p a b) -> p a b
precWith (\Int
d -> Exchange
  (Bool, Integer)
  (Bool, Integer)
  (Bool, Integer)
  (Identity (Bool, Integer))
-> Exchange
     (Bool, Integer) (Bool, Integer) Integer (Identity Integer)
forall a. (Num a, Ord a) => Iso' a (Bool, a)
Iso' Integer (Bool, Integer)
precSigned (Exchange
   (Bool, Integer)
   (Bool, Integer)
   (Bool, Integer)
   (Identity (Bool, Integer))
 -> Exchange
      (Bool, Integer) (Bool, Integer) Integer (Identity Integer))
-> p (Bool, Integer) (Bool, Integer) -> p Integer Integer
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ Int -> p Bool Bool
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Int -> p Bool Bool
precNegativeAt Int
d p Bool Bool
-> p Integer Integer -> p (Bool, Integer) (Bool, Integer)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p Integer Integer
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Integer Integer
precMagnitude))
instance PrecGrammar Double where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Double Double
precGrammar = p Double Double -> p Double Double
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens ((Int -> p Double Double) -> p Double Double
forall a b. (Int -> p a b) -> p a b
forall (p :: * -> * -> *) a b.
PrecGrammatical p =>
(Int -> p a b) -> p a b
precWith Int -> p Double Double
forall a (p :: * -> * -> *).
(RealFloat a, RegGrammatical Char p, NotFollowedBy p) =>
Int -> p a a
precFloating)
instance PrecGrammar Float where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Float Float
precGrammar = p Float Float -> p Float Float
forall (p :: * -> * -> *) a b. PrecGrammatical p => p a b -> p a b
precParens ((Int -> p Float Float) -> p Float Float
forall a b. (Int -> p a b) -> p a b
forall (p :: * -> * -> *) a b.
PrecGrammatical p =>
(Int -> p a b) -> p a b
precWith Int -> p Float Float
forall a (p :: * -> * -> *).
(RealFloat a, RegGrammatical Char p, NotFollowedBy p) =>
Int -> p a a
precFloating)
instance PrecGrammar Int where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Int Int
precGrammar = p Int Int
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Int8 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Int8 Int8
precGrammar = p Int8 Int8
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Int16 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Int16 Int16
precGrammar = p Int16 Int16
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Int32 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Int32 Int32
precGrammar = p Int32 Int32
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Int64 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Int64 Int64
precGrammar = p Int64 Int64
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Word where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Word Word
precGrammar = p Word Word
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Word8 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Word8 Word8
precGrammar = p Word8 Word8
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Word16 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Word16 Word16
precGrammar = p Word16 Word16
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Word32 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Word32 Word32
precGrammar = p Word32 Word32
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral
instance PrecGrammar Word64 where precGrammar :: forall (p :: * -> * -> *). PrecGrammatical p => p Word64 Word64
precGrammar = p Word64 Word64
forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral

-- Sized integers read as `Integer`s, wrapping out of range, as `read` does.
precIntegral :: (Integral a, PrecGrammatical p) => p a a
precIntegral :: forall a (p :: * -> * -> *).
(Integral a, PrecGrammatical p) =>
p a a
precIntegral = (a -> Integer) -> (Integer -> a) -> Iso a a Integer Integer
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso a -> Integer
forall a. Integral a => a -> Integer
toInteger Integer -> a
forall a. Num a => Integer -> a
fromInteger (Exchange Integer Integer Integer (Identity Integer)
 -> Exchange Integer Integer a (Identity a))
-> p Integer Integer -> p a a
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ forall a (p :: * -> * -> *).
(PrecGrammar a, PrecGrammatical p) =>
p a a
precGrammar @Integer

-- A minus, printing bare up to precedence 6, & parsing bare at any.
precNegativeAt :: (RegGrammatical Char p, NotFollowedBy p) => Int -> p Bool Bool
precNegativeAt :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Int -> p Bool Bool
precNegativeAt Int
d
  | Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
6 = p Bool Bool
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Bool Bool
precNegative
  | Bool
otherwise = [p Bool Bool] -> p Bool Bool
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
      [ Bool -> Prism' Bool ()
forall a. Eq a => a -> Prism' a ()
only Bool
False (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> p () () -> p Bool Bool
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP
      , Bool -> Prism' Bool ()
forall a. Eq a => a -> Prism' a ()
only Bool
True (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> (Market () () () (Identity ()) -> Market () () () (Identity ()))
-> Market () () () (Identity ())
-> Market () () Bool (Identity Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Market () () () (Identity ()) -> Market () () () (Identity ())
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> p () () -> p Bool Bool
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"-" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces ]

-- A prism which never matches, so that its alternative parses, but never prints.
precUnprinted :: Prism' a a
precUnprinted :: forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted = (a -> a) -> (a -> Maybe a) -> Prism a a a a
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' a -> a
forall a. a -> a
id (Maybe a -> a -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing)

-- One of two terminals, printing the first, & parsing either.
precEitherOf :: RegGrammatical Char p => String -> String -> p () ()
precEitherOf :: forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
printed [Char]
parsed = [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
printed p () () -> p () () -> p () ()
forall a. DisjunctiveMonoid a => a -> a -> a
>|< (Market () () () (Identity ()) -> Market () () () (Identity ())
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () () (Identity ()) -> Market () () () (Identity ()))
-> p () () -> p () ()
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
parsed)

-- A minus, a lexeme of its own, as whether a number is negative.
precNegative :: (RegGrammatical Char p, NotFollowedBy p) => p Bool Bool
precNegative :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Bool Bool
precNegative = (Bool -> Maybe ())
-> (Maybe () -> Bool) -> Iso Bool Bool (Maybe ()) (Maybe ())
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\Bool
b -> if Bool
b then () -> Maybe ()
forall a. a -> Maybe a
Just () else Maybe ()
forall a. Maybe a
Nothing) Maybe () -> Bool
forall a. Maybe a -> Bool
isJust
  (Exchange (Maybe ()) (Maybe ()) (Maybe ()) (Identity (Maybe ()))
 -> Exchange (Maybe ()) (Maybe ()) Bool (Identity Bool))
-> p (Maybe ()) (Maybe ()) -> p Bool Bool
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p () () -> p (Maybe ()) (Maybe ())
forall a b. p a b -> p (Maybe a) (Maybe b)
forall (p :: * -> * -> *) a b.
Distributor p =>
p a b -> p (Maybe a) (Maybe b)
optionalP ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"-" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces)

-- Whitespace, by `Data.Char.isSpace`.
precSpaceClass :: TokenClass Char
precSpaceClass :: TokenClass Char
precSpaceClass = [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
oneOf [Char]
"\t\n\v\f\r" TokenClass Char -> TokenClass Char -> TokenClass Char
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Categorize Char -> TokenClass Char
forall token p. Tokenized token p => Categorize token -> p
asIn GeneralCategory
Categorize Char
Space

-- A token of a class, as unit, for lookahead.
precToken :: RegGrammatical Char p => TokenClass Char -> p () ()
precToken :: forall (p :: * -> * -> *).
RegGrammatical Char p =>
TokenClass Char -> p () ()
precToken TokenClass Char
cls = (() -> Char) -> (Char -> ()) -> p Char Char -> p () ()
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (Char -> () -> Char
forall a b. a -> b -> a
const Char
' ') (() -> Char -> ()
forall a b. a -> b -> a
const ()) (TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
cls)

-- The digits of a base, in either case.
precDigitClass :: Int -> TokenClass Char
precDigitClass :: Int -> TokenClass Char
precDigitClass Int
base = [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
oneOf ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
  [ [Char
c, Char -> Char
toUpper Char
c] | Char
c <- Int -> ShowS
forall a. Int -> [a] -> [a]
take Int
base ([Char
'0' .. Char
'9'] [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char
'a' .. Char
'z']) ])

-- Identifiers' & symbols' characters, which `read`'s lexer munches.
precIdfClass, precSymbolClass :: TokenClass Char
precIdfClass :: TokenClass Char
precIdfClass = (Categorize Char -> TokenClass Char)
-> [Categorize Char] -> TokenClass Char
forall (f :: * -> *) b a.
(Foldable f, DisjunctiveMonoid b) =>
(a -> b) -> f a -> b
anyP Categorize Char -> TokenClass Char
forall token p. Tokenized token p => Categorize token -> p
asIn
  [ GeneralCategory
UppercaseLetter, GeneralCategory
LowercaseLetter, GeneralCategory
TitlecaseLetter, GeneralCategory
ModifierLetter
  , GeneralCategory
OtherLetter, GeneralCategory
DecimalNumber, GeneralCategory
LetterNumber, GeneralCategory
OtherNumber ]
  TokenClass Char -> TokenClass Char -> TokenClass Char
forall a. DisjunctiveMonoid a => a -> a -> a
>|< [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
oneOf [Char]
"_'"
precSymbolClass :: TokenClass Char
precSymbolClass = [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
notOneOf [Char]
",;()[]{}`" TokenClass Char -> TokenClass Char -> TokenClass Char
forall b. BooleanAlgebra b => b -> b -> b
>&< [TokenClass Char] -> TokenClass Char
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
  [ (Categorize Char -> TokenClass Char)
-> [Categorize Char] -> TokenClass Char
forall (f :: * -> *) b a.
(Foldable f, DisjunctiveMonoid b) =>
(a -> b) -> f a -> b
anyP Categorize Char -> TokenClass Char
forall token p. Tokenized token p => Categorize token -> p
asIn [GeneralCategory
MathSymbol, GeneralCategory
CurrencySymbol, GeneralCategory
ModifierSymbol, GeneralCategory
OtherSymbol, GeneralCategory
DashPunctuation]
  , Categorize Char -> TokenClass Char
forall token p. Tokenized token p => Categorize token -> p
asIn GeneralCategory
Categorize Char
OtherPunctuation TokenClass Char -> TokenClass Char -> TokenClass Char
forall b. BooleanAlgebra b => b -> b -> b
>&< [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
notOneOf [Char]
"'\""
  , Categorize Char -> TokenClass Char
forall token p. Tokenized token p => Categorize token -> p
asIn GeneralCategory
Categorize Char
ConnectorPunctuation TokenClass Char -> TokenClass Char -> TokenClass Char
forall b. BooleanAlgebra b => b -> b -> b
>&< [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
notOneOf [Char]
"_" ]

-- An identifier, & a symbol, munched maximally.
precIdent, precSymbol :: (RegGrammatical Char p, NotFollowedBy p) => String -> p () ()
precIdent :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precIdent [Char]
name = [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
name p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
precIdfClass)
precSymbol :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precSymbol [Char]
name = [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
name p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
precSymbolClass)

-- A space, where `show` separates lexemes, printing one, & parsing one
-- whitespace character, or none where none follows, the rest of it
-- skipped before the next lexeme.
precSpace :: (RegGrammatical Char p, NotFollowedBy p) => p () ()
precSpace :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpace = [p () ()] -> p () ()
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
  [ [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
" "
  , Market () () () (Identity ()) -> Market () () () (Identity ())
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () () (Identity ()) -> Market () () () (Identity ()))
-> p () () -> p () ()
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? TokenClass Char -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
TokenClass Char -> p () ()
precToken (TokenClass Char
precSpaceClass TokenClass Char -> TokenClass Char -> TokenClass Char
forall b. BooleanAlgebra b => b -> b -> b
>&< [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
notOneOf [Char]
" ")
  , Market () () () (Identity ()) -> Market () () () (Identity ())
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () () (Identity ()) -> Market () () () (Identity ()))
-> p () () -> p () ()
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass Char
precSpaceClass) ]

-- A space, printing one, & parsing any whitespace.
precSpaced :: (RegGrammatical Char p, NotFollowedBy p) => p () ()
precSpaced :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpaced = p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precSpace p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces

-- Signed, by an improper iso, where @-0@ parses too.
precSigned :: (Num a, Ord a) => Iso' a (Bool, a)
precSigned :: forall a. (Num a, Ord a) => Iso' a (Bool, a)
precSigned = (a -> (Bool, a)) -> ((Bool, a) -> a) -> Iso a a (Bool, a) (Bool, a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
  (\a
n -> (a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0, a -> a
forall a. Num a => a -> a
abs a
n))
  (\(Bool
negative, a
n) -> if Bool
negative then a -> a
forall a. Num a => a -> a
negate a
n else a
n)

-- A number's lexeme without a fraction or exponent: decimal, printed,
-- or binary, octal or hexadecimal, after @0b@, @0o@ or @0x@, in either
-- case, parsed & tried first, as `read`'s lexer does. Binary is only
-- read from base-4.21 (GHC 9.12) on, as `read`'s lexer only reads it
-- from then.
precMagnitude :: (RegGrammatical Char p, NotFollowedBy p) => p Integer Integer
precMagnitude :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Integer Integer
precMagnitude = (Market Integer Integer Integer (Identity Integer)
-> Market Integer Integer Integer (Identity Integer)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market Integer Integer Integer (Identity Integer)
 -> Market Integer Integer Integer (Identity Integer))
-> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p Integer Integer -> p Integer Integer
forall p. Tried p => p -> p
try ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p Integer Integer
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Integer Integer
precBased))
  p Integer Integer -> p Integer Integer -> p Integer Integer
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Integer -> p Integer Integer
forall n (p :: * -> * -> *).
(Integral n, Alternator p, Tokenized Char (p Char Char)) =>
n -> p n n
numeral Integer
10 p Integer Integer -> p () () -> p Integer Integer
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precDecimalEnd

-- Where a decimal integer ends: not before a fraction, nor an exponent.
precDecimalEnd :: (RegGrammatical Char p, NotFollowedBy p) => p () ()
precDecimalEnd :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precDecimalEnd = RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
10))
  p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *). NotFollowedBy p => p () ()
precNotFraction p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *). NotFollowedBy p => p () ()
precNotExponent

-- Not before a fraction.
precNotFraction :: NotFollowedBy p => p () ()
precNotFraction :: forall (p :: * -> * -> *). NotFollowedBy p => p () ()
precNotFraction = RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy ([Char] -> RegEx Char
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"." RegEx Char -> RegEx Char -> RegEx Char
forall a. Semigroup a => a -> a -> a
<> TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
10))

-- Not before an exponent.
precNotExponent :: NotFollowedBy p => p () ()
precNotExponent :: forall (p :: * -> * -> *). NotFollowedBy p => p () ()
precNotExponent = RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy
  ([Char] -> RegEx Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> RegEx Char
oneOf [Char]
"eE" RegEx Char -> RegEx Char -> RegEx Char
forall a. Semigroup a => a -> a -> a
<> RegEx Char -> RegEx Char
forall k. KleeneStarAlgebra k => k -> k
optK ([Char] -> RegEx Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> RegEx Char
oneOf [Char]
"+-") RegEx Char -> RegEx Char -> RegEx Char
forall a. Semigroup a => a -> a -> a
<> TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
10))

precBased :: forall p. (RegGrammatical Char p, NotFollowedBy p) => p Integer Integer
precBased :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Integer Integer
precBased = [p Integer Integer] -> p Integer Integer
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
  [
#if MIN_VERSION_base(4,21,0)
    [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
"b" [Char]
"B" p () () -> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Int -> p Integer Integer
munched Int
2,
#endif
    [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
"o" [Char]
"O" p () () -> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Int -> p Integer Integer
munched Int
8
  , [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
"x" [Char]
"X" p () () -> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Int -> p Integer Integer
munched Int
16
  ]
  where
    munched :: Int -> p Integer Integer
    munched :: Int -> p Integer Integer
munched Int
base = Integer -> p Integer Integer
forall n (p :: * -> * -> *).
(Integral n, Alternator p, Tokenized Char (p Char Char)) =>
n -> p n n
numeral (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
base)
      p Integer Integer -> p () () -> p Integer Integer
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
base))

-- Natural numbers, & negative zero, as `read` reads integers & keeps
-- those which aren't negative.
precNatural :: (RegGrammatical Char p, NotFollowedBy p) => p Natural Natural
precNatural :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Natural Natural
precNatural = ((Natural -> Integer)
-> (Integer -> Natural) -> Iso Natural Natural Integer Integer
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Natural -> Integer
forall a. Integral a => a -> Integer
toInteger Integer -> Natural
forall a. Num a => Integer -> a
fromInteger (Exchange Integer Integer Integer (Identity Integer)
 -> Exchange Integer Integer Natural (Identity Natural))
-> p Integer Integer -> p Natural Natural
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p Integer Integer
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Integer Integer
precMagnitude)
  p Natural Natural -> p Natural Natural -> p Natural Natural
forall a. DisjunctiveMonoid a => a -> a -> a
>|< (Market () () Natural (Identity Natural)
-> Market () () Natural (Identity Natural)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () Natural (Identity Natural)
 -> Market () () Natural (Identity Natural))
-> (Market () () () (Identity ())
    -> Market () () Natural (Identity Natural))
-> Market () () () (Identity ())
-> Market () () Natural (Identity Natural)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> Prism' Natural ()
forall a. Eq a => a -> Prism' a ()
only Natural
0 (Market () () () (Identity ())
 -> Market () () Natural (Identity Natural))
-> p () () -> p Natural Natural
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"-" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
zerosG)
  where
    zerosG :: p () ()
zerosG = [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [p () ()] -> p () ()
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
      [ p () ()
zeros p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
precDecimalEnd
#if MIN_VERSION_base(4,21,0)
      , [p () ()] -> p () ()
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP (([Char] -> p () ()) -> [[Char]] -> [p () ()]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [[Char]
"b", [Char]
"B"]) p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
zeros
          p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
2))
#endif
      , [p () ()] -> p () ()
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP (([Char] -> p () ()) -> [[Char]] -> [p () ()]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [[Char]
"o", [Char]
"O"]) p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
zeros
          p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
8))
      , [p () ()] -> p () ()
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP (([Char] -> p () ()) -> [[Char]] -> [p () ()]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [[Char]
"x", [Char]
"X"]) p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
zeros
          p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
16))
      ]
    zeros :: p () ()
zeros = p () (f ()) -> p () (f ())
forall a b (p :: * -> * -> *) (f :: * -> *).
(Distributor p, Applicative f) =>
p a (f b) -> p () (f ())
forall {p :: * -> * -> *} {f :: * -> *}.
(Alternator p, Applicative f) =>
p () (f ()) -> p () (f ())
skippedMany (forall {p :: * -> * -> *} {f :: * -> *}.
 (Alternator p, Applicative f) =>
 p () (f ()) -> p () (f ()))
-> p () () -> p () ()
forall (p :: * -> * -> *) s t a b.
Alternator p =>
Prismoid s t a b -> p a b -> p s t
>?* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0"

-- A floating point number's lexeme, without its sign.
data FloatLexeme
  = NotANumber
  | Infinite
  -- | digits, a fraction's digits, & an exponent
  | Decimal [Int] (Maybe [Int]) (Maybe Integer)
  -- | binary, octal or hexadecimal, parsed only
  | Based Integer
  deriving stock FloatLexeme -> FloatLexeme -> Bool
(FloatLexeme -> FloatLexeme -> Bool)
-> (FloatLexeme -> FloatLexeme -> Bool) -> Eq FloatLexeme
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FloatLexeme -> FloatLexeme -> Bool
== :: FloatLexeme -> FloatLexeme -> Bool
$c/= :: FloatLexeme -> FloatLexeme -> Bool
/= :: FloatLexeme -> FloatLexeme -> Bool
Eq

_Decimal :: Prism' FloatLexeme ([Int], (Maybe [Int], Maybe Integer))
_Decimal :: Prism' FloatLexeme ([Int], (Maybe [Int], Maybe Integer))
_Decimal = (([Int], (Maybe [Int], Maybe Integer)) -> FloatLexeme)
-> (FloatLexeme -> Maybe ([Int], (Maybe [Int], Maybe Integer)))
-> Prism' FloatLexeme ([Int], (Maybe [Int], Maybe Integer))
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' (\([Int]
ds, (Maybe [Int]
fraction, Maybe Integer
ex)) -> [Int] -> Maybe [Int] -> Maybe Integer -> FloatLexeme
Decimal [Int]
ds Maybe [Int]
fraction Maybe Integer
ex) ((FloatLexeme -> Maybe ([Int], (Maybe [Int], Maybe Integer)))
 -> Prism' FloatLexeme ([Int], (Maybe [Int], Maybe Integer)))
-> (FloatLexeme -> Maybe ([Int], (Maybe [Int], Maybe Integer)))
-> Prism' FloatLexeme ([Int], (Maybe [Int], Maybe Integer))
forall a b. (a -> b) -> a -> b
$ \case
  Decimal [Int]
ds Maybe [Int]
fraction Maybe Integer
ex -> ([Int], (Maybe [Int], Maybe Integer))
-> Maybe ([Int], (Maybe [Int], Maybe Integer))
forall a. a -> Maybe a
Just ([Int]
ds, (Maybe [Int]
fraction, Maybe Integer
ex))
  FloatLexeme
_ -> Maybe ([Int], (Maybe [Int], Maybe Integer))
forall a. Maybe a
Nothing

_Based :: Prism' FloatLexeme Integer
_Based :: Prism' FloatLexeme Integer
_Based = (Integer -> FloatLexeme)
-> (FloatLexeme -> Maybe Integer) -> Prism' FloatLexeme Integer
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Integer -> FloatLexeme
Based ((FloatLexeme -> Maybe Integer) -> Prism' FloatLexeme Integer)
-> (FloatLexeme -> Maybe Integer) -> Prism' FloatLexeme Integer
forall a b. (a -> b) -> a -> b
$ \case
  Based Integer
n -> Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
n
  FloatLexeme
_ -> Maybe Integer
forall a. Maybe a
Nothing

{- Floating point numbers, printing as `show` does, by `floatToDigits`,
in fixed notation, or in exponent notation outside @0.1 <= x < 10^7@, &
parsing as `read` does, by `fromRational`, rounding correctly. -}
precFloating :: (RealFloat a, RegGrammatical Char p, NotFollowedBy p) => Int -> p a a
precFloating :: forall a (p :: * -> * -> *).
(RealFloat a, RegGrammatical Char p, NotFollowedBy p) =>
Int -> p a a
precFloating Int
d = (a -> (Bool, FloatLexeme))
-> ((Bool, FloatLexeme) -> a)
-> Iso a a (Bool, FloatLexeme) (Bool, FloatLexeme)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso a -> (Bool, FloatLexeme)
forall a. RealFloat a => a -> (Bool, FloatLexeme)
precToLexeme (Bool, FloatLexeme) -> a
forall a. RealFloat a => (Bool, FloatLexeme) -> a
precFromLexeme (Exchange
   (Bool, FloatLexeme)
   (Bool, FloatLexeme)
   (Bool, FloatLexeme)
   (Identity (Bool, FloatLexeme))
 -> Exchange (Bool, FloatLexeme) (Bool, FloatLexeme) a (Identity a))
-> p (Bool, FloatLexeme) (Bool, FloatLexeme) -> p a a
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ Int -> p Bool Bool
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Int -> p Bool Bool
precNegativeAt Int
d p Bool Bool
-> p FloatLexeme FloatLexeme
-> p (Bool, FloatLexeme) (Bool, FloatLexeme)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< [p FloatLexeme FloatLexeme] -> p FloatLexeme FloatLexeme
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
  [ FloatLexeme -> Prism' FloatLexeme ()
forall a. Eq a => a -> Prism' a ()
only FloatLexeme
NotANumber (Market () () () (Identity ())
 -> Market () () FloatLexeme (Identity FloatLexeme))
-> p () () -> p FloatLexeme FloatLexeme
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precIdent [Char]
"NaN"
  , FloatLexeme -> Prism' FloatLexeme ()
forall a. Eq a => a -> Prism' a ()
only FloatLexeme
Infinite (Market () () () (Identity ())
 -> Market () () FloatLexeme (Identity FloatLexeme))
-> p () () -> p FloatLexeme FloatLexeme
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[Char] -> p () ()
precIdent [Char]
"Infinity"
  , Market Integer Integer FloatLexeme (Identity FloatLexeme)
-> Market Integer Integer FloatLexeme (Identity FloatLexeme)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market Integer Integer FloatLexeme (Identity FloatLexeme)
 -> Market Integer Integer FloatLexeme (Identity FloatLexeme))
-> (Market Integer Integer Integer (Identity Integer)
    -> Market Integer Integer FloatLexeme (Identity FloatLexeme))
-> Market Integer Integer Integer (Identity Integer)
-> Market Integer Integer FloatLexeme (Identity FloatLexeme)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Market Integer Integer Integer (Identity Integer)
-> Market Integer Integer FloatLexeme (Identity FloatLexeme)
Prism' FloatLexeme Integer
_Based (Market Integer Integer Integer (Identity Integer)
 -> Market Integer Integer FloatLexeme (Identity FloatLexeme))
-> p Integer Integer -> p FloatLexeme FloatLexeme
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p Integer Integer -> p Integer Integer
forall p. Tried p => p -> p
try ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p Integer Integer
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Integer Integer
precBased)
  , Market
  ([Int], (Maybe [Int], Maybe Integer))
  ([Int], (Maybe [Int], Maybe Integer))
  ([Int], (Maybe [Int], Maybe Integer))
  (Identity ([Int], (Maybe [Int], Maybe Integer)))
-> Market
     ([Int], (Maybe [Int], Maybe Integer))
     ([Int], (Maybe [Int], Maybe Integer))
     FloatLexeme
     (Identity FloatLexeme)
Prism' FloatLexeme ([Int], (Maybe [Int], Maybe Integer))
_Decimal (Market
   ([Int], (Maybe [Int], Maybe Integer))
   ([Int], (Maybe [Int], Maybe Integer))
   ([Int], (Maybe [Int], Maybe Integer))
   (Identity ([Int], (Maybe [Int], Maybe Integer)))
 -> Market
      ([Int], (Maybe [Int], Maybe Integer))
      ([Int], (Maybe [Int], Maybe Integer))
      FloatLexeme
      (Identity FloatLexeme))
-> p ([Int], (Maybe [Int], Maybe Integer))
     ([Int], (Maybe [Int], Maybe Integer))
-> p FloatLexeme FloatLexeme
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p [Int] [Int]
digitsG
      p [Int] [Int]
-> p (Maybe [Int], Maybe Integer) (Maybe [Int], Maybe Integer)
-> p ([Int], (Maybe [Int], Maybe Integer))
     ([Int], (Maybe [Int], Maybe Integer))
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< [p (Maybe [Int]) (Maybe [Int])] -> p (Maybe [Int]) (Maybe [Int])
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
        [ Market [Int] [Int] [Int] (Identity [Int])
-> Market [Int] [Int] (Maybe [Int]) (Identity (Maybe [Int]))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just (Market [Int] [Int] [Int] (Identity [Int])
 -> Market [Int] [Int] (Maybe [Int]) (Identity (Maybe [Int])))
-> p [Int] [Int] -> p (Maybe [Int]) (Maybe [Int])
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"." p () () -> p [Int] [Int] -> p [Int] [Int]
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p [Int] [Int]
digitsG
        , Market () () () (Identity ())
-> Market () () (Maybe [Int]) (Identity (Maybe [Int]))
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p () (f ()) -> p (Maybe a) (f (Maybe a))
_Nothing (Market () () () (Identity ())
 -> Market () () (Maybe [Int]) (Identity (Maybe [Int])))
-> p () () -> p (Maybe [Int]) (Maybe [Int])
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). NotFollowedBy p => p () ()
precNotFraction ]
      p (Maybe [Int]) (Maybe [Int])
-> p (Maybe Integer) (Maybe Integer)
-> p (Maybe [Int], Maybe Integer) (Maybe [Int], Maybe Integer)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< [p (Maybe Integer) (Maybe Integer)]
-> p (Maybe Integer) (Maybe Integer)
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
        [ Market Integer Integer Integer (Identity Integer)
-> Market
     Integer Integer (Maybe Integer) (Identity (Maybe Integer))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just (Market Integer Integer Integer (Identity Integer)
 -> Market
      Integer Integer (Maybe Integer) (Identity (Maybe Integer)))
-> p Integer Integer -> p (Maybe Integer) (Maybe Integer)
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
"e" [Char]
"E" p () () -> p Integer Integer -> p Integer Integer
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* (Exchange
  (Bool, Integer)
  (Bool, Integer)
  (Bool, Integer)
  (Identity (Bool, Integer))
-> Exchange
     (Bool, Integer) (Bool, Integer) Integer (Identity Integer)
forall a. (Num a, Ord a) => Iso' a (Bool, a)
Iso' Integer (Bool, Integer)
precSigned (Exchange
   (Bool, Integer)
   (Bool, Integer)
   (Bool, Integer)
   (Identity (Bool, Integer))
 -> Exchange
      (Bool, Integer) (Bool, Integer) Integer (Identity Integer))
-> p (Bool, Integer) (Bool, Integer) -> p Integer Integer
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ p Bool Bool
exponentSignG p Bool Bool
-> p Integer Integer -> p (Bool, Integer) (Bool, Integer)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< Integer -> p Integer Integer
forall n (p :: * -> * -> *).
(Integral n, Alternator p, Tokenized Char (p Char Char)) =>
n -> p n n
numeral Integer
10
            p Integer Integer -> p () () -> p Integer Integer
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
10)))
        , Market () () () (Identity ())
-> Market () () (Maybe Integer) (Identity (Maybe Integer))
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p () (f ()) -> p (Maybe a) (f (Maybe a))
_Nothing (Market () () () (Identity ())
 -> Market () () (Maybe Integer) (Identity (Maybe Integer)))
-> p () () -> p (Maybe Integer) (Maybe Integer)
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). NotFollowedBy p => p () ()
precNotExponent ]
  ]
  where
    digitsG :: p [Int] [Int]
digitsG = p Int Int -> p [Int] [Int]
forall a b. p a b -> p [a] [b]
forall (p :: * -> * -> *) a b. Alternator p => p a b -> p [a] [b]
someP (Int -> p Int Int
forall n (p :: * -> * -> *).
(Integral n, Choice p, Tokenized Char (p Char Char)) =>
n -> p n n
digit Int
10) p [Int] [Int] -> p () () -> p [Int] [Int]
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< RegEx Char -> p () ()
forall (p :: * -> * -> *). NotFollowedBy p => RegEx Char -> p () ()
notFollowedBy (TokenClass Char -> RegEx Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Int -> TokenClass Char
precDigitClass Int
10))
    exponentSignG :: p Bool Bool
exponentSignG = [p Bool Bool] -> p Bool Bool
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
      [ Bool -> Prism' Bool ()
forall a. Eq a => a -> Prism' a ()
only Bool
True (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> p () () -> p Bool Bool
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"-"
      , Bool -> Prism' Bool ()
forall a. Eq a => a -> Prism' a ()
only Bool
False (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> (Market () () () (Identity ()) -> Market () () () (Identity ()))
-> Market () () () (Identity ())
-> Market () () Bool (Identity Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Market () () () (Identity ()) -> Market () () () (Identity ())
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> p () () -> p Bool Bool
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"+"
      , Bool -> Prism' Bool ()
forall a. Eq a => a -> Prism' a ()
only Bool
False (Market () () () (Identity ())
 -> Market () () Bool (Identity Bool))
-> p () () -> p Bool Bool
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP
      ]

precToLexeme :: RealFloat a => a -> (Bool, FloatLexeme)
precToLexeme :: forall a. RealFloat a => a -> (Bool, FloatLexeme)
precToLexeme a
x
  | a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
x = (Bool
False, FloatLexeme
NotANumber)
  | a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
x = (a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0, FloatLexeme
Infinite)
  | Bool
otherwise = (a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
x, ([Int], Int) -> FloatLexeme
generic (Integer -> a -> ([Int], Int)
forall a. RealFloat a => Integer -> a -> ([Int], Int)
floatToDigits Integer
10 (a -> a
forall a. Num a => a -> a
abs a
x)))
  where
    generic :: ([Int], Int) -> FloatLexeme
generic ([Int]
ds, Int
e)
      | Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
7 = case [Int]
ds of
          Int
d : ds' :: [Int]
ds'@(Int
_ : [Int]
_) -> [Int] -> Maybe [Int] -> Maybe Integer -> FloatLexeme
Decimal [Int
d] ([Int] -> Maybe [Int]
forall a. a -> Maybe a
Just [Int]
ds') (Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
e Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1))
          [Int]
_ -> [Int] -> Maybe [Int] -> Maybe Integer -> FloatLexeme
Decimal (Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
take Int
1 [Int]
ds [Int] -> [Int] -> [Int]
forall a. Semigroup a => a -> a -> a
<> [Int
0 | [Int] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
ds]) ([Int] -> Maybe [Int]
forall a. a -> Maybe a
Just [Int
0]) (Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
e Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1))
      | Bool
otherwise =
          let ([Int]
whole, [Int]
fraction) = Int -> [Int] -> ([Int], [Int])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
e ([Int]
ds [Int] -> [Int] -> [Int]
forall a. Semigroup a => a -> a -> a
<> Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate (Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
ds) Int
0)
          in [Int] -> Maybe [Int] -> Maybe Integer -> FloatLexeme
Decimal ([Int] -> [Int]
forall {a}. Num a => [a] -> [a]
orZero [Int]
whole) ([Int] -> Maybe [Int]
forall a. a -> Maybe a
Just ([Int] -> [Int]
forall {a}. Num a => [a] -> [a]
orZero [Int]
fraction)) Maybe Integer
forall a. Maybe a
Nothing
    orZero :: [a] -> [a]
orZero [a]
ds = if [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
ds then [a
0] else [a]
ds

precFromLexeme :: RealFloat a => (Bool, FloatLexeme) -> a
precFromLexeme :: forall a. RealFloat a => (Bool, FloatLexeme) -> a
precFromLexeme (Bool
negative, FloatLexeme
lexeme) = (if Bool
negative then a -> a
forall a. Num a => a -> a
negate else a -> a
forall a. a -> a
id) a
value
  where
    value :: a
value = case FloatLexeme
lexeme of
      FloatLexeme
NotANumber -> a
0 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
0
      FloatLexeme
Infinite -> a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
0
      Based Integer
n -> Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
n
      Decimal [Int]
whole Maybe [Int]
fraction Maybe Integer
ex -> case Maybe Integer
ex of
        Maybe Integer
Nothing -> Rational -> a
forall a. Fractional a => Rational -> a
fromRational ([Int] -> Maybe [Int] -> Integer -> Rational
forall {t :: * -> *} {a}.
(Foldable t, Integral a) =>
[a] -> t [a] -> Integer -> Rational
rational [Int]
whole Maybe [Int]
fraction Integer
0)
        -- `read` bounds exponents by the type's range, before computing
        Just Integer
e -> case [Int] -> Maybe [Int] -> Maybe Int
forall {a} {a} {t :: * -> *}.
(Num a, Num a, Eq a, Eq a, Foldable t) =>
[a] -> t [a] -> Maybe Int
firstDigit [Int]
whole Maybe [Int]
fraction of
          Maybe Int
Nothing -> a
0
          Just Int
first
            | Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
first Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
e Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger ((Int, Int) -> Int
forall a b. (a, b) -> b
snd (Int, Int)
range Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) -> a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
0
            | Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
first Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
e Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Integer
forall a. Integral a => a -> Integer
toInteger ((Int, Int) -> Int
forall a b. (a, b) -> a
fst (Int, Int)
range Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
3) -> a
0
            | Bool
otherwise -> Rational -> a
forall a. Fractional a => Rational -> a
fromRational ([Int] -> Maybe [Int] -> Integer -> Rational
forall {t :: * -> *} {a}.
(Foldable t, Integral a) =>
[a] -> t [a] -> Integer -> Rational
rational [Int]
whole Maybe [Int]
fraction Integer
e)
    range :: (Int, Int)
range = a -> (Int, Int)
forall a. RealFloat a => a -> (Int, Int)
floatRange a
value
    rational :: [a] -> t [a] -> Integer -> Rational
rational [a]
whole t [a]
fraction Integer
e =
      let
        ds :: [a]
ds = [a]
whole [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> t [a] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat t [a]
fraction
        scale :: Integer
scale = Integer
e Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int -> Integer
forall a. Integral a => a -> Integer
toInteger ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (t [a] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat t [a]
fraction))
        n :: Integer
n = (Integer -> a -> Integer) -> Integer -> [a] -> Integer
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Integer
acc a
d -> Integer
acc Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ a -> Integer
forall a. Integral a => a -> Integer
toInteger a
d) Integer
0 [a]
ds
      in if Integer
scale Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 then Integer -> Rational
forall a. Num a => Integer -> a
fromInteger (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
scale) else Integer
n Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% (Integer
10 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer -> Integer
forall a. Num a => a -> a
negate Integer
scale)
    firstDigit :: [a] -> t [a] -> Maybe Int
firstDigit [a]
whole t [a]
fraction = case (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0) [a]
whole of
      significant :: [a]
significant@(a
_ : [a]
_) -> Int -> Maybe Int
forall a. a -> Maybe a
Just ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
significant)
      [] -> case (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0) (t [a] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat t [a]
fraction) of
        ([a]
_, []) -> Maybe Int
forall a. Maybe a
Nothing
        ([a]
zeros, [a]
_) -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int
forall a. Num a => a -> a
negate ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
zeros))

-- A character's literal, between single quotes.
precChar :: (RegGrammatical Char p, NotFollowedBy p) => p Char Char
precChar :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p Char Char
precChar = [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"'" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Char -> p Char Char
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Char -> p Char Char
precLitChar Char
'\'' p Char Char -> p () () -> p Char Char
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"'"

{- A character within a literal, printing as `show` does, by `showLitChar`,
& parsing as `read` does: unescaped, but for @\\@ & the literal's quote;
by a named escape; or by its decimal, octal or hexadecimal code, up to
`maxBound`, or as a control, @\\^@@ to @\\^_@. -}
precLitChar :: (RegGrammatical Char p, NotFollowedBy p) => Char -> p Char Char
precLitChar :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Char -> p Char Char
precLitChar Char
quote = [Char] -> p Char Char
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> p Char Char
precRaw [Char
quote] p Char Char -> p Char Char -> p Char Char
forall a. DisjunctiveMonoid a => a -> a -> a
>|< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [p Char Char] -> p Char Char
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
  [Bool -> p Char Char
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Bool -> p Char Char
precFixedEsc Bool
True, p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precDecimalEsc, p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precOctalEsc, p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precHexEsc, p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precControlEsc]

-- Unescaped, but for @\\@ & some others, printing only printable ASCII.
precRaw :: RegGrammatical Char p => String -> p Char Char
precRaw :: forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> p Char Char
precRaw [Char]
excluded = TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass ([Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
oneOf [Char
' ' .. Char
'~'] TokenClass Char -> TokenClass Char -> TokenClass Char
forall b. BooleanAlgebra b => b -> b -> b
>&< [Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
notOneOf (Char
'\\' Char -> ShowS
forall a. a -> [a] -> [a]
: [Char]
excluded))
  p Char Char -> p Char Char -> p Char Char
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Market Char Char Char (Identity Char)
-> Market Char Char Char (Identity Char)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market Char Char Char (Identity Char)
 -> Market Char Char Char (Identity Char))
-> p Char Char -> p Char Char
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass ([Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
notOneOf (Char
'\\' Char -> ShowS
forall a. a -> [a] -> [a]
: [Char]
excluded [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char
' ' .. Char
'~']))

-- Named escapes, with @SO@ or not.
precFixedEsc :: (RegGrammatical Char p, NotFollowedBy p) => Bool -> p Char Char
precFixedEsc :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Bool -> p Char Char
precFixedEsc Bool
withSO = [(Char, [Char])] -> p Char Char
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[(Char, [Char])] -> p Char Char
precEscapes ([(Char, [Char])] -> p Char Char)
-> [(Char, [Char])] -> p Char Char
forall a b. (a -> b) -> a -> b
$
  [(Char
'\'', [Char]
"'"), (Char
'\a', [Char]
"a"), (Char
'\b', [Char]
"b"), (Char
'\f', [Char]
"f"), (Char
'\n', [Char]
"n")]
  [(Char, [Char])] -> [(Char, [Char])] -> [(Char, [Char])]
forall a. Semigroup a => a -> a -> a
<> [(Char
'\r', [Char]
"r"), (Char
'\t', [Char]
"t"), (Char
'\v', [Char]
"v"), (Char
'\\', [Char]
"\\"), (Char
'"', [Char]
"\"")]
  [(Char, [Char])] -> [(Char, [Char])] -> [(Char, [Char])]
forall a. Semigroup a => a -> a -> a
<> [(Char, [Char])
e | e :: (Char, [Char])
e@(Char
c, [Char]
_) <- [Char] -> [[Char]] -> [(Char, [Char])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Char
'\NUL' ..] [[Char]]
asciiNames, Bool
withSO Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\SO']
  [(Char, [Char])] -> [(Char, [Char])] -> [(Char, [Char])]
forall a. Semigroup a => a -> a -> a
<> [(Char
' ', [Char]
"SP"), (Char
'\DEL', [Char]
"DEL")]
  where
    asciiNames :: [[Char]]
asciiNames =
      [ [Char]
"NUL", [Char]
"SOH", [Char]
"STX", [Char]
"ETX", [Char]
"EOT", [Char]
"ENQ", [Char]
"ACK", [Char]
"BEL"
      , [Char]
"BS", [Char]
"HT", [Char]
"LF", [Char]
"VT", [Char]
"FF", [Char]
"CR", [Char]
"SO", [Char]
"SI"
      , [Char]
"DLE", [Char]
"DC1", [Char]
"DC2", [Char]
"DC3", [Char]
"DC4", [Char]
"NAK", [Char]
"SYN", [Char]
"ETB"
      , [Char]
"CAN", [Char]
"EM", [Char]
"SUB", [Char]
"ESC", [Char]
"FS", [Char]
"GS", [Char]
"RS", [Char]
"US" ]

-- Codes up to `maxBound`, decimal, printing above @\\DEL@, octal & hexadecimal.
precDecimalEsc, precOctalEsc, precHexEsc :: RegGrammatical Char p => p Char Char
precDecimalEsc :: forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precDecimalEsc = (Int -> Char) -> (Char -> Maybe Int) -> Prism Char Char Int Int
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Int -> Char
forall a. Enum a => Int -> a
toEnum (\Char
c -> if Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
> Char
'\DEL' then Int -> Maybe Int
forall a. a -> Maybe a
Just (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c) else Maybe Int
forall a. Maybe a
Nothing)
  (Market Int Int Int (Identity Int)
 -> Market Int Int Char (Identity Char))
-> p Int Int -> p Char Char
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? Int -> Int -> p Int Int
forall n (p :: * -> * -> *).
(Integral n, RegGrammatical Char p) =>
n -> n -> p n n
precBoundedNumeral Int
10 (Char -> Int
forall a. Enum a => a -> Int
fromEnum (Char
forall a. Bounded a => a
maxBound :: Char))
precOctalEsc :: forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precOctalEsc = Market Int Int Char (Identity Char)
-> Market Int Int Char (Identity Char)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market Int Int Char (Identity Char)
 -> Market Int Int Char (Identity Char))
-> (Market Int Int Int (Identity Int)
    -> Market Int Int Char (Identity Char))
-> Market Int Int Int (Identity Int)
-> Market Int Int Char (Identity Char)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Char) -> (Char -> Maybe Int) -> Prism Char Char Int Int
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Int -> Char
forall a. Enum a => Int -> a
toEnum (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> (Char -> Int) -> Char -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
forall a. Enum a => a -> Int
fromEnum)
  (Market Int Int Int (Identity Int)
 -> Market Int Int Char (Identity Char))
-> p Int Int -> p Char Char
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
"o" [Char]
"O" p () () -> p Int Int -> p Int Int
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Int -> Int -> p Int Int
forall n (p :: * -> * -> *).
(Integral n, RegGrammatical Char p) =>
n -> n -> p n n
precBoundedNumeral Int
8 (Char -> Int
forall a. Enum a => a -> Int
fromEnum (Char
forall a. Bounded a => a
maxBound :: Char))
precHexEsc :: forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precHexEsc = Market Int Int Char (Identity Char)
-> Market Int Int Char (Identity Char)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market Int Int Char (Identity Char)
 -> Market Int Int Char (Identity Char))
-> (Market Int Int Int (Identity Int)
    -> Market Int Int Char (Identity Char))
-> Market Int Int Int (Identity Int)
-> Market Int Int Char (Identity Char)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Char) -> (Char -> Maybe Int) -> Prism Char Char Int Int
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Int -> Char
forall a. Enum a => Int -> a
toEnum (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> (Char -> Int) -> Char -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
forall a. Enum a => a -> Int
fromEnum)
  (Market Int Int Int (Identity Int)
 -> Market Int Int Char (Identity Char))
-> p Int Int -> p Char Char
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char]
"x" [Char]
"X" p () () -> p Int Int -> p Int Int
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Int -> Int -> p Int Int
forall n (p :: * -> * -> *).
(Integral n, RegGrammatical Char p) =>
n -> n -> p n n
precBoundedNumeral Int
16 (Char -> Int
forall a. Enum a => a -> Int
fromEnum (Char
forall a. Bounded a => a
maxBound :: Char))
precControlEsc :: RegGrammatical Char p => p Char Char
precControlEsc :: forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precControlEsc = Market Char Char Char (Identity Char)
-> Market Char Char Char (Identity Char)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market Char Char Char (Identity Char)
 -> Market Char Char Char (Identity Char))
-> (Market Char Char Char (Identity Char)
    -> Market Char Char Char (Identity Char))
-> Market Char Char Char (Identity Char)
-> Market Char Char Char (Identity Char)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> (Char -> Char) -> Iso Char Char Char Char
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\Char
c -> Int -> Char
forall a. Enum a => Int -> a
toEnum (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
64)) (\Char
c -> Int -> Char
forall a. Enum a => Int -> a
toEnum (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
64))
  (Market Char Char Char (Identity Char)
 -> Market Char Char Char (Identity Char))
-> p Char Char -> p Char Char
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"^" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass ([Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
oneOf [Char
'@' .. Char
'_'])

-- A string literal's items: characters, & empty escapes.
data StrItem = StrChar Char | StrEmpty
  deriving stock StrItem -> StrItem -> Bool
(StrItem -> StrItem -> Bool)
-> (StrItem -> StrItem -> Bool) -> Eq StrItem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StrItem -> StrItem -> Bool
== :: StrItem -> StrItem -> Bool
$c/= :: StrItem -> StrItem -> Bool
/= :: StrItem -> StrItem -> Bool
Eq

_StrChar :: Prism' StrItem Char
_StrChar :: Prism' StrItem Char
_StrChar = (Char -> StrItem) -> (StrItem -> Maybe Char) -> Prism' StrItem Char
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Char -> StrItem
StrChar ((StrItem -> Maybe Char) -> Prism' StrItem Char)
-> (StrItem -> Maybe Char) -> Prism' StrItem Char
forall a b. (a -> b) -> a -> b
$ \case
  StrChar Char
c -> Char -> Maybe Char
forall a. a -> Maybe a
Just Char
c
  StrItem
StrEmpty -> Maybe Char
forall a. Maybe a
Nothing

{- A string's literal, between double quotes, printing as `show` does, by
`showLitString`, with an empty escape, @\\&@, after a numeric escape
before a digit, or after @\\SO@ before @H@, & parsing as `read` does, with
empty escapes, & gaps, @\\@, whitespace & @\\@, anywhere, an improper iso,
& with numeric escapes' digits & @\\SOH@ maximally munched, as `read`'s
lexer does, so that an escape is never followed by what would continue it. -}
precString :: forall p. (RegGrammatical Char p, NotFollowedBy p) => p String String
precString :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p [Char] [Char]
precString = ([Char] -> [StrItem])
-> ([StrItem] -> [Char]) -> Iso [Char] [Char] [StrItem] [StrItem]
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso [Char] -> [StrItem]
items (\[StrItem]
xs -> [Char
c | StrChar Char
c <- [StrItem]
xs])
  (Exchange [StrItem] [StrItem] [StrItem] (Identity [StrItem])
 -> Exchange [StrItem] [StrItem] [Char] (Identity [Char]))
-> p [StrItem] [StrItem] -> p [Char] [Char]
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\"" p () () -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p [StrItem] [StrItem]
free p [StrItem] [StrItem] -> p () () -> p [StrItem] [StrItem]
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\""
  where
    -- items, after which a character mustn't continue the last escape
    free, afterDecimal, afterOctal, afterHex, afterSO :: p [StrItem] [StrItem]
    free :: p [StrItem] [StrItem]
free = [Char] -> p [StrItem] [StrItem]
itemsG [Char]
""
    afterDecimal :: p [StrItem] [StrItem]
afterDecimal = [Char] -> p [StrItem] [StrItem]
itemsG [Char
'0' .. Char
'9']
    afterOctal :: p [StrItem] [StrItem]
afterOctal = [Char] -> p [StrItem] [StrItem]
itemsG [Char
'0' .. Char
'7']
    afterHex :: p [StrItem] [StrItem]
afterHex = [Char] -> p [StrItem] [StrItem]
itemsG ([Char
'0' .. Char
'9'] [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char
'a' .. Char
'f'] [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char
'A' .. Char
'F'])
    afterSO :: p [StrItem] [StrItem]
afterSO = [Char] -> p [StrItem] [StrItem]
itemsG [Char]
"H"
    itemsG :: String -> p [StrItem] [StrItem]
    itemsG :: [Char] -> p [StrItem] [StrItem]
itemsG [Char]
continuing = [p [StrItem] [StrItem]] -> p [StrItem] [StrItem]
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
      [ Market () () () (Identity ())
-> Market () () [StrItem] (Identity [StrItem])
forall a. AsEmpty a => Prism' a ()
Prism' [StrItem] ()
_Empty (Market () () () (Identity ())
 -> Market () () [StrItem] (Identity [StrItem]))
-> p () () -> p [StrItem] [StrItem]
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen p StrItem StrItem
emptyG p [StrItem] [StrItem]
free
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen (Market Char Char Char (Identity Char)
-> Market Char Char StrItem (Identity StrItem)
Prism' StrItem Char
_StrChar (Market Char Char Char (Identity Char)
 -> Market Char Char StrItem (Identity StrItem))
-> p Char Char -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p Char Char
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> p Char Char
precRaw (Char
'"' Char -> ShowS
forall a. a -> [a] -> [a]
: [Char]
continuing)) p [StrItem] [StrItem]
free
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen (Market Char Char Char (Identity Char)
-> Market Char Char StrItem (Identity StrItem)
Prism' StrItem Char
_StrChar (Market Char Char Char (Identity Char)
 -> Market Char Char StrItem (Identity StrItem))
-> p Char Char -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [p Char Char] -> p Char Char
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP [Bool -> p Char Char
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
Bool -> p Char Char
precFixedEsc Bool
False, p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precControlEsc]) p [StrItem] [StrItem]
free
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen (Market () () Char (Identity Char)
-> Market () () StrItem (Identity StrItem)
Prism' StrItem Char
_StrChar (Market () () Char (Identity Char)
 -> Market () () StrItem (Identity StrItem))
-> (Market () () () (Identity ())
    -> Market () () Char (Identity Char))
-> Market () () () (Identity ())
-> Market () () StrItem (Identity StrItem)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Prism' Char ()
forall a. Eq a => a -> Prism' a ()
only Char
'\SO' (Market () () () (Identity ())
 -> Market () () StrItem (Identity StrItem))
-> p () () -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\SO") p [StrItem] [StrItem]
afterSO
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen (Market Char Char Char (Identity Char)
-> Market Char Char StrItem (Identity StrItem)
Prism' StrItem Char
_StrChar (Market Char Char Char (Identity Char)
 -> Market Char Char StrItem (Identity StrItem))
-> p Char Char -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precDecimalEsc) p [StrItem] [StrItem]
afterDecimal
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen (Market Char Char Char (Identity Char)
-> Market Char Char StrItem (Identity StrItem)
Prism' StrItem Char
_StrChar (Market Char Char Char (Identity Char)
 -> Market Char Char StrItem (Identity StrItem))
-> p Char Char -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precOctalEsc) p [StrItem] [StrItem]
afterOctal
      , p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen (Market Char Char Char (Identity Char)
-> Market Char Char StrItem (Identity StrItem)
Prism' StrItem Char
_StrChar (Market Char Char Char (Identity Char)
 -> Market Char Char StrItem (Identity StrItem))
-> p Char Char -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\" p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p Char Char
forall (p :: * -> * -> *). RegGrammatical Char p => p Char Char
precHexEsc) p [StrItem] [StrItem]
afterHex
      ]
    itemThen :: p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
    itemThen :: p StrItem StrItem -> p [StrItem] [StrItem] -> p [StrItem] [StrItem]
itemThen p StrItem StrItem
item p [StrItem] [StrItem]
rest = Market
  (StrItem, [StrItem])
  (StrItem, [StrItem])
  (StrItem, [StrItem])
  (Identity (StrItem, [StrItem]))
-> Market
     (StrItem, [StrItem])
     (StrItem, [StrItem])
     [StrItem]
     (Identity [StrItem])
forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
Prism [StrItem] [StrItem] (StrItem, [StrItem]) (StrItem, [StrItem])
_Cons (Market
   (StrItem, [StrItem])
   (StrItem, [StrItem])
   (StrItem, [StrItem])
   (Identity (StrItem, [StrItem]))
 -> Market
      (StrItem, [StrItem])
      (StrItem, [StrItem])
      [StrItem]
      (Identity [StrItem]))
-> p (StrItem, [StrItem]) (StrItem, [StrItem])
-> p [StrItem] [StrItem]
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p StrItem StrItem
item p StrItem StrItem
-> p [StrItem] [StrItem]
-> p (StrItem, [StrItem]) (StrItem, [StrItem])
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< p [StrItem] [StrItem]
rest
    emptyG :: p StrItem StrItem
    emptyG :: p StrItem StrItem
emptyG = StrItem -> Prism' StrItem ()
forall a. Eq a => a -> Prism' a ()
only StrItem
StrEmpty (Market () () () (Identity ())
 -> Market () () StrItem (Identity StrItem))
-> p () () -> p StrItem StrItem
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\&" p () () -> p () () -> p () ()
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Market () () () (Identity ()) -> Market () () () (Identity ())
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market () () () (Identity ()) -> Market () () () (Identity ()))
-> p () () -> p () ()
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\"
      p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* (() -> Char) -> (Char -> ()) -> p Char Char -> p () ()
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (Char -> () -> Char
forall a b. a -> b -> a
const Char
' ') (() -> Char -> ()
forall a b. a -> b -> a
const ()) (TokenClass Char -> p Char Char
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass ([Char] -> TokenClass Char
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f Char -> TokenClass Char
oneOf [Char]
"\t\n\v\f\r" TokenClass Char -> TokenClass Char -> TokenClass Char
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Categorize Char -> TokenClass Char
forall token p. Tokenized token p => Categorize token -> p
asIn GeneralCategory
Categorize Char
Space))
      p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
p () ()
skipSpaces p () () -> p () () -> p () ()
forall (p :: * -> * -> *) a b c.
Monoidal p =>
p a b -> p () c -> p a b
*< [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"\\")
    items :: [Char] -> [StrItem]
items = \case
      Char
c : cs :: [Char]
cs@(Char
c' : [Char]
_) | Char -> Char -> Bool
protected Char
c Char
c' -> Char -> StrItem
StrChar Char
c StrItem -> [StrItem] -> [StrItem]
forall a. a -> [a] -> [a]
: StrItem
StrEmpty StrItem -> [StrItem] -> [StrItem]
forall a. a -> [a] -> [a]
: [Char] -> [StrItem]
items [Char]
cs
      Char
c : [Char]
cs -> Char -> StrItem
StrChar Char
c StrItem -> [StrItem] -> [StrItem]
forall a. a -> [a] -> [a]
: [Char] -> [StrItem]
items [Char]
cs
      [] -> []
    protected :: Char -> Char -> Bool
protected Char
c Char
c' = (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
> Char
'\DEL' Bool -> Bool -> Bool
&& Char -> Bool
isDigit Char
c') Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\SO' Bool -> Bool -> Bool
&& Char
c' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'H')

-- Escapes by fixed strings, as a trie, which is LL(1), maximally munched,
-- like @\\SO@ & @\\SOH@, each character printing by its first string.
precEscapes :: (RegGrammatical Char p, NotFollowedBy p) => [(Char, String)] -> p Char Char
precEscapes :: forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[(Char, [Char])] -> p Char Char
precEscapes [(Char, [Char])]
escapes = [p Char Char] -> p Char Char
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP ([p Char Char] -> p Char Char) -> [p Char Char] -> p Char Char
forall a b. (a -> b) -> a -> b
$
  [Char -> Prism' Char ()
forall a. Eq a => a -> Prism' a ()
only Char
c (Market () () () (Identity ())
 -> Market () () Char (Identity Char))
-> p () () -> p Char Char
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP | (Char
c, [Char]
"") <- Int -> [(Char, [Char])] -> [(Char, [Char])]
forall a. Int -> [a] -> [a]
take Int
1 [(Char, [Char])
e | e :: (Char, [Char])
e@(Char
_, [Char]
"") <- [(Char, [Char])]
escapes]]
  [p Char Char] -> [p Char Char] -> [p Char Char]
forall a. Semigroup a => a -> a -> a
<> [ [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char
first] p () () -> p Char Char -> p Char Char
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* [(Char, [Char])] -> p Char Char
forall (p :: * -> * -> *).
(RegGrammatical Char p, NotFollowedBy p) =>
[(Char, [Char])] -> p Char Char
precEscapes [(Char
c, [Char]
rest) | (Char
c, Char
x : [Char]
rest) <- [(Char, [Char])]
escapes, Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
first]
     | Char
first <- ShowS
forall a. Eq a => [a] -> [a]
nub [Char
x | (Char
_, Char
x : [Char]
_) <- [(Char, [Char])]
escapes] ]

{- A natural number in a base, up to a bound, with leading zeros, which
parse, as `read`'s lexer does, but don't print; a regular grammar, by the
bound's digits, so that a number beyond it fails to parse. -}
precBoundedNumeral :: forall n p. (Integral n, RegGrammatical Char p) => n -> n -> p n n
precBoundedNumeral :: forall n (p :: * -> * -> *).
(Integral n, RegGrammatical Char p) =>
n -> n -> p n n
precBoundedNumeral n
base n
bound
  = (Market n n n (Identity n) -> Market n n n (Identity n)
forall a (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f a) -> p a (f a)
precUnprinted (Market n n n (Identity n) -> Market n n n (Identity n))
-> p n n -> p n n
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p n n -> p n n
forall p. Tried p => p -> p
try ([Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
zerosG p () () -> p n n -> p n n
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p n n
positiveG))
  p n n -> p n n -> p n n
forall a. DisjunctiveMonoid a => a -> a -> a
>|< (n -> Prism' n ()
forall a. Eq a => a -> Prism' a ()
only n
0 (Market () () () (Identity ()) -> Market () () n (Identity n))
-> p () () -> p n n
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0" p () () -> p () () -> p () ()
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p () ()
zerosG)
  p n n -> p n n -> p n n
forall a. DisjunctiveMonoid a => a -> a -> a
>|< p n n
positiveG
  where
    zerosG :: p () ()
    zerosG :: p () ()
zerosG = p () (f ()) -> p () (f ())
forall a b (p :: * -> * -> *) (f :: * -> *).
(Distributor p, Applicative f) =>
p a (f b) -> p () (f ())
forall {p :: * -> * -> *} {f :: * -> *}.
(Alternator p, Applicative f) =>
p () (f ()) -> p () (f ())
skippedMany (forall {p :: * -> * -> *} {f :: * -> *}.
 (Alternator p, Applicative f) =>
 p () (f ()) -> p () (f ()))
-> p () () -> p () ()
forall (p :: * -> * -> *) s t a b.
Alternator p =>
Prismoid s t a b -> p a b -> p s t
>?* [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char]
"0"
    width :: Int
    width :: Int
width = [n] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((n -> Bool) -> [n] -> [n]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (n -> n -> Bool
forall a. Ord a => a -> a -> Bool
<= n
bound) ((n -> n) -> n -> [n]
forall a. (a -> a) -> a -> [a]
iterate (n -> n -> n
forall a. Num a => a -> a -> a
* n
base) n
1))
    -- without leading zeros, by their number of digits, tried longest first
    positiveG :: p n n
    positiveG :: p n n
positiveG = [p n n] -> p n n
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP ([p n n] -> p n n) -> [p n n] -> p n n
forall a b. (a -> b) -> a -> b
$
      p n n -> p n n
forall p. Tried p => p -> p
try (n -> n -> p n n -> p n n
ranged (n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) n
bound (Int -> n -> Bool -> p n n
upTo Int
width n
bound Bool
True))
      p n n -> [p n n] -> [p n n]
forall a. a -> [a] -> [a]
: [ p n n -> p n n
forall p. Tried p => p -> p
try (p n n -> p n n) -> p n n -> p n n
forall a b. (a -> b) -> a -> b
$ n -> n -> p n n -> p n n
ranged (n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) (n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
j n -> n -> n
forall a. Num a => a -> a -> a
- n
1)
            ([n] -> Int -> p n n -> p n n
headed [n
1 .. n
base n -> n -> n
forall a. Num a => a -> a -> a
- n
1] (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> p n n
exactly' (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)))
        | Int
j <- [Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 .. Int
1] ]
    ranged :: n -> n -> p n n -> p n n
    ranged :: n -> n -> p n n -> p n n
ranged n
lo n
hi = ((n -> n) -> (n -> Maybe n) -> Prism n n n n
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' n -> n
forall a. a -> a
id (\n
n -> if n
lo n -> n -> Bool
forall a. Ord a => a -> a -> Bool
<= n
n Bool -> Bool -> Bool
&& n
n n -> n -> Bool
forall a. Ord a => a -> a -> Bool
<= n
hi then n -> Maybe n
forall a. a -> Maybe a
Just n
n else Maybe n
forall a. Maybe a
Nothing) (Market n n n (Identity n) -> Market n n n (Identity n))
-> p n n -> p n n
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>?)
    -- a first digit, & the rest of so many digits
    headed :: [n] -> Int -> p n n -> p n n
    headed :: [n] -> Int -> p n n -> p n n
headed [n]
firsts Int
rest p n n
restG = [p n n] -> p n n
forall (f :: * -> *) a.
(Foldable f, DisjunctiveMonoid a) =>
f a -> a
orP
      [ (n -> n) -> (n -> Maybe n) -> Prism n n n n
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' (\n
r -> n
d n -> n -> n
forall a. Num a => a -> a -> a
* n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
rest n -> n -> n
forall a. Num a => a -> a -> a
+ n
r)
          (\n
n -> if n
n n -> n -> n
forall a. Integral a => a -> a -> a
`div` n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
rest n -> n -> Bool
forall a. Eq a => a -> a -> Bool
== n
d then n -> Maybe n
forall a. a -> Maybe a
Just (n
n n -> n -> n
forall a. Integral a => a -> a -> a
`mod` n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
rest) else Maybe n
forall a. Maybe a
Nothing)
          (Market n n n (Identity n) -> Market n n n (Identity n))
-> p n n -> p n n
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? n -> p () ()
digitIs n
d p () () -> p n n -> p n n
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* p n n
restG
      | n
d <- [n]
firsts ]
    -- exactly so many digits, any
    exactly' :: Int -> p n n
    exactly' :: Int -> p n n
exactly' Int
k = (n -> [n]) -> ([n] -> n) -> Iso n n [n] [n]
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\n
n -> [n
n n -> n -> n
forall a. Integral a => a -> a -> a
`div` n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
i n -> n -> n
forall a. Integral a => a -> a -> a
`mod` n
base | Int
i <- [Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 .. Int
0]])
      ((n -> n -> n) -> n -> [n] -> n
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\n
acc n
d -> n
acc n -> n -> n
forall a. Num a => a -> a -> a
* n
base n -> n -> n
forall a. Num a => a -> a -> a
+ n
d) n
0) (Exchange [n] [n] [n] (Identity [n])
 -> Exchange [n] [n] n (Identity n))
-> p [n] [n] -> p n n
forall (p :: * -> * -> *) s t a b.
Profunctor p =>
AnIso s t a b -> p a b -> p s t
>~ Int -> p n n -> p [n] [n]
forall (p :: * -> * -> *) s a.
(Monoidal p, Choice p, AsEmpty s, Cons s s a a) =>
Int -> p a a -> p s s
replicateP Int
k (n -> p n n
forall n (p :: * -> * -> *).
(Integral n, Choice p, Tokenized Char (p Char Char)) =>
n -> p n n
digit n
base)
    -- so many digits, up to a bound, the first nonzero when leading
    upTo :: Int -> n -> Bool -> p n n
    upTo :: Int -> n -> Bool -> p n n
upTo Int
0 n
_ Bool
_ = n -> Prism' n ()
forall a. Eq a => a -> Prism' a ()
only n
0 (Market () () () (Identity ()) -> Market () () n (Identity n))
-> p () () -> p n n
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? p () ()
forall (p :: * -> * -> *). Monoidal p => p () ()
oneP
    upTo Int
k n
hi Bool
leading =
      let
        w :: n
w = n
base n -> Int -> n
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
        top :: n
top = n
hi n -> n -> n
forall a. Integral a => a -> a -> a
`div` n
w
        lowers :: [n]
lowers = [if Bool
leading then n
1 else n
0 .. n
top n -> n -> n
forall a. Num a => a -> a -> a
- n
1]
      in
        [n] -> Int -> p n n -> p n n
headed [n]
lowers (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> p n n
exactly' (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
        p n n -> p n n -> p n n
forall a. DisjunctiveMonoid a => a -> a -> a
>|< ((n -> n) -> (n -> Maybe n) -> Prism n n n n
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' (\n
r -> n
top n -> n -> n
forall a. Num a => a -> a -> a
* n
w n -> n -> n
forall a. Num a => a -> a -> a
+ n
r)
              (\n
n -> if n
n n -> n -> n
forall a. Integral a => a -> a -> a
`div` n
w n -> n -> Bool
forall a. Eq a => a -> a -> Bool
== n
top then n -> Maybe n
forall a. a -> Maybe a
Just (n
n n -> n -> n
forall a. Integral a => a -> a -> a
`mod` n
w) else Maybe n
forall a. Maybe a
Nothing)
              (Market n n n (Identity n) -> Market n n n (Identity n))
-> p n n -> p n n
forall (p :: * -> * -> *) s t a b.
Choice p =>
APrism s t a b -> p a b -> p s t
>? n -> p () ()
digitIs n
top p () () -> p n n -> p n n
forall (p :: * -> * -> *) c a b.
Monoidal p =>
p () c -> p a b -> p a b
>* Int -> n -> Bool -> p n n
upTo (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (n
hi n -> n -> n
forall a. Integral a => a -> a -> a
`mod` n
w) Bool
False)
    -- a digit, by its value, in either case
    digitIs :: n -> p () ()
    digitIs :: n -> p () ()
digitIs n
d =
      let c :: Char
c = ([Char
'0' .. Char
'9'] [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char
'a' .. Char
'z']) [Char] -> Int -> Char
forall a. HasCallStack => [a] -> Int -> a
!! n -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral n
d
      in if Char -> Bool
isLetter Char
c then [Char] -> [Char] -> p () ()
forall (p :: * -> * -> *).
RegGrammatical Char p =>
[Char] -> [Char] -> p () ()
precEitherOf [Char
c] [Char -> Char
toUpper Char
c] else [Char] -> p () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal [Char
c]