Example #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"))
Example #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"))
Example #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"))
Example #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"))
Example #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"))
Example #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"))
Example #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 =========="
     ])
Example #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)))
Example #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 =========="])
Example #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"))
Example #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"))
Example #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)))
Example #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)")
Example #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")
Example #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)")
Example #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)])")
Example #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')")
Example #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)")
Example #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)")
Example #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)])")
Example #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')")
Example #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")
Example #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)")
Example #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)")