From 0dce54a83f11f0514facf3f5c639c3511e2278b6 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Thu, 12 May 2022 20:32:25 +0200 Subject: [PATCH 2/4] generate_packets.py: combine comment parsing into one pattern See osdn #44572 This changes the handling of potential edge cases where a block comment is started inside an EOL comment Signed-off-by: Alina Lenk --- common/generate_packets.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index c151764570..cec4bf0f01 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -1996,17 +1996,23 @@ def get_enum_packet(packets): ####################### Parsing packets.def contents ####################### -# matches /* ... */ block comments in multiline text -BLOCK_COMMENT_PATTERN = re.compile(r"/\*.*?\*/", re.DOTALL) -# matches # ... and // ... EOL comments in individual lines -LINE_COMMENT_PATTERN = re.compile(r"\s*(?:#|//).*$") +# matches /* ... */ block comments as well as # ... and // ... EOL comments +COMMENT_PATTERN = re.compile(r""" + (?: # block comment + /\* # initial /* + (?:.|\s)*? # note the reluctant quantifier + \*/ # terminating */ + ) | (?: # EOL comment + (?:\#|//) # initial # or // + .* # does *not* match newline without DOTALL + $ # matches line end in MULTILINE mode + ) +""", re.VERBOSE | re.MULTILINE) def packets_def_lines(def_text): """Yield only actual content lines without comments and whitespace""" - text = BLOCK_COMMENT_PATTERN.sub("", def_text) - lines = (LINE_COMMENT_PATTERN.sub("", line) - for line in text.split("\n")) - return filter(None, map(str.strip, lines)) + text = COMMENT_PATTERN.sub("", def_text) + return filter(None, map(str.strip, text.split("\n"))) def parse_packets_def(def_text): """Parse the given string as contents of packets.def""" -- 2.17.1