From 9d3d8ef2be15dfbf279de71241ff747a568e2c49 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 18 Jul 2025 11:51:39 +0800 Subject: Added specs, tests and docs. --- src/yuescript/yue_compiler.cpp | 46 ++++++++++++++++++++++++------------------ src/yuescript/yue_parser.cpp | 4 ++-- src/yuescript/yue_parser.h | 2 +- 3 files changed, 29 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 3768fc9..a8d222d 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -5440,7 +5440,7 @@ private: if (def->defaultValue) { defVal = _parser.toString(def->defaultValue); Utils::trim(defVal); - defVal = '=' + Utils::toLuaString(defVal); + defVal = '=' + Utils::toLuaDoubleString(defVal); } newArgs.emplace_back(_parser.toString(def->name) + defVal); } @@ -9185,15 +9185,34 @@ private: auto line = static_cast(line_); if (!line->segments.empty()) { str_list segs; + bool firstSeg = true; for (auto seg_ : line->segments.objects()) { auto content = static_cast(seg_)->content.get(); switch (content->get_id()) { case id(): { - auto str = _parser.toString(content); - Utils::replace(str, "\r\n"sv, "\n"sv); - Utils::replace(str, "\n"sv, "\\n"sv); - Utils::replace(str, "\\#"sv, "#"sv); - segs.push_back('\"' + str + '\"'); + auto seg = _parser.toString(content); + if (!indent) { + auto pos = seg.find_first_not_of("\t "sv); + if (pos == std::string::npos) { + indent = seg; + firstSeg = false; + continue; + } else { + indent = std::string{seg.c_str(), pos}; + } + } + if (firstSeg) { + firstSeg = false; + if (std::string_view{seg}.substr(0, indent.value().size()) != indent.value()) { + throw CompileError("inconsistent indent"sv, line); + } + auto seqStr = seg.substr(indent.value().size()); + if (!seqStr.empty()) { + segs.push_back(Utils::toLuaDoubleString(seqStr)); + } + } else { + segs.push_back(Utils::toLuaDoubleString(seg)); + } break; } case id(): { @@ -9204,20 +9223,7 @@ private: default: YUEE("AST node mismatch", content); break; } } - auto lineStr = join(segs, " .. "sv); - if (!indent) { - auto pos = lineStr.find_first_not_of("\t "sv, 1); - if (pos == std::string::npos) { - throw CompileError("expecting first line indent"sv, line); - } - indent = std::string{lineStr.c_str(), pos}; - } else { - if (std::string_view{lineStr}.substr(0, indent.value().size()) != indent.value()) { - throw CompileError("inconsistent indent"sv, line); - } - } - lineStr = '"' + lineStr.substr(indent.value().size()); - temp.push_back(lineStr); + temp.push_back(join(segs, " .. "sv)); } } auto str = join(temp, " .. '\\n' .. "sv); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index eebc676..2e21a52 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -645,7 +645,7 @@ YueParser::YueParser() { YAMLLine = check_indent_match >> Seperator >> +YAMLLineContent | advance_match >> Seperator >> ensure(+YAMLLineContent, pop_indent) | Seperator >> *set(" \t") >> and_(line_break); - YAMLMultiline = '|' >> Seperator >> +space_break >> advance_match >> ensure(YAMLLine >> *(*set(" \t") >> line_break >> YAMLLine), pop_indent); + YAMLMultiline = '|' >> space >> Seperator >> +(*set(" \t") >> line_break) >> advance_match >> ensure(YAMLLine >> *(*set(" \t") >> line_break >> YAMLLine), pop_indent); String = DoubleString | SingleString | LuaString | YAMLMultiline; @@ -1185,7 +1185,7 @@ void trim(std::string& str) { str.erase(str.find_last_not_of(" \t\r\n") + 1); } -std::string toLuaString(const std::string& input) { +std::string toLuaDoubleString(const std::string& input) { std::string luaStr = "\""; for (char c : input) { switch (c) { diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 15f9277..f4e0ab1 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -457,7 +457,7 @@ private: namespace Utils { void replace(std::string& str, std::string_view from, std::string_view to); void trim(std::string& str); -std::string toLuaString(const std::string& input); +std::string toLuaDoubleString(const std::string& input); } // namespace Utils } // namespace yue -- cgit v1.2.3-55-g6feb