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
@@ -69,8 +71,7 @@ def dump_bitflags(d, prefix, derives, struct_name, int_t):
6971 Returns:
7072 list: A list of strings representing the bitflags definition.
7173 """
72- items = [(value , name ) for name , value in d .items ()
73- if name .startswith (prefix )]
74+ items = [(value , name ) for name , value in d .items () if name .startswith (prefix )]
7475 content = ["bitflags! {\n " ]
7576 content .append (f"{ derives } \n " ) if derives else None
7677 content .append (f" pub struct { struct_name } : { int_t } {{\n " )
@@ -81,6 +82,7 @@ def dump_bitflags(d, prefix, derives, struct_name, int_t):
8182 content .append ("}\n \n " )
8283 return content
8384
85+
8486def main (
8587 infile = "Lib/re/_constants.py" ,
8688 outfile_constants = "crates/sre_engine/src/constants.rs" ,
@@ -93,22 +95,48 @@ def main(
9395 content = [sre_constants_header ]
9496 content .append ("use bitflags::bitflags;\n \n " )
9597 content .append (f"pub const SRE_MAGIC: usize = { ns ['MAGIC' ]} ;\n " )
96- content .extend (dump_enum (ns ["OPCODES" ], "SreOpcode" , "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" ))
97- content .extend (dump_enum (ns ["ATCODES" ], "SreAtCode" , "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" , "AT_" ))
98- content .extend (dump_enum (ns ["CHCODES" ], "SreCatCode" , "#[derive(num_enum::TryFromPrimitive, Debug)]" , "CATEGORY_" ))
99-
100- content .extend (dump_bitflags (ns , "SRE_FLAG_" , "#[derive(Debug, PartialEq, Eq, Clone, Copy)]" , "SreFlag" , "u16" ))
101- content .extend (dump_bitflags (ns , "SRE_INFO_" , "" , "SreInfo" , "u32" ))
102-
103- update_file (outfile_constants , '' .join (content ))
104-
105-
106- if __name__ == '__main__' :
98+ content .extend (
99+ dump_enum (
100+ ns ["OPCODES" ],
101+ "SreOpcode" ,
102+ "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" ,
103+ )
104+ )
105+ content .extend (
106+ dump_enum (
107+ ns ["ATCODES" ],
108+ "SreAtCode" ,
109+ "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]" ,
110+ "AT_" ,
111+ )
112+ )
113+ content .extend (
114+ dump_enum (
115+ ns ["CHCODES" ],
116+ "SreCatCode" ,
117+ "#[derive(num_enum::TryFromPrimitive, Debug)]" ,
118+ "CATEGORY_" ,
119+ )
120+ )
121+
122+ content .extend (
123+ dump_bitflags (
124+ ns ,
125+ "SRE_FLAG_" ,
126+ "#[derive(Debug, PartialEq, Eq, Clone, Copy)]" ,
127+ "SreFlag" ,
128+ "u16" ,
129+ )
130+ )
131+ content .extend (dump_bitflags (ns , "SRE_INFO_" , "" , "SreInfo" , "u32" ))
132+
133+ update_file (outfile_constants , "" .join (content ))
134+
135+
136+ if __name__ == "__main__" :
107137 import sys
108- main (* sys .argv [1 :])
109-
110-
111138
139+ main (* sys .argv [1 :])
112140
113141
114142# dump(f, OPCODES, "SreOpcode", "u32", "")
@@ -147,8 +175,6 @@ def main(
147175# print("done")
148176
149177
150-
151-
152178# if __name__ == "__main__":
153179# import sys
154180# if len(sys.argv) > 1:
0 commit comments