From e4e29f5127073bfff5d277a21fd022054f99a87c Mon Sep 17 00:00:00 2001 From: MegaIng1 Date: Sat, 25 Jul 2020 18:00:37 +0200 Subject: [PATCH] Added a bit of explanation for `term_subs` --- lark-stubs/reconstruct.pyi | 2 +- lark/reconstruct.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lark-stubs/reconstruct.pyi b/lark-stubs/reconstruct.pyi index 8ac6c14..2220c46 100644 --- a/lark-stubs/reconstruct.pyi +++ b/lark-stubs/reconstruct.pyi @@ -30,7 +30,7 @@ class MakeMatchTree: class Reconstructor: - def __init__(self, parser: Lark): + def __init__(self, parser: Lark, term_subs: Dict[str, str] = ...): ... def reconstruct(self, tree: Tree) -> str: diff --git a/lark/reconstruct.py b/lark/reconstruct.py index 1e3adc7..baf1d2c 100644 --- a/lark/reconstruct.py +++ b/lark/reconstruct.py @@ -87,9 +87,16 @@ def best_from_group(seq, group_key, cmp_key): return list(d.values()) class Reconstructor: - def __init__(self, parser, term_subs={}): + """ + A Reconstructor that will, given a full parse Tree, generate source code. + Pass `term_subs`, a dictionary of [Terminal name as str] to [output text as str] + to say what discarded Terminals should be written as. + """ + def __init__(self, parser, term_subs=None): # XXX TODO calling compile twice returns different results! assert parser.options.maybe_placeholders == False + if term_subs is None: + term_subs = {} tokens, rules, _grammar_extra = parser.grammar.compile(parser.options.start) self.write_tokens = WriteTokensTransformer({t.name:t for t in tokens}, term_subs)