Skip to content

Commit 5c577e5

Browse files
committed
[init] Fix compile errors. Now compilable
1 parent 852847e commit 5c577e5

12 files changed

+301
-384
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BasedOnStyle: Google
22
DerivePointerAlignment: false
3-
ColumnLimit: 100
3+
ColumnLimit: 100
44
PointerAlignment: Left
55
AlignAfterOpenBracket: BlockIndent
66
BinPackArguments: false

cpp/grammar.cc

-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ BNFGrammar DebugFromEBNFStringNoNormalize(
4444
// return DebugFromEBNFStringNoNormalize(ebnf_string, main_rule);
4545
// });
4646

47-
BNFGrammar BNFGrammar::FromJSON(const std::string& json_string) {
48-
return BNFJSONParser::Parse(json_string);
49-
}
50-
51-
// TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarFromJSON").set_body_typed([](String json_string) {
52-
// return BNFGrammar::FromJSON(json_string);
53-
// });
54-
5547
BNFGrammar BNFGrammar::FromSchema(
5648
const std::string& schema,
5749
std::optional<int> indent,

cpp/grammar_parser.cc

-26
Original file line numberDiff line numberDiff line change
@@ -462,30 +462,4 @@ BNFGrammar EBNFParser::Parse(std::string ebnf_string, std::string main_rule) {
462462
return parser.DoParse(ebnf_string, main_rule);
463463
}
464464

465-
// BNFGrammar BNFJSONParser::Parse(std::string json_string) {
466-
// auto node = std::make_shared<BNFGrammar::Impl>();
467-
// picojson::value grammar_json_value;
468-
// std::string err = picojson::parse(grammar_json_value, schema);
469-
470-
// auto grammar_json = json::ParseToJSONObject(json_string);
471-
// auto rules_json = json::Lookup<picojson::array>(grammar_json, "rules");
472-
// for (const auto& rule_json : rules_json) {
473-
// auto rule_json_obj = rule_json.get<picojson::object>();
474-
// auto name = json::Lookup<std::string>(rule_json.get<picojson::object>(), "name");
475-
// auto rule_expr = static_cast<int32_t>(
476-
// json::Lookup<int64_t>(rule_json.get<picojson::object>(), "body_expr_id")
477-
// );
478-
// node->rules_.push_back(BNFGrammar::Impl::Rule({name, rule_expr}));
479-
// }
480-
// auto rule_expr_data_json = json::Lookup<picojson::array>(grammar_json, "rule_expr_data");
481-
// for (const auto& data_json : rule_expr_data_json) {
482-
// node->rule_expr_data_.push_back(static_cast<int32_t>(data_json.get<int64_t>()));
483-
// }
484-
// auto rule_expr_indptr_json = json::Lookup<picojson::array>(grammar_json, "rule_expr_indptr");
485-
// for (const auto& index_ptr_json : rule_expr_indptr_json) {
486-
// node->rule_expr_indptr_.push_back(static_cast<int32_t>(index_ptr_json.get<int64_t>()));
487-
// }
488-
// return BNFGrammar(std::move(node));
489-
// }
490-
491465
} // namespace xgrammar

cpp/grammar_parser.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define XGRAMMAR_GRAMMAR_PARSER_H_
99

1010
#include <xgrammar/grammar.h>
11+
#include <xgrammar/support/logging.h>
1112

1213
namespace xgrammar {
1314

@@ -41,19 +42,6 @@ class EBNFParser {
4142
};
4243
};
4344

44-
/*!
45-
* \brief Parse a BNF grammar from the raw representation of the AST in JSON format.
46-
*/
47-
class BNFJSONParser {
48-
public:
49-
/*!
50-
* \brief Parse the JSON string
51-
* \param json_string The JSON string.
52-
* \return The parsed BNF grammar.
53-
*/
54-
static BNFGrammar Parse(std::string json_string);
55-
};
56-
5745
} // namespace xgrammar
5846

5947
#endif // XGRAMMAR_GRAMMAR_PARSER_H_

cpp/grammar_serializer.cc

+5-40
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
#include "grammar_serializer.h"
77

88
#include <picojson.h>
9-
#include <tvm/runtime/registry.h>
10-
11-
#include "../support/encoding.h"
9+
#include <xgrammar/support/encoding.h>
1210

