Browse Source

maybe_placeholder now doesn't apply to anonymous terminals

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.66
Erez Shinan 6 years ago
parent
commit
adb30180aa
3 changed files with 17 additions and 2 deletions
  1. +6
    -1
      lark/load_grammar.py
  2. +1
    -1
      lark/parse_tree_builder.py
  3. +10
    -0
      tests/test_parser.py

+ 6
- 1
lark/load_grammar.py View File

@@ -176,7 +176,12 @@ class EBNF_to_BNF(Transformer_InPlace):

def expr(self, rule, op, *args):
if op.value == '?':
return ST('expansions', [rule, _EMPTY])
if isinstance(rule, Terminal) and rule.filter_out and not (
self.rule_options and self.rule_options.keep_all_tokens):
empty = ST('expansion', [])
else:
empty = _EMPTY
return ST('expansions', [rule, empty])
elif op.value == '+':
# a : b c+ d
# -->


+ 1
- 1
lark/parse_tree_builder.py View File

@@ -151,7 +151,7 @@ class ParseTreeBuilder:
wrapper_chain = filter(None, [
(expand_single_child and not rule.alias) and ExpandSingleChild,
maybe_create_child_filter(rule.expansion, keep_all_tokens, self.ambiguous),
self.maybe_placeholders and partial(AddMaybePlaceholder, options.empty_indices),
self.maybe_placeholders and partial(AddMaybePlaceholder, options.empty_indices if options else None),
self.propagate_positions and PropagatePositions,
])



+ 10
- 0
tests/test_parser.py View File

@@ -1249,6 +1249,16 @@ def _make_parser_test(LEXER, PARSER):
self.assertEqual(len(res.children), 3)

def test_maybe_placeholders(self):
# Anonymous tokens shouldn't count
p = Lark("""start: "a"? "b"? "c"? """, maybe_placeholders=True)
self.assertEqual(p.parse("").children, [])

# Anonymous tokens shouldn't count, other constructs should
p = Lark("""start: A? "b"? _c?
A: "a"
_c: "c" """, maybe_placeholders=True)
self.assertEqual(p.parse("").children, [None, None])

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])


Loading…
Cancel
Save