@@ -50,61 +50,6 @@ def _var_size(var):
5050StackOffset .push = lambda self ,item :self .pushed .append (_var_size (item ))
5151
5252
53- def group_ranges (it :"Iterable[int]" )-> "Iterator[range]" :
54- """
55- Group consecutive numbers into ranges.
56-
57- Parameters
58- ----------
59- it : Iterable[int]
60- Numbers to group into ranges.
61-
62- Notes
63- -----
64- Numbers in `it` must be sorted in ascending order.
65-
66- Examples
67- --------
68- >>> nums = [0, 1, 2, 3, 17, 18, 42, 50, 51]
69- >>> list(group_ranges(nums))
70- [range(0, 4), range(17, 19), range(42, 43), range(50, 52)]
71- """
72- nums = list (it )
73- start = prev = nums [0 ]
74- for num in nums [1 :]+ [None ]:
75- if num is None or num != prev + 1 :
76- yield range (start ,prev + 1 )
77- start = num
78- prev = num
79-
80-
81- def fmt_ranges (ids :"Iterable[range]" ,* ,min_length :int = 3 )-> str :
82- """
83- Get valid opcode ranges in Rust's `match` syntax.
84-
85- Parameters
86- ----------
87- ids : Iterable[range]
88- Ranges to be formatted.
89- min_length : int, default 3
90- Minimum range length, if a range is less than this it will be expanded.
91-
92- Examples
93- --------
94- >>> ids = [range(10, 11), range(20, 22), range(30, 33)]
95-
96- >>> fmt_ranges(ids)
97- 10 | 20 | 21 | 30..=32
98-
99- >>> fmt_ranges(ids, min_length=2)
100- 10 | 20..=21 | 30..=32
101- """
102- return " | " .join (
103- " | " .join (r )if len (r )< min_length else f"{ r .start } ..={ r .stop - 1 } "
104- for r in ids
105- )
106-
107-
10853def enum_variant_name (name :str )-> str :
10954return name .title ().replace ("_" ,"" )
11055
@@ -154,34 +99,6 @@ def rust_code(self) -> str:
15499}}
155100 """ .strip ()
156101
157- @property
158- def fn_new_unchecked (self )-> str :
159- return f"""
160- /// Creates a new `{ self .enum_name } ` without checking the value is a valid opcode ID.
161- ///
162- /// # Safety
163- ///
164- /// The caller must ensure that `id` satisfies `{ self .enum_name } ::is_valid(id)`.
165- #[must_use]
166- pub const unsafe fn new_unchecked(id:{ self .typ } ) -> Self {{
167- // SAFETY: caller responsibility
168- unsafe {{ std::mem::transmute::<{ self .typ } , Self>(id) }}
169- }}
170- """
171-
172- @property
173- def fn_is_valid (self )-> str :
174- valid_ranges = fmt_ranges (
175- group_ranges (sorted (self ._analysis .opmap [inst .name ]for inst in self ))
176- )
177- return f"""
178- /// Whether the given ID matches one of the opcode IDs.
179- #[must_use]
180- pub const fn is_valid(id:{ self .typ } ) -> bool {{
181- matches!(id,{ valid_ranges } )
182- }}
183- """
184-
185102def build_has_attr_fn (self ,fn_attr :str ,prop_attr :str ,doc_flag :str ):
186103matches = "|" .join (
187104f"Self::{ inst .name } " for inst in self if getattr (inst .properties ,prop_attr )