def test_mult(): s = "1024x768" rule = wg.context["mult"] out, i = rule.accept(s, 4, wg.context) assert out == compat.unichr(215) s = "Up to 3x faster" rule = wg.context["mult"] out, i = rule.accept(s, 7, wg.context) assert out == compat.unichr(215) s = "Up to 3x faster" rule = wg.context["para"] out, i = rule.accept(s, 0, wg.context) assert out == {"type": "para", "indent": 0, "text": [u"Up to 3", u"\u00d7", u" faster"]} from bookish.grammars.wiki import mult, para from bookish.parser import condition_string from bookish.parser.bootstrap import bootstrap_context s = condition_string(s) ctx = bootstrap_context() out, i = mult(s, 7, ctx) assert out is not p.Miss assert out == compat.unichr(215) out, i = para(s, 0, ctx) assert out == {"type": "para", "indent": 0, "text": [u"Up to 3", u"\u00d7", u" faster"]}
def build_string(self, content): content = condition_string(content) ctx = bootstrap.bootstrap_context() ctx["add_import"] = self.add_import g = parse_string(content, ctx) g.snap() self.run(g.ruledict.values())
def _boot_parse(inp, **extra): c = bs.bootstrap_context().push(extra) out, i = bs.rule.accept(inp, 0, c) if out is p.Miss: print(i()) assert out is not p.Miss return out.snap(c)
def test_parse_lookbehind(): rulestring = p.Document(""" lookbehind = '^' expr1:e -> LookBehind(e) """) c = bs.bootstrap_context() rule, i = bs.rule.accept(rulestring, 0, c) assert rule.test("^'a'", context=c) == p.LookBehind(p.String("a"))
def parse(avestring): from bookish.grammars.avenue import grammar ctx = bootstrap_context() out, i = grammar(avestring, 0, ctx) if out is Miss: raise AvenueParserError(i) return out
def parse_string(src): from bookish.grammars.wiki import grammar src = condition_string(src) ctx = bootstrap_context() out, i = grammar(src, 0, ctx) assert out is not Miss jsondata = {"type": "root", "body": out, "attrs": {}} return jsondata
def test_parse_mixed_rule(): rulestring = p.Document(""" mixed = ws "@(" expr1:until (ws ',' expr1:e -> e | -> None ):aim ')' -> Mixed(until, aim) """.strip()) c = bs.bootstrap_context() rule, i = bs.rule.accept(rulestring, 0, c) assert i == len(rulestring)
def parser_context(): from bookish.parser import bootstrap ctx = bootstrap.bootstrap_context() def _make_grammar(rulelist, rulename=None): ruledict = make_ruledict(rulelist) g = Grammar(ruledict, context=ctx, rulename=rulename) return g ctx["make_grammar"] = _make_grammar return ctx
def test_parse_regex_rule(): # Use the bootstrap rules to parse the rule rulestring = """ regex = ws "/" (~"/" (("\\\\/" -> "/") | r.Any))*:pattern "/" -> ''.join(pattern) """ c = bs.bootstrap_context() rrule, i = bs.rule.accept(rulestring, 0, c) # rrule.dump() # Use the new rule to parse an example estr = "/(.*?)\\/\\w+/" assert rrule.test(estr, context=c) == "(.*?)/\\w+"
def test_parse_string_rule(): rulestring = p.Document(r""" string = ws "\"" (escchar | ~"\"" r.Any)+:c "\"" -> String(''.join(c)) """.strip()) c = bs.bootstrap_context() rule, i = c["rule"].accept(rulestring, 0, c) assert i == len(rulestring) assert rule.test('"a"', context=c) == p.String("a") assert rule.test('"a"', context=c) == p.String("a") assert rule.test('"ab"', context=c) == p.String("ab") assert rule.test('"ab"', context=c) == p.String("ab")
def test_parse_entity_rule(): # Use the bootstrap rules to parse the rule rulestring = p.Document(""" charhex = 'x' [0123456789abcdefABCDEF]+:h -> int(''.join(h), 16) chardec = [0123456789]+:d -> int(''.join(d)) charnum = (charhex | chardec) entity = "&#" charnum:num ';' -> compat.unichr(num) """.strip()) c = bs.bootstrap_context() rulelist, i = rules.Star(bs.rule).accept(rulestring, 0, c) ruledict = dict((rule.rulename, rule) for rule in rulelist) xc = c.push(ruledict) # Use the new rule to parse an example assert ruledict["entity"].test("Ѐ", context=xc) == u"\u0400" assert ruledict["entity"].test("ဤ", context=xc) == u"\u1024"
def _test_rule(rule, inp): return rule.test(inp, context=bs.bootstrap_context())
def _ctx(): return bs.bootstrap_context()