Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🖊️ 💻 Display error message when brackets are forgotten around lists #5177

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ def translate_error(code, arguments, keyword_lang):
'echo',
'is',
'if',
'repeat']
'repeat',
'[]']

# Todo TB -> We have to find a more delicate way to fix this: returns some gettext() errors
error_template = gettext('' + str(code))
Expand Down
1 change: 1 addition & 0 deletions content/error-messages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ gettext('input')
gettext('Invalid At Command')
gettext('Misspelled At Command')
gettext('Non Decimal Variable')
gettext('Missing Square Brackets')
# Exceptions groups used in the Live Statistics Module. If you add a new Exception Group here,
# you should use the same name when you assign this exception group to a exception in that module
# in the exception_types dictionary
Expand Down
6 changes: 6 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def __init__(self, text, line_number):
super().__init__('Unquoted Assignment', text=text, line_number=line_number)


class MissingBracketsException(HedyException):
def __init__(self, level, line_number):
super().__init__('Missing Square Brackets',
line_number=line_number)


class LonelyEchoException(HedyException):
def __init__(self):
super().__init__('Lonely Echo')
Expand Down
4 changes: 2 additions & 2 deletions grammars/keywords-ar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ _OR: _SPACE ( "ـ"* "أ" "ـ"* "و" "ـ"* | "or") _SPACE
_WHILE: ( "ـ"* "ب" "ـ"* "ي" "ـ"* "ن" "ـ"* "م" "ـ"* "ا" "ـ"* | "while") _SPACE
_LENGTH: "ـ"* "ط" "ـ"* "و" "ـ"* "ل" "ـ"* | "length"
_COLOR : ( "ـ"* "ل" "ـ"* "و" "ـ"* "ن" "ـ"* | "color") _SPACE?
_PRESSED: ( "ـ"* "p" "ـ"* "r" "ـ"* "e" "ـ"* "s" "ـ"* "s" "ـ"* "e" "ـ"* "d" "ـ"* | "pressed") _SPACE?
_PRESSED: ( "ـ"* "م" "ـ"* "ض" "ـ"* "غ" "ـ"* "و" "ـ"* "ط" "ـ"* | "pressed") _SPACE?
_BUTTON: ( "ـ"* "b" "ـ"* "u" "ـ"* "t" "ـ"* "t" "ـ"* "o" "ـ"* "n" "ـ"* | "button") _SPACE?
clear: ( "ـ"* "c" "ـ"* "l" "ـ"* "e" "ـ"* "a" "ـ"* "r" "ـ"* | "clear") _SPACE?
clear: ( "ـ"* "م" "ـ"* "س" "ـ"* "ح" "ـ"* | "clear") _SPACE?
6 changes: 3 additions & 3 deletions grammars/keywords-fr.lark
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
_DEFINE: ("define" | "define") _SPACE?
_CALL: ("call" | "call") _SPACE?
_DEFINE: ("définis" | "define") _SPACE?
_CALL: ("appelle" | "call") _SPACE?
_WITH: ("with" | "with") _SPACE?
_DEF: ("def" | "def") _SPACE?
_RETURN: ("return" | "return") _SPACE?
Expand Down Expand Up @@ -49,5 +49,5 @@ _WHILE: ("tant que" | "while") _SPACE
_LENGTH: "longueur" | "length"
_COLOR : ("couleur" | "color") _SPACE?
_PRESSED: ("pressé" | "pressed") _SPACE?
_BUTTON: ("button" | "button") _SPACE?
_BUTTON: ("bouton" | "button") _SPACE?
clear: ("effacer" | "clear") _SPACE?
53 changes: 53 additions & 0 deletions grammars/keywords-ia.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
_DEFINE: ("define" | "define") _SPACE?
_CALL: ("call" | "call") _SPACE?
_WITH: ("with" | "with") _SPACE?
_DEF: ("def" | "def") _SPACE?
_RETURN: ("return" | "return") _SPACE?
_PRINT: ("print" | "print") _SPACE?
_PLAY: ("play" | "play") _SPACE
_ASK: ("ask" | "ask") _SPACE?
_ECHO: ("echo" | "echo") _SPACE?
_FORWARD: ("forward" | "forward") _SPACE?
_TURN: ("turn" | "turn") _SPACE?
left: ("left" | "left") _SPACE?
right: ("right" | "right") _SPACE?
black: ("black" | "black") _SPACE?
blue: ("blue" | "blue") _SPACE?
brown: ("brown" | "brown") _SPACE?
gray: ("gray" | "gray") _SPACE?
green: ("green" | "green") _SPACE?
orange: ("orange" | "orange") _SPACE?
pink: ("pink" | "pink") _SPACE?
purple: ("purple" | "purple") _SPACE?
red: ("red" | "red") _SPACE?
white: ("white" | "white") _SPACE?
yellow: ("yellow" | "yellow") _SPACE?
_IS: _SPACE ("is" | "is") _SPACE
_STANDALONE_IS: ("is" | "is")
_SLEEP: ("sleep" | "sleep") _SPACE?
_ADD_LIST: ("add" | "add") _SPACE
_TO_LIST: _SPACE ("to" | "to") _SPACE
_REMOVE: ("remove" | "remove") _SPACE
_FROM: _SPACE ("from" | "from") _SPACE
_AT: _SPACE ("at" | "at") _SPACE
random: ("random" | "random") _SPACE?
_IN: _SPACE ("in" | "in") _SPACE
_NOT_IN: _SPACE ("not in" | "not in") _SPACE
_IF: ("if" | "if") _SPACE
_ELSE: "else" | "else"
_AND: _SPACE ("and" | "and") _SPACE
_REPEAT: ("repeat" | "repeat") _SPACE
_TIMES: _SPACE ("times" | "times")
_FOR: ("for" | "for") _SPACE
_RANGE: ("range" | "range") _SPACE?
_TO: _SPACE ("to" | "to") _SPACE
_STEP: "step" | "step"
_ELIF: _SPACE? ("elif" | "elif") _SPACE
_INPUT: ("input" | "input")
_OR: _SPACE ("or" | "or") _SPACE
_WHILE: ("while" | "while") _SPACE
_LENGTH: "length" | "length"
_COLOR : ("color" | "color") _SPACE?
_PRESSED: ("pressed" | "pressed") _SPACE?
_BUTTON: ("button" | "button") _SPACE?
clear: ("clear" | "clear") _SPACE?
4 changes: 2 additions & 2 deletions grammars/keywords-pt_BR.lark
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _WITH: ("com" | "with") _SPACE?
_DEF: ("def" | "def") _SPACE?
_RETURN: ("retorne" | "return") _SPACE?
_PRINT: ("imprima" | "print") _SPACE?
_PLAY: ("play" | "play") _SPACE
_PLAY: ("toque" | "play") _SPACE
_ASK: ("pergunte" | "ask") _SPACE?
_ECHO: ("eco" | "echo") _SPACE?
_FORWARD: ("adiante" | "forward") _SPACE?
Expand Down Expand Up @@ -42,7 +42,7 @@ _FOR: ("para" | "for") _SPACE
_RANGE: ("intervalo" | "range") _SPACE?
_TO: _SPACE ("para" | "to") _SPACE
_STEP: "passo" | "step"
_ELIF: _SPACE? ("senãose" | "elif") _SPACE
_ELIF: _SPACE? ("senão se" | "elif") _SPACE
_INPUT: ("entrada" | "input")
_OR: _SPACE ("ou" | "or") _SPACE
_WHILE: ("enquanto" | "while") _SPACE
Expand Down
14 changes: 7 additions & 7 deletions grammars/keywords-tn.lark
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ _CALL: ("call" | "call") _SPACE?
_WITH: ("with" | "with") _SPACE?
_DEF: ("def" | "def") _SPACE?
_RETURN: ("return" | "return") _SPACE?
_PRINT: ("gatisa" | "print") _SPACE?
_PRINT: ("print" | "print") _SPACE?
_PLAY: ("play" | "play") _SPACE
_ASK: ("botsa" | "ask") _SPACE?
_ASK: ("ask" | "ask") _SPACE?
_ECHO: ("echo" | "echo") _SPACE?
_FORWARD: ("forward" | "forward") _SPACE?
_TURN: ("turn" | "turn") _SPACE?
Expand All @@ -22,19 +22,19 @@ purple: ("purple" | "purple") _SPACE?
red: ("red" | "red") _SPACE?
white: ("white" | "white") _SPACE?
yellow: ("yellow" | "yellow") _SPACE?
_IS: _SPACE ("ke" | "is") _SPACE
_STANDALONE_IS: ("ke" | "is")
_IS: _SPACE ("is" | "is") _SPACE
_STANDALONE_IS: ("is" | "is")
_SLEEP: ("sleep" | "sleep") _SPACE?
_ADD_LIST: ("tsenya" | "add") _SPACE
_TO_LIST: _SPACE ("to" | "to") _SPACE
_REMOVE: ("ntsha" | "remove") _SPACE
_REMOVE: ("remove" | "remove") _SPACE
_FROM: _SPACE ("from" | "from") _SPACE
_AT: _SPACE ("at" | "at") _SPACE
random: ("random" | "random") _SPACE?
_IN: _SPACE ("in" | "in") _SPACE
_NOT_IN: _SPACE ("not in" | "not in") _SPACE
_IF: ("fa" | "if") _SPACE
_ELSE: "faese" | "else"
_IF: ("if" | "if") _SPACE
_ELSE: "else" | "else"
_AND: _SPACE ("and" | "and") _SPACE
_REPEAT: ("repeat" | "repeat") _SPACE
_TIMES: _SPACE ("times" | "times")
Expand Down
3 changes: 2 additions & 1 deletion grammars/level16-Additions.lark
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// adds list access with numbers and brackets
// TODO for tomorrow: this stills needs tests in level 16 for > and !=
command:+= change_list_item
command:+= change_list_item | error_assign_list_missing_brackets

