{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE ViewPatterns #-}

#undef  WINDOWS
#define POSIX
#define IS_WINDOWS False
#define FILEPATH_NAME PosixPath
#define OSSTRING_NAME PosixString
#define WORD_NAME PosixChar

#include "Common.hs"

-- | QuasiQuote a 'PosixPath'. This accepts Unicode characters
-- and encodes as UTF-8. Runs 'isValid' on the input.
pstr :: QuasiQuoter
pstr :: QuasiQuoter
pstr =
  QuasiQuoter
  { quoteExp :: String -> Q Exp
quoteExp = \String
s -> do
      PosixString
ps <- (EncodingException -> Q PosixString)
-> (PosixString -> Q PosixString)
-> Either EncodingException PosixString
-> Q PosixString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Q PosixString
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Q PosixString)
-> (EncodingException -> String)
-> EncodingException
-> Q PosixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodingException -> String
forall a. Show a => a -> String
show) PosixString -> Q PosixString
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either EncodingException PosixString -> Q PosixString)
-> Either EncodingException PosixString -> Q PosixString
forall a b. (a -> b) -> a -> b
$ TextEncoding -> String -> Either EncodingException PosixString
encodeWith (CodingFailureMode -> TextEncoding
mkUTF8 CodingFailureMode
ErrorOnCodingFailure) String
s
      Bool -> Q () -> Q ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ PosixString -> Bool
isValid PosixString
ps) (Q () -> Q ()) -> Q () -> Q ()
forall a b. (a -> b) -> a -> b
$ String -> Q ()
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"filepath not valid: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PosixString -> String
forall a. Show a => a -> String
show PosixString
ps)
      PosixString -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => PosixString -> m Exp
lift PosixString
ps
  , quotePat :: String -> Q Pat
quotePat = \String
s -> do
      PosixString
osp' <- (EncodingException -> Q PosixString)
-> (PosixString -> Q PosixString)
-> Either EncodingException PosixString
-> Q PosixString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Q PosixString
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Q PosixString)
-> (EncodingException -> String)
-> EncodingException
-> Q PosixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodingException -> String
forall a. Show a => a -> String
show) PosixString -> Q PosixString
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either EncodingException PosixString -> Q PosixString)
-> (String -> Either EncodingException PosixString)
-> String
-> Q PosixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEncoding -> String -> Either EncodingException PosixString
encodeWith (CodingFailureMode -> TextEncoding
mkUTF8 CodingFailureMode
ErrorOnCodingFailure) (String -> Q PosixString) -> String -> Q PosixString
forall a b. (a -> b) -> a -> b
$ String
s
      Bool -> Q () -> Q ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ PosixString -> Bool
isValid PosixString
osp') (Q () -> Q ()) -> Q () -> Q ()
forall a b. (a -> b) -> a -> b
$ String -> Q ()
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"filepath not valid: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PosixString -> String
forall a. Show a => a -> String
show PosixString
osp')
      [p|((==) osp' -> True)|]
  , quoteType :: String -> Q Type
quoteType = \String
_ ->
      String -> Q Type
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"illegal QuasiQuote (allowed as expression or pattern only, used as a type)"
  , quoteDec :: String -> Q [Dec]
quoteDec  = \String
_ ->
      String -> Q [Dec]
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)"
  }