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 -- |@'failP'@ is just a synonym for @'Prelude.fail Prelude.undefined'@.
87 failP = fail undefined
89 -- |@'parse' p bstr@ parses @bstr@ with @p@ and returns @(result,
91 parse :: Parser a -> ByteString -> (ParserResult a, ByteString)
92 parse p input -- input は lazy である必要有り。
94 let (result, (input', _)) = runState (runParser p) (input, True)
96 result `seq` (result, input') -- input' も lazy である必要有り。
98 -- |@'parseStr' p str@ packs @str@ and parses it.
99 parseStr :: Parser a -> String -> (ParserResult a, ByteString)
101 = p `seq` -- input は lazy である必要有り。
102 parse p $! B.pack input
105 anyChar :: Parser Char
107 do (input, isEOFFatal) <- get
111 do put (B.tail input, isEOFFatal)
112 return (Success $! B.head input)
123 -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure.
124 allowEOF :: Parser a -> Parser a
126 Parser $! do (input, isEOFFatal) <- get
129 result <- runParser f
132 put (input', isEOFFatal)
137 satisfy :: (Char -> Bool) -> Parser Char
146 char :: Char -> Parser Char
147 char c = c `seq` satisfy (== c)
150 string :: String -> Parser String
151 string str = str `seq`
158 -- |This is the backtracking alternation. There is no non-backtracking
160 (<|>) :: Parser a -> Parser a -> Parser a
163 Parser $! do saved@(_, isEOFFatal) <- get -- 状態を保存
164 result <- runParser f
166 Success a -> return $ Success a
167 IllegalInput -> do put saved -- 状態を復歸
169 ReachedEOF -> if isEOFFatal then
176 oneOf :: [Char] -> Parser Char
177 oneOf = foldl (<|>) failP . map char
180 notFollowedBy :: Parser a -> Parser ()
181 notFollowedBy p = p `seq`
182 (p >> failP) <|> return ()
186 digit = do c <- anyChar
187 if c >= '0' && c <= '9' then
193 hexDigit :: Parser Char
194 hexDigit = do c <- anyChar
195 if (c >= '0' && c <= '9') ||
196 (c >= 'a' && c <= 'f') ||
197 (c >= 'A' && c <= 'F') then
203 many :: Parser a -> Parser [a]
212 many1 :: Parser a -> Parser [a]
219 count :: Int -> Parser a -> Parser [a]
220 count 0 _ = return []
221 count n p = n `seq` p `seq`
227 option :: a -> Parser a -> Parser a
228 option def p = p `seq`
232 sepBy :: Parser a -> Parser sep -> Parser [a]
233 sepBy p sep = p `seq` sep `seq`
234 sepBy1 p sep <|> return []
237 sepBy1 :: Parser a -> Parser sep -> Parser [a]
238 sepBy1 p sep = p `seq` sep `seq`
240 xs <- many $! sep >> p
252 crlf :: Parser String
253 crlf = string "\x0d\x0a"