Bryan O'Sullivan avatar Bryan O'Sullivan committed ce7088c

Update names, fix some build problems.

Comments (0)

Files changed (1)

ByteStringParser.hs

 -----------------------------------------------------------------------------
 -- |
--- Module      :  ByteStringParser
+-- Module      :  Text.ParserCombinators.ByteStringParser
 -- Copyright   :  (c) Daan Leijen 1999-2001, Jeremy Shaw 2006, Bryan O'Sullivan 2007
 -- License     :  BSD-style (see the file libraries/parsec/LICENSE)
 -- 
--- Maintainer  :  jeremy@n-heptane.com
+-- Maintainer  :  bos@serpentine.com
 -- Stability   :  experimental
 -- Portability :  unknown
 --
 import Data.Char
 import Data.Word
 import Control.Monad
-import qualified Data.ByteString.Char8 as C
+import qualified Data.ByteString.Lazy.Char8 as C
 
 -- * Parser
 
 
 newtype Parser state a = Parser { unParser :: (state -> Either (state,[String]) (a, state)) }
 
+type CharParser = Parser C.ByteString Char
+
 instance Functor (Parser state) where
     fmap f (Parser p) =
         Parser $ \st ->
                )
 
 -- |Always succeed
-pSucceed :: a -> Parser state a
-pSucceed = return
+succeed :: a -> Parser state a
+succeed = return
 
 -- |Always fail
-pFail :: Parser state a
-pFail = Parser (\st ->  Left (st, []))
+fail :: Parser state a
+fail = Parser (\st ->  Left (st, []))
 
 infix 0 <?>
 infixr 1 <|>
 
 -- * Things like in @Parsec.Char@
 
+{-# INLINE satisfy #-}
+
 -- |character parser
-satisfy :: (Char -> Bool) -> Parser C.ByteString Char
+satisfy :: (Char -> Bool) -> CharParser
+
 satisfy f =
     Parser $ \bs ->
         if C.null bs
                 then Right (s,ss)
                 else Left (bs, [])
 
+letter :: CharParser
+
+letter = satisfy isLetter
+
+digit :: CharParser
+
+digit = satisfy isDigit
+
+anyChar :: CharParser
+
+anyChar = satisfy $ const True
+
+space :: CharParser
+
+space = satisfy isSpace
+
 -- |satisfy a specific character
-pChar :: Char -> Parser C.ByteString Char
-pChar c = satisfy (== c) <?> [c]
 
+char :: Char -> CharParser
+
+char c = satisfy (== c) <?> [c]
+
+string :: String -> Parser C.ByteString String
+
+string = mapM char
+
+count :: Int -> Parser st a -> Parser st [a]
+
+count n p = sequence (replicate n p)
 
 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
 
+try :: Parser st a -> Parser st a
+
+try (Parser p)
+    = Parser $ \state -> case p state of
+                          (Left (_, msgs)) -> Left (state, msgs)
+                          ok -> ok
+
 -- |detect 'end of file'
-pEOF :: Parser C.ByteString ()
-pEOF =
+eOF :: Parser C.ByteString ()
+eOF =
     Parser $ \bs -> if C.null bs then Right ((),bs) else (Left (bs, ["EOF"]))
 
--- |pTakeWhile take characters while the predicate is true
-pTakeWhile :: (Char -> Bool) -> Parser C.ByteString C.ByteString
-pTakeWhile f =
+-- |takeWhile take characters while the predicate is true
+takeWhile :: (Char -> Bool) -> Parser C.ByteString C.ByteString
+takeWhile f =
     Parser $ \bs -> Right (C.span f bs)
 
--- |pSkipWhile skip over characters while the predicate is true
-pSkipWhile :: (Char -> Bool) -> Parser C.ByteString ()
-pSkipWhile p =
+-- |skipWhile skip over characters while the predicate is true
+skipWhile :: (Char -> Bool) -> Parser C.ByteString ()
+skipWhile p =
     Parser $ \bs -> Right ((), C.dropWhile p bs)
 
--- |'pMany' - take zero or more instances of the parser
-pMany ::  Parser st a -> Parser st [a]
-pMany p 
-    = scan id
-    where
-      scan f = do x <- p
-                  scan (\tail -> f (x:tail))
-               <|> return (f [])
+-- |'many' - take zero or more instances of the parser
+many ::  Parser st a -> Parser st [a]
+many p = scan id
+    where scan f = do x <- p
+                      scan (\xs -> f (x:xs))
+                 <|> return (f [])
 
--- |'pMany1' - take one or more instances of the parser
-pMany1 :: Parser st a -> Parser st [a]
-pMany1 p =
+-- |'many1' - take one or more instances of the parser
+many1 :: Parser st a -> Parser st [a]
+many1 p =
     do x <- p
-       xs <- pMany p
+       xs <- many p
        return (x:xs)
 
--- |'pSkipMany' - skip zero or many instances of the parser
-pSkipMany :: Parser st a -> Parser st ()
-pSkipMany p = scan
+manyTill :: Parser st a -> Parser st end -> Parser st [a]
+manyTill p end = scan
+    where scan = do end
+                    return []
+               <|>
+                 do x <- p
+                    xs <- scan
+                    return (x:xs)
+
+-- |'skipMany' - skip zero or many instances of the parser
+skipMany :: Parser st a -> Parser st ()
+skipMany p = scan
     where
       scan = (p >> scan) <|> return ()
 
--- |'pSkipMany1' - skip one or many instances of the parser       
-pSkipMany1 :: Parser st a -> Parser st ()
-pSkipMany1 p = p >> pSkipMany p
+-- |'skipMany1' - skip one or many instances of the parser       
+skipMany1 :: Parser st a -> Parser st ()
+skipMany1 p = p >> skipMany p
 
 -- |'notEmpty' - tests that a parser returned a non-null ByteString
 notEmpty :: Parser C.ByteString C.ByteString -> Parser C.ByteString C.ByteString 
 notEmpty (Parser p) =
     Parser $ \s -> case p s of
-                     o@(Right (a, s)) ->
+                     o@(Right (a, s_)) ->
                          if C.null a
                          then Left (a, ["notEmpty"])
                          else o
                      x -> x
 
 -- | parse some input with the given parser and return that input without copying it
-pMatch :: Parser C.ByteString a -> Parser C.ByteString C.ByteString
-pMatch p = do start <- getInput
-              p
-              end <- getInput
-              return (C.take (C.length start - C.length end) start)
+match :: Parser C.ByteString a -> Parser C.ByteString C.ByteString
+match p = do start <- getInput
+             p
+             end <- getInput
+             return (C.take (C.length start - C.length end) start)
 
+lookAhead :: Parser C.ByteString a -> Parser C.ByteString a
+
+lookAhead (Parser p)
+    = Parser $ \state -> case p state of
+                          Left (_, msgs) -> Left (state, msgs)
+                          Right (m, _) -> Right (m, state)
 
 -- * Running parsers
 
       showError [msg] = "Parser error, expected:\n" ++ msg ++ "\n"
       showError msgs = "Parser error, expected one of:\n" ++ unlines msgs
 
+parseTest :: Parser st a -> st -> IO ()
+
 parseTest p s =
     case parse p s of
       Left (st, msg) -> putStrLn $ msg ++ "\nGot:\n" ++ show st
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.