Ejemplo n.º 1
0
 def test_topython_generates_code_for_rep0n(self):
     rep = parsing.Rep0N(ParseTreeStub('a', False))
     ast_ = ast.Module(passes.rule_topython(rep))
     res = codegen.to_source(ast_)
     self.assertEqual(res, ("while True:\n"
                            "    if (not a):\n"
                            "        break"))
Ejemplo n.º 2
0
 def test_topython_generates_code_for_clauses(self):
     clauses = parsing.Seq(
         ParseTreeStub('a', False), ParseTreeStub('b', False))
     res = codegen.to_source(ast.Module(passes.rule_topython(clauses)))
     self.assertEqual(res, ("if (not a):\n"
                            "    return False\n"
                            "if (not b):\n"
                            "    return False"))
Ejemplo n.º 3
0
 def test_topython_generates_code_for_complex_clauses(self):
     clauses = parsing.Seq(ParseTreeStub('a', False),
                           parsing.Seq(ParseTreeStub('b', False)))
     res = codegen.to_source(ast.Module(passes.rule_topython(clauses)))
     self.assertEqual(res, ("if (not a):\n"
                            "    return False\n"
                            "if (not b):\n"
                            "    return False"))
Ejemplo n.º 4
0
 def test_topython_inline_partially_inlinable_clauses(self):
     clauses = parsing.Seq(ParseTreeStub('a', True),
                           ParseTreeStub('b', True),
                           ParseTreeStub('c', False))
     res = codegen.to_source(ast.Module(passes.rule_topython(clauses)))
     self.assertEqual(res, ("if (not (a and b)):\n"
                            "    return False\n"
                            "if (not c):\n"
                            "    return False"))
Ejemplo n.º 5
0
 def test_topython_generates_code_for_rep0n(self):
     rep = parsing.Rep0N(ParseTreeStub('a', False))
     ast_ = ast.Module(passes.rule_topython(rep))
     res = codegen.to_source(ast_)
     self.assertEqual(
         res,
         ("while True:\n"
          "    if (not a):\n"
          "        break"))
Ejemplo n.º 6
0
 def test_topython_inline_partially_inlinable_clauses(self):
     clauses = parsing.Seq(
         ParseTreeStub('a', True),
         ParseTreeStub('b', True),
         ParseTreeStub('c', False))
     res = codegen.to_source(ast.Module(passes.rule_topython(clauses)))
     self.assertEqual(res, ("if (not (a and b)):\n"
                            "    return False\n"
                            "if (not c):\n"
                            "    return False"))
Ejemplo n.º 7
0
 def help(self, rule):
     res = passes.rule_topython(rule)
     stmts = str(res)
     if isinstance(res, list):
         res = ast.Module(res)
     code = codegen.to_source(res)
     return '\n'.join([
         "========= RULE ==========",
         rule.dump_tree(), "========== AST ==========", stmts,
         "========= CODE ==========", code, "========== END =========="
     ])
Ejemplo n.º 8
0
    def test_topython_generates_code_for_parserdsl(self):
        from pprint import pprint
        from pyrser.dsl import EBNF
        import pyrser.passes.dump_tree

        dsl_rules = [
            'bnf_dsl', 'rule', 'alternatives', 'sequences', 'sequence',
            'ns_name', 'repeat', 'hook', 'param'
        ]
        res, parser = [], EBNF()
        #res.append(self.help(parser._rules['alternatives']))
        for rule in dsl_rules:
            res.append(
                codegen.to_source(passes.parserrule_topython(parser, rule)))
Ejemplo n.º 9
0
 def help(self, rule):
     res = passes.rule_topython(rule)
     stmts = str(res)
     if isinstance(res, list):
         res = ast.Module(res)
     code = codegen.to_source(res)
     return '\n'.join([
         "========= RULE ==========",
         rule.dump_tree(),
         "========== AST ==========",
         stmts,
         "========= CODE ==========",
         code,
         "========== END =========="])
Ejemplo n.º 10
0
 def test_topython_generates_code_for_alt(self):
     alt = parsing.Alt(ParseTreeStub('a', False), ParseTreeStub('b', False))
     res = codegen.to_source(ast.Module(passes.rule_topython(alt)))
     self.assertEqual(res, ("try:\n"
                            "    try:\n"
                            "        if (not a):\n"
                            "            raise AltFalse()\n"
                            "        raise AltTrue()\n"
                            "    except AltFalse:\n"
                            "        pass\n"
                            "    try:\n"
                            "        if (not b):\n"
                            "            raise AltFalse()\n"
                            "        raise AltTrue()\n"
                            "    except AltFalse:\n"
                            "        pass\n"
                            "    return False\n"
                            "except AltTrue:\n"
                            "    pass"))
