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 -18
View File
@@ -11,6 +11,7 @@ from dataclasses import dataclass
from operator import attrgetter
import core.builtin_helpers
import core.utils
from cache.Cache import Cache
from core.builtin_concepts import BuiltinConcepts
from core.concept import DEFINITION_TYPE_BNF, DoNotResolve, ConceptParts, Concept
@@ -19,7 +20,7 @@ from core.tokenizer import Tokenizer, TokenKind, Token
from parsers.BaseNodeParser import BaseNodeParser, GrammarErrorNode, UnrecognizedTokensNode, ConceptNode, LexerNode
from parsers.BaseParser import BaseParser
PARSERS = ["AtomNode", "SyaNode", "Python"]
PARSERS = ["Sequence", "Sya", "Python"]
@dataclass
@@ -41,7 +42,7 @@ class ParsingContext:
:return:
"""
self.node.tokens = parser_helper.parser.parser_input.tokens[self.node.start: self.node.end + 1]
self.node.source = BaseParser.get_text_from_tokens(self.node.tokens)
self.node.source = core.utils.get_text_from_tokens(self.node.tokens)
def __mul__(self, other):
res = [self]
@@ -1044,17 +1045,17 @@ class BnfConceptParserHelper:
Adds a new entry,
makes a list if the property already exists
"""
if prop_name not in _concept.compiled or _concept.compiled[prop_name] is None:
if prop_name not in _concept.get_compiled() or _concept.get_compiled()[prop_name] is None:
# new entry
_concept.compiled[prop_name] = value
_concept.get_compiled()[prop_name] = value
else:
# make a list if there was a value
previous_value = _concept.compiled[prop_name]
previous_value = _concept.get_compiled()[prop_name]
if isinstance(previous_value, list):
previous_value.append(value)
else:
new_value = [previous_value, value]
_concept.compiled[prop_name] = new_value
_concept.get_compiled()[prop_name] = new_value
def _look_for_concept_match(_underlying):
"""
@@ -1094,18 +1095,18 @@ class BnfConceptParserHelper:
if _underlying.parsing_expression.rule_name:
value = _get_underlying_value(_underlying)
_add_prop(_concept, _underlying.parsing_expression.rule_name, value)
_concept.metadata.need_validation = True
_concept.get_metadata().need_validation = True
elif isinstance(_underlying, NonTerminalNode):
for child in _underlying.children:
_process_rule_name(_concept, child)
if init_empty_body and concept.metadata.body is None:
if init_empty_body and concept.get_metadata().body is None:
value = _get_underlying_value(underlying)
concept.compiled[ConceptParts.BODY] = value
concept.get_compiled()[ConceptParts.BODY] = value
if underlying.parsing_expression.rule_name:
_add_prop(concept, underlying.parsing_expression.rule_name, value)
# KSI : Why don't we set concept.metadata.need_validation to True ?
# KSI : Why don't we set concept.get_metadata().need_validation to True ?
if isinstance(underlying, NonTerminalNode) and not isinstance(underlying.parsing_expression, ConceptExpression):
for node in underlying.children:
@@ -1147,8 +1148,11 @@ class ToUpdate:
class BnfNodeParser(BaseNodeParser):
NAME = "Bnf"
def __init__(self, **kwargs):
super().__init__("BnfNode", 50, **kwargs)
super().__init__(BnfNodeParser.NAME, 50, **kwargs)
if 'sheerka' in kwargs:
sheerka = kwargs.get("sheerka")
@@ -1162,11 +1166,11 @@ class BnfNodeParser(BaseNodeParser):
@staticmethod
def _is_eligible(concept):
"""
Predicate that select concepts that must handled by AtomNodeParser
Predicate that select concepts that must handled by BnfNodeParser
:param concept:
:return:
"""
return concept.metadata.definition_type == DEFINITION_TYPE_BNF
return concept.get_metadata().definition_type == DEFINITION_TYPE_BNF
@staticmethod
def get_valid(parsers_helpers):
@@ -1422,7 +1426,6 @@ class BnfNodeParser(BaseNodeParser):
with context.push(BuiltinConcepts.INIT_BNF, concept,
who=self.name,
obj=concept,
root_concept=concept,
desc=desc) as sub_context:
# get the parsing expression
to_skip = {concept.id}
@@ -1500,13 +1503,13 @@ class BnfNodeParser(BaseNodeParser):
desc = f"Resolve concept parsing expression for '{concept}'. {key_to_use=}"
with context.push(BuiltinConcepts.INIT_BNF, concept, who=self.name, obj=concept, desc=desc) as sub_context:
if not concept.bnf: # 'if' is done outside to save a function call. Not sure it worth it.
if not concept.get_bnf(): # 'if' is done outside to save a function call. Not sure it worth it.
BaseNodeParser.ensure_bnf(sub_context, concept, self.name)
grammar[key_to_use] = UnderConstruction(concept.id)
if concept.metadata.definition_type == DEFINITION_TYPE_BNF:
expression = concept.bnf
if concept.get_metadata().definition_type == DEFINITION_TYPE_BNF:
expression = concept.get_bnf()
desc = f"Bnf concept detected. Resolving parsing expression '{expression}'"
with sub_context.push(BuiltinConcepts.INIT_BNF, concept, who=self.name, obj=concept, desc=desc) as ssc:
ssc.add_inputs(expression=expression)
@@ -1630,7 +1633,7 @@ class BnfNodeParser(BaseNodeParser):
if isinstance(concept, Concept):
return concept
if concept in context.concepts:
if context.concepts and concept in context.concepts:
return context.concepts[concept]
return self.sheerka.get_by_key(concept)