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

Commit

Permalink
fix(language): f-string sinlge quote in double quote bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
marsninja committed Jan 29, 2024
1 parent c4abf00 commit 7709d9a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
12 changes: 5 additions & 7 deletions examples/micro/simple_walk.jac
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ node Item {
has value: int = 1;
}

edge Checking {
}
edge Checking {}

walker Creator {
has count: int = 0;
Expand All @@ -14,7 +13,7 @@ walker Creator {
<here> ++> Item();
<self>.count+=1;
if <self>.count < 10 {
visit-->;
visit -->;
}
}
}
Expand All @@ -23,17 +22,17 @@ walker Walk {
has count: int = 0;

can skip_root with `<root> entry {
visit-->;
visit -->;
}

can step with Item entry {
<here>.value = <self>.count;
<self>.count+=1;
visit-->else {
visit --> else {
print(f'Final Value: {<here>.value - 1}');
"Done walking." |> print;
disengage;
}

f"Value: {<here>.value - 1}" |> print;
}
}
Expand All @@ -46,4 +45,3 @@ can test_run {
with entry {
test_run();
}

1 change: 1 addition & 0 deletions jaclang/compiler/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class Tokens(str, Enum):
FSTR_SQ_START = "FSTR_SQ_START"
FSTR_SQ_END = "FSTR_SQ_END"
FSTR_PIECE = "FSTR_PIECE"
FSTR_SQ_PIECE = "FSTR_SQ_PIECE"
FSTR_BESC = "FSTR_BESC"
COMMENT = "COMMENT"
WS = "WS"
Expand Down
4 changes: 3 additions & 1 deletion jaclang/compiler/jac.lark
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,10 @@ atom_literal: builtin_type
multistring: (fstring | STRING)+

fstring: FSTR_START fstr_parts FSTR_END
| FSTR_SQ_START fstr_parts FSTR_SQ_END
| FSTR_SQ_START fstr_sq_parts FSTR_SQ_END

fstr_parts: (FSTR_PIECE | FSTR_BESC | LBRACE expression RBRACE )*
fstr_sq_parts: (FSTR_SQ_PIECE | FSTR_BESC | LBRACE expression RBRACE )*

// Collection values
atom_collection: dict_compr
Expand Down Expand Up @@ -637,6 +638,7 @@ FSTR_END: "\""
FSTR_SQ_START.1: "f'"
FSTR_SQ_END: "'"
FSTR_PIECE.-1: /[^\{\}\"]+/
FSTR_SQ_PIECE.-1: /[^\{\}\']+/
FSTR_BESC.1: /{{|}}/

COMMENT: /#\*(.|\n|\r)*?\*#|#.*/
Expand Down
22 changes: 22 additions & 0 deletions jaclang/compiler/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2401,6 +2401,27 @@ def fstr_parts(
)
)

def fstr_sq_parts(
self, kid: list[ast.AstNode]
) -> ast.SubNodeList[ast.String | ast.ExprStmt]:
"""Grammar rule.
fstr_sq_parts: (FSTR_SQ_PIECE | FSTR_BESC | LBRACE expression RBRACE )*
"""
valid_parts: list[ast.String | ast.ExprStmt] = [
i
if isinstance(i, ast.String)
else ast.ExprStmt(expr=i, in_fstring=True, kid=[i])
for i in kid
if isinstance(i, ast.Expr)
]
return self.nu(
ast.SubNodeList[ast.String | ast.ExprStmt](
items=valid_parts,
kid=valid_parts,
)
)

def list_val(self, kid: list[ast.AstNode]) -> ast.ListVal:
"""Grammar rule.
Expand Down Expand Up @@ -3610,6 +3631,7 @@ def __default_token__(self, token: jl.Token) -> ast.Token:
Tok.STRING,
Tok.FSTR_BESC,
Tok.FSTR_PIECE,
Tok.FSTR_SQ_PIECE,
Tok.DOC_STRING,
]:
ret_type = ast.String
Expand Down
9 changes: 7 additions & 2 deletions jaclang/compiler/passes/tool/jac_formatter_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,17 +1267,22 @@ def exit_f_string(self, node: ast.FString) -> None:
parts: list["Token | ExprType"],
"""
self.emit(node, 'f"')
self.emit(
node, node.kid[0].value if isinstance(node.kid[0], ast.Token) else 'f"'
)
if node.parts:
for part in node.parts.items:
if isinstance(part, ast.String) and part.name in [
Tok.FSTR_PIECE,
Tok.FSTR_SQ_PIECE,
Tok.FSTR_BESC,
]:
self.emit(node, f"{part.gen.jac}")
else:
self.emit(node, "{" + part.gen.jac + "}")
self.emit(node, '"')
self.emit(
node, node.kid[-1].value if isinstance(node.kid[-1], ast.Token) else '"'
)

def exit_if_else_expr(self, node: ast.IfElseExpr) -> None:
"""Sub objects.
Expand Down
2 changes: 1 addition & 1 deletion jaclang/tests/fixtures/chandra_bugs.jac
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ can foo(param: int, **kwargs: dict) -> int {
with entry {
mydict = {'new_val':1, 'where':0 };
(mydict['new_val'], mydict['where']) = foo(1, bar=2);
print(mydict);
print(f"<link href='{mydict}' rel='stylesheet'>");
print(1!=2);
}

2 changes: 1 addition & 1 deletion jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_chandra_bugs(self) -> None:
stdout_value = captured_output.getvalue()
self.assertEqual(
stdout_value,
"{'new_val': 3, 'where': 'from_foo'}\nTrue\n",
"<link href='{'new_val': 3, 'where': 'from_foo'} rel='stylesheet'\nTrue\n",
)

def test_ignore(self) -> None:
Expand Down

0 comments on commit 7709d9a

Please sign in to comment.