{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
-- | This is a POSIX version of parseRegex that allows NUL characters.
-- Lazy\/Possessive\/Backrefs are not recognized.  Anchors \^ and \$ are
-- recognized.
--
-- The PGroup returned always have (Maybe GroupIndex) set to (Just _)
-- and never to Nothing.
module Text.Regex.TDFA.ReadRegex (parseRegex) where

{- By Chris Kuklewicz, 2007. BSD License, see the LICENSE file. -}

import Text.Regex.TDFA.Pattern {- all -}
import Text.ParserCombinators.Parsec((<|>), (<?>),
  unexpected, try, runParser, many, getState, setState, CharParser, ParseError,
  sepBy1, option, notFollowedBy, many1, lookAhead, eof, between,
  string, noneOf, digit, char, anyChar)
import Control.Monad(liftM, when, guard)
import qualified Data.Set as Set(fromList)

-- | BracketElement is internal to this module
data BracketElement = BEChar Char | BEChars String | BEColl String | BEEquiv String | BEClass String

-- | Return either an error message or a tuple of the Pattern and the
-- largest group index and the largest DoPa index (both have smallest
-- index of 1).  Since the regular expression is supplied as [Char] it
-- automatically supports unicode and @\\NUL@ characters.
parseRegex :: String -> Either ParseError (Pattern,(GroupIndex,DoPa))
parseRegex :: [Char] -> Either ParseError (Pattern, (GroupIndex, DoPa))
parseRegex [Char]
x = GenParser
  Char (GroupIndex, GroupIndex) (Pattern, (GroupIndex, DoPa))
-> (GroupIndex, GroupIndex)
-> [Char]
-> [Char]
-> Either ParseError (Pattern, (GroupIndex, DoPa))
forall tok st a.
GenParser tok st a -> st -> [Char] -> [tok] -> Either ParseError a
runParser (do Pattern
pat <- CharParser (GroupIndex, GroupIndex) Pattern
p_regex
                             ParsecT [Char] (GroupIndex, GroupIndex) Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof
                             (GroupIndex
lastGroupIndex,GroupIndex
lastDopa) <- ParsecT
  [Char] (GroupIndex, GroupIndex) Identity (GroupIndex, GroupIndex)
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
                             (Pattern, (GroupIndex, DoPa))
-> GenParser
     Char (GroupIndex, GroupIndex) (Pattern, (GroupIndex, DoPa))
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern
pat,(GroupIndex
lastGroupIndex,GroupIndex -> DoPa
DoPa GroupIndex
lastDopa))) (GroupIndex
0,GroupIndex
0) [Char]
x [Char]
x

p_regex :: CharParser (GroupIndex,Int) Pattern
p_regex :: CharParser (GroupIndex, GroupIndex) Pattern
p_regex = ([Pattern] -> Pattern)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [Pattern] -> Pattern
POr (ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
 -> CharParser (GroupIndex, GroupIndex) Pattern)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
-> CharParser (GroupIndex, GroupIndex) Pattern
forall a b. (a -> b) -> a -> b
$ CharParser (GroupIndex, GroupIndex) Pattern
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
sepBy1 CharParser (GroupIndex, GroupIndex) Pattern
p_branch (Char -> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'|')

-- man re_format helps alot, it says one-or-more pieces so this is
-- many1 not many.  Use "()" to indicate an empty piece.
p_branch :: CharParser (GroupIndex, GroupIndex) Pattern
p_branch = ([Pattern] -> Pattern)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [Pattern] -> Pattern
PConcat (ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
 -> CharParser (GroupIndex, GroupIndex) Pattern)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
-> CharParser (GroupIndex, GroupIndex) Pattern
forall a b. (a -> b) -> a -> b
$ CharParser (GroupIndex, GroupIndex) Pattern
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Pattern]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 CharParser (GroupIndex, GroupIndex) Pattern
p_piece

p_piece :: CharParser (GroupIndex, GroupIndex) Pattern
p_piece = (CharParser (GroupIndex, GroupIndex) Pattern
p_anchor CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (GroupIndex, GroupIndex) Pattern
p_atom) CharParser (GroupIndex, GroupIndex) Pattern
-> (Pattern -> CharParser (GroupIndex, GroupIndex) Pattern)
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pattern -> CharParser (GroupIndex, GroupIndex) Pattern
forall {u}. Pattern -> ParsecT [Char] u Identity Pattern
p_post_atom -- correct specification

