Update to megaparsec 6.0.0 (#51)

* Update megaparsec to version 6.0

* Add void package for GHC versions lower than 7.10

* Add orphan instances for ParseError

* Drop support for GHC 7.6.3

* Add a type alias for Parser

* Change newtypes with function Gen's
This commit is contained in:
Cristhian Motoche 2017-08-03 16:39:00 -05:00 committed by GitHub
parent 32e69f8545
commit 6cdef174b2
9 changed files with 79 additions and 27 deletions

View File

@ -9,8 +9,6 @@ cache:
matrix: matrix:
include: include:
- env: CABALVER=1.24 GHCVER=7.6.3
addons: {apt: {packages: [cabal-install-1.24,ghc-7.6.3], sources: [hvr-ghc]}}
- env: CABALVER=1.24 GHCVER=7.8.4 - env: CABALVER=1.24 GHCVER=7.8.4
addons: {apt: {packages: [cabal-install-1.24,ghc-7.8.4,],sources: [hvr-ghc]}} addons: {apt: {packages: [cabal-install-1.24,ghc-7.8.4,],sources: [hvr-ghc]}}
- env: CABALVER=1.24 GHCVER=7.10.3 - env: CABALVER=1.24 GHCVER=7.10.3
@ -33,10 +31,6 @@ script:
*) cabal configure --enable-tests -v2 -f dev ;; *) cabal configure --enable-tests -v2 -f dev ;;
esac esac
- cabal build - cabal build
- case "$GHCVER" in
"7.6.3") true ;;
*) cabal test --show-details=always ;;
esac
- cabal sdist - cabal sdist
- cabal haddock | grep "100%" | wc -l | grep "13" - cabal haddock | grep "100%" | wc -l | grep "13"

View File

@ -1,3 +1,7 @@
## Inflections 0.4.0.0
* Update megaparsec to version 6.
## Inflections 0.3.0.0 ## Inflections 0.3.0.0
* A more type-safe API forbidding creation of invalid words. * A more type-safe API forbidding creation of invalid words.

View File

@ -117,6 +117,7 @@ where
import Control.Monad.Catch (MonadThrow (..)) import Control.Monad.Catch (MonadThrow (..))
import Data.Text (Text) import Data.Text (Text)
import Data.Void (Void)
import Text.Inflections.Camelize (camelize, camelizeCustom) import Text.Inflections.Camelize (camelize, camelizeCustom)
import Text.Inflections.Dasherize (dasherize) import Text.Inflections.Dasherize (dasherize)
import Text.Inflections.Data (Transliterations, defaultTransliterations) import Text.Inflections.Data (Transliterations, defaultTransliterations)
@ -141,7 +142,7 @@ import Prelude hiding (Word)
-- --
-- >>> toUnderscore "FooBarBazz" -- >>> toUnderscore "FooBarBazz"
-- "foo_bar_bazz" -- "foo_bar_bazz"
toUnderscore :: Text -> Either (ParseError Char Dec) Text toUnderscore :: Text -> Either (ParseError Char Void) Text
toUnderscore = fmap underscore . parseCamelCase [] toUnderscore = fmap underscore . parseCamelCase []
-- | Transforms CamelCasedString to snake-cased-string-with-dashes. -- | Transforms CamelCasedString to snake-cased-string-with-dashes.
@ -150,7 +151,7 @@ toUnderscore = fmap underscore . parseCamelCase []
-- --
-- >>> toDashed "FooBarBazz" -- >>> toDashed "FooBarBazz"
-- "foo-bar-bazz" -- "foo-bar-bazz"
toDashed :: Text -> Either (ParseError Char Dec) Text toDashed :: Text -> Either (ParseError Char Void) Text
toDashed = fmap dasherize . parseCamelCase [] toDashed = fmap dasherize . parseCamelCase []
-- | Transforms underscored_text to CamelCasedText. If first argument is -- | Transforms underscored_text to CamelCasedText. If first argument is
@ -166,7 +167,7 @@ toDashed = fmap dasherize . parseCamelCase []
toCamelCased toCamelCased
:: Bool -- ^ Capitalize the first character :: Bool -- ^ Capitalize the first character
-> Text -- ^ Input -> Text -- ^ Input
-> Either (ParseError Char Dec) Text -- ^ Output -> Either (ParseError Char Void) Text -- ^ Output
toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase [] toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
-- | Transforms underscored_text to space-separated human-readable text. -- | Transforms underscored_text to space-separated human-readable text.
@ -185,10 +186,10 @@ toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
toHumanized toHumanized
:: Bool -- ^ Capitalize the first character :: Bool -- ^ Capitalize the first character
-> Text -- ^ Input -> Text -- ^ Input
-> Either (ParseError Char Dec) Text -- ^ Output -> Either (ParseError Char Void) Text -- ^ Output
toHumanized c = fmap (humanizeCustom c) . parseSnakeCase [] toHumanized c = fmap (humanizeCustom c) . parseSnakeCase []
-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to -- | Lift something of type @'Either' ('ParseError' 'Char' 'Void') a@ to
-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing -- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
-- failures and you're in an instance of 'MonadThrow'. -- failures and you're in an instance of 'MonadThrow'.
-- --
@ -196,6 +197,6 @@ toHumanized c = fmap (humanizeCustom c) . parseSnakeCase []
-- --
-- /since 0.3.0.0/ -- /since 0.3.0.0/
betterThrow :: MonadThrow m => Either (ParseError Char Dec) a -> m a betterThrow :: MonadThrow m => Either (ParseError Char Void) a -> m a
betterThrow (Left err) = throwM (InflectionParsingFailed err) betterThrow (Left err) = throwM (InflectionParsingFailed err)
betterThrow (Right x) = return x betterThrow (Right x) = return x

