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
53 import Control.Monad.State.Strict
54 import Data.ByteString.Base (LazyByteString)
55 import Data.ByteString.Lazy ()
56 import qualified Data.ByteString.Lazy.Char8 as B
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 :: LazyByteString
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 -> LazyByteString -> (ParserResult a, LazyByteString)
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, LazyByteString)
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
183 oneOf :: [Char] -> Parser Char
184 oneOf = foldl (<|>) failP . map char
187 notFollowedBy :: Parser a -> Parser ()
188 notFollowedBy p = p `seq`
189 (p >> failP) <|> return ()
193 digit = do c <- anyChar
194 if c >= '0' && c <= '9' then
200 hexDigit :: Parser Char
201 hexDigit = do c <- anyChar
202 if (c >= '0' && c <= '9') ||
203 (c >= 'a' && c <= 'f') ||
204 (c >= 'A' && c <= 'F') then
210 many :: Parser a -> Parser [a]
219 many1 :: Parser a -> Parser [a]
226 count :: Int -> Parser a -> Parser [a]
227 count 0 _ = return []
228 count n p = n `seq` p `seq`
234 option :: a -> Parser a -> Parser a
235 option def p = p `seq`
239 sepBy :: Parser a -> Parser sep -> Parser [a]
240 sepBy p sep = p `seq` sep `seq`
241 sepBy1 p sep <|> return []
244 sepBy1 :: Parser a -> Parser sep -> Parser [a]
245 sepBy1 p sep = p `seq` sep `seq`
247 xs <- many $! sep >> p
259 crlf :: Parser String
260 crlf = string "\x0d\x0a"