We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent eceab13 commit 660aa2aCopy full SHA for 660aa2a
src/lpython/parser/parser.yy
@@ -179,6 +179,9 @@ void yyerror(YYLTYPE *yyloc, LCompilers::LPython::Parser &p, const std::string &
179
%token KW_MATCH
180
%token KW_CASE
181
182
+%token KW_STR_PREFIX
183
+%type <string> KW_STR_PREFIX
184
+
185
// Nonterminal tokens
186
187
%type <ast> script_unit
@@ -1102,9 +1105,9 @@ subscript
1102
1105
1103
1106
string
1104
1107
: string TK_STRING { $$ = STRING2($1, $2, @$); } // TODO
- | string id TK_STRING { $$ = STRING4($1, STRING3($2, $3, @$), @$); }
1108
+ | string KW_STR_PREFIX TK_STRING { $$ = STRING4($1, STRING3($2, $3, @$), @$); }
1109
| TK_STRING { $$ = STRING1($1, @$); }
- | id TK_STRING { $$ = STRING3($1, $2, @$); }
1110
+ | KW_STR_PREFIX TK_STRING { $$ = STRING3($1, $2, @$); }
1111
;
1112
1113
lambda_parameter
src/lpython/parser/semantics.h
@@ -800,7 +800,7 @@ static inline ast_t* concat_string(Allocator &al, Location &l,
800
#define INTEGER(x, l) make_ConstantInt_t(p.m_a, l, x, nullptr)
801
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, str_unescape_c(p.m_a, x), nullptr)
802
#define STRING2(x, y, l) concat_string(p.m_a, l, EXPR(x), str_unescape_c(p.m_a, y), nullptr)
803
-#define STRING3(id, x, l) PREFIX_STRING(p.m_a, l, name2char(id), x.c_str(p.m_a))
+#define STRING3(prefix, x, l) PREFIX_STRING(p.m_a, l, prefix.c_str(p.m_a), x.c_str(p.m_a))
804
#define STRING4(x, s, l) concat_string(p.m_a, l, EXPR(x), "", EXPR(s))
805
#define FLOAT(x, l) make_ConstantFloat_t(p.m_a, l, x, nullptr)
806
#define COMPLEX(x, l) make_ConstantComplex_t(p.m_a, l, 0, x, nullptr)
src/lpython/parser/tokenizer.re
@@ -435,6 +435,19 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
435
}
436
437
438
+ [rR][bB] | [bB][rR]
439
+ | [fF][rR] | [rR][fF]
440
+ | [rR] | [bB] | [fF] | [uU]
441
+ {
442
+ if(cur[0] == '\'' || cur[0] == '"'){
443
+ KW(STR_PREFIX);
444
+ }
445
+ else {
446
+ token(yylval.string);
447
+ RET(TK_NAME);
448
449
450
451
// Tokens
452
newline {
453
if(parenlevel) { continue; }
@@ -763,6 +776,7 @@ std::string token2text(const int token)
763
776
764
777
T(KW_MATCH, "match")
765
778
T(KW_CASE, "case")
779
+ T(KW_STR_PREFIX, "string prefix")
766
780
767
781
default : {
768
782
std::cout << "TOKEN: " << token << std::endl;
tests/errors/prefix_string_01.py
@@ -0,0 +1,5 @@
1
+def main():
2
+ # python2 syntax should result in a syntax error
3
+ print "Hello", "World"
4
5
+main()
tests/errors/prefix_string_02.py
@@ -0,0 +1,9 @@
+from lpython import i32
+# fix difference between lpython and cpython in prefix string grammar
+# Prefix should be attached to the quote without any whitespace.
6
7
+ print(r "Hello World")
8
9
tests/reference/ast-prefix_string_01-cf221fd.json
@@ -0,0 +1,13 @@
+{
+ "basename": "ast-prefix_string_01-cf221fd",
+ "cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
+ "infile": "tests/errors/prefix_string_01.py",
+ "infile_hash": "0d83c0e32a78023fccb343a4d3358071792265c1ae357176fe0912eb",
+ "outfile": null,
+ "outfile_hash": null,
+ "stdout": null,
+ "stdout_hash": null,
10
+ "stderr": "ast-prefix_string_01-cf221fd.stderr",
11
+ "stderr_hash": "b600057f41f59ba7fdebe3971bfea0eadca972747ccf70d575c1cdcd",
12
+ "returncode": 1
13
+}
tests/reference/ast-prefix_string_01-cf221fd.stderr
+syntax error: Token '"Hello"' (of type 'string') is unexpected here
+ --> tests/errors/prefix_string_01.py:3:11
+ |
+3 | print "Hello", "World"
+ | ^^^^^^^
tests/reference/ast-prefix_string_02-3d530b2.json
+ "basename": "ast-prefix_string_02-3d530b2",
+ "infile": "tests/errors/prefix_string_02.py",
+ "infile_hash": "5d0c279ea735e60d5243a4b33100832dc1564917d6ef83c9b32705f9",
+ "stderr": "ast-prefix_string_02-3d530b2.stderr",
+ "stderr_hash": "cd72affed29823c0364d52bfb3ba0674d9d7950390b7cd6b04f7538b",
tests/reference/ast-prefix_string_02-3d530b2.stderr
+syntax error: Token '"Hello World"' (of type 'string') is unexpected here
+ --> tests/errors/prefix_string_02.py:7:13
+7 | print(r "Hello World")
+ | ^^^^^^^^^^^^^
tests/tests.toml
@@ -1286,6 +1286,14 @@ asr = true
1286
filename = "errors/unsigned_04.py"
1287
asr = true
1288
1289
+[[test]]
1290
+filename = "errors/prefix_string_01.py"
1291
+ast = true
1292
1293
1294
+filename = "errors/prefix_string_02.py"
1295
1296
1297
# tests/runtime_errors
1298
[[test]]
1299
filename = "runtime_errors/test_list_01.py"
0 commit comments