?comparison_arg: var_access | text_in_quotes | NUMBER | list_access

?atom: NUMBER | _MINUS NUMBER | var_access | list_access | text_in_quotes
list_access: var_access _LEFT_SQUARE_BRACKET (INT | random | var_access) _RIGHT_SQUARE_BRACKET
change_list_item: var_access _LEFT_SQUARE_BRACKET (INT | var_access) _RIGHT_SQUARE_BRACKET _EQUALS (var_access | textwithoutspaces)
assign_list: var (_IS | _EQUALS) _LEFT_SQUARE_BRACKET ((quoted_text | NUMBER) (_COMMA (quoted_text | NUMBER))*)? _RIGHT_SQUARE_BRACKET
error_assign_list_missing_brackets: var (_IS | _EQUALS) (_LEFT_SQUARE_BRACKET)? ((quoted_text | NUMBER) _COMMA (quoted_text | NUMBER) (_COMMA (quoted_text | NUMBER))*)? (_RIGHT_SQUARE_BRACKET)?

error_list_access_at: var_access _AT (INT | random)

Expand Down
3 changes: 3 additions & 0 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,9 @@ def error_for_missing_to(self, meta, args):
def error_for_missing_command(self, meta, args):
raise exceptions.IncompleteCommandException(incomplete_command='for', level=self.level, line_number=meta.line)

