Beispiel #1
0
def ast_to_actions_seq(node, rule_names, file_id, parent_id: int, action_id):
    global rules_dict, actions_dict, action_counter, rules_in_files, actions_in_files
    rule_name = Trees.getNodeText(node, rule_names)
    if rule_name not in rules_dict:
        rules_dict[rule_name] = (len(rules_dict), 1, 1)
        rules_in_files[rule_name] = {file_id}
    else:
        rules_in_files[rule_name].add(file_id)
        rules_dict[rule_name] = (rules_dict[rule_name][0],
                                 len(rules_in_files[rule_name]),
                                 rules_dict[rule_name][2] + 1)
    action_str = rule_name
    rule_seq = [[
        action_id, file_id, rules_dict[rule_name][0], parent_id, action_counter
    ]]
    action_counter += 1
    if not isinstance(node, antlr4.tree.Tree.TerminalNodeImpl):
        next_action_id = action_id
        for child in node.getChildren():
            child_subtree_seq = ast_to_actions_seq(child, rule_names, file_id,
                                                   action_id,
                                                   next_action_id + 1)
            rule_seq.extend(child_subtree_seq)
            action_str += '##' + Trees.getNodeText(child, rule_names)
            next_action_id = rule_seq[-1][0]
    if action_str not in actions_dict:
        actions_dict[action_str] = (len(actions_dict), 1, 1)
        actions_in_files[action_str] = {file_id}
    else:
        actions_in_files[action_str].add(file_id)
        actions_dict[action_str] = (actions_dict[action_str][0],
                                    len(actions_in_files[action_str]),
                                    actions_dict[action_str][2] + 1)
    rule_seq[0].append(actions_dict[action_str][0])
    return rule_seq
Beispiel #2
0
def traverse(tree: Tree, file, dot, node_name):
    children = Trees.getChildren(tree)
    for child in children:
        node = Trees.getNodeText(tree, DbQlGrammarParser.ruleNames)
        child_node = Trees.getNodeText(child, DbQlGrammarParser.ruleNames)
        child_name = get_node_name()

        dot.node(node_name, node)
        dot.node(child_name, child_node)

        dot.edge(node_name, child_name)
        traverse(child, file, dot, child_name)
Beispiel #3
0
def process(t, ruleNames):
    global level
    if t.getChildCount() == 0:
        return escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    sb = ""
    sb += lead(level)
    level += 1
    s = escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    sb += (s + ' ')
    for i in range(t.getChildCount()):
        sb += process(t.getChild(i), ruleNames)
    level -= 1
    # sb += lead(level)
    return sb
    def __traverse(self, tree, counter):
        root = Trees.getNodeText(tree, DbQLGrammarParser.ruleNames)
        root_name = str(counter)

        for child in Trees.getChildren(tree):
            child_node = Trees.getNodeText(child, DbQLGrammarParser.ruleNames)
            new_counter = counter + 1
            child_name = str(new_counter)
            
            self.dot.node(root_name, root)
            self.dot.node(child_name, child_node)
            self.dot.edge(root_name, child_name)
            
            counter = self.__traverse(child, new_counter)
        
        return counter
Beispiel #5
0
def get_pretty_tree(
    tree: "ParseTree", rule_names: list = None, parser: Parser = None, level: int = 0
) -> str:
    """Take antlr ``ParseTree`` and return indented tree format for test comparison.

    Adapted from ``antrl4.tree.Trees.toStringTree()`` method.

    Args:
        tree: The antlr parse tree.
        rule_names: Names of parser rules.
        parser: The parser used to generated the tree.
        level: Level of tree (used for indentation).

    Returns:
        Pretty tree format (indents of one space at each level).
    """
    indent_value = "  "  # indent using two spaces to match ``yaml`` reference files

    if parser is not None:
        rule_names = parser.ruleNames

    node_text = Trees.getNodeText(tree, rule_names)
    pretty_tree = level * indent_value + node_text + "\n"

    if tree.getChildCount() > 0:
        for i in range(0, tree.getChildCount()):
            pretty_tree += get_pretty_tree(tree.getChild(i), rule_names=rule_names, level=level + 1)

    return pretty_tree
