From fd8ecf5d030d4045709f6448a8cf7bb59481afcf Mon Sep 17 00:00:00 2001 From: Antoine Latter <aslatter@gmail.com> Date: Tue, 9 Sep 2014 20:17:49 -0500 Subject: [PATCH] Fix off-by-one error in Token charControl. Fixes #2. --- Text/Parsec/Token.hs | 2 +- parsec.cabal | 15 ++++++++++++++- test/Main.hs | 10 ++++++++++ test/Tokens.hs | 29 +++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/Main.hs create mode 100644 test/Tokens.hs diff --git a/Text/Parsec/Token.hs b/Text/Parsec/Token.hs index 6a6f29b..885f797 100644 --- a/Text/Parsec/Token.hs +++ b/Text/Parsec/Token.hs @@ -448,7 +448,7 @@ makeTokenParser languageDef charControl = do{ char '^' ; code <- upper - ; return (toEnum (fromEnum code - fromEnum 'A')) + ; return (toEnum (fromEnum code - fromEnum 'A' + 1)) } charNum = do{ code <- decimal diff --git a/parsec.cabal b/parsec.cabal index fbe1750..bc1c9ea 100644 --- a/parsec.cabal +++ b/parsec.cabal @@ -1,6 +1,6 @@ name: parsec version: 3.1.6 -cabal-version: >= 1.6 +cabal-version: >= 1.8 license: BSD3 license-file: LICENSE author: Daan Leijen <daan@microsoft.com>, Paolo Martini <paolo@nemail.it> @@ -63,3 +63,16 @@ library 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 \ No newline at end of file diff --git a/test/Main.hs b/test/Main.hs new file mode 100644 index 0000000..864f077 --- /dev/null +++ b/test/Main.hs @@ -0,0 +1,10 @@ + +import Test.Framework + +import Tokens ( tokensTests ) + +main :: IO () +main = do + defaultMain + [ testGroup "Text.Parsec.Tokens" tokensTests + ] \ No newline at end of file diff --git a/test/Tokens.hs b/test/Tokens.hs new file mode 100644 index 0000000..3a62625 --- /dev/null +++ b/test/Tokens.hs @@ -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 \ No newline at end of file -- GitLab