1 -- |Yet another parser combinator. This is mostly a subset of
2 -- "Text.ParserCombinators.Parsec" but there are some differences:
4 -- * This parser works on 'Data.ByteString.Base.LazyByteString'
5 -- instead of 'Prelude.String'.
7 -- * Backtracking is the only possible behavior so there is no \"try\"
10 -- * On success, the remaining string is returned as well as the
13 -- * You can choose whether to treat reaching EOF (trying to eat one
14 -- more letter at the end of string) a fatal error or to treat it a
15 -- normal failure. If a fatal error occurs, the entire parsing
16 -- process immediately fails without trying any backtracks. The
17 -- default behavior is to treat EOF fatal.
19 -- In general, you don't have to use this module directly.
20 module Network.HTTP.Lucu.Parser
54 import Control.Monad.State.Strict
55 import qualified Data.ByteString.Lazy as Lazy (ByteString)
56 import qualified Data.ByteString.Lazy.Char8 as B hiding (ByteString)
58 -- |@'Parser' a@ is obviously a parser which parses and returns @a@.
59 newtype Parser a = Parser {
60 runParser :: State ParserState (ParserResult a)
66 pstInput :: Lazy.ByteString
67 , pstIsEOFFatal :: !Bool
72 data ParserResult a = Success !a
73 | IllegalInput -- 受理出來ない入力があった
74 | ReachedEOF -- 限界を越えて讀まうとした
78 -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
79 instance Monad Parser where
80 p >>= f = Parser $! do saved <- get -- 失敗した時の爲に状態を保存
83 Success a -> runParser (f a)
84 IllegalInput -> do put saved -- 状態を復歸
86 ReachedEOF -> do put saved -- 状態を復歸
88 return x = x `seq` Parser $! return $! Success x
89 fail _ = Parser $! return $! IllegalInput
91 -- |@'failP'@ is just a synonym for @'Prelude.fail'
92 -- 'Prelude.undefined'@.
94 failP = fail undefined
96 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(# result,
98 parse :: Parser a -> Lazy.ByteString -> (# ParserResult a, Lazy.ByteString #)
99 parse p input -- input は lazy である必要有り。
101 let (result, state') = runState (runParser p) (PST input True)
103 result `seq` (# result, pstInput state' #) -- pstInput state' も lazy である必要有り。
105 -- |@'parseStr' p str@ packs @str@ and parses it.
106 parseStr :: Parser a -> String -> (# ParserResult a, Lazy.ByteString #)
108 = p `seq` -- input は lazy である必要有り。
109 parse p (B.pack input)
112 anyChar :: Parser Char
114 do state@(PST input _) <- get
118 do put $! state { pstInput = B.tail input }
119 return (Success $! B.head input)
124 do PST input _ <- get
130 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
131 allowEOF :: Parser a -> Parser a
133 Parser $! do saved@(PST _ isEOFFatal) <- get
134 put $! saved { pstIsEOFFatal = False }
136 result <- runParser f
139 put $! state { pstIsEOFFatal = isEOFFatal }
144 satisfy :: (Char -> Bool) -> Parser Char
153 char :: Char -> Parser Char
154 char c = c `seq` satisfy (== c)
157 string :: String -> Parser String
158 string str = str `seq`
165 -- |This is the backtracking alternation. There is no non-backtracking
167 (<|>) :: Parser a -> Parser a -> Parser a
170 Parser $! do saved <- get -- 状態を保存
171 result <- runParser f
173 Success a -> return $! Success a
174 IllegalInput -> do put saved -- 状態を復歸
176 ReachedEOF -> if pstIsEOFFatal saved then
184 choice :: [Parser a] -> Parser a
185 choice = foldl (<|>) failP
188 oneOf :: [Char] -> Parser Char
189 oneOf = foldl (<|>) failP . map char
192 notFollowedBy :: Parser a -> Parser ()
195 Parser $! do saved <- get -- 状態を保存
196 result <- runParser p
198 Success _ -> do put saved -- 状態を復歸
200 IllegalInput -> do put saved -- 状態を復歸
202 ReachedEOF -> do put saved -- 状態を復歸
207 digit = do c <- anyChar
208 if c >= '0' && c <= '9' then
214 hexDigit :: Parser Char
215 hexDigit = do c <- anyChar
216 if (c >= '0' && c <= '9') ||
217 (c >= 'a' && c <= 'f') ||
218 (c >= 'A' && c <= 'F') then
224 many :: Parser a -> Parser [a]
225 many !p = Parser $! many' p []
227 -- This implementation is rather ugly but we need to make it
228 -- tail-recursive to avoid stack overflow.
229 many' :: Parser a -> [a] -> State ParserState (ParserResult [a])
232 result <- runParser p
234 Success a -> many' p (a:soFar)
235 IllegalInput -> do put saved
236 return $! Success $ reverse soFar
237 ReachedEOF -> if pstIsEOFFatal saved then
242 return $! Success $ reverse soFar
245 many1 :: Parser a -> Parser [a]
251 count :: Int -> Parser a -> Parser [a]
252 count !n !p = Parser $! count' n p []
254 -- This implementation is rather ugly but we need to make it
255 -- tail-recursive to avoid stack overflow.
256 count' :: Int -> Parser a -> [a] -> State ParserState (ParserResult [a])
257 count' 0 _ !soFar = return $! Success $ reverse soFar
258 count' !n !p !soFar = do saved <- get
259 result <- runParser p
261 Success a -> count' (n-1) p (a:soFar)
262 IllegalInput -> do put saved
264 ReachedEOF -> do put saved
269 option :: a -> Parser a -> Parser a
270 option def p = p `seq`
274 sepBy :: Parser a -> Parser sep -> Parser [a]
275 sepBy p sep = p `seq` sep `seq`
276 sepBy1 p sep <|> return []
279 sepBy1 :: Parser a -> Parser sep -> Parser [a]
280 sepBy1 p sep = p `seq` sep `seq`
282 xs <- many $! sep >> p
294 crlf :: Parser String
295 crlf = string "\x0d\x0a"