Added first version of DebugManager. Implemented draft of the rule engine

This commit is contained in:
2020-11-20 13:41:45 +01:00
parent cd066881b4
commit 315f8ea09b
156 changed files with 8388 additions and 2852 deletions
+21 -20
View File
@@ -7,6 +7,7 @@ from core import builtin_helpers
from core.builtin_concepts import BuiltinConcepts
from core.builtin_helpers import parse_function
from core.concept import Concept, DEFINITION_TYPE_BNF
from core.global_symbols import CONCEPT_COMPARISON_CONTEXT
from core.sheerka.services.SheerkaComparisonManager import SheerkaComparisonManager
from core.sheerka.services.SheerkaExecute import ParserInput
from core.tokenizer import Token, TokenKind, Tokenizer
@@ -15,7 +16,7 @@ from parsers.BaseNodeParser import UnrecognizedTokensNode, ConceptNode, SourceCo
SourceCodeWithConceptNode, BaseNodeParser
from parsers.BaseParser import ErrorNode
PARSERS = ["BnfNode", "AtomNode", "Python"]
PARSERS = ["Sequence", "Bnf", "Python"]
function_parser_res = namedtuple("FunctionParserRes", 'to_out function')
@@ -159,7 +160,7 @@ class SyaConceptParserHelper:
return len(self.expected) == 0
def is_atom(self):
return len(self.concept.concept.metadata.variables) == 0 and len(self.expected) == 0
return len(self.concept.concept.get_metadata().variables) == 0 and len(self.expected) == 0
def is_next(self, token):
"""
@@ -1056,9 +1057,10 @@ class PostFixToItem:
class SyaNodeParser(BaseNodeParser):
NAME = "Sya"
def __init__(self, **kwargs):
super().__init__("SyaNode", 50, **kwargs)
super().__init__(SyaNodeParser.NAME, 50, **kwargs)
if 'sheerka' in kwargs:
sheerka = kwargs.get("sheerka")
self.sya_definitions = sheerka.resolved_sya_def
@@ -1085,16 +1087,15 @@ class SyaNodeParser(BaseNodeParser):
@staticmethod
def _is_eligible(concept):
"""
Predicate that select concepts that must handled by AtomNodeParser
Predicate that select concepts that must handled by SyaNodeParser
:param concept:
:return:
"""
# We only concepts that has parameter (refuse atoms)
# Bnf definitions are not supposed to be managed by this parser either
return len(concept.metadata.variables) > 0 and concept.metadata.definition_type != DEFINITION_TYPE_BNF
return len(concept.get_metadata().variables) > 0 and concept.get_metadata().definition_type != DEFINITION_TYPE_BNF
@staticmethod
def _get_sya_concept_def(parser, concept):
def _get_sya_concept_def(self, parser, concept):
sya_concept_def = SyaConceptDef(concept)
if concept.id in parser.sya_definitions:
# Manage when precedence and associativity are given in the unit tests
@@ -1105,9 +1106,9 @@ class SyaNodeParser(BaseNodeParser):
sya_concept_def.associativity = sya_def[1]
if parser.sheerka:
concept_weight = parser.sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE)
if concept.id in concept_weight:
sya_concept_def.precedence = concept_weight[concept.id]
concept_weight = parser.sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE, CONCEPT_COMPARISON_CONTEXT)
if concept.str_id in concept_weight:
sya_concept_def.precedence = concept_weight[concept.str_id]
if associativity := concept.get_prop(BuiltinConcepts.ASSOCIATIVITY):
sya_concept_def.associativity = SyaAssociativity(associativity)
@@ -1137,7 +1138,7 @@ class SyaNodeParser(BaseNodeParser):
res.extend(forked)
forked.clear()
res = [InFixToPostFix(context, context.in_context(BuiltinConcepts.DEBUG))]
res = [InFixToPostFix(context, context.debug_enabled)]
while self.parser_input.next_token(False):
for infix_to_postfix in res:
infix_to_postfix.reset()
@@ -1184,13 +1185,13 @@ class SyaNodeParser(BaseNodeParser):
infix_to_postfix.finalize(self.parser_input.pos)
_add_forked_to_res()
if context.in_context(BuiltinConcepts.DEBUG):
context.debug(f"Parsing {parser_input}")
context.debug(f"{len(res)} InfixToPostFix(s) found")
if context.debug_enabled:
context.debug(self.name, "infix_to_postfix", None, f"Parsing {parser_input}")
context.debug(self.name, "infix_to_postfix", "nb_found", f"{len(res)} InfixToPostFix(s) found")
for i, r in enumerate(res):
context.debug(f"#{i}")
context.debug(self.name, "infix_to_postfix", "infix_to_postfix", f"#{i}")
for line in r.debug:
context.debug(line)
context.debug(self.name, "infix_to_postfix", "infix_to_postfix", line)
return res
@@ -1221,21 +1222,21 @@ class SyaNodeParser(BaseNodeParser):
end = item.end
has_unrecognized = False
concept = sheerka.new_from_template(item.concept, item.concept.key)
for param_index in reversed(range(len(concept.metadata.variables))):
for param_index in reversed(range(len(concept.get_metadata().variables))):
inner_item = self.postfix_to_item(sheerka, postfixed)
if inner_item.start < start:
start = inner_item.start
if inner_item.end > end:
end = inner_item.end
has_unrecognized |= isinstance(inner_item, (UnrecognizedTokensNode, SourceCodeWithConceptNode)) or \
hasattr(inner_item, "has_unrecognized") and inner_item.has_unrecognized
hasattr(inner_item, "has_unrecognized") and inner_item.has_unrecognized
param_name = concept.metadata.variables[param_index][0]
param_name = concept.get_metadata().variables[param_index][0]
param_value = inner_item.concept if hasattr(inner_item, "concept") else \
[inner_item.return_value] if isinstance(inner_item, SourceCodeNode) else \
inner_item
concept.compiled[param_name] = param_value
concept.get_compiled()[param_name] = param_value
return PostFixToItem(concept, start, end, has_unrecognized)