Browse Source

Fixed bugs in the standalone generator (Issue #212)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.5
Erez Shinan 6 years ago
parent
commit
843da8e809
4 changed files with 13 additions and 11 deletions
  1. +2
    -1
      lark/exceptions.py
  2. +2
    -0
      lark/tools/standalone.py
  3. +8
    -9
      lark/tree.py
  4. +1
    -1
      lark/utils.py

+ 2
- 1
lark/exceptions.py View File

@@ -1,5 +1,6 @@
from .utils import STRING_TYPE from .utils import STRING_TYPE


###{standalone
class LarkError(Exception): class LarkError(Exception):
pass pass


@@ -85,4 +86,4 @@ class UnexpectedToken(ParseError, UnexpectedInput):


super(UnexpectedToken, self).__init__(message) super(UnexpectedToken, self).__init__(message)


###}

+ 2
- 0
lark/tools/standalone.py View File

@@ -52,6 +52,7 @@ _larkdir = path.join(_dir, path.pardir)


EXTRACT_STANDALONE_FILES = [ EXTRACT_STANDALONE_FILES = [
'tools/standalone.py', 'tools/standalone.py',
'exceptions.py',
'utils.py', 'utils.py',
'common.py', 'common.py',
'tree.py', 'tree.py',
@@ -160,6 +161,7 @@ class TreeBuilderAtoms:
self.ptb = lark._parse_tree_builder self.ptb = lark._parse_tree_builder


def print_python(self): def print_python(self):
print('class InlineTransformer: pass')
print('RULES = {') print('RULES = {')
for i, r in enumerate(self.rules): for i, r in enumerate(self.rules):
rule_ids[r] = i rule_ids[r] = i


+ 8
- 9
lark/tree.py View File

@@ -5,10 +5,10 @@ except ImportError:


from copy import deepcopy from copy import deepcopy


###{standalone
class Meta: class Meta:
pass pass


###{standalone
class Tree(object): class Tree(object):
def __init__(self, data, children, meta=None): def __init__(self, data, children, meta=None):
self.data = data self.data = data
@@ -42,14 +42,6 @@ class Tree(object):


def pretty(self, indent_str=' '): def pretty(self, indent_str=' '):
return ''.join(self._pretty(0, indent_str)) return ''.join(self._pretty(0, indent_str))
###}

def expand_kids_by_index(self, *indices):
"Expand (inline) children at the given indices"
for i in sorted(indices, reverse=True): # reverse so that changing tail won't affect indices
kid = self.children[i]
self.children[i:i+1] = kid.children

def __eq__(self, other): def __eq__(self, other):
try: try:
return self.data == other.data and self.children == other.children return self.data == other.data and self.children == other.children
@@ -61,6 +53,13 @@ class Tree(object):


def __hash__(self): def __hash__(self):
return hash((self.data, tuple(self.children))) return hash((self.data, tuple(self.children)))
###}

def expand_kids_by_index(self, *indices):
"Expand (inline) children at the given indices"
for i in sorted(indices, reverse=True): # reverse so that changing tail won't affect indices
kid = self.children[i]
self.children[i:i+1] = kid.children


def find_pred(self, pred): def find_pred(self, pred):
"Find all nodes where pred(tree) == True" "Find all nodes where pred(tree) == True"


+ 1
- 1
lark/utils.py View File

@@ -42,12 +42,12 @@ def bfs(initial, expand):






###{standalone
try: try:
STRING_TYPE = basestring STRING_TYPE = basestring
except NameError: # Python 3 except NameError: # Python 3
STRING_TYPE = str STRING_TYPE = str


###{standalone


import types import types
from functools import wraps, partial from functools import wraps, partial


Loading…
Cancel
Save