Browse Source

Added VisitError for transformers

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.66
Erez Shinan 6 years ago
parent
commit
8e7c05a8f6
3 changed files with 23 additions and 11 deletions
  1. +2
    -0
      lark/exceptions.py
  2. +5
    -1
      lark/utils.py
  3. +16
    -10
      lark/visitors.py

+ 2
- 0
lark/exceptions.py View File

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

super(UnexpectedToken, self).__init__(message)

class VisitError(Exception):
pass
###}

+ 5
- 1
lark/utils.py View File

@@ -57,12 +57,16 @@ from functools import wraps, partial
from contextlib import contextmanager

Str = type(u'')
try:
classtype = types.ClassType # Python2
except AttributeError:
classtype = type # Python3

def smart_decorator(f, create_decorator):
if isinstance(f, types.FunctionType):
return wraps(f)(create_decorator(f, True))

elif isinstance(f, (type, types.BuiltinFunctionType)):
elif isinstance(f, (classtype, type, types.BuiltinFunctionType)):
return wraps(f)(create_decorator(f, False))

elif isinstance(f, types.MethodType):


+ 16
- 10
lark/visitors.py View File

@@ -2,6 +2,7 @@ from functools import wraps

from .utils import smart_decorator
from .tree import Tree
from .exceptions import VisitError, GrammarError

###{standalone
from inspect import getmembers, getmro
@@ -28,16 +29,21 @@ class Transformer:
except AttributeError:
return self.__default__(tree.data, children, tree.meta)
else:
if getattr(f, 'meta', False):
return f(children, tree.meta)
elif getattr(f, 'inline', False):
return f(*children)
elif getattr(f, 'whole_tree', False):
if new_children is not None:
raise NotImplementedError("Doesn't work with the base Transformer class")
return f(tree)
else:
return f(children)
try:
if getattr(f, 'meta', False):
return f(children, tree.meta)
elif getattr(f, 'inline', False):
return f(*children)
elif getattr(f, 'whole_tree', False):
if new_children is not None:
raise NotImplementedError("Doesn't work with the base Transformer class")
return f(tree)
else:
return f(children)
except GrammarError:
raise
except Exception as e:
raise VisitError('Error trying to process rule "%s":\n\n%s' % (tree.data, e))

def _transform_children(self, children):
for c in children:


Loading…
Cancel
Save