Skip to content

Commit 84857eb

Browse files
committed
feat: add aeson parser transformer
1 parent 42de75f commit 84857eb

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

megaparsec-utils.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ library
2626
src
2727
ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
2828
build-depends:
29-
base >=4.7 && <5
29+
aeson
30+
, base >=4.7 && <5
3031
, extra
3132
, megaparsec
3233
, parser-combinators
34+
, text
3335
, time
3436
, uuid
3537
default-language: Haskell2010
@@ -46,12 +48,14 @@ test-suite megaparsec-utils-test
4648
ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
4749
build-depends:
4850
QuickCheck
51+
, aeson
4952
, base >=4.7 && <5
5053
, extra
5154
, hspec
5255
, megaparsec
5356
, megaparsec-utils
5457
, parser-combinators
58+
, text
5559
, time
5660
, uuid
5761
default-language: Haskell2010

package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ description: Utilities for the Megaparsec library.
1313

1414
dependencies:
1515
- base >= 4.7 && < 5
16+
- aeson
1617
- extra
1718
- megaparsec
1819
- parser-combinators
20+
- text
1921
- time
2022
- uuid
2123

src/Text/Megaparsec/Utils.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Text.Megaparsec.Utils
77
, numParser
88
, occurrence
99
, occurrences
10+
, parsecToJSONParser
1011
, parsecToReadsPrec
1112
, posDecNumParser
1213
, posNumParser
@@ -17,14 +18,17 @@ import Control.Applicative (many, some, (<|>))
1718
import Control.Applicative.Combinators (choice)
1819
import Control.Monad (replicateM, void)
1920
import Control.Monad.Combinators (optional)
21+
import Data.Aeson.Types (Parser, Value, withText)
2022
import Data.Functor (($>))
2123
import Data.List (intercalate, sortOn)
2224
import Data.List.NonEmpty (NonEmpty ((:|)))
2325
import Data.Maybe (fromJust)
26+
import qualified Data.Text as T (unpack)
2427
import Data.UUID (UUID)
2528
import qualified Data.UUID as U (fromString)
2629
import Data.Void (Void)
27-
import Text.Megaparsec (Parsec, anySingle, runParser,
30+
import Text.Megaparsec (Parsec, anySingle,
31+
errorBundlePretty, runParser,
2832
try)
2933
import Text.Megaparsec.Char (char, digitChar, hexDigitChar,
3034
string')
@@ -76,6 +80,15 @@ posNumParser = read <$> some digitChar
7680
numParser :: Parsec Void String Int
7781
numParser = (char '-' >> negate <$> posNumParser) <|> posNumParser
7882

83+
-- | Convert a 'Parsec' parser into a 'Parser' suited for 'FromJSON' instances.
84+
parsecToJSONParser
85+
:: String
86+
-> Parsec Void String a
87+
-> Value
88+
-> Parser a
89+
parsecToJSONParser n p =
90+
withText n $ either (fail . errorBundlePretty) pure . runParser p n . T.unpack
91+
7992
-- | Convert a 'Parsec' parser into a 'ReadS' parser. Useful for defining 'Read'
8093
-- instances with 'Megaparsec'.
8194
parsecToReadsPrec :: Parsec Void String a -> ReadS a

0 commit comments

Comments
 (0)