bookhound 0.1.19.0 → 0.1.20.0
raw patch · 3 files changed
+31/−24 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Bookhound.Parser: withErrorNFrom :: Int -> (a -> String) -> Parser a -> Parser a
+ Bookhound.Parser: mapError :: (ParseError -> ParseError) -> Parser a -> Parser a
Files
- bookhound.cabal +1/−1
- src/Bookhound/Parser.hs +25/−18
- src/Bookhound/Parsers/Collections.hs +5/−5
bookhound.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: bookhound-version: 0.1.19.0+version: 0.1.20.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, ParseResult, ParseError(..), runParser, errorParser, andThen, exactly, isMatch, check, anyOf, allOf, char,- withTransform, withError, withErrorN, withErrorFrom, withErrorNFrom, except) where+ withTransform, withError, withErrorN, withErrorFrom, mapError, except) where import Bookhound.Utils.Foldable (findJust) import Control.Applicative (liftA2)@@ -52,7 +52,7 @@ instance Functor ParseResult where fmap f (Result i a) = Result i (f a)- fmap _ (Error pe) = Error pe+ fmap _ (Error pe) = (Error pe) instance Functor Parser where@@ -72,10 +72,8 @@ (>>=) (P p t e) f = applyTransformError t e $ mkParser (\x -> case p x of+ Result i a -> parse (f a) i Error pe -> Error pe- Result i a -> parse (applyError (e <> e') p2) i- where- p2@(P _ _ e') = f a ) runParser :: Parser a -> Input -> Either [ParseError] a@@ -83,8 +81,14 @@ where toEither = \case Result _ a -> Right a- Error pe -> Left $ (snd <$> reverse (Set.toList e)) <> [pe]+ Error pe -> Left $ filter (hasPriorityError) [pe] <>+ (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 @@ -103,7 +107,7 @@ case p x of result@(Result i _) | i == mempty -> result Result i _ -> Error $ ExpectedEof i- err@(Error _) -> err+ err -> err ) anyOf :: [Parser a] -> Parser a@@ -123,12 +127,11 @@ applyTransformsErrors [t, t'] [e, e'] $ mkParser (\x -> case p x of- result@(Result _ _) -> result- Error _ -> parse (anyOfHelper rest t e) x+ Error _ -> parse (anyOfHelper rest t e) x+ result -> result ) - allOfHelper :: [Parser a] -> (forall b. Maybe (Parser b -> Parser b)) -> Set (Int, ParseError)@@ -139,12 +142,11 @@ applyTransformsErrors [t, t'] [e, e'] $ mkParser (\x -> case p x of- Result _ _ -> parse (allOfHelper rest t e) x- err@(Error _) -> err+ Result _ _ -> parse (allOfHelper rest t e) x+ err -> err ) - isMatch :: (Char -> Char -> Bool) -> Parser Char -> Char -> Parser Char isMatch cond parser c1 = do c2 <- parser@@ -174,12 +176,17 @@ withErrorN n str = applyError . Set.singleton $ (n, ErrorAt str) withErrorFrom :: (a -> String) -> Parser a -> Parser a-withErrorFrom = withErrorNFrom 0--withErrorNFrom :: Int -> (a -> String) -> Parser a -> Parser a-withErrorNFrom n errFn p =+withErrorFrom errFn p = do value <- p- withErrorN n (errFn value) $ pure $ value+ mapError (const $ ErrorAt $ errFn value) $ pure value++mapError :: (ParseError -> ParseError) -> Parser a -> Parser a+mapError f (P p t e) = applyTransformError t e $ mkParser (+ \x -> case p x of+ Error pe -> Error $ f pe+ result -> result+ )+ withTransform :: (forall b. Parser b -> Parser b) -> Parser a -> Parser a withTransform t = applyTransform $ Just t
src/Bookhound/Parsers/Collections.hs view
@@ -11,10 +11,10 @@ collOf :: Parser a -> Parser b -> Parser c -> Parser d -> Parser [d]-collOf start end sep elemParser = withErrorN (-1) "Collection" $+collOf start end sep elemParser = start *> elemsParser <* end- where- elemsParser = anySepBy sep $ maybeWithin spacing elemParser+ where+ elemsParser = anySepBy sep $ maybeWithin spacing elemParser listOf :: Parser a -> Parser [a]@@ -31,5 +31,5 @@ mapOf :: Ord b => Parser a -> Parser b -> Parser c -> Parser (Map b c) mapOf sep p1 p2 = withErrorN (-1) "Map" $ Map.fromList <$> collOf openCurly closeCurly comma mapEntry- where- mapEntry = (,) <$> p1 <* maybeWithin spacing sep <*> p2+ where+ mapEntry = (,) <$> p1 <* maybeWithin spacing sep <*> p2