本文整理汇总了Python中parsimonious.grammar.Grammar.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.parse方法的具体用法?Python Grammar.parse怎么用?Python Grammar.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parsimonious.grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rewinding
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)
示例2: test_parse_with_leftovers
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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).")
示例3: test_lookahead
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)]))
示例4: test_favoring_named_rules
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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).")
示例5: test_resolve_refs_order
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)')
示例6: test_no_named_rule_succeeding
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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')
示例7: test_line_and_column
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
def test_line_and_column(self):
"""Make sure we got the line and column computation right."""
grammar = Grammar(r"""
whee_lah = whee "\n" lah "\n"
whee = "whee"
lah = "lah"
""")
try:
grammar.parse('whee\nlahGOO')
except ParseError as error:
# TODO: Right now, this says "Rule <Literal "\n" at 0x4368250432>
# didn't match". That's not the greatest. Fix that, then fix this.
ok_(str(error).endswith(r"""didn't match at 'GOO' (line 2, column 4)."""))
示例8: test_right_recursive
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)
示例9: test_parens
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
def test_parens(self):
grammar = Grammar(r'''sequence = "chitty" (" " "bang")+''')
# Make sure it's not as if the parens aren't there:
eq_(grammar.parse('chitty bangbang'), None)
s = 'chitty bang bang'
eq_(str(grammar.parse(s)),
"""<Node called "sequence" matching "chitty bang bang">
<Node matching "chitty">
<Node matching " bang bang">
<Node matching " bang">
<Node matching " ">
<Node matching "bang">
<Node matching " bang">
<Node matching " ">
<Node matching "bang">""")
示例10: test_lookahead
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)]))
示例11: __init__
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
def __init__(self, code):
self.object_query = {}
self.steps = []
# parsing:
grammar = Grammar(QUERY_PEG)
self.__nodes = grammar.parse(code)
self._translate()
示例12: test_lookahead
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)]))
示例13: result
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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]
示例14: test_multi_line
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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)
示例15: test_lazy_default_rule
# 需要导入模块: from parsimonious.grammar import Grammar [as 别名]
# 或者: from parsimonious.grammar.Grammar import parse [as 别名]
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))