hpython-0.1: Syntax tree and DSL for Python

Copyright(C) CSIRO 2017-2018
LicenseBSD3
MaintainerIsaac Elliott <isaace71295@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Language.Python.Internal.Render.Correction

Description

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

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

correctSpaces :: (PyToken () -> Text) -> [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:

[Char_lit '\\', Char_esc_n] would naively render to '\\n', which would parse to [Char_esc_bslash, Char_lit 'n'], breaking the parse . print identity

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.