Beispiel #6
0
 def traverse(self, tree):
     parent_label = Trees.getNodeText(tree, self.parser.ruleNames)
     self.nodes.append((self.stack[-1], parent_label))
     for child in Trees.getChildren(tree):
         self.cnt += 1
         self.edges.append((self.stack[-1], self.cnt))
         self.stack.append(self.cnt)
         self.traverse(child)
         self.stack.pop()
Beispiel #7
0
def treeDesc(t: Tree, p, indent=0):
    ruleNames = p.ruleNames
    s = escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    if t.getChildCount() == 0:
        return s
    with StringIO() as buf:
        buf.write(s + "\n")
        indent += 2
        for i in range(0, t.getChildCount()):
            buf.write(' ' * indent)
            buf.write(treeDesc(t.getChild(i), p, indent) + "\n")
        return buf.getvalue()
def iterate_over_tree(node, rule_names):
    name = Trees.getNodeText(node, ruleNames=rule_names)

    if node.getChildCount() == 0:
        return name

    children = []

    for c in node.getChildren():
        children.append(iterate_over_tree(c, rule_names))

    if node.getChildCount() > 1:
        return {name: children}
    else:
        return {name: children[0]}
Beispiel #9
0
def _pretty_tree_inner(parse_tree: ParseTree, rule_names: list,
                       level: int) -> str:
    """Internal recursive routine used in pretty-printing the parse tree.

    Args:
        parse_tree: a node in the parse tree of the output of the ANTLR parser.
        rule_names: the ANTLR-generated list of rule names in the grammar.
        level: the current indentation level.

    Returns:
        the pretty-printed tree starting from this node, indented correctly.
    """
    indent = "  " * level
    tree = indent + Trees.getNodeText(parse_tree, rule_names) + "\n"
    return tree + "".join(
        _pretty_tree_inner(parse_tree.getChild(i), rule_names, level + 1)
        for i in range(parse_tree.getChildCount()))
Beispiel #10
0
 def process(tree: ParserRuleContext,
             level: int,
             ruleNames: list[str] = None,
             recog: Parser = None) -> str:
     s = Utils.escapeWhitespace(
         Trees.getNodeText(tree, ruleNames=ruleNames, recog=recog), False)
     sb = StringIO()
     sb.write(lead(level))
     if isinstance(tree, TerminalNode):
         sb.write('"')
         sb.write(s)
         sb.write('"')
     else:
         sb.write(s)
         sb.write(' ')
         for child in tree.children:
             sb.write(process(child, level + 1, ruleNames, recog))
     return sb.getvalue()
Beispiel #11
0
    def getTreeString(self, t, ruleNames=None, parser=None):
        if parser is not None:
            ruleNames = parser.ruleNames
        s = self.escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
        c = t.__class__.__name__
        if c.endswith("Context"):
            s = c[:-7]
        if t.getChildCount() == 0:
            return s

        with StringIO() as buf:
            buf.write(u"\n")
            buf.write(u"  " * self.printDepth)
            buf.write(u"")
            buf.write(s)
            buf.write(u' ')
            self.printDepth += 1
            for i in range(0, t.getChildCount()):
                if i > 0:
                    buf.write(u' ')
                buf.write(self.getTreeString(t.getChild(i), ruleNames, parser))
            self.printDepth -= 1
            buf.write(u"")
            return buf.getvalue()
Beispiel #12
0
    from antlr4.tree.Trees import Trees
    from antlr4.Utils import escapeWhitespace

    # from analyzer.solParser import solParser

    results = []
    test_tree = list(Trees.getChildren(tree))[6:7][0]
    builded = build_tree(Trees, test_tree, parser)
    print(builded)

    for line in list(Trees.getChildren(tree))[6:7]:
        res = child_pairs(line)
        res_pairs = [('program', 'line')]
        for r in res:
            if escapeWhitespace(Trees.getNodeText(r[1], None, recog=parser),
                                False) == '\\r\\n':
                continue

            parent = escapeWhitespace(
                Trees.getNodeText(r[0], None, recog=parser), False)
            child = escapeWhitespace(
                Trees.getNodeText(r[1], None, recog=parser), False)

            print(parent, str(r[0]))
            print(child, str(r[1]))

            if parent in ('positive_number', 'row_of_numbers',
                          'natural_number'):
                continue