|
| 1 | +/*! |
| 2 | + * Copyright (c) 2024 by Contributors |
| 3 | + * \file xgrammar/pybind.cc |
| 4 | + */ |
| 5 | + |
| 6 | +#include <pybind11/pybind11.h> |
| 7 | +#include <xgrammar/grammar.h> |
| 8 | +#include <xgrammar/json_schema_converter.h> |
| 9 | + |
| 10 | +namespace py = pybind11; |
| 11 | +using namespace xgrammar; |
| 12 | + |
| 13 | +// PYBIND11_MODULE(xgrammar_bindings, m) { |
| 14 | +// auto pyBNFGrammar = py::class_<BNFGrammar>(m, "BNFGrammar"); |
| 15 | +// pyBNFGrammar.def_static("from_ebnf_string", &BNFGrammar::FromEBNFString) |
| 16 | +// .def("to_string", &BNFGrammar::ToString) |
| 17 | + |
| 18 | +// .def_static("from_json", &BNFGrammar::FromJSON) |
| 19 | +// .def_static("from_schema", &BNFGrammar::FromSchema) |
| 20 | +// .def_static("get_grammar_of_json", &BNFGrammar::GetGrammarOfJSON); |
| 21 | +// } |
| 22 | + |
| 23 | +// namespace xgrammar { |
| 24 | + |
| 25 | +// std::ostream& operator<<(std::ostream& os, const BNFGrammar& grammar) { |
| 26 | +// os << BNFGrammarPrinter(grammar).ToString(); |
| 27 | +// return os; |
| 28 | +// } |
| 29 | + |
| 30 | +// BNFGrammar BNFGrammar::FromEBNFString( |
| 31 | +// const std::string& ebnf_string, const std::string& main_rule |
| 32 | +// ) { |
| 33 | +// auto grammar = EBNFParser::Parse(ebnf_string, main_rule); |
| 34 | +// // Normalize the grammar by default |
| 35 | +// grammar = BNFGrammarNormalizer().Apply(grammar); |
| 36 | +// return grammar; |
| 37 | +// } |
| 38 | + |
| 39 | +// // TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarFromEBNFString") |
| 40 | +// // .set_body_typed([](String ebnf_string, String main_rule) { |
| 41 | +// // return BNFGrammar::FromEBNFString(ebnf_string, main_rule); |
| 42 | +// // }); |
| 43 | + |
| 44 | +// // Parse the EBNF string but not normalize it |
| 45 | +// BNFGrammar DebugFromEBNFStringNoNormalize( |
| 46 | +// const std::string& ebnf_string, const std::string& main_rule |
| 47 | +// ) { |
| 48 | +// return EBNFParser::Parse(ebnf_string, main_rule); |
| 49 | +// } |
| 50 | + |
| 51 | +// // TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarDebugFromEBNFStringNoNormalize") |
| 52 | +// // .set_body_typed([](String ebnf_string, String main_rule) { |
| 53 | +// // return DebugFromEBNFStringNoNormalize(ebnf_string, main_rule); |
| 54 | +// // }); |
| 55 | + |
| 56 | +// BNFGrammar BNFGrammar::FromSchema( |
| 57 | +// const std::string& schema, |
| 58 | +// std::optional<int> indent, |
| 59 | +// std::optional<std::pair<std::string, std::string>> separators, |
| 60 | +// bool strict_mode |
| 61 | +// ) { |
| 62 | +// return FromEBNFString(JSONSchemaToEBNF(schema, indent, separators, strict_mode)); |
| 63 | +// } |
| 64 | + |
| 65 | +// // TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarFromSchema").set_body([](TVMArgs args, TVMRetValue* |
| 66 | +// // rv) { |
| 67 | +// // std::optional<int> indent; |
| 68 | +// // if (args[1].type_code() != kTVMNullptr) { |
| 69 | +// // indent = args[1]; |
| 70 | +// // } else { |
| 71 | +// // indent = std::nullopt; |
| 72 | +// // } |
| 73 | + |
| 74 | +// // std::optional<std::pair<std::string, std::string>> separators; |
| 75 | +// // if (args[2].type_code() != kTVMNullptr) { |
| 76 | +// // Array<String> separators_arr = args[2]; |
| 77 | +// // XGRAMMAR_CHECK(separators_arr.size() == 2); |
| 78 | +// // separators = std::make_pair(separators_arr[0], separators_arr[1]); |
| 79 | +// // } else { |
| 80 | +// // separators = std::nullopt; |
| 81 | +// // } |
| 82 | + |
| 83 | +// // *rv = BNFGrammar::FromSchema(args[0], indent, separators, args[3]); |
| 84 | +// // }); |
| 85 | + |
| 86 | +// // Optimized json grammar for the speed of the grammar state matcher |
| 87 | +// const std::string kJSONGrammarString = R"( |
| 88 | +// main ::= ( |
| 89 | +// "{" [ \n\t]* members_and_embrace | |
| 90 | +// "[" [ \n\t]* elements_or_embrace |
| 91 | +// ) |
| 92 | +// value_non_str ::= ( |
| 93 | +// "{" [ \n\t]* members_and_embrace | |
| 94 | +// "[" [ \n\t]* elements_or_embrace | |
| 95 | +// "0" fraction exponent | |
| 96 | +// [1-9] [0-9]* fraction exponent | |
| 97 | +// "-" [0-9] fraction exponent | |
| 98 | +// "-" [1-9] [0-9]* fraction exponent | |
| 99 | +// "true" | |
| 100 | +// "false" | |
| 101 | +// "null" |
| 102 | +// ) (= [ \n\t,}\]]) |
| 103 | +// members_and_embrace ::= ("\"" characters_and_colon [ \n\t]* members_suffix | "}") (= [ \n\t,}\]]) |
| 104 | +// members_suffix ::= ( |
| 105 | +// value_non_str [ \n\t]* member_suffix_suffix | |
| 106 | +// "\"" characters_and_embrace | |
| 107 | +// "\"" characters_and_comma [ \n\t]* "\"" characters_and_colon [ \n\t]* members_suffix |
| 108 | +// ) (= [ \n\t,}\]]) |
| 109 | +// member_suffix_suffix ::= ( |
| 110 | +// "}" | |
| 111 | +// "," [ \n\t]* "\"" characters_and_colon [ \n\t]* members_suffix |
| 112 | +// ) (= [ \n\t,}\]]) |
| 113 | +// elements_or_embrace ::= ( |
| 114 | +// "{" [ \n\t]* members_and_embrace elements_rest [ \n\t]* "]" | |
| 115 | +// "[" [ \n\t]* elements_or_embrace elements_rest [ \n\t]* "]" | |
| 116 | +// "\"" characters_item elements_rest [ \n\t]* "]" | |
| 117 | +// "0" fraction exponent elements_rest [ \n\t]* "]" | |
| 118 | +// [1-9] [0-9]* fraction exponent elements_rest [ \n\t]* "]" | |
| 119 | +// "-" "0" fraction exponent elements_rest [ \n\t]* "]" | |
| 120 | +// "-" [1-9] [0-9]* fraction exponent elements_rest [ \n\t]* "]" | |
| 121 | +// "true" elements_rest [ \n\t]* "]" | |
| 122 | +// "false" elements_rest [ \n\t]* "]" | |
| 123 | +// "null" elements_rest [ \n\t]* "]" | |
| 124 | +// "]" |
| 125 | +// ) |
| 126 | +// elements ::= ( |
| 127 | +// "{" [ \n\t]* members_and_embrace elements_rest | |
| 128 | +// "[" [ \n\t]* elements_or_embrace elements_rest | |
| 129 | +// "\"" characters_item elements_rest | |
| 130 | +// "0" fraction exponent elements_rest | |
| 131 | +// [1-9] [0-9]* fraction exponent elements_rest | |
| 132 | +// "-" [0-9] fraction exponent elements_rest | |
| 133 | +// "-" [1-9] [0-9]* fraction exponent elements_rest | |
| 134 | +// "true" elements_rest | |
| 135 | +// "false" elements_rest | |
| 136 | +// "null" elements_rest |
| 137 | +// ) |
| 138 | +// elements_rest ::= ( |
| 139 | +// "" | |
| 140 | +// [ \n\t]* "," [ \n\t]* elements |
| 141 | +// ) |
| 142 | +// characters_and_colon ::= ( |
| 143 | +// "\"" [ \n\t]* ":" | |
| 144 | +// [^"\\\x00-\x1F] characters_and_colon | |
| 145 | +// "\\" escape characters_and_colon |
| 146 | +// ) (=[ \n\t]* [\"{[0-9tfn-]) |
| 147 | +// characters_and_comma ::= ( |
| 148 | +// "\"" [ \n\t]* "," | |
| 149 | +// [^"\\\x00-\x1F] characters_and_comma | |
| 150 | +// "\\" escape characters_and_comma |
| 151 | +// ) (=[ \n\t]* "\"") |
| 152 | +// characters_and_embrace ::= ( |
| 153 | +// "\"" [ \n\t]* "}" | |
| 154 | +// [^"\\\x00-\x1F] characters_and_embrace | |
| 155 | +// "\\" escape characters_and_embrace |
| 156 | +// ) (=[ \n\t]* [},]) |
| 157 | +// characters_item ::= ( |
| 158 | +// "\"" | |
| 159 | +// [^"\\\x00-\x1F] characters_item | |
| 160 | +// "\\" escape characters_item |
| 161 | +// ) (= [ \n\t]* [,\]]) |
| 162 | +// escape ::= ["\\/bfnrt] | "u" [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9] [A-Fa-f0-9] |
| 163 | +// fraction ::= "" | "." [0-9] [0-9]* |
| 164 | +// exponent ::= "" | "e" sign [0-9] [0-9]* | "E" sign [0-9] [0-9]* |
| 165 | +// sign ::= "" | "+" | "-" |
| 166 | +// )"; |
| 167 | + |
| 168 | +// BNFGrammar BNFGrammar::GetGrammarOfJSON() { |
| 169 | +// static const BNFGrammar grammar = BNFGrammar::FromEBNFString(kJSONGrammarString, "main"); |
| 170 | +// return grammar; |
| 171 | +// } |
| 172 | + |
| 173 | +// // TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarGetGrammarOfJSON").set_body_typed([]() { |
| 174 | +// // return BNFGrammar::GetGrammarOfJSON(); |
| 175 | +// // }); |
| 176 | + |
| 177 | +// } // namespace xgrammar |
0 commit comments