@@ -31,6 +31,31 @@ def split_args(s: str) -> list[str]:
3131 return args
3232
3333
34+ class Intervals :
35+ def __init__ (self , text : str ):
36+ self .intervals = []
37+ for i in text .split ("," ):
38+ rng = i .split ("-" )
39+ if rng != ["" ]:
40+ start = int (rng [0 ], 0 )
41+ self .intervals .append (
42+ (start , int (rng [1 ], 0 ) if len (rng ) == 2 else start )
43+ )
44+
45+ def contains (self , v : int ) -> bool :
46+ return any (map (lambda x : v >= x [0 ] and v <= x [1 ], self .intervals ))
47+
48+ def __str__ (self ):
49+ rngs = map (
50+ lambda x : f"{ x [0 ]} -{ x [1 ]} " if x [0 ] != x [1 ] else f"{ x [0 ]} " ,
51+ self .intervals ,
52+ )
53+ return "," .join (rngs )
54+
55+ def __repr__ (self ):
56+ return f'Intervals("{ self } ")'
57+
58+
3459class Message :
3560 cpp_prefix : str = "sc::message<sc::undefined"
3661
@@ -152,7 +177,7 @@ def assign_ids_with(items, id_fn):
152177 return list (sorted_items )
153178
154179
155- def assign_ids (messages , modules , stable_data ):
180+ def assign_ids (messages , modules , stable_data , reserved_ids ):
156181 def get_id (stables , gen , obj ):
157182 key = obj .key ()
158183 if key in stables :
@@ -167,7 +192,10 @@ def get_id(stables, gen, obj):
167192 stable_modules [module .key ()] = module
168193
169194 old_msg_ids = set (m .id for m in stable_msgs .values ())
170- msg_id_gen = itertools .filterfalse (old_msg_ids .__contains__ , itertools .count (0 ))
195+ msg_id_gen = itertools .filterfalse (
196+ lambda x : old_msg_ids .__contains__ (x ) or reserved_ids .contains (x ),
197+ itertools .count (0 ),
198+ )
171199 get_msg_id = partial (get_id , stable_msgs , msg_id_gen )
172200
173201 old_module_ids = set (m .id for m in stable_modules .values ())
@@ -182,7 +210,7 @@ def get_id(stables, gen, obj):
182210 )
183211
184212
185- def read_input (filenames : list [str ], stable_data ):
213+ def read_input (filenames : list [str ], stable_data , reserved_ids ):
186214 line_re = re .compile (r"^.*unsigned (?:int|long) (catalog|module)<(.+?)>\(\)$" )
187215
188216 def read_file (filename ):
@@ -199,7 +227,7 @@ def read_file(filename):
199227 modules = filter (lambda x : isinstance (x , Module ), items )
200228 unique_modules = {m .key (): m for m in modules }.values ()
201229
202- return assign_ids (unique_messages , unique_modules , stable_data )
230+ return assign_ids (unique_messages , unique_modules , stable_data , reserved_ids )
203231
204232
205233def make_cpp_scoped_enum_decl (e : str , ut : str ) -> str :
@@ -542,6 +570,12 @@ def parse_cmdline():
542570 default = 127 ,
543571 help = "The maximum value of a module ID." ,
544572 )
573+ parser .add_argument (
574+ "--reserved_ids" ,
575+ type = lambda x : Intervals (x ),
576+ default = "" ,
577+ help = "A list of (inclusive) ranges of string IDs that should be reserved and not used. e.g. '1-5,10-15,20'" ,
578+ )
545579 return parser .parse_args ()
546580
547581
@@ -565,7 +599,7 @@ def main():
565599
566600 stable_data = read_stable (args .stable_json )
567601 try :
568- messages , modules = read_input (args .input , stable_data )
602+ messages , modules = read_input (args .input , stable_data , args . reserved_ids )
569603 except Exception as e :
570604 raise Exception (f"{ str (e )} from file { args .input } " )
571605
0 commit comments