json: revert from c++17 to 11

This commit is contained in:
ochafik 2024-03-12 04:07:22 +00:00
parent ee6166af73
commit 6165c55d3a
4 changed files with 28 additions and 23 deletions

View file

@ -135,7 +135,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
if (LLAMA_SYCL) if (LLAMA_SYCL)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
else() else()
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 11)
endif() endif()
set(CMAKE_CXX_STANDARD_REQUIRED true) set(CMAKE_CXX_STANDARD_REQUIRED true)

View file

@ -100,7 +100,7 @@ endif
# keep standard at C11 and C++11 # keep standard at C11 and C++11
MK_CPPFLAGS = -I. -Icommon MK_CPPFLAGS = -I. -Icommon
MK_CFLAGS = -std=c11 -fPIC MK_CFLAGS = -std=c11 -fPIC
MK_CXXFLAGS = -std=c++17 -fPIC MK_CXXFLAGS = -std=c++11 -fPIC
MK_NVCCFLAGS = -std=c++11 MK_NVCCFLAGS = -std=c++11
# -Ofast tends to produce faster code, but may not be available for some compilers. # -Ofast tends to produce faster code, but may not be available for some compilers.

View file

@ -115,7 +115,7 @@ static std::string replacePattern(const std::string& input, const regex& regex,
} }
static string _format_literal(const string& literal) { static string _format_literal(const string& literal) {
string escaped = replacePattern(json(literal).dump(), GRAMMAR_LITERAL_ESCAPE_RE, [&](const auto& match) { string escaped = replacePattern(json(literal).dump(), GRAMMAR_LITERAL_ESCAPE_RE, [&](const smatch& match) {
char c = match.str()[0]; char c = match.str()[0];
return GRAMMAR_LITERAL_ESCAPES.at(c); return GRAMMAR_LITERAL_ESCAPES.at(c);
}); });
@ -123,7 +123,7 @@ static string _format_literal(const string& literal) {
} }
static string _format_range_char(const string& ch) { static string _format_range_char(const string& ch) {
return replacePattern(ch, GRAMMAR_RANGE_LITERAL_ESCAPE_RE, [&](const auto& match) { return replacePattern(ch, GRAMMAR_RANGE_LITERAL_ESCAPE_RE, [&](const smatch& match) {
char c = match.str()[0]; char c = match.str()[0];
return GRAMMAR_LITERAL_ESCAPES.at(c); return GRAMMAR_LITERAL_ESCAPES.at(c);
}); });
@ -132,7 +132,7 @@ static string _format_range_char(const string& ch) {
class SchemaConverter { class SchemaConverter {
private: private:
std::optional<std::function<json(const string&)>> _fetch_json; std::function<json(const string&)> _fetch_json;
bool _dotall; bool _dotall;
map<string, string> _rules; map<string, string> _rules;
unordered_map<string, nlohmann::json> _refs; unordered_map<string, nlohmann::json> _refs;
@ -288,13 +288,16 @@ private:
max_times = stoi(nums[1]); max_times = stoi(nums[1]);
} }
} }
auto [sub, sub_is_literal] = seq.back(); auto &last = seq.back();
auto &sub = last.first;
auto sub_is_literal = last.second;
if (min_times == 0 && max_times == numeric_limits<int>::max()) { if (min_times == 0 && max_times == numeric_limits<int>::max()) {
seq.back().first = sub + "*"; sub += "*";
} else if (min_times == 0 && max_times == 1) { } else if (min_times == 0 && max_times == 1) {
seq.back().first = sub + "?"; sub += "?";
} else if (min_times == 1 && max_times == numeric_limits<int>::max()) { } else if (min_times == 1 && max_times == numeric_limits<int>::max()) {
seq.back().first = sub + "+"; sub += "+";
} else { } else {
if (!sub_is_literal) { if (!sub_is_literal) {
string& sub_id = sub_rule_ids[sub]; string& sub_id = sub_rule_ids[sub];
@ -379,7 +382,10 @@ private:
vector<string> required_props; vector<string> required_props;
vector<string> optional_props; vector<string> optional_props;
unordered_map<string, string> prop_kv_rule_names; unordered_map<string, string> prop_kv_rule_names;
for (const auto& [prop_name, prop_schema] : properties) { for (const auto& kv : properties) {
const auto &prop_name = kv.first;
const auto &prop_schema = kv.second;
string prop_rule_name = visit(prop_schema, name + (name.empty() ? "" : "-") + prop_name); string prop_rule_name = visit(prop_schema, name + (name.empty() ? "" : "-") + prop_name);
prop_kv_rule_names[prop_name] = _add_rule( prop_kv_rule_names[prop_name] = _add_rule(
name + (name.empty() ? "" : "-") + prop_name + "-kv", name + (name.empty() ? "" : "-") + prop_name + "-kv",
@ -446,7 +452,7 @@ private:
public: public:
SchemaConverter( SchemaConverter(
const std::optional<std::function<json(const string&)>>& fetch_json, const std::function<json(const string&)>& fetch_json,
bool dotall) bool dotall)
: _fetch_json(fetch_json), _dotall(dotall) : _fetch_json(fetch_json), _dotall(dotall)
{ {
@ -469,14 +475,14 @@ public:
string ref = n["$ref"]; string ref = n["$ref"];
if (_refs.find(ref) == _refs.end()) { if (_refs.find(ref) == _refs.end()) {
json target; json target;
if (ref.find("https://") == 0 && _fetch_json) { if (ref.find("https://") == 0) {
string base_url = ref.substr(0, ref.find('#')); string base_url = ref.substr(0, ref.find('#'));
auto it = _refs.find(base_url); auto it = _refs.find(base_url);
if (it != _refs.end()) { if (it != _refs.end()) {
target = it->second; target = it->second;
} else { } else {
// Fetch the referenced schema and resolve its refs // Fetch the referenced schema and resolve its refs
auto referenced = _fetch_json.value()(ref); auto referenced = _fetch_json(ref);
resolve_refs(referenced, base_url); resolve_refs(referenced, base_url);
_refs[base_url] = referenced; _refs[base_url] = referenced;
} }
@ -504,8 +510,8 @@ public:
_refs[ref] = target; _refs[ref] = target;
} }
} else { } else {
for (auto& [key, value] : n.items()) { for (auto& kv : n.items()) {
visit_refs(value); visit_refs(kv.value());
} }
} }
} }
@ -628,15 +634,15 @@ public:
} else if ((schema_type.is_null() || schema_type == "string") && schema.contains("pattern")) { } else if ((schema_type.is_null() || schema_type == "string") && schema.contains("pattern")) {
return _visit_pattern(schema["pattern"], rule_name); return _visit_pattern(schema["pattern"], rule_name);
} else if (schema.empty() || (schema.size() == 1 && schema_type == "object")) { } else if (schema.empty() || (schema.size() == 1 && schema_type == "object")) {
for (const auto& [t, r] : PRIMITIVE_RULES) { for (const auto& kv : PRIMITIVE_RULES) {
_add_rule(t, r); _add_rule(kv.first, kv.second);
} }
return "object"; return "object";
} else if ((schema_type.is_null() || schema_type == "string") && regex_match(schema_format, regex("^uuid[1-5]?$"))) { } else if ((schema_type.is_null() || schema_type == "string") && regex_match(schema_format, regex("^uuid[1-5]?$"))) {
return _add_rule(rule_name == "root" ? "root" : schema_format, PRIMITIVE_RULES.at("uuid")); return _add_rule(rule_name == "root" ? "root" : schema_format, PRIMITIVE_RULES.at("uuid"));
} else if ((schema_type.is_null() || schema_type == "string") && DATE_RULES.find(schema_format) != DATE_RULES.end()) { } else if ((schema_type.is_null() || schema_type == "string") && DATE_RULES.find(schema_format) != DATE_RULES.end()) {
for (const auto& [t, r] : DATE_RULES) { for (const auto& kv : DATE_RULES) {
_add_rule(t, r); _add_rule(kv.first, kv.second);
} }
return schema_format + "-string"; return schema_format + "-string";
} else { } else {
@ -660,15 +666,15 @@ public:
string format_grammar() { string format_grammar() {
stringstream ss; stringstream ss;
for (const auto& [name, rule] : _rules) { for (const auto& kv : _rules) {
ss << name << " ::= " << rule << endl; ss << kv.first << " ::= " << kv.second << endl;
} }
return ss.str(); return ss.str();
} }
}; };
string json_schema_to_grammar(const json& schema) { string json_schema_to_grammar(const json& schema) {
SchemaConverter converter(/* fetch_json= */ std::nullopt, /* dotall= */ false); SchemaConverter converter([](const string&) { return json::object(); }, /* dotall= */ false);
auto copy = schema; auto copy = schema;
converter.resolve_refs(copy, "input"); converter.resolve_refs(copy, "input");
converter.visit(copy, ""); converter.visit(copy, "");

View file

@ -9,7 +9,6 @@
#include <regex> #include <regex>
using namespace std; using namespace std;
namespace fs = std::filesystem;
string INPUT_NAME(tmpnam(nullptr)); string INPUT_NAME(tmpnam(nullptr));
string OUT_NAME(tmpnam(nullptr)); string OUT_NAME(tmpnam(nullptr));