def error_assign_list_missing_brackets(self, meta, args):
raise exceptions.MissingBracketsException(level=self.level, line_number=meta.line)

def error_nested_define(self, meta, args):
raise exceptions.NestedFunctionException()

Expand Down
22 changes: 22 additions & 0 deletions tests/test_level/test_level_16.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,28 @@ def test_change_list_item_number(self):

self.single_level_tester(code=code, expected=expected)

def test_assign_list_missing_brackets_gives_error(self):
code = textwrap.dedent("""\
animals = 'chicken', 'horse', 'cow'
print animals[random]""")

self.multi_level_tester(
code=code,
exception=hedy.exceptions.MissingBracketsException,
max_level=17
)

def test_assign_list_missing_bracket_gives_error(self):
code = textwrap.dedent("""\
animals = ['chicken', 'horse', 'cow'
print animals[random]""")

self.multi_level_tester(
code=code,
exception=hedy.exceptions.MissingBracketsException,
max_level=17
)

def test_equality_with_number_and_list_gives_error(self):
code = textwrap.dedent("""\
color is [5, 6, 7]
Expand Down
11 changes: 7 additions & 4 deletions translations/ar/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-01-18 10:49+0100\n"
"POT-Creation-Date: 2024-02-26 13:05+0100\n"
"PO-Revision-Date: 2024-02-19 01:12+0000\n"
"Last-Translator: Snoring Parrot <[email protected]>\n"
"Language-Team: ar <[email protected]>\n"
"Language: ar\n"
"Language-Team: ar <[email protected]>\n"
"Plural-Forms: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=0 && n%100<=2 ? 4 : 5);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=0 && n%100<=2 ? 4 : 5);\n"
"X-Generator: Weblate 5.4\n"
"Generated-By: Babel 2.14.0\n"

#, fuzzy
Expand Down Expand Up @@ -73,6 +72,9 @@ msgstr "It looks like you forgot to use a command with the text you put in line
msgid "Missing Additional Command"
msgstr "It looks like you forgot to complete writing {command} on line {line_number}."

msgid "Missing Square Brackets"
msgstr ""

msgid "Missing Command"
msgstr "يبدو أنك نسيت استخدام أمر برمجي في السطر رقم {line_number}."

Expand Down Expand Up @@ -2225,3 +2227,4 @@ msgstr "Your program"

#~ msgid "write_first_program"
#~ msgstr "اكتب أول برامجك!"

11 changes: 7 additions & 4 deletions translations/ca/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-01-29 20:23-0800\n"
"POT-Creation-Date: 2024-02-26 13:05+0100\n"
"PO-Revision-Date: 2024-02-19 01:12+0000\n"
"Last-Translator: Snoring Parrot <[email protected]>\n"
"Language-Team: ca <[email protected]>\n"
"Language: ca\n"
"Language-Team: ca <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.4\n"
"Generated-By: Babel 2.14.0\n"

msgid "Access Before Assign"
Expand Down Expand Up @@ -69,6 +68,9 @@ msgstr "Sembla que t'has oblidat d'utilitzar una comanda al text de la línia {l
msgid "Missing Additional Command"
msgstr "It looks like you forgot to complete writing {command} on line {line_number}."

msgid "Missing Square Brackets"
msgstr ""

msgid "Missing Command"
msgstr "Sembla que t'has oblidat d'usar una comanda a la línia {line_number}."

Expand Down Expand Up @@ -2238,3 +2240,4 @@ msgstr "El teu programa"

#~ msgid "write_first_program"
#~ msgstr "Escriu el teu primer programa!"

11 changes: 7 additions & 4 deletions translations/de/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-01-18 10:49+0100\n"
"POT-Creation-Date: 2024-02-26 13:05+0100\n"
"PO-Revision-Date: 2024-02-19 01:12+0000\n"
"Last-Translator: Snoring Parrot <[email protected]>\n"
"Language-Team: de <[email protected]>\n"
"Language: de\n"
"Language-Team: de <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.4\n"
"Generated-By: Babel 2.14.0\n"

msgid "Access Before Assign"
Expand Down Expand Up @@ -69,6 +68,9 @@ msgstr "Es sieht so aus als ob du vergessen hast einen Befehl für deinen Text i
msgid "Missing Additional Command"
msgstr "It looks like you forgot to complete writing {command} on line {line_number}."

msgid "Missing Square Brackets"
msgstr ""

msgid "Missing Command"
msgstr "Es sieht so aus als hättest du einen Befehl in Zeile {line_number} vergessen."

Expand Down Expand Up @@ -2096,3 +2098,4 @@ msgstr "Dein Programm"

#~ msgid "write_first_program"
#~ msgstr "Schreibe dein erstes Programm!"

11 changes: 7 additions & 4 deletions translations/el/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-01-18 10:49+0100\n"
"POT-Creation-Date: 2024-02-26 13:05+0100\n"
"PO-Revision-Date: 2024-02-19 01:12+0000\n"
"Last-Translator: Snoring Parrot <[email protected]>\n"
"Language-Team: el <[email protected]>\n"
"Language: el\n"
"Language-Team: el <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.4\n"
"Generated-By: Babel 2.14.0\n"

#, fuzzy
Expand Down Expand Up @@ -73,6 +72,9 @@ msgstr "It looks like you forgot to use a command with the text you put in line
msgid "Missing Additional Command"
msgstr "It looks like you forgot to complete writing {command} on line {line_number}."

msgid "Missing Square Brackets"
msgstr ""

msgid "Missing Command"
msgstr "Φαίνεται ότι ξέχασες να χρησιμοποιήσεις μια εντολή στη γραμμή {line_number}."

Expand Down Expand Up @@ -2321,3 +2323,4 @@ msgstr "Your program"

#~ msgid "write_first_program"
#~ msgstr "Γράψε το πρώτο σου πρόγραμμα!"

11 changes: 7 additions & 4 deletions translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-01-29 20:23-0800\n"
"POT-Creation-Date: 2024-02-26 13:05+0100\n"
"PO-Revision-Date: 2024-02-19 01:12+0000\n"
"Last-Translator: Snoring Parrot <[email protected]>\n"
"Language-Team: en <[email protected]>\n"
"Language: en\n"
"Language-Team: en <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.4\n"
"Generated-By: Babel 2.14.0\n"

msgid "Access Before Assign"
Expand Down Expand Up @@ -67,6 +66,9 @@ msgstr "It looks like you forgot to use a command with the text you used in line
msgid "Missing Additional Command"
msgstr "It looks like you forgot to complete writing {command} on line {line_number}. Try adding {missing_command} to your code."

msgid "Missing Square Brackets"
msgstr "It looks like you forgot to use square brackets [] around the list you were creating on line {line_number}."

msgid "Missing Command"
msgstr "It looks like you forgot to use a command on line {line_number}."

Expand Down Expand Up @@ -2041,3 +2043,4 @@ msgstr "Your program"

#~ msgid "write_first_program"
#~ msgstr "Write your first program!"

Loading
Loading