diff options
author | Li Jin <dragon-fly@qq.com> | 2025-07-18 11:51:39 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2025-07-18 11:51:39 +0800 |
commit | 9d3d8ef2be15dfbf279de71241ff747a568e2c49 (patch) | |
tree | 53f88fa8a0e64dfa2262898312c9b4d988876fe8 /src | |
parent | 51a74726e6f68851bafccba0fe69900274a81d59 (diff) | |
download | yuescript-9d3d8ef2be15dfbf279de71241ff747a568e2c49.tar.gz yuescript-9d3d8ef2be15dfbf279de71241ff747a568e2c49.tar.bz2 yuescript-9d3d8ef2be15dfbf279de71241ff747a568e2c49.zip |
Added specs, tests and docs.
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 46 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 4 | ||||
-rw-r--r-- | src/yuescript/yue_parser.h | 2 |
3 files changed, 29 insertions, 23 deletions
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: | |||
5440 | if (def->defaultValue) { | 5440 | if (def->defaultValue) { |
5441 | defVal = _parser.toString(def->defaultValue); | 5441 | defVal = _parser.toString(def->defaultValue); |
5442 | Utils::trim(defVal); | 5442 | Utils::trim(defVal); |
5443 | defVal = '=' + Utils::toLuaString(defVal); | 5443 | defVal = '=' + Utils::toLuaDoubleString(defVal); |
5444 | } | 5444 | } |
5445 | newArgs.emplace_back(_parser.toString(def->name) + defVal); | 5445 | newArgs.emplace_back(_parser.toString(def->name) + defVal); |
5446 | } | 5446 | } |
@@ -9185,15 +9185,34 @@ private: | |||
9185 | auto line = static_cast<YAMLLine_t*>(line_); | 9185 | auto line = static_cast<YAMLLine_t*>(line_); |
9186 | if (!line->segments.empty()) { | 9186 | if (!line->segments.empty()) { |
9187 | str_list segs; | 9187 | str_list segs; |
9188 | bool firstSeg = true; | ||
9188 | for (auto seg_ : line->segments.objects()) { | 9189 | for (auto seg_ : line->segments.objects()) { |
9189 | auto content = static_cast<YAMLLineContent_t*>(seg_)->content.get(); | 9190 | auto content = static_cast<YAMLLineContent_t*>(seg_)->content.get(); |
9190 | switch (content->get_id()) { | 9191 | switch (content->get_id()) { |
9191 | case id<YAMLLineInner_t>(): { | 9192 | case id<YAMLLineInner_t>(): { |
9192 | auto str = _parser.toString(content); | 9193 | auto seg = _parser.toString(content); |
9193 | Utils::replace(str, "\r\n"sv, "\n"sv); | 9194 | if (!indent) { |
9194 | Utils::replace(str, "\n"sv, "\\n"sv); | 9195 | auto pos = seg.find_first_not_of("\t "sv); |
9195 | Utils::replace(str, "\\#"sv, "#"sv); | 9196 | if (pos == std::string::npos) { |
9196 | segs.push_back('\"' + str + '\"'); | 9197 | indent = seg; |
9198 | firstSeg = false; | ||
9199 | continue; | ||
9200 | } else { | ||
9201 | indent = std::string{seg.c_str(), pos}; | ||
9202 | } | ||
9203 | } | ||
9204 | if (firstSeg) { | ||
9205 | firstSeg = false; | ||
9206 | if (std::string_view{seg}.substr(0, indent.value().size()) != indent.value()) { | ||
9207 | throw CompileError("inconsistent indent"sv, line); | ||
9208 | } | ||
9209 | auto seqStr = seg.substr(indent.value().size()); | ||
9210 | if (!seqStr.empty()) { | ||
9211 | segs.push_back(Utils::toLuaDoubleString(seqStr)); | ||
9212 | } | ||
9213 | } else { | ||
9214 | segs.push_back(Utils::toLuaDoubleString(seg)); | ||
9215 | } | ||
9197 | break; | 9216 | break; |
9198 | } | 9217 | } |
9199 | case id<Exp_t>(): { | 9218 | case id<Exp_t>(): { |
@@ -9204,20 +9223,7 @@ private: | |||
9204 | default: YUEE("AST node mismatch", content); break; | 9223 | default: YUEE("AST node mismatch", content); break; |
9205 | } | 9224 | } |
9206 | } | 9225 | } |
9207 | auto lineStr = join(segs, " .. "sv); | 9226 | temp.push_back(join(segs, " .. "sv)); |
9208 | if (!indent) { | ||
9209 | auto pos = lineStr.find_first_not_of("\t "sv, 1); | ||
9210 | if (pos == std::string::npos) { | ||
9211 | throw CompileError("expecting first line indent"sv, line); | ||
9212 | } | ||
9213 | indent = std::string{lineStr.c_str(), pos}; | ||
9214 | } else { | ||
9215 | if (std::string_view{lineStr}.substr(0, indent.value().size()) != indent.value()) { | ||
9216 | throw CompileError("inconsistent indent"sv, line); | ||
9217 | } | ||
9218 | } | ||
9219 | lineStr = '"' + lineStr.substr(indent.value().size()); | ||
9220 | temp.push_back(lineStr); | ||
9221 | } | 9227 | } |
9222 | } | 9228 | } |
9223 | auto str = join(temp, " .. '\\n' .. "sv); | 9229 | 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() { | |||
645 | YAMLLine = check_indent_match >> Seperator >> +YAMLLineContent | | 645 | YAMLLine = check_indent_match >> Seperator >> +YAMLLineContent | |
646 | advance_match >> Seperator >> ensure(+YAMLLineContent, pop_indent) | | 646 | advance_match >> Seperator >> ensure(+YAMLLineContent, pop_indent) | |
647 | Seperator >> *set(" \t") >> and_(line_break); | 647 | Seperator >> *set(" \t") >> and_(line_break); |
648 | YAMLMultiline = '|' >> Seperator >> +space_break >> advance_match >> ensure(YAMLLine >> *(*set(" \t") >> line_break >> YAMLLine), pop_indent); | 648 | YAMLMultiline = '|' >> space >> Seperator >> +(*set(" \t") >> line_break) >> advance_match >> ensure(YAMLLine >> *(*set(" \t") >> line_break >> YAMLLine), pop_indent); |
649 | 649 | ||
650 | String = DoubleString | SingleString | LuaString | YAMLMultiline; | 650 | String = DoubleString | SingleString | LuaString | YAMLMultiline; |
651 | 651 | ||
@@ -1185,7 +1185,7 @@ void trim(std::string& str) { | |||
1185 | str.erase(str.find_last_not_of(" \t\r\n") + 1); | 1185 | str.erase(str.find_last_not_of(" \t\r\n") + 1); |
1186 | } | 1186 | } |
1187 | 1187 | ||
1188 | std::string toLuaString(const std::string& input) { | 1188 | std::string toLuaDoubleString(const std::string& input) { |
1189 | std::string luaStr = "\""; | 1189 | std::string luaStr = "\""; |
1190 | for (char c : input) { | 1190 | for (char c : input) { |
1191 | switch (c) { | 1191 | 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: | |||
457 | namespace Utils { | 457 | namespace Utils { |
458 | void replace(std::string& str, std::string_view from, std::string_view to); | 458 | void replace(std::string& str, std::string_view from, std::string_view to); |
459 | void trim(std::string& str); | 459 | void trim(std::string& str); |
460 | std::string toLuaString(const std::string& input); | 460 | std::string toLuaDoubleString(const std::string& input); |
461 | } // namespace Utils | 461 | } // namespace Utils |
462 | 462 | ||
463 | } // namespace yue | 463 | } // namespace yue |