Skip to content

Commit 8a016cd

Browse files
Merge pull request #1013 from Thirumalai-Shaktivel/string
2 parents 3aa8515 + dd744a4 commit 8a016cd

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

src/lpython/parser/tokenizer.re

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,18 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
275275
integer = int_dec | int_oct | int_bin | int_hex;
276276
real = (significand exp?) | (digit+ exp);
277277
imag_number = (real | digit+)[jJ];
278-
string1 = '"' ('\\"'|[^"\x00])* '"';
279-
string2 = "'" ("\\'"|[^'\x00])* "'";
280-
string3 = '"""' ( '\\"' | '"' [^"\x00] | '""' [^"\x00] | [^"\x00] )* '"""';
281-
string4 = "'''" ( "\\'" | "'" [^'\x00] | "''" [^'\x00] | [^'\x00] )* "'''";
278+
string1 = '"' ('\\'[^\x00] | [^"\x00\n\\])* '"';
279+
string2 = "'" ("\\"[^\x00] | [^'\x00\n\\])* "'";
280+
string3 = '"""' ( '\\'[^\x00]
281+
| ('"' | '"' '\\'+) [^"\x00\\]
282+
| ('""' | '""' '\\'+) [^"\x00\\]
283+
| [^"\x00\\] )*
284+
'"""';
285+
string4 = "'''" ( "\\"[^\x00]
286+
| ("'" | "'" "\\"+) [^'\x00\\]
287+
| ("''" | "''" "\\"+) [^'\x00\\]
288+
| [^'\x00\\] )*
289+
"'''";
282290
type_ignore = "#" whitespace? "type:" whitespace? "ignore" [^\n\x00]*;
283291
type_comment = "#" whitespace? "type:" whitespace? [^\n\x00]*;
284292
comment = "#" [^\n\x00]*;

tests/parser/string2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"\"\\"
2+
"\n\\"
3+
"\\"
4+
" \\"
5+
"\\ "
6+
"\
7+
"
8+
"\\" "|"
9+
10+
r"""Text\\"""
11+
"""\
12+
"""
13+
x("""\
14+
class a:
15+
def test():
16+
return r"\\LaTeX"
17+
""")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "ast_new-string2-44323ea",
3+
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
4+
"infile": "tests/parser/string2.py",
5+
"infile_hash": "fc6c3d133d5b93c5b5f052b9d86c2b1f270149aa48dba8ae8ced3e86",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "ast_new-string2-44323ea.stdout",
9+
"stdout_hash": "cf514f21d4a57de66c5fbfc107a6ab58818a09359ef9ba651896d71f",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(Module [(Expr (ConstantStr "\"\\" ())) (Expr (ConstantStr "
2+
\\" ())) (Expr (ConstantStr "\\" ())) (Expr (ConstantStr " \\" ())) (Expr (ConstantStr "\\ " ())) (Expr (ConstantStr "\
3+
" ())) (Expr (ConstantStr "\\|" ())) (Expr (ConstantStr "Text\\" ())) (Expr (ConstantStr "\
4+
" ())) (Expr (Call (Name x Load) [(ConstantStr "\
5+
class a:
6+
def test():
7+
return r"\\LaTeX"
8+
" ())] []))] [])

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ ast_new = true
506506
filename = "parser/string1.py"
507507
ast_new = true
508508

509+
[[test]]
510+
filename = "parser/string2.py"
511+
ast_new = true
512+
509513
[[test]]
510514
filename = "parser/global1.py"
511515
ast = true

0 commit comments

Comments
 (0)