Browse Source

Proposed corrections to PR #970

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.12.0
Erez Sh 3 years ago
parent
commit
f5c7af8ce9
2 changed files with 8 additions and 7 deletions
  1. +2
    -3
      lark/load_grammar.py
  2. +6
    -4
      lark/tree.py

+ 2
- 3
lark/load_grammar.py View File

@@ -358,9 +358,8 @@ class SimplifyRule_Visitor(Visitor):


@staticmethod @staticmethod
def _flatten(tree): def _flatten(tree):
while True:
if not tree.expand_kids_by_data(tree.data):
break
while tree.expand_kids_by_data(tree.data):
pass


def expansion(self, tree): def expansion(self, tree):
# rules_list unpacking # rules_list unpacking


+ 6
- 4
lark/tree.py View File

@@ -110,12 +110,14 @@ class Tree(object):
def expand_kids_by_data(self, *data_values): def expand_kids_by_data(self, *data_values):
"""Expand (inline) children with any of the given data values. Returns True if anything changed""" """Expand (inline) children with any of the given data values. Returns True if anything changed"""
indices = [i for i, c in enumerate(self.children) if isinstance(c, Tree) and c.data in data_values] indices = [i for i, c in enumerate(self.children) if isinstance(c, Tree) and c.data in data_values]
if indices:
self.expand_kids_by_index(*indices)
return True
else:
if not indices:
return False return False


for i in reversed(indices): # reverse so that changing tail won't affect indices
child = self.children[i]
self.children[i:i+1] = child.children
return True

def scan_values(self, pred): def scan_values(self, pred):
"""Return all values in the tree that evaluate pred(value) as true. """Return all values in the tree that evaluate pred(value) as true.




Loading…
Cancel
Save