Skip to content

Commit ed834e6

Browse files
committed
Move parser.grammar check deeper into TreeMatcher
1 parent 81fbfe4 commit ed834e6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lark/reconstruct.py

-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from typing import Dict, Callable, Iterable, Optional
55

6-
from .exceptions import ConfigurationError
76
from .lark import Lark
87
from .tree import Tree, ParseTree
98
from .visitors import Transformer_InPlace
@@ -79,9 +78,6 @@ class Reconstructor(TreeMatcher):
7978
write_tokens: WriteTokensTransformer
8079

8180
def __init__(self, parser: Lark, term_subs: Optional[Dict[str, Callable[[Symbol], str]]]=None) -> None:
82-
if not hasattr(parser, 'grammar') and parser.options.cache:
83-
raise ConfigurationError('Unanalyzed grammar not available from cached parser, use cache_grammar=True')
84-
8581
TreeMatcher.__init__(self, parser)
8682

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

lark/tree_matcher.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import re
44
from collections import defaultdict
5+
from copy import deepcopy
56

67
from . import Tree, Token
78
from .common import ParserConf
9+
from .exceptions import ConfigurationError
810
from .parsers import earley
911
from .grammar import Rule, Terminal, NonTerminal
1012

@@ -89,8 +91,15 @@ class TreeMatcher:
8991
def __init__(self, parser):
9092
# XXX TODO calling compile twice returns different results!
9193
assert not parser.options.maybe_placeholders
94+
9295
# XXX TODO: we just ignore the potential existence of a postlexer
93-
self.tokens, rules, _extra = parser.grammar.compile(parser.options.start, set())
96+
if parser.options.postlex is None:
97+
self.tokens = deepcopy(parser.tokens)
98+
rules = deepcopy(parser.rules)
99+
else:
100+
if not hasattr(parser, 'grammar') and parser.options.cache:
101+
raise ConfigurationError('Unanalyzed grammar not available from cached parser, use cache_grammar=True')
102+
self.tokens, rules, _extra = parser.grammar.compile(parser.options.start, set())
94103

95104
self.rules_for_root = defaultdict(list)
96105

0 commit comments

Comments
 (0)