{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternGuards, ViewPatterns, TupleSections #-}
module Development.Shake.Internal.FilePattern(
FilePattern, (?==), (<//>),
filePattern,
simple, (?==*),
compatible, extract, substitute,
Walk(..), walk,
internalTest, isRelativePath, isRelativePattern
) where
import Development.Shake.Internal.Errors
import System.FilePath(isPathSeparator)
import Data.List.Extra
import Control.Monad
import Data.Char
import Data.Maybe
import System.Info.Extra
type FilePattern = String
infixr 5 <//>
(<//>) :: FilePattern -> FilePattern -> FilePattern
FilePattern
a <//> :: FilePattern -> FilePattern -> FilePattern
<//> FilePattern
b = (Char -> Bool) -> FilePattern -> FilePattern
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd Char -> Bool
isPathSeparator FilePattern
a FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
"//" FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ (Char -> Bool) -> FilePattern -> FilePattern
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isPathSeparator FilePattern
b
data Pat = Lit String
| Star
| Skip
| Skip1
| Stars String [String] String
deriving (Int -> Pat -> FilePattern -> FilePattern
[Pat] -> FilePattern -> FilePattern
Pat -> FilePattern
(Int -> Pat -> FilePattern -> FilePattern)
-> (Pat -> FilePattern)
-> ([Pat] -> FilePattern -> FilePattern)
-> Show Pat
forall a.
(Int -> a -> FilePattern -> FilePattern)
-> (a -> FilePattern)
-> ([a] -> FilePattern -> FilePattern)
-> Show a
showList :: [Pat] -> FilePattern -> FilePattern
$cshowList :: [Pat] -> FilePattern -> FilePattern
show :: Pat -> FilePattern
$cshow :: Pat -> FilePattern
showsPrec :: Int -> Pat -> FilePattern -> FilePattern
$cshowsPrec :: Int -> Pat -> FilePattern -> FilePattern
Show,Pat -> Pat -> Bool
(Pat -> Pat -> Bool) -> (Pat -> Pat -> Bool) -> Eq Pat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Pat -> Pat -> Bool
$c/= :: Pat -> Pat -> Bool
== :: Pat -> Pat -> Bool
$c== :: Pat -> Pat -> Bool
Eq,Eq Pat
Eq Pat
-> (Pat -> Pat -> Ordering)
-> (Pat -> Pat -> Bool)
-> (Pat -> Pat -> Bool)
-> (Pat -> Pat -> Bool)
-> (Pat -> Pat -> Bool)
-> (Pat -> Pat -> Pat)
-> (Pat -> Pat -> Pat)
-> Ord Pat
Pat -> Pat -> Bool
Pat -> Pat -> Ordering
Pat -> Pat -> Pat
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Pat -> Pat -> Pat
$cmin :: Pat -> Pat -> Pat
max :: Pat -> Pat -> Pat
$cmax :: Pat -> Pat -> Pat
>= :: Pat -> Pat -> Bool
$c>= :: Pat -> Pat -> Bool
> :: Pat -> Pat -> Bool
$c> :: Pat -> Pat -> Bool
<= :: Pat -> Pat -> Bool
$c<= :: Pat -> Pat -> Bool
< :: Pat -> Pat -> Bool
$c< :: Pat -> Pat -> Bool
compare :: Pat -> Pat -> Ordering
$ccompare :: Pat -> Pat -> Ordering
$cp1Ord :: Eq Pat
Ord)
fromLit :: Pat -> Maybe String
fromLit :: Pat -> Maybe FilePattern
fromLit (Lit FilePattern
x) = FilePattern -> Maybe FilePattern
forall a. a -> Maybe a
Just FilePattern
x
fromLit Pat
_ = Maybe FilePattern
forall a. Maybe a
Nothing
data Lexeme = Str String | Slash | SlashSlash
lexer :: FilePattern -> [Lexeme]
lexer :: FilePattern -> [Lexeme]
lexer FilePattern
"" = []
lexer (Char
x1:Char
x2:FilePattern
xs) | Char -> Bool
isPathSeparator Char
x1, Char -> Bool
isPathSeparator Char
x2 = Lexeme
SlashSlash Lexeme -> [Lexeme] -> [Lexeme]
forall a. a -> [a] -> [a]
: FilePattern -> [Lexeme]
lexer FilePattern
xs
lexer (Char
x1:FilePattern
xs) | Char -> Bool
isPathSeparator Char
x1 = Lexeme
Slash Lexeme -> [Lexeme] -> [Lexeme]
forall a. a -> [a] -> [a]
: FilePattern -> [Lexeme]
lexer FilePattern
xs
lexer FilePattern
xs = FilePattern -> Lexeme
Str FilePattern
a Lexeme -> [Lexeme] -> [Lexeme]
forall a. a -> [a] -> [a]
: FilePattern -> [Lexeme]
lexer FilePattern
b
where (FilePattern
a,FilePattern
b) = (Char -> Bool) -> FilePattern -> (FilePattern, FilePattern)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Char -> Bool
isPathSeparator FilePattern
xs
parse :: FilePattern -> [Pat]
parse :: FilePattern -> [Pat]
parse = Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
False Bool
True ([Lexeme] -> [Pat])
-> (FilePattern -> [Lexeme]) -> FilePattern -> [Pat]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePattern -> [Lexeme]
lexer
where
f :: Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
str Bool
slash = \case
[] -> [FilePattern -> Pat
Lit FilePattern
"" | Bool
slash]
Str FilePattern
"**":[Lexeme]
xs -> Pat
Skip Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
: Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
True Bool
False [Lexeme]
xs
Str FilePattern
x:[Lexeme]
xs -> FilePattern -> Pat
parseLit FilePattern
x Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
: Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
True Bool
False [Lexeme]
xs
Lexeme
SlashSlash:Lexeme
Slash:[Lexeme]
xs | Bool -> Bool
not Bool
str -> Pat
Skip1 Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
: Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
str Bool
True [Lexeme]
xs
Lexeme
SlashSlash:[Lexeme]
xs -> Pat
Skip Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
: Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
str Bool
False [Lexeme]
xs
Lexeme
Slash:[Lexeme]
xs -> [FilePattern -> Pat
Lit FilePattern
"" | Bool -> Bool
not Bool
str] [Pat] -> [Pat] -> [Pat]
forall a. [a] -> [a] -> [a]
++ Bool -> Bool -> [Lexeme] -> [Pat]
f Bool
str Bool
True [Lexeme]
xs
parseLit :: String -> Pat
parseLit :: FilePattern -> Pat
parseLit FilePattern
"*" = Pat
Star
parseLit FilePattern
x = case (Char -> Bool) -> FilePattern -> [FilePattern]
forall a. (a -> Bool) -> [a] -> [[a]]
split (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'*') FilePattern
x of
[FilePattern
x] -> FilePattern -> Pat
Lit FilePattern
x
FilePattern
pre:[FilePattern]
xs | Just ([FilePattern]
mid,FilePattern
post) <- [FilePattern] -> Maybe ([FilePattern], FilePattern)
forall a. [a] -> Maybe ([a], a)
unsnoc [FilePattern]
xs -> FilePattern -> [FilePattern] -> FilePattern -> Pat
Stars FilePattern
pre [FilePattern]
mid FilePattern
post
[FilePattern]
_ -> FilePattern -> Pat
Lit FilePattern
""
internalTest :: IO ()
internalTest :: IO ()
internalTest = do
let FilePattern
x # :: FilePattern -> [Pat] -> f ()
# [Pat]
y = Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FilePattern -> [Pat]
parse FilePattern
x [Pat] -> [Pat] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Pat]
y) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$ FilePattern -> f ()
forall (m :: * -> *) a. MonadFail m => FilePattern -> m a
fail (FilePattern -> f ()) -> FilePattern -> f ()
forall a b. (a -> b) -> a -> b
$ (FilePattern, FilePattern, [Pat], [Pat]) -> FilePattern
forall a. Show a => a -> FilePattern
show (FilePattern
"FilePattern.internalTest",FilePattern
x,FilePattern -> [Pat]
parse FilePattern
x,[Pat]
y)
FilePattern
"" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"/" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"",FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"x/" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"/x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"",FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"x/y" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",FilePattern -> Pat
Lit FilePattern
"y"]
FilePattern
"//" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip]
FilePattern
"**" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip]
FilePattern
"//x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"**/x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"x//" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x", Pat
Skip]
FilePattern
"x/**" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x", Pat
Skip]
FilePattern
"x//y" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",Pat
Skip, FilePattern -> Pat
Lit FilePattern
"y"]
FilePattern
"x/**/y" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",Pat
Skip, FilePattern -> Pat
Lit FilePattern
"y"]
FilePattern
"///" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip1, FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"**/**" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip,Pat
Skip]
FilePattern
"**/**/" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, Pat
Skip, FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"///x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip1, FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"**/x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"x///" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x", Pat
Skip, FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"x/**/" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x", Pat
Skip, FilePattern -> Pat
Lit FilePattern
""]
FilePattern
"x///y" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",Pat
Skip, FilePattern -> Pat
Lit FilePattern
"y"]
FilePattern
"x/**/y" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",Pat
Skip, FilePattern -> Pat
Lit FilePattern
"y"]
FilePattern
"////" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, Pat
Skip]
FilePattern
"**/**/**" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, Pat
Skip, Pat
Skip]
FilePattern
"////x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, Pat
Skip, FilePattern -> Pat
Lit FilePattern
"x"]
FilePattern
"x////" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x", Pat
Skip, Pat
Skip]
FilePattern
"x////y" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [FilePattern -> Pat
Lit FilePattern
"x",Pat
Skip, Pat
Skip, FilePattern -> Pat
Lit FilePattern
"y"]
FilePattern
"**//x" FilePattern -> [Pat] -> IO ()
forall (f :: * -> *). MonadFail f => FilePattern -> [Pat] -> f ()
# [Pat
Skip, Pat
Skip, FilePattern -> Pat
Lit FilePattern
"x"]
optimise :: [Pat] -> [Pat]
optimise :: [Pat] -> [Pat]
optimise (Pat
Skip:Pat
Skip:[Pat]
xs) = [Pat] -> [Pat]
optimise ([Pat] -> [Pat]) -> [Pat] -> [Pat]
forall a b. (a -> b) -> a -> b
$ Pat
SkipPat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs
optimise (Pat
Skip:Pat
Star:[Pat]
xs) = [Pat] -> [Pat]
optimise ([Pat] -> [Pat]) -> [Pat] -> [Pat]
forall a b. (a -> b) -> a -> b
$ Pat
Skip1Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs
optimise (Pat
Star:Pat
Skip:[Pat]
xs) = [Pat] -> [Pat]
optimise ([Pat] -> [Pat]) -> [Pat] -> [Pat]
forall a b. (a -> b) -> a -> b
$ Pat
Skip1Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs
optimise (Pat
x:[Pat]
xs) = Pat
x Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
: [Pat] -> [Pat]
optimise [Pat]
xs
optimise [] =[]
isRelativePattern :: FilePattern -> Bool
isRelativePattern :: FilePattern -> Bool
isRelativePattern (Char
'*':Char
'*':FilePattern
xs)
| [] <- FilePattern
xs = Bool
True
| Char
x:FilePattern
_ <- FilePattern
xs, Char -> Bool
isPathSeparator Char
x = Bool
True
isRelativePattern FilePattern
_ = Bool
False
isRelativePath :: FilePath -> Bool
isRelativePath :: FilePattern -> Bool
isRelativePath (Char
x:FilePattern
_) | Char -> Bool
isPathSeparator Char
x = Bool
False
isRelativePath (Char
x:Char
':':FilePattern
_) | Bool
isWindows, Char -> Bool
isAlpha Char
x = Bool
False
isRelativePath FilePattern
_ = Bool
True
match :: [Pat] -> [String] -> [[String]]
match :: [Pat] -> [FilePattern] -> [[FilePattern]]
match (Pat
Skip:[Pat]
xs) (FilePattern
y:[FilePattern]
ys) = ([FilePattern] -> [FilePattern])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> [a] -> [b]
map (FilePattern
""FilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:) ([Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
xs (FilePattern
yFilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:[FilePattern]
ys)) [[FilePattern]] -> [[FilePattern]] -> [[FilePattern]]
forall a. [a] -> [a] -> [a]
++ [Pat] -> [FilePattern] -> [[FilePattern]]
match (Pat
Skip1Pat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs) (FilePattern
yFilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:[FilePattern]
ys)
match (Pat
Skip1:[Pat]
xs) (FilePattern
y:[FilePattern]
ys) = [(FilePattern
yFilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++FilePattern
"/"FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++FilePattern
r)FilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:[FilePattern]
rs | FilePattern
r:[FilePattern]
rs <- [Pat] -> [FilePattern] -> [[FilePattern]]
match (Pat
SkipPat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs) [FilePattern]
ys]
match (Pat
Skip:[Pat]
xs) [] = ([FilePattern] -> [FilePattern])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> [a] -> [b]
map (FilePattern
""FilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:) ([[FilePattern]] -> [[FilePattern]])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> a -> b
$ [Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
xs []
match (Pat
Star:[Pat]
xs) (FilePattern
y:[FilePattern]
ys) = ([FilePattern] -> [FilePattern])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> [a] -> [b]
map (FilePattern
yFilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:) ([[FilePattern]] -> [[FilePattern]])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> a -> b
$ [Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
xs [FilePattern]
ys
match (Lit FilePattern
x:[Pat]
xs) (FilePattern
y:[FilePattern]
ys) = [[[FilePattern]]] -> [[FilePattern]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[[FilePattern]]] -> [[FilePattern]])
-> [[[FilePattern]]] -> [[FilePattern]]
forall a b. (a -> b) -> a -> b
$ [[Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
xs [FilePattern]
ys | FilePattern
x FilePattern -> FilePattern -> Bool
forall a. Eq a => a -> a -> Bool
== FilePattern
y] [[[FilePattern]]] -> [[[FilePattern]]] -> [[[FilePattern]]]
forall a. [a] -> [a] -> [a]
++ [[Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
xs (FilePattern
yFilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
:[FilePattern]
ys) | FilePattern
x FilePattern -> FilePattern -> Bool
forall a. Eq a => a -> a -> Bool
== FilePattern
"."]
match (x :: Pat
x@Stars{}:[Pat]
xs) (FilePattern
y:[FilePattern]
ys) | Just [FilePattern]
rs <- Pat -> FilePattern -> Maybe [FilePattern]
matchStars Pat
x FilePattern
y = ([FilePattern] -> [FilePattern])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> [a] -> [b]
map ([FilePattern]
rs [FilePattern] -> [FilePattern] -> [FilePattern]
forall a. [a] -> [a] -> [a]
++) ([[FilePattern]] -> [[FilePattern]])
-> [[FilePattern]] -> [[FilePattern]]
forall a b. (a -> b) -> a -> b
$ [Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
xs [FilePattern]
ys
match [] [] = [[]]
match [Pat]
_ [FilePattern]
_ = []
matchOne :: Pat -> String -> Bool
matchOne :: Pat -> FilePattern -> Bool
matchOne (Lit FilePattern
x) FilePattern
y = FilePattern
x FilePattern -> FilePattern -> Bool
forall a. Eq a => a -> a -> Bool
== FilePattern
y
matchOne x :: Pat
x@Stars{} FilePattern
y = Maybe [FilePattern] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [FilePattern] -> Bool) -> Maybe [FilePattern] -> Bool
forall a b. (a -> b) -> a -> b
$ Pat -> FilePattern -> Maybe [FilePattern]
matchStars Pat
x FilePattern
y
matchOne Pat
Star FilePattern
_ = Bool
True
matchOne Pat
p FilePattern
_ = SomeException -> Bool
forall a. SomeException -> a
throwImpure (SomeException -> Bool) -> SomeException -> Bool
forall a b. (a -> b) -> a -> b
$ Partial => FilePattern -> SomeException
FilePattern -> SomeException
errorInternal (FilePattern -> SomeException) -> FilePattern -> SomeException
forall a b. (a -> b) -> a -> b
$ FilePattern
"unreachablePattern, matchOne " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ Pat -> FilePattern
forall a. Show a => a -> FilePattern
show Pat
p
matchStars :: Pat -> String -> Maybe [String]
matchStars :: Pat -> FilePattern -> Maybe [FilePattern]
matchStars (Stars FilePattern
pre [FilePattern]
mid FilePattern
post) FilePattern
x = do
FilePattern
x <- FilePattern -> FilePattern -> Maybe FilePattern
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix FilePattern
pre FilePattern
x
FilePattern
x <- if FilePattern -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePattern
post then FilePattern -> Maybe FilePattern
forall a. a -> Maybe a
Just FilePattern
x else FilePattern -> FilePattern -> Maybe FilePattern
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripSuffix FilePattern
post FilePattern
x
[FilePattern] -> FilePattern -> Maybe [FilePattern]
forall a. Eq a => [[a]] -> [a] -> Maybe [[a]]
stripInfixes [FilePattern]
mid FilePattern
x
where
stripInfixes :: [[a]] -> [a] -> Maybe [[a]]
stripInfixes [] [a]
x = [[a]] -> Maybe [[a]]
forall a. a -> Maybe a
Just [[a]
x]
stripInfixes ([a]
m:[[a]]
ms) [a]
x = do
([a]
a,[a]
x) <- [a] -> [a] -> Maybe ([a], [a])
forall a. Eq a => [a] -> [a] -> Maybe ([a], [a])
stripInfix [a]
m [a]
x
([a]
a[a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
:) ([[a]] -> [[a]]) -> Maybe [[a]] -> Maybe [[a]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[a]] -> [a] -> Maybe [[a]]
stripInfixes [[a]]
ms [a]
x
matchStars Pat
p FilePattern
_ = SomeException -> Maybe [FilePattern]
forall a. SomeException -> a
throwImpure (SomeException -> Maybe [FilePattern])
-> SomeException -> Maybe [FilePattern]
forall a b. (a -> b) -> a -> b
$ Partial => FilePattern -> SomeException
FilePattern -> SomeException
errorInternal (FilePattern -> SomeException) -> FilePattern -> SomeException
forall a b. (a -> b) -> a -> b
$ FilePattern
"unreachablePattern, matchStars " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ Pat -> FilePattern
forall a. Show a => a -> FilePattern
show Pat
p
(?==) :: FilePattern -> FilePath -> Bool
?== :: FilePattern -> FilePattern -> Bool
(?==) FilePattern
p = case [Pat] -> [Pat]
optimise ([Pat] -> [Pat]) -> [Pat] -> [Pat]
forall a b. (a -> b) -> a -> b
$ FilePattern -> [Pat]
parse FilePattern
p of
[Pat
x] | Pat
x Pat -> Pat -> Bool
forall a. Eq a => a -> a -> Bool
== Pat
Skip Bool -> Bool -> Bool
|| Pat
x Pat -> Pat -> Bool
forall a. Eq a => a -> a -> Bool
== Pat
Skip1 -> if Bool
rp then FilePattern -> Bool
isRelativePath else Bool -> FilePattern -> Bool
forall a b. a -> b -> a
const Bool
True
[Pat]
p -> let f :: FilePattern -> Bool
f = Bool -> Bool
not (Bool -> Bool) -> (FilePattern -> Bool) -> FilePattern -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[FilePattern]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([[FilePattern]] -> Bool)
-> (FilePattern -> [[FilePattern]]) -> FilePattern -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
p ([FilePattern] -> [[FilePattern]])
-> (FilePattern -> [FilePattern]) -> FilePattern -> [[FilePattern]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> FilePattern -> [FilePattern]
forall a. (a -> Bool) -> [a] -> [[a]]
split Char -> Bool
isPathSeparator
in if Bool
rp then (\FilePattern
x -> FilePattern -> Bool
isRelativePath FilePattern
x Bool -> Bool -> Bool
&& FilePattern -> Bool
f FilePattern
x) else FilePattern -> Bool
f
where rp :: Bool
rp = FilePattern -> Bool
isRelativePattern FilePattern
p
(?==*) :: [FilePattern] -> FilePath -> Bool
?==* :: [FilePattern] -> FilePattern -> Bool
(?==*) [FilePattern]
ps = \FilePattern
x -> ((FilePattern -> Bool) -> Bool) -> [FilePattern -> Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((FilePattern -> Bool) -> FilePattern -> Bool
forall a b. (a -> b) -> a -> b
$ FilePattern
x) [FilePattern -> Bool]
vs
where vs :: [FilePattern -> Bool]
vs = (FilePattern -> FilePattern -> Bool)
-> [FilePattern] -> [FilePattern -> Bool]
forall a b. (a -> b) -> [a] -> [b]
map FilePattern -> FilePattern -> Bool
(?==) [FilePattern]
ps
filePattern :: FilePattern -> FilePath -> Maybe [String]
filePattern :: FilePattern -> FilePattern -> Maybe [FilePattern]
filePattern FilePattern
p = \FilePattern
x -> if FilePattern -> Bool
eq FilePattern
x then [FilePattern] -> Maybe [FilePattern]
forall a. a -> Maybe a
Just ([FilePattern] -> Maybe [FilePattern])
-> [FilePattern] -> Maybe [FilePattern]
forall a b. (a -> b) -> a -> b
$ FilePattern -> [FilePattern]
ex FilePattern
x else Maybe [FilePattern]
forall a. Maybe a
Nothing
where eq :: FilePattern -> Bool
eq = FilePattern -> FilePattern -> Bool
(?==) FilePattern
p
ex :: FilePattern -> [FilePattern]
ex = FilePattern -> FilePattern -> [FilePattern]
extract FilePattern
p
specials :: FilePattern -> [Pat]
specials :: FilePattern -> [Pat]
specials = (Pat -> [Pat]) -> [Pat] -> [Pat]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Pat -> [Pat]
f ([Pat] -> [Pat]) -> (FilePattern -> [Pat]) -> FilePattern -> [Pat]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePattern -> [Pat]
parse
where
f :: Pat -> [Pat]
f Lit{} = []
f Pat
Star = [Pat
Star]
f Pat
Skip = [Pat
Skip]
f Pat
Skip1 = [Pat
Skip]
f (Stars FilePattern
_ [FilePattern]
xs FilePattern
_) = Int -> Pat -> [Pat]
forall a. Int -> a -> [a]
replicate ([FilePattern] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [FilePattern]
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Pat
Star
simple :: FilePattern -> Bool
simple :: FilePattern -> Bool
simple = [Pat] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Pat] -> Bool) -> (FilePattern -> [Pat]) -> FilePattern -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePattern -> [Pat]
specials
compatible :: [FilePattern] -> Bool
compatible :: [FilePattern] -> Bool
compatible [] = Bool
True
compatible (FilePattern
x:[FilePattern]
xs) = (FilePattern -> Bool) -> [FilePattern] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([Pat] -> [Pat] -> Bool
forall a. Eq a => a -> a -> Bool
(==) (FilePattern -> [Pat]
specials FilePattern
x) ([Pat] -> Bool) -> (FilePattern -> [Pat]) -> FilePattern -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePattern -> [Pat]
specials) [FilePattern]
xs
extract :: FilePattern -> FilePath -> [String]
FilePattern
p = let pat :: [Pat]
pat = FilePattern -> [Pat]
parse FilePattern
p in \FilePattern
x ->
case [Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
pat ((Char -> Bool) -> FilePattern -> [FilePattern]
forall a. (a -> Bool) -> [a] -> [[a]]
split Char -> Bool
isPathSeparator FilePattern
x) of
[] | FilePattern
p FilePattern -> FilePattern -> Bool
?== FilePattern
x -> SomeException -> [FilePattern]
forall a. SomeException -> a
throwImpure (SomeException -> [FilePattern]) -> SomeException -> [FilePattern]
forall a b. (a -> b) -> a -> b
$ Partial => FilePattern -> SomeException
FilePattern -> SomeException
errorInternal (FilePattern -> SomeException) -> FilePattern -> SomeException
forall a b. (a -> b) -> a -> b
$ FilePattern
"extract with " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern -> FilePattern
forall a. Show a => a -> FilePattern
show FilePattern
p FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
" and " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern -> FilePattern
forall a. Show a => a -> FilePattern
show FilePattern
x
| Bool
otherwise -> FilePattern -> [FilePattern]
forall a. Partial => FilePattern -> a
error (FilePattern -> [FilePattern]) -> FilePattern -> [FilePattern]
forall a b. (a -> b) -> a -> b
$ FilePattern
"Pattern " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern -> FilePattern
forall a. Show a => a -> FilePattern
show FilePattern
p FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
" does not match " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
x FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
", when trying to extract the FilePattern matches"
[FilePattern]
ms:[[FilePattern]]
_ -> [FilePattern]
ms
substitute :: [String] -> FilePattern -> FilePath
substitute :: [FilePattern] -> FilePattern -> FilePattern
substitute [FilePattern]
oms FilePattern
oxs = FilePattern -> [FilePattern] -> FilePattern
forall a. [a] -> [[a]] -> [a]
intercalate FilePattern
"/" ([FilePattern] -> FilePattern) -> [FilePattern] -> FilePattern
forall a b. (a -> b) -> a -> b
$ [[FilePattern]] -> [FilePattern]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[FilePattern]] -> [FilePattern])
-> [[FilePattern]] -> [FilePattern]
forall a b. (a -> b) -> a -> b
$ ([FilePattern], [[FilePattern]]) -> [[FilePattern]]
forall a b. (a, b) -> b
snd (([FilePattern], [[FilePattern]]) -> [[FilePattern]])
-> ([FilePattern], [[FilePattern]]) -> [[FilePattern]]
forall a b. (a -> b) -> a -> b
$ ([FilePattern] -> Pat -> ([FilePattern], [FilePattern]))
-> [FilePattern] -> [Pat] -> ([FilePattern], [[FilePattern]])
forall (t :: * -> *) a b c.
Traversable t =>
(a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL [FilePattern] -> Pat -> ([FilePattern], [FilePattern])
f [FilePattern]
oms (FilePattern -> [Pat]
parse FilePattern
oxs)
where
f :: [FilePattern] -> Pat -> ([FilePattern], [FilePattern])
f [FilePattern]
ms (Lit FilePattern
x) = ([FilePattern]
ms, [FilePattern
x])
f (FilePattern
m:[FilePattern]
ms) Pat
Star = ([FilePattern]
ms, [FilePattern
m])
f (FilePattern
m:[FilePattern]
ms) Pat
Skip = ([FilePattern]
ms, FilePattern -> [FilePattern]
split FilePattern
m)
f (FilePattern
m:[FilePattern]
ms) Pat
Skip1 = ([FilePattern]
ms, FilePattern -> [FilePattern]
split FilePattern
m)
f [FilePattern]
ms (Stars FilePattern
pre [FilePattern]
mid FilePattern
post) = ([FilePattern]
ms2, [[FilePattern] -> FilePattern
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([FilePattern] -> FilePattern) -> [FilePattern] -> FilePattern
forall a b. (a -> b) -> a -> b
$ FilePattern
pre FilePattern -> [FilePattern] -> [FilePattern]
forall a. a -> [a] -> [a]
: (FilePattern -> FilePattern -> FilePattern)
-> [FilePattern] -> [FilePattern] -> [FilePattern]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
(++) [FilePattern]
ms1 ([FilePattern]
mid[FilePattern] -> [FilePattern] -> [FilePattern]
forall a. [a] -> [a] -> [a]
++[FilePattern
post])])
where ([FilePattern]
ms1,[FilePattern]
ms2) = Int -> [FilePattern] -> ([FilePattern], [FilePattern])
forall a. Int -> [a] -> ([a], [a])
splitAt ([FilePattern] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [FilePattern]
mid Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [FilePattern]
ms
f [FilePattern]
_ Pat
_ = FilePattern -> ([FilePattern], [FilePattern])
forall a. Partial => FilePattern -> a
error (FilePattern -> ([FilePattern], [FilePattern]))
-> FilePattern -> ([FilePattern], [FilePattern])
forall a b. (a -> b) -> a -> b
$ FilePattern
"Substitution failed into pattern " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern -> FilePattern
forall a. Show a => a -> FilePattern
show FilePattern
oxs FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
" with " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ Int -> FilePattern
forall a. Show a => a -> FilePattern
show ([FilePattern] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [FilePattern]
oms) FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ FilePattern
" matches, namely " FilePattern -> FilePattern -> FilePattern
forall a. [a] -> [a] -> [a]
++ [FilePattern] -> FilePattern
forall a. Show a => a -> FilePattern
show [FilePattern]
oms
split :: FilePattern -> [FilePattern]
split = (Char -> Bool) -> FilePattern -> [FilePattern]
forall a. (a -> Bool) -> [a] -> [[a]]
linesBy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/')
data Walk = Walk ([String] -> ([String],[(String,Walk)]))
| WalkTo ([String],[(String,Walk)])
walk :: [FilePattern] -> (Bool, Walk)
walk :: [FilePattern] -> (Bool, Walk)
walk [FilePattern]
ps = (([Pat] -> Bool) -> [[Pat]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\[Pat]
p -> [Pat] -> Bool
isEmpty [Pat]
p Bool -> Bool -> Bool
|| Bool -> Bool
not ([[FilePattern]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([[FilePattern]] -> Bool) -> [[FilePattern]] -> Bool
forall a b. (a -> b) -> a -> b
$ [Pat] -> [FilePattern] -> [[FilePattern]]
match [Pat]
p [FilePattern
""])) [[Pat]]
ps2, [[Pat]] -> Walk
f [[Pat]]
ps2)
where
ps2 :: [[Pat]]
ps2 = (FilePattern -> [Pat]) -> [FilePattern] -> [[Pat]]
forall a b. (a -> b) -> [a] -> [b]
map ((Pat -> Bool) -> [Pat] -> [Pat]
forall a. (a -> Bool) -> [a] -> [a]
filter (Pat -> Pat -> Bool
forall a. Eq a => a -> a -> Bool
/= FilePattern -> Pat
Lit FilePattern
".") ([Pat] -> [Pat]) -> (FilePattern -> [Pat]) -> FilePattern -> [Pat]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Pat] -> [Pat]
optimise ([Pat] -> [Pat]) -> (FilePattern -> [Pat]) -> FilePattern -> [Pat]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePattern -> [Pat]
parse) [FilePattern]
ps
f :: [[Pat]] -> Walk
f ([[Pat]] -> [[Pat]]
forall a. Ord a => [a] -> [a]
nubOrd -> [[Pat]]
ps)
| Just [FilePattern]
fin <- (Pat -> Maybe FilePattern) -> [Pat] -> Maybe [FilePattern]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pat -> Maybe FilePattern
fromLit [Pat]
fin
, Just [(FilePattern, Walk)]
nxt <- ((Pat, [[Pat]]) -> Maybe (FilePattern, Walk))
-> [(Pat, [[Pat]])] -> Maybe [(FilePattern, Walk)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\(Pat
a,[[Pat]]
b) -> (,[[Pat]] -> Walk
f [[Pat]]
b) (FilePattern -> (FilePattern, Walk))
-> Maybe FilePattern -> Maybe (FilePattern, Walk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pat -> Maybe FilePattern
fromLit Pat
a) [(Pat, [[Pat]])]
nxt
= ([FilePattern], [(FilePattern, Walk)]) -> Walk
WalkTo ([FilePattern]
fin, [(FilePattern, Walk)]
nxt)
| Bool
otherwise = ([FilePattern] -> ([FilePattern], [(FilePattern, Walk)])) -> Walk
Walk (([FilePattern] -> ([FilePattern], [(FilePattern, Walk)])) -> Walk)
-> ([FilePattern] -> ([FilePattern], [(FilePattern, Walk)]))
-> Walk
forall a b. (a -> b) -> a -> b
$ \[FilePattern]
xs ->
(if Bool
finStar then [FilePattern]
xs else (FilePattern -> Bool) -> [FilePattern] -> [FilePattern]
forall a. (a -> Bool) -> [a] -> [a]
filter (\FilePattern
x -> (Pat -> Bool) -> [Pat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Pat -> FilePattern -> Bool
`matchOne` FilePattern
x) [Pat]
fin) [FilePattern]
xs
,[(FilePattern
x, [[Pat]] -> Walk
f [[Pat]]
ys) | FilePattern
x <- [FilePattern]
xs, let ys :: [[Pat]]
ys = [[[Pat]]] -> [[Pat]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[[Pat]]
b | (Pat
a,[[Pat]]
b) <- [(Pat, [[Pat]])]
nxt, Pat -> FilePattern -> Bool
matchOne Pat
a FilePattern
x], Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [[Pat]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Pat]]
ys])
where
finStar :: Bool
finStar = Pat
Star Pat -> [Pat] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Pat]
fin
fin :: [Pat]
fin = [Pat] -> [Pat]
forall a. Ord a => [a] -> [a]
nubOrd ([Pat] -> [Pat]) -> [Pat] -> [Pat]
forall a b. (a -> b) -> a -> b
$ ([Pat] -> Maybe Pat) -> [[Pat]] -> [Pat]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [Pat] -> Maybe Pat
final [[Pat]]
ps
nxt :: [(Pat, [[Pat]])]
nxt = [(Pat, [Pat])] -> [(Pat, [[Pat]])]
forall k v. Ord k => [(k, v)] -> [(k, [v])]
groupSort ([(Pat, [Pat])] -> [(Pat, [[Pat]])])
-> [(Pat, [Pat])] -> [(Pat, [[Pat]])]
forall a b. (a -> b) -> a -> b
$ ([Pat] -> [(Pat, [Pat])]) -> [[Pat]] -> [(Pat, [Pat])]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [Pat] -> [(Pat, [Pat])]
next [[Pat]]
ps
next :: [Pat] -> [(Pat, [Pat])]
next :: [Pat] -> [(Pat, [Pat])]
next (Pat
Skip1:[Pat]
xs) = [(Pat
Star,Pat
SkipPat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs)]
next (Pat
Skip:[Pat]
xs) = (Pat
Star,Pat
SkipPat -> [Pat] -> [Pat]
forall a. a -> [a] -> [a]
:[Pat]
xs) (Pat, [Pat]) -> [(Pat, [Pat])] -> [(Pat, [Pat])]
forall a. a -> [a] -> [a]
: [Pat] -> [(Pat, [Pat])]
next [Pat]
xs
next (Pat
x:[Pat]
xs) = [(Pat
x,[Pat]
xs) | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Pat] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Pat]
xs]
next [] = []
final :: [Pat] -> Maybe Pat
final :: [Pat] -> Maybe Pat
final (Pat
Skip:[Pat]
xs) = if [Pat] -> Bool
isEmpty [Pat]
xs then Pat -> Maybe Pat
forall a. a -> Maybe a
Just Pat
Star else [Pat] -> Maybe Pat
final [Pat]
xs
final (Pat
Skip1:[Pat]
xs) = if [Pat] -> Bool
isEmpty [Pat]
xs then Pat -> Maybe Pat
forall a. a -> Maybe a
Just Pat
Star else Maybe Pat
forall a. Maybe a
Nothing
final (Pat
x:[Pat]
xs) = if [Pat] -> Bool
isEmpty [Pat]
xs then Pat -> Maybe Pat
forall a. a -> Maybe a
Just Pat
x else Maybe Pat
forall a. Maybe a
Nothing
final [] = Maybe Pat
forall a. Maybe a
Nothing
isEmpty :: [Pat] -> Bool
isEmpty = (Pat -> Bool) -> [Pat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Pat -> Pat -> Bool
forall a. Eq a => a -> a -> Bool
== Pat
Skip)