def test_pickle(protocol): """Ensure that parsers can be pickled""" source = importlib_resources.read_text("bootpeg.grammars", "bpeg.bpeg") parser = boot.boot_parser for _ in range(2): parser = pickle.loads(pickle.dumps(parser, protocol=protocol)) parser = create_parser(source, parser, actions)
def test_unparse(): """Test that unparsing produces valid grammars""" parser = bpeg.parse for _ in range(3): prev_gram = bpeg.unparse(parser) parser = create_parser(prev_gram, parser, actions) assert prev_gram == bpeg.unparse(parser)
def test_parse_reference(): """Parse the PEG reference grammar""" parse = create_parser(peg_grammar, dialect=peg) # reference PEG does not understand results, but bootpeg requires them parse = Parser( Rule(parse.rules[0].name, Transform(parse.rules[0].sub_clause, "()")), *parse.rules[1:], ) assert parse(peg_grammar) == ()
def test_unparse(): """Test that unparsing produces valid grammars""" parser = peg.parse for i in range(3): prev_gram = peg.unparse(parser) parser = create_parser(prev_gram, parser, { **actions, "unescape": peg.unescape }) # grammar is not optimal for peg when coming from bpeg assert i == 0 or prev_gram == peg.unparse(parser)
def test_bootstrap(): source = importlib_resources.read_text("bootpeg.grammars", "bpeg.bpeg") parser = boot.boot_parser # ensure each parser handles itself for _ in range(5): parser = create_parser(source, parser, actions)
def test_not_anything(): """Test that ``Any`` does not match after the end""" not_anything_parse = create_parser("top:\n | a=(.) !. { (a) }\n", bpeg) assert not_anything_parse("b") == "b" with pytest.raises(ParseFailure): not_anything_parse("bb")
def test_multiuse_actions(source): """Test that using the same capture multiple times is valid""" multiuse_parse = create_parser("top:\n | a=(.*) { (a, a) }\n", bpeg) result = multiuse_parse(source) assert result == (source, source)