チケット #44978

generate_packets.py: unify format of generated code strings

登録: 2022-07-02 06:04 最終更新: 2022-07-05 18:41

報告者:
担当者:
チケットの種類:
状況:
完了
コンポーネント:
マイルストーン:
優先度:
5 - 中
重要度:
5 - 中
解決法:
修正済み
ファイル:
1

詳細

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.

チケットの履歴 (4 件中 3 件表示)

2022-07-02 06:04 更新者: alienvalkyrie
  • 新しいチケット "generate_packets.py: unify format of generated code strings" が作成されました
2022-07-04 01:58 更新者: alienvalkyrie
  • 解決法なし から 受領 に更新されました
コメント

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 更新者: alienvalkyrie
  • 状況オープン から 完了 に更新されました
  • 解決法受領 から 修正済み に更新されました

添付ファイルリスト

編集

このチケットにコメントを追加するには、ログインが必要です » ログインする