{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
#if __GLASGOW_HASKELL__ < 709
{-# OPTIONS_GHC -fcontext-stack=50 #-}
#endif
module RERE.Examples.JSON where
import Prelude hiding (exponent)
import Data.Vec.Lazy (Vec (..))
import Data.Void (Void)
import qualified Data.Type.Nat as N
import qualified Data.Vec.Lazy as V
import RERE
import qualified RERE.CharSet as CS
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup (..))
#endif
type Size = N.Mult2 (N.Plus N.Nat5 N.Nat6)
jsonRE' :: RE Void
jsonRE' = compact (cfgToRE jsonNames jsonCFG)
jsonNames :: Vec Size Name
jsonNames = V.reverse $
"json" ::: "value" ::: "object" ::: "members" ::: "member" ::: "array" ::: "elements" ::: "element" ::: "string" ::: "characters" ::: "character" ::: "escape" ::: "hex" ::: "number" ::: "integer" ::: "digits" ::: "digit" ::: "onenine" ::: "fraction" ::: "exponent" ::: "sign" ::: "ws" ::: VNil
jsonCFG :: forall a. Ord a => CFG Size a
jsonCFG = V.reverse $
json ::: value ::: object ::: members ::: member ::: array ::: elements ::: element ::: string ::: characters ::: character ::: escape ::: hex ::: number ::: integer ::: digits ::: digit ::: onenine ::: fraction ::: exponent ::: sign ::: ws ::: VNil
where
_jsonV, valueV, objectV, membersV, memberV, arrayV, elementsV, elementV, stringV, charactersV, characterV, escapeV, hexV, numberV, integerV, digitsV, digitV, onenineV, fractionV, exponentV, signV, wsV :: CFGBase Size a
_jsonV = Var $ Left 21
valueV = Var $ Left 20
objectV = Var $ Left 19
membersV = Var $ Left 18
memberV = Var $ Left 17
arrayV = Var $ Left 16
elementsV = Var $ Left 15
elementV = Var $ Left 14
stringV = Var $ Left 13
charactersV = Var $ Left 12
characterV = Var $ Left 11
escapeV = Var $ Left 10
hexV = Var $ Left 9
numberV = Var $ Left 8
integerV = Var $ Left 7
digitsV = Var $ Left 6
digitV = Var $ Left 5
onenineV = Var $ Left 4
fractionV = Var $ Left 3
exponentV = Var $ Left 2
signV = Var $ Left 1
wsV = Var $ Left 0
json = elementV
value = unions [ objectV, arrayV, stringV, numberV, "true", "false", "null" ]
object = "{" <> wsV <> "}" \/ "{" <> membersV <> "}"
members = memberV \/ memberV <> "," <> membersV
member = wsV <> stringV <> wsV <> ":" <> elementV
array = "[" <> wsV <> "]" \/ "[" <> elementsV <> "]"
elements = elementV \/ elementV <> "," <> elementsV
element = wsV <> valueV <> wsV
string = "\"" <> charactersV <> "\""
characters = "" \/ characterV <> charactersV
character = Ch chars \/ "\\" <> escapeV
chars = CS.fromList [ c | c <- ['\x20' .. maxBound ], c /= '"', c /= '\\' ]
escape =
Ch (CS.fromList "\"\\/bfnrt")
\/ "u" <> hexV <> hexV <> hexV <> hexV
hex = digitV <> Ch (CS.fromList "ABCDEFabcdef")
number = integerV <> fractionV <> exponentV
integer = digitV \/ onenineV <> digitsV \/ "-" <> digitV \/ "-" <> onenineV <> digitsV
digits = digitV \/ digitV <> digitsV
digit = "0" \/ onenineV
onenine = Ch (CS.fromList "123456789")
fraction = "" \/ "." <> digitsV
exponent = "" \/ "E" <> signV <> digitsV \/ "e" <> signV <> digitsV
sign = "" \/ "+" \/ "-"
ws = unions ["", "\x20" <> wsV, "\x0A" <> wsV, "\x0D" <> wsV, "\x09" <> wsV ]
unions = foldr (\/) Null
jsonRE :: RE Void
jsonRE =
Let "ws" (Fix "ws" (Alt Eps (Alt (App (Ch " ") (Var B)) (Alt (App (Ch "\n") (Var B)) (Alt (App (Ch "\r") (Var B)) (App (Ch "\t") (Var B))))))) (Let "hex" (App (Ch "0123456789") (Ch "ABCDEFabcdef")) (Let "escape" (Alt (Ch "\"/\\bfnrt") (App (Ch "u") (App (Var B) (App (Var B) (App (Var B) (Var B)))))) (Let "character" (Alt (Ch (CS.fromIntervalList [('\32','\33'),('\35','\91'),('\93','\1114111')])) (App (Ch "\\") (Var B))) (Let "characters" (Fix "characters" (Alt Eps (App (Var (F B)) (Var B)))) (Let "string" (App (Ch "\"") (App (Var B) (Ch "\""))) (Let "digits" (Fix "digits" (Alt (Ch "0123456789") (App (Ch "0123456789") (Var B)))) (Let "integer" (Alt (Ch "0123456789") (Alt (App (Ch "123456789") (Var B)) (Alt (App (Ch "-") (Ch "0123456789")) (App (Ch "-") (App (Ch "123456789") (Var B)))))) (Let "fraction" (Alt Eps (App (Ch ".") (Var (F B)))) (Let "sign" (Alt Eps (Ch "+-")) (Let "exponent" (Alt Eps (Alt (App (Ch "E") (App (Var B) (Var (F (F (F B)))))) (App (Ch "e") (App (Var B) (Var (F (F (F B)))))))) (Let "number" (App (Var (F (F (F B)))) (App (Var (F (F B))) (Var B))) (Let "value" (Fix "value" (Let "element" (App (Var (F (F (F (F (F (F (F (F (F (F (F (F B))))))))))))) (App (Var B) (Var (F (F (F (F (F (F (F (F (F (F (F (F B))))))))))))))) (Let "member" (App (Var (F (F (F (F (F (F (F (F (F (F (F (F (F B)))))))))))))) (App (Var (F (F (F (F (F (F (F (F B))))))))) (App (Var (F (F (F (F (F (F (F (F (F (F (F (F (F B)))))))))))))) (App (Ch ":") (Var B))))) (Let "members" (Fix "members" (Alt (Var (F B)) (App (Var (F B)) (App (Ch ",") (Var B))))) (Let "object" (Alt (App (Ch "{") (App (Var (F (F (F (F (F (F (F (F (F (F (F (F (F (F (F B)))))))))))))))) (Ch "}"))) (App (Ch "{") (App (Var B) (Ch "}")))) (Let "elements" (Fix "elements" (Alt (Var (F (F (F (F B))))) (App (Var (F (F (F (F B))))) (App (Ch ",") (Var B))))) (Let "array" (Alt (App (Ch "[") (App (Var (F (F (F (F (F (F (F (F (F (F (F (F (F (F (F (F (F B)))))))))))))))))) (Ch "]"))) (App (Ch "[") (App (Var B) (Ch "]")))) (Alt (Var (F (F B))) (Alt (Var B) (Alt (Var (F (F (F (F (F (F (F (F (F (F (F (F (F B)))))))))))))) (Alt (Var (F (F (F (F (F (F (F B)))))))) (Alt (App (Ch "t") (App (Ch "r") (App (Ch "u") (Ch "e")))) (Alt (App (Ch "f") (App (Ch "a") (App (Ch "l") (App (Ch "s") (Ch "e"))))) (App (Ch "n") (App (Ch "u") (App (Ch "l") (Ch "l"))))))))))))))))) (App (Var (F (F (F (F (F (F (F (F (F (F (F (F B))))))))))))) (App (Var B) (Var (F (F (F (F (F (F (F (F (F (F (F (F B)))))))))))))))))))))))))))
#ifdef RERE_SLOW_DOCTEST
_doctest1 :: ()
_doctest1 = ()
#endif