Added ExactConceptParser

This commit is contained in:
2019-11-09 17:29:50 +01:00
parent a636198222
commit 576ce77740
12 changed files with 603 additions and 169 deletions
+18 -13
View File
@@ -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