Ticket #44978

generate_packets.py: unify format of generated code strings

Open Date: 2022-07-02 06:04 Last Update: 2022-07-05 18:41

Reporter:
Owner:
Type:
Status:
Closed
Component:
MileStone:
Priority:
5 - Medium
Severity:
5 - Medium
Resolution:
Fixed
File:
1

Details

Part of #43927. Make all strings representing generated code have the same general shape so they can all be worked with the same way.

The style that seemed to work well when prototyping is as follows:

Functionally, strings that represent zero or more entire lines of code should

  • have a line break at the end of each line (including the final line)
  • have no line break before the first line

or, equivalently

  • start with no line break (unless semantically starting with an empty line)
  • end with a line break (unless empty)

Stylistically, strings that represent one or more entire lines of code should

  • be multiline strings wrapped in """threefold double quotes"""
  • have each line of C code be on its own line in the Python code. Accordingly
    • the opening quotes must be followed by an escaped line break, i.e. """\ at the start of the string
    • the closing quotes must be at the very beginning of a line after the last line of C code
  • have each other, inserted string of lines of code be on its own line in the Python code
    • followed by an escaped line break, since the inserted string comes with its own final line break
  • not contain \n, but rather write out blank lines explicitly
  • not escape ' and " that occur in the C code

The empty string should be written as "" (using double quotes, not single quotes).

Functionally, strings that represent an expression that is not an entire line should

  • contain no line breaks (since we'd need a more advanced prefix() function for those)
  • have no padding or indentation

Stylistically, strings that represent an expression should

  • be single-line strings wrapped in "double quotes"
  • escape " that occur in the C code (rather than changing the quotes of the string itself)

Example:

  1. def an_expression(foobar) -> str:
  2. return "{foobar.func}({foobar.baz})".format(foobar = foobar)
  3. def some_lines(foobar: Foobar) -> str:
  4. return """\
  5. defoo(foos[{foobar.foo}]);
  6. foos[{foobar.foo}] = bars[{foobar.bar}].foo;
  7. enfoo(foos[{foobar.foo}]);
  8. """.format(foobar = foobar)
  9. def some_more_lines(foobar: Foobar) -> str:
  10. condition = an_expression(foobar)
  11. inner = prefix(" ", some_lines(foobar))
  12. return """\
  13. if ({condition}) {{
  14. log("Re-fooing {foobar.name}...")
  15. {inner}\
  16. re_foos++;
  17. }}
  18. """.format(foobar = foobar, condition = condition, inner = inner)

Note: All code being written as un-indented as possible (as in this example) is covered by #44977.

Ticket History (3/4 Histories)

2022-07-02 06:04 Updated by: alienvalkyrie
  • New Ticket "generate_packets.py: unify format of generated code strings" created
2022-07-04 01:58 Updated by: alienvalkyrie
  • Resolution Update from None to Accepted
Comment

As a side effect (or rather, the conscious decision to not always avoid those side effects), this patch also gets rid of a significant number of unnecessary/"erroneous" blank lines introduced by unescaped line breaks at the beginning of certain multiline strings (that didn't fit the surrounding pattern ~> likely weren't intended).

2022-07-05 18:41 Updated by: alienvalkyrie
  • Status Update from Open to Closed
  • Resolution Update from Accepted to Fixed

Edit

Please login to add comment to this ticket » Login