| @@ -2,7 +2,7 @@ | |||||
| # This example shows how to write a basic calculator with variables. | # This example shows how to write a basic calculator with variables. | ||||
| # | # | ||||
| from lark import Lark, Transformer, visitor_args | |||||
| from lark import Lark, Transformer, v_args | |||||
| try: | try: | ||||
| input = raw_input # For Python2 compatibility | input = raw_input # For Python2 compatibility | ||||
| @@ -34,7 +34,7 @@ calc_grammar = """ | |||||
| %ignore WS_INLINE | %ignore WS_INLINE | ||||
| """ | """ | ||||
| @visitor_args(inline=True) | |||||
| @v_args(inline=True) | |||||
| class CalculateTree(Transformer): | class CalculateTree(Transformer): | ||||
| from operator import add, sub, mul, truediv as div, neg | from operator import add, sub, mul, truediv as div, neg | ||||
| number = float | number = float | ||||
| @@ -7,7 +7,7 @@ | |||||
| import sys | import sys | ||||
| from lark import Lark, Transformer, visitor_args | |||||
| from lark import Lark, Transformer, v_args | |||||
| json_grammar = r""" | json_grammar = r""" | ||||
| ?start: value | ?start: value | ||||
| @@ -34,14 +34,14 @@ json_grammar = r""" | |||||
| """ | """ | ||||
| class TreeToJson(Transformer): | class TreeToJson(Transformer): | ||||
| @visitor_args(inline=True) | |||||
| @v_args(inline=True) | |||||
| def string(self, s): | def string(self, s): | ||||
| return s[1:-1].replace('\\"', '"') | return s[1:-1].replace('\\"', '"') | ||||
| array = list | array = list | ||||
| pair = tuple | pair = tuple | ||||
| object = dict | object = dict | ||||
| number = visitor_args(inline=True)(float) | |||||
| number = v_args(inline=True)(float) | |||||
| null = lambda self, _: None | null = lambda self, _: None | ||||
| true = lambda self, _: True | true = lambda self, _: True | ||||
| @@ -1,5 +1,5 @@ | |||||
| from .tree import Tree | from .tree import Tree | ||||
| from .visitors import Transformer, Visitor, visitor_args, Discard | |||||
| from .visitors import Transformer, Visitor, v_args, Discard | |||||
| from .visitors import InlineTransformer, inline_args # XXX Deprecated | from .visitors import InlineTransformer, inline_args # XXX Deprecated | ||||
| from .common import ParseError, GrammarError, UnexpectedToken | from .common import ParseError, GrammarError, UnexpectedToken | ||||
| from .lexer import UnexpectedInput, LexError | from .lexer import UnexpectedInput, LexError | ||||
| @@ -16,7 +16,8 @@ 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 .visitors import Transformer, Visitor, visitor_args | |||||
| from .visitors import Transformer, Visitor, v_args | |||||
| inline_args = v_args(inline=True) | |||||
| __path__ = os.path.dirname(__file__) | __path__ = os.path.dirname(__file__) | ||||
| IMPORT_PATHS = [os.path.join(__path__, 'grammars')] | IMPORT_PATHS = [os.path.join(__path__, 'grammars')] | ||||
| @@ -138,7 +139,7 @@ RULES = { | |||||
| } | } | ||||
| @visitor_args(inline=True) | |||||
| @inline_args | |||||
| class EBNF_to_BNF(Transformer): | class EBNF_to_BNF(Transformer): | ||||
| def __init__(self): | def __init__(self): | ||||
| self.new_rules = [] | self.new_rules = [] | ||||
| @@ -243,7 +244,7 @@ class RuleTreeToText(Transformer): | |||||
| return expansion, alias.value | return expansion, alias.value | ||||
| @visitor_args(inline=True) | |||||
| @inline_args | |||||
| class CanonizeTree(Transformer): | class CanonizeTree(Transformer): | ||||
| def maybe(self, expr): | def maybe(self, expr): | ||||
| return ST('expr', [expr, Token('OP', '?', -1)]) | return ST('expr', [expr, Token('OP', '?', -1)]) | ||||
| @@ -264,7 +265,7 @@ class PrepareAnonTerminals(Transformer): | |||||
| self.i = 0 | self.i = 0 | ||||
| @visitor_args(inline=True) | |||||
| @inline_args | |||||
| 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: | ||||
| @@ -354,7 +355,7 @@ def _literal_to_pattern(literal): | |||||
| 'REGEXP': PatternRE }[literal.type](s, flags) | 'REGEXP': PatternRE }[literal.type](s, flags) | ||||
| @visitor_args(inline=True) | |||||
| @inline_args | |||||
| class PrepareLiterals(Transformer): | class PrepareLiterals(Transformer): | ||||
| def literal(self, literal): | def literal(self, literal): | ||||
| return ST('pattern', [_literal_to_pattern(literal)]) | return ST('pattern', [_literal_to_pattern(literal)]) | ||||
| @@ -532,7 +533,7 @@ def options_from_rule(name, *x): | |||||
| def symbols_from_strcase(expansion): | def symbols_from_strcase(expansion): | ||||
| return [Terminal(x, filter_out=x.startswith('_')) if is_terminal(x) else NonTerminal(x) for x in expansion] | return [Terminal(x, filter_out=x.startswith('_')) if is_terminal(x) else NonTerminal(x) for x in expansion] | ||||
| @visitor_args(inline=True) | |||||
| @inline_args | |||||
| class PrepareGrammar(Transformer): | class PrepareGrammar(Transformer): | ||||
| def terminal(self, name): | def terminal(self, name): | ||||
| return name | return name | ||||
| @@ -14,7 +14,7 @@ | |||||
| # Email : erezshin@gmail.com | # Email : erezshin@gmail.com | ||||
| from ..tree import Tree | from ..tree import Tree | ||||
| from ..visitors import Transformer_InPlace, visitor_args | |||||
| from ..visitors import Transformer_InPlace, v_args | |||||
| 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 | ||||
| @@ -234,6 +234,6 @@ class ApplyCallbacks(Transformer_InPlace): | |||||
| def __init__(self, postprocess): | def __init__(self, postprocess): | ||||
| self.postprocess = postprocess | self.postprocess = postprocess | ||||
| @visitor_args(meta=True) | |||||
| @v_args(meta=True) | |||||
| def drv(self, children, meta): | def drv(self, children, meta): | ||||
| return self.postprocess[meta.rule](children) | return self.postprocess[meta.rule](children) | ||||
| @@ -206,7 +206,7 @@ def _visitor_args_func_dec(func, inline=False, meta=False): | |||||
| f.meta = meta | f.meta = meta | ||||
| return f | return f | ||||
| def visitor_args(inline=False, meta=False): | |||||
| def v_args(inline=False, meta=False): | |||||
| if inline and meta: | if inline and meta: | ||||
| raise ValueError("Visitor functions can either accept meta, or be inlined. Not both.") | raise ValueError("Visitor functions can either accept meta, or be inlined. Not both.") | ||||
| def _visitor_args_dec(obj): | def _visitor_args_dec(obj): | ||||
| @@ -6,7 +6,7 @@ import copy | |||||
| import pickle | import pickle | ||||
| from lark.tree import Tree | from lark.tree import Tree | ||||
| from lark.visitors import Transformer, Interpreter, visit_children_decor, visitor_args | |||||
| from lark.visitors import Transformer, Interpreter, visit_children_decor, v_args | |||||
| class TestTrees(TestCase): | class TestTrees(TestCase): | ||||
| @@ -63,8 +63,8 @@ class TestTrees(TestCase): | |||||
| t = Tree('add', [Tree('sub', [Tree('i', ['3']), Tree('f', ['1.1'])]), Tree('i', ['1'])]) | t = Tree('add', [Tree('sub', [Tree('i', ['3']), Tree('f', ['1.1'])]), Tree('i', ['1'])]) | ||||
| class T(Transformer): | class T(Transformer): | ||||
| i = visitor_args(inline=True)(int) | |||||
| f = visitor_args(inline=True)(float) | |||||
| i = v_args(inline=True)(int) | |||||
| f = v_args(inline=True)(float) | |||||
| sub = lambda self, values: values[0] - values[1] | sub = lambda self, values: values[0] - values[1] | ||||
| @@ -74,7 +74,7 @@ class TestTrees(TestCase): | |||||
| res = T().transform(t) | res = T().transform(t) | ||||
| self.assertEqual(res, 2.9) | self.assertEqual(res, 2.9) | ||||
| @visitor_args(inline=True) | |||||
| @v_args(inline=True) | |||||
| class T(Transformer): | class T(Transformer): | ||||
| i = int | i = int | ||||
| f = float | f = float | ||||
| @@ -88,7 +88,7 @@ class TestTrees(TestCase): | |||||
| self.assertEqual(res, 2.9) | self.assertEqual(res, 2.9) | ||||
| @visitor_args(inline=True) | |||||
| @v_args(inline=True) | |||||
| class T(Transformer): | class T(Transformer): | ||||
| i = int | i = int | ||||
| f = float | f = float | ||||