本文整理汇总了Python中parsimonious.grammar.Grammar类的典型用法代码示例。如果您正苦于以下问题:Python Grammar类的具体用法?Python Grammar怎么用?Python Grammar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Grammar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_right_recursive
def test_right_recursive(self):
"""Right-recursive refs should resolve."""
grammar = Grammar("""
digits = digit digits?
digit = ~r"[0-9]"
""")
ok_(grammar.parse('12') is not None)
示例2: test_rewinding
def test_rewinding(self):
"""Make sure rewinding the stack and trying an alternative (which
progresses farther) from a higher-level rule can blame an expression
within the alternative on failure.
There's no particular reason I suspect this wouldn't work, but it's a
more real-world example than the no-alternative cases already tested.
"""
grammar = Grammar("""
formatted_text = bold_text / weird_text
bold_text = open_parens text close_parens
weird_text = open_parens text "!!" bork
bork = "bork"
open_parens = "(("
text = ~"[a-zA-Z]+"
close_parens = "))"
""")
text = '((fred!!'
try:
grammar.parse(text)
except ParseError as error:
eq_(error.pos, 8)
eq_(error.expr, grammar['bork'])
eq_(error.text, text)
示例3: test_parse_with_leftovers
def test_parse_with_leftovers(self):
"""Make sure ``parse()`` reports where we started failing to match,
even if a partial match was successful."""
grammar = Grammar(r'''sequence = "chitty" (" " "bang")+''')
try:
grammar.parse('chitty bangbang')
except IncompleteParseError as error:
eq_(str(error), "Rule 'sequence' matched in its entirety, but it didn't consume all the text. The non-matching portion of the text begins with 'bang' (line 1, column 12).")
示例4: test_lookahead
def test_lookahead(self):
grammar = Grammar(r'''starts_with_a = &"a" ~"[a-z]+"''')
assert_raises(ParseError, grammar.parse, 'burp')
s = 'arp'
eq_(grammar.parse('arp'), Node('starts_with_a', s, 0, 3, children=[
Node('', s, 0, 0),
Node('', s, 0, 3)]))
示例5: __init__
def __init__(self, code):
self.object_query = {}
self.steps = []
# parsing:
grammar = Grammar(QUERY_PEG)
self.__nodes = grammar.parse(code)
self._translate()
示例6: test_lookahead
def test_lookahead(self):
grammar = Grammar(r'''starts_with_a = &"a" ~"[a-z]+"''')
eq_(grammar.parse('burp'), None)
s = 'arp'
eq_(grammar.parse('arp'), Node('starts_with_a', s, 0, 3, children=[
Node('', s, 0, 0),
Node('', s, 0, 3)]))
示例7: test_lookahead
def test_lookahead(self):
grammar = Grammar(r'''starts_with_a = &"a" ~"[a-z]+"''')
assert_raises(ParseError, grammar.parse, 'burp')
s = 'arp'
eq_(grammar.parse('arp'), Node(grammar['starts_with_a'], s, 0, 3, children=[
Node(Lookahead(Literal('a')), s, 0, 0),
Node(Regex(r'[a-z]+'), s, 0, 3)]))
示例8: result
def result(self):
""" The 'result' property """
g = Grammar("""
condition = always / never / comparison
ws = ~"\s*"
never = ~"never"i
always = ~"always"i
value = numeric / varname
numeric = ~"[+-]?\d+(\.\d+)?"
varname = ~"[a-z_][a-z0-9_]*"i
range = percentage / numeric
percentage = numeric percent_sign
percent_sign = "%"
comparison = range_eq_comparison / range_leftrocket_comparison / range_rightrocket_comparison / range_muchlessthan_comparison / range_muchgreaterthan_comparison / simple_comparison
simple_comparison = value ws simple_comparator ws value
simple_comparator = cmp_eq / cmp_neq / cmp_gte / cmp_gt / cmp_lte / cmp_lt
cmp_eq = "=="
cmp_neq = "!="
cmp_gte = ">="
cmp_gt = ">"
cmp_lte = "<="
cmp_lt = "<"
range_muchlessthan_comparison = value ws range_lt_prev range range_lt_post ws value
range_lt_prev = "<"
range_lt_post = "<"
range_leftrocket_comparison = value ws range_lr_prev range range_lr_post ws value
range_lr_prev = "<"
range_lr_post = "="
range_eq_comparison = value ws range_eq_prev range range_eq_post ws value
range_eq_prev = "="
range_eq_post = "="
range_rightrocket_comparison = value ws range_rr_prev range range_rr_post ws value
range_rr_prev = "="
range_rr_post = ">"
range_muchgreaterthan_comparison = value ws range_gt_prev range range_gt_post ws value
range_gt_prev = ">"
range_gt_post = ">"
""")
tree = g.parse(self._condition)
v = ConditionVisitor(self.data)
return v.visit(tree)[0]
示例9: test_favoring_named_rules
def test_favoring_named_rules(self):
"""Named rules should be used in error messages in favor of anonymous
ones, even if those are rightward-progressing-more, and even if the
failure starts at position 0."""
grammar = Grammar(r'''starts_with_a = &"a" ~"[a-z]+"''')
try:
grammar.parse('burp')
except ParseError as error:
eq_(str(error), "Rule 'starts_with_a' trying to match (&(\"a\") ~\"[a-z]+\"u) didn't match at 'burp' (line 1, column 1).")
示例10: test_resolve_refs_order
def test_resolve_refs_order(self):
"""Smoke-test a circumstance where lazy references don't get resolved."""
grammar = Grammar("""
expression = "(" terms ")"
terms = term+
term = number
number = ~r"[0-9]+"
""")
grammar.parse('(34)')
示例11: test_no_named_rule_succeeding
def test_no_named_rule_succeeding(self):
"""Make sure ParseErrors have sane printable representations even if we
never succeeded in matching any named expressions."""
grammar = Grammar('''bork = "bork"''')
try:
grammar.parse('snork')
except ParseError as error:
eq_(error.pos, 0)
eq_(error.expr, grammar['bork'])
eq_(error.text, 'snork')
示例12: test_multi_line
def test_multi_line(self):
"""Make sure we tolerate all sorts of crazy line breaks and comments in
the middle of rules."""
grammar = Grammar("""
bold_text = bold_open # commenty comment
text # more comment
bold_close
text = ~"[A-Z 0-9]*"i
bold_open = "((" bold_close = "))"
""")
ok_(grammar.parse('((booyah))') is not None)
示例13: test_lazy_default_rule
def test_lazy_default_rule(self):
"""Make sure we get an actual rule set as our default rule, even when
the first rule has forward references and is thus a LazyReference at
some point during grammar compilation.
"""
grammar = Grammar(r"""
styled_text = text
text = "hi"
""")
eq_(grammar.parse('hi'), Node('text', 'hi', 0, 2))
示例14: test_unconnected_custom_rules
def test_unconnected_custom_rules(self):
"""Make sure custom rules that aren't hooked to any other rules still
get included in the grammar and that lone ones get set as the
default.
Incidentally test Grammar's `rules` default arg.
"""
grammar = Grammar(one_char=lambda text, pos: pos + 1).default('one_char')
s = '4'
eq_(grammar.parse(s),
Node('one_char', s, 0, 1))
示例15: test_expressions_from_rules
def test_expressions_from_rules(self):
"""Test the ``Grammar`` base class's ability to compile an expression
tree from rules.
That the correct ``Expression`` tree is built is already tested in
``RuleGrammarTests``. This tests only that the ``Grammar`` base class's
``_expressions_from_rules`` works.
"""
greeting_grammar = Grammar('greeting = "hi" / "howdy"')
tree = greeting_grammar.parse("hi")
eq_(tree, Node("greeting", "hi", 0, 2, children=[Node("", "hi", 0, 2)]))