First steps of ConceptLexer. Need to update DefaultParser before continuing
This commit is contained in:
+48
-9
@@ -1,6 +1,7 @@
|
||||
from core.builtin_concepts import BuiltinConcepts, ListConcept
|
||||
from core.concept import Concept
|
||||
import ast
|
||||
import core.utils
|
||||
|
||||
import logging
|
||||
|
||||
@@ -45,18 +46,18 @@ class NodeParent:
|
||||
|
||||
|
||||
class NodeConcept(Concept):
|
||||
def __init__(self, key, parent: NodeParent):
|
||||
def __init__(self, key, node_type, parent: NodeParent):
|
||||
super().__init__(key, True, False, key)
|
||||
self.parent = parent
|
||||
self.node_type = node_type
|
||||
|
||||
def get_node_type(self):
|
||||
return self.key
|
||||
return self.node_type
|
||||
|
||||
|
||||
class GenericNodeConcept(NodeConcept):
|
||||
def __init__(self, node_type, parent):
|
||||
super().__init__(BuiltinConcepts.GENERIC_NODE, parent)
|
||||
self.node_type = node_type
|
||||
super().__init__(BuiltinConcepts.GENERIC_NODE, node_type, parent)
|
||||
|
||||
def __repr__(self):
|
||||
return "Generic:" + self.node_type
|
||||
@@ -74,17 +75,25 @@ class GenericNodeConcept(NodeConcept):
|
||||
return self.body
|
||||
|
||||
|
||||
class IdentifierConcept(NodeConcept):
|
||||
class IdentifierNodeConcept(NodeConcept):
|
||||
def __init__(self, parent, name):
|
||||
super().__init__(BuiltinConcepts.IDENTIFIER_NODE, parent)
|
||||
super().__init__(BuiltinConcepts.IDENTIFIER_NODE, "Name", parent)
|
||||
self.body = name
|
||||
|
||||
|
||||
def transform(node):
|
||||
class CallNodeConcept(NodeConcept):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(BuiltinConcepts.IDENTIFIER_NODE, "Call", parent)
|
||||
|
||||
def get_args_names(self, sheerka):
|
||||
return sheerka.values(self.get_prop("args"))
|
||||
|
||||
|
||||
def python_to_concept(python_node):
|
||||
"""
|
||||
Transform Python AST node into concept nodes
|
||||
for better usage
|
||||
:param node:
|
||||
:param python_node:
|
||||
:return:
|
||||
"""
|
||||
|
||||
@@ -107,4 +116,34 @@ def transform(node):
|
||||
concept.set_prop(field, value)
|
||||
return concept
|
||||
|
||||
return _transform(node, None)
|
||||
return _transform(python_node, None)
|
||||
|
||||
|
||||
def concept_to_python(concept_node):
|
||||
"""
|
||||
Transform back concept_node to Python AST node
|
||||
:param concept_node:
|
||||
:return:
|
||||
"""
|
||||
|
||||
def _transform(node):
|
||||
node_type = node.get_node_type()
|
||||
ast_object = core.utils.new_object("_ast." + node_type)
|
||||
for field in node.props:
|
||||
if field not in ast_object._fields:
|
||||
continue
|
||||
|
||||
value = node.get_prop(field)
|
||||
if isinstance(value, list) or isinstance(value, Concept) and value.key == str(BuiltinConcepts.LIST):
|
||||
lst = []
|
||||
for i in value:
|
||||
lst.append(_transform(i))
|
||||
setattr(ast_object, field, lst)
|
||||
elif isinstance(value, NodeConcept):
|
||||
setattr(ast_object, field, _transform(value))
|
||||
else:
|
||||
setattr(ast_object, field, value)
|
||||
return ast_object
|
||||
|
||||
res = _transform(concept_node)
|
||||
return res
|
||||
|
||||
+11
-3
@@ -57,13 +57,13 @@ class UnreferencedNamesVisitor(ConceptNodeVisitor):
|
||||
|
||||
def visit_Name(self, node):
|
||||
parents = get_parents(node)
|
||||
if ("For", "target") in parents: # variable used by the 'for' iteration
|
||||
if ("For", "target") in parents: # variable used by the 'for' iteration
|
||||
return
|
||||
|
||||
if ("Call", "func") in parents: # name of the function
|
||||
if ("Call", "func") in parents: # name of the function
|
||||
return
|
||||
|
||||
if ("Assign", "targets") in parents: # variable which is assigned
|
||||
if ("Assign", "targets") in parents: # variable which is assigned
|
||||
return
|
||||
|
||||
if self.can_be_discarded(self.sheerka.value(node), parents):
|
||||
@@ -91,6 +91,14 @@ class UnreferencedNamesVisitor(ConceptNodeVisitor):
|
||||
return False
|
||||
|
||||
|
||||
class ExtractPredicateVisitor(ConceptNodeVisitor):
|
||||
def __init__(self, variable_name):
|
||||
self.predicates = []
|
||||
self.variable_name = variable_name
|
||||
|
||||
|
||||
|
||||
|
||||
def get_parents(node):
|
||||
if node.parent is None:
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user