|
21 | 21 | ) |
22 | 22 | from mathics.core.characters import letters, letterlikes |
23 | 23 |
|
24 | | - |
| 24 | +type_compiled_pattern = type(re.compile("a.a")) |
25 | 25 | names_wildcards = "@*" |
26 | 26 | base_names_pattern = r"((?![0-9])([0-9${0}{1}{2}])+)".format( |
27 | 27 | letters, letterlikes, names_wildcards |
@@ -313,37 +313,38 @@ def get_matching_names(self, pattern) -> typing.List[str]: |
313 | 313 | which aren't uppercase letters. In the context pattern, both |
314 | 314 | '*' and '@' match context marks. |
315 | 315 | """ |
316 | | - |
317 | | - if re.match(full_names_pattern, pattern) is None: |
318 | | - # The pattern contained characters which weren't allowed |
319 | | - # in symbols and aren't valid wildcards. Hence, the |
320 | | - # pattern can't match any symbols. |
321 | | - return [] |
322 | | - |
323 | | - # If we get here, there aren't any regexp metacharacters in |
324 | | - # the pattern. |
325 | | - |
326 | | - if "`" in pattern: |
327 | | - ctx_pattern, short_pattern = pattern.rsplit("`", 1) |
328 | | - ctx_pattern = ( |
329 | | - (ctx_pattern + "`") |
330 | | - .replace("@", "[^A-Z`]+") |
331 | | - .replace("*", ".*") |
332 | | - .replace("$", r"\$") |
333 | | - ) |
| 316 | + if isinstance(pattern, type_compiled_pattern): |
| 317 | + regex = pattern |
334 | 318 | else: |
335 | | - short_pattern = pattern |
336 | | - # start with a group matching the accessible contexts |
337 | | - ctx_pattern = "(?:%s)" % "|".join( |
338 | | - re.escape(c) for c in self.get_accessible_contexts() |
339 | | - ) |
340 | | - |
341 | | - short_pattern = ( |
342 | | - short_pattern.replace("@", "[^A-Z]+") |
343 | | - .replace("*", "[^`]*") |
344 | | - .replace("$", r"\$") |
345 | | - ) |
346 | | - regex = re.compile("^" + ctx_pattern + short_pattern + "$") |
| 319 | + if re.match(full_names_pattern, pattern) is None: |
| 320 | + # The pattern contained characters which weren't allowed |
| 321 | + # in symbols and aren't valid wildcards. Hence, the |
| 322 | + # pattern can't match any symbols. |
| 323 | + return [] |
| 324 | + |
| 325 | + # If we get here, there aren't any regexp metacharacters in |
| 326 | + # the pattern. |
| 327 | + |
| 328 | + if '`' in pattern: |
| 329 | + ctx_pattern, short_pattern = pattern.rsplit('`', 1) |
| 330 | + if ctx_pattern == "": |
| 331 | + ctx_pattern="System`" |
| 332 | + else: |
| 333 | + ctx_pattern = ((ctx_pattern + '`') |
| 334 | + .replace('@', '[^A-Z`]+') |
| 335 | + .replace('*', '.*') |
| 336 | + .replace('$', r'\$')) |
| 337 | + else: |
| 338 | + short_pattern = pattern |
| 339 | + # start with a group matching the accessible contexts |
| 340 | + ctx_pattern = "(?:%s)" % "|".join( |
| 341 | + re.escape(c) for c in self.get_accessible_contexts()) |
| 342 | + |
| 343 | + short_pattern = (short_pattern |
| 344 | + .replace('@', '[^A-Z]+') |
| 345 | + .replace('*', '[^`]*') |
| 346 | + .replace('$', r'\$')) |
| 347 | + regex = re.compile('^' + ctx_pattern + short_pattern + '$') |
347 | 348 |
|
348 | 349 | return [name for name in self.get_names() if regex.match(name)] |
349 | 350 |
|
|
0 commit comments