Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Jul 2, 2023
1 parent 85a2da5 commit 7775f4c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions spec/inputs/string.yue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ d = "#{hello world}"
e = "#{1} #{2} #{3}"

f = [[hello #{world} world]]
g = "\#{hello world}"

--

Expand Down
1 change: 1 addition & 0 deletions spec/outputs/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down
5 changes: 3 additions & 2 deletions src/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static std::unordered_set<std::string> 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 {
Expand Down Expand Up @@ -6926,8 +6926,9 @@ class YueCompilerImpl {
switch (content->get_id()) {
case id<DoubleStringInner_t>(): {
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;
}
Expand Down
11 changes: 8 additions & 3 deletions src/yuescript/yue_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/yuescript/yue_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7775f4c

Please sign in to comment.