| @@ -241,14 +241,17 @@ class Lark(Serialize): | |||||
| return self.parser_class(self.lexer_conf, parser_conf, options=self.options) | return self.parser_class(self.lexer_conf, parser_conf, options=self.options) | ||||
| @classmethod | @classmethod | ||||
| def deserialize(cls, data, namespace, memo): | |||||
| def deserialize(cls, data, namespace, memo, transformer=None, postlex=None): | |||||
| if memo: | if memo: | ||||
| memo = SerializeMemoizer.deserialize(memo, namespace, {}) | memo = SerializeMemoizer.deserialize(memo, namespace, {}) | ||||
| inst = cls.__new__(cls) | inst = cls.__new__(cls) | ||||
| inst.options = LarkOptions.deserialize(data['options'], memo) | |||||
| options = dict(data['options']) | |||||
| options['transformer'] = transformer | |||||
| options['postlex'] = postlex | |||||
| inst.options = LarkOptions.deserialize(options, memo) | |||||
| inst.rules = [Rule.deserialize(r, memo) for r in data['rules']] | inst.rules = [Rule.deserialize(r, memo) for r in data['rules']] | ||||
| inst._prepare_callbacks() | inst._prepare_callbacks() | ||||
| inst.parser = inst.parser_class.deserialize(data['parser'], memo, inst._callbacks) | |||||
| inst.parser = inst.parser_class.deserialize(data['parser'], memo, inst._callbacks, inst.options.postlex) | |||||
| return inst | return inst | ||||
| @@ -54,9 +54,9 @@ class WithLexer(Serialize): | |||||
| __serialize_namespace__ = Rule, ContextualLexer, TraditionalLexer | __serialize_namespace__ = Rule, ContextualLexer, TraditionalLexer | ||||
| @classmethod | @classmethod | ||||
| def deserialize(cls, data, memo, callbacks): | |||||
| def deserialize(cls, data, memo, callbacks, postlex): | |||||
| inst = super(WithLexer, cls).deserialize(data, memo) | inst = super(WithLexer, cls).deserialize(data, memo) | ||||
| inst.postlex = None # TODO | |||||
| inst.postlex = postlex | |||||
| inst.parser = LALR_Parser.deserialize(inst.parser, memo, callbacks) | inst.parser = LALR_Parser.deserialize(inst.parser, memo, callbacks) | ||||
| return inst | return inst | ||||
| @@ -113,9 +113,9 @@ def main(fobj, start): | |||||
| print('Shift = 0') | print('Shift = 0') | ||||
| print('Reduce = 1') | print('Reduce = 1') | ||||
| print("def Lark_StandAlone():") | |||||
| print("def Lark_StandAlone(transformer=None, postlex=None):") | |||||
| print(" namespace = {'Rule': Rule, 'TerminalDef': TerminalDef}") | print(" namespace = {'Rule': Rule, 'TerminalDef': TerminalDef}") | ||||
| print(" return Lark.deserialize(DATA, namespace, MEMO)") | |||||
| print(" return Lark.deserialize(DATA, namespace, MEMO, transformer=transformer, postlex=postlex)") | |||||
| @@ -70,6 +70,10 @@ class TestStandalone(TestCase): | |||||
| x = T().transform(x) | x = T().transform(x) | ||||
| self.assertEqual(x, ['a', 'b']) | self.assertEqual(x, ['a', 'b']) | ||||
| l2 = _Lark(transformer=T()) | |||||
| x = l2.parse('ABAB') | |||||
| self.assertEqual(x, ['a', 'b']) | |||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||
| unittest.main() | unittest.main() | ||||