Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parse_tree_builder.py #1291

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lark/parse_tree_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def __init__(self, rules, tree_class, propagate_positions=False, ambiguous=False
self.ambiguous = ambiguous
self.maybe_placeholders = maybe_placeholders

self.rule_builders = list(self._init_builders(rules))
self.rule_builders = self._init_builders(rules)

def _init_builders(self, rules):
propagate_positions = make_propagate_positions(self.propagate_positions)
Expand All @@ -341,13 +341,13 @@ def _init_builders(self, rules):
keep_all_tokens = options.keep_all_tokens
expand_single_child = options.expand1

wrapper_chain = list(filter(None, [
wrapper_chain = filter(None, [
(expand_single_child and not rule.alias) and ExpandSingleChild,
maybe_create_child_filter(rule.expansion, keep_all_tokens, self.ambiguous, options.empty_indices if self.maybe_placeholders else None),
propagate_positions,
self.ambiguous and maybe_create_ambiguous_expander(self.tree_class, rule.expansion, keep_all_tokens),
self.ambiguous and partial(AmbiguousIntermediateExpander, self.tree_class)
]))
])

yield rule, wrapper_chain

Expand All @@ -362,6 +362,8 @@ def default_callback(data, children):
default_callback = self.tree_class

for rule, wrapper_chain in self.rule_builders:
if rule in callbacks:
raise GrammarError("Rule '%s' already exists" % (rule,))

user_callback_name = rule.alias or rule.options.template_source or rule.origin.name
try:
Expand All @@ -377,9 +379,6 @@ def default_callback(data, children):
for w in wrapper_chain:
f = w(f)

if rule in callbacks:
raise GrammarError("Rule '%s' already exists" % (rule,))

callbacks[rule] = f

return callbacks
Expand Down