syntax-example-json 0.2.0.0 → 1.0.0.0
raw patch · 2 files changed
+26/−21 lines, 2 filesdep ~semi-isodep ~syntaxdep ~syntax-attoparsec
Dependency ranges changed: semi-iso, syntax, syntax-attoparsec, syntax-printer
Files
- Main.hs +21/−18
- syntax-example-json.cabal +5/−3
Main.hs view
@@ -1,15 +1,18 @@+{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedStrings #-} import Control.Applicative+import Control.Category.Structures import Control.Lens import Control.Lens.SemiIso+import Control.SIArrow import qualified Data.Attoparsec.Text.Lazy as AP import Data.Map (Map) import qualified Data.Map as Map import Data.Scientific (Scientific)-import Data.SemiIsoFunctor import qualified Data.Syntax as S import qualified Data.Syntax.Attoparsec.Text.Lazy as S import Data.Syntax.Char (SyntaxText)@@ -33,28 +36,28 @@ $(makePrisms ''Value) --- Indent is basically a Reader monad transformer.+-- Indent is basically a Reader transformer. -- S.breakLine inserts a new line and correct indentation, -- but does not require any formatting when parsing (it just -- skips all white space). -- S.indented increases the indentation level. -- | Char encoded as 4 hex digits.-hexCode :: SyntaxText syn => Indent syn Char+hexCode :: SyntaxText syn => Indent syn () Char hexCode = from enum . bifoldl1_ (iso u f) /$/ sireplicate 4 S.digitHex where f (x, y) = 16 * x + y u 0 = Nothing u n = Just (n `quotRem` 16) -string :: SyntaxText syn => Indent syn Text+string :: SyntaxText syn => Indent syn () Text string = S.packed /$/ S.char '"' */ simany character /* S.char '"' where character = S.satisfy (\x -> x /= '"' && x /= '\\')- /|/ S.char '\\' */ escapedChar+ /+/ S.char '\\' */ escapedChar escapedChar = S.char 'u' */ hexCode- /|/ S.choice+ /+/ S.choice [ exact v /$/ S.char e | (v, e) <- [ ('"', '"'), ('\\', '\\'), ('/', '/'), ('\b', 'b') , ('\f', 'f'), ('\n', 'n') , ('\r', 'r'), ('\t', 't')]@@ -63,7 +66,7 @@ mapFromList :: Ord i => Iso' (Map i v) [(i, v)] mapFromList = iso Map.toList Map.fromList -object :: SyntaxText syn => Indent syn Value+object :: SyntaxText syn => Indent syn () Value object = _Object . mapFromList /$~ S.char '{' /*/ S.indented (@@ -73,7 +76,7 @@ /*/ S.breakLine /*/ S.char '}' where field = string /* S.spaces_ /* S.char ':' /* S.spaces /*/ value -array :: SyntaxText syn => Indent syn Value+array :: SyntaxText syn => Indent syn () Value array = _Array /$~ S.char '[' /*/ S.indented (@@ -82,14 +85,14 @@ ) /*/ S.breakLine /*/ S.char ']' -value :: SyntaxText syn => Indent syn Value+value :: SyntaxText syn => Indent syn () Value value = _String /$/ string- /|/ _Number /$/ S.scientific- /|/ object- /|/ array- /|/ _Bool . exact True /$/ S.string "true"- /|/ _Bool . exact False /$/ S.string "false"- /|/ _Null /$/ S.string "null"+ /+/ _Number /$/ S.scientific+ /+/ object+ /+/ array+ /+/ _Bool . exact True /$/ S.string "true"+ /+/ _Bool . exact False /$/ S.string "false"+ /+/ _Null /$/ S.string "null" /?/ "Invalid JSON." main :: IO ()@@ -98,10 +101,10 @@ t <- T.getContents -- Indent with 2 spaces.- let tab :: SyntaxText syn => syn ()+ let tab :: SyntaxText syn => syn () () tab = sireplicate_ 2 (S.char ' ')- parser = S.getParser $ S.runIndent value tab- printer = S.runPrinter $ S.runIndent value tab+ parser = S.getParser_ $ S.runIndent value tab+ printer = S.runPrinter_ $ S.runIndent value tab -- Try to parse it. case AP.parse (AP.skipSpace *> parser <* AP.skipSpace <* AP.endOfInput) t of
syntax-example-json.cabal view
@@ -1,16 +1,17 @@ name: syntax-example-json-version: 0.2.0.0+version: 1.0.0.0 synopsis: Example JSON parser/pretty-printer. description: Example JSON parser/pretty-printer. . Source code: .- * <https://github.com/Pawel834/syntax-example-json/blob/master/Main.hs Main.hs>+ * <https://github.com/Pawel834/syntax-example-json/blob/7236aefc018ce3d968321b5b85ddcf80b46aeef4/Main.hs Main.hs> . Example input and output: . * <https://github.com/Pawel834/syntax-example-json/blob/master/in.json in.json>+ . * <https://github.com/Pawel834/syntax-example-json/blob/master/out.json out.json> license: MIT license-file: LICENSE@@ -28,5 +29,6 @@ executable syntax-example-json main-is: Main.hs default-language: Haskell2010- build-depends: base <5, semi-iso >= 0.5, syntax >= 0.3, syntax-attoparsec, syntax-printer,+ build-depends: base <5, semi-iso >= 1, syntax >= 1,+ syntax-attoparsec >= 1, syntax-printer >= 1, containers, text, scientific, lens, attoparsec