Skip to content

Commit

Permalink
Add documentation and error message to Reconstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
NasalDaemon committed Feb 5, 2025
1 parent dfe32cc commit a56d144
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lark/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class LarkOptions(Serialize):
- When ``False``, does nothing (default)
- When ``True``, caches to a temporary file in the local directory
- When given a string, caches to the path pointed by the string
cache_grammar
For use with ``cache`` option. When ``True``, the unanalyzed grammar is also included in the cache.
Useful for classes that require the ``Lark.grammar`` to be present (e.g. Reconstructor).
(default= ``False``)
regex
When True, uses the ``regex`` module instead of the stdlib ``re``.
g_regex_flags
Expand Down Expand Up @@ -336,7 +340,9 @@ def __init__(self, grammar: 'Union[Grammar, str, IO[str]]', **options) -> None:
# specific reason - we just want a username.
username = "unknown"

cache_fn = tempfile.gettempdir() + "/.lark_cache_%s_%s_%s_%s.tmp" % (username, cache_sha256, *sys.version_info[:2])

cache_fn = tempfile.gettempdir() + "/.lark_%s_%s_%s_%s_%s_%s.tmp" % (
"cache_grammar" if self.options.cache_grammar else "cache", username, cache_sha256, *sys.version_info[:2])

old_options = self.options
try:
Expand Down
4 changes: 4 additions & 0 deletions lark/reconstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing import Dict, Callable, Iterable, Optional

from .exceptions import ConfigurationError
from .lark import Lark
from .tree import Tree, ParseTree
from .visitors import Transformer_InPlace
Expand Down Expand Up @@ -80,6 +81,9 @@ class Reconstructor(TreeMatcher):
def __init__(self, parser: Lark, term_subs: Optional[Dict[str, Callable[[Symbol], str]]]=None) -> None:
TreeMatcher.__init__(self, parser)

if not hasattr(parser, 'grammar') and parser.options.cache:
raise ConfigurationError('Unanalyzed grammar not available from cached parser, use cache_grammar=True')

self.write_tokens = WriteTokensTransformer({t.name:t for t in self.tokens}, term_subs or {})

def _reconstruct(self, tree):
Expand Down

0 comments on commit a56d144

Please sign in to comment.