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)
57 import qualified Data.Foldable as Fold
58 import qualified Data.Sequence as Seq
59 import Data.Sequence (Seq, (|>))
61 -- |@'Parser' a@ is obviously a parser which parses and returns @a@.
62 newtype Parser a = Parser {
63 runParser :: State ParserState (ParserResult a)
69 pstInput :: Lazy.ByteString
70 , pstIsEOFFatal :: !Bool
75 data ParserResult a = Success !a
76 | IllegalInput -- 受理出來ない入力があった
77 | ReachedEOF -- 限界を越えて讀まうとした
81 -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
82 instance Monad Parser where
83 p >>= f = Parser $! do saved <- get -- 失敗した時の爲に状態を保存
86 Success a -> runParser (f a)
87 IllegalInput -> do put saved -- 状態を復歸
89 ReachedEOF -> do put saved -- 状態を復歸
91 return x = x `seq` Parser $! return $! Success x
92 fail _ = Parser $! return $! IllegalInput
94 -- |@'failP'@ is just a synonym for @'Prelude.fail'
95 -- 'Prelude.undefined'@.
97 failP = fail undefined
99 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(# result,
101 parse :: Parser a -> Lazy.ByteString -> (# ParserResult a, Lazy.ByteString #)
102 parse p input -- input は lazy である必要有り。
104 let (result, state') = runState (runParser p) (PST input True)
106 result `seq` (# result, pstInput state' #) -- pstInput state' も lazy である必要有り。
108 -- |@'parseStr' p str@ packs @str@ and parses it.
109 parseStr :: Parser a -> String -> (# ParserResult a, Lazy.ByteString #)
111 = p `seq` -- 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
161 string !str = str `seq`
168 -- |This is the backtracking alternation. There is no non-backtracking
170 (<|>) :: Parser a -> Parser a -> Parser a
173 Parser $! do saved <- get -- 状態を保存
174 result <- runParser f
176 Success a -> return $! Success a
177 IllegalInput -> do put saved -- 状態を復歸
179 ReachedEOF -> if pstIsEOFFatal saved then
187 choice :: [Parser a] -> Parser a
188 choice = foldl (<|>) failP
191 oneOf :: [Char] -> Parser Char
192 oneOf = foldl (<|>) failP . map char
195 notFollowedBy :: Parser a -> Parser ()
197 = Parser $! do saved <- get -- 状態を保存
198 result <- runParser p
200 Success _ -> do put saved -- 状態を復歸
202 IllegalInput -> do put saved -- 状態を復歸
204 ReachedEOF -> do put saved -- 状態を復歸
209 digit = do c <- anyChar
210 if c >= '0' && c <= '9' then
216 hexDigit :: Parser Char
217 hexDigit = do c <- anyChar
218 if (c >= '0' && c <= '9') ||
219 (c >= 'a' && c <= 'f') ||
220 (c >= 'A' && c <= 'F') then
226 many :: forall a. Parser a -> Parser [a]
229 let (# result, state' #) = many' state Seq.empty
233 many' :: ParserState -> Seq a -> (# ParserResult [a], ParserState #)
235 = case runState (runParser p) st of
236 (Success a, st') -> many' st' (soFar |> a)
237 (IllegalInput, _) -> (# Success (Fold.toList soFar), st #)
238 (ReachedEOF , _) -> if pstIsEOFFatal st then
241 (# Success (Fold.toList soFar), st #)
244 many1 :: Parser a -> Parser [a]
250 count :: Int -> Parser a -> Parser [a]
251 count !n !p = Parser $! count' n p Seq.empty
253 -- This implementation is rather ugly but we need to make it
254 -- tail-recursive to avoid stack overflow.
255 count' :: Int -> Parser a -> Seq a -> State ParserState (ParserResult [a])
256 count' 0 _ !soFar = return $! Success $! Fold.toList soFar
257 count' !n !p !soFar = do saved <- get
258 result <- runParser p
260 Success a -> count' (n-1) p (soFar |> a)
261 IllegalInput -> do put saved
263 ReachedEOF -> do put saved
268 option :: a -> Parser a -> Parser a
269 option def p = p `seq`
273 sepBy :: Parser a -> Parser sep -> Parser [a]
274 sepBy p sep = p `seq` sep `seq`
275 sepBy1 p sep <|> return []
278 sepBy1 :: Parser a -> Parser sep -> Parser [a]
279 sepBy1 p sep = p `seq` sep `seq`
281 xs <- many $! sep >> p
293 crlf :: Parser String
294 crlf = string "\x0d\x0a"