示例#1
0
def main():
    grammar = AttributeGrammar([Parent, Child])

    from pprint import pprint
    pprint(grammar.grammar_info())

    pprint("Local Functional Dependencies")
    for i, j in grammar.local_functional_dependence.items():
        pprint(i)

        for jj in j:
            pprint(jj, indent=2)

        print()

    pprint("Lower Dependencies")
    pprint(grammar.lower_dependence)

    print()
    print(grammar.is_acyclic)
    print(grammar.is_absolutely_acyclic)

    array = [4, 8, 2, 24, 32]

    root = Parent(*map(Child, array))
    root = StaticEvaluator(grammar).for_tree(root)

    expected = []
    tmp = max(array)
    for a in array:
        tmp = tmp / 2 + a
        expected.append(tmp)

    print(root.attenuated_sums)
    print(expected)
示例#2
0
class EvaluatorStaticTest(unittest.TestCase, EvaluatorTestCases):
    grammar_scopes = AttributeGrammar.from_modules(scopes, rule_extractor=Parser())
    grammar_arithmetic = AttributeGrammar.from_modules(arithmetic, rule_extractor=Parser())

    evaluator_scopes = StaticEvaluator(grammar_scopes)
    evaluator_arithmetic = StaticEvaluator(grammar_arithmetic)

    def test_arithmetic(self):
        for e, r, t, d in self.expressions:
            ast = arithmetic.parse_rpn(e)
            ast.depth = 0
            ast.index = None
            ast = self.evaluator_arithmetic.for_tree(ast).content
            self.assertEqual(ast.value, r)
            self.assertEqual(ast.type, t)
            self.assertEqual(ast.subtree_depth, d)

    def test_scopes(self):
        for e in self.ok:
            ast = scopes_parser.parse(e)
            ast.same = []
            ast.env = {}

            self.assertTrue(self.evaluator_scopes.for_tree(ast).ok)

        for e in self.not_ok:
            ast = scopes_parser.parse(e)
            ast.same = []
            ast.env = {}

            self.assertFalse(self.evaluator_scopes.for_tree(ast).ok, e)
示例#3
0
def main():
    from securify.grammar.attributes.evaluators.evaluator import DemandDrivenRecursive

    grammar = AttributeGrammar([
        ASTNode,
        Root,
        # Other,
        Term,
        Addition,
        Multiplication,
        Constant,
        Sum,
    ], rule_extractor=Parser())

    from pprint import pprint
    info = grammar.grammar_info()
    pprint(grammar.grammar_info(), width=120, indent=2)

    root = Sum(
        Multiplication(Addition(Constant(1), Constant(2)), Constant(2.5)),
        Constant(2),
        Constant(2),
        Constant(2),
        Constant(5)
    )

    root.implicit_pushdown = "Works!"
    root.depth = 0
    root.index = None

    root = DemandDrivenRecursive(grammar).for_tree(root)

    print()
    print("Results:")

    print("Root value", root.value)
    print("Root string", root.string)

    print("Root string", root.subtree_depth)
    print("Root lhs depth", root.addends[1].depth)
    print("Root lhs depth", root.addends[0].lhs.depth)
    print("Root lhs max subtree", root.subtree_depth)

    print("Implicit Attribute", root.addends[0].implicit_pushdown)

    print(DemandDrivenRecursive(grammar).for_tree(Root(Constant(1))).content.depth)
    # print(DemandDriven(grammar).for_tree(Root(Other())).content.comment)

    print("Index", root.addends[0].index)
    print("Index", root.addends[1].index)
    print("Index", root.addends[2].index)
    print("Index", root.addends[3].index)
    print("Index", root.addends[4].index)

    print(root.set_in_module)
def main():
    grammar = AttributeGrammar([
        NodeBase,
        NodeA,
        NodeB,
        NodeC,
        NodeD
    ], Parser())

    from pprint import pprint
    pprint(grammar.grammar_info())
