From 73a6d4e6c2f2c0121aa70e238be3472ef8b21842 Mon Sep 17 00:00:00 2001 From: Kodjo Sossouvi Date: Sat, 11 Jan 2020 16:03:30 +0100 Subject: [PATCH] Minor code enhancements --- core/sheerka.py | 43 +++++++++++++------------- evaluators/PythonEvaluator.py | 7 ++++- parsers/PythonWithConceptsParser.py | 18 +++++------ tests/test_PythonWithConceptsParser.py | 5 ++- tests/test_sheerka_non_reg.py | 36 +++++++++++++++++++++ 5 files changed, 75 insertions(+), 34 deletions(-) diff --git a/core/sheerka.py b/core/sheerka.py index 538ba70..bebc4c6 100644 --- a/core/sheerka.py +++ b/core/sheerka.py @@ -933,28 +933,29 @@ class Sheerka(Concept): defs = self.sdp.get(self.CONCEPTS_DEFINITIONS_ENTRY) self.log.info(defs) - def dump_desc(self, concept_name): - if isinstance(concept_name, Concept): - concepts = concept_name - else: - concepts = self.get(concept_name) - if self.isinstance(concepts, BuiltinConcepts.UNKNOWN_CONCEPT): - self.log.error("Concept unknown") - return False - - if not hasattr(concepts, "__iter__"): - concepts = [concepts] - + def dump_desc(self, *concept_names): first = True - for c in concepts: - if not first: - self.log.info("") - self.log.info(f"name : {c.name}") - self.log.info(f"bnf : {c.metadata.definition}") - self.log.info(f"key : {c.key}") - self.log.info(f"body : {c.body}") - self.log.info(f"digest : {c.get_digest()}") - first = False + for concept_name in concept_names: + if isinstance(concept_name, Concept): + concepts = concept_name + else: + concepts = self.get(concept_name) + if self.isinstance(concepts, BuiltinConcepts.UNKNOWN_CONCEPT): + self.log.error(f"Concept '{concept_name}' is unknown") + return False + + if not hasattr(concepts, "__iter__"): + concepts = [concepts] + + for c in concepts: + if not first: + self.log.info("") + self.log.info(f"name : {c.name}") + self.log.info(f"bnf : {c.metadata.definition}") + self.log.info(f"key : {c.key}") + self.log.info(f"body : {c.body}") + self.log.info(f"digest : {c.get_digest()}") + first = False @staticmethod def get_builtins_classes_as_dict(): diff --git a/evaluators/PythonEvaluator.py b/evaluators/PythonEvaluator.py index 1be83fe..971077c 100644 --- a/evaluators/PythonEvaluator.py +++ b/evaluators/PythonEvaluator.py @@ -59,7 +59,12 @@ class PythonEvaluator(OneReturnValueEvaluator): return sheerka.ret(self.name, False, error, parents=[return_value]) def get_locals(self, context, node): - my_locals = {"sheerka": context.sheerka} + my_locals = { + "sheerka": context.sheerka, + "desc": context.sheerka.dump_desc, + "concepts": context.sheerka.dump_concepts, + "definitions": context.sheerka.dump_definitions, + } if context.obj: context.log(self.verbose_log, f"Concept '{context.obj}' is in context. Adding its properties to locals if any.", self.name) diff --git a/parsers/PythonWithConceptsParser.py b/parsers/PythonWithConceptsParser.py index 7d4d957..8e4b5ea 100644 --- a/parsers/PythonWithConceptsParser.py +++ b/parsers/PythonWithConceptsParser.py @@ -1,8 +1,11 @@ from core.builtin_concepts import BuiltinConcepts from parsers.BaseParser import BaseParser -from parsers.ConceptLexerParser import UnrecognizedTokensNode, ConceptNode +from parsers.ConceptLexerParser import ConceptNode +from parsers.MultipleConceptsParser import MultipleConceptsParser from parsers.PythonParser import PythonParser +multiple_concepts_parser = MultipleConceptsParser() + class PythonWithConceptsParser(BaseParser): def __init__(self, **kwargs): @@ -22,16 +25,10 @@ class PythonWithConceptsParser(BaseParser): if not sheerka.isinstance(text, BuiltinConcepts.PARSER_RESULT): return None + if not text.parser == multiple_concepts_parser: + return None + nodes = text.body - if not isinstance(nodes, list): - return None - - if len(nodes) == 0: - return None - - if not isinstance(nodes[0], (ConceptNode, UnrecognizedTokensNode)): - return None - source = "" to_parse = "" identifiers = {} @@ -107,7 +104,6 @@ class PythonWithConceptsParser(BaseParser): if id(concept) in self.identifiers: return self.identifiers[id(concept)] - identifier = "__C__" + (concept.key or concept.name) if concept.id: identifier += "__" + concept.id diff --git a/tests/test_PythonWithConceptsParser.py b/tests/test_PythonWithConceptsParser.py index 8ebe1a0..2e1dff7 100644 --- a/tests/test_PythonWithConceptsParser.py +++ b/tests/test_PythonWithConceptsParser.py @@ -7,10 +7,13 @@ from core.concept import Concept from core.sheerka import Sheerka, ExecutionContext from core.tokenizer import Token, TokenKind, Tokenizer from parsers.ConceptLexerParser import ConceptNode, UnrecognizedTokensNode +from parsers.MultipleConceptsParser import MultipleConceptsParser from parsers.PythonParser import PythonNode, PythonErrorNode from parsers.PythonWithConceptsParser import PythonWithConceptsParser from sdp.sheerkaDataProvider import Event +multiple_concepts_parser = MultipleConceptsParser() + def get_context(): sheerka = Sheerka(skip_builtins_in_db=True) @@ -31,7 +34,7 @@ def get_ret_from(*args): result.append(UnrecognizedTokensNode(index, index + len(tokens) - 1, tokens)) index += len(tokens) - return ReturnValueConcept("who", False, ParserResultConcept(parser="name", value=result)) + return ReturnValueConcept("who", False, ParserResultConcept(parser=multiple_concepts_parser, value=result)) def to_str_ast(expression): diff --git a/tests/test_sheerka_non_reg.py b/tests/test_sheerka_non_reg.py index 3557dd2..a0cb356 100644 --- a/tests/test_sheerka_non_reg.py +++ b/tests/test_sheerka_non_reg.py @@ -427,6 +427,42 @@ def test_i_can_eval_a_mix_with_bnf_and_python_when_rule_name(): assert res[0].status assert res[0].body == 22 + res = sheerka.evaluate_user_input("twenty one + twenty two") + assert len(res) == 1 + assert res[0].status + assert res[0].body == 43 + + res = sheerka.evaluate_user_input("twenty one + one") + assert len(res) == 1 + assert res[0].status + assert res[0].body == 22 + + +def test_i_can_eval_a_mix_with_bnf_and_python_when_rule_name_2(): + sheerka = get_sheerka() + + sheerka.evaluate_user_input("def concept one as 1") + sheerka.evaluate_user_input("def concept two as 2") + sheerka.evaluate_user_input("def concept twenty as 20") + sheerka.evaluate_user_input("def concept twenties from bnf twenty (one | two)=unit as twenty + unit") + + assert sheerka.evaluate_user_input("eval twenty one")[0].body == 21 + + res = sheerka.evaluate_user_input("twenty one + 1") + assert len(res) == 1 + assert res[0].status + assert res[0].body == 22 + + res = sheerka.evaluate_user_input("twenty one + twenty two") + assert len(res) == 1 + assert res[0].status + assert res[0].body == 43 + + res = sheerka.evaluate_user_input("twenty one + one") + assert len(res) == 1 + assert res[0].status + assert res[0].body == 22 + def test_i_can_eval_a_more_complicated_mix_with_bnf_and_python(): sheerka = get_sheerka()