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
55 import Control.Monad.State.Strict
56 import qualified Data.ByteString.Lazy as Lazy (ByteString)
57 import qualified Data.ByteString.Lazy.Char8 as B hiding (ByteString)
58 import qualified Data.Foldable as Fold
60 import qualified Data.Sequence as Seq
61 import Data.Sequence (Seq, (|>))
63 -- |@'Parser' a@ is obviously a parser which parses and returns @a@.
64 newtype Parser a = Parser {
65 runParser :: State ParserState (ParserResult a)
71 pstInput :: Lazy.ByteString
72 , pstIsEOFFatal :: !Bool
77 data ParserResult a = Success !a
78 | IllegalInput -- 受理出來ない入力があった
79 | ReachedEOF -- 限界を越えて讀まうとした
83 -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
84 instance Monad Parser where
85 p >>= f = Parser $! do saved <- get -- 失敗した時の爲に状態を保存
88 Success a -> runParser (f a)
89 IllegalInput -> do put saved -- 状態を復歸
91 ReachedEOF -> do put saved -- 状態を復歸
93 return !x = Parser $! return $! Success x
94 fail _ = Parser $! return $! IllegalInput
96 -- |@'failP'@ is just a synonym for @'Prelude.fail'
97 -- 'Prelude.undefined'@.
99 failP = fail undefined
101 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(# result,
103 parse :: Parser a -> Lazy.ByteString -> (# ParserResult a, Lazy.ByteString #)
104 parse !p input -- input は lazy である必要有り。
105 = let (!result, state') = runState (runParser p) (PST input True)
107 (# result, pstInput state' #) -- pstInput state' も lazy である必要有り。
109 -- |@'parseStr' p str@ packs @str@ and parses it.
110 parseStr :: Parser a -> String -> (# ParserResult a, Lazy.ByteString #)
111 parseStr !p input -- input は lazy である必要有り。
112 = parse p (B.pack input)
115 anyChar :: Parser Char
117 do state@(PST input _) <- get
121 do put $! state { pstInput = B.tail input }
122 return (Success $! B.head input)
127 do PST input _ <- get
133 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
134 allowEOF :: Parser a -> Parser a
136 = Parser $! do saved@(PST _ isEOFFatal) <- get
137 put $! saved { pstIsEOFFatal = False }
139 result <- runParser f
142 put $! state { pstIsEOFFatal = isEOFFatal }
147 satisfy :: (Char -> Bool) -> Parser Char
156 char :: Char -> Parser Char
157 char !c = satisfy (== c)
160 string :: String -> Parser String
162 = let bs = B.pack str
167 let (bs', rest) = B.splitAt len $ pstInput st
168 st' = st { pstInput = rest }
169 if B.length bs' < len then
181 -- |This is the backtracking alternation. There is no non-backtracking
183 (<|>) :: Parser a -> Parser a -> Parser a
185 = Parser $! do saved <- get -- 状態を保存
186 result <- runParser f
188 Success a -> return $! Success a
189 IllegalInput -> do put saved -- 状態を復歸
191 ReachedEOF -> if pstIsEOFFatal saved then
199 choice :: [Parser a] -> Parser a
200 choice = foldl (<|>) failP
203 oneOf :: [Char] -> Parser Char
204 oneOf = foldl (<|>) failP . map char
207 notFollowedBy :: Parser a -> Parser ()
209 = Parser $! do saved <- get -- 状態を保存
210 result <- runParser p
212 Success _ -> do put saved -- 状態を復歸
214 IllegalInput -> do put saved -- 状態を復歸
216 ReachedEOF -> do put saved -- 状態を復歸
221 digit = do c <- anyChar
222 if c >= '0' && c <= '9' then
228 hexDigit :: Parser Char
229 hexDigit = do c <- anyChar
230 if (c >= '0' && c <= '9') ||
231 (c >= 'a' && c <= 'f') ||
232 (c >= 'A' && c <= 'F') then
238 many :: forall a. Parser a -> Parser [a]
241 let (# result, state' #) = many' state Seq.empty
245 many' :: ParserState -> Seq a -> (# ParserResult [a], ParserState #)
247 = case runState (runParser p) st of
248 (Success a, st') -> many' st' (soFar |> a)
249 (IllegalInput, _) -> (# Success (Fold.toList soFar), st #)
250 (ReachedEOF , _) -> if pstIsEOFFatal st then
253 (# Success (Fold.toList soFar), st #)
255 manyChar :: Parser Char -> Parser Lazy.ByteString
256 manyChar !p = Parser $!
258 case scan' state 0 of
260 -> do let (bs, rest) = B.splitAt len (pstInput state)
261 state' = state { pstInput = rest }
265 -> if pstIsEOFFatal state then
268 error "internal error"
269 _ -> error "internal error"
271 scan' :: ParserState -> Int64 -> ParserResult Int64
273 = case runState (runParser p) st of
274 (Success _ , st') -> scan' st' (soFar + 1)
275 (IllegalInput, _ ) -> Success soFar
276 (ReachedEOF , _ ) -> if pstIsEOFFatal st then
282 many1 :: Parser a -> Parser [a]
288 count :: Int -> Parser a -> Parser [a]
289 count !n !p = Parser $! count' n p Seq.empty
291 -- This implementation is rather ugly but we need to make it
292 -- tail-recursive to avoid stack overflow.
293 count' :: Int -> Parser a -> Seq a -> State ParserState (ParserResult [a])
294 count' 0 _ !soFar = return $! Success $! Fold.toList soFar
295 count' !n !p !soFar = do saved <- get
296 result <- runParser p
298 Success a -> count' (n-1) p (soFar |> a)
299 IllegalInput -> do put saved
301 ReachedEOF -> do put saved
306 option :: a -> Parser a -> Parser a
307 option def !p = p <|> return def
310 sepBy :: Parser a -> Parser sep -> Parser [a]
311 sepBy !p !sep = sepBy1 p sep <|> return []
314 sepBy1 :: Parser a -> Parser sep -> Parser [a]
317 xs <- many $! sep >> p
329 crlf :: Parser String
330 crlf = string "\x0d\x0a"