Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 674bb08

Browse filesBrowse files
committed
handle & print parser errors
1 parent 9e77f42 commit 674bb08
Copy full SHA for 674bb08

File tree

Expand file treeCollapse file tree

2 files changed

+25
-19
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+25
-19
lines changed

‎examples/grammar-parser.cpp

Copy file name to clipboardExpand all lines: examples/grammar-parser.cpp
+19-14Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace grammar_parser {
4747
pos++;
4848
}
4949
if (pos == src) {
50-
throw std::string("expecting name at ") + src;
50+
throw std::runtime_error(std::string("expecting name at ") + src);
5151
}
5252
return std::make_pair(pos, parse_space(pos));
5353
}
@@ -63,7 +63,7 @@ namespace grammar_parser {
6363
return std::make_pair((first << 4) + second, src + 4);
6464
}
6565
}
66-
throw std::string("expecting \\xNN at ") + src;
66+
throw std::runtime_error(std::string("expecting \\xNN at ") + src);
6767
} else if (esc == '"' || esc == '[' || esc == ']') {
6868
return std::make_pair(esc, src + 2);
6969
} else if (esc == 'r') {
@@ -73,11 +73,11 @@ namespace grammar_parser {
7373
} else if (esc == 't') {
7474
return std::make_pair('\t', src + 2);
7575
}
76-
throw std::string("unknown escape at ") + src;
76+
throw std::runtime_error(std::string("unknown escape at ") + src);
7777
} else if (*src) {
7878
return std::make_pair(*src, src + 1);
7979
}
80-
throw std::string("unexpected end of input");
80+
throw std::runtime_error("unexpected end of input");
8181
}
8282

8383
const char * parse_alternates(
@@ -151,12 +151,12 @@ namespace grammar_parser {
151151
outbuf.push_back(1);
152152
outbuf.push_back(sub_rule_id);
153153
if (*pos != ')') {
154-
throw std::string("expecting ')' at ") + pos;
154+
throw std::runtime_error(std::string("expecting ')' at ") + pos);
155155
}
156156
pos = parse_space(pos + 1);
157157
} else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
158158
if (outbuf.size() - out_start - 1 == 0) {
159-
throw std::string("expecting preceeding item to */+/? at ") + pos;
159+
throw std::runtime_error(std::string("expecting preceeding item to */+/? at ") + pos);
160160
}
161161
std::vector<uint16_t> & out_grammar = state.out_grammar;
162162

@@ -236,7 +236,7 @@ namespace grammar_parser {
236236
const std::string name(src, name_len);
237237

238238
if (!(pos[0] == ':' && pos[1] == ':' && pos[2] == '=')) {
239-
throw std::string("expecting ::= at ") + pos;
239+
throw std::runtime_error(std::string("expecting ::= at ") + pos);
240240
}
241241
pos = parse_space(pos + 3);
242242

@@ -247,19 +247,24 @@ namespace grammar_parser {
247247
} else if (*pos == '\n') {
248248
pos++;
249249
} else if (*pos) {
250-
throw std::string("expecting newline or end at ") + pos;
250+
throw std::runtime_error(std::string("expecting newline or end at ") + pos);
251251
}
252252
return parse_space(pos);
253253
}
254254

255255
parse_state parse(const char * src) {
256-
parse_state state;
257-
const char * pos = parse_space(src);
258-
while (*pos) {
259-
pos = parse_rule(state, pos);
256+
try {
257+
parse_state state;
258+
const char * pos = parse_space(src);
259+
while (*pos) {
260+
pos = parse_rule(state, pos);
261+
}
262+
state.out_grammar.push_back(0xffff);
263+
return state;
264+
} catch (const std::exception & err) {
265+
fprintf(stderr, "%s: error parsing grammar: %s\n", __func__, err.what());
266+
return parse_state();
260267
}
261-
state.out_grammar.push_back(0xffff);
262-
return state;
263268
}
264269

265270
const uint16_t * print_rule(

‎examples/main/main.cpp

Copy file name to clipboardExpand all lines: examples/main/main.cpp
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ int main(int argc, char ** argv) {
296296
llama_grammar * grammar = NULL;
297297
if (!params.grammar.empty()) {
298298
parsed_grammar = grammar_parser::parse(params.grammar.c_str());
299+
// will be empty (default) if there are parse errors
300+
if (parsed_grammar.out_grammar.empty()) {
301+
return 1;
302+
}
299303
fprintf(stderr, "%s: grammar:\n", __func__);
300304
grammar_parser::print_grammar(stderr, parsed_grammar);
301305
fprintf(stderr, "\n");
@@ -631,11 +635,8 @@ int main(int argc, char ** argv) {
631635
if (n_past > 0) {
632636
if (is_interacting) {
633637
// reset grammar state if we're restarting generation
634-
if (!params.grammar.empty()) {
635-
parsed_grammar = grammar_parser::parse(params.grammar.c_str());
636-
if (grammar != NULL) {
637-
llama_grammar_free(grammar);
638-
}
638+
if (grammar != NULL) {
639+
llama_grammar_free(grammar);
639640
grammar = llama_grammar_init(
640641
parsed_grammar.out_grammar.data(), parsed_grammar.symbol_ids.at("root"));
641642
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.