module System.FilePath.Glob.Base where
import Data.Maybe (fromMaybe)
import System.FilePath (pathSeparator, extSeparator)
data Token
= Literal !Char
| ExtSeparator
| PathSeparator
| NonPathSeparator
| CharRange !Bool [Either Char (Char,Char)]
| OpenRange (Maybe String) (Maybe String)
| AnyNonPathSeparator
| AnyDirectory
| LongLiteral !Int String
deriving (Eq)
newtype Pattern = Pattern { unPattern :: [Token] } deriving (Eq)
liftP :: ([Token] -> [Token]) -> Pattern -> Pattern
liftP f (Pattern pat) = Pattern (f pat)
instance Show Token where
show (Literal c) = [c]
show ExtSeparator = [ extSeparator]
show PathSeparator = [pathSeparator]
show NonPathSeparator = "?"
show AnyNonPathSeparator = "*"
show AnyDirectory = "**/"
show (LongLiteral _ s) = s
show (CharRange b r) =
'[' : (if b then "" else "^") ++
concatMap (either (:[]) (\(x,y) -> [x,'-',y])) r ++ "]"
show (OpenRange a b) =
'<' : fromMaybe "" a ++ "-" ++
fromMaybe "" b ++ ">"
showList = showList . concatMap show
instance Show Pattern where
showsPrec d (Pattern ts) =
showParen (d > 10) $ showString "compile " . shows ts