X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FParser.hs;h=6c66e7f42e3c02c7fc12b63ceab537dfda0774d1;hp=8c591defd4be602e13db5534567637039b0679bb;hb=a2a726f3581933cea2d805b76aca0e93da778994;hpb=e2a6ee839c0ca27b25b32656a5c080e4b464e7c9 diff --git a/Network/HTTP/Lucu/Parser.hs b/Network/HTTP/Lucu/Parser.hs index 8c591de..6c66e7f 100644 --- a/Network/HTTP/Lucu/Parser.hs +++ b/Network/HTTP/Lucu/Parser.hs @@ -54,6 +54,9 @@ module Network.HTTP.Lucu.Parser import Control.Monad.State.Strict import qualified Data.ByteString.Lazy as Lazy (ByteString) import qualified Data.ByteString.Lazy.Char8 as B hiding (ByteString) +import qualified Data.Foldable as Fold +import qualified Data.Sequence as Seq +import Data.Sequence (Seq, (|>)) -- |@'Parser' a@ is obviously a parser which parses and returns @a@. newtype Parser a = Parser { @@ -142,22 +145,22 @@ allowEOF f = f `seq` satisfy :: (Char -> Bool) -> Parser Char -satisfy f = f `seq` - do c <- anyChar - if f $! c then - return c - else - failP +satisfy !f + = do c <- anyChar + if f c then + return c + else + failP char :: Char -> Parser Char -char c = c `seq` satisfy (== c) +char !c = satisfy (== c) string :: String -> Parser String -string str = str `seq` - do mapM_ char str - return str +string !str = str `seq` + do mapM_ char str + return str infixr 0 <|> @@ -190,9 +193,8 @@ oneOf = foldl (<|>) failP . map char notFollowedBy :: Parser a -> Parser () -notFollowedBy p - = p `seq` - Parser $! do saved <- get -- 状態を保存 +notFollowedBy !p + = Parser $! do saved <- get -- 状態を保存 result <- runParser p case result of Success _ -> do put saved -- 状態を復歸 @@ -221,25 +223,22 @@ hexDigit = do c <- anyChar failP -many :: Parser a -> Parser [a] -many !p = Parser $! many' p [] - --- This implementation is rather ugly but we need to make it --- tail-recursive to avoid stack overflow. -many' :: Parser a -> [a] -> State ParserState (ParserResult [a]) -many' !p !soFar - = do saved <- get - result <- runParser p - case result of - Success a -> many' p (a:soFar) - IllegalInput -> do put saved - return $! Success $ reverse soFar - ReachedEOF -> if pstIsEOFFatal saved then - do put saved - return ReachedEOF - else - do put saved - return $! Success $ reverse soFar +many :: forall a. Parser a -> Parser [a] +many !p = Parser $! + do state <- get + let (# result, state' #) = many' state Seq.empty + put state' + return result + where + many' :: ParserState -> Seq a -> (# ParserResult [a], ParserState #) + many' !st !soFar + = case runState (runParser p) st of + (Success a, st') -> many' st' (soFar |> a) + (IllegalInput, _) -> (# Success (Fold.toList soFar), st #) + (ReachedEOF , _) -> if pstIsEOFFatal st then + (# ReachedEOF, st #) + else + (# Success (Fold.toList soFar), st #) many1 :: Parser a -> Parser [a] @@ -249,16 +248,16 @@ many1 !p = do x <- p count :: Int -> Parser a -> Parser [a] -count !n !p = Parser $! count' n p [] +count !n !p = Parser $! count' n p Seq.empty -- This implementation is rather ugly but we need to make it -- tail-recursive to avoid stack overflow. -count' :: Int -> Parser a -> [a] -> State ParserState (ParserResult [a]) -count' 0 _ !soFar = return $! Success $ reverse soFar +count' :: Int -> Parser a -> Seq a -> State ParserState (ParserResult [a]) +count' 0 _ !soFar = return $! Success $! Fold.toList soFar count' !n !p !soFar = do saved <- get result <- runParser p case result of - Success a -> count' (n-1) p (a:soFar) + Success a -> count' (n-1) p (soFar |> a) IllegalInput -> do put saved return IllegalInput ReachedEOF -> do put saved