| 1 | module Data.JSON.QQ (JsonValue (..), HashKey (..), parsedJson) where |
| 2 | |
| 3 | import Control.Applicative |
| 4 | import Language.Haskell.TH |
| 5 | import Text.ParserCombinators.Parsec hiding (many, (<|>)) |
| 6 | import Language.Haskell.Meta.Parse |
| 7 | import qualified Data.Attoparsec.Text as A |
| 8 | import Data.Scientific (Scientific) |
| 9 | import qualified Data.Text as T |
| 10 | |
| 11 | parsedJson :: String -> Either ParseError JsonValue |
| 12 | parsedJson = parse (jpValue <* eof) "txt" |
| 13 | |
| 14 | ------- |
| 15 | -- Internal representation |
| 16 | |
| 17 | data JsonValue = |
| 18 | JsonNull |
| 19 | | JsonString String |
| 20 | | JsonNumber Scientific |
| 21 | | JsonObject [(HashKey,JsonValue)] |
| 22 | | JsonArray [JsonValue] |
| 23 | | JsonBool Bool |
| 24 | | JsonCode Exp |
| 25 | deriving (Eq, Show) |
| 26 | |
| 27 | data HashKey = |
| 28 | HashVarKey String |
| 29 | | HashStringKey String |
| 30 | deriving (Eq, Show) |
| 31 | |
| 32 | type JsonParser = Parser JsonValue |
| 33 | |
| 34 | jpValue :: JsonParser |
| 35 | jpValue = do |
| 36 | spaces |
| 37 | res <- jpBool <|> jpNull <|> jpString <|> jpObject <|> jpNumber <|> jpArray <|> jpCode |
| 38 | spaces |
| 39 | return res |
| 40 | |
| 41 | jpBool :: JsonParser |
| 42 | jpBool = JsonBool <$> (string "true" *> pure True <|> string "false" *> pure False) |
| 43 | |
| 44 | jpCode :: JsonParser |
| 45 | jpCode = JsonCode <$> (string "#{" *> parseExp') |
| 46 | where |
| 47 | parseExp' = do |
| 48 | str <- many1 (noneOf "}") <* char '}' |
| 49 | case (parseExp str) of |
| 50 | Left l -> fail l |
| 51 | Right r -> return r |
| 52 | |
| 53 | jpNull :: JsonParser |
| 54 | jpNull = string "null" *> pure JsonNull |
| 55 | |
| 56 | jpString :: JsonParser |
| 57 | jpString = between (char '"') (char '"') (option [""] $ many chars) >>= return . JsonString . concat -- do |
| 58 | |
| 59 | jpNumber :: JsonParser |
| 60 | jpNumber = JsonNumber <$> do |
| 61 | isMinus <- option "" (string "-") |
| 62 | d <- many1 digit |
| 63 | o <- option "" withDot |
| 64 | e <- option "" withE |
| 65 | convert (isMinus ++ d ++ o ++ e) |
| 66 | where |
| 67 | withE = do |
| 68 | e <- char 'e' <|> char 'E' |
| 69 | plusMinus <- option "" (string "+" <|> string "-") |
| 70 | d <- many digit |
| 71 | return $ e : plusMinus ++ d |
| 72 | |
| 73 | withDot = do |
| 74 | o <- char '.' |
| 75 | d <- many digit |
| 76 | return $ o:d |
| 77 | |
| 78 | convert :: String -> Parser Scientific |
| 79 | convert = either fail return . A.parseOnly (A.scientific <* A.endOfInput) . T.pack |
| 80 | |
| 81 | jpObject :: JsonParser |
| 82 | jpObject = do |
| 83 | list <- between (char '{') (char '}') (spaces *> commaSep jpHash) |
| 84 | return $ JsonObject $ list |
| 85 | where |
| 86 | jpHash :: CharParser () (HashKey,JsonValue) -- (String,JsonValue) |
| 87 | jpHash = do |
| 88 | spaces |
| 89 | name <- varKey <|> symbolKey <|> quotedStringKey |
| 90 | spaces |
| 91 | _ <- char ':' |
| 92 | spaces |
| 93 | value <- jpValue |
| 94 | spaces |
| 95 | return (name,value) |
| 96 | |
| 97 | symbolKey :: CharParser () HashKey |
| 98 | symbolKey = HashStringKey <$> symbol |
| 99 | |
| 100 | quotedStringKey :: CharParser () HashKey |
| 101 | quotedStringKey = HashStringKey <$> quotedString |
| 102 | |
| 103 | varKey :: CharParser () HashKey |
| 104 | varKey = HashVarKey <$> (char '$' *> symbol) |
| 105 | |
| 106 | jpArray :: CharParser () JsonValue |
| 107 | jpArray = JsonArray <$> between (char '[') (char ']') (spaces *> commaSep jpValue) |
| 108 | |
| 109 | ------- |
| 110 | -- helpers for parser/grammar |
| 111 | quotedString :: CharParser () String |
| 112 | quotedString = concat <$> between (char '"') (char '"') (option [""] $ many chars) |
| 113 | |
| 114 | symbol :: CharParser () String |
| 115 | symbol = many1 (noneOf "\\ \":;><${}") |
| 116 | |
| 117 | commaSep :: CharParser () a -> CharParser () [a] |
| 118 | commaSep p = p `sepBy` (char ',') |
| 119 | |
| 120 | chars :: CharParser () String |
| 121 | chars = do |
| 122 | try (string "\\\"" *> pure "\"") |
| 123 | <|> try (string "\\\\" *> pure "\\") |
| 124 | <|> try (string "\\/" *> pure "/") |
| 125 | <|> try (string "\\b" *> pure "\b") |
| 126 | <|> try (string "\\f" *> pure "\f") |
| 127 | <|> try (string "\\n" *> pure "\n") |
| 128 | <|> try (string "\\r" *> pure "\r") |
| 129 | <|> try (string "\\t" *> pure "\t") |
| 130 | <|> try (unicodeChars) |
| 131 | <|> many1 (noneOf "\\\"") |
| 132 | |
| 133 | unicodeChars :: CharParser () String |
| 134 | unicodeChars = do |
| 135 | u <- string "\\u" |
| 136 | d1 <- hexDigit |
| 137 | d2 <- hexDigit |
| 138 | d3 <- hexDigit |
| 139 | d4 <- hexDigit |
| 140 | return $ u ++ [d1] ++ [d2] ++ [d3] ++ [d4] |