diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-30 13:03:10 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-30 13:03:10 +0200 |
commit | c91efbb3200085c555285d23fa3316ace25bc027 (patch) | |
tree | 432523667388e91b575404170cb3172d2fb015cc | |
parent | 33dc1a1bd218c85bc253da07a46a9d48e89375b4 (diff) | |
download | stylish-haskell-c91efbb3200085c555285d23fa3316ace25bc027.tar.gz |
Actually use extra language extensions
-rw-r--r-- | src/Main.hs | 3 | ||||
-rw-r--r-- | src/StylishHaskell.hs | 9 | ||||
-rw-r--r-- | src/StylishHaskell/Config.hs | 7 | ||||
-rw-r--r-- | src/StylishHaskell/Parse.hs | 33 | ||||
-rw-r--r-- | stylish-haskell.cabal | 10 | ||||
-rw-r--r-- | tests/StylishHaskell/Tests/Util.hs | 2 |
6 files changed, 44 insertions, 20 deletions
diff --git a/src/Main.hs b/src/Main.hs index 4b74c6f..40c801e 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -74,6 +74,7 @@ stylishHaskell sa file :: StylishArgs -> Config -> Maybe FilePath -> IO () file sa conf mfp = do contents <- maybe getContents readFile mfp - write $ unlines $ runSteps mfp (configSteps conf) $ lines contents + write $ unlines $ runSteps (configLanguageExtensions conf) + mfp (configSteps conf) $ lines contents where write = maybe putStr (if inPlace sa then writeFile else const putStr) mfp diff --git a/src/StylishHaskell.hs b/src/StylishHaskell.hs index b052429..7705d3a 100644 --- a/src/StylishHaskell.hs +++ b/src/StylishHaskell.hs @@ -6,17 +6,18 @@ module StylishHaskell -------------------------------------------------------------------------------- +import StylishHaskell.Config import StylishHaskell.Parse import StylishHaskell.Step -------------------------------------------------------------------------------- -runStep :: Maybe FilePath -> Step -> Lines -> Lines -runStep mfp step ls = case parseModule mfp (unlines ls) of +runStep :: Extensions -> Maybe FilePath -> Step -> Lines -> Lines +runStep exts mfp step ls = case parseModule exts mfp (unlines ls) of Left err -> error err -- TODO: maybe return original lines? Right module' -> stepFilter step ls module' -------------------------------------------------------------------------------- -runSteps :: Maybe FilePath -> [Step] -> Lines -> Lines -runSteps mfp = foldr (flip (.)) id . map (runStep mfp) +runSteps :: Extensions -> Maybe FilePath -> [Step] -> Lines -> Lines +runSteps exts mfp = foldr (flip (.)) id . map (runStep exts mfp) diff --git a/src/StylishHaskell/Config.hs b/src/StylishHaskell/Config.hs index 7feef74..b9d9bca 100644 --- a/src/StylishHaskell/Config.hs +++ b/src/StylishHaskell/Config.hs @@ -1,7 +1,8 @@ -------------------------------------------------------------------------------- {-# LANGUAGE OverloadedStrings #-} module StylishHaskell.Config - ( Config (..) + ( Extensions + , Config (..) , defaultConfigFilePath , configFilePath , loadConfig @@ -35,6 +36,10 @@ import StylishHaskell.Verbose -------------------------------------------------------------------------------- +type Extensions = [String] + + +-------------------------------------------------------------------------------- data Config = Config { configSteps :: [Step] , configLanguageExtensions :: [String] diff --git a/src/StylishHaskell/Parse.hs b/src/StylishHaskell/Parse.hs index 57179b1..24aa8f2 100644 --- a/src/StylishHaskell/Parse.hs +++ b/src/StylishHaskell/Parse.hs @@ -5,11 +5,13 @@ module StylishHaskell.Parse -------------------------------------------------------------------------------- +import Control.Monad.Error (throwError) import Data.Maybe (fromMaybe) import qualified Language.Haskell.Exts.Annotated as H -------------------------------------------------------------------------------- +import StylishHaskell.Config import StylishHaskell.Step @@ -23,19 +25,32 @@ unCpp = unlines . map unCpp' . lines -------------------------------------------------------------------------------- +-- | Read an extension name from a string +parseExtension :: String -> Either String H.Extension +parseExtension str = case reads str of + [(x, "")] -> return x + _ -> throwError $ "Unknown extension: " ++ str + + +-------------------------------------------------------------------------------- -- | Abstraction over HSE's parsing -parseModule :: Maybe FilePath -> String -> Either String Module -parseModule mfp string = - let fp = fromMaybe "<unknown>" mfp - -- Determine the extensions used in the file, and update the parsing - -- mode based upon those - exts = fromMaybe [] $ H.readExtensions string +parseModule :: Extensions -> Maybe FilePath -> String -> Either String Module +parseModule extraExts mfp string = do + -- Determine the extensions: those specified in the file and the extra ones + extraExts' <- mapM parseExtension extraExts + let fileExts = fromMaybe [] $ H.readExtensions string + exts = fileExts ++ extraExts' + + -- Parsing options... + fp = fromMaybe "<unknown>" mfp mode = H.defaultParseMode {H.extensions = exts, H.fixities = Nothing} + -- Special handling for CPP, haskell-src-exts can't deal with it string' = if H.CPP `elem` exts then unCpp string else string - in case H.parseModuleWithComments mode string' of - H.ParseOk md -> Right md - err -> Left $ + + case H.parseModuleWithComments mode string' of + H.ParseOk md -> return md + err -> throwError $ "StylishHaskell.Parse.parseModule: could not parse " ++ fp ++ ": " ++ show err diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index 4c07edd..ec03f90 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -51,9 +51,10 @@ Executable stylish-haskell directory >= 1.1 && < 1.2, filepath >= 1.1 && < 1.4, haskell-src-exts >= 1.13 && < 1.14, + mtl >= 2.0 && < 2.2, + strict >= 0.3 && < 0.4, syb >= 0.3 && < 0.4, - yaml >= 0.7 && < 0.9, - strict >= 0.3 && < 0.4 + yaml >= 0.7 && < 0.9 Test-suite stylish-haskell-tests Ghc-options: -Wall @@ -82,9 +83,10 @@ Test-suite stylish-haskell-tests directory >= 1.1 && < 1.2, filepath >= 1.1 && < 1.4, haskell-src-exts >= 1.13 && < 1.14, + mtl >= 2.0 && < 2.2, + strict >= 0.3 && < 0.4, syb >= 0.3 && < 0.4, - yaml >= 0.7 && < 0.9, - strict >= 0.3 && < 0.4 + yaml >= 0.7 && < 0.9 Source-repository head Type: git diff --git a/tests/StylishHaskell/Tests/Util.hs b/tests/StylishHaskell/Tests/Util.hs index 4cd4a5a..49cc421 100644 --- a/tests/StylishHaskell/Tests/Util.hs +++ b/tests/StylishHaskell/Tests/Util.hs @@ -10,7 +10,7 @@ import StylishHaskell.Step -------------------------------------------------------------------------------- testStep :: Step -> String -> String -testStep step str = case parseModule Nothing str of +testStep step str = case parseModule [] Nothing str of Left err -> error err Right module' -> unlines $ stepFilter step ls module' where |