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.Strict
53 import qualified Data.ByteString.Lazy.Char8 as B
54 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)
65 pstInput :: 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 Prelude.undefined'@.
92 failP = fail undefined
94 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(result,
96 parse :: Parser a -> ByteString -> (ParserResult a, ByteString)
97 parse p input -- input は lazy である必要有り。
99 let (result, state') = runState (runParser p) (PST input True)
101 result `seq` (result, pstInput state') -- pstInput state' も lazy である必要有り。
103 -- |@'parseStr' p str@ packs @str@ and parses it.
104 parseStr :: Parser a -> String -> (ParserResult a, ByteString)
106 = p `seq` -- input は lazy である必要有り。
107 parse p $! B.pack input
110 anyChar :: Parser Char
112 do state@(PST input _) <- get
116 do put $! state { pstInput = B.tail input }
117 return (Success $! B.head input)
122 do PST input _ <- get
128 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
129 allowEOF :: Parser a -> Parser a
131 Parser $! do saved@(PST _ isEOFFatal) <- get
132 put $! saved { pstIsEOFFatal = False }
134 result <- runParser f
137 put $! state { pstIsEOFFatal = isEOFFatal }
142 satisfy :: (Char -> Bool) -> Parser Char
151 char :: Char -> Parser Char
152 char c = c `seq` satisfy (== c)
155 string :: String -> Parser String
156 string str = str `seq`
163 -- |This is the backtracking alternation. There is no non-backtracking
165 (<|>) :: Parser a -> Parser a -> Parser a
168 Parser $! do saved <- get -- 状態を保存
169 result <- runParser f
171 Success a -> return $! Success a
172 IllegalInput -> do put saved -- 状態を復歸
174 ReachedEOF -> if pstIsEOFFatal saved then
181 oneOf :: [Char] -> Parser Char
182 oneOf = foldl (<|>) failP . map char
185 notFollowedBy :: Parser a -> Parser ()
186 notFollowedBy p = p `seq`
187 (p >> failP) <|> return ()
191 digit = do c <- anyChar
192 if c >= '0' && c <= '9' then
198 hexDigit :: Parser Char
199 hexDigit = do c <- anyChar
200 if (c >= '0' && c <= '9') ||
201 (c >= 'a' && c <= 'f') ||
202 (c >= 'A' && c <= 'F') then
208 many :: Parser a -> Parser [a]
217 many1 :: Parser a -> Parser [a]
224 count :: Int -> Parser a -> Parser [a]
225 count 0 _ = return []
226 count n p = n `seq` p `seq`
232 option :: a -> Parser a -> Parser a
233 option def p = p `seq`
237 sepBy :: Parser a -> Parser sep -> Parser [a]
238 sepBy p sep = p `seq` sep `seq`
239 sepBy1 p sep <|> return []
242 sepBy1 :: Parser a -> Parser sep -> Parser [a]
243 sepBy1 p sep = p `seq` sep `seq`
245 xs <- many $! sep >> p
257 crlf :: Parser String
258 crlf = string "\x0d\x0a"