View File

@ -19,9 +19,10 @@ where
import Control.Applicative import Control.Applicative
import Data.Text (Text) import Data.Text (Text)
import Data.Void (Void)
import Text.Inflections.Types import Text.Inflections.Types
import Text.Megaparsec import Text.Megaparsec
import Text.Megaparsec.Text import Text.Megaparsec.Char
import qualified Data.Text as T import qualified Data.Text as T
#if MIN_VERSION_base(4,8,0) #if MIN_VERSION_base(4,8,0)
@ -31,6 +32,8 @@ import Data.Foldable
import Prelude hiding (elem) import Prelude hiding (elem)
#endif #endif
type Parser = Parsec Void Text
-- | Parse a CamelCase string. -- | Parse a CamelCase string.
-- --
-- >>> bar <- mkAcronym "bar" -- >>> bar <- mkAcronym "bar"
@ -44,7 +47,7 @@ import Prelude hiding (elem)
parseCamelCase :: (Foldable f, Functor f) parseCamelCase :: (Foldable f, Functor f)
=> f (Word 'Acronym) -- ^ Collection of acronyms => f (Word 'Acronym) -- ^ Collection of acronyms
-> Text -- ^ Input -> Text -- ^ Input
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing -> Either (ParseError Char Void) [SomeWord] -- ^ Result of parsing
parseCamelCase acronyms = parse (parser acronyms) "" parseCamelCase acronyms = parse (parser acronyms) ""
parser :: (Foldable f, Functor f) parser :: (Foldable f, Functor f)
@ -59,7 +62,7 @@ acronym :: (Foldable f, Functor f)
=> f (Word 'Acronym) => f (Word 'Acronym)
-> Parser (Word 'Acronym) -> Parser (Word 'Acronym)
acronym acronyms = do acronym acronyms = do
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms) x <- choice (string . unWord <$> acronyms)
case mkAcronym x of case mkAcronym x of
Nothing -> empty -- cannot happen if the system is sound Nothing -> empty -- cannot happen if the system is sound
Just acr -> return acr Just acr -> return acr

View File

@ -18,9 +18,10 @@ where
import Control.Applicative import Control.Applicative
import Data.Text (Text) import Data.Text (Text)
import Data.Void (Void)
import Text.Inflections.Types import Text.Inflections.Types
import Text.Megaparsec import Text.Megaparsec
import Text.Megaparsec.Text import Text.Megaparsec.Char
import qualified Data.Text as T import qualified Data.Text as T
#if MIN_VERSION_base(4,8,0) #if MIN_VERSION_base(4,8,0)
@ -30,6 +31,8 @@ import Data.Foldable
import Prelude hiding (elem) import Prelude hiding (elem)
#endif #endif
type Parser = Parsec Void Text
-- | Parse a snake_case string. -- | Parse a snake_case string.
-- --
-- >>> bar <- mkAcronym "bar" -- >>> bar <- mkAcronym "bar"
@ -43,7 +46,7 @@ import Prelude hiding (elem)
parseSnakeCase :: (Foldable f, Functor f) parseSnakeCase :: (Foldable f, Functor f)
=> f (Word 'Acronym) -- ^ Collection of acronyms => f (Word 'Acronym) -- ^ Collection of acronyms
-> Text -- ^ Input -> Text -- ^ Input
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing -> Either (ParseError Char Void) [SomeWord] -- ^ Result of parsing
parseSnakeCase acronyms = parse (parser acronyms) "" parseSnakeCase acronyms = parse (parser acronyms) ""
parser :: (Foldable f, Functor f) parser :: (Foldable f, Functor f)

View File

