Ver a proveniência

fix: Propagate debug flag on loading grammar from cache

The debug flag is already saved in the cached file, but is not
resumed on loading the dumped file. To solve that,
 - add argument `debug` to parser_fronteds.WithLexer.deserialize
 - add argument `debug` lalr_parser.LALR_Parser.deserialize
 - propagate the value of the `debug` option on resuming a cached
   grammar, in lark.Lark._load
tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.0
Louis Dubois há 4 anos
committed by Erez Sh
ascendente
cometimento
a423097362
4 ficheiros alterados com 14 adições e 5 eliminações
  1. +2
    -1
      lark/lark.py
  2. +4
    -2
      lark/parser_frontends.py
  3. +2
    -2
      lark/parsers/lalr_parser.py
  4. +6
    -0
      tests/test_cache.py

+ 2
- 1
lark/lark.py Ver ficheiro

@@ -374,7 +374,8 @@ class Lark(Serialize):
self._callbacks,
self.options.postlex,
self.options.transformer,
re_module
re_module,
self.options.debug
)
self.terminals = self.parser.lexer_conf.tokens
self._terminals_dict = {t.name: t for t in self.terminals}


+ 4
- 2
lark/parser_frontends.py Ver ficheiro

@@ -82,11 +82,13 @@ class WithLexer(_ParserFrontend):
self.postlex = lexer_conf.postlex

@classmethod
def deserialize(cls, data, memo, callbacks, postlex, transformer, re_module):
def deserialize(cls, data, memo, callbacks, postlex, transformer, re_module,
debug):
inst = super(WithLexer, cls).deserialize(data, memo)

inst.postlex = postlex
inst.parser = LALR_Parser.deserialize(inst.parser, memo, callbacks)
inst.parser = LALR_Parser.deserialize(inst.parser, memo, callbacks,
debug)

terminals = [item for item in memo.values() if isinstance(item, TerminalDef)]
inst.lexer_conf.callbacks = _get_lexer_callbacks(transformer, terminals)


+ 2
- 2
lark/parsers/lalr_parser.py Ver ficheiro

@@ -23,10 +23,10 @@ class LALR_Parser(object):
self.parser = _Parser(analysis.parse_table, callbacks, debug)

@classmethod
def deserialize(cls, data, memo, callbacks):
def deserialize(cls, data, memo, callbacks, debug=False):
inst = cls.__new__(cls)
inst._parse_table = IntParseTable.deserialize(data, memo)
inst.parser = _Parser(inst._parse_table, callbacks)
inst.parser = _Parser(inst._parse_table, callbacks, debug)
return inst

def serialize(self, memo):


+ 6
- 0
tests/test_cache.py Ver ficheiro

@@ -86,6 +86,12 @@ class TestCache(TestCase):
parser = Lark(g, parser='lalr', lexer=CustomLexer, cache=True)
assert len(mock_fs.files) == 1
assert parser.parse('a') == Tree('start', [])

# Test options persistence
mock_fs.files = {}
Lark(g, parser="lalr", debug=True, cache=True)
parser = Lark(g, parser="lalr", debug=True, cache=True)
assert parser.options.options['debug']
finally:
lark_module.FS = fs



Carregando…
Cancelar
Guardar