Skip to content

Commit c02aaa6

Browse files
committed
add free-form operators for const char * to simplify defining literals
1 parent 4bed84d commit c02aaa6

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

common/chat-parser-combinator.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,8 @@ parser::parser(std::shared_ptr<parser_base> parser) : ptr_(std::move(parser)) {}
12221222

12231223
parser::parser(const std::string & literal) : ptr_(std::make_shared<literal_parser>(literal, -1)) {}
12241224

1225+
parser::parser(const char * literal) : ptr_(std::make_shared<literal_parser>(literal, -1)) {}
1226+
12251227
parser parser::operator~() const {
12261228
return parser(std::make_shared<not_parser>(*this, -1));
12271229
}
@@ -1230,30 +1232,18 @@ parser parser::operator+(const parser & other) const {
12301232
return parser(std::make_shared<sequence_parser>(std::initializer_list<parser>{*this, other}, -1));
12311233
}
12321234

1233-
parser parser::operator+(const std::string & literal) const {
1234-
auto lit = parser(std::make_shared<literal_parser>(literal, -1));
1235-
return parser(std::make_shared<sequence_parser>(std::initializer_list<parser>{*this, lit}, -1));
1236-
}
1237-
12381235
parser parser::operator|(const parser & other) const {
12391236
return parser(std::make_shared<choice_parser>(std::initializer_list<parser>{*this, other}, -1));
12401237
}
12411238

1242-
parser parser::operator|(const std::string & literal) const {
1243-
auto lit = parser(std::make_shared<literal_parser>(literal, -1));
1244-
return parser(std::make_shared<choice_parser>(std::initializer_list<parser>{*this, lit}, -1));
1245-
}
1246-
12471239
parser parser::operator<<(const parser & other) const {
12481240
auto ws = parser(std::make_shared<space_parser>(-1));
12491241
return parser(std::make_shared<sequence_parser>(std::initializer_list<parser>{*this, ws, other}, -1));
12501242
}
12511243

1252-
parser parser::operator<<(const std::string & literal) const {
1253-
auto ws = parser(std::make_shared<space_parser>(-1));
1254-
auto lit = parser(std::make_shared<literal_parser>(literal, -1));
1255-
return parser(std::make_shared<sequence_parser>(std::initializer_list<parser>{*this, ws, lit}, -1));
1256-
}
1244+
parser operator+(const char * lhs, const parser & rhs) { return parser(lhs) + rhs; }
1245+
parser operator|(const char * lhs, const parser & rhs) { return parser(lhs) | rhs; }
1246+
parser operator<<(const char * lhs, const parser & rhs) { return parser(lhs) << rhs; }
12571247

12581248
parser_base & parser::operator*() const {
12591249
return *ptr_;
@@ -1373,13 +1363,6 @@ parser parser_builder::json_string(const parser & p) {
13731363
return quote + p + quote;
13741364
}
13751365

1376-
parser parser_builder::between(const std::string & left, const parser & p, const std::string & right, bool allow_spaces) {
1377-
if (allow_spaces) {
1378-
return literal(left) << p << literal(right);
1379-
}
1380-
return literal(left) + p + literal(right);
1381-
}
1382-
13831366
parser parser_builder::add_rule(const std::string & name, const parser & p) {
13841367
(*rules_)[name] = p;
13851368
return rule(name);

common/chat-parser-combinator.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class parser {
106106
parser(std::shared_ptr<parser_base> parser);
107107
parser(const parser & other) = default;
108108
parser(const std::string & literal);
109+
parser(const char * literal);
110+
109111
parser & operator=(const parser & other) {
110112
if (this != &other) {
111113
ptr_ = other.ptr_;
@@ -114,15 +116,9 @@ class parser {
114116
}
115117

116118
parser operator~() const;
117-
118119
parser operator+(const parser & other) const;
119-
parser operator+(const std::string & literal) const;
120-
121120
parser operator|(const parser & other) const;
122-
parser operator|(const std::string & literal) const;
123-
124121
parser operator<<(const parser & other) const;
125-
parser operator<<(const std::string & literal) const;
126122

127123
parser_base & operator*() const;
128124
parser_base * operator->() const;
@@ -136,6 +132,10 @@ class parser {
136132
void build_grammar(const common_grammar_builder & builder) const;
137133
};
138134

135+
parser operator+(const char * lhs, const parser & rhs);
136+
parser operator|(const char * lhs, const parser & rhs);
137+
parser operator<<(const char * lhs, const parser & rhs);
138+
139139
class parser_id_counter {
140140
int next_id_;
141141
public:
@@ -231,8 +231,6 @@ class parser_builder {
231231
parser json_key(const std::string & name, const parser & p);
232232
parser json_string(const parser & p);
233233

234-
parser between(const std::string & left, const parser & p, const std::string & right, bool allow_spaces = true);
235-
236234
// Wraps a parser with JSON schema metadata for grammar generation.
237235
// Used internally to convert JSON schemas to GBNF grammar rules.
238236
parser schema(const parser & p, const std::string & name, const nlohmann::ordered_json & schema);

tests/test-chat-parser-combinator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,10 @@ static void test_gbnf_generation() {
711711
static parser create_command_r7b_parser() {
712712
auto parser = build_parser([](parser_builder & p) {
713713
auto thinking = p.add_rule("thinking",
714-
p.literal("<|START_THINKING|>") << p.until("<|END_THINKING|>") << "<|END_THINKING|>");
714+
"<|START_THINKING|>" << p.until("<|END_THINKING|>") << "<|END_THINKING|>");
715715

716716
auto response = p.add_rule("response",
717-
p.literal("<|START_RESPONSE|>") << p.until("<|END_RESPONSE|>") << "<|END_RESPONSE|>");
717+
"<|START_RESPONSE|>" << p.until("<|END_RESPONSE|>") << "<|END_RESPONSE|>");
718718

719719
auto json = p.add_rule("json", p.json());
720720
auto tool_call_id = p.add_rule("tool-call-id", p.json_key("tool_call_id", p.json_string(p.until("\""))));
@@ -723,14 +723,14 @@ static parser create_command_r7b_parser() {
723723
auto tool_call_fields = p.add_rule("tool-call-fields", tool_call_id | tool_call_name | tool_call_args);
724724

725725
auto tool_call = p.add_rule("tool-call",
726-
p.between("{", tool_call_fields << p.zero_or_more(p.literal(",") << tool_call_fields), "}"));
726+
"{" << tool_call_fields << p.zero_or_more(p.literal(",") << tool_call_fields) << "}");
727727

728728
auto tool_calls = p.add_rule("tool-calls",
729-
p.literal("<|START_ACTION|>")
730-
<< "[" << tool_call << p.zero_or_more(p.literal(",") << tool_call) << "]"
729+
"<|START_ACTION|>"
730+
<< ("[" << tool_call << p.zero_or_more(p.literal(",") << tool_call) << "]")
731731
<< "<|END_ACTION|>");
732732

733-
return p.optional(thinking) << p.add_rule("content", (tool_calls | response));
733+
return p.optional(thinking) << p.add_rule("content", tool_calls | response);
734734
});
735735

736736
auto grammar = build_grammar([&](const common_grammar_builder & builder) {

0 commit comments

Comments
 (0)