From 08494dc57a7fd4c64a91c7a48dddbf6987ce9cc2 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Thu, 28 Jul 2022 18:56:10 -0700 Subject: [PATCH] Interpolated strings as args --- Ast/src/Parser.cpp | 24 ++++++++++++++++-------- tests/Parser.test.cpp | 17 ----------------- tests/conformance/stringinterp.lua | 7 +++++++ 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index dfb57806..9f87444c 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -2010,16 +2010,16 @@ AstExpr* Parser::parsePrimaryExpr(bool asStatement) expr = parseFunctionArgs(expr, false, Location()); } - else if (lexer.current().type == '{' || lexer.current().type == Lexeme::RawString || lexer.current().type == Lexeme::QuotedString) + else if ( + lexer.current().type == '{' + || lexer.current().type == Lexeme::RawString + || lexer.current().type == Lexeme::QuotedString + || lexer.current().type == Lexeme::InterpStringBegin + || lexer.current().type == Lexeme::InterpStringEnd + ) { expr = parseFunctionArgs(expr, false, Location()); } - else if (lexer.current().type == Lexeme::InterpStringBegin) - { - report(lexer.current().location, "Interpolated strings cannot be used alone to call a function. Wrap this in parentheses."); - - break; - } else { break; @@ -2256,7 +2256,7 @@ AstExpr* Parser::parseSimpleExpr() } } -// args ::= `(' [explist] `)' | tableconstructor | String +// args ::= `(' [explist] `)' | tableconstructor | String | InterpString AstExpr* Parser::parseFunctionArgs(AstExpr* func, bool self, const Location& selfLocation) { if (lexer.current().type == '(') @@ -2298,6 +2298,14 @@ AstExpr* Parser::parseFunctionArgs(AstExpr* func, bool self, const Location& sel return allocator.alloc(Location(func->location, expr->location), func, copy(&expr, 1), self, argLocation); } + else if (FFlag::LuauInterpolatedStringBaseSupport && (lexer.current().type == Lexeme::InterpStringBegin || lexer.current().type == Lexeme::InterpStringEnd)) + { + Position argStart = lexer.current().location.end; + AstExpr* expr = parseInterpString(); + Position argEnd = lexer.previousLocation().end; + + return allocator.alloc(Location(func->location, expr->location), func, copy(&expr, 1), self, Location(argStart, argEnd)); + } else { if (self && lexer.current().location.begin.line != func->location.end.line) diff --git a/tests/Parser.test.cpp b/tests/Parser.test.cpp index 1cc31d49..9e8ff250 100644 --- a/tests/Parser.test.cpp +++ b/tests/Parser.test.cpp @@ -1093,23 +1093,6 @@ TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_without_end_brace") CHECK_NE(columnOfEndBraceError("_ = `{a`"), columnOfEndBraceError("_ = `{a`")); } -TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_after_prefixexp") -{ - ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; - - try - { - parse(R"( - print`Hello {name}` - )"); - FAIL("Expected ParseErrors to be thrown"); - } - catch (const ParseErrors& e) - { - CHECK_EQ("Interpolated strings cannot be used alone to call a function. Wrap this in parentheses.", e.getErrors().front().getMessage()); - } -} - TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_as_type_fail") { ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; diff --git a/tests/conformance/stringinterp.lua b/tests/conformance/stringinterp.lua index f2ac3ee5..02a69da8 100644 --- a/tests/conformance/stringinterp.lua +++ b/tests/conformance/stringinterp.lua @@ -54,4 +54,11 @@ end assertEq(shadowsString("hello"), "Value is hello") assertEq(shadowsString(1), "Value is 1") +local function identity(x) + return x +end + +assertEq(identity`text`, "text") +-- assertEq(identity`foo{"bar"}`, "foobar") + return "OK"