Skip to content

Commit 4615870

Browse files
Merge pull request #847 from akshanshbhatt/pr_string_prefix
2 parents 4e1a222 + d78d389 commit 4615870

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

src/lpython/parser/semantics.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,14 @@ static inline ast_t *PREFIX_STRING(Allocator &al, Location &l, char *prefix, cha
683683
Vec<expr_t *> exprs;
684684
exprs.reserve(al, 4);
685685
ast_t *tmp = nullptr;
686-
// Assuming prefix has only one character.
687-
prefix[0] = tolower(prefix[0]);
688-
if (strcmp(prefix, "f") == 0) {
686+
if (strcmp(prefix, "U") == 0 ) {
687+
return make_ConstantStr_t(al, l, s, nullptr);
688+
}
689+
for (size_t i = 0; i < strlen(prefix); i++) {
690+
prefix[i] = tolower(prefix[i]);
691+
}
692+
if (strcmp(prefix, "f") == 0 || strcmp(prefix, "fr") == 0
693+
|| strcmp(prefix, "rf") == 0) {
689694
std::string str = std::string(s);
690695
std::string s1 = "\"";
691696
std::string id;
@@ -729,7 +734,8 @@ static inline ast_t *PREFIX_STRING(Allocator &al, Location &l, char *prefix, cha
729734
}
730735
}
731736
tmp = make_JoinedStr_t(al, l, exprs.p, exprs.size());
732-
} else if (strcmp(prefix, "b") == 0) {
737+
} else if (strcmp(prefix, "b") == 0 || strcmp(prefix, "br") == 0
738+
|| strcmp(prefix, "rb") == 0) {
733739
std::string str = std::string(s);
734740
size_t start_pos = 0;
735741
while((start_pos = str.find("\n", start_pos)) != std::string::npos) {

tests/parser/string1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,31 @@
1919

2020
u"1,2,3 # comment"
2121
u'Text'
22+
U"Text"
23+
24+
fr"""Text {id}"""
25+
Fr"Text {id}"
26+
fR"Text {id}"
27+
FR"Text {id}"
28+
rf"Text {id}"
29+
rF"""
30+
Text {id}
31+
"""
32+
Rf'Text {id}'
33+
RF"Text {id}"
34+
35+
br"Text"
36+
Br"""
37+
Text
38+
"""
39+
bR"Text"
40+
41+
BR"Text"
42+
rb"Text"
43+
rB"""
44+
Text
45+
"""
46+
Rb'Text'
47+
RB"Text"
48+
49+
rf'\N{AMPERSAND}'

tests/reference/ast_new-string1-96b90b3.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-string1-96b90b3",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/string1.py",
5-
"infile_hash": "e30b4b895b30b3ebb04e26d5b0c473fd8f048f60253eadd38088479a",
5+
"infile_hash": "e4090ab45efb09242e14f3dd494ca869a32e71de3617306d959d8721",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-string1-96b90b3.stdout",
9-
"stdout_hash": "dae1d81b8b616fcc63e5f47744c88ac18b8e6f8b351e603fa831176d",
9+
"stdout_hash": "1666ca8ebba593a8bf35e7906efabf5726626b7c82b130324bd78a43",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/ast_new-string1-96b90b3.stdout

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ sometext
33
anotherline
44
" ())) (Expr (ConstantBytes "b'Some text goes here.'" ())) (Expr (ConstantBytes "b'\nMultiple texts goes here.\n'" ())) (Expr (ConstantStr "
55
Text
6-
" ())) (Expr (ConstantStr "Text" ())) (Expr (ConstantStr "a\tb\nA\tB" ())) (Expr (ConstantStr "1,2,3 # comment" "u")) (Expr (ConstantStr "Text" "u"))] [])
6+
" ())) (Expr (ConstantStr "Text" ())) (Expr (ConstantStr "a\tb\nA\tB" ())) (Expr (ConstantStr "1,2,3 # comment" "u")) (Expr (ConstantStr "Text" "u")) (Expr (ConstantStr "Text" ())) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (JoinedStr [(ConstantStr "
7+
Text " ()) (FormattedValue (Name id Load) -1 ()) (ConstantStr "
8+
" ())])) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (JoinedStr [(ConstantStr "Text " ()) (FormattedValue (Name id Load) -1 ())])) (Expr (ConstantBytes "b'Text'" ())) (Expr (ConstantBytes "b'\nText\n'" ())) (Expr (ConstantBytes "b'Text'" ())) (Expr (ConstantBytes "b'Text'" ())) (Expr (ConstantBytes "b'Text'" ())) (Expr (ConstantBytes "b'\nText\n'" ())) (Expr (ConstantBytes "b'Text'" ())) (Expr (ConstantBytes "b'Text'" ())) (Expr (JoinedStr [(ConstantStr "\N" ()) (FormattedValue (Name AMPERSAND Load) -1 ())]))] [])

0 commit comments

Comments
 (0)