本文整理汇总了Python中arpeggio.ParserPython.parse方法的典型用法代码示例。如果您正苦于以下问题:Python ParserPython.parse方法的具体用法?Python ParserPython.parse怎么用?Python ParserPython.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arpeggio.ParserPython
的用法示例。
在下文中一共展示了ParserPython.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_combine_python
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_combine_python():
# This will result in NonTerminal node
def root():
return my_rule(), "."
# This will result in Terminal node
def my_rule():
return Combine(ZeroOrMore("a"), OneOrMore("b"))
parser = ParserPython(root)
input1 = "abbb."
# Whitespaces are preserved in lexical rules so the following input
# should not be recognized.
input2 = "a b bb."
ptree1 = parser.parse(input1)
with pytest.raises(NoMatch):
parser.parse(input2)
assert isinstance(ptree1, NonTerminal)
assert isinstance(ptree1[0], Terminal)
assert ptree1[0].value == "abbb"
示例2: test_reduce_tree
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_reduce_tree():
input = "34 a 3 3 b 3 b"
parser = ParserPython(grammar, reduce_tree=False)
result = parser.parse(input)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert result[0].rule_name == 'first'
assert isinstance(result[0], NonTerminal)
assert result[3].rule_name == 'first'
assert result[0][0].rule_name == 'fourth'
# Check reduction for direct OrderedChoice
assert result[2][0].rule_name == 'third'
parser = ParserPython(grammar, reduce_tree=True)
result = parser.parse(input)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert result[0].rule_name == 'fourth'
assert isinstance(result[0], Terminal)
assert result[3].rule_name == 'fourth'
# Check reduction for direct OrderedChoice
assert result[2][0].rule_name == 'third_str'
示例3: test_zeroormore_with_separator
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_zeroormore_with_separator():
def grammar():
return ZeroOrMore(['a', 'b'], sep=','), EOF
parser = ParserPython(grammar, reduce_tree=False)
result = parser.parse('a, b, b, b, a')
assert result
with pytest.raises(NoMatch):
parser.parse('a, b a')
示例4: test_default_action_disabled
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_default_action_disabled():
parser = ParserPython(grammar)
parser.parse('(-34) strmatch')
parser.getASG(defaults=False)
assert not p_removed
assert not number_str
assert parse_tree_node
示例5: test_sequence_of_nots
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_sequence_of_nots():
"""
Test that sequence of Not rules is handled properly.
"""
def grammar():
return Not('one'), Not('two'), _(r'\w+')
parser = ParserPython(grammar)
with pytest.raises(NoMatch) as e:
parser.parse(' two ident')
assert "Not expected input" in str(e)
示例6: test_optional_no_error
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_optional_no_error():
"""
"""
def grammar(): return Optional('a'), 'b'
parser = ParserPython(grammar)
try:
parser.parse('c')
assert False
except NoMatch as e:
assert "Expected 'b'" in str(e)
示例7: test_file_name_reporting
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_file_name_reporting():
"""
Test that if parser has file name set it will be reported.
"""
def grammar(): return Optional('a'), 'b', EOF
parser = ParserPython(grammar)
try:
parser.parse("\n\n a c", file_name="test_file.peg")
except NoMatch as e:
assert "Expected 'b' at test_file.peg:(3, 6)" in str(e)
示例8: test_file_name_reporting
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_file_name_reporting():
"""
Test that if parser has file name set it will be reported.
"""
def grammar(): return Optional('a'), 'b', EOF
parser = ParserPython(grammar)
with pytest.raises(NoMatch) as e:
parser.parse("\n\n a c", file_name="test_file.peg")
assert "Expected 'b' at position test_file.peg:(3, 6)" in str(e)
assert (e.value.line, e.value.col) == (3, 6)
示例9: test_oneormore_with_ordered_choice_separator
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_oneormore_with_ordered_choice_separator():
def grammar():
return OneOrMore(['a', 'b'], sep=[',', ';']), EOF
parser = ParserPython(grammar, reduce_tree=False)
result = parser.parse('a, a; a, b, a; a')
assert result
with pytest.raises(NoMatch):
parser.parse('a, b a')
with pytest.raises(NoMatch):
parser.parse('a, b: a')
示例10: test_not_match_at_beginning
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_not_match_at_beginning():
"""
Test that matching of Not ParsingExpression is not reported in the
error message.
"""
def grammar():
return Not('one'), _(r'\w+')
parser = ParserPython(grammar)
with pytest.raises(NoMatch) as e:
parser.parse(' one ident')
assert "Not expected input" in str(e)
示例11: test_optional_inside_zeroormore
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_optional_inside_zeroormore():
"""
Test optional match inside a zero or more.
Optional should always succeed thus inducing ZeroOrMore
to try the match again.
Arpeggio handle this using soft failures.
"""
def grammar(): return ZeroOrMore(Optional('a'))
parser = ParserPython(grammar)
with pytest.raises(NoMatch):
# This could lead to infinite loop
parser.parse('b')
示例12: test_comment_matching_not_reported
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_comment_matching_not_reported():
"""
Test that matching of comments is not reported.
"""
def grammar(): return Optional('a'), 'b', EOF
def comments(): return _('\/\/.*$')
parser = ParserPython(grammar, comments)
with pytest.raises(NoMatch) as e:
parser.parse('\n\n a // This is a comment \n c')
assert "Expected 'b' at position (4, 2)" in str(e)
assert (e.value.line, e.value.col) == (4, 2)
示例13: test_comment_matching_not_reported
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_comment_matching_not_reported():
"""
Test that matching of comments is not reported.
"""
def grammar(): return Optional('a'), 'b', EOF
def comments(): return _('\/\/.*$')
parser = ParserPython(grammar, comments)
try:
parser.parse('\n\n a // This is a comment \n c')
except NoMatch as e:
assert "Expected 'b' at position (4, 2)" in str(e)
示例14: test_optional_no_error
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_optional_no_error():
"""
Test that optional match failure does not show up in the NoMatch errors.
"""
def grammar(): return Optional('a'), 'b'
parser = ParserPython(grammar)
try:
parser.parse('c')
assert False
except NoMatch as e:
assert "Expected 'b'" in str(e)
示例15: test_direct_rule_call
# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import parse [as 别名]
def test_direct_rule_call():
'''
Test regression where in direct rule call semantic action is
erroneously attached to both caller and callee.
'''
def grammar(): return rule1, rule2
def rule1(): return "a"
def rule2(): return rule1
call_count = [0]
class DummySemAction(SemanticAction):
def first_pass(self, parser, node, nodes):
call_count[0] += 1
return SemanticAction.first_pass(self, parser, node, nodes)
# Sem action is attached to rule2 only but
# this bug will attach it to rule1 also resulting in
# wrong call count.
rule2.sem = DummySemAction()
parser = ParserPython(grammar)
parse_tree = parser.parse("aa")
parser.getASG()
assert call_count[0] == 1, "Semantic action should be called once!"