Implemented some enhancement requests
This commit is contained in:
@@ -1341,72 +1341,6 @@ class BnfNodeParser(BaseNodeParser):
|
||||
debugger.debug_var("result", concept_parser_helpers)
|
||||
return concept_parser_helpers
|
||||
|
||||
def fix_infinite_recursions(self, context, grammar, concept_id, parsing_expression):
|
||||
"""
|
||||
Check the newly created parsing expression
|
||||
Some infinite recursion can be resolved, simply by removing the pexpression that causes the loop
|
||||
Let's look for that
|
||||
:param context:
|
||||
:param grammar:
|
||||
:param concept_id:
|
||||
:param parsing_expression:
|
||||
:return:
|
||||
"""
|
||||
|
||||
def _find(expression_, path_):
|
||||
index_ = -1
|
||||
parent_ = None
|
||||
for node_id in path_:
|
||||
expression_ = expression_.nodes[0] if isinstance(expression_, ConceptExpression) else expression_
|
||||
for i, node in [(i, n) for i, n in enumerate(expression_.nodes) if isinstance(n, ConceptExpression)]:
|
||||
if node_id == node.concept.id:
|
||||
index_ = i
|
||||
parent_ = expression_
|
||||
expression_ = node # take the child of the ConceptExpression found
|
||||
break
|
||||
else:
|
||||
raise IndexError(f"path {path_} cannot be found in '{expression_}'")
|
||||
|
||||
return parent_, index_, expression_
|
||||
|
||||
def _fix_node(expression, path):
|
||||
parent, index, expression_update = _find(expression, path[1:-2])
|
||||
|
||||
assert isinstance(expression_update, ConceptExpression)
|
||||
|
||||
desc = f"Fixing circular reference {path}"
|
||||
with context.push(BuiltinConcepts.INIT_BNF,
|
||||
expression_update.concept,
|
||||
who=self.name,
|
||||
obj=expression_update.concept,
|
||||
concepts_to_skip=[concept_id],
|
||||
desc=desc) as sub_context:
|
||||
new_grammar = grammar.copy()
|
||||
for node_id in path[-2:]:
|
||||
del new_grammar[node_id]
|
||||
new_nodes = self.resolve_concept_parsing_expression(sub_context,
|
||||
expression_update.concept,
|
||||
expression_update.rule_name, new_grammar, set())
|
||||
new = ConceptExpression(expression_update.concept,
|
||||
rule_name=expression_update.rule_name,
|
||||
nodes=new_nodes)
|
||||
|
||||
parent.nodes[index] = new
|
||||
|
||||
while True:
|
||||
already_found = [concept_id]
|
||||
concepts_in_recursion = []
|
||||
if self.check_for_infinite_recursion(parsing_expression, already_found, concepts_in_recursion):
|
||||
if "#" in concepts_in_recursion[-2]:
|
||||
# means that it's isaset concept
|
||||
_fix_node(parsing_expression, concepts_in_recursion[:-1])
|
||||
else:
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
return concepts_in_recursion
|
||||
|
||||
def check_for_infinite_recursion(self, parsing_expression, already_found, in_recursion, only_first=False):
|
||||
|
||||
if isinstance(parsing_expression, ConceptExpression):
|
||||
|
||||
Reference in New Issue
Block a user