Browse Source

Added transformer, postlex arguments to standalone

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.1
Erez Shinan 5 years ago
parent
commit
5ab12b031c
4 changed files with 14 additions and 7 deletions
  1. +6
    -3
      lark/lark.py
  2. +2
    -2
      lark/parser_frontends.py
  3. +2
    -2
      lark/tools/standalone.py
  4. +4
    -0
      tests/test_tools.py

+ 6
- 3
lark/lark.py View File

@@ -241,14 +241,17 @@ class Lark(Serialize):
return self.parser_class(self.lexer_conf, parser_conf, options=self.options)

@classmethod
def deserialize(cls, data, namespace, memo):
def deserialize(cls, data, namespace, memo, transformer=None, postlex=None):
if memo:
memo = SerializeMemoizer.deserialize(memo, namespace, {})
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._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




+ 2
- 2
lark/parser_frontends.py View File

@@ -54,9 +54,9 @@ class WithLexer(Serialize):
__serialize_namespace__ = Rule, ContextualLexer, TraditionalLexer

@classmethod
def deserialize(cls, data, memo, callbacks):
def deserialize(cls, data, memo, callbacks, postlex):
inst = super(WithLexer, cls).deserialize(data, memo)
inst.postlex = None # TODO
inst.postlex = postlex
inst.parser = LALR_Parser.deserialize(inst.parser, memo, callbacks)
return inst


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

@@ -113,9 +113,9 @@ def main(fobj, start):

print('Shift = 0')
print('Reduce = 1')
print("def Lark_StandAlone():")
print("def Lark_StandAlone(transformer=None, postlex=None):")
print(" namespace = {'Rule': Rule, 'TerminalDef': TerminalDef}")
print(" return Lark.deserialize(DATA, namespace, MEMO)")
print(" return Lark.deserialize(DATA, namespace, MEMO, transformer=transformer, postlex=postlex)")





+ 4
- 0
tests/test_tools.py View File

@@ -70,6 +70,10 @@ class TestStandalone(TestCase):
x = T().transform(x)
self.assertEqual(x, ['a', 'b'])

l2 = _Lark(transformer=T())
x = l2.parse('ABAB')
self.assertEqual(x, ['a', 'b'])


if __name__ == '__main__':
unittest.main()


Loading…
Cancel
Save