haskell-src-meta 0.8.1 → 0.8.2
raw patch · 8 files changed
+73/−21 lines, 8 filesdep ~haskell-src-extsdep ~template-haskellPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: haskell-src-exts, template-haskell
API changes (from Hackage documentation)
+ Language.Haskell.Meta.Parse: parseDecsWithMode :: ParseMode -> String -> Either String [Dec]
+ Language.Haskell.Meta.Parse: parseHsDeclsWithMode :: ParseMode -> String -> Either String [Decl SrcSpanInfo]
Files
- ChangeLog +4/−0
- README.md +3/−2
- haskell-src-meta.cabal +6/−4
- src/Language/Haskell/Meta/Parse.hs +12/−0
- src/Language/Haskell/Meta/Syntax/Translate.hs +6/−1
- src/Language/Haskell/Meta/Utils.hs +1/−1
- tests/Main.hs +20/−5
- tests/Splices.hs +21/−8
ChangeLog view
@@ -1,3 +1,7 @@+0.8.2:+- Added ToExp implementation for type application+- Added parseDecsWithMode and parseHsDeclsWithMode+ 0.8.1: - Compatible with GHC 8.6, haskell-src-exts 1.22
README.md view
@@ -1,4 +1,4 @@-The `haskell-src-meta` Package [](https://hackage.haskell.org/package/haskell-src-meta) [](https://travis-ci.org/mainland/haskell-src-meta)+The `haskell-src-meta` Package [](https://hackage.haskell.org/package/haskell-src-meta) [](https://travis-ci.org/DanBurton/haskell-src-meta) ================== `haskell-src-meta` is a package originally by Matt Morrow for converting a@@ -12,4 +12,5 @@ the github repository, or you think you could do a better job as maintainer, just ask. -The above was written by Ben Millwood, but I (Dan Burton) share the same sentiment.+The above was written by Ben Millwood,+but I (Dan Burton) share the same sentiment.
haskell-src-meta.cabal view
@@ -1,5 +1,5 @@ name: haskell-src-meta-version: 0.8.1+version: 0.8.2 cabal-version: >= 1.8 build-type: Simple license: BSD3@@ -55,7 +55,9 @@ build-depends: base,- haskell-src-meta+ haskell-src-exts,+ haskell-src-meta,+ template-haskell test-suite examples type: exitcode-stdio-1.0@@ -66,9 +68,9 @@ base, containers, haskell-src-meta,+ pretty, syb,- template-haskell,- pretty+ template-haskell other-modules: BF,
src/Language/Haskell/Meta/Parse.hs view
@@ -13,11 +13,13 @@ parseExp, parseType, parseDecs,+ parseDecsWithMode, myDefaultParseMode, myDefaultExtensions, parseResultToEither, parseHsModule, parseHsDecls,+ parseHsDeclsWithMode, parseHsType, parseHsExp, parseHsPat,@@ -63,6 +65,11 @@ parseDecs :: String -> Either String [Dec] parseDecs = either Left (Right . toDecs) . parseHsDecls +-- | @since 0.8.2+parseDecsWithMode :: ParseMode -> String -> Either String [Dec]+parseDecsWithMode parseMode = either Left (Right . toDecs)+ . parseHsDeclsWithMode parseMode+ ----------------------------------------------------------------------------- {-# DEPRECATED myDefaultParseMode, myDefaultExtensions@@ -98,6 +105,11 @@ parseHsDecls :: String -> Either String [Hs.Decl Hs.SrcSpanInfo] parseHsDecls = either Left (Right . moduleDecls) . parseResultToEither . parseModuleWithMode myDefaultParseMode++-- | @since 0.8.2+parseHsDeclsWithMode :: ParseMode -> String -> Either String [Hs.Decl Hs.SrcSpanInfo]+parseHsDeclsWithMode parseMode = either Left (Right . moduleDecls)+ . parseResultToEither . parseModuleWithMode parseMode parseHsType :: String -> Either String (Hs.Type Hs.SrcSpanInfo)
src/Language/Haskell/Meta/Syntax/Translate.hs view
@@ -135,7 +135,7 @@ toName (Hs.UnitCon _) = mkName "()" toName (Hs.ListCon _) = ''[] -- Parser only uses this in types toName (Hs.FunCon _) = ''(->)- toName (Hs.TupleCon _ _ n) = + toName (Hs.TupleCon _ _ n) = mkName $ concat ["(",replicate (n-1) ',',")"] toName (Hs.Cons _) = '(:) @@ -236,6 +236,11 @@ toExp (Hs.Con _ n) = ConE (toName n) toExp (Hs.Lit _ l) = LitE (toLit l) toExp (Hs.InfixApp _ e o f) = UInfixE (toExp e) (toExp o) (toExp f)+#if MIN_VERSION_template_haskell(2,12,0)+ toExp (Hs.App _ e (Hs.TypeApp _ t)) = AppTypeE (toExp e) (toType t)+#else+ toExp (Hs.App _ e aTypeApp@Hs.TypeApp{}) = noTHyet "toExp" "2.12.0" aTypeApp+#endif toExp (Hs.App _ e f) = AppE (toExp e) (toExp f) toExp (Hs.NegApp _ e) = AppE (VarE 'negate) (toExp e) toExp (Hs.Lambda _ ps e) = LamE (fmap toPat ps) (toExp e)
src/Language/Haskell/Meta/Utils.hs view
@@ -92,7 +92,7 @@ (c:_) | isSym c -> concat ["(",nb,")"] _ -> nb isSym :: Char -> Bool- isSym = (`elem` "><.\\/!@#$%^&*-+?:|")+ isSym = (`elem` ("><.\\/!@#$%^&*-+?:|" :: [Char])) -----------------------------------------------------------------------------
tests/Main.hs view
@@ -3,11 +3,9 @@ module Main where import Language.Haskell.Meta.Parse-#if MIN_VERSION_haskell_src_exts(1,18,0) import qualified Language.Haskell.Exts as Exts-#else-import qualified Language.Haskell.Exts.Annotated as Exts-#endif+import qualified Language.Haskell.Exts.Extension as Extension+import qualified Language.Haskell.Exts.Parser as Parser import qualified Language.Haskell.TH as TH import Test.Framework import Test.Framework.Providers.HUnit@@ -17,17 +15,34 @@ main = defaultMain tests tests :: [Test]-tests = [derivingClausesTest]+tests = [ derivingClausesTest+#if MIN_VERSION_template_haskell(2,12,0)+ , typeAppTest+#endif+ ] derivingClausesTest :: Test derivingClausesTest = testCase "Deriving clauses preserved" $ roundTripDecls "data Foo = Foo deriving (A, B, C)" +typeAppMode :: Exts.ParseMode+typeAppMode = Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications] }++typeAppTest :: Test+typeAppTest = testCase "Type app preserved" $+ roundTripDeclsWithMode typeAppMode "tenStr = show @Int 10"+ roundTripDecls :: String -> Assertion roundTripDecls s = do declsExts <- liftEither $ parseHsDecls s declsExts' <- liftEither $ parseDecs s >>= parseHsDecls . TH.pprint declsExts' @?= declsExts++roundTripDeclsWithMode :: Exts.ParseMode -> String -> Assertion+roundTripDeclsWithMode mode s = do+ declsExts <- liftEither $ parseHsDeclsWithMode mode s+ declsExts' <- liftEither $ parseDecsWithMode mode s >>= parseHsDeclsWithMode mode . TH.pprint+ declsExts' @?= declsExts liftEither :: Monad m => Either String a -> m a liftEither = either fail return
tests/Splices.hs view
@@ -1,50 +1,63 @@ {-# LANGUAGE CPP, TemplateHaskell #-} -- | Tests stuff mostly by just compiling correctly-import Language.Haskell.Meta+import qualified Language.Haskell.Exts.Extension as Extension+import qualified Language.Haskell.Exts.Parser as Parser+import qualified Language.Haskell.Meta as Meta ----- Testing names ----- -- Test that the unit constructor works-$(either error return $ parseDecs+$(either error return $ Meta.parseDecs "unit :: IO ()\nunit = return ()") -- Testing that the [] constructor works in types, #if MIN_VERSION_base(4,9,0)-$(either error return $ parseDecs+$(either error return $ Meta.parseDecs "nilp :: [a] -> ([] a)\nnilp [] = []") #else -- CPP Note: Apparently ghc < 7 doesn't parse this correctly w/o the forall. -- https://github.com/DanBurton/haskell-src-meta/issues/2-$(either error return $ parseDecs+$(either error return $ Meta.parseDecs "nilp :: forall a. [a] -> ([] a)\nnilp [] = []") #endif -$(either error return $ parseDecs+$(either error return $ Meta.parseDecs "pair :: (,) Int Int\npair = (,) 1 2") ----- Testing classes and instances ----- #if MIN_VERSION_base(4,9,0)-$(either error return $ parseDecs $ unlines+$(either error return $ Meta.parseDecs $ unlines ["class MyClass a where mymethod :: a -> b -> (a,b)" ,"instance MyClass Bool where mymethod a b = (a,b)" ]) #else -- CPP Note: Apparently ghc < 7 doesn't parse this correctly w/o the forall. -- https://github.com/DanBurton/haskell-src-meta/issues/2-$(either error return $ parseDecs $ unlines+$(either error return $ Meta.parseDecs $ unlines ["class MyClass a where mymethod :: forall b. a -> b -> (a,b)" ,"instance MyClass Bool where mymethod a b = (a,b)" ]) #endif +#if MIN_VERSION_template_haskell(2,12,0)+$(either error return $ Meta.parseDecsWithMode (Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications] }) $ unlines+ ["tenStr :: String"+ ,"tenStr = show @Int 10"])+#else+-- Type Application not supported by template-haskell < 2.12+$(either error return $ Meta.parseDecs $ unlines+ ["tenStr :: String"+ ,"tenStr = show (10 :: Int)"])+#endif -- Just to check that it works as intended main = do- -9 <- return $(either error return $ parseExp "-3^2") :: IO Int+ -9 <- return $(either error return $ Meta.parseExp "-3^2") :: IO Int () <- unit [] <- return (nilp []) (1,2) <- return pair (True,1) <- return $ mymethod True 1+ "10" <- return tenStr return ()