mini 1.2.2.0 → 1.2.2.1
raw patch · 3 files changed
+122/−22 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +4/−0
- Mini/Transformers/ParserT.hs +117/−21
- mini.cabal +1/−1
CHANGELOG.md view
@@ -1,3 +1,7 @@+1.2.2.1 [2024-03-20]+--------------------+* docs: Add parser examples+ 1.2.2.0 [2024-03-20] -------------------- * Mini.Transformers.ParserT:
Mini/Transformers/ParserT.hs view
@@ -81,7 +81,7 @@ pure a = ParserT $ pure . Right . (a,) (<*>) = ap --- | TODO+-- | Parse @p@ or, if @p@ fails, backtrack and parse @q@ via @p \<|\> q@ instance (Monad m, Eq s) => Alternative (ParserT s m) where empty = ParserT . const . pure $ Left mempty m <|> n = ParserT $ \ss ->@@ -131,8 +131,8 @@ === __Examples__ ->>> runParserT (string "foe" <|> string "foo") "foobar"-Right ("foo","bar")+>>> runParserT (some $ sat isDigit) "123abc"+Right ("123","abc") -} sat :: (Applicative m, Show s) => (s -> Bool) -> ParserT s m s sat p = ParserT $ \case@@ -157,25 +157,49 @@ === __Examples__ ->>> runParserT (EXAMPLE) "STRING"-Right (RESULT,"REST")+>>> runParserT (symbol 'f') "foo"+Right ('f',"oo") -} symbol :: (Applicative m, Show s, Eq s) => s -> ParserT s m s symbol = sat . (==) --- | Parse a sequence of symbols+{- | Parse a sequence of symbols++=== __Examples__++>>> runParserT (string "foo") "foobar"+Right ("foo","bar")+-} string :: (Monad m, Traversable t, Show s, Eq s) => t s -> ParserT s m (t s) string = traverse symbol --- | Parse symbols included in a collection+{- | Parse symbols included in a collection++=== __Examples__++>>> runParserT (oneOf "abc") "bar"+Right ('b',"ar")+-} oneOf :: (Applicative m, Foldable t, Show s, Eq s) => t s -> ParserT s m s oneOf = sat . flip elem --- | Parse symbols excluded from a collection+{- | Parse symbols excluded from a collection++=== __Examples__++>>> runParserT (noneOf "abc") "foo"+Right ('f',"oo")+-} noneOf :: (Applicative m, Foldable t, Show s, Eq s) => t s -> ParserT s m s noneOf = sat . flip notElem --- | Parse successfully only at end of input+{- | Parse successfully only at end of input++=== __Examples__++>>> runParserT (string "foobar" *> eof) "foobar"+Right ((),"")+-} eof :: (Monad m, Show s) => ParserT s m () eof = reject item @@ -183,23 +207,53 @@ - Combinators -} --- | Parse zero or more @p@ separated by @q@ via @p \`sepBy\` q@+{- | Parse zero or more @p@ separated by @q@ via @p \`sepBy\` q@++=== __Examples__++>>> runParserT (sat isDigit `sepBy` symbol ',') "1,2,3,four"+Right ("123",",four")+-} sepBy :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] sepBy p = option [] . sepBy1 p --- | Parse one or more @p@ separated by @q@ via @p \`sepBy1\` q@+{- | Parse one or more @p@ separated by @q@ via @p \`sepBy1\` q@++=== __Examples__++>>> runParserT (sat isDigit `sepBy1` symbol ',') "1,2,3,four"+Right ("123",",four")+-} sepBy1 :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] sepBy1 p sep = (:) <$> p <*> many (sep *> p) --- | Parse zero or more @p@ separated and ended by @q@ via @p \`endBy\` q@+{- | Parse zero or more @p@ separated and ended by @q@ via @p \`endBy\` q@++=== __Examples__++>>> runParserT (sat isDigit `endBy` symbol ',') "1,2,3,four"+Right ("123","four")+-} endBy :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] endBy p = option [] . endBy1 p --- | Parse one or more @p@ separated and ended by @q@ via @p \`endBy1\` q@+{- | Parse one or more @p@ separated and ended by @q@ via @p \`endBy1\` q@++=== __Examples__++>>> runParserT (sat isDigit `endBy1` symbol ',') "1,2,3,four"+Right ("123","four")+-} endBy1 :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] endBy1 p sep = sepBy1 p sep <* sep --- | Parse zero or more @p@ left-chained with @op@ atop @a@ via @chainl p op a@+{- | Parse zero or more @p@ left-chained with @op@ atop @a@ via @chainl p op a@++=== __Examples__++>>> runParserT (chainl (read <$> some (sat isDigit)) ((+) <$ item) 10) "2a3b4c"+Right (9,"c")+-} chainl :: (Monad m, Eq s) => ParserT s m a@@ -208,7 +262,13 @@ -> ParserT s m a chainl p op a = option a $ chainl1 p op --- | Parse one or more @p@ left-chained with @op@ via @chainl1 p op@+{- | Parse one or more @p@ left-chained with @op@ via @chainl1 p op@++=== __Examples__++>>> runParserT (chainl1 (read <$> some (sat isDigit)) ((+) <$ item)) "2a3b4c"+Right (9,"c")+-} chainl1 :: (Monad m, Eq s) => ParserT s m a@@ -218,7 +278,13 @@ where go a = option a $ op <*> pure a <*> p >>= go --- | Parse zero or more @p@ right-chained with @op@ atop @a@ via @chainr p op a@+{- | Parse zero or more @p@ right-chained with @op@ atop @a@ via @chainr p op a@++=== __Examples__++>>> runParserT (chainr (read <$> some (sat isDigit)) ((*) <$ item) 10) "2a3b4c"+Right (24,"c")+-} chainr :: (Monad m, Eq s) => ParserT s m a@@ -227,7 +293,13 @@ -> ParserT s m a chainr p op a = option a $ chainr1 p op --- | Parse one or more @p@ right-chained with @op@ via @chainr1 p op@+{- | Parse one or more @p@ right-chained with @op@ via @chainr1 p op@++=== __Examples__++>>> runParserT (chainr1 (read <$> some (sat isDigit)) ((*) <$ item)) "2a3b4c"+Right (24,"c")+-} chainr1 :: (Monad m, Eq s) => ParserT s m a@@ -238,7 +310,13 @@ go = p >>= rest rest a = option a $ op <*> pure a <*> go >>= rest --- | Parse @p@ enclosed by @a@ and @b@ via @between a b p@+{- | Parse @p@ enclosed by @a@ and @b@ via @between a b p@++=== __Examples__++>>> runParserT (between (symbol '(') (symbol ')') (many $ sat isLetter)) "(yes)"+Right ("yes","")+-} between :: (Monad m) => ParserT s m open@@ -247,11 +325,23 @@ -> ParserT s m a between open close p = open *> p <* close --- | Parse @p@ returning @a@ in case of failure via @option a p@+{- | Parse @p@ returning @a@ in case of failure via @option a p@++=== __Examples__++>>> runParserT (option "foo" $ string "bar") "baz"+Right ("foo","baz")+-} option :: (Monad m, Eq s) => a -> ParserT s m a -> ParserT s m a option a p = p <|> pure a --- | Parse @p@, without consuming input, iff @p@ fails via @reject p@+{- | Parse @p@, without consuming input, iff @p@ fails via @reject p@++=== __Examples__++>>> runParserT (string "foo" <* reject (sat isLetter)) "foo(bar)"+Right ("foo","(bar)")+-} reject :: (Monad m, Show a) => ParserT s m a -> ParserT s m () reject p = ParserT $ \ss -> runParserT p ss@@ -259,7 +349,13 @@ (const . pure $ Right ((), ss)) (pure . Left . ParseError . pure . mappend "unexpected " . show . fst) --- | Parse @p@, without consuming input, iff @p@ succeeds via @accept p@+{- | Parse @p@, without consuming input, iff @p@ succeeds via @accept p@++=== __Examples__++>>> runParserT (accept item >>= pure . (== 'a')) "foo"+Right (False,"foo")+-} accept :: (Monad m) => ParserT s m a -> ParserT s m a accept p = ParserT $ \ss -> runParserT p ss
mini.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: mini-version: 1.2.2.0+version: 1.2.2.1 license: MIT license-file: LICENSE copyright: (c) 2023-2024 Victor Wallsten