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._loadtags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.0
@@ -374,7 +374,8 @@ class Lark(Serialize): | |||||
self._callbacks, | self._callbacks, | ||||
self.options.postlex, | self.options.postlex, | ||||
self.options.transformer, | self.options.transformer, | ||||
re_module | |||||
re_module, | |||||
self.options.debug | |||||
) | ) | ||||
self.terminals = self.parser.lexer_conf.tokens | self.terminals = self.parser.lexer_conf.tokens | ||||
self._terminals_dict = {t.name: t for t in self.terminals} | self._terminals_dict = {t.name: t for t in self.terminals} | ||||
@@ -82,11 +82,13 @@ class WithLexer(_ParserFrontend): | |||||
self.postlex = lexer_conf.postlex | self.postlex = lexer_conf.postlex | ||||
@classmethod | @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 = super(WithLexer, cls).deserialize(data, memo) | ||||
inst.postlex = postlex | 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)] | terminals = [item for item in memo.values() if isinstance(item, TerminalDef)] | ||||
inst.lexer_conf.callbacks = _get_lexer_callbacks(transformer, terminals) | inst.lexer_conf.callbacks = _get_lexer_callbacks(transformer, terminals) | ||||
@@ -23,10 +23,10 @@ class LALR_Parser(object): | |||||
self.parser = _Parser(analysis.parse_table, callbacks, debug) | self.parser = _Parser(analysis.parse_table, callbacks, debug) | ||||
@classmethod | @classmethod | ||||
def deserialize(cls, data, memo, callbacks): | |||||
def deserialize(cls, data, memo, callbacks, debug=False): | |||||
inst = cls.__new__(cls) | inst = cls.__new__(cls) | ||||
inst._parse_table = IntParseTable.deserialize(data, memo) | inst._parse_table = IntParseTable.deserialize(data, memo) | ||||
inst.parser = _Parser(inst._parse_table, callbacks) | |||||
inst.parser = _Parser(inst._parse_table, callbacks, debug) | |||||
return inst | return inst | ||||
def serialize(self, memo): | def serialize(self, memo): | ||||
@@ -86,6 +86,12 @@ class TestCache(TestCase): | |||||
parser = Lark(g, parser='lalr', lexer=CustomLexer, cache=True) | parser = Lark(g, parser='lalr', lexer=CustomLexer, cache=True) | ||||
assert len(mock_fs.files) == 1 | assert len(mock_fs.files) == 1 | ||||
assert parser.parse('a') == Tree('start', []) | 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: | finally: | ||||
lark_module.FS = fs | lark_module.FS = fs | ||||