summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-06-04 08:11:20 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-06-04 08:11:20 (GMT)
commitc3850a4d3f94436aab6b23020242bc99c71506ee (patch) (unidiff)
tree644cd582c39c833124d1d071a4b7738bac97428d
parent43a1c4ce04cf9301b88c8df31087fe84dcb7e232 (diff)
downloadgrecs-c3850a4d3f94436aab6b23020242bc99c71506ee.tar.gz
grecs-c3850a4d3f94436aab6b23020242bc99c71506ee.tar.bz2
json: improve parser reentrability
* include/grecs/json.h (jsonlex_cleanup): New proto. * src/json-lex.l (jsonlex_setup): Initialize parser state. Initialize json_current_locus_point.file to a non-NULL value. (jsonlex_cleanup): New function. * src/json-gram.y (json_parse_string): Call jsonlex_cleanup before returning. * src/diag.c (default_print_diag): Print the passed errcode argument, instead of errno.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/grecs/json.h1
-rw-r--r--include/wordsplit.h8
-rw-r--r--src/diag.c2
-rw-r--r--src/json-gram.y1
-rw-r--r--src/json-lex.l35
5 files changed, 31 insertions, 16 deletions
diff --git a/include/grecs/json.h b/include/grecs/json.h
index f8402e7..49a6b87 100644
--- a/include/grecs/json.h
+++ b/include/grecs/json.h
@@ -54,6 +54,7 @@ extern struct grecs_locus json_err_locus;
54extern struct json_value *json_return_obj; 54extern struct json_value *json_return_obj;
55 55
56void jsonlex_setup(char const *s, size_t l); 56void jsonlex_setup(char const *s, size_t l);
57void jsonlex_cleanup(void);
57void jsonlex_diag(const char *s); 58void jsonlex_diag(const char *s);
58int json_unescape(char c, char *o); 59int json_unescape(char c, char *o);
59 60
diff --git a/include/wordsplit.h b/include/wordsplit.h
index d13ec3c..7229df3 100644
--- a/include/wordsplit.h
+++ b/include/wordsplit.h
@@ -26,9 +26,11 @@ typedef struct wordsplit wordsplit_t;
26 provide return values when the function returns. If neither mark is 26 provide return values when the function returns. If neither mark is
27 used, the member is internal and must not be used by the caller. 27 used, the member is internal and must not be used by the caller.
28 28
29 In the comments below, the 29 In the comments below, the identifiers in parentheses indicate bits that
30 identifiers in parentheses indicate bits that must be set (or unset, if 30 must be set (or unset, if starting with !) in ws_flags (if starting with
31 starting with !) in the ws_flags to initialize or use the given member. 31 WRDSF_) or ws_options (if starting with WRDSO_) to initialize or use the
32 given member.
33
32 If not redefined explicitly, most of them are set to some reasonable 34 If not redefined explicitly, most of them are set to some reasonable
33 default value upon entry to wordsplit(). */ 35 default value upon entry to wordsplit(). */
34struct wordsplit 36struct wordsplit
diff --git a/src/diag.c b/src/diag.c
index bdbb407..db96e75 100644
--- a/src/diag.c
+++ b/src/diag.c
@@ -37,7 +37,7 @@ default_print_diag(grecs_locus_t const *locus, int err, int errcode,
37 fprintf(stderr, "warning: "); 37 fprintf(stderr, "warning: ");
38 fprintf(stderr, "%s", msg); 38 fprintf(stderr, "%s", msg);
39 if (errcode) 39 if (errcode)
40 fprintf(stderr, ": %s", strerror(errno)); 40 fprintf(stderr, ": %s", strerror(errcode));
41 fputc('\n', stderr); 41 fputc('\n', stderr);
42} 42}
43 43
diff --git a/src/json-gram.y b/src/json-gram.y
index fe8996b..d5b0248 100644
--- a/src/json-gram.y
+++ b/src/json-gram.y
@@ -280,6 +280,7 @@ json_parse_string(char const *input, size_t len)
280 /* FIXME: error recovery */ 280 /* FIXME: error recovery */
281 return NULL; 281 return NULL;
282 } 282 }
283 jsonlex_cleanup();
283 return json_return_obj; 284 return json_return_obj;
284} 285}
285 286
diff --git a/src/json-lex.l b/src/json-lex.l
index 169cf32..4bbdedf 100644
--- a/src/json-lex.l
+++ b/src/json-lex.l
@@ -55,18 +55,6 @@ struct grecs_locus json_err_locus;
55 } while (0); 55 } while (0);
56 56
57void 57void
58jsonlex_setup(char const *s, size_t l)
59{
60 input_ptr = s;
61 input_size = l;
62 json_current_locus_point.file = NULL;
63 json_current_locus_point.line = 1;
64 json_current_locus_point.col = 0;
65 json_err_diag = NULL;
66 yy_flex_debug = 0;
67}
68
69void
70jsonlex_diag(const char *s) 58jsonlex_diag(const char *s)
71{ 59{
72 if (!json_err_diag) { 60 if (!json_err_diag) {
@@ -227,3 +215,26 @@ false { yylval.b = 0; return T_BOOL; }
227\n grecs_locus_point_advance_line(json_current_locus_point); 215\n grecs_locus_point_advance_line(json_current_locus_point);
228. { jsonlex_diag("bogus character"); 216. { jsonlex_diag("bogus character");
229 return T_ERR; } 217 return T_ERR; }
218%%
219void
220jsonlex_setup(char const *s, size_t l)
221{
222 input_ptr = s;
223 input_size = l;
224 json_current_locus_point.file = "input";
225 json_current_locus_point.line = 1;
226 json_current_locus_point.col = 0;
227 json_err_diag = NULL;
228 yy_flex_debug = 0;
229 BEGIN(INITIAL);
230 yyrestart(NULL);
231}
232
233void
234jsonlex_cleanup(void)
235{
236 if (json_line_acc) {
237 grecs_txtacc_free(json_line_acc);
238 json_line_acc = NULL;
239 }
240}

Return to:

Send suggestions and report system problems to the System administrator.