diff --git a/spec/inputs/string.yue b/spec/inputs/string.yue index 201e60d..f91383e 100644 --- a/spec/inputs/string.yue +++ b/spec/inputs/string.yue @@ -51,6 +51,7 @@ d = "#{hello world}" e = "#{1} #{2} #{3}" f = [[hello #{world} world]] +g = "\#{hello world}" -- diff --git a/spec/outputs/string.lua b/spec/outputs/string.lua index 87ecf2c..febea62 100644 --- a/spec/outputs/string.lua +++ b/spec/outputs/string.lua @@ -29,6 +29,7 @@ local c = "hello " .. tostring(5 + 1) local d = tostring(hello(world)) local e = tostring(1) .. " " .. tostring(2) .. " " .. tostring(3) local f = [[hello #{world} world]] +local g = "#{hello world}" a = 'hello #{hello} hello' b = '#{hello} hello' c = 'hello #{hello}' diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 3dc8e19..3353a2a 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -72,7 +72,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.17.4"sv; +const std::string_view version = "0.17.5"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -6926,8 +6926,9 @@ class YueCompilerImpl { switch (content->get_id()) { case id(): { auto str = _parser.toString(content); - Utils::replace(str, "\r\n"sv, "\n"); + Utils::replace(str, "\r\n"sv, "\n"sv); Utils::replace(str, "\n"sv, "\\n"sv); + Utils::replace(str, "\\#"sv, "#"sv); temp.push_back('\"' + str + '\"'); break; } diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 294f1e5..66ef373 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -98,6 +98,11 @@ YueParser::YueParser() { return false; }); + invalid_interpolation_error = pl::user(true_(), [](const item_t& item) { + throw ParserError("invalid string interpolation"sv, item.begin); + return false; + }); + #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> cut) #define key(str) (expr(str) >> not_alpha_num) @@ -529,9 +534,9 @@ YueParser::YueParser() { single_string_inner = '\\' >> set("'\\") | not_('\'') >> any_char; SingleString = '\'' >> *single_string_inner >> '\''; - interp = "#{" >> space >> Exp >> space >> '}'; - double_string_plain = '\\' >> set("\"\\") | not_('"') >> any_char; - DoubleStringInner = +(not_(interp) >> double_string_plain); + interp = "#{" >> space >> (Exp >> space >> '}' | invalid_interpolation_error); + double_string_plain = '\\' >> set("\"\\#") | not_('"') >> any_char; + DoubleStringInner = +(not_("#{") >> double_string_plain); DoubleStringContent = DoubleStringInner | interp; DoubleString = '"' >> Seperator >> *DoubleStringContent >> '"'; String = DoubleString | SingleString | LuaString; diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 9b4adae..ac6524d 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -137,6 +137,7 @@ class YueParser { NONE_AST_RULE(braces_expression_error); NONE_AST_RULE(brackets_expression_error); NONE_AST_RULE(export_expression_error); + NONE_AST_RULE(invalid_interpolation_error); NONE_AST_RULE(inc_exp_level); NONE_AST_RULE(dec_exp_level);