I can also get concept by name
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, ParserResultConcept
|
||||
from core.concept import ConceptParts
|
||||
from core.concept import ConceptParts, DEFINITION_TYPE_BNF, DEFINITION_TYPE_DEF
|
||||
import core.builtin_helpers
|
||||
import core.utils
|
||||
from parsers.BaseParser import BaseParser, Node, ErrorNode, NotInitializedNode
|
||||
@@ -84,6 +84,7 @@ class DefConceptNode(DefaultParserNode):
|
||||
post: ReturnValueConcept = NotInitializedNode()
|
||||
body: ReturnValueConcept = NotInitializedNode()
|
||||
definition: ReturnValueConcept = NotInitializedNode()
|
||||
definition_type: str = None
|
||||
|
||||
def get_asts(self):
|
||||
asts = {}
|
||||
@@ -93,7 +94,7 @@ class DefConceptNode(DefaultParserNode):
|
||||
ParserResultConcept) and hasattr(
|
||||
prop_value.body.body, "ast_"):
|
||||
asts[part_key] = prop_value
|
||||
#asts[part_key] = prop_value.body.body.ast_
|
||||
# asts[part_key] = prop_value.body.body.ast_
|
||||
return asts
|
||||
|
||||
|
||||
@@ -244,14 +245,15 @@ class DefaultParser(BaseParser):
|
||||
keywords_tokens = [def_token]
|
||||
concept_found = DefConceptNode(keywords_tokens)
|
||||
|
||||
# the definition of a concept consists of several parts
|
||||
# Keywords.CONCEPT to get the name of the concept
|
||||
# Keywords.FROM [Keywords.BNF] to get the definition of the concept
|
||||
# Keywords.AS to get the body
|
||||
# Keywords.WHERE to get the conditions to recognize for the variables
|
||||
# Keywords.PRE to know if the conditions to evaluate the concept
|
||||
# Keywords.POST to apply or verify once the concept is executed
|
||||
#
|
||||
# ##
|
||||
# ## the definition of a concept consists of several parts
|
||||
# ## Keywords.CONCEPT to get the name of the concept
|
||||
# ## Keywords.FROM [Keywords.BNF] | [Keywords.DEF] to get the definition of the concept
|
||||
# ## Keywords.AS to get the body
|
||||
# ## Keywords.WHERE to get the conditions to recognize for the variables
|
||||
# ## Keywords.PRE to know if the conditions to evaluate the concept
|
||||
# ## Keywords.POST to apply or verify once the concept is executed
|
||||
|
||||
# Regroup the tokens by parts
|
||||
first_token, tokens_found_by_parts = self.regroup_tokens_by_parts(keywords_tokens)
|
||||
|
||||
@@ -262,7 +264,9 @@ class DefaultParser(BaseParser):
|
||||
concept_found.name = self.get_concept_name(first_token, tokens_found_by_parts)
|
||||
|
||||
# get the definition
|
||||
concept_found.definition = self.get_concept_definition(concept_found, tokens_found_by_parts)
|
||||
def_type, def_value = self.get_concept_definition(concept_found, tokens_found_by_parts)
|
||||
concept_found.definition_type = def_type
|
||||
concept_found.definition = def_value
|
||||
|
||||
# get the ASTs for the remaining parts
|
||||
asts_found_by_parts = self.get_concept_parts(tokens_found_by_parts)
|
||||
@@ -362,16 +366,23 @@ class DefaultParser(BaseParser):
|
||||
|
||||
def get_concept_definition(self, current_concept_def, tokens_found_by_parts):
|
||||
if tokens_found_by_parts[Keywords.FROM] is None:
|
||||
return NotInitializedNode()
|
||||
return None, NotInitializedNode()
|
||||
|
||||
definition_tokens = tokens_found_by_parts[Keywords.FROM]
|
||||
if definition_tokens[1].value != Keywords.BNF:
|
||||
return NotInitializedNode()
|
||||
if len(definition_tokens) == 1:
|
||||
self.add_error(SyntaxErrorNode([], "Empty declaration"), False)
|
||||
return None, NotInitializedNode()
|
||||
|
||||
if definition_tokens[1].value == Keywords.BNF:
|
||||
return self.get_concept_bnf_definition(current_concept_def, definition_tokens)
|
||||
|
||||
return self.get_concept_simple_definition(definition_tokens)
|
||||
|
||||
def get_concept_bnf_definition(self, current_concept_def, definition_tokens):
|
||||
tokens = core.utils.strip_tokens(definition_tokens[2:])
|
||||
if len(tokens) == 0:
|
||||
self.add_error(SyntaxErrorNode([definition_tokens[1]], "Empty declaration"), False)
|
||||
return NotInitializedNode()
|
||||
return None, NotInitializedNode()
|
||||
|
||||
regex_parser = BnfParser()
|
||||
with self.context.push(self.name, obj=current_concept_def) as sub_context:
|
||||
@@ -380,9 +391,18 @@ class DefaultParser(BaseParser):
|
||||
|
||||
if not parsing_result.status:
|
||||
self.add_error(parsing_result.value)
|
||||
return NotInitializedNode()
|
||||
return None, NotInitializedNode()
|
||||
|
||||
return parsing_result
|
||||
return DEFINITION_TYPE_BNF, parsing_result
|
||||
|
||||
def get_concept_simple_definition(self, definition_tokens):
|
||||
start = 2 if definition_tokens[1].value == Keywords.DEF else 1
|
||||
tokens = core.utils.strip_tokens(definition_tokens[start:])
|
||||
if len(tokens) == 0:
|
||||
self.add_error(SyntaxErrorNode([definition_tokens[start]], "Empty declaration"), False)
|
||||
return None, NotInitializedNode()
|
||||
|
||||
return DEFINITION_TYPE_DEF, NameNode(tokens)
|
||||
|
||||
def get_concept_parts(self, tokens_found_by_parts):
|
||||
asts_found_by_parts = {
|
||||
|
||||
Reference in New Issue
Block a user