2020-05-18 15:59:07 +03:00
|
|
|
module Language.JSON.Parser
|
|
|
|
|
|
|
|
import Language.JSON.Data
|
|
|
|
import Text.Parser
|
|
|
|
import Data.List
|
|
|
|
|
|
|
|
import public Language.JSON.Tokens
|
|
|
|
|
2020-05-21 18:03:00 +03:00
|
|
|
%default total
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
punct : Punctuation -> Grammar state JSONToken True ()
|
2020-05-18 15:59:07 +03:00
|
|
|
punct p = match $ JTPunct p
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
rawString : Grammar state JSONToken True String
|
2020-05-18 15:59:07 +03:00
|
|
|
rawString = do mstr <- match JTString
|
2021-08-06 12:03:13 +03:00
|
|
|
the (Grammar _ _ False _) $
|
2020-05-18 15:59:07 +03:00
|
|
|
case mstr of
|
|
|
|
Just str => pure str
|
|
|
|
Nothing => fail "invalid string"
|
|
|
|
|
|
|
|
mutual
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
json : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
json = object
|
|
|
|
<|> array
|
|
|
|
<|> string
|
|
|
|
<|> boolean
|
|
|
|
<|> number
|
|
|
|
<|> null
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
object : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
object = do punct $ Curly Open
|
|
|
|
commit
|
|
|
|
props <- properties
|
|
|
|
punct $ Curly Close
|
|
|
|
pure $ JObject props
|
|
|
|
where
|
2021-08-06 12:03:13 +03:00
|
|
|
properties : Grammar state JSONToken False (List (String, JSON))
|
2020-05-18 15:59:07 +03:00
|
|
|
properties = sepBy (punct Comma) $
|
|
|
|
do key <- rawString
|
|
|
|
punct Colon
|
|
|
|
value <- json
|
|
|
|
pure (key, value)
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
array : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
array = do punct (Square Open)
|
|
|
|
commit
|
|
|
|
vals <- values
|
|
|
|
punct (Square Close)
|
|
|
|
pure (JArray vals)
|
|
|
|
where
|
2021-08-06 12:03:13 +03:00
|
|
|
values : Grammar state JSONToken False (List JSON)
|
2020-05-18 15:59:07 +03:00
|
|
|
values = sepBy (punct Comma) json
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
string : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
string = map JString rawString
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
boolean : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
boolean = map JBoolean $ match JTBoolean
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
number : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
number = map JNumber $ match JTNumber
|
|
|
|
|
|
|
|
private
|
2021-08-06 12:03:13 +03:00
|
|
|
null : Grammar state JSONToken True JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
null = map (const JNull) $ match JTNull
|
|
|
|
|
|
|
|
export
|
2021-08-06 12:03:13 +03:00
|
|
|
parseJSON : List (WithBounds JSONToken) -> Maybe JSON
|
2020-05-18 15:59:07 +03:00
|
|
|
parseJSON toks = case parse json $ filter (not . ignored) toks of
|
|
|
|
Right (j, []) => Just j
|
|
|
|
_ => Nothing
|