|
1 | 1 | {-# LANGUAGE CPP #-}
|
2 | 2 | {-# LANGUAGE RankNTypes #-}
|
3 | 3 | {-# LANGUAGE UnliftedFFITypes #-}
|
| 4 | +{-# LANGUAGE TemplateHaskellQuotes #-} |
| 5 | +{-# LANGUAGE ViewPatterns #-} -- needed to quote a view pattern |
4 | 6 |
|
5 | 7 | module System.OsPath.Internal where
|
6 | 8 |
|
@@ -111,32 +113,37 @@ fromBytes = OS.fromBytes
|
111 | 113 |
|
112 | 114 | -- | QuasiQuote an 'OsPath'. This accepts Unicode characters
|
113 | 115 | -- and encodes as UTF-8 on unix and UTF-16LE on windows. Runs 'isValid'
|
114 |
| --- on the input. |
| 116 | +-- on the input. If used as a pattern, requires turning on the @ViewPatterns@ |
| 117 | +-- extension. |
115 | 118 | osp :: QuasiQuoter
|
116 | 119 | osp = QuasiQuoter
|
117 | 120 | #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
|
118 | 121 | { quoteExp = \s -> do
|
119 | 122 | osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s
|
120 | 123 | when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp')
|
121 | 124 | lift osp'
|
122 |
| - , quotePat = \_ -> |
123 |
| - fail "illegal QuasiQuote (allowed as expression only, used as a pattern)" |
| 125 | + , quotePat = \s -> do |
| 126 | + osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s |
| 127 | + when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp') |
| 128 | + [p|((==) osp' -> True)|] |
124 | 129 | , quoteType = \_ ->
|
125 |
| - fail "illegal QuasiQuote (allowed as expression only, used as a type)" |
| 130 | + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)" |
126 | 131 | , quoteDec = \_ ->
|
127 |
| - fail "illegal QuasiQuote (allowed as expression only, used as a declaration)" |
| 132 | + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)" |
128 | 133 | }
|
129 | 134 | #else
|
130 | 135 | { quoteExp = \s -> do
|
131 | 136 | osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF8 ErrorOnCodingFailure) $ s
|
132 | 137 | when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp')
|
133 | 138 | lift osp'
|
134 |
| - , quotePat = \_ -> |
135 |
| - fail "illegal QuasiQuote (allowed as expression only, used as a pattern)" |
| 139 | + , quotePat = \s -> do |
| 140 | + osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF8 ErrorOnCodingFailure) $ s |
| 141 | + when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp') |
| 142 | + [p|((==) osp' -> True)|] |
136 | 143 | , quoteType = \_ ->
|
137 |
| - fail "illegal QuasiQuote (allowed as expression only, used as a type)" |
| 144 | + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)" |
138 | 145 | , quoteDec = \_ ->
|
139 |
| - fail "illegal QuasiQuote (allowed as expression only, used as a declaration)" |
| 146 | + fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)" |
140 | 147 | }
|
141 | 148 | #endif
|
142 | 149 |
|
|
0 commit comments