本文整理汇总了Python中arpeggio.ParserPython类的典型用法代码示例。如果您正苦于以下问题:Python ParserPython类的具体用法?Python ParserPython怎么用?Python ParserPython使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ParserPython类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_combine_python
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_direct_rule_call
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!"
示例3: test_reduce_tree
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'
示例4: parse_tree
def parse_tree():
def grammar(): return ("first", "second", "third")
parser = ParserPython(grammar)
return parser.parse(" first \n\n second third")
示例5: parse_string
def parse_string(self, src, grammar=program, filename=None):
oldsrcs = self.input_sources
self.context.optimization_level = self.optimization_level
self.input_sources = src
parser = ParserPython(
grammar,
comment_def=comment,
skipws=True,
reduce_tree=False,
memoization=True,
debug=False,
)
self.context.parsers.append(parser)
self.context.filenames.append(filename)
try:
parse_tree = parser.parse(self.input_sources)
visitor = MuvVisitor(debug=False)
visitor.muvparser = self
parse_tree = visit_parse_tree(parse_tree, visitor)
out = parse_tree.generate_code(self.context)
if self.error_found:
return False
if len(self.context.filenames) == 1:
if self.context.filenames[-1]:
filetext = " from {0}".format(
self.context.filenames[-1]
)
else:
filetext = ''
self.output = (
"( Generated{0} by the MUV compiler. )\n"
"( https://github.com/revarbat/pymuv )\n"
"{1}\n"
).format(filetext, self.output)
self.output += out
if not self.error_found and len(self.context.filenames) == 1:
if self.wrapper_program:
self.output = (
"@program {0}\n"
"1 99999 d\n"
"1 i\n"
"{1}\n"
".\n"
"c\n"
"q\n"
).format(self.wrapper_program, self.output)
return True
except MuvError as e:
line, col = parser.pos_to_linecol(e.position)
self.print_error(filename, line, col, str(e))
return False
except NoMatch as e:
line, col = parser.pos_to_linecol(e.position)
expected = self.simplify_parse_error(e)
self.print_error(filename, line, col, "Expected %s" % expected)
return False
finally:
self.input_sources = oldsrcs
self.context.parsers.pop()
self.context.filenames.pop()
示例6: test_memoization_positive
def test_memoization_positive(capsys):
'''
Test that already matched rule is found in the cache on
subsequent matches.
Args:
capsys - pytest fixture for output capture
'''
def grammar(): return [(rule1, ruleb), (rule1, rulec)]
def rule1(): return rulea, ruleb
def rulea(): return "a"
def ruleb(): return "b"
def rulec(): return "c"
parser = ParserPython(grammar, memoization=True, debug=True)
# Parse input where a rule1 will match but ruleb will fail
# Second sequence will try rule1 again on the same location
# and result should be found in the cache.
parse_tree = parser.parse("a b c")
# Assert that cached result is used
assert "Cache hit" in capsys.readouterr()[0]
assert parser.cache_hits == 1
assert parser.cache_misses == 4
示例7: test_parse_input
def test_parse_input():
parser = ParserPython(calc)
input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result = parser.parse(input)
assert isinstance(result, NonTerminal)
assert str(result) == "4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
assert repr(result) == "[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
示例8: test_sequence_suppress
def test_sequence_suppress():
"""
"""
def grammar(): return Sequence("one", "two", "three", suppress=True), "four"
parser = ParserPython(grammar)
result = parser.parse("one two three four")
assert result[0] == "four"
示例9: _from_peg
def _from_peg(self, language_def):
parser = ParserPython(peggrammar, comment, reduce_tree=False,
debug=self.debug)
parser.root_rule_name = self.root_rule_name
parse_tree = parser.parse(language_def)
return visit_parse_tree(parse_tree, PEGVisitor(self.root_rule_name,
self.comment_rule_name,
self.ignore_case,
debug=self.debug))
示例10: test_sequence
def test_sequence():
def grammar():
return ("a", "b", "c")
parser = ParserPython(grammar)
parsed = parser.parse("a b c")
assert str(parsed) == "a | b | c"
assert repr(parsed) == "[ 'a' [0], 'b' [2], 'c' [4] ]"
示例11: test_eolterm
def test_eolterm():
# first rule should match only first line
# so that second rule will match "a" on the new line
input = """a a b a b b
a"""
parser = ParserPython(grammar, reduce_tree=False)
result = parser.parse(input)
assert result
示例12: test_default_action_disabled
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
示例13: main
def main(debug=False):
parser = ParserPython(initial, debug=debug)
file_input = open("input.txt", 'r')
input_expr = file_input.read()
parse_tree = parser.parse(input_expr)
result = parser.getASG()
示例14: test_zeroormore_with_separator
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')
示例15: parse_bibtex
def parse_bibtex(file_name, debug=False):
global parser
if parser is None:
parser = ParserPython(bibfile, debug=debug)
with codecs.open(file_name, "r", encoding="utf-8") as bibtexfile:
bibtexfile_content = bibtexfile.read()
parse_tree = parser.parse(bibtexfile_content)
return visit_parse_tree(parse_tree,
BibtexVisitor(debug=debug))