From 3873bb473e5797a6f2dc9197cf4536113d584ffe Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sat, 2 Dec 2023 18:02:28 +0800 Subject: [PATCH] Make osstr :: QuasiQuoter valid as a pattern Similar to https://github.com/haskell/filepath/pull/210 --- System/OsString/Internal.hs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/System/OsString/Internal.hs b/System/OsString/Internal.hs index 18825f8..e0d5254 100644 --- a/System/OsString/Internal.hs +++ b/System/OsString/Internal.hs @@ -3,6 +3,8 @@ {-# LANGUAGE UnliftedFFITypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} +{-# LANGUAGE TemplateHaskellQuotes #-} +{-# LANGUAGE ViewPatterns #-} -- needed to quote a view pattern module System.OsString.Internal where @@ -122,6 +124,7 @@ fromBytes = fmap OsString . PF.fromBytes -- | QuasiQuote an 'OsString'. This accepts Unicode characters -- and encodes as UTF-8 on unix and UTF-16 on windows. +-- If used as pattern, requires turning on the @ViewPatterns@ extension. osstr :: QuasiQuoter osstr = QuasiQuoter @@ -129,23 +132,25 @@ osstr = { quoteExp = \s -> do osp <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s lift osp - , quotePat = \_ -> - fail "illegal QuasiQuote (allowed as expression only, used as a pattern)" + , quotePat = \s -> do + osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s + [p|((==) osp' -> True)|] , quoteType = \_ -> - fail "illegal QuasiQuote (allowed as expression only, used as a type)" + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)" , quoteDec = \_ -> - fail "illegal QuasiQuote (allowed as expression only, used as a declaration)" + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)" } #else { quoteExp = \s -> do osp <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF8 ErrorOnCodingFailure) $ s lift osp - , quotePat = \_ -> - fail "illegal QuasiQuote (allowed as expression only, used as a pattern)" + , quotePat = \s -> do + osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF8 ErrorOnCodingFailure) $ s + [p|((==) osp' -> True)|] , quoteType = \_ -> - fail "illegal QuasiQuote (allowed as expression only, used as a type)" + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)" , quoteDec = \_ -> - fail "illegal QuasiQuote (allowed as expression only, used as a declaration)" + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)" } #endif