1 -- |Yet another parser combinator. This is mostly a subset of Parsec
2 -- but there are some differences:
4 -- * This parser works on ByteString instead of String.
6 -- * Backtracking is the only possible behavior so there is no \"try\"
9 -- * On success, the remaining string is returned as well as the
12 -- * You can choose whether to treat reaching EOF (trying to eat one
13 -- more letter at the end of string) a fatal error or to treat it a
14 -- normal failure. If a fatal error occurs, the entire parsing
15 -- process immediately fails without trying any backtracks. The
16 -- default behavior is to treat EOF fatal.
18 -- In general, you don't have to use this module directly.
19 module Network.HTTP.Lucu.Parser
53 import Control.Monad.State
54 import qualified Data.ByteString.Lazy.Char8 as B
55 import Data.ByteString.Lazy.Char8 (ByteString)
57 -- |@Parser a@ is obviously a parser which parses and returns @a@.
58 newtype Parser a = Parser {
59 runParser :: State ParserState (ParserResult a)
62 type ParserState = (ByteString, IsEOFFatal)
64 type IsEOFFatal = Bool
66 data ParserResult a = Success !a
67 | IllegalInput -- 受理出來ない入力があった
68 | ReachedEOF -- 限界を越えて讀まうとした
72 -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
73 instance Monad Parser where
74 p >>= f = Parser $! do saved@(_, isEOFFatal) <- get -- 失敗した時の爲に状態を保存
77 Success a -> a `seq` runParser (f a)
78 IllegalInput -> do put saved -- 状態を復歸
80 ReachedEOF -> do unless isEOFFatal
83 return x = x `seq` Parser $! return $! Success x
84 fail _ = Parser $! return $! IllegalInput
86 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(result,
88 parse :: Parser a -> ByteString -> (ParserResult a, ByteString)
89 parse p input -- input は lazy である必要有り。
91 let (result, (input', _)) = runState (runParser p) (input, True)
93 result `seq` (result, input') -- input' も lazy である必要有り。
95 -- |@'parseStr' p str@ packs @str@ and parses it.
96 parseStr :: Parser a -> String -> (ParserResult a, ByteString)
98 = p `seq` -- input は lazy である必要有り。
99 parse p $! B.pack input
102 anyChar :: Parser Char
104 do (input, isEOFFatal) <- get
108 do let c = B.head input
109 put (B.tail input, isEOFFatal)
121 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
122 allowEOF :: Parser a -> Parser a
124 Parser $! do (input, isEOFFatal) <- get
127 result <- runParser f
130 put (input', isEOFFatal)
135 satisfy :: (Char -> Bool) -> Parser Char
138 unless (f c) (fail "")
142 char :: Char -> Parser Char
143 char c = c `seq` satisfy (== c)
146 string :: String -> Parser String
147 string str = str `seq`
154 -- |This is the backtracking alternation. There is no non-backtracking
156 (<|>) :: Parser a -> Parser a -> Parser a
159 Parser $! do saved@(_, isEOFFatal) <- get -- 状態を保存
160 result <- runParser f
162 Success a -> return $ Success a
163 IllegalInput -> do put saved -- 状態を復歸
165 ReachedEOF -> if isEOFFatal then
172 oneOf :: [Char] -> Parser Char
173 oneOf = foldl (<|>) (fail "") . map char
176 notFollowedBy :: Parser a -> Parser ()
177 notFollowedBy p = p `seq`
178 p >>= fail "" <|> return ()
182 digit = do c <- anyChar
183 if c >= '0' && c <= '9' then
189 hexDigit :: Parser Char
190 hexDigit = do c <- anyChar
191 if (c >= '0' && c <= '9') ||
192 (c >= 'a' && c <= 'f') ||
193 (c >= 'A' && c <= 'F') then
199 many :: Parser a -> Parser [a]
208 many1 :: Parser a -> Parser [a]
216 manyTill :: Parser a -> Parser end -> Parser [a]
224 many1Till :: Parser a -> Parser end -> Parser [a]
232 count :: Int -> Parser a -> Parser [a]
233 count 0 _ = return []
234 count n p = n `seq` p `seq`
240 option :: a -> Parser a -> Parser a
241 option def p = p `seq`
245 sepBy :: Parser a -> Parser sep -> Parser [a]
246 sepBy p sep = p `seq` sep `seq`
247 sepBy1 p sep <|> return []
250 sepBy1 :: Parser a -> Parser sep -> Parser [a]
251 sepBy1 p sep = p `seq` sep `seq`
253 xs <- many $! sep >> p
265 crlf :: Parser String
266 crlf = string "\x0d\x0a"