Copyright | (c) Carlo Nucera, 2016 |
---|---|
License | BSD3 |
Maintainer | meditans@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe |
Language | Haskell2010 |
After cpp preprocessing, the file is left by the compilation pipeline in the
output format of the cpp
program, described in
this section of
the C Preprocessor manual.
By default, the cpp
program inserts blank lines to preserve line numbering,
but only if the number of blank lines to be created is not too high (<6 or so).
Otherwise a linemarker is created, to reduce the size of the generated file, of
the form:
# linenum filename flags
As cpp doesn't have an option to output only blank lines and keeping the line
numbering, the following functions parse a file with linemarkers separating it
in CppOutputComponents
(the source chunks between the linemarkers), and pad
them with the appropriate amount of blank lines.
- addPadding :: FilePath -> String -> String
- data LineMarker = LineMarker {
- beginsAtLine :: Int
- filePath :: FilePath
- isLineMarker :: String -> Bool
- parseLineMarker :: String -> LineMarker
- data CppOutputComponent = CppOutputComponent {
- lineMarker :: LineMarker
- sourceBlock :: [String]
- parseCppOutputComponents :: [String] -> [CppOutputComponent]
- discardUnusefulComponents :: FilePath -> [CppOutputComponent] -> [CppOutputComponent]
- reconstructSource :: [CppOutputComponent] -> [String]
Entry point for padding
addPadding :: FilePath -> String -> String Source #
Substitutes the lineMarker in the content of a file with the appropriate blank line padding.
Data Types
data LineMarker Source #
A LineMarker
follows the structure described
here. We only
retain the linenumber and the file the line is referring to. Note that the
filename is surrounded by quotation marks in the cpp output, but not in this
representation.
isLineMarker :: String -> Bool Source #
>>>
isLineMarker "# 42 \"/path/to/file\""
True
parseLineMarker :: String -> LineMarker Source #
>>>
parseLineMarker "# 42 \"/path/to/file\""
LineMarker {beginsAtLine = 42, filePath = "/path/to/file"}
data CppOutputComponent Source #
A CppOutputComponent
is constituted by a LineMarker
and the block of
code till the next LineMarker
.
Stages of padding
parseCppOutputComponents :: [String] -> [CppOutputComponent] Source #
Given the lines of a file, parses the CppOutputComponents. Note that a file
that doesn't need cpp preprocessing doesn't have any LineMarker
. In that
case a dummy component is created, with an empty path.
discardUnusefulComponents :: FilePath -> [CppOutputComponent] -> [CppOutputComponent] Source #
Discard the parts of cpp output which correspond to cpp include files. If there's a unique component then we return that one, otherwise we return all the components relative to our file other than the first (which has no real meaning).
reconstructSource :: [CppOutputComponent] -> [String] Source #
Adds padding to the source blocks to mantain the correct line numbers of the source code.