Structural matching of term S to discrimination net node N.
(S, N)
| 372 | |
| 373 | |
| 374 | def _match(S, N): |
| 375 | """Structural matching of term S to discrimination net node N.""" |
| 376 | |
| 377 | stack = deque() |
| 378 | restore_state_flag = False |
| 379 | # matches are stored in a tuple, because all mutations result in a copy, |
| 380 | # preventing operations from changing matches stored on the stack. |
| 381 | matches = () |
| 382 | while True: |
| 383 | if S.current is END: |
| 384 | yield N.patterns, matches |
| 385 | try: |
| 386 | # This try-except block is to catch hashing errors from un-hashable |
| 387 | # types. This allows for variables to be matched with un-hashable |
| 388 | # objects. |
| 389 | n = N.edges.get(S.current, None) |
| 390 | if n and not restore_state_flag: |
| 391 | stack.append((S.copy(), N, matches)) |
| 392 | N = n |
| 393 | S.next() |
| 394 | continue |
| 395 | except TypeError: |
| 396 | pass |
| 397 | n = N.edges.get(VAR, None) |
| 398 | if n: |
| 399 | restore_state_flag = False |
| 400 | matches = matches + (S.term,) |
| 401 | S.skip() |
| 402 | N = n |
| 403 | continue |
| 404 | try: |
| 405 | # Backtrack here |
| 406 | S, N, matches = stack.pop() |
| 407 | restore_state_flag = True |
| 408 | except Exception: |
| 409 | return |
| 410 | |
| 411 | |
| 412 | def _process_match(rule, syms): |