class AttributeDecoratorsTest(unittest.TestCase):
    grammar = AttributeGrammar([Rule, Child], rule_extractor=Parser())

    # pprint(grammar.grammar_info())

    def test(self):
        rules_s = self.grammar.synthesized_rules[Rule]

        self.assertEqual(rules_s[("self", "attr1")][0].arguments[0].node,
                         "self")
        self.assertEqual(rules_s[("self", "attr2")][0].arguments[0].node,
                         "self")
        self.assertEqual(rules_s[("self", "attr3")][0].arguments[0].node,
                         "self")
        self.assertEqual(rules_s[("self", "attr4")][0].arguments[0].node,
                         "self")

        self.assertIn(("self", "attr1"),
                      rules_s[("self", "attr2")][0].dependencies)
        self.assertIn(("self", "attr2"),
                      rules_s[("self", "attr3")][0].dependencies)
        self.assertIn(("self", "attr3"),
                      rules_s[("self", "attr4")][0].dependencies)

        self.assertEqual(len(rules_s[("self", "attr4")][0].arguments), 3)
        self.assertEqual(len(rules_s[("self", "attr4")][0].dependencies), 2)
        self.assertIn(("self", "attr3"),
                      rules_s[("self", "attr4")][0].dependencies)
        self.assertIn(("node", "some_attribute"),
                      rules_s[("self", "attr4")][0].dependencies)
示例#6
0
class DefaultAttributesTest(unittest.TestCase, EvaluatorTestCases):
    grammar = AttributeGrammar.from_modules(defaults, rule_extractor=Parser())

    def test(self):
        ast = Parent(ChildA(ChildB()), ChildA(None))
        ast = StaticEvaluator(self.grammar).for_tree(ast)

        self.assertEqual(ast.default, "Default Synth")

        self.assertEqual(ast.child1.attr, "Success")
        self.assertEqual(ast.child1.child.attr, "Success")
示例#7
0
class AttributeVisitorTest(unittest.TestCase):
    grammar = AttributeGrammar.from_modules(arithmetic,
                                            rule_extractor=Parser())
    visitor = grammar.attribute_visitor()

    def test_attribute_visitor(self):
        tree = arithmetic.parse_rpn("1 2 + 2.5 * 2 5 SUM")

        tree.depth = 0
        tree.index = None

        tree = DemandDrivenRecursive(self.grammar).for_tree(tree)

        def print_attribute(node, attribute):
            getattr(node, attribute)

        self.visitor.visit(tree, print_attribute)
class AttributeDecoratorsTest(unittest.TestCase):
    grammar = AttributeGrammar([SymbolRule], rule_extractor=Parser())

    # pprint(grammar.grammar_info())

    def test(self):
        rules_i = self.grammar.inheritable_rules[SymbolRule]
        rules_s = self.grammar.synthesized_rules[SymbolRule]
        self.assertEqual(rules_i[("child", "attr3")][0].arguments[0].node, "self")
        self.assertEqual(rules_i[("child", "attr3")][0].arguments[1].node, "node")

        self.assertIn(("node", "attr1"), rules_i[("child", "attr3")][0].dependencies)
        self.assertIn(("node", "attr2"), rules_i[("child", "attr3")][0].dependencies)

        self.assertEqual(rules_i[("child", "attr3")][0].target, ("child", "attr3"))

        self.assertEqual(rules_s[("self", "test_synthesized")][0].arguments[0].node, "self")
        self.assertEqual(rules_s[("self", "test_synthesized")][0].arguments[1].node, "node")

        self.assertIn(("node", "attr1"), rules_s[("self", "test_synthesized")][0].dependencies)
        self.assertIn(("node", "attr2"), rules_s[("self", "test_synthesized")][0].dependencies)
示例#9
0
def main():
    from tests.grammar.grammars import scopes

    grammar = AttributeGrammar.from_modules(scopes, rule_extractor=Parser())

    # print(grammar.synthesized_attributes)
    # print(grammar.inheritable_attributes)

    from tests.grammar.grammars.scopes_parser import parse
    ast = parse(program)
    # dump_ast(grammar.grammar_info())

    grammar.traverse(ast, lambda n, a, c: setattr(n, "_id", id(n)))

    ast.same = []
    ast.env = {}

    ast_original = ast

    ast = DemandDrivenIterative(grammar).for_tree(ast)

    print(ast)
    print(ast.ok)
    print(ast.procs)

    from pprint import pprint

    pprint(grammar.grammar_info())
    print()
    print("# Local Functional Dependencies")
    pprint({
        a.__name__: set(b)
        for a, b in grammar.local_functional_dependence.items()
    })
    print()
    print("# Lower Dependence Relations")
    pprint({a.__name__: set(b) for a, b in grammar.lower_dependence.items()})

    print()
    print("# Lower Dependence All")
    pprint({
        a.__name__: set(b)
        for a, b in grammar.lower_dependence_combined.items()
    })
    print()
    # DictTransformer(grammar)
    #
    #
    # ast.env = {}
    # ast.same = []

    # ast = DemandDriven(ast).root
    # print(ast.ok)

    evaluated = StaticEvaluator(grammar).for_tree(ast_original)

    def attribute_dict(tree):
        result = {}

        def fill_dict(node, attribute):
            result[(node._id, attribute)] = getattr(node, attribute)

        grammar.attribute_visitor().visit(tree, fill_dict)

        return result

    for a in attribute_dict(evaluated):
        print(attribute_dict(ast)[a])
        print(attribute_dict(evaluated)[a])

    pprint(attribute_dict(evaluated) == attribute_dict(ast))

    pass
