def assign_links(self, results, tokens, tree):
# Create the lists of significant tokens and results.
sig_tokens = list(filter(self.is_significant, tokens))
sig_results = list(filter(self.is_significant, results))
# Raise an exception if the two lists are not compatible.
self.check(sig_results, sig_tokens)
# Make two-way links between tokens and results.
for r, t in zip(sig_results, sig_tokens):
self.set_links(r, t, tokens)
tx = 0 # The index of the last patched token.
def set_links(self, r, t, tokens):
# Patch all previous assignable tokens.
while self.tx <= t.index:
token = tokens[self.tx]
if self.should_be_assigned(token, r.node):
# Patch the token.
token.node = r.node
# Add the token to r.node.token_list.
token_list = getattr(r.node, 'token_list', [])
r.node.token_list = token_list + [token]
self.tx += 1
def check(self, results, tokens):
n1, n2 = len(results), len(tokens)
assert n1 == n2, (n1, n2)
for r, t in zip(results, tokens):
assert r.kind == t.kind, (repr(r), repr(t))
assert self.compare_values(r, t), (repr(r), repr(t))