{-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell -XQuasiQuotes -XUndecidableInstances #-}

-- | This package expose the function @jsonQQ@ that compile time converts json code into a @Text.JSON.JSValue@.
--    @jsonQQ@ got the signature
--    
--    > jsonQQ :: QuasiQuoter
--    
--    and is used like
--    
--    > myCode = [$jsonQQ| {age: 23, name: "Pelle", likes: ["mac","Haskell"] } |]
--    
--    where it is important that
--    
--    * you got no space in @[$jsonQQ|@ and
--    
--    * no additional code after @|]@.
--    
--    The quasiquatation can also bind to variables like
--    
--    > myCode = [$jsonQQ| {age: <<age>>, name: <<name>>} |]
--    > where age = 34 :: Integer
--    >       name = "Pelle"
--    
--    where the function  @toJSON@ will be called on @age@ and @name@ runtime.
--    
--    You can use a similar syntax if you want to insert a value of type JSValue like
--    
--    > myCode = [$jsonQQ| {"age": <<<age>>>} |]
--    
--    If you want to replace the name of the key in a hash you'll use the $-syntax:
--    
--    > foo = [$jsonQQ| {$bar: 42} |]
--    > bar = "age"
--    

module Text.JSON.QQ (
  jsonQQ
) where

import Language.Haskell.TH
import Language.Haskell.TH.Quote

import Data.Data
import Data.Maybe

import Text.JSON
import Text.JSON.Generic

import Data.Ratio
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Error

jsonQQ :: QuasiQuoter
jsonQQ = QuasiQuoter jsonExp jsonPat


jsonExp :: String -> ExpQ
jsonExp txt =
  case parsed' of 
    Left err -> error $ "Error in jsonExp: " ++ show err
    Right val -> return $toExp val -- test -- dataToExpQ undefined test -- [| test |] -- [| toJSObject [("a","b")] |] --
  where
    parsed' = parse jpValue "txt" txt
    -- test = JSObject $ toJSObject [("a",JSNull)]

jsonPat :: String -> PatQ
jsonPat s = undefined


----
-- JSValue etc to ExpQ
---------

class ToExp a where
  toExp :: a -> Exp

instance ToExp JsonValue where
  toExp (JsonString str) = 
    AppE (ConE $ mkName "Text.JSON.Types.JSString") (AppE (VarE $ mkName "Text.JSON.Types.toJSString") (LitE (StringL $ str)))

  toExp (JsonNull) = ConE $ mkName "Text.JSON.Types.JSNull"

  toExp (JsonObject objs) = 
    AppE (ConE $ mkName "Text.JSON.Types.JSObject") (AppE (VarE $ mkName "Text.JSON.Types.toJSObject") (ListE $ jsList ))
    where
      jsList :: [Exp] -- [(String,JSValue)]
      jsList = map objs2list (objs)
      objs2list :: (HashKey,JsonValue) -> Exp
      objs2list (HashStringKey k,v) = TupE [LitE (StringL k), toExp v]
      objs2list (HashVarKey k,v) = TupE [VarE $ mkName k, toExp v]

  toExp (JsonArray arr) =
    AppE (ConE $ mkName "Text.JSON.Types.JSArray") (ListE $ map toExp arr)

  toExp (JsonNumber b rat) =
    AppE (AppE (ConE $ mkName "Text.JSON.Types.JSRational") (ConE $ mkName (if b then "True" else "False"))) (InfixE (Just (LitE (IntegerL $ numerator rat))) (VarE $ mkName "Data.Ratio.%") (Just (LitE (IntegerL $ denominator rat))))
    
  toExp (JsonVar v) =
    AppE (VarE $ mkName "Text.JSON.Generic.toJSON") (VarE $ mkName v)

  toExp (JsonIdVar v) =
    VarE $ mkName v

  toExp (JsonBool b) =
    AppE (ConE $ mkName "Text.JSON.Types.JSBool") (ConE $ mkName (if b then "True" else "False"))

-------
-- ToJsonOrId

-- class ToJsonOrId a where
--   toJsonOrId :: a -> JSValue

-------
-- Internal representation

data JsonValue =
  JsonNull
  | JsonString String
  | JsonNumber Bool Rational
  | JsonObject [(HashKey,JsonValue)] -- [(String,JsonValue)]
  | JsonArray [JsonValue]
  | JsonVar String
  | JsonIdVar String
  | JsonBool Bool

data HashKey =
  HashVarKey String
  | HashStringKey String

-- data JsonHashPair =
--   JsonStringPair String JsonValue
--   | JsonVarPair String JsonValue

------
-- Grammar
-- jp = json parsec
-----

data QQJsCode =
  QQjs JSValue
  | QQcode String

jpValue :: CharParser st JsonValue
jpValue = do
  spaces
  res <- jpTrue <|> jpFalse <|> try jpVar <|> try jpIdVar <|> jpNull <|> jpString <|> jpObject <|> try jpNumber  <|> jpArray
  spaces
  return res

jpTrue :: CharParser st JsonValue
jpTrue = do
  string "true"
  return $JsonBool True

jpFalse :: CharParser st JsonValue
jpFalse = do
  string "false"
  return $ JsonBool False

jpIdVar :: CharParser st JsonValue
jpIdVar = do
  string "<<<"
  sym <- symbol
  string ">>>"
  return $ JsonIdVar sym

jpVar :: CharParser st JsonValue
jpVar = do
  string "<<"
  sym <- symbol
  string ">>"
  return $ JsonVar sym

jpNull :: CharParser st JsonValue
jpNull = do
  string "null"
  return $ JsonNull

jpString :: CharParser st JsonValue 
jpString = do
  char '"'
  sym <- try $ option [""] $ many chars
  char '"'
  return $ JsonString $ concat sym 

jpNumber :: CharParser st JsonValue 
jpNumber = do
  val <- float
  return $ JsonNumber False (toRational val)

jpObject :: CharParser st JsonValue
jpObject = do
  char '{'
  spaces
  list <- commaSep jpHash
  spaces
  char '}'
  return $ JsonObject $ list
  where
    jpHash :: CharParser st (HashKey,JsonValue) -- (String,JsonValue)
    jpHash = do
      spaces
      name <- varKey <|> symbolKey <|> quotedStringKey
      spaces
      char ':'
      spaces
      value <- jpValue
      spaces
      return (name,value)

symbolKey :: CharParser st HashKey
symbolKey = do
  sym <- symbol
  return $ HashStringKey sym

quotedStringKey :: CharParser st HashKey
quotedStringKey = do
  quo <- quotedString
  return $ HashStringKey quo

varKey :: CharParser st HashKey
varKey = do
  char '$'
  sym <- symbol
  return $ HashVarKey sym

jpArray :: CharParser st JsonValue
jpArray = do
  char '['
  spaces
  list <- commaSep jpValue
  spaces
  char ']'
  return $ JsonArray list

-------
-- helpers for parser/grammar

float :: CharParser st Double 
float = do
  isMinus <- option ' ' (char '-')
  d <- many1 digit
  o <- option "" withDot
  e <- option "" withE
  return $ (read $ isMinus : d ++ o ++ e :: Double)

withE = do
  e <- char 'e' <|> char 'E'
  plusMinus <- option "" (string "+" <|> string "-")
  d <- many digit
  return $ e : plusMinus ++ d 

withDot = do
  o <- char '.'
  d <- many digit
  return $ o:d

-- jsonSymbol :: CharParser st String
-- jsonSymbol = do
--   symbol <|> symbol'
--   where
--     symbol' = do
--       char '"'

quotedString :: CharParser st String
quotedString = do
  char '"'
  sym <- try $ option [""] $ many chars
  char '"'
  return $ concat sym 

symbol :: CharParser st String
symbol = many1 (noneOf "\\ \":;><") -- alphaNum -- should be replaced!

commaSep p  = p `sepBy` (char ',')

chars :: CharParser st String
chars = do
   try (string "\\\"")
   <|> try (string "\\/")
   <|> try (string "\\\\")
   <|> try (string "\\b")
   <|> try (string "\\f")
   <|> try (string "\\n")
   <|> try (string "\\r")
   <|> try (string "\\t")
   <|> try (unicodeChars)
   <|> many1 (noneOf "\\\"")

unicodeChars :: CharParser st String
unicodeChars = do
  u <- string "\\u"
  d1 <- hexDigit
  d2 <- hexDigit
  d3 <- hexDigit
  d4 <- hexDigit
  return $u ++ [d1] ++ [d2] ++ [d3] ++ [d4]