当前位置: 首页>>代码示例>>Python>>正文


Python ParserPython.parse方法代码示例

本文整理汇总了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"
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:28,代码来源:test_decorator_combine.py

示例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'
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:28,代码来源:test_reduce_tree.py

示例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')
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:13,代码来源:test_separators.py

示例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
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:13,代码来源:test_default_semantic_action.py

示例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)
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:15,代码来源:test_error_reporting.py

示例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)
开发者ID:qs9816,项目名称:Arpeggio,代码行数:15,代码来源:test_optional_no_error.py

示例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)
开发者ID:Amper,项目名称:Arpeggio,代码行数:15,代码来源:test_error_reporting.py

示例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)
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:15,代码来源:test_error_reporting.py

示例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')
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:16,代码来源:test_separators.py

示例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)
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:16,代码来源:test_error_reporting.py

示例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')
开发者ID:Avram88,项目名称:Arpeggio,代码行数:16,代码来源:test_pathologic_models.py

示例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)
开发者ID:igordejanovic,项目名称:Arpeggio,代码行数:16,代码来源:test_error_reporting.py

示例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)
开发者ID:Amper,项目名称:Arpeggio,代码行数:16,代码来源:test_error_reporting.py

示例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)
开发者ID:moreati,项目名称:Arpeggio,代码行数:16,代码来源:test_optional_no_error.py

示例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!"
开发者ID:Amper,项目名称:Arpeggio,代码行数:29,代码来源:test_direct_rule_call.py


注:本文中的arpeggio.ParserPython.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。