Skip to content

Commit 935e13f

Browse files
committed
Allow pstr quasiquoter as pattern too
1 parent ccfab36 commit 935e13f

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

System/OsString/Common.hs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{-# LANGUAGE PatternSynonyms #-}
44
{-# LANGUAGE RankNTypes #-}
55
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE TemplateHaskellQuotes #-}
7+
{-# LANGUAGE ViewPatterns #-}
68
{-# OPTIONS_GHC -Wno-unused-imports #-}
79

810
-- This template expects CPP definitions for:
@@ -175,7 +177,7 @@ import GHC.IO.Encoding.UTF8 ( mkUTF8 )
175177
import qualified System.OsString.Data.ByteString.Short as BSP
176178
#endif
177179
import GHC.Stack (HasCallStack)
178-
import Prelude (Bool, Int, Maybe(..), IO, String, Either(..), fmap, ($), (.), mconcat, fromEnum, fromInteger, mempty, fromIntegral, fail, (<$>), show, either, pure, const, flip)
180+
import Prelude (Bool(..), Int, Maybe(..), IO, String, Either(..), fmap, ($), (.), mconcat, fromEnum, fromInteger, mempty, fromIntegral, fail, (<$>), show, either, pure, const, flip)
179181
import Data.Bifunctor ( bimap )
180182
import qualified System.OsString.Data.ByteString.Short.Word16 as BS16
181183
import qualified System.OsString.Data.ByteString.Short as BS8
@@ -345,23 +347,25 @@ pstr =
345347
{ quoteExp = \s -> do
346348
ps <- either (fail . show) pure $ encodeWith (mkUTF16le ErrorOnCodingFailure) s
347349
lift ps
348-
, quotePat = \_ ->
349-
fail "illegal QuasiQuote (allowed as expression only, used as a pattern)"
350+
, quotePat = \s -> do
351+
osp' <- either (fail . show) pure . encodeWith (mkUTF16le ErrorOnCodingFailure) $ s
352+
[p|((==) osp' -> True)|]
350353
, quoteType = \_ ->
351-
fail "illegal QuasiQuote (allowed as expression only, used as a type)"
354+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)"
352355
, quoteDec = \_ ->
353-
fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"
356+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)"
354357
}
355358
#else
356359
{ quoteExp = \s -> do
357360
ps <- either (fail . show) pure $ encodeWith (mkUTF8 ErrorOnCodingFailure) s
358361
lift ps
359-
, quotePat = \_ ->
360-
fail "illegal QuasiQuote (allowed as expression only, used as a pattern)"
362+
, quotePat = \s -> do
363+
osp' <- either (fail . show) pure . encodeWith (mkUTF8 ErrorOnCodingFailure) $ s
364+
[p|((==) osp' -> True)|]
361365
, quoteType = \_ ->
362-
fail "illegal QuasiQuote (allowed as expression only, used as a type)"
366+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)"
363367
, quoteDec = \_ ->
364-
fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"
368+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)"
365369
}
366370
#endif
367371

0 commit comments

Comments
 (0)