p_atom :: CharParser (GroupIndex, GroupIndex) Pattern
p_atom =  CharParser (GroupIndex, GroupIndex) Pattern
p_group CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (GroupIndex, GroupIndex) Pattern
forall {a}. ParsecT [Char] (a, GroupIndex) Identity Pattern
p_bracket CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (GroupIndex, GroupIndex) Pattern
forall {a}. ParsecT [Char] (a, GroupIndex) Identity Pattern
p_char CharParser (GroupIndex, GroupIndex) Pattern
-> [Char] -> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> [Char]
"an atom"

group_index :: CharParser (GroupIndex,Int) (Maybe GroupIndex)
group_index :: CharParser (GroupIndex, GroupIndex) (Maybe GroupIndex)
group_index = do
  (GroupIndex
gi,GroupIndex
ci) <- ParsecT
  [Char] (GroupIndex, GroupIndex) Identity (GroupIndex, GroupIndex)
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  let index :: GroupIndex
index = GroupIndex -> GroupIndex
forall a. Enum a => a -> a
succ GroupIndex
gi
  (GroupIndex, GroupIndex)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity ()
forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState (GroupIndex
index,GroupIndex
ci)
  Maybe GroupIndex
-> CharParser (GroupIndex, GroupIndex) (Maybe GroupIndex)
forall (m :: * -> *) a. Monad m => a -> m a
return (GroupIndex -> Maybe GroupIndex
forall a. a -> Maybe a
Just GroupIndex
index)

p_group :: CharParser (GroupIndex, GroupIndex) Pattern
p_group = ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (Char -> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> do
  Maybe GroupIndex
index <- CharParser (GroupIndex, GroupIndex) (Maybe GroupIndex)
group_index
  (Pattern -> Pattern)
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Maybe GroupIndex -> Pattern -> Pattern
PGroup Maybe GroupIndex
index) (CharParser (GroupIndex, GroupIndex) Pattern
 -> CharParser (GroupIndex, GroupIndex) Pattern)
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall a b. (a -> b) -> a -> b
$ ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between (Char -> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') (Char -> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')') CharParser (GroupIndex, GroupIndex) Pattern
p_regex

-- p_post_atom takes the previous atom as a parameter
p_post_atom :: Pattern -> ParsecT [Char] u Identity Pattern
p_post_atom Pattern
atom = (Char -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'?' ParsecT [Char] u Identity Char
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Pattern -> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> Pattern
PQuest Pattern
atom))
               ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (Char -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'+' ParsecT [Char] u Identity Char
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Pattern -> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> Pattern
PPlus Pattern
atom))
               ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (Char -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'*' ParsecT [Char] u Identity Char
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Pattern -> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Pattern -> Pattern
PStar Bool
True Pattern
atom))
               ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Pattern -> ParsecT [Char] u Identity Pattern
forall {u}. Pattern -> ParsecT [Char] u Identity Pattern
p_bound Pattern
atom
               ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
-> ParsecT [Char] u Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Pattern -> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return Pattern
atom

p_bound :: Pattern -> GenParser Char st Pattern
p_bound Pattern
atom = GenParser Char st Pattern -> GenParser Char st Pattern
forall tok st a. GenParser tok st a -> GenParser tok st a
try (GenParser Char st Pattern -> GenParser Char st Pattern)
-> GenParser Char st Pattern -> GenParser Char st Pattern
forall a b. (a -> b) -> a -> b
$ ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity Char
-> GenParser Char st Pattern
-> GenParser Char st Pattern
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between (Char -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'{') (Char -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'}') (Pattern -> GenParser Char st Pattern
forall {u}. Pattern -> ParsecT [Char] u Identity Pattern
p_bound_spec Pattern
atom)

p_bound_spec :: Pattern -> ParsecT [Char] u Identity Pattern
p_bound_spec Pattern
atom = do [Char]
lowS <- ParsecT [Char] u Identity Char -> ParsecT [Char] u Identity [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT [Char] u Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit
                       let lowI :: GroupIndex
lowI = [Char] -> GroupIndex
forall a. Read a => [Char] -> a
read [Char]
lowS
                       Maybe GroupIndex
highMI <- Maybe GroupIndex
-> ParsecT [Char] u Identity (Maybe GroupIndex)
-> ParsecT [Char] u Identity (Maybe GroupIndex)
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option (GroupIndex -> Maybe GroupIndex
forall a. a -> Maybe a
Just GroupIndex
lowI) (ParsecT [Char] u Identity (Maybe GroupIndex)
 -> ParsecT [Char] u Identity (Maybe GroupIndex))
-> ParsecT [Char] u Identity (Maybe GroupIndex)
-> ParsecT [Char] u Identity (Maybe GroupIndex)
forall a b. (a -> b) -> a -> b
$ ParsecT [Char] u Identity (Maybe GroupIndex)
-> ParsecT [Char] u Identity (Maybe GroupIndex)
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT [Char] u Identity (Maybe GroupIndex)
 -> ParsecT [Char] u Identity (Maybe GroupIndex))
