From 8e2bbe7327fb6dbc35e5787bbd0eb3a96f35b508 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Sun, 20 Feb 2022 19:13:39 +0100 Subject: [PATCH 5/5] generate_packets.py: Extract packets.def parsing from main See osdn #43951 Signed-off-by: Alina Lenk --- common/generate_packets.py | 43 +++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index bd4cce11e2..330e020cb5 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -1865,6 +1865,9 @@ def get_enum_packet(packets): ''' return intro+body+extro + +####################### Parsing packets.def contents ####################### + def strip_c_comment(s): # The obvious way: # s=re.sub(r"/\*(.|\n)*?\*/","",s) @@ -1877,24 +1880,16 @@ def strip_c_comment(s): result=result+l[1] return result -# Main function. It reads and parses the input and generates the -# various files. -def main(raw_args=None): - ### parsing arguments - global is_verbose - script_args = get_argparser().parse_args(raw_args) - is_verbose = script_args.verbose - - ### parsing input - src_dir = Path(__file__).parent - input_path = src_dir / "networking" / "packets.def" +def parse_packets_def(def_text): + """Parse the given string as contents of packets.def""" - content = input_path.open().read() - content=strip_c_comment(content) - lines=content.split("\n") + def_text = strip_c_comment(def_text) + lines = def_text.split("\n") lines=map(lambda x: re.sub("\s*#.*$","",x),lines) lines=map(lambda x: re.sub("\s*//.*$","",x),lines) lines=filter(lambda x:not re.search("^\s*$",x),lines) + + # parse type alias definitions lines2=[] types=[] for i in lines: @@ -1904,12 +1899,32 @@ def main(raw_args=None): else: lines2.append(i) + # parse packet definitions packets=[] for packet_text in re.split("(?m)^end$","\n".join(lines2)): packet_text = packet_text.strip() if packet_text: packets.append(Packet(packet_text, types)) + # Note: only the packets are returned, as the type aliases + # are not needed any further + return packets + + +# Main function. It reads and parses the input and generates the +# various files. +def main(raw_args=None): + ### parsing arguments + global is_verbose + script_args = get_argparser().parse_args(raw_args) + is_verbose = script_args.verbose + + ### parsing input + src_dir = Path(__file__).parent + input_path = src_dir / "networking" / "packets.def" + + def_text = input_path.open().read() + packets = parse_packets_def(def_text) ### parsing finished ### writing packets_gen.h -- 2.17.1