11#! /usr/bin/env python3
22# This script generates crates/sre_engine/src/constants.rs from Lib/re/_constants.py.
33
4- SCRIPT_NAME = ' scripts/generate_sre_constants.py'
4+ SCRIPT_NAME = " scripts/generate_sre_constants.py"
55
66
77def update_file (file , content ):
88 try :
9- with open (file , 'r' ) as fobj :
9+ with open (file , "r" ) as fobj :
1010 if fobj .read () == content :
1111 return False
1212 except (OSError , ValueError ):
1313 pass
14- with open (file , 'w' ) as fobj :
14+ with open (file , "w" ) as fobj :
1515 fobj .write (content )
1616 return True
1717
18+
1819sre_constants_header = f"""\
1920 /*
2021 * Secret Labs' Regular Expression Engine
@@ -31,6 +32,7 @@ def update_file(file, content):
3132
3233"""
3334
35+
3436def dump_enum (d , enum_name , derives , strip_prefix = "" ):
3537 """Generate Rust enum definitions from a Python dictionary.
3638
@@ -68,8 +70,7 @@ def dump_bitflags(d, prefix, derives, struct_name, int_t):
6870 Returns:
6971 list: A list of strings representing the bitflags definition.
7072 """
71- items = [(value , name ) for name , value in d .items ()
72- if name .startswith (prefix )]
73+ items = [(value , name ) for name , value in d .items () if name .startswith (prefix )]
7374 content = ["bitflags! {\n " ]
7475 content .append (f"{ derives } \n " ) if derives else None
7576 content .append (f" pub struct { struct_name } : { int_t } {{\n " )
@@ -80,6 +81,7 @@ def dump_bitflags(d, prefix, derives, struct_name, int_t):
8081 content .append ("}\n \n " )
8182 return content
8283
84+
8385def main (
8486 infile = "Lib/re/_constants.py" ,
8587 outfile_constants = "crates/sre_engine/src/constants.rs" ,
@@ -92,22 +94,48 @@ def main(
9294 content = [sre_constants_header ]
9395 content .append ("use bitflags::bitflags;\n \n " )
9496 content .append (f"pub const SRE_MAGIC: usize = { ns ['MAGIC' ]} ;\n " )
95- content .extend (dump_enum (ns ["OPCODES" ], "SreOpcode" , "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" ))
96- content .extend (dump_enum (ns ["ATCODES" ], "SreAtCode" , "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" , "AT_" ))
97- content .extend (dump_enum (ns ["CHCODES" ], "SreCatCode" , "#[derive(num_enum::TryFromPrimitive, Debug)]" , "CATEGORY_" ))
98-
99- content .extend (dump_bitflags (ns , "SRE_FLAG_" , "#[derive(Debug, PartialEq, Eq, Clone, Copy)]" , "SreFlag" , "u16" ))
100- content .extend (dump_bitflags (ns , "SRE_INFO_" , "" , "SreInfo" , "u32" ))
101-
102- update_file (outfile_constants , '' .join (content ))
103-
104-
105- if __name__ == '__main__' :
97+ content .extend (
98+ dump_enum (
99+ ns ["OPCODES" ],
100+ "SreOpcode" ,
101+ "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" ,
102+ )
103+ )
104+ content .extend (
105+ dump_enum (
106+ ns ["ATCODES" ],
107+ "SreAtCode" ,
108+ "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" ,
109+ "AT_" ,
110+ )
111+ )
112+ content .extend (
113+ dump_enum (
114+ ns ["CHCODES" ],
115+ "SreCatCode" ,
116+ "#[derive(num_enum::TryFromPrimitive, Debug)]" ,
117+ "CATEGORY_" ,
118+ )
119+ )
120+
121+ content .extend (
122+ dump_bitflags (
123+ ns ,
124+ "SRE_FLAG_" ,
125+ "#[derive(Debug, PartialEq, Eq, Clone, Copy)]" ,
126+ "SreFlag" ,
127+ "u16" ,
128+ )
129+ )
130+ content .extend (dump_bitflags (ns , "SRE_INFO_" , "" , "SreInfo" , "u32" ))
131+
132+ update_file (outfile_constants , "" .join (content ))
133+
134+
135+ if __name__ == "__main__" :
106136 import sys
107- main (* sys .argv [1 :])
108-
109-
110137
138+ main (* sys .argv [1 :])
111139
112140
113141# dump(f, OPCODES, "SreOpcode", "u32", "")
@@ -146,8 +174,6 @@ def main(
146174# print("done")
147175
148176
149-
150-
151177# if __name__ == "__main__":
152178# import sys
153179# if len(sys.argv) > 1:
0 commit comments