Ejemplo n.º 11
0
 def test_topython_generates_code_for_complex_alt(self):
     self.maxDiff = None
     alt = parsing.Alt(
         ParseTreeStub('a', False),
         parsing.Seq(
             ParseTreeStub('b', False),
             parsing.Alt(
                 ParseTreeStub('c', False),
                 ParseTreeStub('d', False))))
     res = codegen.to_source(ast.Module(passes.rule_topython(alt)))
     self.assertEqual(res, ("try:\n"
                            "    try:\n"
                            "        if (not a):\n"
                            "            raise AltFalse()\n"
                            "        raise AltTrue()\n"
                            "    except AltFalse:\n"
                            "        pass\n"
                            "    try:\n"
                            "        if (not b):\n"
                            "            raise AltFalse()\n"
                            "        try:\n"
                            "            try:\n"
                            "                if (not c):\n"
                            "                    raise AltFalse()\n"
                            "                raise AltTrue()\n"
                            "            except AltFalse:\n"
                            "                pass\n"
                            "            try:\n"
                            "                if (not d):\n"
                            "                    raise AltFalse()\n"
                            "                raise AltTrue()\n"
                            "            except AltFalse:\n"
                            "                pass\n"
                            "            raise AltFalse()\n"
                            "        except AltTrue:\n"
                            "            pass\n"
                            "        raise AltTrue()\n"
                            "    except AltFalse:\n"
                            "        pass\n"
                            "    return False\n"
                            "except AltTrue:\n"
                            "    pass"))
Ejemplo n.º 12
0
    def test_topython_generates_code_for_parserdsl(self):
        from pprint import pprint
        from pyrser.dsl import EBNF
        import pyrser.passes.dump_tree

        dsl_rules = [
            'bnf_dsl',
            'rule',
            'alternatives',
            'sequences',
            'sequence',
            'ns_name',
            'repeat',
            'hook',
            'param']
        res, parser = [], EBNF()
        #res.append(self.help(parser._rules['alternatives']))
        for rule in dsl_rules:
            res.append(codegen.to_source(
                passes.parserrule_topython(parser, rule)))
Ejemplo n.º 13
0
 def test_topython_generates_code_for_inlined_repoptional(self):
     rep = parsing.RepOptional(ParseTreeStub('a', True))
     res = codegen.to_source(passes.rule_topython(rep))
     self.assertEqual(res, "(a or True)")
Ejemplo n.º 14
0
 def test_topython_generates_code_for_repoptional(self):
     rep = parsing.RepOptional(ParseTreeStub('a', False))
     ast_ = ast.Module(passes.rule_topython(rep))
     res = codegen.to_source(ast_)
     self.assertEqual(res, "if (not a):\n"
                           "    pass")
Ejemplo n.º 15
0
 def test_topython_inline_inlinable_clauses(self):
     clauses = parsing.Seq(ParseTreeStub('a', True),
                           ParseTreeStub('b', True))
     res = codegen.to_source(ast.Module(passes.rule_topython(clauses)))
     self.assertEqual(res, "(a and b)")
Ejemplo n.º 16
0
 def test_topython_generates_code_for_hook(self):
     hook = parsing.Hook('hookname', tuple())
     res = codegen.to_source(passes.rule_topython(hook))
     self.assertEqual(res,
                      "self.evalHook('hookname', self.ruleNodes[(-1)])")
Ejemplo n.º 17
0
 def test_topython_generates_code_for_rule(self):
     rule = parsing.Rule('rulename')
     res = codegen.to_source(passes.rule_topython(rule))
     self.assertEqual(res, "self.evalRule('rulename')")
Ejemplo n.º 18
0
 def test_topython_generates_code_for_calltrue(self):
     method = parsing.BasicParser.read_char
     call = parsing.CallTrue(method, 'a')
     res = codegen.to_source(passes.rule_topython(call))
     self.assertEqual(res, "lambda : (self.read_char('a') or True)")
Ejemplo n.º 19
0
 def test_topython_generates_code_for_calltrue(self):
     method = parsing.BasicParser.read_char
     call = parsing.CallTrue(method, 'a')
     res = codegen.to_source(passes.rule_topython(call))
     self.assertEqual(res, "lambda : (self.read_char('a') or True)")
Ejemplo n.º 20
0
 def test_topython_generates_code_for_hook(self):
     hook = parsing.Hook('hookname', tuple())
     res = codegen.to_source(passes.rule_topython(hook))
     self.assertEqual(res,
                      "self.evalHook('hookname', self.ruleNodes[(-1)])")
Ejemplo n.º 21
0
 def test_topython_generates_code_for_rule(self):
     rule = parsing.Rule('rulename')
     res = codegen.to_source(passes.rule_topython(rule))
     self.assertEqual(res, "self.evalRule('rulename')")
Ejemplo n.º 22
0
 def test_topython_generates_code_for_repoptional(self):
     rep = parsing.RepOptional(ParseTreeStub('a', False))
     ast_ = ast.Module(passes.rule_topython(rep))
     res = codegen.to_source(ast_)
     self.assertEqual(res, "if (not a):\n" "    pass")
Ejemplo n.º 23
0
 def test_topython_generates_code_for_inlined_repoptional(self):
     rep = parsing.RepOptional(ParseTreeStub('a', True))
     res = codegen.to_source(passes.rule_topython(rep))
     self.assertEqual(res, "(a or True)")
Ejemplo n.º 24
0
 def test_topython_inline_inlinable_clauses(self):
     clauses = parsing.Seq(
         ParseTreeStub('a', True), ParseTreeStub('b', True))
     res = codegen.to_source(ast.Module(passes.rule_topython(clauses)))
     self.assertEqual(res, "(a and b)")