本文整理汇总了Python中plyplus.plyplus.Grammar类的典型用法代码示例。如果您正苦于以下问题:Python Grammar类的具体用法?Python Grammar怎么用?Python Grammar使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Grammar类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_basic3
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
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
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
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_python_lex
def test_python_lex(code=FIB, expected=54):
g = Grammar(_read('python.g'))
l = list(g.lex(code))
for x in l:
y = x.value
if isinstance(y, TokValue):
logging.debug('%s %s %s', y.type, y.line, y.column)
else:
logging.debug('%s %s', x.type, x.value)
assert len(l) == expected, len(l)
示例6: test_expand1_lists_with_one_item
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)
示例7: test_multiple_item_flatten_list
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'))
示例8: test_empty_expand1_list_2
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], ())
示例9: test_dont_expand1_lists_with_multiple_items_2
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'))
示例10: setUp
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}")
示例11: test_unicode
def test_unicode(self):
g = Grammar(r"""start: UNIA UNIB UNIA;
UNIA: '\xa3';
UNIB: '\u0101';
""")
g.parse(u'\xa3\u0101\u00a3')