bookhound 0.1.21.0 → 0.1.22.0
raw patch · 2 files changed
+28/−21 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Bookhound.Parser: nonConsumingParser :: Parser a -> Parser a
Files
- bookhound.cabal +1/−1
- src/Bookhound/Parser.hs +27/−20
bookhound.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: bookhound-version: 0.1.21.0+version: 0.1.22.0 synopsis: Simple Parser Combinators description: Please see the README on GitHub at <https://github.com/albertprz/bookhound#readme> category: Parser Combinators
src/Bookhound/Parser.hs view
@@ -1,6 +1,6 @@ module Bookhound.Parser (Parser, ParseError(..), runParser, errorParser, andThen, exactly, isMatch, check, anyOf, allOf, char,- withTransform, withError, withErrorN, withErrorFrom, mapError, except) where+ withTransform, withError, withErrorN, withErrorFrom, mapError, except, nonConsumingParser) where import Bookhound.Utils.Foldable (findJust) import Control.Applicative (liftA2)@@ -53,7 +53,7 @@ instance Functor ParseResult where fmap f (Result i pe a) = Result i pe (f a)- fmap _ (Error pe) = (Error pe)+ fmap _ (Error pe) = Error pe instance Functor Parser where@@ -88,10 +88,6 @@ (snd <$> reverse (Set.toList e)) <> filter (not . hasPriorityError) [pe] -hasPriorityError :: ParseError -> Bool-hasPriorityError (ErrorAt _) = True-hasPriorityError _ = False- errorParser :: ParseError -> Parser a errorParser = mkParser . const . Error @@ -131,8 +127,9 @@ applyTransformsErrors [t, t'] [e, e'] $ mkParser (\x -> case p x of- Error _ -> parse (anyOfHelper rest t e) x- result -> result+ Error pe -> mapErrorResult (const $ wrapMaybeError pe) $+ parse (anyOfHelper rest t e) x+ result -> result ) @@ -146,7 +143,8 @@ applyTransformsErrors [t, t'] [e, e'] $ mkParser (\x -> case p x of- Result _ _ _ -> parse (allOfHelper rest t e) x+ Result _ pe _ -> mapErrorResult (const pe) $+ parse (allOfHelper rest t e) x err -> err ) @@ -188,19 +186,8 @@ mapError f (P p t e) = applyTransformError t e $ mkParser $ mapErrorResult f . p -mapErrorResult :: (ParseError -> Maybe ParseError) -> ParseResult a -> ParseResult a-mapErrorResult f (Error pe) = Error $ fromMaybe pe $ f pe-mapErrorResult _ result = result -nonConsumingParser :: Parser a -> Parser a-nonConsumingParser (P p t e) = applyTransformError t e $ mkParser (- \x -> case p x of- Result _ pe a -> Result x pe a- err -> err- )-- withTransform :: (forall b. Parser b -> Parser b) -> Parser a -> Parser a withTransform t = applyTransform $ Just t @@ -221,6 +208,26 @@ applyTransformError t e = applyTransform t . applyError e ++nonConsumingParser :: Parser a -> Parser a+nonConsumingParser (P p t e) = applyTransformError t e $ mkParser (+ \x -> case p x of+ Result _ pe a -> Result x pe a+ err -> err+ )++mapErrorResult :: (ParseError -> Maybe ParseError) -> ParseResult a -> ParseResult a+mapErrorResult f (Error pe) = Error $ fromMaybe pe $ f pe+mapErrorResult _ result = result+++wrapMaybeError :: ParseError -> Maybe ParseError+wrapMaybeError pe | hasPriorityError pe = pure pe+ | otherwise = Nothing++hasPriorityError :: ParseError -> Bool+hasPriorityError (ErrorAt _) = True+hasPriorityError _ = False applyTransform :: (forall b. Maybe (Parser b -> Parser b)) -> Parser a -> Parser a applyTransform f p = maybe p (\f' -> (f' p) {transform = f}) f