-- RegexPRTypes.hs
--
-- Author: Yoshikuni Jujo <PAF01143@nifty.ne.jp>
--
-- This file is part of regexpr library
--
-- regexpr is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or any later version.
--
-- regexpr is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANGY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this program. If not, see
-- <http://www.gnu.org/licenses/>.

module Hidden.RegexPRTypes (
  reverseRegexAction
, RegexSrcParser
, getBR
, modifyBR
, isModeI
, isModeM
, isModeX
, setMode
, setModes
, getModes
, runRegexSrcParser
, RegexAction(..)

, RegexResult
, VerboseResult
, MatchList
, RegexParser
, runRegexParser

, MatchFun
, VerboseMatchFun
) where

import Text.ParserCombinators.MTLParse ( Parse, runParse )
import Control.Monad.State             ( StateT, runStateT, gets, modify )
import Control.Monad.Reader            ( ReaderT(runReaderT) )
import Control.Arrow                   ( first, second )

type RegexResult = ( String, (String, String) )
type MatchList   = [ (Int, String) ]
type RegexParser = ReaderT (String, String) (StateT MatchList (Parse Char))
runRegexParser ::
  (String, String) ->
  RegexParser a -> (String, String) -> [((a, MatchList), (String, String))]
runRegexParser :: forall a.
([Char], [Char])
-> RegexParser a
-> ([Char], [Char])
-> [((a, MatchList), ([Char], [Char]))]
runRegexParser ([Char], [Char])
point = forall a b. Parse a b -> ([a], [a]) -> [(b, ([a], [a]))]
runParse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ([Char], [Char])
point

type Modes = String
type RegexSrcParser = StateT (Int, Modes) (Parse Char)

getBR :: RegexSrcParser Int
getBR :: RegexSrcParser Int
getBR    = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a, b) -> a
fst

modifyBR :: (Int -> Int) -> RegexSrcParser ()
modifyBR :: (Int -> Int) -> RegexSrcParser ()
modifyBR = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first

setMode :: Char -> Bool -> RegexSrcParser ()
setMode :: Char -> Bool -> RegexSrcParser ()
setMode Char
c Bool
True  = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (Char
cforall a. a -> [a] -> [a]
:)
setMode Char
c Bool
False = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/=Char
c))

getModes :: RegexSrcParser Modes
getModes :: RegexSrcParser [Char]
getModes = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a, b) -> b
snd
setModes :: Modes -> RegexSrcParser ()
setModes :: [Char] -> RegexSrcParser ()
setModes [Char]
ms = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const [Char]
ms

isModeI, isModeM, isModeX :: RegexSrcParser Bool
isModeI :: RegexSrcParser Bool
isModeI = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Char
'i' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd
isModeM :: RegexSrcParser Bool
isModeM = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Char
'm' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd
isModeX :: RegexSrcParser Bool
isModeX = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Char
'x' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd
runRegexSrcParser :: RegexSrcParser a -> Parse Char (a, (Int,String))
runRegexSrcParser :: forall a. RegexSrcParser a -> Parse Char (a, (Int, [Char]))
runRegexSrcParser = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (Int
1, [Char]
"")

data RegexAction = Select (Char -> Bool)                           |
                   Repeat          Int (Maybe Int) RegexAction     |
                   RepeatNotGreedy Int (Maybe Int) RegexAction     |
		   RegexOr [RegexAction] [RegexAction]             |
                   Note Int [RegexAction] | BackReference Int      |
		   Still [RegexAction]    | Backword [RegexAction] |
		   RegActNot [RegexAction]                         |
		   BeginningOfInput       | EndOfInput             |
		   PreMatchPoint          | Parens [RegexAction]   |
		   Comment String         | NopRegex               |
		   NoBacktrack [RegexAction]

reverseRegexAction :: RegexAction -> RegexAction
reverseRegexAction :: RegexAction -> RegexAction
reverseRegexAction (Note Int
i [RegexAction]
ras)
  = Int -> [RegexAction] -> RegexAction
Note Int
i forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map RegexAction -> RegexAction
reverseRegexAction [RegexAction]
ras
reverseRegexAction (Parens [RegexAction]
ras)
  = [RegexAction] -> RegexAction
Parens forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map RegexAction -> RegexAction
reverseRegexAction [RegexAction]
ras
reverseRegexAction (RegexOr [RegexAction]
ras1 [RegexAction]
ras2)
  = [RegexAction] -> [RegexAction] -> RegexAction
RegexOr (forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map RegexAction -> RegexAction
reverseRegexAction [RegexAction]
ras1)
            (forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map RegexAction -> RegexAction
reverseRegexAction [RegexAction]
ras2)
-- reverseRegexAction (Still ras)
--  = Still $ reverse $ map reverseRegexAction ras
-- reverseRegexAction (Backword ras)
--  = Backword $ reverse $ map reverseRegexAction ras
reverseRegexAction RegexAction
ra = RegexAction
ra

type MatchFun f
  = String -> String -> f ( RegexResult, MatchList )

type VerboseResult = ( String, String, (String, String) )
type VerboseMatchFun f
  = String -> (String, String) -> f ( VerboseResult, MatchList )