Skip to content

Commit

Permalink
Fix str parsing
Browse files Browse the repository at this point in the history
Resolves   #839.
  • Loading branch information
evhub committed May 29, 2024
1 parent 9959820 commit bad4ec5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
15 changes: 7 additions & 8 deletions coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1512,19 +1512,17 @@ def str_proc(self, inputstring, **kwargs):

# if we might be at the end of the string
elif hold["stop"] is not None:
if c == "\\":
self.str_hold_contents(hold, append=hold["stop"] + c)
hold["stop"] = None
elif c == hold["start"][0]:
if c == hold["start"][0]:
hold["stop"] += c
elif len(hold["stop"]) > len(hold["start"]):
raise self.make_err(CoconutSyntaxError, "invalid number of closing " + repr(hold["start"][0]) + "s", inputstring, i, reformat=False)
elif hold["stop"] == hold["start"]:
done = True
rerun = True
else:
self.str_hold_contents(hold, append=hold["stop"] + c)
self.str_hold_contents(hold, append=hold["stop"])
hold["stop"] = None
rerun = True

# if we might be at the start of an f string expr
elif hold.get("saw_brace", False):
Expand All @@ -1539,15 +1537,16 @@ def str_proc(self, inputstring, **kwargs):
hold["exprs"].append("")
rerun = True

elif is_f and c == "{":
hold["saw_brace"] = True
self.str_hold_contents(hold, append=c)
# backslashes should escape quotes, but nothing else
elif count_end(self.str_hold_contents(hold), "\\") % 2 == 1:
self.str_hold_contents(hold, append=c)
elif c == hold["start"]:
done = True
elif c == hold["start"][0]:
hold["stop"] = c
elif is_f and c == "{":
hold["saw_brace"] = True
self.str_hold_contents(hold, append=c)
else:
self.str_hold_contents(hold, append=c)

Expand Down
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.1.0"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 14
DEVELOP = 15
ALPHA = False # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down
4 changes: 4 additions & 0 deletions coconut/tests/src/cocotest/agnostic/primary_2.coco
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ def primary_test_2() -> bool:
assert CoconutWarning `issubclass` Warning
x = y = 2
assert f"{x + y = }" == "x + y = 4"
assert f"""
"{x}"
""" == '\n"2"\n'
assert f"\{1}" == "\\1"

with process_map.multiple_sequential_calls(): # type: ignore
assert map((+), range(3), range(4)$[:-1], strict=True) |> list == [0, 2, 4] == process_map((+), range(3), range(4)$[:-1], strict=True) |> list # type: ignore
Expand Down
2 changes: 2 additions & 0 deletions coconut/tests/src/cocotest/non_strict/non_strict_test.coco
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def non_strict_test() -> bool:
assert value == "123"
"{" f"{key}" ": " + value + "}" = "{abc: aaa}"
assert value == "aaa"
assert """ """\
== " "
return True

if __name__ == "__main__":
Expand Down

0 comments on commit bad4ec5

Please sign in to comment.