@@ -47,7 +47,7 @@ namespace grammar_parser {
47
47
pos++;
48
48
}
49
49
if (pos == src) {
50
- throw std::string (" expecting name at " ) + src;
50
+ throw std::runtime_error ( std:: string (" expecting name at " ) + src) ;
51
51
}
52
52
return std::make_pair (pos, parse_space (pos));
53
53
}
@@ -63,7 +63,7 @@ namespace grammar_parser {
63
63
return std::make_pair ((first << 4 ) + second, src + 4 );
64
64
}
65
65
}
66
- throw std::string (" expecting \\ xNN at " ) + src;
66
+ throw std::runtime_error ( std:: string (" expecting \\ xNN at " ) + src) ;
67
67
} else if (esc == ' "' || esc == ' [' || esc == ' ]' ) {
68
68
return std::make_pair (esc, src + 2 );
69
69
} else if (esc == ' r' ) {
@@ -73,11 +73,11 @@ namespace grammar_parser {
73
73
} else if (esc == ' t' ) {
74
74
return std::make_pair (' \t ' , src + 2 );
75
75
}
76
- throw std::string (" unknown escape at " ) + src;
76
+ throw std::runtime_error ( std:: string (" unknown escape at " ) + src) ;
77
77
} else if (*src) {
78
78
return std::make_pair (*src, src + 1 );
79
79
}
80
- throw std::string (" unexpected end of input" );
80
+ throw std::runtime_error (" unexpected end of input" );
81
81
}
82
82
83
83
const char * parse_alternates (
@@ -151,12 +151,12 @@ namespace grammar_parser {
151
151
outbuf.push_back (1 );
152
152
outbuf.push_back (sub_rule_id);
153
153
if (*pos != ' )' ) {
154
- throw std::string (" expecting ')' at " ) + pos;
154
+ throw std::runtime_error ( std:: string (" expecting ')' at " ) + pos) ;
155
155
}
156
156
pos = parse_space (pos + 1 );
157
157
} else if (*pos == ' *' || *pos == ' +' || *pos == ' ?' ) { // repetition operator
158
158
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) ;
160
160
}
161
161
std::vector<uint16_t > & out_grammar = state.out_grammar ;
162
162
@@ -236,7 +236,7 @@ namespace grammar_parser {
236
236
const std::string name (src, name_len);
237
237
238
238
if (!(pos[0 ] == ' :' && pos[1 ] == ' :' && pos[2 ] == ' =' )) {
239
- throw std::string (" expecting ::= at " ) + pos;
239
+ throw std::runtime_error ( std:: string (" expecting ::= at " ) + pos) ;
240
240
}
241
241
pos = parse_space (pos + 3 );
242
242
@@ -247,19 +247,24 @@ namespace grammar_parser {
247
247
} else if (*pos == ' \n ' ) {
248
248
pos++;
249
249
} 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) ;
251
251
}
252
252
return parse_space (pos);
253
253
}
254
254
255
255
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 ();
260
267
}
261
- state.out_grammar .push_back (0xffff );
262
- return state;
263
268
}
264
269
265
270
const uint16_t * print_rule (
0 commit comments