parsec 3.1.5 → 3.1.6
raw patch · 13 files changed
+248/−88 lines, 13 filesdep +HUnitdep +parsecdep +test-frameworkdep ~basedep ~bytestringdep ~mtl
Dependencies added: HUnit, parsec, test-framework, test-framework-hunit
Dependency ranges changed: base, bytestring, mtl, text
Files
- CHANGES +9/−0
- Text/Parsec.hs +102/−19
- Text/Parsec/ByteString.hs +1/−7
- Text/Parsec/ByteString/Lazy.hs +1/−7
- Text/Parsec/Char.hs +17/−1
- Text/Parsec/Prim.hs +56/−19
- Text/Parsec/String.hs +0/−8
- Text/Parsec/Text.hs +1/−8
- Text/Parsec/Text/Lazy.hs +1/−8
- Text/Parsec/Token.hs +1/−1
- parsec.cabal +20/−10
- test/Main.hs +10/−0
- test/Tokens.hs +29/−0
CHANGES view
@@ -1,3 +1,12 @@+3.1.6++- Export 'Text' instances from Text.Parsec+- Make Text.Parsec exports more visible+- Re-arrange Text.Parsec exports+- Add functions 'crlf' and 'endOfLine' to Text.Parsec.Char for handling+ input streams that do not have normalized line terminators.+- Fix off-by-one error in Token.charControl+ 3.1.4 & 3.1.5 - Bump dependency on 'text'
Text/Parsec.hs view
@@ -1,29 +1,115 @@--------------------------------------------------------------------------------- |--- Module : Text.Parsec--- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007--- License : BSD-style (see the LICENSE file)--- --- Maintainer : derek.a.elkins@gmail.com--- Stability : provisional--- Portability : portable--- ------------------------------------------------------------------------------+{-|+Module : Text.Parsec+Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007+License : BSD-style (see the LICENSE file) +Maintainer : aslatter@gmail.com+Stability : provisional+Portability : portable++This module includes everything you need to get started writing a+parser.++By default this module is set up to parse character data. If you'd like+to parse the result of your own tokenizer you should start with the following+imports:++@+ import Text.Parsec.Prim+ import Text.Parsec.Combinator+@++Then you can implement your own version of 'satisfy' on top of the 'tokenPrim'+primitive.++-}+ module Text.Parsec- ( module Text.Parsec.Prim+ ( -- * Parsers+ ParsecT+ , Parsec+ , token+ , tokens+ , runParserT+ , runParser+ , parse+ , parseTest+ , getPosition+ , getInput+ , getState+ , putState+ , modifyState+ -- * Combinators+ , (<|>)+ , (<?>)+ , label+ , labels+ , try+ , unexpected+ , choice+ , many+ , many1+ , skipMany+ , skipMany1+ , count+ , between+ , option+ , optionMaybe+ , optional+ , sepBy+ , sepBy1+ , endBy+ , endBy1+ , sepEndBy+ , sepEndBy1+ , chainl+ , chainl1+ , chainr+ , chainr1+ , eof+ , notFollowedBy+ , manyTill+ , lookAhead+ , anyToken+ -- * Character Parsing , module Text.Parsec.Char- , module Text.Parsec.Combinator- , module Text.Parsec.String- , module Text.Parsec.ByteString- , module Text.Parsec.ByteString.Lazy+ -- * Error messages , ParseError , errorPos+ -- * Position , SourcePos , SourceName, Line, Column , sourceName, sourceLine, sourceColumn , incSourceLine, incSourceColumn , setSourceLine, setSourceColumn, setSourceName+ -- * Low-level operations+ , manyAccum+ , tokenPrim+ , tokenPrimEx+ , runPT+ , unknownError+ , sysUnExpectError+ , mergeErrorReply+ , getParserState+ , setParserState+ , updateParserState+ , Stream+ , runParsecT+ , mkPT+ , Consumed+ , Reply+ , State+ , setPosition+ , setInput+ -- * Other stuff+ , setState+ , updateState+ , parsecMap+ , parserReturn+ , parserBind+ , parserFail+ , parserZero+ , parserPlus ) where import Text.Parsec.Pos@@ -31,6 +117,3 @@ import Text.Parsec.Prim import Text.Parsec.Char import Text.Parsec.Combinator-import Text.Parsec.String hiding ( Parser, GenParser, parseFromFile )-import Text.Parsec.ByteString hiding ( Parser, GenParser, parseFromFile )-import Text.Parsec.ByteString.Lazy hiding ( Parser, GenParser, parseFromFile )
Text/Parsec/ByteString.hs view
@@ -8,13 +8,10 @@ -- Stability : provisional -- Portability : portable -- --- Make strict ByteStrings an instance of 'Stream' with 'Char' token type.+-- Convinience definitions for working with 'C.ByteString's. -- ----------------------------------------------------------------------------- -{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}- module Text.Parsec.ByteString ( Parser, GenParser, parseFromFile ) where@@ -23,9 +20,6 @@ import Text.Parsec.Prim import qualified Data.ByteString.Char8 as C--instance (Monad m) => Stream C.ByteString m Char where- uncons = return . C.uncons type Parser = Parsec C.ByteString () type GenParser t st = Parsec C.ByteString st
Text/Parsec/ByteString/Lazy.hs view
@@ -8,13 +8,10 @@ -- Stability : provisional -- Portability : portable ----- Make lazy ByteStrings an instance of 'Stream' with 'Char' token type.+-- Convinience definitions for working with lazy 'C.ByteString's. -- ----------------------------------------------------------------------------- -{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}- module Text.Parsec.ByteString.Lazy ( Parser, GenParser, parseFromFile ) where@@ -23,9 +20,6 @@ import Text.Parsec.Prim import qualified Data.ByteString.Lazy.Char8 as C--instance (Monad m) => Stream C.ByteString m Char where- uncons = return . C.uncons type Parser = Parsec C.ByteString () type GenParser t st = Parsec C.ByteString st
Text/Parsec/Char.hs view
@@ -19,6 +19,7 @@ import Data.Char import Text.Parsec.Pos import Text.Parsec.Prim+import Control.Applicative ((*>)) -- | @oneOf cs@ succeeds if the current character is in the supplied -- list of characters @cs@. Returns the parsed character. See also@@ -52,7 +53,22 @@ -- | Parses a newline character (\'\\n\'). Returns a newline character. newline :: (Stream s m Char) => ParsecT s u m Char-newline = char '\n' <?> "new-line"+newline = char '\n' <?> "lf new-line"++-- | Parses a carriage return character (\'\\r\') followed by a newline character (\'\\n\').+-- Returns a newline character. ++crlf :: (Stream s m Char) => ParsecT s u m Char+crlf = char '\r' *> char '\n' <?> "crlf new-line"++-- | Parses a CRLF (see 'crlf') or LF (see 'newline') end-of-line.+-- Returns a newline character (\'\\n\').+--+-- > endOfLine = newline <|> crlf+--++endOfLine :: (Stream s m Char) => ParsecT s u m Char+endOfLine = newline <|> crlf <?> "new-line" -- | Parses a tab character (\'\\t\'). Returns a tab character.
Text/Parsec/Prim.hs view
@@ -13,7 +13,8 @@ ----------------------------------------------------------------------------- {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleContexts,- UndecidableInstances #-}+ UndecidableInstances, ScopedTypeVariables #-}+{-# OPTIONS_HADDOCK not-home #-} module Text.Parsec.Prim ( unknownError@@ -67,6 +68,13 @@ , updateState ) where ++import qualified Data.ByteString.Lazy.Char8 as CL+import qualified Data.ByteString.Char8 as C++import qualified Data.Text as Text+import qualified Data.Text.Lazy as TextL+ import qualified Control.Applicative as Applicative ( Applicative(..), Alternative(..) ) import Control.Monad() import Control.Monad.Trans@@ -299,7 +307,7 @@ infix 0 <?> infixr 1 <|> --- | The parser @p <?> msg@ behaves as parser @p@, but whenever the+-- | The parser @p \<?> msg@ behaves as parser @p@, but whenever the -- parser @p@ fails /without consuming any input/, it replaces expect -- error messages with the expect error message @msg@. --@@ -329,6 +337,7 @@ (<|>) :: (ParsecT s u m a) -> (ParsecT s u m a) -> (ParsecT s u m a) p1 <|> p2 = mplus p1 p2 +-- | A synonym for @<?>@, but as a function instead of an operator. label :: ParsecT s u m a -> String -> ParsecT s u m a label p msg = labels p [msg]@@ -363,7 +372,29 @@ class (Monad m) => Stream s m t | s -> t where uncons :: s -> m (Maybe (t,s)) -tokens :: (Stream s m t, Eq t)+instance (Monad m) => Stream [tok] m tok where+ uncons [] = return $ Nothing+ uncons (t:ts) = return $ Just (t,ts)+ {-# INLINE uncons #-}+++instance (Monad m) => Stream CL.ByteString m Char where+ uncons = return . CL.uncons++instance (Monad m) => Stream C.ByteString m Char where+ uncons = return . C.uncons++instance (Monad m) => Stream Text.Text m Char where+ uncons = return . Text.uncons+ {-# INLINE uncons #-}++instance (Monad m) => Stream TextL.Text m Char where+ uncons = return . TextL.uncons+ {-# INLINE uncons #-}+++tokens :: forall u s m t .+ (Stream s m t, Eq t) => ([t] -> String) -- Pretty print a list of tokens -> (SourcePos -> [t] -> SourcePos) -> [t] -- List of tokens to parse@@ -373,32 +404,38 @@ = ParsecT $ \s _ _ eok _ -> eok [] s $ unknownError s tokens showTokens nextposs tts@(tok:toks)- = ParsecT $ \(State input pos u) cok cerr eok eerr -> + = ParsecT $ \(State input pos0 u) cok cerr eok eerr -> let- errEof = (setErrorMessage (Expect (showTokens tts))- (newErrorMessage (SysUnExpect "") pos))+ nextpos :: SourcePos -> t -> SourcePos+ nextpos pos t = nextposs pos [t] - errExpect x = (setErrorMessage (Expect (showTokens tts))- (newErrorMessage (SysUnExpect (showTokens [x])) pos))+ errEof pos ts =+ (setErrorMessage (Expect (showTokens ts))+ (newErrorMessage (SysUnExpect "") pos)) - walk [] rs = ok rs- walk (t:ts) rs = do+ errExpect pos ts x =+ (setErrorMessage (Expect (showTokens ts))+ (newErrorMessage (SysUnExpect (showTokens [x])) pos))++ -- 'pos' is the position of the first token in the stream+ walk [] pos rs = ok pos rs+ walk allTs@(t:ts) pos rs = do sr <- uncons rs case sr of- Nothing -> cerr $ errEof- Just (x,xs) | t == x -> walk ts xs- | otherwise -> cerr $ errExpect x+ Nothing -> cerr $ errEof pos allTs+ Just (x,xs) | t == x -> walk ts (nextpos pos x) xs+ | otherwise -> cerr $ errExpect pos allTs x - ok rs = let pos' = nextposs pos tts- s' = State rs pos' u- in cok tts s' (newErrorUnknown pos')+ ok pos rs =+ let s' = State rs pos u+ in cok tts s' (newErrorUnknown pos) in do sr <- uncons input case sr of- Nothing -> eerr $ errEof+ Nothing -> eerr $ errEof pos0 tts Just (x,xs)- | tok == x -> walk toks xs- | otherwise -> eerr $ errExpect x+ | tok == x -> walk toks (nextpos pos0 x) xs+ | otherwise -> eerr $ errExpect pos0 tts x -- | The parser @try p@ behaves like parser @p@, except that it -- pretends that it hasn't consumed any input when an error occurs.
Text/Parsec/String.hs view
@@ -12,20 +12,12 @@ -- ----------------------------------------------------------------------------- -{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}- module Text.Parsec.String ( Parser, GenParser, parseFromFile ) where import Text.Parsec.Error import Text.Parsec.Prim--instance (Monad m) => Stream [tok] m tok where- uncons [] = return $ Nothing- uncons (t:ts) = return $ Just (t,ts)- {-# INLINE uncons #-} type Parser = Parsec String () type GenParser tok st = Parsec [tok] st
Text/Parsec/Text.hs view
@@ -8,13 +8,10 @@ -- Stability : provisional -- Portability : portable -- --- Make Text an instance of 'Stream' with 'Char' token type.+-- Convinience definitions for working with 'Text.Text'. -- ----------------------------------------------------------------------------- -{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}- module Text.Parsec.Text ( Parser, GenParser ) where@@ -22,10 +19,6 @@ import qualified Data.Text as Text import Text.Parsec.Error import Text.Parsec.Prim--instance (Monad m) => Stream Text.Text m Char where- uncons = return . Text.uncons- {-# INLINE uncons #-} type Parser = Parsec Text.Text () type GenParser st = Parsec Text.Text st
Text/Parsec/Text/Lazy.hs view
@@ -8,13 +8,10 @@ -- Stability : provisional -- Portability : portable -- --- Make Text an instance of 'Stream' with 'Char' token type.+-- Convinience definitions for working with lazy 'Text.Text'. -- ----------------------------------------------------------------------------- -{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}- module Text.Parsec.Text.Lazy ( Parser, GenParser ) where@@ -22,10 +19,6 @@ import qualified Data.Text.Lazy as Text import Text.Parsec.Error import Text.Parsec.Prim--instance (Monad m) => Stream Text.Text m Char where- uncons = return . Text.uncons- {-# INLINE uncons #-} type Parser = Parsec Text.Text () type GenParser st = Parsec Text.Text st
Text/Parsec/Token.hs view
@@ -448,7 +448,7 @@ charControl = do{ char '^' ; code <- upper- ; return (toEnum (fromEnum code - fromEnum 'A'))+ ; return (toEnum (fromEnum code - fromEnum 'A' + 1)) } charNum = do{ code <- decimal
parsec.cabal view
@@ -1,11 +1,12 @@ name: parsec-version: 3.1.5-cabal-version: >= 1.6+version: 3.1.6+cabal-version: >= 1.8 license: BSD3 license-file: LICENSE author: Daan Leijen <daan@microsoft.com>, Paolo Martini <paolo@nemail.it> maintainer: Antoine Latter <aslatter@gmail.com> homepage: http://www.cs.uu.nl/~daan/parsec.html+bug-reports: https://github.com/aslatter/parsec/issues category: Parsing synopsis: Monadic parser combinators build-type: Simple@@ -19,13 +20,8 @@ extra-source-files: CHANGES source-repository head- type: darcs- location: http://code.haskell.org/parsec3--source-repository this- type: darcs- location: http://code.haskell.org/parsec3- tag: 3.1.5+ type: git+ location: https://github.com/aslatter/parsec flag base4 Description: Use base-4.*@@ -63,6 +59,20 @@ else build-depends: base >= 3.0.3 && < 4 cpp-options: -DBASE3- build-depends: mtl, bytestring, text >= 0.2 && < 1.2++ build-depends: mtl, bytestring, text >= 0.2 && < 1.3 extensions: ExistentialQuantification, PolymorphicComponents, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, DeriveDataTypeable, CPP ghc-options: -O2++Test-Suite tests+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Main.hs+ other-modules:+ Tokens+ build-depends:+ base,+ parsec,+ HUnit == 1.2.*,+ test-framework >= 0.6 && < 0.9,+ test-framework-hunit >= 0.2 && < 0.4
+ test/Main.hs view
@@ -0,0 +1,10 @@++import Test.Framework++import Tokens ( tokensTests )++main :: IO ()+main = do+ defaultMain+ [ testGroup "Text.Parsec.Tokens" tokensTests+ ]
+ test/Tokens.hs view
@@ -0,0 +1,29 @@++module Tokens+ ( tokensTests+ ) where++import Test.HUnit hiding ( Test )+import Test.Framework+import Test.Framework.Providers.HUnit++import Text.Parsec+import Text.Parsec.String+import qualified Text.Parsec.Token as P+import Text.Parsec.Language (haskellDef)++tokensTests :: [Test]+tokensTests =+ return $+ testCase "Control Char Parsing" $+ parseString "\"test\\^Bstring\"" @?= "test\^Bstring"++ where+ parseString :: String -> String+ parseString input =+ case parse parser "Example" input of+ Left{} -> error "Parse failure"+ Right str -> str++ parser :: Parser String+ parser = P.stringLiteral $ P.makeTokenParser haskellDef