summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Language/Haskell/Stylish/Parse.hs13
-rw-r--r--tests/Language/Haskell/Stylish/Parse/Tests.hs10
2 files changed, 21 insertions, 2 deletions
diff --git a/src/Language/Haskell/Stylish/Parse.hs b/src/Language/Haskell/Stylish/Parse.hs
index 8b3d34b..a3ef579 100644
--- a/src/Language/Haskell/Stylish/Parse.hs
+++ b/src/Language/Haskell/Stylish/Parse.hs
@@ -7,6 +7,7 @@ module Language.Haskell.Stylish.Parse
--------------------------------------------------------------------------------
import Data.Maybe (fromMaybe, listToMaybe)
import qualified Language.Haskell.Exts.Annotated as H
+import Data.List (isPrefixOf)
--------------------------------------------------------------------------------
@@ -27,6 +28,14 @@ unCpp = unlines . go False . lines
--------------------------------------------------------------------------------
+-- | Remove shebang from the first line
+unShebang :: String -> String
+unShebang str
+ | "#!" `isPrefixOf` str = unlines $ drop 1 $ lines str
+ | otherwise = str
+
+
+--------------------------------------------------------------------------------
-- | If the given string is prefixed with an UTF-8 Byte Order Mark, drop it
-- because haskell-src-exts can't handle it.
dropBom :: String -> String
@@ -55,10 +64,10 @@ parseModule extraExts mfp string = do
}
-- Preprocessing
- noCpp =
+ processed = unShebang $
if H.EnableExtension H.CPP `elem` exts then unCpp noBom else noBom
- case H.parseModuleWithComments mode noCpp of
+ case H.parseModuleWithComments mode processed of
H.ParseOk md -> return md
err -> Left $
"Language.Haskell.Stylish.Parse.parseModule: could not parse " ++
diff --git a/tests/Language/Haskell/Stylish/Parse/Tests.hs b/tests/Language/Haskell/Stylish/Parse/Tests.hs
index 718c7f6..d5f3c1d 100644
--- a/tests/Language/Haskell/Stylish/Parse/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Parse/Tests.hs
@@ -21,6 +21,7 @@ tests = testGroup "Language.Haskell.Stylish.Parse"
, testCase "Extra extensions" testExtraExtensions
, testCase "Multiline CPP" testMultilineCpp
, testCase "Haskell2010 extension" testHaskell2010
+ , testCase "Shebang" testShebang
]
@@ -59,6 +60,15 @@ testHaskell2010 = assert $ isRight $ parseModule [] Nothing $ unlines
--------------------------------------------------------------------------------
+testShebang :: Assertion
+testShebang = assert $ isRight $ parseModule [] Nothing $ unlines
+ [ "#!runhaskell"
+ , "module Main where"
+ , "main = return ()"
+ ]
+
+
+--------------------------------------------------------------------------------
isRight :: Either a b -> Bool
isRight (Right _) = True
isRight _ = False