Exemple #1
0
def score(tree: Tree):
    """
    Scores an option by how many children (and grand-children, and
    grand-grand-children, ...) it has.
    This means that the option with fewer large terminals get's selected

    Between
        object
          pair
            string	_foo_
            object
          pair
            string	_bar_: [], _baz_
            string	__

    and

        object
          pair
            string	_foo_
            object
          pair
            string	_bar_
            array
          pair
            string	_baz_
            string	__

    this will give the second a higher score. (9 vs 13)
    """
    return sum(len(t.children) for t in tree.iter_subtrees())
Exemple #2
0
def _unnecessary_pass_check(parse_tree: Tree) -> List[Problem]:
    problems = []
    for node in parse_tree.iter_subtrees():
        if isinstance(node, Tree):
            pass_stmts = _find_stmts_among_children(tree=node,
                                                    suffix="pass_stmt")
            all_stmts = _find_stmts_among_children(tree=node, suffix="_stmt")
            if len(pass_stmts) < len(all_stmts):
                for pass_stmt in pass_stmts:
                    problems.append(
                        Problem(
                            name="unnecessary-pass",
                            description='"pass" statement not necessary',
                            line=pass_stmt.line,
                            column=pass_stmt.column,
                        ))
    return problems
def _gather_rule_name_tokens(parse_tree: Tree,
                             rules,
                             predicate=lambda _: True) -> Dict[str, List[str]]:
    name_tokens_per_rule = {rule: []
                            for rule in rules}  # type: Dict[str, List[str]]
    for node in parse_tree.iter_subtrees():
        if isinstance(node, Tree) and node.data in rules:
            rule_name = node.data
            name_token = find_name_token_among_children(node)
            if name_token is None:
                name_token = find_name_token_among_children(node.children[0])
                predicate_outcome = predicate(node.children[0])
            else:
                predicate_outcome = predicate(node)
            assert name_token is not None
            if predicate_outcome:
                name_tokens_per_rule[rule_name].append(name_token)
    return name_tokens_per_rule
Exemple #4
0
def count_nodes(tree: lark.Tree) -> int:
    """Used to get the number of parent nodes in the tree"""
    return sum(len(x.children) > 0 for x in tree.iter_subtrees())