def main(cmdline=None): args = parser.parse_args(cmdline or sys.argv[1:]) data = args.input.read() pparser = peggy.Peggy(args.grammar, debug=args.debug) ast = pparser.parse(data, args.rule) if args.debug: pprint.pprint(ast) if ast['error'] is False: print('Took {} iteration'.format(pparser._count)) if len(data) != ast['nodes'][0]['length']: print(len(data), '!=', ast['nodes'][0]['length']) peggy.astprint(ast['nodes'][0], data) else: pprint.pprint(ast) pprint.pprint(pparser._error)
def test_simple_ko(self, ): parser = peggy.Peggy(grammar) ast = parser.parse('b+b', 'Expr') self.assertTrue(ast['error']) self.assertEqual(ast['length'], 0)
def test_simple_partial(self, ): parser = peggy.Peggy(grammar) ast = parser.parse('1+b', 'Expr') self.assertFalse(ast['error']) self.assertEqual(ast['length'], 1)
def test_simple_ok(self): parser = peggy.Peggy(grammar) ast = parser.parse('1+1', 'Expr') self.assertFalse(ast['error']) self.assertEqual(ast['length'], 3)
def test_simple_ko_6(self, ): parser = peggy.Peggy(grammar) ast = parser.parse('abcc', 'S') self.assertTrue(ast['error']) self.assertEqual(ast['length'], 0)
def test_simple_ok_3(self): parser = peggy.Peggy(grammar) ast = parser.parse('aaabbbccc', 'S') self.assertFalse(ast['error']) self.assertEqual(ast['length'], 9)
def convert(grammar, data, ast): parser = peggy.Peggy(all[grammar]['convert'](ast['nodes'][0], data)) parser.compute_lookahead() return parser
def parse(grammar, data, debug = False): parser = peggy.Peggy(all[grammar]['tree'], debug = debug) ast = parser.parse(data, all[grammar]['rule']) return ast