Skip to content

Commit

Permalink
Add ‘Word8’ instance of ‘ShowToken’
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Jul 2, 2017
1 parent 44f8461 commit 711d9b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Text/Megaparsec/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ where

import Control.DeepSeq
import Control.Exception
import Data.Char (chr)
import Data.Data (Data)
import Data.List (intercalate)
import Data.List.NonEmpty (NonEmpty (..))
Expand All @@ -44,6 +45,7 @@ import Data.Semigroup
import Data.Set (Set)
import Data.Typeable (Typeable)
import Data.Void
import Data.Word (Word8)
import GHC.Generics
import Prelude hiding (concat)
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -199,6 +201,9 @@ class ShowToken a where
instance ShowToken Char where
showTokens = stringPretty

instance ShowToken Word8 where
showTokens = stringPretty . fmap (chr . fromIntegral)

-- | The type class defines how to print custom data component of
-- 'ParseError'.
--
Expand Down
8 changes: 8 additions & 0 deletions tests/Text/Megaparsec/ErrorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Data.List (isInfixOf, isSuffixOf)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Monoid
import Data.Void
import Data.Word (Word8)
import Test.Hspec
import Test.Hspec.Megaparsec.AdHoc ()
import Test.QuickCheck
Expand Down Expand Up @@ -173,6 +174,13 @@ spec = do
it "shows control characters in long strings property"
(f "{\n" "\"{<newline>\"")

describe "showTokens (Word8 instance)" $
it "basically works" $ do
-- NOTE Currently the Word8 instance is defined via Char intance, so
-- the testing is rather swallow.
let ts = NE.fromList [10,48,49,50] :: NonEmpty Word8
showTokens ts `shouldBe` "\"<newline>012\""

describe "parseErrorPretty" $ do
it "shows unknown ParseError correctly" $
parseErrorPretty (mempty :: PE) `shouldBe` "1:1:\nunknown parse error\n"
Expand Down

0 comments on commit 711d9b1

Please sign in to comment.