def test_take(): mg = _metagrammar() gstring = """ paren = "(" <(~")" Any)+>:tag ")" -> tag """.strip() rg = mg.parse(gstring, context=p.parser_context()) paren = rg.ruledict["paren"] out, i = paren.accept("(hi)", 0, p.parser_context()) assert out == "hi"
def test_action(): mg = _metagrammar() gstring = """ x = !(None) """.strip() rg = mg.parse(gstring, context=p.parser_context()) x = rg.ruledict["x"] assert type(x) is p.Do assert x.source == "None"
def test_metaparse_regex(): mg = _metagrammar() print(mg.ruledict) assert "ws" in mg.context gstring = p.Document(""" x = /foo(?P<num>[0-9]+)/ """.strip()) rg = mg.parse(gstring, context=p.parser_context()) assert len(rg.ruledict) == 1 rule = rg.ruledict["x"] assert type(rule) is p.Regex assert rule.match == "foo(?P<num>[0-9]+)" xc = p.ParserContext() out, i = rule.accept("foo123", 0, xc) assert xc["num"] == "123" assert out == "foo123" assert i == 6
def test_repeat(): mg = _metagrammar() gstring = """ y = 'b'{3} z = 'z'{4,} x = 'a'{2,5} """.strip() rg = mg.parse(gstring, context=p.parser_context()) x = rg.ruledict["x"] assert type(x) is p.Repeat assert x.mintimes == 2 assert x.maxtimes == 5 y = rg.ruledict["y"] assert type(y) is p.Repeat assert y.mintimes == 3 assert y.maxtimes == 3 z = rg.ruledict["z"] assert type(z) is p.Repeat assert z.mintimes == 4 assert z.maxtimes is None