{-# LANGUAGE DeriveGeneric #-}
module Scrappy.Grep.DSL
( ParserExpr(..)
, MatchResult(..)
, containsRef
, extractRefs
) where
import GHC.Generics (Generic)
data ParserExpr
= PChar Char
| PString String
| PAnyChar
| PDigit
| PLetter
| PAlphaNum
| PSpace
| PSpaces
| PNewline
| POneOf String
| PNoneOf String
| PSeq ParserExpr ParserExpr
| PSeqConcat ParserExpr ParserExpr
| PAlt ParserExpr ParserExpr
| PMany ParserExpr
| PSome ParserExpr
| POptional ParserExpr
| PTry ParserExpr
| PBetween Char Char ParserExpr
| PCount Int ParserExpr
| PManyTill ParserExpr ParserExpr
| PRef String
deriving (Int -> ParserExpr -> ShowS
[ParserExpr] -> ShowS
ParserExpr -> String
(Int -> ParserExpr -> ShowS)
-> (ParserExpr -> String)
-> ([ParserExpr] -> ShowS)
-> Show ParserExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ParserExpr -> ShowS
showsPrec :: Int -> ParserExpr -> ShowS
$cshow :: ParserExpr -> String
show :: ParserExpr -> String
$cshowList :: [ParserExpr] -> ShowS
showList :: [ParserExpr] -> ShowS
Show, ParserExpr -> ParserExpr -> Bool
(ParserExpr -> ParserExpr -> Bool)
-> (ParserExpr -> ParserExpr -> Bool) -> Eq ParserExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ParserExpr -> ParserExpr -> Bool
== :: ParserExpr -> ParserExpr -> Bool
$c/= :: ParserExpr -> ParserExpr -> Bool
/= :: ParserExpr -> ParserExpr -> Bool
Eq, (forall x. ParserExpr -> Rep ParserExpr x)
-> (forall x. Rep ParserExpr x -> ParserExpr) -> Generic ParserExpr
forall x. Rep ParserExpr x -> ParserExpr
forall x. ParserExpr -> Rep ParserExpr x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ParserExpr -> Rep ParserExpr x
from :: forall x. ParserExpr -> Rep ParserExpr x
$cto :: forall x. Rep ParserExpr x -> ParserExpr
to :: forall x. Rep ParserExpr x -> ParserExpr
Generic)
data MatchResult = MatchResult
{ MatchResult -> String
mrFilePath :: FilePath
, MatchResult -> Int
mrLine :: Int
, MatchResult -> Int
mrCol :: Int
, MatchResult -> String
mrMatchText :: String
} deriving (Int -> MatchResult -> ShowS
[MatchResult] -> ShowS
MatchResult -> String
(Int -> MatchResult -> ShowS)
-> (MatchResult -> String)
-> ([MatchResult] -> ShowS)
-> Show MatchResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MatchResult -> ShowS
showsPrec :: Int -> MatchResult -> ShowS
$cshow :: MatchResult -> String
show :: MatchResult -> String
$cshowList :: [MatchResult] -> ShowS
showList :: [MatchResult] -> ShowS
Show, MatchResult -> MatchResult -> Bool
(MatchResult -> MatchResult -> Bool)
-> (MatchResult -> MatchResult -> Bool) -> Eq MatchResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MatchResult -> MatchResult -> Bool
== :: MatchResult -> MatchResult -> Bool
$c/= :: MatchResult -> MatchResult -> Bool
/= :: MatchResult -> MatchResult -> Bool
Eq, (forall x. MatchResult -> Rep MatchResult x)
-> (forall x. Rep MatchResult x -> MatchResult)
-> Generic MatchResult
forall x. Rep MatchResult x -> MatchResult
forall x. MatchResult -> Rep MatchResult x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MatchResult -> Rep MatchResult x
from :: forall x. MatchResult -> Rep MatchResult x
$cto :: forall x. Rep MatchResult x -> MatchResult
to :: forall x. Rep MatchResult x -> MatchResult
Generic)
containsRef :: ParserExpr -> Bool
containsRef :: ParserExpr -> Bool
containsRef ParserExpr
expr = case ParserExpr
expr of
PRef String
_ -> Bool
True
PSeq ParserExpr
a ParserExpr
b -> ParserExpr -> Bool
containsRef ParserExpr
a Bool -> Bool -> Bool
|| ParserExpr -> Bool
containsRef ParserExpr
b
PSeqConcat ParserExpr
a ParserExpr
b -> ParserExpr -> Bool
containsRef ParserExpr
a Bool -> Bool -> Bool
|| ParserExpr -> Bool
containsRef ParserExpr
b
PAlt ParserExpr
a ParserExpr
b -> ParserExpr -> Bool
containsRef ParserExpr
a Bool -> Bool -> Bool
|| ParserExpr -> Bool
containsRef ParserExpr
b
PMany ParserExpr
a -> ParserExpr -> Bool
containsRef ParserExpr
a
PSome ParserExpr
a -> ParserExpr -> Bool
containsRef ParserExpr
a
POptional ParserExpr
a -> ParserExpr -> Bool
containsRef ParserExpr
a
PTry ParserExpr
a -> ParserExpr -> Bool
containsRef ParserExpr
a
PBetween Char
_ Char
_ ParserExpr
a -> ParserExpr -> Bool
containsRef ParserExpr
a
PCount Int
_ ParserExpr
a -> ParserExpr -> Bool
containsRef ParserExpr
a
PManyTill ParserExpr
a ParserExpr
b -> ParserExpr -> Bool
containsRef ParserExpr
a Bool -> Bool -> Bool
|| ParserExpr -> Bool
containsRef ParserExpr
b
ParserExpr
_ -> Bool
False
extractRefs :: ParserExpr -> [String]
ParserExpr
expr = case ParserExpr
expr of
PRef String
name -> [String
name]
PSeq ParserExpr
a ParserExpr
b -> ParserExpr -> [String]
extractRefs ParserExpr
a [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ ParserExpr -> [String]
extractRefs ParserExpr
b
PSeqConcat ParserExpr
a ParserExpr
b -> ParserExpr -> [String]
extractRefs ParserExpr
a [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ ParserExpr -> [String]
extractRefs ParserExpr
b
PAlt ParserExpr
a ParserExpr
b -> ParserExpr -> [String]
extractRefs ParserExpr
a [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ ParserExpr -> [String]
extractRefs ParserExpr
b
PMany ParserExpr
a -> ParserExpr -> [String]
extractRefs ParserExpr
a
PSome ParserExpr
a -> ParserExpr -> [String]
extractRefs ParserExpr
a
POptional ParserExpr
a -> ParserExpr -> [String]
extractRefs ParserExpr
a
PTry ParserExpr
a -> ParserExpr -> [String]
extractRefs ParserExpr
a
PBetween Char
_ Char
_ ParserExpr
a -> ParserExpr -> [String]
extractRefs ParserExpr
a
PCount Int
_ ParserExpr
a -> ParserExpr -> [String]
extractRefs ParserExpr
a
PManyTill ParserExpr
a ParserExpr
b -> ParserExpr -> [String]
extractRefs ParserExpr
a [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ ParserExpr -> [String]
extractRefs ParserExpr
b
ParserExpr
_ -> []