-> ParsecT [Char] u Identity (Maybe GroupIndex)
-> ParsecT [Char] u Identity (Maybe GroupIndex)
forall a b. (a -> b) -> a -> b
$ do
                                   Char
_ <- Char -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
','
  -- parsec note: if 'many digits' fails below then the 'try' ensures
  -- that the ',' will not match the closing '}' in p_bound, same goes
  -- for any non '}' garbage after the 'many digits'.
                                   [Char]
highS <- ParsecT [Char] u Identity Char -> ParsecT [Char] u Identity [Char]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Char] u Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit
                                   if [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
highS then Maybe GroupIndex -> ParsecT [Char] u Identity (Maybe GroupIndex)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe GroupIndex
forall a. Maybe a
Nothing -- no upper bound
                                     else do let highI :: GroupIndex
highI = [Char] -> GroupIndex
forall a. Read a => [Char] -> a
read [Char]
highS
                                             Bool -> ParsecT [Char] u Identity ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (GroupIndex
lowI GroupIndex -> GroupIndex -> Bool
forall a. Ord a => a -> a -> Bool
<= GroupIndex
highI)
                                             Maybe GroupIndex -> ParsecT [Char] u Identity (Maybe GroupIndex)
forall (m :: * -> *) a. Monad m => a -> m a
return (GroupIndex -> Maybe GroupIndex
forall a. a -> Maybe a
Just ([Char] -> GroupIndex
forall a. Read a => [Char] -> a
read [Char]
highS))
                       Pattern -> ParsecT [Char] u Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (GroupIndex -> Maybe GroupIndex -> Pattern -> Pattern
PBound GroupIndex
lowI Maybe GroupIndex
highMI Pattern
atom)

-- An anchor cannot be modified by a repetition specifier
p_anchor :: CharParser (GroupIndex, GroupIndex) Pattern
p_anchor = (Char -> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'^' ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (DoPa -> Pattern)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity DoPa
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM DoPa -> Pattern
PCarat ParsecT [Char] (GroupIndex, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index)
       CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (Char -> ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'$' ParsecT [Char] (GroupIndex, GroupIndex) Identity Char
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (DoPa -> Pattern)
-> ParsecT [Char] (GroupIndex, GroupIndex) Identity DoPa
-> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM DoPa -> Pattern
PDollar ParsecT [Char] (GroupIndex, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index)
       CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (GroupIndex, GroupIndex) Pattern
-> CharParser (GroupIndex, GroupIndex) Pattern
forall tok st a. GenParser tok st a -> GenParser tok st a
try (do [Char]
_ <- [Char] -> ParsecT [Char] (GroupIndex, GroupIndex) Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
"()"
                   Maybe GroupIndex
index <- CharParser (GroupIndex, GroupIndex) (Maybe GroupIndex)
group_index
                   Pattern -> CharParser (GroupIndex, GroupIndex) Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> CharParser (GroupIndex, GroupIndex) Pattern)
-> Pattern -> CharParser (GroupIndex, GroupIndex) Pattern
forall a b. (a -> b) -> a -> b
$ Maybe GroupIndex -> Pattern -> Pattern
PGroup Maybe GroupIndex
index Pattern
PEmpty)
       CharParser (GroupIndex, GroupIndex) Pattern
-> [Char] -> CharParser (GroupIndex, GroupIndex) Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> [Char]
"empty () or anchor ^ or $"

char_index :: ParsecT s (a, GroupIndex) Identity DoPa
char_index = do (a
gi,GroupIndex
ci) <- ParsecT s (a, GroupIndex) Identity (a, GroupIndex)
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
                let ci' :: GroupIndex
ci' = GroupIndex -> GroupIndex
forall a. Enum a => a -> a
succ GroupIndex
ci
                (a, GroupIndex) -> ParsecT s (a, GroupIndex) Identity ()
forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState (a
gi,GroupIndex
ci')
                DoPa -> ParsecT s (a, GroupIndex) Identity DoPa
forall (m :: * -> *) a. Monad m => a -> m a
return (GroupIndex -> DoPa
DoPa GroupIndex
ci')

p_char :: ParsecT [Char] (a, GroupIndex) Identity Pattern
p_char = ParsecT [Char] (a, GroupIndex) Identity Pattern
forall {a}. ParsecT [Char] (a, GroupIndex) Identity Pattern
p_dot ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall {a}. ParsecT [Char] (a, GroupIndex) Identity Pattern
p_left_brace ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall {a}. ParsecT [Char] (a, GroupIndex) Identity Pattern
p_escaped ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall {a}. ParsecT [Char] (a, GroupIndex) Identity Pattern
p_other_char where
  p_dot :: ParsecT [Char] (a, GroupIndex) Identity Pattern
p_dot = Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'.' ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity DoPa
-> ParsecT [Char] (a, GroupIndex) Identity DoPa
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Char] (a, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index ParsecT [Char] (a, GroupIndex) Identity DoPa
-> (DoPa -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> (DoPa -> Pattern)
-> DoPa
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DoPa -> Pattern
PDot
  p_left_brace :: GenParser Char (a, GroupIndex) Pattern
p_left_brace = GenParser Char (a, GroupIndex) Pattern
-> GenParser Char (a, GroupIndex) Pattern
forall tok st a. GenParser tok st a -> GenParser tok st a
try (GenParser Char (a, GroupIndex) Pattern
 -> GenParser Char (a, GroupIndex) Pattern)
-> GenParser Char (a, GroupIndex) Pattern
-> GenParser Char (a, GroupIndex) Pattern
forall a b. (a -> b) -> a -> b
$ (Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'{' ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity ()
-> ParsecT [Char] (a, GroupIndex) Identity ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit ParsecT [Char] (a, GroupIndex) Identity ()
-> ParsecT [Char] (a, GroupIndex) Identity DoPa
-> ParsecT [Char] (a, GroupIndex) Identity DoPa
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Char] (a, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index ParsecT [Char] (a, GroupIndex) Identity DoPa
-> (DoPa -> GenParser Char (a, GroupIndex) Pattern)
-> GenParser Char (a, GroupIndex) Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pattern -> GenParser Char (a, GroupIndex) Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> GenParser Char (a, GroupIndex) Pattern)
-> (DoPa -> Pattern)
-> DoPa
-> GenParser Char (a, GroupIndex) Pattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DoPa -> Char -> Pattern
`PChar` Char
'{'))
  p_escaped :: ParsecT [Char] (a, GroupIndex) Identity Pattern
p_escaped = Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'\\' ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity Char
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar ParsecT [Char] (a, GroupIndex) Identity Char
-> (Char -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Char
c -> ParsecT [Char] (a, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index ParsecT [Char] (a, GroupIndex) Identity DoPa
-> (DoPa -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> (DoPa -> Pattern)
-> DoPa
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DoPa -> Char -> Pattern
`PEscape` Char
c)
  p_other_char :: ParsecT [Char] (a, GroupIndex) Identity Pattern
p_other_char = [Char] -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
specials ParsecT [Char] (a, GroupIndex) Identity Char
-> (Char -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Char
c -> ParsecT [Char] (a, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index ParsecT [Char] (a, GroupIndex) Identity DoPa
-> (DoPa -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> (DoPa -> Pattern)
-> DoPa
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DoPa -> Char -> Pattern
`PChar` Char
c)
    where specials :: [Char]
specials  = [Char]
"^.[$()|*+?{\\"

-- parse [bar] and [^bar] sets of characters
p_bracket :: ParsecT [Char] (a, GroupIndex) Identity Pattern
p_bracket = (Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'[') ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ( (Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'^' ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall {a}. Bool -> ParsecT [Char] (a, GroupIndex) Identity Pattern
p_set Bool
True) ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (Bool -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall {a}. Bool -> ParsecT [Char] (a, GroupIndex) Identity Pattern
p_set Bool
False) )

-- p_set :: Bool -> GenParser Char st Pattern
p_set :: Bool -> ParsecT [Char] (a, GroupIndex) Identity Pattern
p_set Bool
invert = do [Char]
initial <- ([Char]
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [Char]
"" ((Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']' ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> ParsecT [Char] (a, GroupIndex) Identity [Char]
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
"]") ParsecT [Char] (a, GroupIndex) Identity [Char]
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'-' ParsecT [Char] (a, GroupIndex) Identity Char
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
-> ParsecT [Char] (a, GroupIndex) Identity [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> ParsecT [Char] (a, GroupIndex) Identity [Char]
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
"-")))
                  [BracketElement]
values <- if [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
initial then ParsecT [Char] (a, GroupIndex) Identity BracketElement
-> ParsecT [Char] (a, GroupIndex) Identity [BracketElement]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT [Char] (a, GroupIndex) Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem else ParsecT [Char] (a, GroupIndex) Identity BracketElement
-> ParsecT [Char] (a, GroupIndex) Identity [BracketElement]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Char] (a, GroupIndex) Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem
                  Char
_ <- Char -> ParsecT [Char] (a, GroupIndex) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']'
                  DoPa
ci <- ParsecT [Char] (a, GroupIndex) Identity DoPa
forall {s} {a}. ParsecT s (a, GroupIndex) Identity DoPa
char_index
                  let chars :: Maybe (Set Char)
chars = [Char] -> Maybe (Set Char)
forall {a}. Ord a => [a] -> Maybe (Set a)
maybe'set ([Char] -> Maybe (Set Char)) -> [Char] -> Maybe (Set Char)
forall a b. (a -> b) -> a -> b
$ [Char]
initial
                                          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char
c | BEChar Char
c <- [BracketElement]
values ]
                                          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Char]
s | BEChars [Char]
s <- [BracketElement]
values ]
                      colls :: Maybe (Set PatternSetCollatingElement)
colls = [PatternSetCollatingElement]
-> Maybe (Set PatternSetCollatingElement)
forall {a}. Ord a => [a] -> Maybe (Set a)
maybe'set [[Char] -> PatternSetCollatingElement
PatternSetCollatingElement [Char]
coll | BEColl [Char]
coll <- [BracketElement]
values ]
                      equivs :: Maybe (Set PatternSetEquivalenceClass)
equivs = [PatternSetEquivalenceClass]
-> Maybe (Set PatternSetEquivalenceClass)
forall {a}. Ord a => [a] -> Maybe (Set a)
maybe'set [[Char] -> PatternSetEquivalenceClass
PatternSetEquivalenceClass [Char]
equiv | BEEquiv [Char]
equiv <- [BracketElement]
values]
                      class's :: Maybe (Set PatternSetCharacterClass)
class's = [PatternSetCharacterClass] -> Maybe (Set PatternSetCharacterClass)
forall {a}. Ord a => [a] -> Maybe (Set a)
maybe'set [[Char] -> PatternSetCharacterClass
PatternSetCharacterClass [Char]
a'class | BEClass [Char]
a'class <- [BracketElement]
values]
                      maybe'set :: [a] -> Maybe (Set a)
maybe'set [a]
x = if [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
x then Maybe (Set a)
forall a. Maybe a
Nothing else Set a -> Maybe (Set a)
forall a. a -> Maybe a
Just ([a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList [a]
x)
                      sets :: PatternSet
sets = Maybe (Set Char)
-> Maybe (Set PatternSetCharacterClass)
-> Maybe (Set PatternSetCollatingElement)
-> Maybe (Set PatternSetEquivalenceClass)
-> PatternSet
PatternSet Maybe (Set Char)
chars Maybe (Set PatternSetCharacterClass)
class's Maybe (Set PatternSetCollatingElement)
colls Maybe (Set PatternSetEquivalenceClass)
equivs
                  PatternSet
sets PatternSet
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
-> ParsecT [Char] (a, GroupIndex) Identity Pattern
`seq` Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall (m :: * -> *) a. Monad m => a -> m a
return (Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern)
-> Pattern -> ParsecT [Char] (a, GroupIndex) Identity Pattern
forall a b. (a -> b) -> a -> b
$ if Bool
invert then DoPa -> PatternSet -> Pattern
PAnyNot DoPa
ci PatternSet
sets else DoPa -> PatternSet -> Pattern
PAny DoPa
ci PatternSet
sets

-- From here down the code is the parser and functions for pattern [ ] set things

p_set_elem :: ParsecT [Char] u Identity BracketElement
p_set_elem = ParsecT [Char] u Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem_class ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] u Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem_equiv ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] u Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem_coll
         ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] u Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem_range ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
