From 591192c3e582b9a750a30be8b00368c49b7ddbec Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Tue, 26 Jul 2022 23:06:56 -0700 Subject: [PATCH] Fix location, Transpiler --- Analysis/src/Transpiler.cpp | 22 ++++++++++++++++++++++ Ast/src/Parser.cpp | 6 ++++-- tests/JsonEncoder.test.cpp | 2 +- tests/Transpiler.test.cpp | 10 ++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Analysis/src/Transpiler.cpp b/Analysis/src/Transpiler.cpp index 9feff1c0..d0bdf4a4 100644 --- a/Analysis/src/Transpiler.cpp +++ b/Analysis/src/Transpiler.cpp @@ -511,6 +511,28 @@ struct Printer writer.keyword("else"); visualize(*a->falseExpr); } + else if (const auto& a = expr.as()) + { + writer.symbol("`"); + + size_t index = 0; + + for (const auto& string : a->strings) + { + writer.write(escape(std::string_view(string.data, string.size))); + + if (index < a->expressions.size) + { + writer.symbol("{"); + visualize(*a->expressions.data[index]); + writer.symbol("}"); + } + + index++; + } + + writer.symbol("`"); + } else if (const auto& a = expr.as()) { writer.symbol("(error-expr"); diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index 440be35b..9a9ac315 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -2638,6 +2638,8 @@ AstExpr* Parser::parseInterpString() std::vector> strings; std::vector expressions; + Location startLocation = lexer.current().location; + do { auto currentLexeme = lexer.current(); LUAU_ASSERT(currentLexeme.type == Lexeme::InterpStringBegin || currentLexeme.type == Lexeme::InterpStringMid || currentLexeme.type == Lexeme::InterpStringEnd); @@ -2651,7 +2653,7 @@ AstExpr* Parser::parseInterpString() if (!Lexer::fixupQuotedString(scratchData)) { nextLexeme(); - return reportExprError(location, {}, "Interpolated string literal contains malformed escape sequence"); + return reportExprError(startLocation, {}, "Interpolated string literal contains malformed escape sequence"); } AstArray chars = copy(scratchData); @@ -2670,7 +2672,7 @@ AstExpr* Parser::parseInterpString() AstArray> stringsArray = copy(strings.data(), strings.size()); AstArray expressionsArray = copy(expressions.data(), expressions.size()); - return allocator.alloc(location, stringsArray, expressionsArray); + return allocator.alloc(startLocation, stringsArray, expressionsArray); } AstExpr* expression = parseExpr(); diff --git a/tests/JsonEncoder.test.cpp b/tests/JsonEncoder.test.cpp index 99bb3187..7d7b18d7 100644 --- a/tests/JsonEncoder.test.cpp +++ b/tests/JsonEncoder.test.cpp @@ -183,7 +183,7 @@ TEST_CASE_FIXTURE(JsonEncoderFixture, "encode_AstExprInterpString") AstStat* statement = expectParseStatement("local a = `var = {x}`"); std::string_view expected = - R"({"type":"AstStatLocal","location":"0,0 - 0,21","vars":[{"luauType":null,"name":"a","type":"AstLocal","location":"0,6 - 0,7"}],"values":[{"type":"AstExprInterpString","location":"0,20 - 0,21","strings":["var = ",""],"expressions":[{"type":"AstExprGlobal","location":"0,18 - 0,19","global":"x"}]}]})"; + R"({"type":"AstStatLocal","location":"0,0 - 0,17","vars":[{"luauType":null,"name":"a","type":"AstLocal","location":"0,6 - 0,7"}],"values":[{"type":"AstExprInterpString","location":"0,10 - 0,17","strings":["var = ",""],"expressions":[{"type":"AstExprGlobal","location":"0,18 - 0,19","global":"x"}]}]})"; CHECK(toJson(statement) == expected); } diff --git a/tests/Transpiler.test.cpp b/tests/Transpiler.test.cpp index d2ed9aef..d6eac999 100644 --- a/tests/Transpiler.test.cpp +++ b/tests/Transpiler.test.cpp @@ -6,6 +6,7 @@ #include "Luau/Transpiler.h" #include "Fixture.h" +#include "ScopedFlags.h" #include "doctest.h" @@ -678,4 +679,13 @@ TEST_CASE_FIXTURE(Fixture, "transpile_for_in_multiple_types") CHECK_EQ(code, transpile(code, {}, true).code); } +TEST_CASE_FIXTURE(Fixture, "transpile_string_interp") +{ + ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; + + std::string code = R"( local _ = `hello {name}` )"; + + CHECK_EQ(code, transpile(code, {}, true).code); +} + TEST_SUITE_END();