From 2f6aaa6b38cef903205eb623a3be09bc2a4e7a04 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sun, 30 Jun 2024 15:42:53 +0200 Subject: [PATCH] Bugfix for issue #1434 --- lark/parsers/grammar_analysis.py | 8 ++++---- lark/utils.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lark/parsers/grammar_analysis.py b/lark/parsers/grammar_analysis.py index b52e50d5..28d3cb63 100644 --- a/lark/parsers/grammar_analysis.py +++ b/lark/parsers/grammar_analysis.py @@ -3,7 +3,7 @@ from collections import Counter, defaultdict from typing import List, Dict, Iterator, FrozenSet, Set -from ..utils import bfs, fzset, classify +from ..utils import bfs, fzset, classify, OrderedSet from ..exceptions import GrammarError from ..grammar import Rule, Terminal, NonTerminal, Symbol from ..common import ParserConf @@ -177,13 +177,13 @@ def __init__(self, parser_conf: ParserConf, debug: bool=False, strict: bool=Fals self.FIRST, self.FOLLOW, self.NULLABLE = calculate_sets(rules) - def expand_rule(self, source_rule: NonTerminal, rules_by_origin=None) -> State: + def expand_rule(self, source_rule: NonTerminal, rules_by_origin=None) -> OrderedSet[RulePtr]: "Returns all init_ptrs accessible by rule (recursive)" if rules_by_origin is None: rules_by_origin = self.rules_by_origin - init_ptrs = set() + init_ptrs = OrderedSet[RulePtr]() def _expand_rule(rule: NonTerminal) -> Iterator[NonTerminal]: assert not rule.is_term, rule @@ -200,4 +200,4 @@ def _expand_rule(rule: NonTerminal) -> Iterator[NonTerminal]: for _ in bfs([source_rule], _expand_rule): pass - return fzset(init_ptrs) + return init_ptrs diff --git a/lark/utils.py b/lark/utils.py index 04d6eae2..f7ab9e3e 100644 --- a/lark/utils.py +++ b/lark/utils.py @@ -341,3 +341,6 @@ def __bool__(self): def __len__(self) -> int: return len(self.d) + + def __repr__(self): + return f"{type(self).__name__}({', '.join(map(str,self))})"