Added ExactConceptParser
This commit is contained in:
+18
-13
@@ -26,36 +26,41 @@ class PythonNode(Node):
|
||||
|
||||
|
||||
class PythonParser(BaseParser):
|
||||
def __init__(self, text, source="<undef>"):
|
||||
text = text if isinstance(text, str) else self.get_text_from_tokens(text)
|
||||
text = text.strip()
|
||||
BaseParser.__init__(self, "PythonParser", text)
|
||||
"""
|
||||
Parse Python scripts
|
||||
"""
|
||||
def __init__(self, source="<undef>"):
|
||||
|
||||
BaseParser.__init__(self, "PythonParser")
|
||||
self.source = source
|
||||
|
||||
def parse(self):
|
||||
def parse(self, context, text):
|
||||
text = text if isinstance(text, str) else self.get_text_from_tokens(text)
|
||||
text = text.strip()
|
||||
|
||||
# first, try to parse an expression
|
||||
res, tree, error = self.try_parse_expression()
|
||||
res, tree, error = self.try_parse_expression(text)
|
||||
if not res:
|
||||
# then try to parse a statement
|
||||
res, tree, error = self.try_parse_statement()
|
||||
res, tree, error = self.try_parse_statement(text)
|
||||
if not res:
|
||||
self.has_error = True
|
||||
error_node = PythonErrorNode(self.text, error)
|
||||
error_node = PythonErrorNode(text, error)
|
||||
self.error_sink.append(error_node)
|
||||
return error_node
|
||||
|
||||
log.debug("Recognized python code.")
|
||||
return PythonNode(self.text, tree)
|
||||
return PythonNode(text, tree)
|
||||
|
||||
def try_parse_expression(self):
|
||||
def try_parse_expression(self, text):
|
||||
try:
|
||||
return True, ast.parse(self.text, f"<{self.source}>", 'eval'), None
|
||||
return True, ast.parse(text, f"<{self.source}>", 'eval'), None
|
||||
except Exception as error:
|
||||
return False, None, error
|
||||
|
||||
def try_parse_statement(self):
|
||||
def try_parse_statement(self, text):
|
||||
try:
|
||||
return True, ast.parse(self.text, f"<{self.source}>", 'exec'), None
|
||||
return True, ast.parse(text, f"<{self.source}>", 'exec'), None
|
||||
except Exception as error:
|
||||
return False, None, error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user