Visitar URL original
Updated the re library + test by terryluan12 · Pull Request #6648 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Auto-format: cargo fmt --all
  • Loading branch information
github-actions[bot] committed Jan 5, 2026
commit e4bc05c5bfbc01b313ff92bdfadf764f02765bda
1 change: 0 additions & 1 deletion crates/sre_engine/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,3 @@ bitflags! {
const CHARSET = 4;
}
}

68 changes: 47 additions & 21 deletions scripts/generate_sre_constants.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#! /usr/bin/env python3
# This script generates crates/sre_engine/src/constants.rs from Lib/re/_constants.py.

SCRIPT_NAME = 'scripts/generate_sre_constants.py'
SCRIPT_NAME = "scripts/generate_sre_constants.py"


def update_file(file, content):
try:
with open(file, 'r') as fobj:
with open(file, "r") as fobj:
if fobj.read() == content:
return False
except (OSError, ValueError):
pass
with open(file, 'w') as fobj:
with open(file, "w") as fobj:
fobj.write(content)
return True


sre_constants_header = f"""\
/*
* Secret Labs' Regular Expression Engine
Expand All @@ -31,6 +32,7 @@ def update_file(file, content):

"""


def dump_enum(d, enum_name, derives, strip_prefix=""):
"""Generate Rust enum definitions from a Python dictionary.

Expand Down Expand Up @@ -69,8 +71,7 @@ def dump_bitflags(d, prefix, derives, struct_name, int_t):
Returns:
list: A list of strings representing the bitflags definition.
"""
items = [(value, name) for name, value in d.items()
if name.startswith(prefix)]
items = [(value, name) for name, value in d.items() if name.startswith(prefix)]
content = ["bitflags! {\n"]
content.append(f"{derives}\n") if derives else None
content.append(f" pub struct {struct_name}: {int_t} {{\n")
Expand All @@ -81,6 +82,7 @@ def dump_bitflags(d, prefix, derives, struct_name, int_t):
content.append("}\n\n")
return content


def main(
infile="Lib/re/_constants.py",
outfile_constants="crates/sre_engine/src/constants.rs",
Expand All @@ -93,22 +95,48 @@ def main(
content = [sre_constants_header]
content.append("use bitflags::bitflags;\n\n")
content.append(f"pub const SRE_MAGIC: usize = {ns['MAGIC']};\n")
content.extend(dump_enum(ns["OPCODES"], "SreOpcode", "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]"))
content.extend(dump_enum(ns["ATCODES"], "SreAtCode", "#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]", "AT_"))
content.extend(dump_enum(ns["CHCODES"], "SreCatCode", "#[derive(num_enum::TryFromPrimitive, Debug)]", "CATEGORY_"))

content.extend(dump_bitflags(ns, "SRE_FLAG_", "#[derive(Debug, PartialEq, Eq, Clone, Copy)]", "SreFlag", "u16"))
content.extend(dump_bitflags(ns, "SRE_INFO_", "", "SreInfo", "u32"))

update_file(outfile_constants, ''.join(content))


if __name__ == '__main__':
content.extend(
dump_enum(
ns["OPCODES"],
"SreOpcode",
"#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]",
)
)
content.extend(
dump_enum(
ns["ATCODES"],
"SreAtCode",
"#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]",
"AT_",
)
)
content.extend(
dump_enum(
ns["CHCODES"],
"SreCatCode",
"#[derive(num_enum::TryFromPrimitive, Debug)]",
"CATEGORY_",
)
)

content.extend(
dump_bitflags(
ns,
"SRE_FLAG_",
"#[derive(Debug, PartialEq, Eq, Clone, Copy)]",
"SreFlag",
"u16",
)
)
content.extend(dump_bitflags(ns, "SRE_INFO_", "", "SreInfo", "u32"))

update_file(outfile_constants, "".join(content))


if __name__ == "__main__":
import sys
main(*sys.argv[1:])



main(*sys.argv[1:])


# dump(f, OPCODES, "SreOpcode", "u32", "")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these lines supposed to be deleted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... yep. They were deleted in a commit which I believe was lost to the ether on rebase lol

Expand Down Expand Up @@ -147,8 +175,6 @@ def main(
# print("done")




# if __name__ == "__main__":
# import sys
# if len(sys.argv) > 1:
Expand Down