module ParseList where

import Control.Applicative
import Data.Char
import Parser

data ListInt
  = Nil
  | Cons Int ListInt

instance Show ListInt where
  show Nil = "[]"
  show (Cons n ns) = show n ++ ":" ++ show ns

instance Read ListInt where
  readsPrec _ = runParser listInt

listInt :: Parser ListInt
listInt = nil <++ cons <++ sweetList

nil :: Parser ListInt
nil = const Nil <$> string "[]"

{-
int :: Parser Int
int = do
  n <- some $ satisfy isDigit
  pure $ read n

cons :: Parser ListInt
cons = do
  n <- int
  char ':'
  ns <- listInt
  pure $ Cons n ns

sweetList :: Parser ListInt
sweetList = do
  ns <- between (char '[') (char ']') (sepBy int (char ','))
  pure $ foldr Cons Nil ns
-}

int :: Parser Int
int =
  pure read
    <*> (some $ satisfy isDigit)

cons :: Parser ListInt
cons =
  pure Cons
    <*> int
    <*  char ':'
    <*> listInt

sweetList :: Parser ListInt
sweetList =
  pure (foldr Cons Nil)
    <*> between (char '[') (char ']') (sepBy int (char ','))
