From 8d7f424e353629d7b6822793fd2d1b80e718f349 Mon Sep 17 00:00:00 2001 From: Felienne Date: Fri, 20 Mar 2020 14:00:01 +0100 Subject: [PATCH] now allowing empty lines in the grammar, and created scaffold for better error messages --- grammars/level1.txt | 2 +- grammars/level2.txt | 2 +- grammars/level3.txt | 2 +- grammars/level4.txt | 2 +- hedy.py | 13 ++++++++++--- static/texts.json | 9 ++++++--- tests.py | 5 ++++- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/grammars/level1.txt b/grammars/level1.txt index 7d85933195a..4a93ecc4b29 100644 --- a/grammars/level1.txt +++ b/grammars/level1.txt @@ -1,5 +1,5 @@ start: program -program: command ("\n" command)* +program: command ("\n"+ command)* "\n"* command: "print " text -> print | "ask " text -> ask | "echo " text -> echo diff --git a/grammars/level2.txt b/grammars/level2.txt index c557355eb18..6eaa58581f8 100644 --- a/grammars/level2.txt +++ b/grammars/level2.txt @@ -1,5 +1,5 @@ start: program -program: command ("\n" command)* +program: command ("\n"+ command)* "\n"* command: "print " ( list_access | (text punctuation)| text) (" " ( list_access | (text punctuation)| text) )* -> print | text " is ask " textwithspaces punctuation* -> ask | text " is " text ((", "|",") text)+ -> assign_list diff --git a/grammars/level3.txt b/grammars/level3.txt index 079066a366b..4e0fbff7f98 100644 --- a/grammars/level3.txt +++ b/grammars/level3.txt @@ -1,5 +1,5 @@ start: program -program: command ("\n" command)* +program: command ("\n"+ command)* "\n"* command: "print " (quoted_text | list_access | var) (" " (quoted_text | list_access | var))* -> print | text " is ask " textwithspaces punctuation* -> ask | text " is " text ((", "|",") text)+ -> assign_list diff --git a/grammars/level4.txt b/grammars/level4.txt index 48b5906ee06..0165d6d398c 100644 --- a/grammars/level4.txt +++ b/grammars/level4.txt @@ -1,5 +1,5 @@ start: program -program: command ("\n" command)* +program: command ("\n"+ command)* "\n"* command: "print " (quoted_text | list_access | var) (" " (quoted_text | list_access | var))* -> print | "if " equality_check " " command " else " command -> ifelse | "if " equality_check " " command -> ifs //if cannot be used in Python, hence ifs diff --git a/hedy.py b/hedy.py index 5b1067cd7c0..d0a4c237461 100644 --- a/hedy.py +++ b/hedy.py @@ -8,6 +8,7 @@ def __init__(self, message, **arguments): self.arguments = arguments + class AllCommands(Transformer): #creates a list of all commands in a tree for further processing # it removes command and program nodes @@ -325,9 +326,14 @@ def transpile(input_string, level): punctuation_symbols = ['!', '?', '.'] level = int(level) parser = create_parser(level) - program_root = parser.parse(input_string).children[0] # getting rid of the root could also be done in the transformer would be nicer - lookup_table = all_assignments(program_root) - flattened_tree = FlattenText().transform(program_root) + try: + program_root = parser.parse(input_string).children[0] # getting rid of the root could also be done in the transformer would be nicer + lookup_table = all_assignments(program_root) + flattened_tree = FlattenText().transform(program_root) + except Exception as e: + # TODO: here we could translate Lark error messages into more sensible texts! + raise HedyException('Parse', level=level, parse_error=e.args[0]) + is_valid = IsValid().transform(program_root) if is_valid[0]: @@ -360,6 +366,7 @@ def transpile(input_string, level): else: raise Exception('Levels 5 to 7 are not implemented yet') + def execute(input_string): python = transpile(input_string) exec(python) diff --git a/static/texts.json b/static/texts.json index 4603c2a714d..4b89dbf6975 100644 --- a/static/texts.json +++ b/static/texts.json @@ -18,7 +18,8 @@ "Execute_error": "Er ging iets fout bij het uitvoeren van het programma." }, "HedyErrorMessages": { - "Invalid": "{command} is geen command in Hedy level {level}." + "Invalid": "{command} is geen command in Hedy level {level}.", + "Parse": "De code die jij intypte is geen geldige Hedy code. De foutmelding is: {parse_error}" } }, "en": { @@ -40,7 +41,8 @@ "Execute_error": "Something went wrong while running the program." }, "HedyErrorMessages": { - "Invalid": "{command} is not a Hedy level {level} command." + "Invalid": "{command} is not a Hedy level {level} command.", + "Parse": "The code you entered is not valid Hedy code" } }, "es": { @@ -62,7 +64,8 @@ "Execute_error": "Algo estuvo mal mientras se ejecuto el programa." }, "HedyErrorMessages": { - "Invalid": "{command} no es un comando de Hedy en nivel {level}." + "Invalid": "{command} no es un comando de Hedy en nivel {level}.", + "Parse": "El servidor no puede traducir este programa de Hedy a Python." } } } diff --git a/tests.py b/tests.py index 58b13846c17..d09fd968204 100644 --- a/tests.py +++ b/tests.py @@ -27,7 +27,6 @@ class TestsLevel1(unittest.TestCase): def test_transpile_other(self): with self.assertRaises(Exception) as context: result = hedy.transpile("abc felienne 123", 1) - self.assertEqual(str(context.exception), 'Invalid') def test_transpile_print(self): @@ -35,6 +34,10 @@ def test_transpile_print(self): self.assertEqual(result, "print('Hallo welkom bij Hedy!')") self.assertEqual(run_code(result), 'Hallo welkom bij Hedy!') + def test_transpile_empty(self): + with self.assertRaises(hedy.HedyException) as context: + result = hedy.transpile("", 1) + def test_transpile_ask(self): result = hedy.transpile("ask wat is je lievelingskleur?", 1) self.assertEqual(result, "answer = input('wat is je lievelingskleur?')")