Fixed #20: I can parse simple concepts

This commit is contained in:
2023-07-09 18:08:47 +02:00
parent ba397b0b72
commit 57f9ce2bbb
44 changed files with 2462 additions and 149 deletions
+109
View File
@@ -1,7 +1,11 @@
from common.global_symbols import NotInit
from common.utils import unstr_concept
from core.ExecutionContext import ExecutionContext
from core.ReturnValue import ReturnValue
from core.concept import Concept, ConceptDefaultProps, ConceptMetadata, DefinitionType
from parsers.ParserInput import ParserInput
from parsers.state_machine import MetadataToken, UnrecognizedToken
from parsers.tokenizer import Tokenizer
from services.SheerkaConceptManager import ConceptManager
ATTR_MAP = {
@@ -122,7 +126,34 @@ def get_evaluated_concept(blueprint: Concept | ConceptMetadata, **kwargs):
:return:
:rtype:
"""
def _isfloat(num):
try:
float(num)
return True
except ValueError:
return False
res = Concept(blueprint.get_metadata())
for attr in ATTR_MAP:
source_code = getattr(res.get_metadata(), attr)
if source_code == "" or source_code is None:
value = NotInit
elif source_code[0] in ("'", '"'):
value = source_code[1:-1]
elif source_code in ("True", "False"):
value = source_code == "True"
elif source_code.isdecimal():
value = int(source_code)
elif _isfloat(source_code):
value = float(source_code)
else:
raise Exception(f"Cannot manage {attr=}, {source_code=}")
setattr(res, ATTR_MAP[attr], value)
# force values
for k, v in kwargs.items():
res.set_value(ATTR_MAP.get(k, k), v)
@@ -347,6 +378,13 @@ def get_concepts(context: ExecutionContext, *concepts, **kwargs) -> list[Concept
return res
def get_evaluated_concepts(context, *concepts, use_sheerka=False) -> list[Concept]:
if use_sheerka:
return [context.sheerka.evaluate_concept(context, Concept(c.get_metadata())) for c in concepts]
else:
return [get_evaluated_concept(concept) for concept in concepts]
def define_new_concept(context: ExecutionContext, c: str | Concept | ConceptMetadata) -> Concept:
sheerka = context.sheerka
if isinstance(c, str):
@@ -381,6 +419,43 @@ def get_file_content(file_name):
return f.read()
def get_parser_input(text):
pi = ParserInput(text)
assert pi.init()
return pi
def get_from(*args, **kwargs):
"""
Convert the input to fix the positions
:param args:
:type args:
:return:
:rtype:
"""
cache = {} # I keep the name in cache to avoid having to remind it everytime
pos = 0
res = []
for item in args:
start = pos
if isinstance(item, MetadataToken):
if item.metadata.name:
cache[item.metadata.id] = item.metadata.name
tokens = list(Tokenizer(cache[item.metadata.id], yield_eof=False))
pos += len(tokens)
resolution_method = kwargs.get("resolution_method", item.resolution_method)
parser = kwargs.get("parser", item.parser)
res.append(MetadataToken(item.metadata, start, pos - 1, resolution_method, parser))
elif isinstance(item, UnrecognizedToken):
tokens = list(Tokenizer(item.buffer, yield_eof=False))
pos += len(tokens)
res.append(UnrecognizedToken(item.buffer, start, pos - 1))
return res
def _rv(value, who="Test"):
return ReturnValue(who=who, status=True, value=value)
@@ -400,3 +475,37 @@ def _rvf(value, who="Test"):
:rtype:
"""
return ReturnValue(who=who, status=False, value=value)
def _ut(buffer, start=0, end=-1):
"""
helper to UnrecognizedToken
:param buffer:
:type buffer:
:param start:
:type start:
:param end:
:type end:
:return:
:rtype:
"""
return UnrecognizedToken(buffer, start, end)
def _mt(concept_id, start=0, end=-1, resolution_method="id", parser="simple"):
"""
helper to MetadataToken
:param concept_id:
:type concept_id:
:param start:
:type start:
:param end:
:type end:
:return:
:rtype:
"""
name, _id = unstr_concept(concept_id)
if _id is None:
return MetadataToken(get_metadata(id=concept_id), start, end, resolution_method, parser)
else:
return MetadataToken(get_metadata(id=_id, name=name), start, end, resolution_method, parser)