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 qualified Data.ByteString.Lazy as Lazy (ByteString)
55 import qualified Data.ByteString.Lazy.Char8 as B hiding (ByteString)
57 -- |@'Parser' a@ is obviously a parser which parses and returns @a@.
58 newtype Parser a = Parser {
59 runParser :: State ParserState (ParserResult a)
65 pstInput :: Lazy.ByteString
66 , pstIsEOFFatal :: !Bool
71 data ParserResult a = Success !a
72 | IllegalInput -- 受理出來ない入力があった
73 | ReachedEOF -- 限界を越えて讀まうとした
77 -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
78 instance Monad Parser where
79 p >>= f = Parser $! do saved <- get -- 失敗した時の爲に状態を保存
82 Success a -> runParser (f a)
83 IllegalInput -> do put saved -- 状態を復歸
85 ReachedEOF -> do put saved -- 状態を復歸
87 return x = x `seq` Parser $! return $! Success x
88 fail _ = Parser $! return $! IllegalInput
90 -- |@'failP'@ is just a synonym for @'Prelude.fail'
91 -- 'Prelude.undefined'@.
93 failP = fail undefined
95 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(# result,
97 parse :: Parser a -> Lazy.ByteString -> (# ParserResult a, Lazy.ByteString #)
98 parse p input -- input は lazy である必要有り。
100 let (result, state') = runState (runParser p) (PST input True)
102 result `seq` (# result, pstInput state' #) -- pstInput state' も lazy である必要有り。
104 -- |@'parseStr' p str@ packs @str@ and parses it.
105 parseStr :: Parser a -> String -> (# ParserResult a, Lazy.ByteString #)
107 = p `seq` -- input は lazy である必要有り。
108 parse p (B.pack input)
111 anyChar :: Parser Char
113 do state@(PST input _) <- get
117 do put $! state { pstInput = B.tail input }
118 return (Success $! B.head input)
123 do PST input _ <- get
129 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
130 allowEOF :: Parser a -> Parser a
132 Parser $! do saved@(PST _ isEOFFatal) <- get
133 put $! saved { pstIsEOFFatal = False }
135 result <- runParser f
138 put $! state { pstIsEOFFatal = isEOFFatal }
143 satisfy :: (Char -> Bool) -> Parser Char
152 char :: Char -> Parser Char
153 char c = c `seq` satisfy (== c)
156 string :: String -> Parser String
157 string str = str `seq`
164 -- |This is the backtracking alternation. There is no non-backtracking
166 (<|>) :: Parser a -> Parser a -> Parser a
169 Parser $! do saved <- get -- 状態を保存
170 result <- runParser f
172 Success a -> return $! Success a
173 IllegalInput -> do put saved -- 状態を復歸
175 ReachedEOF -> if pstIsEOFFatal saved then
182 oneOf :: [Char] -> Parser Char
183 oneOf = foldl (<|>) failP . map char
186 notFollowedBy :: Parser a -> Parser ()
189 Parser $! do saved <- get -- 状態を保存
190 result <- runParser p
192 Success _ -> do put saved -- 状態を復歸
194 IllegalInput -> do put saved -- 状態を復歸
196 ReachedEOF -> do put saved -- 状態を復歸
201 digit = do c <- anyChar
202 if c >= '0' && c <= '9' then
208 hexDigit :: Parser Char
209 hexDigit = do c <- anyChar
210 if (c >= '0' && c <= '9') ||
211 (c >= 'a' && c <= 'f') ||
212 (c >= 'A' && c <= 'F') then
218 many :: Parser a -> Parser [a]
227 many1 :: Parser a -> Parser [a]
234 count :: Int -> Parser a -> Parser [a]
235 count 0 _ = return []
236 count n p = n `seq` p `seq`
242 option :: a -> Parser a -> Parser a
243 option def p = p `seq`
247 sepBy :: Parser a -> Parser sep -> Parser [a]
248 sepBy p sep = p `seq` sep `seq`
249 sepBy1 p sep <|> return []
252 sepBy1 :: Parser a -> Parser sep -> Parser [a]
253 sepBy1 p sep = p `seq` sep `seq`
255 xs <- many $! sep >> p
267 crlf :: Parser String
268 crlf = string "\x0d\x0a"