Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ void yyerror(YYLTYPE *yyloc, LCompilers::LPython::Parser &p, const std::string &
%token KW_MATCH
%token KW_CASE

%token KW_STR_PREFIX
%type <string> KW_STR_PREFIX

// Nonterminal tokens

%type <ast> script_unit
Expand Down Expand Up @@ -1102,9 +1105,9 @@ subscript

string
: string TK_STRING { $$ = STRING2($1, $2, @$); } // TODO
| string id TK_STRING { $$ = STRING4($1, STRING3($2, $3, @$), @$); }
| string KW_STR_PREFIX TK_STRING { $$ = STRING4($1, STRING3($2, $3, @$), @$); }
| TK_STRING { $$ = STRING1($1, @$); }
| id TK_STRING { $$ = STRING3($1, $2, @$); }
| KW_STR_PREFIX TK_STRING { $$ = STRING3($1, $2, @$); }
;

lambda_parameter
Expand Down
2 changes: 1 addition & 1 deletion src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ static inline ast_t* concat_string(Allocator &al, Location &l,
#define INTEGER(x, l) make_ConstantInt_t(p.m_a, l, x, nullptr)
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, str_unescape_c(p.m_a, x), nullptr)
#define STRING2(x, y, l) concat_string(p.m_a, l, EXPR(x), str_unescape_c(p.m_a, y), nullptr)
#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))
#define STRING4(x, s, l) concat_string(p.m_a, l, EXPR(x), "", EXPR(s))
#define FLOAT(x, l) make_ConstantFloat_t(p.m_a, l, x, nullptr)
#define COMPLEX(x, l) make_ConstantComplex_t(p.m_a, l, 0, x, nullptr)
Expand Down
14 changes: 14 additions & 0 deletions src/lpython/parser/tokenizer.re
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,19 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
}
}

[rR][bB] | [bB][rR]
| [fF][rR] | [rR][fF]
| [rR] | [bB] | [fF] | [uU]
{
if(cur[0] == '\'' || cur[0] == '"'){
KW(STR_PREFIX);
}
else {
token(yylval.string);
RET(TK_NAME);
}
}

// Tokens
newline {
if(parenlevel) { continue; }
Expand Down Expand Up @@ -763,6 +776,7 @@ std::string token2text(const int token)

T(KW_MATCH, "match")
T(KW_CASE, "case")
T(KW_STR_PREFIX, "string prefix")

default : {
std::cout << "TOKEN: " << token << std::endl;
Expand Down
5 changes: 5 additions & 0 deletions tests/errors/prefix_string_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main():
# python2 syntax should result in a syntax error
print "Hello", "World"
Comment thread
mrdaybird marked this conversation as resolved.
Outdated

main()
9 changes: 9 additions & 0 deletions tests/errors/prefix_string_02.py
Original file line number Diff line number Diff line change
@@ -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 with any whitespace.
Comment thread
mrdaybird marked this conversation as resolved.
Outdated

def main():
print(r "Hello World")
Comment thread
mrdaybird marked this conversation as resolved.
Outdated

main()
13 changes: 13 additions & 0 deletions tests/reference/ast-prefix_string_01-cf221fd.json
Original file line number Diff line number Diff line change
@@ -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": "cabdebe33479b322c57ccff7c6b27eae2269832872208d2dc2ce3548",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "ast-prefix_string_01-cf221fd.stderr",
"stderr_hash": "b489b5727a016d8cce4fba576c74f0137d38d2783d38a7c3869df76f",
"returncode": 1
}
5 changes: 5 additions & 0 deletions tests/reference/ast-prefix_string_01-cf221fd.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax error: Token '"Hello"' (of type 'string') is unexpected here
--> tests/errors/prefix_string_01.py:3:8
|
3 | print "Hello", "World"
| ^^^^^^^
Comment thread
mrdaybird marked this conversation as resolved.
Outdated
13 changes: 13 additions & 0 deletions tests/reference/ast-prefix_string_02-3d530b2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "ast-prefix_string_02-3d530b2",
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
"infile": "tests/errors/prefix_string_02.py",
"infile_hash": "6a69d14e7424e65fec4c97212ad5388bc756f0979e3f2c607519b02d",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "ast-prefix_string_02-3d530b2.stderr",
"stderr_hash": "240c89e2b84cbcff81575453f87813d784fa51bfa7c2725436db5f31",
"returncode": 1
}
5 changes: 5 additions & 0 deletions tests/reference/ast-prefix_string_02-3d530b2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax error: Token '"Hello World"' (of type 'string') is unexpected here
--> tests/errors/prefix_string_02.py:7:10
|
7 | print(r "Hello World")
| ^^^^^^^^^^^^^
Comment thread
mrdaybird marked this conversation as resolved.
Outdated
8 changes: 8 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,14 @@ asr = true
filename = "errors/unsigned_04.py"
asr = true

[[test]]
filename = "errors/prefix_string_01.py"
ast = true

[[test]]
filename = "errors/prefix_string_02.py"
ast = true

# tests/runtime_errors
[[test]]
filename = "runtime_errors/test_list_01.py"
Expand Down