From d0923800e0b2b2476ff39d8144549172b5c668cb Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 29 Jul 2021 08:05:46 -0700 Subject: parser: allow trailing commas in json, via opt-in flag. * parser.c (read_bad_json_s): New symbol variable. (parser_common_init): Propagate value of *read-bad-json* into read_bad_json flag in parser structure. (parser_init): Initialize read_bad_json_s and register the *read-bad-json* dynamic variable. * parser.h (struct parser): New member, read_bad_json. (read_bad_json_s): Declared. * parser.y (json_val): Support an opt_comma symbol just before the closing bracket or brace. (opt_comma): New nonterminal symbol. Recognizes ',' or nothing. Error is flagged if ',' is recognized, and *read-bad-json* is nil. * y.tab.c.shipped: Updated. * tests/010/json.tl: New tests. * txr.1: Documented. --- tests/010/json.tl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests/010/json.tl') diff --git a/tests/010/json.tl b/tests/010/json.tl index a7a73623..e23d36d4 100644 --- a/tests/010/json.tl +++ b/tests/010/json.tl @@ -132,6 +132,23 @@ (get-jsons "true") (t) (get-jsons "1 1 [2] {3:4}") (1.0 1.0 #(2.0) #H(() (3.0 4.0)))) +(mtest + (get-json "{ , }") :error + (get-json "{ 1:2, }") :error + (get-json "{ 1:2, 3:4, }") :error + (get-json "[ , ]") :error + (get-json "[ 1, ]") :error + (get-json "[ 1, 2, ]") :error) + +(let ((*read-bad-json* t)) + (mtest + (get-json "{ , }") :error + (get-json "{ 1:2, }") #H(() (1.0 2.0)) + (get-json "{ 1:2, 3:4, }") #H(() (1.0 2.0) (3.0 4.0)) + (get-json "[ , ]") :error + (get-json "[ 1, ]") #(1.0) + (get-json "[ 1, 2, ]") #(1.0 2.0))) + (mtest (with-out-string-stream (s) (put-json nil s)) "false" (with-out-string-stream (s) (put-jsons nil s)) "" -- cgit v1.2.3