-> ParsecT [Char] u Identity BracketElement
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT [Char] u Identity BracketElement
forall {u}. ParsecT [Char] u Identity BracketElement
p_set_elem_char ParsecT [Char] u Identity BracketElement
-> [Char] -> ParsecT [Char] u Identity BracketElement
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> [Char]
"Failed to parse bracketed string"

p_set_elem_class :: ParsecT [Char] st Identity BracketElement
p_set_elem_class = ([Char] -> BracketElement)
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity BracketElement
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [Char] -> BracketElement
BEClass (ParsecT [Char] st Identity [Char]
 -> ParsecT [Char] st Identity BracketElement)
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity BracketElement
forall a b. (a -> b) -> a -> b
$
  ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between ([Char] -> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
"[:") ([Char] -> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
":]") (ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT [Char] st Identity Char
 -> ParsecT [Char] st Identity [Char])
-> ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
":]"))

p_set_elem_equiv :: ParsecT [Char] st Identity BracketElement
p_set_elem_equiv = ([Char] -> BracketElement)
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity BracketElement
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [Char] -> BracketElement
BEEquiv (ParsecT [Char] st Identity [Char]
 -> ParsecT [Char] st Identity BracketElement)
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity BracketElement
forall a b. (a -> b) -> a -> b
$
  ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between ([Char] -> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
"[=") ([Char] -> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
"=]") (ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT [Char] st Identity Char
 -> ParsecT [Char] st Identity [Char])
-> ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"=]"))

p_set_elem_coll :: ParsecT [Char] st Identity BracketElement
p_set_elem_coll =  ([Char] -> BracketElement)
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity BracketElement
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [Char] -> BracketElement
BEColl (ParsecT [Char] st Identity [Char]
 -> ParsecT [Char] st Identity BracketElement)
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity BracketElement
forall a b. (a -> b) -> a -> b
$
  ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
-> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between ([Char] -> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
"[.") ([Char] -> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m [Char]
string [Char]
".]") (ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT [Char] st Identity Char
 -> ParsecT [Char] st Identity [Char])
-> ParsecT [Char] st Identity Char
-> ParsecT [Char] st Identity [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
".]"))

p_set_elem_range :: GenParser Char st BracketElement
p_set_elem_range = GenParser Char st BracketElement
-> GenParser Char st BracketElement
forall tok st a. GenParser tok st a -> GenParser tok st a
try (GenParser Char st BracketElement
 -> GenParser Char st BracketElement)
-> GenParser Char st BracketElement
-> GenParser Char st BracketElement
forall a b. (a -> b) -> a -> b
$ do
  Char
start <- [Char] -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"]-"
  Char
_  <- Char -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'-'
  Char
end <- [Char] -> ParsecT [Char] st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"]"
  -- bug fix: check start <= end before "return (BEChars [start..end])"
  if Char
start Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
end
    then BracketElement -> GenParser Char st BracketElement
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> BracketElement
BEChars [Char
start..Char
end])
    else [Char] -> GenParser Char st BracketElement
forall s (m :: * -> *) t u a.
Stream s m t =>
[Char] -> ParsecT s u m a
unexpected [Char]
"End point of dashed character range is less than starting point"

p_set_elem_char :: ParsecT [Char] u Identity BracketElement
p_set_elem_char = do
  Char
c <- [Char] -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"]"
  Bool
-> ParsecT [Char] u Identity () -> ParsecT [Char] u Identity ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-') (ParsecT [Char] u Identity () -> ParsecT [Char] u Identity ())
-> ParsecT [Char] u Identity () -> ParsecT [Char] u Identity ()
forall a b. (a -> b) -> a -> b
$ do
    Bool
atEnd <- (ParsecT [Char] u Identity Char -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (Char -> ParsecT [Char] u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']') ParsecT [Char] u Identity Char
-> ParsecT [Char] u Identity Bool -> ParsecT [Char] u Identity Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> ParsecT [Char] u Identity Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True) ParsecT [Char] u Identity Bool
-> ParsecT [Char] u Identity Bool -> ParsecT [Char] u Identity Bool
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (Bool -> ParsecT [Char] u Identity Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)
    Bool
-> ParsecT [Char] u Identity () -> ParsecT [Char] u Identity ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
atEnd) ([Char] -> ParsecT [Char] u Identity ()
forall s (m :: * -> *) t u a.
Stream s m t =>
[Char] -> ParsecT s u m a
unexpected [Char]
"A dash is in the wrong place in a bracket")
  BracketElement -> ParsecT [Char] u Identity BracketElement
forall (m :: * -> *) a. Monad m => a -> m a
return (Char -> BracketElement
BEChar Char
c)