@@ -15,10 +15,11 @@ import Data.Maybe (maybeToList)
1515import FastString
1616import Ormolu.Parser.Shebang (isShebang)
1717import Ormolu.Processing.Common
18+import qualified Ormolu.Processing.Cpp as Cpp
1819import SrcLoc
1920
2021-- | Transform given input possibly returning comments extracted from it.
21−-- This handles LINE pragmas, shebangs, and the magic comments for
22+-- This handles LINE pragmas, CPP, shebangs, and the magic comments for
2223-- enabling\/disabling of Ormolu.
2324preprocess ::
2425 -- | File name, just to use in the spans
@@ -27,9 +28,9 @@ preprocess ::
2728 String ->
2829 -- | Adjusted input with comments extracted from it
2930 (String, [Located String])
30−preprocess path input = go 1 OrmoluEnabled id id (lines input)
31+preprocess path input = go 1 OrmoluEnabled Cpp.Outside id id (lines input)
3132 where
32− go !n ormoluState inputSoFar csSoFar = \case
33+ go !n ormoluState cppState inputSoFar csSoFar = \case
3334 [] ->
3435 let input' = unlines (inputSoFar [])
3536 in ( case ormoluState of
@@ -38,10 +39,12 @@ preprocess path input = go 1 OrmoluEnabled id id (lines input)
3839 csSoFar []
3940 )
4041 (x : xs) ->
41− let (x', ormoluState', cs) = processLine path n ormoluState x
42+ let (x', ormoluState', cppState', cs) =
43+ processLine path n ormoluState cppState x
4244 in go
4345 (n + 1)
4446 ormoluState'
47+ cppState'
4548 (inputSoFar . (x' :))
4649 (csSoFar . (maybeToList cs ++))
4750 xs
@@ -54,34 +57,41 @@ processLine ::
5457 Int ->
5558 -- | Whether Ormolu is currently enabled
5659 OrmoluState ->
60+ -- | CPP state
61+ Cpp.State ->
5762 -- | The actual line
5863 String ->
5964 -- | Adjusted line and possibly a comment extracted from it
60− (String, OrmoluState, Maybe (Located String))
61−processLine path n ormoluState line
65+ (String, OrmoluState, Cpp.State, Maybe (Located String))
66+processLine path n ormoluState Cpp.Outside line
6267 | "{-# LINE" `L.isPrefixOf` line =
6368 let (pragma, res) = getPragma line
6469 size = length pragma
6570 ss = mkSrcSpan (mkSrcLoc' 1) (mkSrcLoc' (size + 1))
66− in (res, ormoluState, Just (L ss pragma))
71+ in (res, ormoluState, Cpp.Outside, Just (L ss pragma))
6772 | isOrmoluEnable line =
6873 case ormoluState of
6974 OrmoluEnabled ->
70− (enableMarker, OrmoluEnabled, Nothing)
75+ (enableMarker, OrmoluEnabled, Cpp.Outside, Nothing)
7176 OrmoluDisabled ->
72− (endDisabling ++ enableMarker, OrmoluEnabled, Nothing)
77+ (endDisabling ++ enableMarker, OrmoluEnabled, Cpp.Outside, Nothing)
7378 | isOrmoluDisable line =
7479 case ormoluState of
7580 OrmoluEnabled ->
76− (disableMarker ++ startDisabling, OrmoluDisabled, Nothing)
81+ (disableMarker ++ startDisabling, OrmoluDisabled, Cpp.Outside, Nothing)
7782 OrmoluDisabled ->
78− (disableMarker, OrmoluDisabled, Nothing)
83+ (disableMarker, OrmoluDisabled, Cpp.Outside, Nothing)
7984 | isShebang line =
8085 let ss = mkSrcSpan (mkSrcLoc' 1) (mkSrcLoc' (length line))
81− in ("", ormoluState, Just (L ss line))
82− | otherwise = (line, ormoluState, Nothing)
86+ in ("", ormoluState, Cpp.Outside, Just (L ss line))
87+ | otherwise =
88+ let (line', cppState') = Cpp.processLine line Cpp.Outside
89+ in (line', ormoluState, cppState', Nothing)
8390 where
8491 mkSrcLoc' = mkSrcLoc (mkFastString path) n
92+processLine _ _ ormoluState cppState line =
93+ let (line', cppState') = Cpp.processLine line cppState
94+ in (line', ormoluState, cppState', Nothing)
8595
8696-- | Take a line pragma and output its replacement (where line pragma is
8797-- replaced with spaces) and the contents of the pragma itself.