コード例 #1
0
 def _create_array(self, template: lark.Tree, header: lark.Tree,
                   indices: t.Sequence[int]) -> lark.Tree:
     actor_header = lark.Tree("actor_header", [header.children[0]])
     items = [
         self._create_item_at_index(template, index) for index in indices
     ]
     items.insert(0, actor_header)
     items.insert(1, Token("COLON", ":"))
     items.append(lark.Tree("actor_footer", [True]))
     return lark.Tree("actor_def", items)
コード例 #2
0
    def outs_literal(self, tree):
        string = eval(str(tree.children[0]))
        for i in range(0, len(string), 8):
            current = string[i:i + 8]
            to_parse = repr(current)

            function_name = lark.Tree('function_name',
                                      [lark.Token('CNAME', 'OUTS')])
            args = lark.Tree(
                'expression_function_arg_list',
                [lark.Tree('string', [lark.Token('STRING', to_parse)])])

            function_call = lark.Tree('function_call', [function_name, args])

            self.visit(function_call)
コード例 #3
0
 def test_find_with_recursion(self):
     mt = _MyGraph(None)
     r1 = lark.Tree('val', '1')
     r2 = lark.Tree('val', '2')
     r3 = lark.Tree('val', '3')
     r4 = lark.Tree('val', '4')
     child1 = _MyGraph(r1)
     child2 = _MyGraph(r2)
     child3 = _MyGraph(r4)
     child1.add_child(child2)
     child1.add_child(child3)
     child2.add_child(child1)
     mt.add_child(child1)
     self.assertEqual(mt.find(r3), None)
     self.assertEqual(mt.find(r4), child3)
コード例 #4
0
 def _create_item_at_index(self, template: lark.Tree,
                           index: int) -> lark.Tree:
     item = copy.deepcopy(template)
     identifier = [
         Token("IDENTIFIER", "item"),
         Token("INTEGER", str(index))
     ]
     identifier_tree = lark.Tree("tokens_identifier", identifier)
     actor_header = lark.Tree("actor_header", [identifier_tree])
     item.children[0] = actor_header
     try:
         loc = item.children.index(Token("COLON", ":")) + 1
     except ValueError:
         item.children.insert(-1, Token("COLON", ":"))
         loc = -1
     item.children.insert(loc, self._create_index_property(index))
     return lark.Tree("sub_actor", [item])
コード例 #5
0
 def test_visitdfs_recursion(self):
     mt = _MyGraph(None)
     r1 = lark.Tree('val', '1')
     r2 = lark.Tree('val', '2')
     r3 = lark.Tree('val', '3')
     r4 = lark.Tree('val', '4')
     child1 = _MyGraph(r1)
     child2 = _MyGraph(r2)
     child3 = _MyGraph(r4)
     child1.add_child(child2)
     child1.add_child(child3)
     child2.add_child(child1)
     mt.add_child(child1)
     order = [child2, child3, child1]
     def visitor(rule):
         self.assertEqual(order.pop(0).get_rule(), rule)
     mt.visit_dfs(visitor)
コード例 #6
0
def tr(name: str, *children: typing.Union[lark.Tree, str]) -> typing.Union[lark.Tree, lark.Token]:
    class Meta:
        def __init__(self):
            self.line = 0
            self.column = 0
            self.end_line = 0
            self.end_column = 0

    return lark.Tree(name, [lark.Token("", x) if isinstance(x, str) else x
                            for x in children], meta=Meta())
コード例 #7
0
    def test_visitdfs_simple(self):
        mt = _MyGraph(None)
        r1 = lark.Tree('val', '1')
        r2 = lark.Tree('val', '2')
        r3 = lark.Tree('val', '3')
        r4 = lark.Tree('val', '4')
        r5 = lark.Tree('val', '5')
        r6 = lark.Tree('val', '6')
        child1 = _MyGraph(r1)
        child2 = _MyGraph(r2)
        child3 = _MyGraph(r3)
        child4 = _MyGraph(r4)
        child5 = _MyGraph(r5)
        child6 = _MyGraph(r6)

        mt.add_child(child1)
        child1.add_child(child2)
        child2.add_child(child3)
        child3.add_child(child4)
        child2.add_child(child5)
        child5.add_child(child6)
        order = [child4, child3, child6, child5, child2, child1]
        def visitor(rule):
            self.assertEqual(order.pop(0).get_rule(), rule)
        mt.visit_dfs(visitor)
コード例 #8
0
 def _create_index_property(self, index: int) -> lark.Tree:
     return lark.Tree("has_property", [
         lark.Tree("has_or_has_meta", [Token("HAS", 'has')]),
         lark.Tree("tokens_identifier",
                   [Token("IDENTIFIER", self.DEPTH_MAP[self.depth])]),
         lark.Tree("single_inheritance", [
             lark.Tree("tokens_identifier",
                       [Token("IDENTIFIER", "integer")])
         ]),
         lark.Tree(
             "initializer",
             [lark.Tree("tokens", [Token("UNSIGNED_INTEGER", str(index))])])
     ])
コード例 #9
0
ファイル: preprocessor.py プロジェクト: Synook/elfhex
 def _merge(self, parsed):
     canonical, _ = parsed[0]
     # Reset children (we don't want fragments in the output).
     merged = lark.Tree(canonical.data, [])
     segments = {}
     metadata = None
     for program, fragments_only in parsed:
         metadata = self._merge_metadata(metadata, program)
         if fragments_only:
             continue
         for segment in program.find_data("segment"):
             name, _, contents, auto_labels = segment.children
             if name in segments:
                 next(segments[name].find_data(
                     "segment_content")).children.extend(contents.children)
                 next(segments[name].find_data(
                     "auto_labels")).children.extend(auto_labels.children)
             else:
                 merged.children.append(segment)
                 segments[name] = segment
     merged.children.insert(0, metadata)
     return merged
コード例 #10
0
    def test_find_simple(self):
        mt = _MyGraph(None)
        r1 = lark.Tree('val', '1')
        r2 = lark.Tree('val', '2')
        r3 = lark.Tree('val', '3')
        r4 = lark.Tree('val', '4')
        r5 = lark.Tree('val', '5')
        r6 = lark.Tree('val', '6')
        child1 = _MyGraph(r1)
        child2 = _MyGraph(r2)
        child3 = _MyGraph(r3)
        child4 = _MyGraph(r4)
        child5 = _MyGraph(r5)
        child6 = _MyGraph(r6)

        mt.add_child(child1)
        child1.add_child(child2)
        child2.add_child(child3)
        child3.add_child(child4)
        child2.add_child(child5)
        child5.add_child(child6)
        self.assertEqual(mt.find(r6), child6)
        self.assertEqual(mt.find(r5), child5)
        self.assertEqual(mt.find(None), mt)
コード例 #11
0
 def __default__(self, data, children):
     return lark.Tree(data, children)
コード例 #12
0
 def null(self, *args):
     if len(args) == 1:
         return args[0]
     return lark.Tree("null", list(args))