1311
namespace xgrammar {
1412

15-
using namespace tvm::runtime;
16-
1713
std::string BNFGrammarPrinter::PrintRule(const Rule& rule) {
1814
std::string res = rule.name + " ::= " + PrintRuleExpr(rule.body_expr_id);
1915
if (rule.lookahead_assertion_id != -1) {
@@ -132,40 +128,9 @@ std::string BNFGrammarPrinter::ToString() {
132128
return result;
133129
}
134130

135-
TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarToString").set_body_typed([](const BNFGrammar& grammar) {
136-
return BNFGrammarPrinter(grammar).ToString();
137-
});
138-
139-
std::string BNFGrammarJSONSerializer::ToString() {
140-
picojson::object grammar_json_obj;
141-
142-
picojson::array rules_json;
143-
for (const auto& rule : grammar_->rules_) {
144-
picojson::object rule_json;
145-
rule_json["name"] = picojson::value(rule.name);
146-
rule_json["body_expr_id"] = picojson::value(static_cast<int64_t>(rule.body_expr_id));
147-
rules_json.push_back(picojson::value(rule_json));
148-
}
149-
grammar_json_obj["rules"] = picojson::value(rules_json);
150-
151-
picojson::array rule_expr_data_json;
152-
for (const auto& data : grammar_->rule_expr_data_) {
153-
rule_expr_data_json.push_back(picojson::value(static_cast<int64_t>(data)));
154-
}
155-
grammar_json_obj["rule_expr_data"] = picojson::value(rule_expr_data_json);
156-
picojson::array rule_expr_indptr_json;
157-
for (const auto& index_ptr : grammar_->rule_expr_indptr_) {
158-
rule_expr_indptr_json.push_back(picojson::value(static_cast<int64_t>(index_ptr)));
159-
}
160-
grammar_json_obj["rule_expr_indptr"] = picojson::value(rule_expr_indptr_json);
161-
162-
auto grammar_json = picojson::value(grammar_json_obj);
163-
return grammar_json.serialize(prettify_);
164-
}
165-
166-
TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarToJSON")
167-
.set_body_typed([](const BNFGrammar& grammar, bool prettify) {
168-
return BNFGrammarJSONSerializer(grammar, prettify).ToString();
169-
});
131+
// TVM_REGISTER_GLOBAL("mlc.grammar.BNFGrammarToString").set_body_typed([](const BNFGrammar&
132+
// grammar) {
133+
// return BNFGrammarPrinter(grammar).ToString();
134+
// });
170135

171136
} // namespace xgrammar

cpp/grammar_serializer.h

+2-32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <string>
1313

14+
#include "grammar_ast.h"
15+
1416
namespace xgrammar {
1517

1618
/*!
@@ -76,38 +78,6 @@ class BNFGrammarPrinter : public BNFGrammarSerializer {
7678
std::string PrintChoices(const RuleExpr& rule_expr);
7779
};
7880

79-
/*!
80-
* \brief Serialize the raw representation of the BNF AST to a string with JSON format.
81-
* \sa BNFJSONParser::Parse for parsing the JSON string.
82-
* \details JSON format:
83-
* {
84-
* "rules": [
85-
* {"name": "...", "rule_expr": rule_expr_id},
86-
* {"name": "...", "rule_expr": rule_expr_id},
87-
* ],
88-
* "rule_expr_data": [integers...],
89-
* "rule_expr_indptr": [integers...],
90-
* }
91-
*/
92-
class BNFGrammarJSONSerializer : public BNFGrammarSerializer {
93-
public:
94-
/*!
95-
* \brief Constructor.
96-
* \param grammar The grammar to print.
97-
*/
98-
explicit BNFGrammarJSONSerializer(const BNFGrammar& grammar, bool prettify = true)
99-
: BNFGrammarSerializer(grammar), prettify_(prettify) {}
100-
101-
/*!
102-
* \brief Dump the raw representation of the AST to a JSON file.
103-
* \param prettify Whether to format the JSON string. If false, all whitespaces will be removed.
104-
*/
105-
std::string ToString() final;
106-
107-
private:
108-
bool prettify_;
109-
};
110-
11181
} // namespace xgrammar
11282

11383
#endif // XGRAMMAR_GRAMMAR_SERIALIZER_H_

0 commit comments

Comments
 (0)