Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_earley4(self):
grammar = """
start: A A?
A: "a"+
"""
l = Lark(grammar, parser='earley', lexer=LEXER)
res = l.parse("aaa")
assert set(res.children) == {'aa', 'a'} or res.children == ['aaa']
# XXX TODO fix Earley to maintain correct order
def test_same_ast(self):
"Tests that Earley and LALR parsers produce equal trees"
g = Lark(r"""start: "(" name_list ("," "*" NAME)? ")"
name_list: NAME | name_list "," NAME
NAME: /\w+/ """, parser='lalr')
l = g.parse('(a,b,c,*x)')
g = Lark(r"""start: "(" name_list ("," "*" NAME)? ")"
name_list: NAME | name_list "," NAME
NAME: /\w/+ """)
l2 = g.parse('(a,b,c,*x)')
assert l == l2, '%s != %s' % (l.pretty(), l2.pretty())
?a : b
b : "x"
""", parser='lalr', transformer=T())
r = g.parse("x")
self.assertEqual( r.children, ["<b>"] )
# Test Expand1 -> Alias
g = Lark("""start: a
?a : b b -> c
b : "x"
""", parser='lalr')
r = T().transform(g.parse("xx"))
self.assertEqual( r.children, [""] )
g = Lark("""start: a
?a : b b -> c
b : "x"
""", parser='lalr', transformer=T())
r = g.parse("xx")
self.assertEqual( r.children, [""] )
</b>
def test_ambiguity1(self):
grammar = """
start: cd+ "e"
!cd: "c"
| "d"
| "cd"
"""
l = Lark(grammar, parser='earley', ambiguity='explicit', lexer=LEXER)
ambig_tree = l.parse('cde')
assert ambig_tree.data == '_ambig', ambig_tree
assert len(ambig_tree.children) == 2
"""
l = _Lark(grammar, ambiguity='resolve__antiscore_sum')
res = l.parse('abba')
self.assertEqual(''.join(child.data for child in res.children), 'ab_b_a_')
grammar = """
start: ab_ b_ a_ | indirection
indirection: a_ bb_ a_
a_: "a"
b_: "b"
ab_.1: "ab"
bb_: "bb"
"""
l = Lark(grammar, parser='earley', ambiguity='resolve__antiscore_sum')
res = l.parse('abba')
self.assertEqual(''.join(child.data for child in res.children), 'indirection')
grammar = """
start: ab_ b_ a_ | indirection
indirection: a_ bb_ a_
a_.2: "a"
b_.1: "b"
ab_.3: "ab"
bb_.3: "bb"
"""
l = Lark(grammar, parser='earley', ambiguity='resolve__antiscore_sum')
res = l.parse('abba')
self.assertEqual(''.join(child.data for child in res.children), 'ab_b_a_')
def A(self, tok):
return tok.update(value=tok.upper())
# Test regular
g = """start: a
a : A
A: "x"
"""
p = Lark(g, parser='lalr')
r = T(False).transform(p.parse("x"))
self.assertEqual( r.children, ["x!"] )
r = T().transform(p.parse("x"))
self.assertEqual( r.children, ["X!"] )
# Test internal transformer
p = Lark(g, parser='lalr', transformer=T())
r = p.parse("x")
self.assertEqual( r.children, ["X!"] )
def test_earley3(self):
"""Tests prioritization and disambiguation for pseudo-terminals (there should be only one result)
By default, `+` should immitate regexp greedy-matching
"""
grammar = """
start: A A
A: "a"+
"""
l = Lark(grammar, parser='earley', lexer=LEXER)
res = l.parse("aaa")
self.assertEqual(set(res.children), {'aa', 'a'})
# XXX TODO fix Earley to maintain correct order
def test_term_ambig_resolve(self):
grammar = r"""
!start: NAME+
NAME: /\w+/
%ignore " "
"""
text = """foo bar"""
parser = Lark(grammar)
tree = parser.parse(text)
self.assertEqual(tree.children, ['foo', 'bar'])