Skip to content

Commit

Permalink
Merge pull request #922 from ornariece/master
Browse files Browse the repository at this point in the history
Fix #696 now providing the correct amount of placeholders
  • Loading branch information
erezsh authored Sep 13, 2021
2 parents fe488d1 + 31509ac commit a12effe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lark/load_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,11 @@ def will_not_get_removed(sym):
return not sym.name.startswith('_')
if isinstance(sym, Terminal):
return keep_all_tokens or not sym.filter_out
assert False
if sym is _EMPTY:
return False
assert False, sym

if any(rule.scan_values(will_not_get_removed)):
empty = _EMPTY
else:
empty = ST('expansion', [])
empty = ST('expansion', [_EMPTY] * len(list(rule.scan_values(will_not_get_removed))))

return ST('expansions', [rule, empty])

Expand Down
9 changes: 9 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,15 @@ def test_maybe_placeholders(self):
self.assertEqual(p.parse("abba").children, ['a', None, 'b', 'b', 'a', None])
self.assertEqual(p.parse("cbbbb").children, [None, 'c', 'b', 'b', 'b', 'b', None, None])

p = _Lark("""!start: ["a" "b" "c"] """, maybe_placeholders=True)
self.assertEqual(p.parse("").children, [None, None, None])
self.assertEqual(p.parse("abc").children, ['a', 'b', 'c'])

p = _Lark("""!start: ["a" ["b" "c"]] """, maybe_placeholders=True)
self.assertEqual(p.parse("").children, [None, None, None])
self.assertEqual(p.parse("a").children, ['a', None, None])
self.assertEqual(p.parse("abc").children, ['a', 'b', 'c'])


def test_escaped_string(self):
"Tests common.ESCAPED_STRING"
Expand Down

0 comments on commit a12effe

Please sign in to comment.