|
5 | 5 | {-# LANGUAGE LambdaCase #-} |
6 | 6 | {-# LANGUAGE OverloadedStrings #-} |
7 | 7 | {-# LANGUAGE TupleSections #-} |
| 8 | +{-# LANGUAGE CPP #-} |
8 | 9 |
|
9 | 10 | module Database.Persist.Quasi.Internal.ModelParser |
10 | 11 | ( SourceLoc (..) |
@@ -75,7 +76,11 @@ initialExtraState = |
75 | 76 | , esLastDocumentablePosition = Nothing |
76 | 77 | } |
77 | 78 |
|
78 | | --- @since 2.16.0.0 |
| 79 | +-- megaparsec <9.5 lacks a MonadWriter instance for ParsecT. |
| 80 | +-- We must continue supporting megaparsec <9.5 in order to support |
| 81 | +-- GHC <9, so we will work around this by disabling warning functionality |
| 82 | +-- on old megaparsecs. |
| 83 | +#if MIN_VERSION_megaparsec(9,5,0) |
79 | 84 | newtype Parser a = Parser |
80 | 85 | { unParser |
81 | 86 | :: ReaderT |
@@ -103,6 +108,31 @@ newtype Parser a = Parser |
103 | 108 | , MonadParsec Void String |
104 | 109 | , MonadWriter (Set ParserWarning) |
105 | 110 | ) |
| 111 | +#else |
| 112 | +newtype Parser a = Parser |
| 113 | + { unParser |
| 114 | + :: ReaderT |
| 115 | + PersistSettings |
| 116 | + ( StateT |
| 117 | + ExtraState |
| 118 | + ( Parsec |
| 119 | + Void |
| 120 | + String |
| 121 | + ) |
| 122 | + ) |
| 123 | + a |
| 124 | + } |
| 125 | + deriving newtype |
| 126 | + ( Functor |
| 127 | + , Applicative |
| 128 | + , Monad |
| 129 | + , Alternative |
| 130 | + , MonadPlus |
| 131 | + , MonadState ExtraState |
| 132 | + , MonadReader PersistSettings |
| 133 | + , MonadParsec Void String |
| 134 | + ) |
| 135 | +#endif |
106 | 136 |
|
107 | 137 | type EntityParseError = ParseErrorBundle String Void |
108 | 138 |
|
@@ -139,10 +169,16 @@ runConfiguredParser |
139 | 169 | -> InternalParseResult a |
140 | 170 | runConfiguredParser ps acc parser fp s = (warnings, either) |
141 | 171 | where |
| 172 | +#if MIN_VERSION_megaparsec(9,5,0) |
142 | 173 | sm = runReaderT (unParser parser) ps |
143 | 174 | pm = runStateT sm acc |
144 | 175 | wm = runParserT' pm initialInternalState |
145 | 176 | ((_is, either), warnings) = runWriter wm |
| 177 | +#else |
| 178 | + sm = runReaderT (unParser parser) ps |
| 179 | + pm = runStateT sm acc |
| 180 | + ((_is, either), warnings) = (runParser' pm initialInternalState, mempty) |
| 181 | +#endif |
146 | 182 |
|
147 | 183 | initialSourcePos = |
148 | 184 | SourcePos |
@@ -186,23 +222,25 @@ tryOrWarn msg p l r = do |
186 | 222 | parserState <- getParserState |
187 | 223 | withRecovery (warnAndRetry $ statePosState parserState) l |
188 | 224 | where |
189 | | - warnAndRetry posState err = do |
190 | | - if p err |
191 | | - then do |
192 | | - let |
193 | | - (pairs, _) = attachSourcePos errorOffset [err] posState |
194 | | - tell . Set.fromList $ |
195 | | - map |
196 | | - ( \(e, _pos) -> |
197 | | - ParserWarning |
198 | | - { parserWarningExtraMessage = msg <> "\n" |
199 | | - , parserWarningUnderlyingError = e |
200 | | - , parserWarningPosState = posState |
201 | | - } |
202 | | - ) |
203 | | - pairs |
204 | | - r |
205 | | - else parseError err |
| 225 | + warnAndRetry posState err = |
| 226 | + if not (p err) |
| 227 | + then parseError err |
| 228 | + else do |
| 229 | +#if MIN_VERSION_megaparsec(9,5,0) |
| 230 | + let |
| 231 | + (pairs, _) = attachSourcePos errorOffset [err] posState |
| 232 | + tell . Set.fromList $ |
| 233 | + map |
| 234 | + ( \(e, _pos) -> |
| 235 | + ParserWarning |
| 236 | + { parserWarningExtraMessage = msg <> "\n" |
| 237 | + , parserWarningUnderlyingError = e |
| 238 | + , parserWarningPosState = posState |
| 239 | + } |
| 240 | + ) |
| 241 | + pairs |
| 242 | +#endif |
| 243 | + r |
206 | 244 |
|
207 | 245 | -- | Attempts to parse with a provided parser. If it fails with an error matching |
208 | 246 | -- the provided predicate, it registers a delayed error with the provided message and falls |
|
0 commit comments