Skip to content

Commit

Permalink
Finish text error reporting system
Browse files Browse the repository at this point in the history
It can be considered feature-complete at this point.
  • Loading branch information
jmi2k committed Aug 6, 2020
1 parent 33d52d2 commit 4d956c5
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 82 deletions.
4 changes: 2 additions & 2 deletions learnbot_dsl/learnbotCode/LearnBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,10 @@ def textCodeToPython(self, name_Client):
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.setDefaultButton(QtWidgets.QMessageBox.Ok)
msgBox.exec_()

self.updateNotifications()
else:
self.ui.pythonCode.setPlainText(code)

self.updateNotifications()
return code
except ParseException as e:
traceback.print_exc()
Expand Down
11 changes: 6 additions & 5 deletions learnbot_dsl/learnbotCode/Notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Notification(QtWidgets.QWidget):
def __init__(self, src, start, end = None, *args, **kwargs):
super().__init__(*args, **kwargs)

self.HINT = self.tr('Hint')
self.src = src

self.vLayout = QtWidgets.QVBoxLayout()
Expand Down Expand Up @@ -43,6 +44,7 @@ def __init__(self, src, start, end = None, *args, **kwargs):
font = QtGui.QFont()
font.setFamily('Courier')
font.setFixedPitch(True)

self.snippet.setFont(font)
self.snippet.setReadOnly(True)
self.snippet.setOffset(start[1]-1)
Expand Down Expand Up @@ -109,16 +111,15 @@ def setHints(self, hints):

for hint in hints:
item = QtWidgets.QLabel(self)
item.setText(self.tr('<b>Hint:</b> ') + hint)
item.setText('<b>' + self.HINT + '</b>: ' + hint)
self.hints.addWidget(item)

class SyntaxError(Notification):
def __init__(self, *args, **kwargs):
class InvalidSyntax(Notification):
def __init__(self, rule, *args, **kwargs):
super().__init__(*args, **kwargs)

# TODO
hints = [
self.tr('did you forget something while typing the <code>%s</code>?') % 'TODO',
self.tr('did you forget something while typing the <code>%s</code>?') % rule.__name__,
]

self.setSeverity(Severity.ERROR)
Expand Down
94 changes: 56 additions & 38 deletions learnbot_dsl/learnbotCode/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class bcolors:
UNDERLINE = '\033[4m'

class Node:
last_stmt = None

def __init__(self, src, start, tokens):
global last_parsed

end = tokens[-1]
del tokens[-1]

Expand All @@ -95,6 +99,14 @@ def typecheck(self, ctx):
def used_vars(self):
return set()

@classmethod
def set_last_statement(cls, last):
cls.last_stmt = last

@classmethod
def last_statement(cls, last):
return cls.last_stmt

KEYWORDS = (
Keyword('def')
| Keyword('=')
Expand Down Expand Up @@ -233,19 +245,8 @@ def used_vars(self):
return {var for arg in self.args
for var in arg.used_vars}

"""
@staticmethod
def parse_failed(src, location, expr, error):
print('+--------------+')
print('| Parse error! |')
print('+--------------+')
print(f' line {lineno(location, src)}')
print(f' col {col(location, src)}')
raise ParseFatalException
"""

CALL = (
Suppress(Literal('function'))
Suppress(Literal('function')).addParseAction(lambda: Node.set_last_statement(Call))
- Suppress(DOT)
- IDENTIFIER
- OPAR
Expand Down Expand Up @@ -278,7 +279,7 @@ def used_vars(self):
for var in arg.used_vars}

SIMPLECALL = (Group(
IDENTIFIER
IDENTIFIER.addParseAction(lambda: Node.set_last_statement(SimpleCall))
+ OPAR
- Group(Optional(delimitedList(OPERATION)))
- CPAR
Expand Down Expand Up @@ -440,7 +441,7 @@ def used_vars(self):
VAR = (
INDENT
+ IDENTIFIER
+ (ASSIGN | PLUA | MINA | DIVA | MULA)
+ (ASSIGN | PLUA | MINA | DIVA | MULA).addParseAction(lambda: Node.set_last_statement(Var))
- OPERATION
+ ENDLOC
).setParseAction(Var)
Expand Down Expand Up @@ -553,31 +554,31 @@ def used_vars(self):

ELSEIF << (
INDENT
+ Suppress(Literal('elif'))
+ Suppress(Literal('elif')).addParseAction(lambda: Node.set_last_statement(ElseIf))
- OPERATION
- COLON
- LINES
+ ENDLOC
).setParseAction(ElseIf).setResultsName('ELSEIF')
).setParseAction(ElseIf)

ELSE << (
INDENT
+ Suppress(Literal('else'))
+ Suppress(Literal('else')).addParseAction(lambda: Node.set_last_statement(Else))
- COLON
- LINES
+ ENDLOC
).setParseAction(Else).setResultsName('ELSE')
).setParseAction(Else)

IF = (
INDENT
+ Suppress(Literal('if'))
+ Suppress(Literal('if')).addParseAction(lambda: Node.set_last_statement(If))
- OPERATION
- COLON
- LINES
- Group(ZeroOrMore(ELSEIF) + Optional(ELSE))
- END
+ ENDLOC
).setParseAction(If).setResultsName('IF')
).setParseAction(If)

