Skip to content

Commit

Permalink
Tiny refactor for PR #1451
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Aug 30, 2024
1 parent 95e9700 commit 6b52f72
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lark/parsers/earley.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ def parse(self, lexer, start):
if not solutions:
expected_terminals = [t.expect.name for t in to_scan]
raise UnexpectedEOF(expected_terminals, state=frozenset(i.s for i in to_scan))
if len(solutions) > 1:
raise RuntimeError('Earley should not generate multiple start symbol items! Please report this bug.')

Check warning on line 290 in lark/parsers/earley.py

View check run for this annotation

Codecov / codecov/patch

lark/parsers/earley.py#L290

Added line #L290 was not covered by tests
solution ,= solutions

if self.debug:
from .earley_forest import ForestToPyDotVisitor
Expand All @@ -294,18 +297,16 @@ def parse(self, lexer, start):
except ImportError:
logger.warning("Cannot find dependency 'pydot', will not generate sppf debug image")
else:
debug_walker.visit(solutions[0], "sppf.png")
debug_walker.visit(solution, "sppf.png")

Check warning on line 300 in lark/parsers/earley.py

View check run for this annotation

Codecov / codecov/patch

lark/parsers/earley.py#L300

Added line #L300 was not covered by tests

if len(solutions) > 1:
assert False, 'Earley should not generate multiple start symbol items!'

if self.Tree is not None:
# Perform our SPPF -> AST conversion
# Disable the ForestToParseTree cache when ambiguity='resolve'
# to prevent a tree construction bug. See issue #1283
use_cache = not self.resolve_ambiguity
transformer = ForestToParseTree(self.Tree, self.callbacks, self.forest_sum_visitor and self.forest_sum_visitor(), self.resolve_ambiguity, use_cache)
return transformer.transform(solutions[0])
return transformer.transform(solution)

# return the root of the SPPF
return solutions[0]
return solution
2 changes: 1 addition & 1 deletion lark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def is_id_start(s: str) -> bool:
return _test_unicode_category(s, _ID_START)


def dedup_list(l: Sequence[T]) -> List[T]:
def dedup_list(l: Iterable[T]) -> List[T]:
"""Given a list (l) will removing duplicates from the list,
preserving the original order of the list. Assumes that
the list entries are hashable."""
Expand Down

0 comments on commit 6b52f72

Please sign in to comment.