aboutsummaryrefslogtreecommitdiff
path: root/ch19/Module_19_b_1.hs
blob: eb68a9cc61867abc7c85e758510c77e4a253c5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- Write a many parser, with type Parser a -> Parser [a]. It should apply a
-- parser until it fails.

-- Control.Monad.Error is deprecated. Control.Monad.Except should be used
-- instead. I modified the code from examples here to use it.


{-- From examples/examples/ch19/ParseInt.hs and modified --}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Module_19_b_1 where

import Control.Monad.Except
import Control.Monad
import Control.Monad.State
import qualified Data.ByteString.Char8 as B

data ParseError = NumericOverflow
                | EndOfInput
                | Chatty String
                  deriving (Eq, Ord, Show)

newtype Parser a = P {
      runP :: ExceptT ParseError (State B.ByteString) a
    } deriving (Functor, Applicative, Monad, MonadError ParseError)

liftP :: State B.ByteString a -> Parser a
liftP m = P (lift m)

satisfy :: (Char -> Bool) -> Parser Char
satisfy p = do
  s <- liftP get
  case B.uncons s of
    Nothing         -> throwError EndOfInput
    Just (c, s')
        | p c       -> liftP (put s') >> return c
        | otherwise -> throwError (Chatty "satisfy failed")

runParser :: Parser a -> B.ByteString
          -> Either ParseError (a, B.ByteString)
runParser p bs = case runState (runExceptT (runP p)) bs of
                   (Left err, _) -> Left err
                   (Right r, bs) -> Right (r, bs)

optional :: Parser a -> Parser (Maybe a)
optional p = (Just `liftM` p) `catchError` \_ -> return Nothing
{-- End of code from examples --}


many :: Parser a -> Parser [a]
many p = ((:) <$> p <*> many p) `catchError` \_ -> return []


-- ghci> :l Module_19_b_1.hs
-- [1 of 1] Compiling Module_19_b_1    ( Module_19_b_1.hs, interpreted )
-- Ok, one module loaded.

-- ghci> :m +Data.Char

-- ghci> runParser (many (satisfy isDigit)) (B.pack "")
-- Right ("","")

-- ghci> runParser (many (satisfy isDigit)) (B.pack "abc")
-- Right ("","abc")

-- ghci> runParser (many (satisfy isDigit)) (B.pack "9abc")
-- Right ("9","abc")

-- ghci> runParser (many (satisfy isDigit)) (B.pack "987abc")
-- Right ("987","abc")

-- ghci> runParser (many (satisfy isDigit)) (B.pack "x987abc")
-- Right ("","x987abc")