mirror of https://github.com/lark-parser/lark.git
args decorators actually work now
This commit is contained in:
parent
9daacb9082
commit
5e546f38a9
|
@ -1,5 +1,5 @@
|
||||||
from .tree import Tree
|
from .tree import Tree
|
||||||
from .transformers import Transformer
|
from .visitors import Transformer, Visitor, children_args, children_args_inline
|
||||||
from .common import ParseError, GrammarError, UnexpectedToken
|
from .common import ParseError, GrammarError, UnexpectedToken
|
||||||
from .lexer import UnexpectedInput, LexError
|
from .lexer import UnexpectedInput, LexError
|
||||||
from .lark import Lark
|
from .lark import Lark
|
||||||
|
|
|
@ -16,7 +16,7 @@ from .grammar import RuleOptions, Rule, Terminal, NonTerminal, Symbol
|
||||||
from .utils import classify, suppress
|
from .utils import classify, suppress
|
||||||
|
|
||||||
from .tree import Tree, SlottedTree as ST
|
from .tree import Tree, SlottedTree as ST
|
||||||
from .transformers import Transformer, Visitor, children_args, children_args_inline
|
from .visitors import Transformer, Visitor, children_args, children_args_inline
|
||||||
|
|
||||||
__path__ = os.path.dirname(__file__)
|
__path__ = os.path.dirname(__file__)
|
||||||
IMPORT_PATHS = [os.path.join(__path__, 'grammars')]
|
IMPORT_PATHS = [os.path.join(__path__, 'grammars')]
|
||||||
|
@ -255,7 +255,6 @@ class CanonizeTree(Transformer):
|
||||||
tokenmods, value = args
|
tokenmods, value = args
|
||||||
return tokenmods + [value]
|
return tokenmods + [value]
|
||||||
|
|
||||||
@children_args_inline
|
|
||||||
class PrepareAnonTerminals(Transformer):
|
class PrepareAnonTerminals(Transformer):
|
||||||
"Create a unique list of anonymous tokens. Attempt to give meaningful names to them when we add them"
|
"Create a unique list of anonymous tokens. Attempt to give meaningful names to them when we add them"
|
||||||
|
|
||||||
|
@ -266,6 +265,7 @@ class PrepareAnonTerminals(Transformer):
|
||||||
self.i = 0
|
self.i = 0
|
||||||
|
|
||||||
|
|
||||||
|
@children_args_inline
|
||||||
def pattern(self, p):
|
def pattern(self, p):
|
||||||
value = p.value
|
value = p.value
|
||||||
if p in self.token_reverse and p.flags != self.token_reverse[p].pattern.flags:
|
if p in self.token_reverse and p.flags != self.token_reverse[p].pattern.flags:
|
||||||
|
@ -409,8 +409,8 @@ class TokenTreeToPattern(Transformer):
|
||||||
def value(self, v):
|
def value(self, v):
|
||||||
return v[0]
|
return v[0]
|
||||||
|
|
||||||
@children_args
|
|
||||||
class PrepareSymbols(Transformer):
|
class PrepareSymbols(Transformer):
|
||||||
|
@children_args
|
||||||
def value(self, v):
|
def value(self, v):
|
||||||
v ,= v
|
v ,= v
|
||||||
if isinstance(v, Tree):
|
if isinstance(v, Tree):
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
# Email : erezshin@gmail.com
|
# Email : erezshin@gmail.com
|
||||||
|
|
||||||
from ..tree import Tree
|
from ..tree import Tree
|
||||||
from ..transformers import Transformer_InPlace
|
from ..visitors import Transformer_InPlace
|
||||||
from ..common import ParseError, UnexpectedToken
|
from ..common import ParseError, UnexpectedToken
|
||||||
from .grammar_analysis import GrammarAnalyzer
|
from .grammar_analysis import GrammarAnalyzer
|
||||||
from ..grammar import NonTerminal
|
from ..grammar import NonTerminal
|
||||||
|
|
|
@ -57,13 +57,13 @@ Str = type(u'')
|
||||||
|
|
||||||
def smart_decorator(f, create_decorator):
|
def smart_decorator(f, create_decorator):
|
||||||
if isinstance(f, types.FunctionType):
|
if isinstance(f, types.FunctionType):
|
||||||
return wraps(create_decorator(f, True))
|
return wraps(f)(create_decorator(f, True))
|
||||||
|
|
||||||
elif isinstance(f, (type, types.BuiltinFunctionType)):
|
elif isinstance(f, (type, types.BuiltinFunctionType)):
|
||||||
return wraps(create_decorator(f, False))
|
return wraps(f)(create_decorator(f, False))
|
||||||
|
|
||||||
elif isinstance(f, types.MethodType):
|
elif isinstance(f, types.MethodType):
|
||||||
return wraps(create_decorator(f.__func__, True))
|
return wraps(f)(create_decorator(f.__func__, True))
|
||||||
|
|
||||||
elif isinstance(f, partial):
|
elif isinstance(f, partial):
|
||||||
# wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
|
# wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import inspect
|
from inspect import isclass
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from .utils import smart_decorator
|
from .utils import smart_decorator
|
||||||
|
@ -119,6 +119,7 @@ def _children_args__func(f):
|
||||||
else:
|
else:
|
||||||
def f(args):
|
def f(args):
|
||||||
return _f(tree.children)
|
return _f(tree.children)
|
||||||
|
return f
|
||||||
|
|
||||||
return smart_decorator(f, create_decorator)
|
return smart_decorator(f, create_decorator)
|
||||||
|
|
||||||
|
@ -136,7 +137,7 @@ def _children_args__class(cls):
|
||||||
|
|
||||||
|
|
||||||
def children_args(obj):
|
def children_args(obj):
|
||||||
decorator = _children_args__class if issubclass(obj, Base) else _children_args__func
|
decorator = _children_args__class if isclass(obj) and issubclass(obj, Base) else _children_args__func
|
||||||
return decorator(obj)
|
return decorator(obj)
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,6 +151,7 @@ def _children_args_inline__func(f):
|
||||||
else:
|
else:
|
||||||
def f(args):
|
def f(args):
|
||||||
return _f(*tree.children)
|
return _f(*tree.children)
|
||||||
|
return f
|
||||||
|
|
||||||
return smart_decorator(f, create_decorator)
|
return smart_decorator(f, create_decorator)
|
||||||
|
|
||||||
|
@ -167,5 +169,5 @@ def _children_args_inline__class(cls):
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
def children_args_inline(obj):
|
def children_args_inline(obj):
|
||||||
decorator = _children_args_inline__class if issubclass(obj, Base) else _children_args_inline__func
|
decorator = _children_args_inline__class if isclass(obj) and issubclass(obj, Base) else _children_args_inline__func
|
||||||
return decorator(obj)
|
return decorator(obj)
|
|
@ -21,7 +21,7 @@ from lark.lark import Lark
|
||||||
from lark.common import GrammarError, ParseError, UnexpectedToken
|
from lark.common import GrammarError, ParseError, UnexpectedToken
|
||||||
from lark.lexer import LexError, UnexpectedInput
|
from lark.lexer import LexError, UnexpectedInput
|
||||||
from lark.tree import Tree
|
from lark.tree import Tree
|
||||||
from lark.transformers import Transformer, children_args
|
from lark.visitors import Transformer, children_args
|
||||||
|
|
||||||
__path__ = os.path.dirname(__file__)
|
__path__ = os.path.dirname(__file__)
|
||||||
def _read(n, *args):
|
def _read(n, *args):
|
||||||
|
|
|
@ -6,7 +6,7 @@ import copy
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
from lark.tree import Tree
|
from lark.tree import Tree
|
||||||
from lark.transformers import Interpreter, visit_children_decor
|
from lark.visitors import Interpreter, visit_children_decor
|
||||||
|
|
||||||
|
|
||||||
class TestTrees(TestCase):
|
class TestTrees(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue