{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Regex.KDE.Regex
 ( Direction(..)
 , Regex(..)
 , isWordChar
 ) where

import Data.Char
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup ((<>), Semigroup)
#endif

data Direction = Forward | Backward
  deriving (Int -> Direction -> ShowS
[Direction] -> ShowS
Direction -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Direction] -> ShowS
$cshowList :: [Direction] -> ShowS
show :: Direction -> String
$cshow :: Direction -> String
showsPrec :: Int -> Direction -> ShowS
$cshowsPrec :: Int -> Direction -> ShowS
Show, Direction -> Direction -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Direction -> Direction -> Bool
$c/= :: Direction -> Direction -> Bool
== :: Direction -> Direction -> Bool
$c== :: Direction -> Direction -> Bool
Eq)

data Regex =
  MatchAnyChar |
  MatchDynamic !Int |
  MatchChar (Char -> Bool) |
  MatchSome !Regex |
  MatchAlt !Regex !Regex |
  MatchConcat !Regex !Regex |
  MatchCapture !Int !Regex |
  MatchCaptured !Int |
  AssertWordBoundary |
  AssertBeginning |
  AssertEnd |
  AssertPositive !Direction !Regex |
  AssertNegative !Direction !Regex |
  Possessive !Regex |
  Lazy !Regex |
  Subroutine !Int |
  MatchNull

instance Show Regex where
  show :: Regex -> String
show Regex
MatchAnyChar = String
"MatchAnyChar"
  show (MatchDynamic Int
i) = String
"MatchDynamic " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
i
  show (MatchChar Char -> Bool
_) = String
"(MatchChar <fn>)"
  show (MatchSome Regex
re) = String
"(MatchSome " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
re forall a. Semigroup a => a -> a -> a
<> String
")"
  show (MatchAlt Regex
r1 Regex
r2) = String
"(MatchAlt " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
r1 forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
r2 forall a. Semigroup a => a -> a -> a
<> String
")"
  show (MatchConcat Regex
r1 Regex
r2) = String
"(MatchConcat " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
r1 forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
r2 forall a. Semigroup a => a -> a -> a
<>
            String
")"
  show (MatchCapture Int
i Regex
re) = String
"(MatchCapture " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
i forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<>
                forall a. Show a => a -> String
show Regex
re forall a. Semigroup a => a -> a -> a
<> String
")"
  show (MatchCaptured Int
n) = String
"(MatchCaptured " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
n forall a. Semigroup a => a -> a -> a
<> String
")"
  show Regex
AssertWordBoundary = String
"AssertWordBoundary"
  show Regex
AssertBeginning = String
"AssertBeginning"
  show Regex
AssertEnd = String
"AssertEnd"
  show (AssertPositive Direction
dir Regex
re) = String
"(AssertPositive " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Direction
dir forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<>
                  forall a. Show a => a -> String
show Regex
re forall a. Semigroup a => a -> a -> a
<> String
")"
  show (AssertNegative Direction
dir Regex
re) = String
"(AssertNegativeLookahead " forall a. Semigroup a => a -> a -> a
<>
                  forall a. Show a => a -> String
show Direction
dir forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
re forall a. Semigroup a => a -> a -> a
<> String
")"
  show (Possessive Regex
re) = String
"(Possessive " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
re forall a. Semigroup a => a -> a -> a
<> String
")"
  show (Lazy Regex
re) = String
"(Lazy " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Regex
re forall a. Semigroup a => a -> a -> a
<> String
")"
  show (Subroutine Int
i) = String
"(Subroutine " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
i forall a. Semigroup a => a -> a -> a
<> String
")"
  show Regex
MatchNull = String
"MatchNull"

instance Semigroup Regex where
  <> :: Regex -> Regex -> Regex
(<>) = Regex -> Regex -> Regex
MatchConcat

instance Monoid Regex where
  mempty :: Regex
mempty = Regex
MatchNull
  mappend :: Regex -> Regex -> Regex
mappend = forall a. Semigroup a => a -> a -> a
(<>)

isWordChar :: Char -> Bool
isWordChar :: Char -> Bool
isWordChar Char
c = Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char -> GeneralCategory
generalCategory Char
c forall a. Eq a => a -> a -> Bool
== GeneralCategory
ConnectorPunctuation