"""-----------------LOOP----------------------------"""
class While(Node):
Expand Down Expand Up @@ -616,7 +617,7 @@ def used_vars(self):

BLOQUEWHILE = (
INDENT
+ Suppress(Literal('while'))
+ Suppress(Literal('while')).addParseAction(lambda: Node.set_last_statement(While))
- OPERATION
- COLON
- LINES
Expand Down Expand Up @@ -690,7 +691,7 @@ def typecheck(self, ctx):

BLOQUEWHENCOND = (
INDENT
+ Suppress(Literal('when'))
+ Suppress(Literal('when')).addParseAction(lambda: Node.set_last_statement(When))
- IDENTIFIER
- Optional(
Suppress(ASSIGN)
Expand Down Expand Up @@ -720,14 +721,14 @@ def to_python(self, gen, *_):
return f'state_{self.name} = False'

ACTIVATE = (
Suppress(Literal('activate'))
+ IDENTIFIER
Suppress(Literal('activate')).addParseAction(lambda: Node.set_last_statement(Activate))
- IDENTIFIER
+ ENDLOC
).setParseAction(Activate)

DEACTIVATE = (
Suppress(Literal('activate'))
+ IDENTIFIER
Suppress(Literal('deactivate')).addParseAction(lambda: Node.set_last_statement(Deactivate))
- IDENTIFIER
+ ENDLOC
).setParseAction(Deactivate)

Expand Down Expand Up @@ -779,7 +780,7 @@ def used_vars(self):
for var in node.used_vars}

DEF = (
Suppress(Literal('def'))
Suppress(Literal('def')).addParseAction(lambda: Node.set_last_statement(Def))
- IDENTIFIER
- OPAR
- CPAR
Expand All @@ -801,7 +802,7 @@ def to_python(self, gen, *_):
return ''

IMPORT = (
Suppress(Literal('import'))
Suppress(Literal('import')).addParseAction(lambda: Node.set_last_statement(Import))
- STRING
+ ENDLOC
).setParseAction(Import)
Expand Down Expand Up @@ -829,7 +830,7 @@ def used_vars(self):
for var in node.used_vars}

MAIN = (
Suppress(Literal('main'))
Suppress(Literal('main')).addParseAction(lambda: Node.set_last_statement(Main))
- COLON
- LINES
- END
Expand Down Expand Up @@ -1082,15 +1083,26 @@ def parserLearntBotCode(inputFile, outputFile, client_name):
f.write(text)

return header + text, notifications
except (ParseException, ParseSyntaxException) as pe:
except ParseSyntaxException as pe:
traceback.print_exc()

print("=================================")
print("ParseSyntaxException 1")
print("=================================")
print(pe.__dict__)

notifications.append(InvalidSyntax(
src = code,
start = (pe.lineno, pe.col, pe.loc)
))
return None, notifications
except ParseException as pe:
traceback.print_exc()

finally:
notifications.append(InvalidSyntax(
src = code,
start = (pe.lineno, pe.col, pe.loc)
))
return None, notifications

def parserLearntBotCodeFromCode(code, name_client):
Expand Down Expand Up @@ -1120,27 +1132,34 @@ def parserLearntBotCodeFromCode(code, name_client):
))

return header + text, notifications
except (ParseException, ParseSyntaxException) as pe:
except ParseSyntaxException as pe:
traceback.print_exc()

notifications.append(InvalidSyntax(
src = code,
start = (pe.lineno, pe.col, pe.loc),
rule = Node.last_statement()
))
return None, notifications
except ParseException as pe:
traceback.print_exc()
notifications.append(InvalidSyntax(
src = code,
start = (pe.lineno, pe.col, pe.loc)
start = (pe.lineno, pe.col, pe.loc),
rule = Node.last_statement()
))

finally:
return None, notifications

if __name__ == "__main__":
textprueba = """
main:
function x()
x = 2
end
"""
try:
"""
print("Original source code")
print("====================")
print()
Expand Down Expand Up @@ -1178,7 +1197,6 @@ def parserLearntBotCodeFromCode(code, name_client):
print()
print(mismatches)
print()
"""
parserLearntBotCodeFromCode(textprueba, "robots")
except Exception as pe:
print(pe.line)
Expand Down
Binary file modified learnbot_dsl/learnbotCode/languages/t_en.qm
Binary file not shown.
47 changes: 23 additions & 24 deletions learnbot_dsl/learnbotCode/languages/t_en.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en">
<!DOCTYPE TS><TS version="2.0" language="en" sourcelanguage="">
<context>
<name>Dialog</name>
<message numerus="yes">
Expand Down Expand Up @@ -444,6 +443,19 @@
</translation>
</message>
</context>
<context>
<name>InvalidSyntax</name>
<message>
<location filename="../Notification.py" line="122"/>
<source>did you forget something while typing the &lt;code&gt;%s&lt;/code&gt;?</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Notification.py" line="126"/>
<source>Invalid syntax</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>LearnBlock</name>
<message>
Expand Down Expand Up @@ -1252,18 +1264,18 @@
<context>
<name>Notification</name>
<message>
<location filename="../Notification.py" line="95"/>
<source>at %s:%s—%s:%s</source>
<location filename="../Notification.py" line="97"/>
<source>at %s:%s&#xe2;&#x80;&#x94;%s:%s</source>
<translation></translation>
</message>
<message>
<location filename="../Notification.py" line="100"/>
<location filename="../Notification.py" line="102"/>
<source>at %s:%s</source>
<translation></translation>
</message>
<message>
<location filename="../Notification.py" line="112"/>
<source>&lt;b&gt;Hint:&lt;/b&gt; </source>
<location filename="../Notification.py" line="16"/>
<source>Hint</source>
<translation></translation>
</message>
</context>
Expand All @@ -1280,38 +1292,25 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SyntaxError</name>
<message>
<location filename="../Notification.py" line="121"/>
<source>did you forget something while typing the &lt;code&gt;%s&lt;/code&gt;?</source>
<translation></translation>
</message>
<message>
<location filename="../Notification.py" line="125"/>
<source>Invalid syntax</source>
<translation></translation>
</message>
</context>
<context>
<name>TypeMismatch</name>
<message>
<location filename="../Notification.py" line="139"/>
<location filename="../Notification.py" line="140"/>
<source>Type mismatch: expected %s, got %s</source>
<translation></translation>
</message>
<message>
<location filename="../Notification.py" line="133"/>
<location filename="../Notification.py" line="134"/>
<source>check the marked expression: does it really return a value of type &lt;code&gt;%s&lt;/code&gt;?</source>
<translation></translation>
</message>
<message>
<location filename="../Notification.py" line="134"/>
<location filename="../Notification.py" line="135"/>
<source>did you make a typo while writing the expression? Check the operators!</source>
<translation></translation>
</message>
<message>
<location filename="../Notification.py" line="135"/>
<location filename="../Notification.py" line="136"/>
<source>be careful with operator precedence!</source>
<translation></translation>
</message>
Expand Down
Binary file modified learnbot_dsl/learnbotCode/languages/t_es.qm
Binary file not shown.
Loading

0 comments on commit 4d956c5

Please sign in to comment.