Copyright | (C) CSIRO 2017-2018 |
---|---|
License | BSD3 |
Maintainer | Isaac Elliott <isaace71295@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
There are configurations of the core syntax tree which won't print to valid Python
if we printed them naively. Many of these we catch in the
Syntax
phase, because those mistakes correspond to
some Python syntax error. In other cases, the mistakes are more benign and have
a "resonable correction" which doesn't break the "print-parse idempotence" law.
This module is where such corrections are defined
Synopsis
- correctParams :: CommaSep (Param v a) -> CommaSep (Param v a)
- correctSpaces :: (PyToken () -> Text) -> [PyToken ()] -> [PyToken ()]
- correctNewlines :: [PyToken ()] -> [PyToken ()]
- correctAdjacentStrings :: NonEmpty (StringLiteral a) -> NonEmpty (StringLiteral a)
- quoteChar :: QuoteType -> PyChar
- quote :: QuoteType -> Char
- correctBackslashEscapes :: [PyChar] -> [PyChar]
- correctBackslashes :: [PyChar] -> [PyChar]
- naps :: (a -> Maybe b) -> [a] -> ([a], [b])
- correctBackslashEscapesRaw :: [PyChar] -> [PyChar]
- correctBackslashesRaw :: [PyChar] -> [PyChar]
- correctQuotes :: QuoteType -> [PyChar] -> [PyChar]
- correctQuotesRaw :: QuoteType -> [PyChar] -> [PyChar]
- correctInitialQuotes :: QuoteType -> [PyChar] -> [PyChar]
- correctInitialFinalQuotesLongRaw :: QuoteType -> [PyChar] -> [PyChar]
- correctInitialFinalQuotesLong :: QuoteType -> [PyChar] -> [PyChar]
- correctTrailingNewline :: HasTrailingNewline s => Bool -> s v a -> s v a
Documentation
correctParams :: CommaSep (Param v a) -> CommaSep (Param v a) Source #
Trailing commas can only be present in a parameter list of entirely positional arguments. This removes the bad trailing comma, and appends the comma's trailing whitespace to the previous token
correctNewlines :: [PyToken ()] -> [PyToken ()] Source #
correctAdjacentStrings :: NonEmpty (StringLiteral a) -> NonEmpty (StringLiteral a) Source #
Two non-typed single-quoted strings cannot be lexically adjacent, because this would be a parse error
eg. '''' or """"
we correct for this by inserting a single space where required '' '' or "" ""
correctBackslashEscapes :: [PyChar] -> [PyChar] Source #
When a backslash character, precedes an escape sequence it needs to be escaped so that it doesn't interfere with the backslash that begins the escape sequence.
For example:
[
would naively render to '\\n', which
would parse to Char_lit
'\\', Char_esc_n][
, breaking the
Char_esc_bslash
, Char_lit
'n']parse . print
identity
correctBackslashes :: [PyChar] -> [PyChar] Source #
naps :: (a -> Maybe b) -> [a] -> ([a], [b]) Source #
(as, bs) = span p xs
bs
is the longest suffix that satisfies the predicate, and as
is the
prefix up to that point
It's like the reverse of span
correctBackslashEscapesRaw :: [PyChar] -> [PyChar] Source #
Sometimes strings need to be corrected when certain characters follow a literal
backslash. For example, a literal backslash followed by an escape sequence means
that the literal backslash actually needs to be escaped, so that it doesn't get
combined
with the backslash in the escape sequence.
correctBackslashesRaw :: [PyChar] -> [PyChar] Source #
It turns out that raw strings can only ever be constructed with an even number of trailing backslash characters. This functon corrects raw strings with an odd number of trailing backslash characters
correctQuotes :: QuoteType -> [PyChar] -> [PyChar] Source #
Every quote in a string of a particular quote type should be escaped
correctQuotesRaw :: QuoteType -> [PyChar] -> [PyChar] Source #
Every quote in short raw string that isn't preceded by a backslash should be escaped
correctInitialQuotes :: QuoteType -> [PyChar] -> [PyChar] Source #
Every third literal quote at the beginning of a long (non-raw) string should be escaped
correctInitialFinalQuotesLongRaw :: QuoteType -> [PyChar] -> [PyChar] Source #
Literal quotes at the beginning and end of a long raw string should be escaped
correctInitialFinalQuotesLong :: QuoteType -> [PyChar] -> [PyChar] Source #
Literal quotes at the beginning and end of a long (non-raw) string should be escaped
correctTrailingNewline :: HasTrailingNewline s => Bool -> s v a -> s v a Source #
It's possible that successive statements have no newlines in between them. This would cause them to be displayed on the same line. In every line where this would be the case, we explicitly insert a line-feed character.