Skip to content

Commit fb4a215

Browse files
authored
Merge pull request #664 from certik/str_fix
Unescape strings in the new parser
2 parents 60f686d + e46931a commit fb4a215

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/lpython/parser/semantics.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,24 @@ char* concat_string(Allocator &al, ast_t *a, char *b) {
423423
return LFortran::s2c(al, std::string(s) + std::string(b));
424424
}
425425

426+
char* unescape(Allocator &al, LFortran::Str &s) {
427+
std::string x;
428+
for (size_t idx=0; idx < s.size(); idx++) {
429+
if (s.p[idx] == '\\' && s.p[idx+1] == 'n') {
430+
x += "\n";
431+
idx++;
432+
} else {
433+
x += s.p[idx];
434+
}
435+
}
436+
return LFortran::s2c(al, x);
437+
}
438+
426439
#define SYMBOL(x, l) make_Name_t(p.m_a, l, \
427440
x.c_str(p.m_a), expr_contextType::Load)
428441
// `x.int_n` is of type BigInt but we store the int64_t directly in AST
429442
#define INTEGER(x, l) make_ConstantInt_t(p.m_a, l, x, nullptr)
430-
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, x.c_str(p.m_a), nullptr)
443+
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, unescape(p.m_a, x), nullptr)
431444
#define STRING2(x, y, l) make_ConstantStr_t(p.m_a, l, concat_string(p.m_a, x, y.c_str(p.m_a)), nullptr)
432445
#define STRING3(id, x, l) PREFIX_STRING(p.m_a, l, name2char(id), x.c_str(p.m_a))
433446
#define FLOAT(x, l) make_ConstantFloat_t(p.m_a, l, x, nullptr)

tests/parser/string1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
print(f"{__file__} executed in {elapsed} seconds.")
44

5+
"\nsometext\nanotherline\n"
6+
57
# TODO: Support format specifiers
68

79
# print(f"{__file__} executed in {elapsed:0.2f} seconds.")

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": "e51ab7a60ad39354a13d987f0ed3bdb8f51b8e6d2a677a9d7b720749",
5+
"infile_hash": "83fc48a45a9a7373d7a8bc4340620acefc0eb1a0258f5df5c5f5bf2a",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-string1-96b90b3.stdout",
9-
"stdout_hash": "00a9b4899bd6ba7938b503da224840f51e2e7ba2e41121292809bdf2",
9+
"stdout_hash": "43d6ec3f9816a431342762957a9ee4e0bc5c6eb362d38e958e910842",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
(Module [(Expr (JoinedStr [(ConstantStr "Hello, " ()) (FormattedValue (Name first_name Load) -1 ()) (ConstantStr " " ()) (FormattedValue (Name last_name Load) -1 ()) (ConstantStr ". You are " ()) (FormattedValue (Name age Load) -1 ()) (ConstantStr " years old." ())])) (Expr (Call (Name print Load) [(JoinedStr [(FormattedValue (Name __file__ Load) -1 ()) (ConstantStr " executed in " ()) (FormattedValue (Name elapsed Load) -1 ()) (ConstantStr " seconds." ())])] []))] [])
1+
(Module [(Expr (JoinedStr [(ConstantStr "Hello, " ()) (FormattedValue (Name first_name Load) -1 ()) (ConstantStr " " ()) (FormattedValue (Name last_name Load) -1 ()) (ConstantStr ". You are " ()) (FormattedValue (Name age Load) -1 ()) (ConstantStr " years old." ())])) (Expr (Call (Name print Load) [(JoinedStr [(FormattedValue (Name __file__ Load) -1 ()) (ConstantStr " executed in " ()) (FormattedValue (Name elapsed Load) -1 ()) (ConstantStr " seconds." ())])] [])) (Expr (ConstantStr "
2+
sometext
3+
anotherline
4+
" ()))] [])

0 commit comments

Comments
 (0)