bookhound 0.1.20.0 → 0.1.21.0
raw patch · 2 files changed
+42/−29 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Bookhound.Parser: data ParseResult a
- Bookhound.Parser: mapError :: (ParseError -> ParseError) -> Parser a -> Parser a
+ Bookhound.Parser: mapError :: (ParseError -> Maybe ParseError) -> Parser a -> Parser a
- Bookhound.Parser: withErrorFrom :: (a -> String) -> Parser a -> Parser a
+ Bookhound.Parser: withErrorFrom :: (a -> String) -> Parser a -> Parser b -> Parser b
Files
- bookhound.cabal +1/−1
- src/Bookhound/Parser.hs +41/−28
bookhound.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: bookhound-version: 0.1.20.0+version: 0.1.21.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,10 +1,11 @@-module Bookhound.Parser (Parser, ParseResult, ParseError(..), runParser, errorParser,+module Bookhound.Parser (Parser, ParseError(..), runParser, errorParser, andThen, exactly, isMatch, check, anyOf, allOf, char, withTransform, withError, withErrorN, withErrorFrom, mapError, except) where import Bookhound.Utils.Foldable (findJust) import Control.Applicative (liftA2) import Data.Either (fromRight)+import Data.Maybe (fromMaybe) import Data.Set (Set) import qualified Data.Set as Set import Data.Text (Text, pack, uncons, unpack)@@ -19,7 +20,7 @@ } data ParseResult a- = Result Input a+ = Result Input (Maybe ParseError) a | Error ParseError deriving (Eq) @@ -34,8 +35,8 @@ instance Show a => Show (ParseResult a) where- show (Result i a) = "Pending: " <> " >" <> unpack i <> "< "- <> "\n\nResult: \n" <> show a+ show (Result i _ a) = "Pending: " <> " >" <> unpack i <> "< "+ <> "\n\nResult: \n" <> show a show (Error err) = show err instance Show ParseError where@@ -51,8 +52,8 @@ instance Functor ParseResult where- fmap f (Result i a) = Result i (f a)- fmap _ (Error pe) = (Error pe)+ fmap f (Result i pe a) = Result i pe (f a)+ fmap _ (Error pe) = (Error pe) instance Functor Parser where@@ -60,27 +61,29 @@ mkParser (fmap f . p) instance Applicative Parser where- pure a = mkParser (`Result` a)+ pure a = mkParser (\x -> Result x Nothing a) liftA2 f (P p t e) mb@(P _ t' e') = applyTransformsErrors [t, t'] [e, e'] $ mkParser (\x -> case p x of- Result i a -> parse ((f a) <$> mb) i- Error pe -> Error pe+ Result i pe a -> mapErrorResult (const pe) $+ parse ((f a) <$> mb) i+ Error pe -> Error pe ) instance Monad Parser where (>>=) (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 pe a -> mapErrorResult (const pe) $+ parse (f a) i+ Error pe -> Error pe ) runParser :: Parser a -> Input -> Either [ParseError] a runParser p@(P _ _ e) i = toEither $ parse (exactly p) i where toEither = \case- Result _ a -> Right a+ Result _ _ a -> Right a Error pe -> Left $ filter (hasPriorityError) [pe] <> (snd <$> reverse (Set.toList e)) <> filter (not . hasPriorityError) [pe]@@ -98,16 +101,17 @@ char :: Parser Char char = mkParser $- maybe (Error UnexpectedEof) (\(ch, rest) -> Result rest ch) . uncons+ maybe (Error UnexpectedEof) (\(ch, rest) -> Result rest Nothing ch)+ . uncons exactly :: Parser a -> Parser a exactly (P p t e) = applyTransformError t e $ mkParser (\x -> case p x of- result@(Result i _) | i == mempty -> result- Result i _ -> Error $ ExpectedEof i- err -> err+ result@(Result i _ _) | i == mempty -> result+ Result i _ _ -> Error $ ExpectedEof i+ err -> err ) anyOf :: [Parser a] -> Parser a@@ -142,8 +146,8 @@ applyTransformsErrors [t, t'] [e, e'] $ mkParser (\x -> case p x of- Result _ _ -> parse (allOfHelper rest t e) x- err -> err+ Result _ _ _ -> parse (allOfHelper rest t e) x+ err -> err ) @@ -165,8 +169,8 @@ except :: Parser a -> Parser a -> Parser a except (P p t e) (P p' _ _) = applyTransformError t e $ mkParser ( \x -> case p' x of- Result _ _ -> Error $ NoMatch "except"- Error _ -> p x+ Result _ _ _ -> Error $ NoMatch "except"+ Error _ -> p x ) withError :: String -> Parser a -> Parser a@@ -175,16 +179,25 @@ withErrorN :: Int -> String -> Parser a -> Parser a withErrorN n str = applyError . Set.singleton $ (n, ErrorAt str) -withErrorFrom :: (a -> String) -> Parser a -> Parser a-withErrorFrom errFn p =- do value <- p- mapError (const $ ErrorAt $ errFn value) $ pure value+withErrorFrom :: (a -> String) -> Parser a -> Parser b -> Parser b+withErrorFrom errFn pDoc p =+ do value <- nonConsumingParser pDoc+ mapError (const $ pure $ ErrorAt $ errFn value) p -mapError :: (ParseError -> ParseError) -> Parser a -> Parser a-mapError f (P p t e) = applyTransformError t e $ mkParser (+mapError :: (ParseError -> Maybe ParseError) -> Parser a -> Parser a+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- Error pe -> Error $ f pe- result -> result+ Result _ pe a -> Result x pe a+ err -> err )