diff --git a/grammars/level12-Additions.lark b/grammars/level12-Additions.lark index 75ca4ab1c0d..a348bd7532f 100644 --- a/grammars/level12-Additions.lark +++ b/grammars/level12-Additions.lark @@ -13,7 +13,7 @@ sleep: _SLEEP (NUMBER | list_access | var_access | expression)? // all literal strings have to be quoted now, so arithmetic operators don't need to be excluded anymore text_with_spaces_without_single_quotes: /(?:[^\n،,' ]| (?!else|başka|अन्यथा|否则|ellers|anders|sinon|sino|وإلا))+/ -> text text_with_spaces_without_double_quotes: /(?:[^\n،," ]| (?!else|başka|अन्यथा|否则|ellers|anders|sinon|sino|وإلا))+/ -> text -text_in_quotes: _SINGLE_QUOTE text_with_spaces_without_single_quotes _SINGLE_QUOTE | _DOUBLE_QUOTE text_with_spaces_without_double_quotes _DOUBLE_QUOTE +text_in_quotes: _SINGLE_QUOTE text_with_spaces_without_single_quotes? _SINGLE_QUOTE | _DOUBLE_QUOTE text_with_spaces_without_double_quotes? _DOUBLE_QUOTE // FUNCTIONS ======================================== ============================= command:+= call | error_nested_define | return diff --git a/hedy.py b/hedy.py index 65597effa6c..567f54ea0bc 100644 --- a/hedy.py +++ b/hedy.py @@ -812,7 +812,8 @@ def text(self, tree): return self.to_typed_tree(tree, type_) def text_in_quotes(self, tree): - return self.to_typed_tree(tree.children[0], HedyType.string) + t = tree.children[0] if tree.children else tree + return self.to_typed_tree(t, HedyType.string) def var_access(self, tree): return self.to_typed_tree(tree, HedyType.string) @@ -2458,7 +2459,7 @@ def NEGATIVE_NUMBER(self, meta, args): def text_in_quotes(self, meta, args): # We need to re-add the quotes, so that the Python code becomes name = 'Jan' or "Jan's" - text = args[0] + text = args[0] if args else '' if "'" in text: return f'"{text}"' return f"'{text}'" diff --git a/tests/test_level/test_level_12.py b/tests/test_level/test_level_12.py index 1f445bde1d8..52eff652f7e 100644 --- a/tests/test_level/test_level_12.py +++ b/tests/test_level/test_level_12.py @@ -1070,6 +1070,13 @@ def test_assign_string(self, q): self.multi_level_tester(code=code, unused_allowed=True, expected=expected, max_level=17) + @parameterized.expand(HedyTester.quotes) + def test_assign_empty_string(self, q): + code = f"name = {q}{q}" + expected = "name = ''" + + self.multi_level_tester(code=code, unused_allowed=True, expected=expected) + def test_assign_text_with_inner_double_quote(self): code = """a is 'It says "Hedy"'""" expected = """a = 'It says "Hedy"'"""