@ -33,6 +33,7 @@ where
import Control.Monad.Catch import Control.Monad.Catch
import Data.Char (isAlphaNum) import Data.Char (isAlphaNum)
import Data.Data (Data) import Data.Data (Data)
import Data.Void (Void)
import Data.Text (Text) import Data.Text (Text)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import GHC.Generics (Generic) import GHC.Generics (Generic)
@ -135,7 +136,7 @@ instance Transformable (Word 'Acronym) where
-- /since 0.3.0.0/ -- /since 0.3.0.0/
data InflectionException data InflectionException
= InflectionParsingFailed (ParseError Char Dec) = InflectionParsingFailed (ParseError Char Void)
| InflectionInvalidWord Text | InflectionInvalidWord Text
| InflectionInvalidAcronym Text | InflectionInvalidAcronym Text
deriving (Eq, Show, Typeable, Data, Generic) deriving (Eq, Show, Typeable, Data, Generic)

View File

@ -1,5 +1,5 @@
name: inflections name: inflections
version: 0.3.0.0 version: 0.4.0.0
synopsis: Inflections library for Haskell synopsis: Inflections library for Haskell
description: description:
Inflections provides methods for singularization, pluralization, Inflections provides methods for singularization, pluralization,
@ -49,9 +49,12 @@ library
ghc-options: -O2 -Wall ghc-options: -O2 -Wall
build-depends: base >= 4.6 && < 5.0 build-depends: base >= 4.6 && < 5.0
, exceptions >= 0.6 && < 0.9 , exceptions >= 0.6 && < 0.9
, megaparsec >= 5.0 && < 6.0 , megaparsec >= 6.0 && < 7.0
, text >= 0.2 && < 1.3 , text >= 0.2 && < 1.3
, unordered-containers >= 0.2.7 && < 0.3 , unordered-containers >= 0.2.7 && < 0.3
if !impl(ghc >= 7.10)
build-depends: void == 0.7.*
default-language: Haskell2010 default-language: Haskell2010
test-suite test test-suite test
@ -61,10 +64,17 @@ test-suite test
build-depends: inflections build-depends: inflections
, QuickCheck >= 2.7.6 && < 3.0 , QuickCheck >= 2.7.6 && < 3.0
, base >= 4.6 && < 5.0 , base >= 4.6 && < 5.0
, containers >= 0.5 && < 0.7
, hspec >= 2.0 && < 3.0 , hspec >= 2.0 && < 3.0
, hspec-megaparsec >= 0.3 && < 0.4 , hspec-megaparsec >= 1.0 && < 2.0
, megaparsec >= 5.1 && < 6.0 , megaparsec
, text >= 0.2 && < 1.3 , text >= 0.2 && < 1.3
if !impl(ghc >= 7.10)
build-depends: void == 0.7.*
if !impl(ghc >= 8.0)
build-depends: semigroups == 0.18.*
if flag(dev) if flag(dev)
ghc-options: -Wall -Werror ghc-options: -Wall -Werror
else else

View File

@ -1,6 +1,7 @@
---
resolver: lts-7.13 resolver: lts-7.13
packages:
- '.'
extra-deps: extra-deps:
- hspec-megaparsec-0.3.0 - hspec-megaparsec-1.0.0
- megaparsec-5.1.2 - megaparsec-6.0.0
- parser-combinators-0.1.0

View File

@ -1,10 +1,44 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Text.InflectionsSpec (spec) where module Text.InflectionsSpec (spec) where
import Data.Void
import Test.Hspec import Test.Hspec
import Test.QuickCheck import Test.QuickCheck
import Text.Inflections import Text.Inflections
import Text.Megaparsec
import qualified Data.List.NonEmpty as NE
import qualified Data.Set as S
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
arbitraryParseError :: Gen (ParseError Char Void)
arbitraryParseError = oneof [trivialError, fancyError]
where
trivialError = TrivialError <$> nonEmptyArbitrary <*> maybeErrorItem <*> setErrorItem
fancyError = FancyError <$> nonEmptyArbitrary <*> setErrorFancy
nonEmptyArbitrary = NE.fromList <$> listOf1 arbitrarySourcePos
setErrorFancy = S.fromList <$> listOf arbitraryErrorFancy
maybeErrorItem = oneof [ Just <$> arbitraryErrorItem, return Nothing]
setErrorItem = S.fromList <$> listOf arbitraryErrorItem
arbitrarySourcePos :: Gen SourcePos
arbitrarySourcePos = SourcePos <$> arbitrary <*> posArbitrary <*> posArbitrary
where
posArbitrary = mkPos <$> ((+1) . abs <$> arbitrary)
arbitraryErrorFancy :: Gen (ErrorFancy e)
arbitraryErrorFancy = oneof [ ErrorFail <$> arbitrary ]
arbitraryErrorItem :: (Arbitrary e) => Gen (ErrorItem e)
arbitraryErrorItem = oneof [ tokens_, labels_, return EndOfInput ]
where
tokens_ = Tokens <$> (NE.fromList <$> listOf1 arbitrary)
labels_ = Label <$> (NE.fromList <$> listOf1 arbitrary)
spec :: Spec spec :: Spec
spec = do spec = do
@ -38,9 +72,10 @@ spec = do
describe "betterThrow" $ do describe "betterThrow" $ do
context "when given a parse error" $ context "when given a parse error" $
it "throws the correct exception" $ it "throws the correct exception" $
property $ \err -> property $ forAll arbitraryParseError $ \err ->
betterThrow (Left err) `shouldThrow` betterThrow (Left err) `shouldThrow`
(== InflectionParsingFailed err) (== InflectionParsingFailed err)
context "when given a value in Right" $ context "when given a value in Right" $
it "returns the value" $ it "returns the value" $
property $ \x -> property $ \x ->