@@ -49,7 +49,7 @@ parseModule ::
4949 Either (SrcSpan, String) ParseResult
5050 )
5151parseModule Config {..} path input' = liftIO $ do
52− let (input, extraComments) = stripLinePragmas path input'
52+ let (input, extraComments) = extractCommentsFromLines path input'
5353 -- NOTE It's important that 'setDefaultExts' is done before
5454 -- 'parsePragmasIntoDynFlags', because otherwise we might enable an
5555 -- extension that was explicitly disabled in the file.
@@ -73,12 +73,13 @@ parseModule Config {..} path input' = liftIO $ do
7373 GHC.PFailed _ ss m ->
7474 Left (ss, GHC.showSDoc dynFlags m)
7575 GHC.POk pstate pmod ->
76− let (comments, exts) = mkCommentStream extraComments pstate
76+ let (comments, exts, shebangs) = mkCommentStream extraComments pstate
7777 in Right ParseResult
7878 { prParsedSource = pmod,
7979 prAnns = mkAnns pstate,
8080 prCommentStream = comments,
81− prExtensions = exts
81+ prExtensions = exts,
82+ prShebangs = shebangs
8283 }
8384 return (warnings, r)
8485
@@ -127,18 +128,31 @@ runParser parser flags filename input = GHC.unP parser parseState
127128 buffer = GHC.stringToStringBuffer input
128129 parseState = GHC.mkPState flags buffer location
129130
130−-- | Remove GHC style line pragams (@{-# LINE .. #-}@) and convert them into
131−-- comments.
132−stripLinePragmas :: FilePath -> String -> (String, [Located String])
133−stripLinePragmas path = unlines' . unzip . findLines path . lines
131+-- | Transform given lines possibly returning comments extracted from them.
132+-- This handles LINE pragmas and shebangs.
133+extractCommentsFromLines ::
134+ -- | File name, just to use in the spans
135+ FilePath ->
136+ -- | List of lines from that file
137+ String ->
138+ -- | Adjusted lines together with comments extracted from them
139+ (String, [Located String])
140+extractCommentsFromLines path =
141+ unlines' . unzip . zipWith (extractCommentFromLine path) [1 ..] . lines
134142 where
135143 unlines' (a, b) = (unlines a, catMaybes b)
136144
137−findLines :: FilePath -> [String] -> [(String, Maybe (Located String))]
138−findLines path = zipWith (checkLine path) [1 ..]
139−
140−checkLine :: FilePath -> Int -> String -> (String, Maybe (Located String))
141−checkLine path line s
145+-- | Transform a given line possibly returning a comment extracted from it.
146+extractCommentFromLine ::
147+ -- | File name, just to use in the spans
148+ FilePath ->
149+ -- | Line number of this line
150+ Int ->
151+ -- | The actual line
152+ String ->
153+ -- | Adjusted line and possibly a comment extracted from it
154+ (String, Maybe (Located String))
155+extractCommentFromLine path line s
142156 | "{-# LINE" `isPrefixOf` s =
143157 let (pragma, res) = getPragma s
144158 size = length pragma
@@ -151,7 +165,13 @@ checkLine path line s
151165 where
152166 mkSrcLoc' = mkSrcLoc (GHC.mkFastString path) line
153167
154−getPragma :: String -> (String, String)
168+-- | Take a line pragma and output its replacement (where line pragma is
169+-- replaced with spaces) and the contents of the pragma itself.
170+getPragma ::
171+ -- | Pragma line to analyze
172+ String ->
173+ -- | Contents of the pragma and its replacement line
174+ (String, String)
155175getPragma [] = error "Ormolu.Parser.getPragma: input must not be empty"
156176getPragma s@(x : xs)
157177 | "#-}" `isPrefixOf` s = ("#-}", " " ++ drop 3 s)