on master help sign in
Fix positioning of shebangs
commita83796bccb2d53aaaaa38701d038193d1eb9aaeb authormrkkrp <mark.karpov@tweag.io> dateSat 14 Sep 2019, 16:36 (7 years ago) parent75ac291
3 files changed+21−11
data/examples/module-header/shebang-out.hs+5 data/examples/module-header/shebang.hs+5 src/Ormolu/Parser.hs+11−11
data/examples/module-header/shebang-out.hsnew file+5
@@ -0,0 +1,5 @@
1+#! /usr/bin/env runhaskell
2+import Prelude
3+
4+main :: IO ()
5+main = putStrLn "hello world"
data/examples/module-header/shebang.hsnew file+5
@@ -0,0 +1,5 @@
1+#! /usr/bin/env runhaskell
2+import Prelude
3+
4+main :: IO ()
5+main = putStrLn "hello world"
src/Ormolu/Parser.hs+11−11
@@ -44,7 +44,7 @@ parseModule
4444 , Either (SrcSpan, String) ParseResult
4545 )
4646parseModule Config {..} path input' = liftIO $ do
47− let (input, extraComments) = stripLinePragmas input'
47+ let (input, extraComments) = stripLinePragmas path input'
4848 (ws, dynFlags) <- ghcWrapper $ do
4949 dynFlags0 <- initDynFlagsPure path input
5050 (dynFlags1, _, ws) <- GHC.parseDynamicFilePragma
@@ -152,27 +152,27 @@ runParser parser flags filename input = GHC.unP parser parseState
152152-- | Remove GHC style line pragams (@{-# LINE .. #-}@) and convert them into
153153-- comments.
154154
155−stripLinePragmas :: String -> (String, [Located String])
156−stripLinePragmas = unlines' . unzip . findLines . lines
155+stripLinePragmas :: FilePath -> String -> (String, [Located String])
156+stripLinePragmas path = unlines' . unzip . findLines path . lines
157157 where
158158 unlines' (a, b) = (unlines a, catMaybes b)
159159
160−findLines :: [String] -> [(String, Maybe (Located String))]
161−findLines = zipWith checkLine [1..]
160+findLines :: FilePath -> [String] -> [(String, Maybe (Located String))]
161+findLines path = zipWith (checkLine path) [1..]
162162
163−checkLine :: Int -> String -> (String, Maybe (Located String))
164−checkLine line s
163+checkLine :: FilePath -> Int -> String -> (String, Maybe (Located String))
164+checkLine path line s
165165 | "{-# LINE" `isPrefixOf` s =
166166 let (pragma, res) = getPragma s
167167 size = length pragma
168− mSrcLoc = mkSrcLoc (GHC.mkFastString "LINE")
169− ss = mkSrcSpan (mSrcLoc line 1) (mSrcLoc line (size+1))
168+ ss = mkSrcSpan (mkSrcLoc' 1) (mkSrcLoc' (size + 1))
170169 in (res, Just $ L ss pragma)
171170 | "#!" `isPrefixOf` s =
172− let mSrcLoc = mkSrcLoc (GHC.mkFastString "SHEBANG")
173− ss = mkSrcSpan (mSrcLoc line 1) (mSrcLoc line (length s))
171+ let ss = mkSrcSpan (mkSrcLoc' 1) (mkSrcLoc' (length s))
174172 in ("",Just $ L ss s)
175173 | otherwise = (s, Nothing)
174+ where
175+ mkSrcLoc' = mkSrcLoc (GHC.mkFastString path) line
176176
177177getPragma :: String -> (String, String)
178178getPragma [] = error "Ormolu.Parser.getPragma: input must not be empty"