def applications_per_slash_with_path(orig_path, slash_count, examine_modes=False): '''Given a category, returns a list whose index _i_ is the rule which consumed its _i_th slash, or None if it was not consumed.''' result = [] for slash in xrange(slash_count): consumer = None # the rule which consumed this slash, if any first = True # We need to copy the path for each slash, because in each iteration we label # the categories in-place. orig_path, path = tee(orig_path, 2) for (prev_l, prev_r, prev_was_flipped), (l, r, was_flipped) in each_pair(path): if first: if prev_was_flipped and prev_r: prev_r.labelled() elif not prev_was_flipped: prev_l.labelled() first = False cur = r if was_flipped else l prev_cur = prev_r if prev_was_flipped else prev_l rule = analyse(prev_l, prev_r, cur, examine_modes) label_result(cur, prev_cur, rule, prev_was_flipped) if rule == 'fwd_appl': consumed_category = prev_l elif rule == 'bwd_appl': consumed_category = prev_r elif rule in ('fwd_comp', 'bwd_comp', 'bwd_xcomp', 'fwd_xcomp'): consumed_category = prev_cur else: consumed_category = None if consumed_category and consumed_category.label == slash: consumer = rule break result.append(consumer) return result
def applications_per_slash_with_path(orig_path, slash_count, examine_modes=False): '''Given a category, returns a list whose index _i_ is the rule which consumed its _i_th slash, or None if it was not consumed.''' result = [] for slash in xrange(slash_count): consumer = None # the rule which consumed this slash, if any first = True # We need to copy the path for each slash, because in each iteration we label # the categories in-place. orig_path, path = tee(orig_path, 2) for (prev_l, prev_r, prev_was_flipped), (l, r, was_flipped) in each_pair(path): if first: if prev_was_flipped and prev_r: prev_r.labelled() elif not prev_was_flipped: prev_l.labelled() first = False cur = r if was_flipped else l prev_cur = prev_r if prev_was_flipped else prev_l rule = analyse(prev_l, prev_r, cur, examine_modes) label_result(cur, prev_cur, rule, prev_was_flipped) if rule == 'fwd_appl': consumed_category = prev_l elif rule == 'bwd_appl': consumed_category = prev_r elif rule in ('fwd_comp', 'bwd_comp', 'bwd_xcomp', 'fwd_xcomp'): consumed_category = prev_cur else: consumed_category = None if consumed_category and consumed_category.label == slash: consumer = rule break result.append( consumer ) return result
def combinators_and_path_from_node(node): path = category_path_to_root(node) for (prev_l, prev_r, _), (l, r, was_flipped) in each_pair(path): l, r, p = prev_l, prev_r, r if was_flipped else l yield analyse(l, r, p), (l, r, p)
def applications_with_path(path): '''Yields a sequence of rule applications applied along a _path_ to the root.''' for (prev_l, prev_r, _), (l, r, was_flipped) in each_pair(path): yield analyse(prev_l, prev_r, r if was_flipped else l)