{-# LANGUAGE ScopedTypeVariables #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides the ChParser typeclass and some methods for input parsing.
module Text.Chatty.Parser where

import Control.Monad
import Data.Char
import Text.Chatty.Scanner

-- | Typeclass for input parsing.
class ChScanner m => ChParser m where
  -- | Abort the parse process.
  pabort :: m a
  -- | Provide an alternative if the first choice fails. This version nubs the available forks and thus only works with instances of 'Eq'.
  (??) :: Eq a => m a -> m a -> m a
  -- | Provide an alternative if the first choice fails. This version works for everything, but you should use '??' whenever possible.
  (???) :: m a -> m a -> m a
  -- | Find all/one solution(s) for the given parsing function
  ptry :: MonadPlus n => m a -> m (n a)

-- | An prettier alias for 'mscan1'. It scans and removes the next available character.
request :: ChScanner m => m Char
request = mscan1

-- | Scan the next character and abort if it does not match the given one.
match :: ChParser m => Char -> m Char
match c = do
  k <- request
  if k == c then return c else pabort

-- | Try to match all of the given characters. Abort otherwise.
matchs :: ChParser m => String -> m String
matchs [] = return []
matchs (c:cs) = do
  match c
  matchs cs
  return (c:cs)

-- | Try to match a whitespace. Abort otherwise.
white :: ChParser m => m Char
white = do
  k <- request
  if isSpace k then return k else pabort

-- | Try to match a digit. Abort otherwise.
digit :: ChParser m => m Int
digit = do
  k <- request
  if isDigit k then return (read [k]) else pabort

-- | Try to match a letter. Abort otherwise.
alpha :: ChParser m => m Char
alpha = do
  k <- request
  if isAlpha k then return k else pabort

-- | Try to match a letter or digit. Abort otherwise.
anum :: ChParser m => m Char
anum = do
  k <- request
  if isAlphaNum k then return k else pabort

-- | Try to run the given parsing function. Return 'Nothing' otherwise.
possibly :: ChParser m => m a -> m (Maybe a)
possibly f = liftM Just f ??? return Nothing

-- | Try to run the given parsing function as often as possible.
many :: ChParser m => m a -> m [a]
many f = do
  x <- possibly f
  case x of
    Nothing -> return []
    Just x -> do
      xs <- many f
      return (x : xs)

-- | Try to run the given parsing function as often as possible, but at least once. Abort otherwise.
some :: ChParser m => m a -> m [a]
some f = do
  x <- f
  xs <- many f
  return (x : xs)

-- | Try to match a natural decimal number.
number :: ChParser m => m Int
number = do
  many white
  ds <- some digit
  many white
  return $ foldl (\k n -> k * 10 + n) 0 ds

-- | Try to match a fractional number.
fract :: ChParser m => m Double
fract = liftM fromIntegral number ?? do
  many white
  (ds :: [Integer]) <- liftM (map fromIntegral) $ many digit
  match '.' ?? match ','
  (es :: [Integer]) <- liftM (map fromIntegral) $ many digit
  return (fromIntegral (foldl (\k n -> k * 10 + n) 0 (ds++es)) / (10 ^ length es))