from __future__ import annotations

from securify.grammar import production
from securify.grammar.attributes import AttributeGrammar
from securify.grammar.attributes.annotations3 import synthesized, Parser
from securify.grammar.attributes.evaluators import DemandDrivenRecursive


@production
class Test:
    print = synthesized()
    test = synthesized()

    @synthesized
    def print(self):
        print("this should be printed once!")
        return 0

    @synthesized
    def test(self: {Test.print}):
        return "this should be printed after the above string"


grammar = AttributeGrammar([Test], Parser())

root = DemandDrivenRecursive(grammar).for_tree(Test())

print(root.test)
print(root.print)
示例#11
0
class EvaluatorConsistencyTest(unittest.TestCase, EvaluatorTestCases):
    grammar_scopes = AttributeGrammar.from_modules(scopes, rule_extractor=Parser())
    grammar_arithmetic = AttributeGrammar.from_modules(arithmetic, rule_extractor=Parser())

    evaluator_scopes_1 = StaticEvaluator(grammar_scopes)
    evaluator_arithmetic_1 = StaticEvaluator(grammar_arithmetic)

    evaluator_scopes_2 = DemandDrivenRecursive(grammar_scopes)
    evaluator_arithmetic_2 = DemandDrivenRecursive(grammar_arithmetic)

    def test_arithmetic(self):
        for e, r, t, d in self.expressions:
            ast = arithmetic.parse_rpn(e)
            ast.depth = 0
            ast.index = None

            self.mark_nodes(self.grammar_arithmetic, ast)
            self.same_evaluations(ast,
                                  self.evaluator_arithmetic_1,
                                  self.evaluator_arithmetic_2)

    def test_scopes(self):
        for e in self.ok:
            ast = scopes_parser.parse(e)
            ast.same = []
            ast.env = {}

            self.mark_nodes(self.grammar_scopes, ast)
            self.same_evaluations(ast,
                                  self.evaluator_scopes_1,
                                  self.evaluator_scopes_2)

        for e in self.not_ok:
            ast = scopes_parser.parse(e)
            ast.same = []
            ast.env = {}

            self.mark_nodes(self.grammar_scopes, ast)
            self.same_evaluations(ast,
                                  self.evaluator_scopes_1,
                                  self.evaluator_scopes_2)

    def same_evaluations(self, tree, evaluator1, evaluator2):
        evaluated1 = evaluator1.for_tree(tree)
        evaluated2 = evaluator2.for_tree(tree)
        d1 = self.attribute_dict(evaluator1.grammar, evaluated1)
        d2 = self.attribute_dict(evaluator2.grammar, evaluated2)

        self.assertEqual(d1, d2)

    @staticmethod
    def mark_nodes(grammar, tree):
        # Required bc. node ids change after being passed through evaluators
        grammar.traverse(tree, lambda n, c, a: setattr(n, "node_id", id(n)))

    @staticmethod
    def attribute_dict(grammar, tree):
        result = {}

        def fill_dict(node, attribute):
            result[(node.node_id, attribute)] = getattr(node, attribute)

        grammar.attribute_visitor().visit(tree, fill_dict)

        return result
示例#12
0
@dataclass(eq=False)
class ChildA:
    child: Optional[ChildB]
    attr = inherited(default="Success")
    listA = inherited(default=[])
    listB = inherited(default=lambda: [])


@production
@dataclass(eq=False)
class ChildB:
    attr = inherited()


if __name__ == '__main__':
    grammar = AttributeGrammar([Parent, ChildA, ChildB],
                               rule_extractor=Parser())

    ast = Parent(ChildA(ChildB()), ChildA(None))
    ast = StaticEvaluator(grammar).for_tree(ast)

    print(ast.default)

    print(ast.child1.attr)
    print(ast.child1.child.attr)

    print(ast.child1.listA, ast.child1.listB)
    ast.child1.listA.append(1)
    ast.child1.listB.append(1)
    print(ast.child1.listA, ast.child1.listB)
    print(ast.child2.listA, ast.child2.listB)