Skip to content

Commit

Permalink
Crude (no, really) error-passing mechanism
Browse files Browse the repository at this point in the history
This serves as a PoC of the error-passing interface between the parser
and the frontend. It's as simple as it can get (there is no 'error
format', just a str).
  • Loading branch information
jmi2k committed Jun 13, 2020
1 parent cd3ef25 commit 1d9f060
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 6 additions & 3 deletions learnbot_dsl/learnbotCode/LearnBlock.py
Expand Up @@ -915,13 +915,16 @@ def checkProgramRunning(self):
def textCodeToPython(self, name_Client):
textCode = self.ui.textCode.toPlainText()
try:
code = parserLearntBotCodeFromCode(textCode, name_Client)
code, errors = parserLearntBotCodeFromCode(textCode, name_Client)
self.ui.pythonCode.clear()
if not code:
if errors:
errorList = '\n'.join(map(lambda error: '- ' + error, errors))
errorMsg = f"Your code is empty or is not correct. The following errors were found:\n\n{errorList}"

msgBox = QtWidgets.QMessageBox()
msgBox.setWindowTitle(self.tr("Warning"))
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setText(self.tr("Your code is empty or is not correct"))
msgBox.setText(self.tr(errorMsg))
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.setDefaultButton(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
Expand Down
17 changes: 13 additions & 4 deletions learnbot_dsl/learnbotCode/Parser.py
Expand Up @@ -317,6 +317,9 @@ def __parserFromString(text):
list_when = []
usedFunctions = []

def __typecheckParseTree():
pass

def __generatePy(lines):
global usedFunctions
usedFunctions = []
Expand Down Expand Up @@ -583,8 +586,11 @@ def parserLearntBotCodeOnlyUserFuntion(code):
def parserLearntBotCode(inputFile, outputFile, client_name):
global usedFunctions

errors = []

try:
tree = __parserFromFile(inputFile)
errors.append("Parse error (somewhere)")
except Exception as e:
traceback.print_exc()
raise e
Expand All @@ -600,20 +606,23 @@ def parserLearntBotCode(inputFile, outputFile, client_name):
with open(outputFile, 'w') as f:
f.write(header)
f.write(text)
return True
return header + text, errors
else:
return False
return False, errors

def parserLearntBotCodeFromCode(code, name_client):
global usedFunctions

errors = []

try:
tree = __parserFromString(code)
except Exception as e:
traceback.print_exc()
raise e
if not tree:
text = ""
errors.append("Parse error (somewhere)")
else:
text = elapsedTimeFunction
text += signalHandlerFunction
Expand All @@ -624,9 +633,9 @@ def parserLearntBotCodeFromCode(code, name_client):
header = cleanCode(_code=header)

if text is not "":
return header + text
return header + text, errors
else:
return False
return False, errors

def cleanCode(_code):
newcode = _code.replace(" :", ":").replace(" ", " ").replace("\n\n\n", "\n\n")
Expand Down

0 comments on commit 1d9f060

Please sign in to comment.