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

🖊️ Add support for empty strings in level 12 and up #4494 #5392

Merged
merged 3 commits into from
Apr 16, 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
2 changes: 1 addition & 1 deletion grammars/level12-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}'"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_level/test_level_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"'"""
Expand Down
Loading