| 1 |
Description: Permit use of rapidyaml-0.11.0+ |
| 2 |
Author: Pat Riehecky <[email protected]> |
| 3 |
Origin: upstream, https://github.com/google/jsonnet/pull/1314 |
| 4 |
|
| 5 |
--- a/CMakeLists.txt |
| 6 |
+++ b/CMakeLists.txt |
| 7 |
@@ -159,6 +159,9 @@ function(configure_jsonnet_obj_target LIB_TARGET_NAME) |
| 8 |
if(USE_SYSTEM_RAPIDYAML) |
| 9 |
target_link_libraries(${LIB_TARGET_NAME} PRIVATE ryml::ryml) |
| 10 |
target_compile_definitions(${LIB_TARGET_NAME} PRIVATE USE_SYSTEM_RAPIDYAML) |
| 11 |
+ if(ryml_VERSION VERSION_GREATER_EQUAL "0.11.0") |
| 12 |
+ target_compile_definitions(${LIB_TARGET_NAME} PRIVATE RYML_NEW_ERROR_API) |
| 13 |
+ endif() |
| 14 |
else() |
| 15 |
target_sources(${LIB_TARGET_NAME} |
| 16 |
PRIVATE |
| 17 |
--- a/core/vm.cpp |
| 18 |
+++ b/core/vm.cpp |
| 19 |
@@ -32,6 +32,10 @@ limitations under the License. |
| 20 |
#include "parser.h" |
| 21 |
#ifdef USE_SYSTEM_RAPIDYAML |
| 22 |
#include <ryml.hpp> |
| 23 |
+#include <ryml_std.hpp> |
| 24 |
+#ifdef RYML_NEW_ERROR_API |
| 25 |
+#include <c4/yml/error.def.hpp> |
| 26 |
+#endif |
| 27 |
#else |
| 28 |
#include "rapidyaml-0.10.0.hpp" |
| 29 |
#endif |
| 30 |
@@ -1715,10 +1718,22 @@ class Interpreter { |
| 31 |
std::string value = encode_utf8(static_cast<HeapString *>(args[0].v.h)->value); |
| 32 |
json j; |
| 33 |
try { |
| 34 |
+#ifdef RYML_NEW_ERROR_API |
| 35 |
+ // rapidyaml >= 0.11.0: positional Callbacks constructor is deprecated; |
| 36 |
+ // use setter API. Pass the source buffer as userdata so the error |
| 37 |
+ // handler can reconstruct the source-context lines that 0.10.0 |
| 38 |
+ // provided automatically inside the pre-formatted inner_msg. |
| 39 |
+ c4::csubstr src = ryml::to_csubstr(value); |
| 40 |
+ ryml::Callbacks cb{}; |
| 41 |
+ cb.m_user_data = &src; |
| 42 |
+ cb.set_error_parse(&Interpreter::handleRapidYamlError); |
| 43 |
+ ryml::EventHandlerTree et{cb}; |
| 44 |
+#else |
| 45 |
// Use a custom EventHandler so we can attach error handling. |
| 46 |
ryml::EventHandlerTree et{ryml::Callbacks{ |
| 47 |
nullptr, nullptr, nullptr, &Interpreter::handleRapidYamlError |
| 48 |
}}; |
| 49 |
+#endif |
| 50 |
ryml::Parser pe(&et); |
| 51 |
ryml::Tree tree = ryml::parse_in_arena(&pe, ryml::to_csubstr(value)); |
| 52 |
|
| 53 |
@@ -3474,6 +3489,55 @@ class Interpreter { |
| 54 |
return r; |
| 55 |
} |
| 56 |
|
| 57 |
+#ifdef RYML_NEW_ERROR_API |
| 58 |
+ static void handleRapidYamlError(c4::csubstr msg, const c4::yml::ErrorDataParse& data, void* userdata) |
| 59 |
+ { |
| 60 |
+ std::ostringstream out; |
| 61 |
+ const c4::yml::Location& loc = data.ymlloc; |
| 62 |
+ |
| 63 |
+ out << "YAML error: "; |
| 64 |
+ if (loc.line != ryml::npos) { |
| 65 |
+ out << loc.line << ":"; |
| 66 |
+ out << (loc.col != ryml::npos ? loc.col : loc.offset) << ":"; |
| 67 |
+ } |
| 68 |
+ out << " ERROR: " << std::string_view(msg.str, msg.len); |
| 69 |
+ |
| 70 |
+ // Emit a single context line matching the old format: |
| 71 |
+ // line:col: snippet (size=N) |
| 72 |
+ // ^ (cols C-C) |
| 73 |
+ if (userdata && loc.line != ryml::npos) { |
| 74 |
+ const auto& src = *static_cast<const c4::csubstr*>(userdata); |
| 75 |
+ |
| 76 |
+ // Find the source line referenced by loc (loc.line is 1-based) |
| 77 |
+ size_t line_start = 0; |
| 78 |
+ size_t line_num = 1; |
| 79 |
+ while (line_num < loc.line && line_start < src.len) { |
| 80 |
+ if (src.str[line_start] == '\n') |
| 81 |
+ ++line_num; |
| 82 |
+ ++line_start; |
| 83 |
+ } |
| 84 |
+ size_t line_end = line_start; |
| 85 |
+ while (line_end < src.len && src.str[line_end] != '\n') |
| 86 |
+ ++line_end; |
| 87 |
+ c4::csubstr srcline = src.sub(line_start, line_end - line_start); |
| 88 |
+ |
| 89 |
+ // Format: \nline:col: snippet (size=N)\n<spaces>^ (cols C-C) |
| 90 |
+ size_t col = (loc.col != ryml::npos ? loc.col : 1); |
| 91 |
+ out << "\n" << loc.line << ":" << col |
| 92 |
+ << ": " << std::string_view(srcline.str, srcline.len) |
| 93 |
+ << " (size=" << srcline.len << ")"; |
| 94 |
+ |
| 95 |
+ // Caret line: pad to align under the column |
| 96 |
+ out << "\n" << std::string( |
| 97 |
+ /* prefix width */ std::to_string(loc.line).size() + 1 + |
| 98 |
+ std::to_string(col).size() + 2 + (col - 1), |
| 99 |
+ ' ') |
| 100 |
+ << "^ (cols " << col << "-" << col << ")\n"; |
| 101 |
+ } |
| 102 |
+ |
| 103 |
+ throw RapidYamlError(out.str()); |
| 104 |
+ } |
| 105 |
+#else |
| 106 |
static void handleRapidYamlError(const char* inner_msg, size_t length, ryml::Location loc, void * /* unused: userdata */) |
| 107 |
{ |
| 108 |
std::ostringstream msg; |
| 109 |
@@ -3486,6 +3550,7 @@ class Interpreter { |
| 110 |
msg << " " << std::string_view(inner_msg, length); |
| 111 |
throw RapidYamlError(msg.str()); |
| 112 |
} |
| 113 |
+#endif |
| 114 |
}; |
| 115 |
|
| 116 |
} // namespace |