本文整理汇总了Python中plyplus.plyplus.Grammar.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.parse方法的具体用法?Python Grammar.parse怎么用?Python Grammar.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类plyplus.plyplus.Grammar
的用法示例。
在下文中一共展示了Grammar.parse方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_basic3
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_basic3(self):
g = Grammar("start: '\(' name_list (COMMA MUL NAME)? '\)'; @name_list: NAME | name_list COMMA NAME ; MUL: '\*'; COMMA: ','; NAME: '\w+'; ")
l = g.parse('(a,b,c,*x)')
g = Grammar("start: '\(' name_list (COMMA MUL NAME)? '\)'; @name_list: NAME | name_list COMMA NAME ; MUL: '\*'; COMMA: ','; NAME: '\w+'; ")
l2 = g.parse('(a,b,c,*x)')
assert l == l2, '%s != %s' % (l, l2)
示例2: test_basic1
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_basic1(self):
g = Grammar("start: a+ b a+? 'b' a*; b: 'b'; a: 'a';")
r = g.parse('aaabaab')
self.assertEqual( ''.join(x.head for x in r.tail), 'aaabaa' )
r = g.parse('aaabaaba')
self.assertEqual( ''.join(x.head for x in r.tail), 'aaabaaa' )
self.assertRaises(ParseError, g.parse, 'aaabaa')
示例3: test_basic2
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_basic2(self):
# Multiple parsers and colliding tokens
g = Grammar("start: B A ; B: '12'; A: '1'; ", auto_filter_tokens=False)
g2 = Grammar("start: B A; B: '12'; A: '2'; ", auto_filter_tokens=False)
x = g.parse('121')
assert x.head == 'start' and x.tail == ['12', '1'], x
x = g2.parse('122')
assert x.head == 'start' and x.tail == ['12', '2'], x
示例4: test_recurse_expansion
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_recurse_expansion(self):
"""Verify that stack depth doesn't get exceeded on recursive rules marked for expansion."""
g = Grammar(r"""@start: a | start a ; a : A ; A : 'a' ;""")
# Force PLY to write to the debug log, but prevent writing it to the terminal (uses repr() on the half-built
# STree data structures, which uses recursion).
g._grammar.debug = yacc.NullLogger()
g.parse("a" * (sys.getrecursionlimit() / 4))
示例5: test_expand1_lists_with_one_item
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_expand1_lists_with_one_item(self):
g = Grammar(r"""start: list ;
?list: item+ ;
item : A ;
A: 'a' ;
""")
r = g.parse("a")
# because 'list' is an expand-if-contains-one rule and we only provided one element it should have expanded to 'item'
self.assertSequenceEqual([subtree.head for subtree in r.tail], ('item',))
# regardless of the amount of items: there should be only *one* child in 'start' because 'list' isn't an expand-all rule
self.assertEqual(len(r.tail), 1)
示例6: test_multiple_item_flatten_list
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_multiple_item_flatten_list(self):
g = Grammar(r"""start: list ;
#list: | item ',' list ;
item : A ;
A: 'a' ;
""")
r = g.parse("a,a,")
# Because 'list' is a flatten rule it's top-level element should *never* be expanded
self.assertSequenceEqual([subtree.head for subtree in r.tail], ('list',))
# Sanity check: verify that 'list' contains exactly the two 'item's we've given it
[list] = r.tail
self.assertSequenceEqual([item.head for item in list.tail], ('item', 'item'))
示例7: test_empty_expand1_list_2
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_empty_expand1_list_2(self):
g = Grammar(r"""start: list ;
?list: item* '!'?;
item : A ;
A: 'a' ;
""")
r = g.parse("")
# because 'list' is an expand-if-contains-one rule and we've provided less than one element (i.e. none) it should *not* have expanded
self.assertSequenceEqual([subtree.head for subtree in r.tail], ('list',))
# regardless of the amount of items: there should be only *one* child in 'start' because 'list' isn't an expand-all rule
self.assertEqual(len(r.tail), 1)
# Sanity check: verify that 'list' contains no 'item's as we've given it none
[list] = r.tail
self.assertSequenceEqual([item.head for item in list.tail], ())
示例8: test_dont_expand1_lists_with_multiple_items_2
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_dont_expand1_lists_with_multiple_items_2(self):
g = Grammar(r"""start: list ;
?list: item+ '!';
item : A ;
A: 'a' ;
""")
r = g.parse("aa!")
# because 'list' is an expand-if-contains-one rule and we've provided more than one element it should *not* have expanded
self.assertSequenceEqual([subtree.head for subtree in r.tail], ('list',))
# regardless of the amount of items: there should be only *one* child in 'start' because 'list' isn't an expand-all rule
self.assertEqual(len(r.tail), 1)
# Sanity check: verify that 'list' contains the two 'item's we've given it
[list] = r.tail
self.assertSequenceEqual([item.head for item in list.tail], ('item', 'item'))
示例9: setUp
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def setUp(self):
tree_grammar = Grammar("start: branch; branch: name ('{' branch* '}')?; name: '[a-z]';")
self.tree1 = tree_grammar.parse("a{b{cde}}")
self.tree2 = tree_grammar.parse("a{abc{bbab}c}")
示例10: test_unicode
# 需要导入模块: from plyplus.plyplus import Grammar [as 别名]
# 或者: from plyplus.plyplus.Grammar import parse [as 别名]
def test_unicode(self):
g = Grammar(r"""start: UNIA UNIB UNIA;
UNIA: '\xa3';
UNIB: '\u0101';
""")
g.parse(u'\xa3\u0101\u00a3')