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
52 import Control.Monad.State
53 import qualified Data.ByteString.Lazy.Char8 as B
54 import Data.ByteString.Lazy.Char8 (ByteString)
56 -- |@Parser a@ is obviously a parser which parses and returns @a@.
57 newtype Parser a = Parser {
58 runParser :: State ParserState (ParserResult a)
61 type ParserState = (ByteString, IsEOFFatal)
63 type IsEOFFatal = Bool
65 data ParserResult a = Success !a
66 | IllegalInput -- 受理出來ない入力があった
67 | ReachedEOF -- 限界を越えて讀まうとした
71 -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
72 instance Monad Parser where
73 p >>= f = Parser $! do saved@(_, isEOFFatal) <- get -- 失敗した時の爲に状態を保存
76 Success a -> a `seq` runParser (f a)
77 IllegalInput -> do put saved -- 状態を復歸
79 ReachedEOF -> do unless isEOFFatal
82 return x = x `seq` Parser $! return $! Success x
83 fail _ = Parser $! return $! IllegalInput
85 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(result,
87 parse :: Parser a -> ByteString -> (ParserResult a, ByteString)
88 parse p input -- input は lazy である必要有り。
90 let (result, (input', _)) = runState (runParser p) (input, True)
92 result `seq` (result, input') -- input' も lazy である必要有り。
94 -- |@'parseStr' p str@ packs @str@ and parses it.
95 parseStr :: Parser a -> String -> (ParserResult a, ByteString)
97 = p `seq` -- input は lazy である必要有り。
98 parse p $! B.pack input
101 anyChar :: Parser Char
103 do (input, isEOFFatal) <- get
107 do let c = B.head input
108 put (B.tail input, isEOFFatal)
120 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
121 allowEOF :: Parser a -> Parser a
123 Parser $! do (input, isEOFFatal) <- get
126 result <- runParser f
129 put (input', isEOFFatal)
134 satisfy :: (Char -> Bool) -> Parser Char
137 unless (f c) (fail "")
141 char :: Char -> Parser Char
142 char c = c `seq` satisfy (== c)
145 string :: String -> Parser String
146 string str = str `seq`
153 -- |This is the backtracking alternation. There is no non-backtracking
155 (<|>) :: Parser a -> Parser a -> Parser a
158 Parser $! do saved@(_, isEOFFatal) <- get -- 状態を保存
159 result <- runParser f
161 Success a -> return $ Success a
162 IllegalInput -> do put saved -- 状態を復歸
164 ReachedEOF -> if isEOFFatal then
171 oneOf :: [Char] -> Parser Char
172 oneOf = foldl (<|>) (fail "") . map char
175 notFollowedBy :: Parser a -> Parser ()
176 notFollowedBy p = p `seq`
177 p >>= fail "" <|> return ()
181 digit = do c <- anyChar
182 if c >= '0' && c <= '9' then
188 hexDigit :: Parser Char
189 hexDigit = do c <- anyChar
190 if (c >= '0' && c <= '9') ||
191 (c >= 'a' && c <= 'f') ||
192 (c >= 'A' && c <= 'F') then
198 many :: Parser a -> Parser [a]
207 many1 :: Parser a -> Parser [a]
215 manyTill :: Parser a -> Parser end -> Parser [a]
223 many1Till :: Parser a -> Parser end -> Parser [a]
231 count :: Int -> Parser a -> Parser [a]
232 count 0 _ = return []
233 count n p = n `seq` p `seq`
239 option :: a -> Parser a -> Parser a
240 option def p = p `seq`
244 sepBy :: Parser a -> Parser sep -> Parser [a]
245 sepBy p sep = p `seq` sep `seq`
246 sepBy1 p sep <|> return []
249 sepBy1 :: Parser a -> Parser sep -> Parser [a]
250 sepBy1 p sep = p `seq` sep `seq`
252 xs <- many $! sep >> p
264 crlf :: Parser String
265 crlf = string "\x0d\x0a"