Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #550 from Jaseci-Labs/triple_quoted_string_formatter
Browse files Browse the repository at this point in the history
Triple-Quoted String Formatting Enhance
  • Loading branch information
marsninja authored Aug 13, 2024
2 parents c7bc501 + 27122a4 commit a6151a8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
14 changes: 9 additions & 5 deletions jaclang/compiler/passes/tool/jac_formatter_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,11 @@ def exit_string(self, node: ast.String) -> None:
pos_end: int,
"""
# if string is in docstring format and spans multiple lines turn into the multiple single quoted strings
if "\n" in node.value and node.parent and isinstance(node.parent, ast.Expr):
if "\n" in node.value and (
node.parent
and isinstance(node.parent, ast.Expr)
and not isinstance(node.parent, ast.MultiString)
):
string_type = node.value[0:3]
pure_string = node.value[3:-3]
lines = pure_string.split("\n")
Expand All @@ -2476,10 +2480,10 @@ def exit_string(self, node: ast.String) -> None:
string_type = node.value[0:3]
pure_string = node.value[3:-3]
lines = pure_string.split("\n")
self.emit(node, string_type)
for line in lines[:-1]:
self.emit_ln(node, line)
self.emit_ln(node, f"{lines[-1]}{string_type}")
self.emit_ln(node, f"{string_type}{lines[0].lstrip()}")
for line in lines[1:-1]:
self.emit_ln(node, line.lstrip())
self.emit(node, f"{lines[-1].lstrip()}{string_type}")
else:
self.emit(node, node.value)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
with entry {
triple_quoted_string = """This is a triple quoted string.
It can span multiple lines.
It can contain any number of quotes or apostrophes.
""";
}
21 changes: 11 additions & 10 deletions jaclang/tests/fixtures/chandra_bugs2.jac
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import:py re;

glob a: int=5;
glob a: int = 5;

with entry {
arguments = {x:None for x in re.findall(r'\{([A-Za-z0-9_]+)\}', "Apple {apple} pineapple {pineapple}")};
a: int=5;
arguments = {x: None for x in re.findall(
r'\{([A-Za-z0-9_]+)\}',
"Apple {apple} pineapple {pineapple}"
)};
a: int = 5;
if False {
with open(f"Apple{apple}.txt") as f { # Fix syntax highlighting

print(f.read());
}
}
print(arguments);
print(
"""This is a long
line of code."""
);
print("""This is a long
line of code.""");
}

with entry{
a={"a":"apple", "b":"ball", "c":"cat"};
y={**a, "d":"dog", "e":"elephant"};
with entry {
a = {"a": "apple", "b": "ball", "c": "cat"};
y = {**a, "d": "dog", "e": "elephant"};
print(y);
}
# Use before def error would be nice
2 changes: 1 addition & 1 deletion jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_chandra_bugs2(self) -> None:
stdout_value,
"{'apple': None, 'pineapple': None}\n"
"This is a long\n"
" line of code.\n"
" line of code.\n"
"{'a': 'apple', 'b': 'ball', 'c': 'cat', 'd': 'dog', 'e': 'elephant'}\n",
)

Expand Down

0 comments on commit a6151a8

Please sign in to comment.