From 20f1b6df41c0a4a2e0516d45fdb81f58fa4814c5 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Fri, 27 Jul 2018 05:52:50 +0000 Subject: [PATCH 1/8] Fix json decoding for wrapper messages --- upb/json/parser.rl | 171 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 7 deletions(-) diff --git a/upb/json/parser.rl b/upb/json/parser.rl index c7310354dc..9ceb2a59b7 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -30,6 +30,16 @@ #define UPB_JSON_MAX_DEPTH 64 +static const char *kDoubleValueFullMessageName = "google.protobuf.DoubleValue"; +static const char *kFloatValueFullMessageName = "google.protobuf.FloatValue"; +static const char *kInt64ValueFullMessageName = "google.protobuf.Int64Value"; +static const char *kUInt64ValueFullMessageName = "google.protobuf.UInt64Value"; +static const char *kInt32ValueFullMessageName = "google.protobuf.Int32Value"; +static const char *kUInt32ValueFullMessageName = "google.protobuf.UInt32Value"; +static const char *kBoolValueFullMessageName = "google.protobuf.BoolValue"; +static const char *kStringValueFullMessageName = "google.protobuf.StringValue"; +static const char *kBytesValueFullMessageName = "google.protobuf.BytesValue"; + typedef struct { upb_sink sink; @@ -1229,6 +1239,102 @@ static void end_object(upb_json_parser *p) { } } +static bool is_double_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kDoubleValueFullMessageName) == 0; +} + +static bool is_float_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kFloatValueFullMessageName) == 0; +} + +static bool is_int64_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kInt64ValueFullMessageName) == 0; +} + +static bool is_uint64_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kUInt64ValueFullMessageName) == 0; +} + +static bool is_int32_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kInt32ValueFullMessageName) == 0; +} + +static bool is_uint32_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kUInt32ValueFullMessageName) == 0; +} + +static bool is_bool_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kBoolValueFullMessageName) == 0; +} + +static bool is_string_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kStringValueFullMessageName) == 0; +} + +static bool is_bytes_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kBytesValueFullMessageName) == 0; +} + +static bool is_number_wrapper(const upb_msgdef *m) { + return is_double_value(m) || + is_float_value(m) || + is_int64_value(m) || + is_uint64_value(m) || + is_int32_value(m) || + is_uint32_value(m); +} + +static bool is_string_wrapper(const upb_msgdef *m) { + return is_string_value(m) || + is_bytes_value(m); +} + +static void start_wrapper_object(upb_json_parser *p) { + const char *membername = "value"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); +} + +static void end_wrapper_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static bool does_number_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_number_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_number_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_number_wrapper(p->top->m); +} + +static bool does_string_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_string_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_string_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool does_boolean_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_bool_value(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_boolean_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_bool_value(p->top->m); +} #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -1261,7 +1367,9 @@ static void end_object(upb_json_parser *p) { number_machine := ("-"? integer decimal? exponent?) - <: any >{ fhold; fret; }; + <: any + >{ fhold; fret; } + ; number = /[0-9\-]/ >{ fhold; fcall number_machine; }; text = @@ -1325,15 +1433,63 @@ static void end_object(upb_json_parser *p) { value = number - >{ start_number(parser, p); } - %{ CHECK_RETURN_TOP(end_number(parser, p)); } + >{ + if (does_number_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + start_number(parser, p); + } + %{ + CHECK_RETURN_TOP(end_number(parser, p)); + if (does_number_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } | string - >{ CHECK_RETURN_TOP(start_stringval(parser)); } - @{ CHECK_RETURN_TOP(end_stringval(parser)); } + >{ + if (does_string_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + CHECK_RETURN_TOP(start_stringval(parser)); + } + @{ + CHECK_RETURN_TOP(end_stringval(parser)); + if (does_string_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } | "true" - %{ CHECK_RETURN_TOP(parser_putbool(parser, true)); } + >{ + if (does_boolean_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + } + %{ + CHECK_RETURN_TOP(parser_putbool(parser, true)); + if (does_boolean_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } | "false" - %{ CHECK_RETURN_TOP(parser_putbool(parser, false)); } + >{ + if (does_boolean_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + } + %{ + CHECK_RETURN_TOP(parser_putbool(parser, false)); + if (does_boolean_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } | "null" %{ /* null value */ } | object @@ -1361,6 +1517,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, const char *p = buf; const char *pe = buf + size; + const char *eof = pe; parser->handle = handle; From 9449cf68ccea85a662aa8d4438f26e1634dc285e Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Mon, 30 Jul 2018 06:23:00 +0000 Subject: [PATCH 2/8] Make top level wrapper value parsing work --- upb/json/parser.rl | 86 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 5 deletions(-) diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 9ceb2a59b7..f83af8711d 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -1316,6 +1316,10 @@ static bool does_number_wrapper_end(upb_json_parser *p) { return p->top->m != NULL && is_number_wrapper(p->top->m); } +static bool is_number_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_number_wrapper(p->top->m); +} + static bool does_string_wrapper_start(upb_json_parser *p) { return p->top->f != NULL && upb_fielddef_issubmsg(p->top->f) && @@ -1326,6 +1330,10 @@ static bool does_string_wrapper_end(upb_json_parser *p) { return p->top->m != NULL && is_string_wrapper(p->top->m); } +static bool is_string_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + static bool does_boolean_wrapper_start(upb_json_parser *p) { return p->top->f != NULL && upb_fielddef_issubmsg(p->top->f) && @@ -1336,6 +1344,10 @@ static bool does_boolean_wrapper_end(upb_json_parser *p) { return p->top->m != NULL && is_bool_value(p->top->m); } +static bool is_boolean_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_bool_value(p->top->m); +} + #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -1366,10 +1378,10 @@ static bool does_boolean_wrapper_end(upb_json_parser *p) { exponent = /[eE]/ /[+\-]/? /[0-9]/+; number_machine := - ("-"? integer decimal? exponent?) - <: any - >{ fhold; fret; } - ; + ("-"? integer decimal? exponent?) + <: any + >{ fhold; fret; } + ; number = /[0-9\-]/ >{ fhold; fcall number_machine; }; text = @@ -1421,6 +1433,67 @@ static bool does_boolean_wrapper_end(upb_json_parser *p) { >{ end_object(parser); } ; + true_object = + "true" + >{ + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } + %{ + CHECK_RETURN_TOP(parser_putbool(parser, true)); + end_wrapper_object(parser); + } + ; + + false_object = + "false" + >{ + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } + %{ + CHECK_RETURN_TOP(parser_putbool(parser, false)); + end_wrapper_object(parser); + } + ; + + number_object_machine := + ("-"? integer decimal? exponent?) + >{ + CHECK_RETURN_TOP(is_number_wrapper_object(parser)); + start_wrapper_object(parser); + start_number(parser, p); + } + { + fhold; fcall number_object_machine; + } + ; + + string_object_machine := + (text | unicode_char | escape_char)** + >{ + CHECK_RETURN_TOP(is_string_wrapper_object(parser)); + start_wrapper_object(parser); + CHECK_RETURN_TOP(start_stringval(parser)); + } + '"' + { fhold; fret; } ; - main := ws object ws; + main := + ws + (object | number_object | true_object | false_object | string_object) + ws; }%% %% write data noerror nofinal; From cc27a5b576cd4c7d478e40c2dbca0ba329a14875 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Tue, 31 Jul 2018 19:25:33 +0000 Subject: [PATCH 3/8] Update ragel generated file --- upb/json/parser.c | 657 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 497 insertions(+), 160 deletions(-) diff --git a/upb/json/parser.c b/upb/json/parser.c index 04fedbf06b..38dfe06666 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -32,6 +32,16 @@ #define UPB_JSON_MAX_DEPTH 64 +static const char *kDoubleValueFullMessageName = "google.protobuf.DoubleValue"; +static const char *kFloatValueFullMessageName = "google.protobuf.FloatValue"; +static const char *kInt64ValueFullMessageName = "google.protobuf.Int64Value"; +static const char *kUInt64ValueFullMessageName = "google.protobuf.UInt64Value"; +static const char *kInt32ValueFullMessageName = "google.protobuf.Int32Value"; +static const char *kUInt32ValueFullMessageName = "google.protobuf.UInt32Value"; +static const char *kBoolValueFullMessageName = "google.protobuf.BoolValue"; +static const char *kStringValueFullMessageName = "google.protobuf.StringValue"; +static const char *kBytesValueFullMessageName = "google.protobuf.BytesValue"; + typedef struct { upb_sink sink; @@ -1231,6 +1241,114 @@ static void end_object(upb_json_parser *p) { } } +static bool is_double_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kDoubleValueFullMessageName) == 0; +} + +static bool is_float_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kFloatValueFullMessageName) == 0; +} + +static bool is_int64_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kInt64ValueFullMessageName) == 0; +} + +static bool is_uint64_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kUInt64ValueFullMessageName) == 0; +} + +static bool is_int32_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kInt32ValueFullMessageName) == 0; +} + +static bool is_uint32_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kUInt32ValueFullMessageName) == 0; +} + +static bool is_bool_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kBoolValueFullMessageName) == 0; +} + +static bool is_string_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kStringValueFullMessageName) == 0; +} + +static bool is_bytes_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kBytesValueFullMessageName) == 0; +} + +static bool is_number_wrapper(const upb_msgdef *m) { + return is_double_value(m) || + is_float_value(m) || + is_int64_value(m) || + is_uint64_value(m) || + is_int32_value(m) || + is_uint32_value(m); +} + +static bool is_string_wrapper(const upb_msgdef *m) { + return is_string_value(m) || + is_bytes_value(m); +} + +static void start_wrapper_object(upb_json_parser *p) { + const char *membername = "value"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); +} + +static void end_wrapper_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static bool does_number_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_number_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_number_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_number_wrapper(p->top->m); +} + +static bool is_number_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_number_wrapper(p->top->m); +} + +static bool does_string_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_string_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_string_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool is_string_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool does_boolean_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_bool_value(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_boolean_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_bool_value(p->top->m); +} + +static bool is_boolean_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_bool_value(p->top->m); +} #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -1253,160 +1371,229 @@ static void end_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1349 "upb/json/parser.rl" +#line 1581 "upb/json/parser.rl" -#line 1261 "upb/json/parser.c" +#line 1379 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 2, 1, 3, 1, 5, 1, 6, 1, 7, 1, 8, 1, 10, 1, 12, 1, 13, 1, 14, 1, - 15, 1, 16, 1, 17, 1, 21, 1, - 25, 1, 27, 2, 3, 8, 2, 4, + 15, 1, 16, 1, 17, 1, 18, 1, + 19, 1, 20, 1, 21, 1, 22, 1, + 23, 1, 24, 1, 25, 1, 26, 1, + 27, 1, 31, 1, 32, 1, 34, 1, + 37, 1, 39, 2, 3, 8, 2, 4, 5, 2, 6, 2, 2, 6, 8, 2, 11, 9, 2, 13, 15, 2, 14, 15, - 2, 18, 1, 2, 19, 27, 2, 20, - 9, 2, 22, 27, 2, 23, 27, 2, - 24, 27, 2, 26, 27, 3, 14, 11, - 9 + 2, 23, 2, 2, 28, 1, 2, 29, + 39, 2, 30, 9, 2, 33, 39, 2, + 35, 39, 2, 36, 39, 2, 38, 39, + 3, 14, 11, 9 }; -static const unsigned char _json_key_offsets[] = { - 0, 0, 4, 9, 14, 15, 19, 24, - 29, 34, 38, 42, 45, 48, 50, 54, - 58, 60, 62, 67, 69, 71, 80, 86, - 92, 98, 104, 106, 115, 116, 116, 116, - 121, 126, 131, 132, 133, 134, 135, 135, - 136, 137, 138, 138, 139, 140, 141, 141, - 146, 151, 152, 156, 161, 166, 171, 175, - 175, 178, 178, 178 +static const short _json_key_offsets[] = { + 0, 0, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 23, 28, 29, 33, 38, + 43, 48, 52, 56, 59, 62, 64, 68, + 72, 74, 76, 81, 83, 85, 94, 100, + 106, 112, 118, 120, 124, 127, 129, 133, + 135, 137, 139, 148, 150, 156, 162, 168, + 174, 176, 185, 186, 186, 186, 191, 196, + 201, 202, 203, 204, 205, 205, 206, 207, + 208, 208, 209, 210, 211, 211, 216, 221, + 222, 226, 231, 236, 241, 245, 245, 248, + 251, 254, 254, 254, 257, 261, 263, 268, + 268 }; static const char _json_trans_keys[] = { - 32, 123, 9, 13, 32, 34, 125, 9, - 13, 32, 34, 125, 9, 13, 34, 32, - 58, 9, 13, 32, 93, 125, 9, 13, - 32, 44, 125, 9, 13, 32, 44, 125, - 9, 13, 32, 34, 9, 13, 45, 48, - 49, 57, 48, 49, 57, 46, 69, 101, - 48, 57, 69, 101, 48, 57, 43, 45, - 48, 57, 48, 57, 48, 57, 46, 69, - 101, 48, 57, 34, 92, 34, 92, 34, - 47, 92, 98, 102, 110, 114, 116, 117, - 48, 57, 65, 70, 97, 102, 48, 57, + 32, 34, 45, 102, 116, 123, 9, 13, + 48, 57, 34, 97, 108, 115, 101, 114, + 117, 101, 32, 34, 125, 9, 13, 32, + 34, 125, 9, 13, 34, 32, 58, 9, + 13, 32, 93, 125, 9, 13, 32, 44, + 125, 9, 13, 32, 44, 125, 9, 13, + 32, 34, 9, 13, 45, 48, 49, 57, + 48, 49, 57, 46, 69, 101, 48, 57, + 69, 101, 48, 57, 43, 45, 48, 57, + 48, 57, 48, 57, 46, 69, 101, 48, + 57, 34, 92, 34, 92, 34, 47, 92, + 98, 102, 110, 114, 116, 117, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, 97, 102, - 34, 92, 34, 45, 91, 102, 110, 116, - 123, 48, 57, 34, 32, 93, 125, 9, - 13, 32, 44, 93, 9, 13, 32, 93, - 125, 9, 13, 97, 108, 115, 101, 117, - 108, 108, 114, 117, 101, 32, 34, 125, - 9, 13, 32, 34, 125, 9, 13, 34, - 32, 58, 9, 13, 32, 93, 125, 9, - 13, 32, 44, 125, 9, 13, 32, 44, - 125, 9, 13, 32, 34, 9, 13, 32, - 9, 13, 0 + 48, 57, 65, 70, 97, 102, 34, 92, + 45, 48, 49, 57, 48, 49, 57, 48, + 57, 43, 45, 48, 57, 48, 57, 34, + 92, 34, 92, 34, 47, 92, 98, 102, + 110, 114, 116, 117, 34, 92, 48, 57, + 65, 70, 97, 102, 48, 57, 65, 70, + 97, 102, 48, 57, 65, 70, 97, 102, + 48, 57, 65, 70, 97, 102, 34, 92, + 34, 45, 91, 102, 110, 116, 123, 48, + 57, 34, 32, 93, 125, 9, 13, 32, + 44, 93, 9, 13, 32, 93, 125, 9, + 13, 97, 108, 115, 101, 117, 108, 108, + 114, 117, 101, 32, 34, 125, 9, 13, + 32, 34, 125, 9, 13, 34, 32, 58, + 9, 13, 32, 93, 125, 9, 13, 32, + 44, 125, 9, 13, 32, 44, 125, 9, + 13, 32, 34, 9, 13, 32, 9, 13, + 32, 9, 13, 32, 9, 13, 46, 69, + 101, 69, 101, 48, 57, 48, 57, 46, + 69, 101, 48, 57, 0 }; static const char _json_single_lengths[] = { - 0, 2, 3, 3, 1, 2, 3, 3, + 0, 6, 1, 1, 1, 1, 1, 1, + 1, 1, 3, 3, 1, 2, 3, 3, 3, 2, 2, 1, 3, 0, 2, 2, 0, 0, 3, 2, 2, 9, 0, 0, - 0, 0, 2, 7, 1, 0, 0, 3, - 3, 3, 1, 1, 1, 1, 0, 1, - 1, 1, 0, 1, 1, 1, 0, 3, - 3, 1, 2, 3, 3, 3, 2, 0, - 1, 0, 0, 0 + 0, 0, 2, 2, 1, 0, 2, 0, + 2, 2, 9, 2, 0, 0, 0, 0, + 2, 7, 1, 0, 0, 3, 3, 3, + 1, 1, 1, 1, 0, 1, 1, 1, + 0, 1, 1, 1, 0, 3, 3, 1, + 2, 3, 3, 3, 2, 0, 1, 1, + 1, 0, 0, 3, 2, 0, 3, 0, + 0 }; static const char _json_range_lengths[] = { - 0, 1, 1, 1, 0, 1, 1, 1, + 0, 2, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 3, 3, - 3, 3, 0, 1, 0, 0, 0, 1, - 1, 1, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1, - 1, 0, 1, 1, 1, 1, 1, 0, - 1, 0, 0, 0 + 3, 3, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 3, 3, 3, 3, + 0, 1, 0, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 1, 0, + 1, 1, 1, 1, 1, 0, 1, 1, + 1, 0, 0, 0, 1, 1, 1, 0, + 0 }; static const short _json_index_offsets[] = { - 0, 0, 4, 9, 14, 16, 20, 25, - 30, 35, 39, 43, 46, 50, 52, 56, - 60, 62, 64, 69, 72, 75, 85, 89, - 93, 97, 101, 104, 113, 115, 116, 117, - 122, 127, 132, 134, 136, 138, 140, 141, - 143, 145, 147, 148, 150, 152, 154, 155, - 160, 165, 167, 171, 176, 181, 186, 190, - 191, 194, 195, 196 + 0, 0, 9, 11, 13, 15, 17, 19, + 21, 23, 25, 30, 35, 37, 41, 46, + 51, 56, 60, 64, 67, 71, 73, 77, + 81, 83, 85, 90, 93, 96, 106, 110, + 114, 118, 122, 125, 129, 132, 134, 138, + 140, 143, 146, 156, 159, 163, 167, 171, + 175, 178, 187, 189, 190, 191, 196, 201, + 206, 208, 210, 212, 214, 215, 217, 219, + 221, 222, 224, 226, 228, 229, 234, 239, + 241, 245, 250, 255, 260, 264, 265, 268, + 271, 274, 275, 276, 280, 284, 286, 291, + 292 }; -static const char _json_indicies[] = { - 0, 2, 0, 1, 3, 4, 5, 3, - 1, 6, 7, 8, 6, 1, 9, 1, - 10, 11, 10, 1, 11, 1, 1, 11, - 12, 13, 14, 15, 13, 1, 16, 17, - 8, 16, 1, 17, 7, 17, 1, 18, - 19, 20, 1, 19, 20, 1, 22, 23, - 23, 21, 24, 1, 23, 23, 24, 21, - 25, 25, 26, 1, 26, 1, 26, 21, - 22, 23, 23, 20, 21, 28, 29, 27, - 31, 32, 30, 33, 33, 33, 33, 33, - 33, 33, 33, 34, 1, 35, 35, 35, - 1, 36, 36, 36, 1, 37, 37, 37, - 1, 38, 38, 38, 1, 40, 41, 39, - 42, 43, 44, 45, 46, 47, 48, 43, - 1, 49, 1, 50, 51, 53, 54, 1, - 53, 52, 55, 56, 54, 55, 1, 56, - 1, 1, 56, 52, 57, 1, 58, 1, - 59, 1, 60, 1, 61, 62, 1, 63, - 1, 64, 1, 65, 66, 1, 67, 1, - 68, 1, 69, 70, 71, 72, 70, 1, - 73, 74, 75, 73, 1, 76, 1, 77, - 78, 77, 1, 78, 1, 1, 78, 79, - 80, 81, 82, 80, 1, 83, 84, 75, - 83, 1, 84, 74, 84, 1, 85, 86, - 86, 1, 1, 1, 1, 0 +static const unsigned char _json_indicies[] = { + 0, 2, 3, 4, 5, 6, 0, 3, + 1, 7, 1, 8, 1, 9, 1, 10, + 1, 11, 1, 12, 1, 13, 1, 14, + 1, 15, 16, 17, 15, 1, 18, 19, + 20, 18, 1, 21, 1, 22, 23, 22, + 1, 23, 1, 1, 23, 24, 25, 26, + 27, 25, 1, 28, 29, 20, 28, 1, + 29, 19, 29, 1, 30, 31, 32, 1, + 31, 32, 1, 34, 35, 35, 33, 36, + 1, 35, 35, 36, 33, 37, 37, 38, + 1, 38, 1, 38, 33, 34, 35, 35, + 32, 33, 40, 41, 39, 43, 44, 42, + 45, 45, 45, 45, 45, 45, 45, 45, + 46, 1, 47, 47, 47, 1, 48, 48, + 48, 1, 49, 49, 49, 1, 50, 50, + 50, 1, 52, 53, 51, 54, 55, 56, + 1, 57, 58, 1, 59, 1, 60, 60, + 61, 1, 61, 1, 63, 64, 62, 66, + 67, 65, 68, 68, 68, 68, 68, 68, + 68, 68, 69, 1, 71, 72, 70, 73, + 73, 73, 1, 74, 74, 74, 1, 75, + 75, 75, 1, 76, 76, 76, 1, 78, + 79, 77, 80, 81, 82, 83, 84, 85, + 86, 81, 1, 87, 1, 88, 89, 91, + 92, 1, 91, 90, 93, 94, 92, 93, + 1, 94, 1, 1, 94, 90, 95, 1, + 96, 1, 97, 1, 98, 1, 99, 100, + 1, 101, 1, 102, 1, 103, 104, 1, + 105, 1, 106, 1, 107, 108, 109, 110, + 108, 1, 111, 112, 113, 111, 1, 114, + 1, 115, 116, 115, 1, 116, 1, 1, + 116, 117, 118, 119, 120, 118, 1, 121, + 122, 113, 121, 1, 122, 112, 122, 1, + 123, 7, 7, 1, 124, 124, 1, 125, + 125, 1, 1, 1, 126, 127, 127, 1, + 127, 127, 59, 1, 61, 1, 126, 127, + 127, 58, 1, 1, 1, 0 }; static const char _json_trans_targs[] = { - 1, 0, 2, 3, 4, 56, 3, 4, - 56, 5, 5, 6, 7, 8, 9, 56, - 8, 9, 11, 12, 18, 57, 13, 15, - 14, 16, 17, 20, 58, 21, 20, 58, - 21, 19, 22, 23, 24, 25, 26, 20, - 58, 21, 28, 30, 31, 34, 39, 43, - 47, 29, 59, 59, 32, 31, 29, 32, - 33, 35, 36, 37, 38, 59, 40, 41, - 42, 59, 44, 45, 46, 59, 48, 49, - 55, 48, 49, 55, 50, 50, 51, 52, - 53, 54, 55, 53, 54, 59, 56 + 1, 0, 2, 78, 3, 7, 10, 78, + 4, 5, 6, 79, 8, 9, 80, 11, + 12, 78, 11, 12, 78, 13, 13, 14, + 15, 16, 17, 78, 16, 17, 19, 20, + 26, 81, 21, 23, 22, 24, 25, 28, + 82, 29, 28, 82, 29, 27, 30, 31, + 32, 33, 34, 28, 82, 29, 36, 83, + 86, 83, 86, 84, 39, 85, 41, 87, + 42, 41, 87, 42, 43, 44, 41, 87, + 42, 45, 46, 47, 48, 41, 87, 42, + 50, 52, 53, 56, 61, 65, 69, 51, + 88, 88, 54, 53, 51, 54, 55, 57, + 58, 59, 60, 88, 62, 63, 64, 88, + 66, 67, 68, 88, 70, 71, 77, 70, + 71, 77, 72, 72, 73, 74, 75, 76, + 77, 75, 76, 88, 78, 78, 37, 38 }; static const char _json_trans_actions[] = { - 0, 0, 0, 21, 77, 53, 0, 47, - 23, 17, 0, 0, 15, 19, 19, 50, - 0, 0, 0, 0, 0, 1, 0, 0, - 0, 0, 0, 3, 13, 0, 0, 35, - 5, 11, 0, 38, 7, 7, 7, 41, - 44, 9, 62, 56, 25, 0, 0, 0, - 31, 29, 33, 59, 15, 0, 27, 0, - 0, 0, 0, 0, 0, 68, 0, 0, - 0, 71, 0, 0, 0, 65, 21, 77, - 53, 0, 47, 23, 17, 0, 0, 15, - 19, 19, 50, 0, 0, 74, 0 + 0, 0, 43, 37, 29, 25, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 21, + 104, 77, 0, 71, 23, 17, 0, 0, + 15, 19, 19, 74, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 3, + 13, 0, 0, 59, 5, 11, 0, 62, + 7, 7, 7, 65, 68, 9, 33, 33, + 33, 0, 0, 0, 0, 0, 80, 39, + 39, 0, 5, 5, 11, 0, 3, 0, + 0, 62, 7, 7, 7, 65, 9, 9, + 89, 83, 45, 53, 0, 51, 55, 49, + 57, 86, 15, 0, 47, 0, 0, 0, + 0, 0, 0, 95, 0, 0, 0, 98, + 0, 0, 0, 92, 21, 104, 77, 0, + 71, 23, 17, 0, 0, 15, 19, 19, + 74, 0, 0, 101, 31, 27, 0, 0 +}; + +static const char _json_eof_actions[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 35, 35, 35, 35, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 31, + 27, 0, 0, 35, 35, 35, 35, 41, + 0 }; static const int json_start = 1; -static const int json_en_number_machine = 10; -static const int json_en_string_machine = 19; -static const int json_en_value_machine = 27; +static const int json_en_number_machine = 18; +static const int json_en_string_machine = 27; +static const int json_en_number_object_machine = 35; +static const int json_en_string_object_machine = 40; +static const int json_en_value_machine = 49; static const int json_en_main = 1; -#line 1352 "upb/json/parser.rl" +#line 1584 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -1419,6 +1606,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, const char *p = buf; const char *pe = buf + size; + const char *eof = pe; parser->handle = handle; @@ -1428,7 +1616,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1432 "upb/json/parser.c" +#line 1620 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -1503,118 +1691,226 @@ _match: switch ( *_acts++ ) { case 0: -#line 1264 "upb/json/parser.rl" +#line 1383 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 1: -#line 1265 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 10; goto _again;} } +#line 1385 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 18; goto _again;} } break; case 2: -#line 1269 "upb/json/parser.rl" +#line 1389 "upb/json/parser.rl" { start_text(parser, p); } break; case 3: -#line 1270 "upb/json/parser.rl" +#line 1390 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 4: -#line 1276 "upb/json/parser.rl" +#line 1396 "upb/json/parser.rl" { start_hex(parser); } break; case 5: -#line 1277 "upb/json/parser.rl" +#line 1397 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 6: -#line 1278 "upb/json/parser.rl" +#line 1398 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 7: -#line 1284 "upb/json/parser.rl" +#line 1404 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 8: -#line 1290 "upb/json/parser.rl" +#line 1410 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 9: -#line 1293 "upb/json/parser.rl" - { {stack[top++] = cs; cs = 19; goto _again;} } +#line 1413 "upb/json/parser.rl" + { {stack[top++] = cs; cs = 27; goto _again;} } break; case 10: -#line 1295 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 27; goto _again;} } +#line 1415 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 49; goto _again;} } break; case 11: -#line 1300 "upb/json/parser.rl" +#line 1420 "upb/json/parser.rl" { start_member(parser); } break; case 12: -#line 1301 "upb/json/parser.rl" +#line 1421 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 13: -#line 1304 "upb/json/parser.rl" +#line 1424 "upb/json/parser.rl" { end_member(parser); } break; case 14: -#line 1310 "upb/json/parser.rl" +#line 1430 "upb/json/parser.rl" { start_object(parser); } break; case 15: -#line 1313 "upb/json/parser.rl" +#line 1433 "upb/json/parser.rl" { end_object(parser); } break; case 16: -#line 1319 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_array(parser)); } +#line 1438 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } break; case 17: -#line 1323 "upb/json/parser.rl" - { end_array(parser); } +#line 1442 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(parser_putbool(parser, true)); + end_wrapper_object(parser); + } break; case 18: -#line 1328 "upb/json/parser.rl" - { start_number(parser, p); } +#line 1450 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } break; case 19: -#line 1329 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_number(parser, p)); } +#line 1454 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(parser_putbool(parser, false)); + end_wrapper_object(parser); + } break; case 20: -#line 1331 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_stringval(parser)); } - break; - case 21: -#line 1332 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_stringval(parser)); } +#line 1462 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(is_number_wrapper_object(parser)); + start_wrapper_object(parser); + start_number(parser, p); + } break; case 22: -#line 1334 "upb/json/parser.rl" - { CHECK_RETURN_TOP(parser_putbool(parser, true)); } +#line 1475 "upb/json/parser.rl" + { + p--; {stack[top++] = cs; cs = 35; goto _again;} + } break; case 23: -#line 1336 "upb/json/parser.rl" - { CHECK_RETURN_TOP(parser_putbool(parser, false)); } +#line 1482 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(is_string_wrapper_object(parser)); + start_wrapper_object(parser); + CHECK_RETURN_TOP(start_stringval(parser)); + } break; - case 24: -#line 1338 "upb/json/parser.rl" + case 25: +#line 1495 "upb/json/parser.rl" + { {stack[top++] = cs; cs = 40; goto _again;} } + break; + case 26: +#line 1500 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_array(parser)); } + break; + case 27: +#line 1504 "upb/json/parser.rl" + { end_array(parser); } + break; + case 28: +#line 1509 "upb/json/parser.rl" + { + if (does_number_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + start_number(parser, p); + } + break; + case 29: +#line 1516 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(end_number(parser, p)); + if (does_number_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } + break; + case 30: +#line 1524 "upb/json/parser.rl" + { + if (does_string_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + CHECK_RETURN_TOP(start_stringval(parser)); + } + break; + case 31: +#line 1531 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(end_stringval(parser)); + if (does_string_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } + break; + case 32: +#line 1539 "upb/json/parser.rl" + { + if (does_boolean_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + } + break; + case 33: +#line 1545 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(parser_putbool(parser, true)); + if (does_boolean_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } + break; + case 34: +#line 1553 "upb/json/parser.rl" + { + if (does_boolean_wrapper_start(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + start_wrapper_object(parser); + } + } + break; + case 35: +#line 1559 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(parser_putbool(parser, false)); + if (does_boolean_wrapper_end(parser)) { + end_wrapper_object(parser); + end_subobject(parser); + } + } + break; + case 36: +#line 1567 "upb/json/parser.rl" { /* null value */ } break; - case 25: -#line 1340 "upb/json/parser.rl" + case 37: +#line 1569 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject(parser)); } break; - case 26: -#line 1341 "upb/json/parser.rl" + case 38: +#line 1570 "upb/json/parser.rl" { end_subobject(parser); } break; - case 27: -#line 1346 "upb/json/parser.rl" + case 39: +#line 1575 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1618 "upb/json/parser.c" +#line 1914 "upb/json/parser.c" } } @@ -1624,10 +1920,51 @@ _again: if ( ++p != pe ) goto _resume; _test_eof: {} + if ( p == eof ) + { + const char *__acts = _json_actions + _json_eof_actions[cs]; + unsigned int __nacts = (unsigned int) *__acts++; + while ( __nacts-- > 0 ) { + switch ( *__acts++ ) { + case 17: +#line 1442 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(parser_putbool(parser, true)); + end_wrapper_object(parser); + } + break; + case 19: +#line 1454 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(parser_putbool(parser, false)); + end_wrapper_object(parser); + } + break; + case 21: +#line 1467 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(end_number(parser, p)); + end_wrapper_object(parser); + p--; {cs = stack[--top]; goto _again;} + } + break; + case 24: +#line 1488 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(end_stringval(parser)); + end_wrapper_object(parser); + p--; {cs = stack[--top]; goto _again;} + } + break; +#line 1960 "upb/json/parser.c" + } + } + } + _out: {} } -#line 1373 "upb/json/parser.rl" +#line 1606 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -1668,13 +2005,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 1672 "upb/json/parser.c" +#line 2009 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1413 "upb/json/parser.rl" +#line 1646 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); From ae2f1cdb174e4458b2a3440a6125ad904c294d71 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Wed, 1 Aug 2018 00:37:35 +0000 Subject: [PATCH 4/8] Mark unused variable --- upb/json/parser.c | 6 ++++-- upb/json/parser.rl | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/upb/json/parser.c b/upb/json/parser.c index 38dfe06666..bb3b778091 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -1988,7 +1988,9 @@ bool end(void *closure, const void *hd) { /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); UPB_UNUSED(json_en_number_machine); + UPB_UNUSED(json_en_number_object_machine); UPB_UNUSED(json_en_string_machine); + UPB_UNUSED(json_en_string_object_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); return true; @@ -2005,13 +2007,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 2009 "upb/json/parser.c" +#line 2011 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1646 "upb/json/parser.rl" +#line 1648 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index f83af8711d..389a09a869 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -1626,7 +1626,9 @@ bool end(void *closure, const void *hd) { /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); UPB_UNUSED(json_en_number_machine); + UPB_UNUSED(json_en_number_object_machine); UPB_UNUSED(json_en_string_machine); + UPB_UNUSED(json_en_string_object_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); return true; From ac9d4468f5d9f88e0b0d5dd9f2cc0131e3036b16 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Sun, 5 Aug 2018 23:15:14 +0000 Subject: [PATCH 5/8] Make value top level --- upb/json/parser.c | 643 ++++++++++++++++++++++----------------------- upb/json/parser.rl | 146 +++++----- 2 files changed, 382 insertions(+), 407 deletions(-) diff --git a/upb/json/parser.c b/upb/json/parser.c index bb3b778091..7665e1f103 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -109,6 +109,12 @@ struct upb_json_parser { /* Whether to proceed if unknown field is met. */ bool ignore_json_unknown; + + /* Whether to end parsing. */ + bool ready_to_end; + + /* Indicate whehter last paring ended while parsing number */ + bool parsing_number; }; struct upb_json_parsermethod { @@ -618,6 +624,7 @@ static bool end_text(upb_json_parser *p, const char *ptr) { } static void start_number(upb_json_parser *p, const char *ptr) { + p->parsing_number = true; multipart_startaccum(p); capture_begin(p, ptr); } @@ -625,6 +632,7 @@ static void start_number(upb_json_parser *p, const char *ptr) { static bool parse_number(upb_json_parser *p, bool is_quoted); static bool end_number(upb_json_parser *p, const char *ptr) { + p->parsing_number = false; if (!capture_end(p, ptr)) { return false; } @@ -1308,6 +1316,10 @@ static void end_wrapper_object(upb_json_parser *p) { end_object(p); } +static bool is_top_level(upb_json_parser *p) { + return p->top == p->stack && p->top->f == NULL; +} + static bool does_number_wrapper_start(upb_json_parser *p) { return p->top->f != NULL && upb_fielddef_issubmsg(p->top->f) && @@ -1371,229 +1383,205 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1581 "upb/json/parser.rl" +#line 1562 "upb/json/parser.rl" -#line 1379 "upb/json/parser.c" +#line 1391 "upb/json/parser.c" static const char _json_actions[] = { - 0, 1, 0, 1, 2, 1, 3, 1, - 5, 1, 6, 1, 7, 1, 8, 1, - 10, 1, 12, 1, 13, 1, 14, 1, + 0, 1, 0, 1, 1, 1, 3, 1, + 4, 1, 6, 1, 7, 1, 8, 1, + 9, 1, 11, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, - 19, 1, 20, 1, 21, 1, 22, 1, - 23, 1, 24, 1, 25, 1, 26, 1, - 27, 1, 31, 1, 32, 1, 34, 1, - 37, 1, 39, 2, 3, 8, 2, 4, - 5, 2, 6, 2, 2, 6, 8, 2, - 11, 9, 2, 13, 15, 2, 14, 15, - 2, 23, 2, 2, 28, 1, 2, 29, - 39, 2, 30, 9, 2, 33, 39, 2, - 35, 39, 2, 36, 39, 2, 38, 39, - 3, 14, 11, 9 + 20, 1, 22, 1, 23, 1, 24, 1, + 25, 1, 26, 1, 27, 1, 28, 1, + 29, 1, 30, 2, 4, 9, 2, 5, + 6, 2, 7, 3, 2, 7, 9, 2, + 12, 10, 2, 14, 16, 2, 15, 16, + 2, 19, 2, 2, 20, 30, 2, 21, + 10, 2, 24, 30, 2, 26, 30, 2, + 27, 30, 2, 29, 30, 3, 15, 12, + 10 }; -static const short _json_key_offsets[] = { - 0, 0, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 23, 28, 29, 33, 38, - 43, 48, 52, 56, 59, 62, 64, 68, - 72, 74, 76, 81, 83, 85, 94, 100, - 106, 112, 118, 120, 124, 127, 129, 133, - 135, 137, 139, 148, 150, 156, 162, 168, - 174, 176, 185, 186, 186, 186, 191, 196, - 201, 202, 203, 204, 205, 205, 206, 207, - 208, 208, 209, 210, 211, 211, 216, 221, - 222, 226, 231, 236, 241, 245, 245, 248, - 251, 254, 254, 254, 257, 261, 263, 268, - 268 +static const unsigned char _json_key_offsets[] = { + 0, 0, 12, 13, 18, 23, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, + 38, 43, 48, 49, 53, 58, 63, 68, + 72, 76, 79, 82, 84, 88, 92, 94, + 96, 101, 103, 105, 114, 120, 126, 132, + 138, 140, 149, 150, 150, 150, 155, 160, + 165, 166, 167, 168, 169, 169, 170, 171, + 172, 172, 173, 174, 175, 175, 180, 185, + 186, 190, 195, 200, 205, 209, 209, 212, + 215, 218, 221, 224, 227, 227, 227 }; static const char _json_trans_keys[] = { - 32, 34, 45, 102, 116, 123, 9, 13, - 48, 57, 34, 97, 108, 115, 101, 114, - 117, 101, 32, 34, 125, 9, 13, 32, - 34, 125, 9, 13, 34, 32, 58, 9, - 13, 32, 93, 125, 9, 13, 32, 44, - 125, 9, 13, 32, 44, 125, 9, 13, - 32, 34, 9, 13, 45, 48, 49, 57, - 48, 49, 57, 46, 69, 101, 48, 57, - 69, 101, 48, 57, 43, 45, 48, 57, - 48, 57, 48, 57, 46, 69, 101, 48, - 57, 34, 92, 34, 92, 34, 47, 92, - 98, 102, 110, 114, 116, 117, 48, 57, - 65, 70, 97, 102, 48, 57, 65, 70, - 97, 102, 48, 57, 65, 70, 97, 102, - 48, 57, 65, 70, 97, 102, 34, 92, - 45, 48, 49, 57, 48, 49, 57, 48, - 57, 43, 45, 48, 57, 48, 57, 34, - 92, 34, 92, 34, 47, 92, 98, 102, - 110, 114, 116, 117, 34, 92, 48, 57, + 32, 34, 45, 91, 102, 110, 116, 123, + 9, 13, 48, 57, 34, 32, 93, 125, + 9, 13, 32, 44, 93, 9, 13, 32, + 93, 125, 9, 13, 97, 108, 115, 101, + 117, 108, 108, 114, 117, 101, 32, 34, + 125, 9, 13, 32, 34, 125, 9, 13, + 34, 32, 58, 9, 13, 32, 93, 125, + 9, 13, 32, 44, 125, 9, 13, 32, + 44, 125, 9, 13, 32, 34, 9, 13, + 45, 48, 49, 57, 48, 49, 57, 46, + 69, 101, 48, 57, 69, 101, 48, 57, + 43, 45, 48, 57, 48, 57, 48, 57, + 46, 69, 101, 48, 57, 34, 92, 34, + 92, 34, 47, 92, 98, 102, 110, 114, + 116, 117, 48, 57, 65, 70, 97, 102, + 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, - 97, 102, 48, 57, 65, 70, 97, 102, - 48, 57, 65, 70, 97, 102, 34, 92, - 34, 45, 91, 102, 110, 116, 123, 48, - 57, 34, 32, 93, 125, 9, 13, 32, - 44, 93, 9, 13, 32, 93, 125, 9, - 13, 97, 108, 115, 101, 117, 108, 108, - 114, 117, 101, 32, 34, 125, 9, 13, - 32, 34, 125, 9, 13, 34, 32, 58, - 9, 13, 32, 93, 125, 9, 13, 32, - 44, 125, 9, 13, 32, 44, 125, 9, - 13, 32, 34, 9, 13, 32, 9, 13, - 32, 9, 13, 32, 9, 13, 46, 69, - 101, 69, 101, 48, 57, 48, 57, 46, - 69, 101, 48, 57, 0 + 97, 102, 34, 92, 34, 45, 91, 102, + 110, 116, 123, 48, 57, 34, 32, 93, + 125, 9, 13, 32, 44, 93, 9, 13, + 32, 93, 125, 9, 13, 97, 108, 115, + 101, 117, 108, 108, 114, 117, 101, 32, + 34, 125, 9, 13, 32, 34, 125, 9, + 13, 34, 32, 58, 9, 13, 32, 93, + 125, 9, 13, 32, 44, 125, 9, 13, + 32, 44, 125, 9, 13, 32, 34, 9, + 13, 32, 9, 13, 32, 9, 13, 32, + 9, 13, 32, 9, 13, 32, 9, 13, + 32, 9, 13, 0 }; static const char _json_single_lengths[] = { - 0, 6, 1, 1, 1, 1, 1, 1, - 1, 1, 3, 3, 1, 2, 3, 3, - 3, 2, 2, 1, 3, 0, 2, 2, - 0, 0, 3, 2, 2, 9, 0, 0, - 0, 0, 2, 2, 1, 0, 2, 0, - 2, 2, 9, 2, 0, 0, 0, 0, + 0, 8, 1, 3, 3, 3, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 3, 3, 1, 2, 3, 3, 3, 2, + 2, 1, 3, 0, 2, 2, 0, 0, + 3, 2, 2, 9, 0, 0, 0, 0, 2, 7, 1, 0, 0, 3, 3, 3, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 3, 3, 1, 2, 3, 3, 3, 2, 0, 1, 1, - 1, 0, 0, 3, 2, 0, 3, 0, - 0 + 1, 1, 1, 1, 0, 0, 0 }; static const char _json_range_lengths[] = { - 0, 2, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 1, 0, 1, 1, 1, - 1, 1, 1, 1, 0, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 3, 3, - 3, 3, 0, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 3, 3, 3, 3, + 0, 2, 0, 1, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 3, 3, 3, 3, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, - 1, 0, 0, 0, 1, 1, 1, 0, - 0 + 1, 1, 1, 1, 0, 0, 0 }; static const short _json_index_offsets[] = { - 0, 0, 9, 11, 13, 15, 17, 19, - 21, 23, 25, 30, 35, 37, 41, 46, - 51, 56, 60, 64, 67, 71, 73, 77, - 81, 83, 85, 90, 93, 96, 106, 110, - 114, 118, 122, 125, 129, 132, 134, 138, - 140, 143, 146, 156, 159, 163, 167, 171, - 175, 178, 187, 189, 190, 191, 196, 201, - 206, 208, 210, 212, 214, 215, 217, 219, - 221, 222, 224, 226, 228, 229, 234, 239, - 241, 245, 250, 255, 260, 264, 265, 268, - 271, 274, 275, 276, 280, 284, 286, 291, - 292 + 0, 0, 11, 13, 18, 23, 28, 30, + 32, 34, 36, 38, 40, 42, 44, 46, + 48, 53, 58, 60, 64, 69, 74, 79, + 83, 87, 90, 94, 96, 100, 104, 106, + 108, 113, 116, 119, 129, 133, 137, 141, + 145, 148, 157, 159, 160, 161, 166, 171, + 176, 178, 180, 182, 184, 185, 187, 189, + 191, 192, 194, 196, 198, 199, 204, 209, + 211, 215, 220, 225, 230, 234, 235, 238, + 241, 244, 247, 250, 253, 254, 255 }; -static const unsigned char _json_indicies[] = { - 0, 2, 3, 4, 5, 6, 0, 3, - 1, 7, 1, 8, 1, 9, 1, 10, - 1, 11, 1, 12, 1, 13, 1, 14, - 1, 15, 16, 17, 15, 1, 18, 19, - 20, 18, 1, 21, 1, 22, 23, 22, - 1, 23, 1, 1, 23, 24, 25, 26, - 27, 25, 1, 28, 29, 20, 28, 1, - 29, 19, 29, 1, 30, 31, 32, 1, - 31, 32, 1, 34, 35, 35, 33, 36, - 1, 35, 35, 36, 33, 37, 37, 38, - 1, 38, 1, 38, 33, 34, 35, 35, - 32, 33, 40, 41, 39, 43, 44, 42, - 45, 45, 45, 45, 45, 45, 45, 45, - 46, 1, 47, 47, 47, 1, 48, 48, - 48, 1, 49, 49, 49, 1, 50, 50, - 50, 1, 52, 53, 51, 54, 55, 56, - 1, 57, 58, 1, 59, 1, 60, 60, - 61, 1, 61, 1, 63, 64, 62, 66, - 67, 65, 68, 68, 68, 68, 68, 68, - 68, 68, 69, 1, 71, 72, 70, 73, - 73, 73, 1, 74, 74, 74, 1, 75, - 75, 75, 1, 76, 76, 76, 1, 78, - 79, 77, 80, 81, 82, 83, 84, 85, - 86, 81, 1, 87, 1, 88, 89, 91, - 92, 1, 91, 90, 93, 94, 92, 93, - 1, 94, 1, 1, 94, 90, 95, 1, - 96, 1, 97, 1, 98, 1, 99, 100, - 1, 101, 1, 102, 1, 103, 104, 1, - 105, 1, 106, 1, 107, 108, 109, 110, - 108, 1, 111, 112, 113, 111, 1, 114, - 1, 115, 116, 115, 1, 116, 1, 1, - 116, 117, 118, 119, 120, 118, 1, 121, - 122, 113, 121, 1, 122, 112, 122, 1, - 123, 7, 7, 1, 124, 124, 1, 125, - 125, 1, 1, 1, 126, 127, 127, 1, - 127, 127, 59, 1, 61, 1, 126, 127, - 127, 58, 1, 1, 1, 0 +static const char _json_indicies[] = { + 0, 2, 3, 4, 5, 6, 7, 8, + 0, 3, 1, 9, 1, 11, 12, 1, + 11, 10, 13, 14, 12, 13, 1, 14, + 1, 1, 14, 10, 15, 1, 16, 1, + 17, 1, 18, 1, 19, 1, 20, 1, + 21, 1, 22, 1, 23, 1, 24, 1, + 25, 26, 27, 25, 1, 28, 29, 30, + 28, 1, 31, 1, 32, 33, 32, 1, + 33, 1, 1, 33, 34, 35, 36, 37, + 35, 1, 38, 39, 30, 38, 1, 39, + 29, 39, 1, 40, 41, 42, 1, 41, + 42, 1, 44, 45, 45, 43, 46, 1, + 45, 45, 46, 43, 47, 47, 48, 1, + 48, 1, 48, 43, 44, 45, 45, 42, + 43, 50, 51, 49, 53, 54, 52, 55, + 55, 55, 55, 55, 55, 55, 55, 56, + 1, 57, 57, 57, 1, 58, 58, 58, + 1, 59, 59, 59, 1, 60, 60, 60, + 1, 62, 63, 61, 64, 65, 66, 67, + 68, 69, 70, 65, 1, 71, 1, 72, + 73, 75, 76, 1, 75, 74, 77, 78, + 76, 77, 1, 78, 1, 1, 78, 74, + 79, 1, 80, 1, 81, 1, 82, 1, + 83, 84, 1, 85, 1, 86, 1, 87, + 88, 1, 89, 1, 90, 1, 91, 92, + 93, 94, 92, 1, 95, 96, 97, 95, + 1, 98, 1, 99, 100, 99, 1, 100, + 1, 1, 100, 101, 102, 103, 104, 102, + 1, 105, 106, 97, 105, 1, 106, 96, + 106, 1, 107, 108, 108, 1, 109, 109, + 1, 110, 110, 1, 111, 111, 1, 112, + 112, 1, 113, 113, 1, 1, 1, 1, + 0 }; static const char _json_trans_targs[] = { - 1, 0, 2, 78, 3, 7, 10, 78, - 4, 5, 6, 79, 8, 9, 80, 11, - 12, 78, 11, 12, 78, 13, 13, 14, - 15, 16, 17, 78, 16, 17, 19, 20, - 26, 81, 21, 23, 22, 24, 25, 28, - 82, 29, 28, 82, 29, 27, 30, 31, - 32, 33, 34, 28, 82, 29, 36, 83, - 86, 83, 86, 84, 39, 85, 41, 87, - 42, 41, 87, 42, 43, 44, 41, 87, - 42, 45, 46, 47, 48, 41, 87, 42, - 50, 52, 53, 56, 61, 65, 69, 51, - 88, 88, 54, 53, 51, 54, 55, 57, - 58, 59, 60, 88, 62, 63, 64, 88, - 66, 67, 68, 88, 70, 71, 77, 70, - 71, 77, 72, 72, 73, 74, 75, 76, - 77, 75, 76, 88, 78, 78, 37, 38 + 1, 0, 2, 71, 3, 6, 10, 13, + 16, 70, 4, 3, 70, 4, 5, 7, + 8, 9, 72, 11, 12, 73, 14, 15, + 74, 17, 18, 75, 17, 18, 75, 19, + 19, 20, 21, 22, 23, 75, 22, 23, + 25, 26, 32, 76, 27, 29, 28, 30, + 31, 34, 77, 35, 34, 77, 35, 33, + 36, 37, 38, 39, 40, 34, 77, 35, + 42, 44, 45, 48, 53, 57, 61, 43, + 78, 78, 46, 45, 43, 46, 47, 49, + 50, 51, 52, 78, 54, 55, 56, 78, + 58, 59, 60, 78, 62, 63, 69, 62, + 63, 69, 64, 64, 65, 66, 67, 68, + 69, 67, 68, 78, 70, 70, 70, 70, + 70, 70 }; static const char _json_trans_actions[] = { - 0, 0, 43, 37, 29, 25, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 21, - 104, 77, 0, 71, 23, 17, 0, 0, - 15, 19, 19, 74, 0, 0, 0, 0, - 0, 1, 0, 0, 0, 0, 0, 3, - 13, 0, 0, 59, 5, 11, 0, 62, - 7, 7, 7, 65, 68, 9, 33, 33, - 33, 0, 0, 0, 0, 0, 80, 39, - 39, 0, 5, 5, 11, 0, 3, 0, - 0, 62, 7, 7, 7, 65, 9, 9, - 89, 83, 45, 53, 0, 51, 55, 49, - 57, 86, 15, 0, 47, 0, 0, 0, - 0, 0, 0, 95, 0, 0, 0, 98, - 0, 0, 0, 92, 21, 104, 77, 0, - 71, 23, 17, 0, 0, 15, 19, 19, - 74, 0, 0, 101, 31, 27, 0, 0 + 0, 0, 78, 72, 27, 39, 0, 35, + 45, 33, 17, 0, 29, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 23, 93, 69, 0, 63, 25, 19, + 0, 0, 17, 21, 21, 66, 0, 0, + 0, 0, 0, 3, 0, 0, 0, 0, + 0, 5, 15, 0, 0, 51, 7, 13, + 0, 54, 9, 9, 9, 57, 60, 11, + 78, 72, 27, 39, 0, 35, 45, 33, + 49, 75, 17, 0, 29, 0, 0, 0, + 0, 0, 0, 84, 0, 0, 0, 87, + 0, 0, 0, 81, 23, 93, 69, 0, + 63, 25, 19, 0, 0, 17, 21, 21, + 66, 0, 0, 90, 0, 31, 41, 43, + 37, 47 }; static const char _json_eof_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 35, 35, 35, 35, - 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, - 27, 0, 0, 35, 35, 35, 35, 41, - 0 + 41, 43, 37, 47, 0, 0, 0 }; static const int json_start = 1; -static const int json_en_number_machine = 18; -static const int json_en_string_machine = 27; -static const int json_en_number_object_machine = 35; -static const int json_en_string_object_machine = 40; -static const int json_en_value_machine = 49; +static const int json_en_number_machine = 24; +static const int json_en_string_machine = 33; +static const int json_en_value_machine = 41; static const int json_en_main = 1; -#line 1584 "upb/json/parser.rl" +#line 1565 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -1616,7 +1604,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1620 "upb/json/parser.c" +#line 1608 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -1690,227 +1678,201 @@ _match: { switch ( *_acts++ ) { - case 0: -#line 1383 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; case 1: -#line 1385 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 18; goto _again;} } +#line 1400 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1389 "upb/json/parser.rl" - { start_text(parser, p); } +#line 1402 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 24; goto _again;} } break; case 3: -#line 1390 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_text(parser, p)); } +#line 1406 "upb/json/parser.rl" + { start_text(parser, p); } break; case 4: -#line 1396 "upb/json/parser.rl" - { start_hex(parser); } +#line 1407 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 1397 "upb/json/parser.rl" - { hexdigit(parser, p); } +#line 1413 "upb/json/parser.rl" + { start_hex(parser); } break; case 6: -#line 1398 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_hex(parser)); } +#line 1414 "upb/json/parser.rl" + { hexdigit(parser, p); } break; case 7: -#line 1404 "upb/json/parser.rl" - { CHECK_RETURN_TOP(escape(parser, p)); } +#line 1415 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 1410 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } +#line 1421 "upb/json/parser.rl" + { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 1413 "upb/json/parser.rl" - { {stack[top++] = cs; cs = 27; goto _again;} } +#line 1427 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 1415 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 49; goto _again;} } +#line 1430 "upb/json/parser.rl" + { {stack[top++] = cs; cs = 33; goto _again;} } break; case 11: -#line 1420 "upb/json/parser.rl" - { start_member(parser); } +#line 1432 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 41; goto _again;} } break; case 12: -#line 1421 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_membername(parser)); } +#line 1437 "upb/json/parser.rl" + { start_member(parser); } break; case 13: -#line 1424 "upb/json/parser.rl" - { end_member(parser); } +#line 1438 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_membername(parser)); } break; case 14: -#line 1430 "upb/json/parser.rl" - { start_object(parser); } +#line 1441 "upb/json/parser.rl" + { end_member(parser); } break; case 15: -#line 1433 "upb/json/parser.rl" - { end_object(parser); } +#line 1447 "upb/json/parser.rl" + { start_object(parser); } break; case 16: -#line 1438 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } - break; - case 17: -#line 1442 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(parser_putbool(parser, true)); - end_wrapper_object(parser); - } - break; - case 18: #line 1450 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } - break; - case 19: -#line 1454 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(parser_putbool(parser, false)); - end_wrapper_object(parser); - } - break; - case 20: -#line 1462 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(is_number_wrapper_object(parser)); - start_wrapper_object(parser); - start_number(parser, p); - } - break; - case 22: -#line 1475 "upb/json/parser.rl" - { - p--; {stack[top++] = cs; cs = 35; goto _again;} - } - break; - case 23: -#line 1482 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(is_string_wrapper_object(parser)); - start_wrapper_object(parser); - CHECK_RETURN_TOP(start_stringval(parser)); - } - break; - case 25: -#line 1495 "upb/json/parser.rl" - { {stack[top++] = cs; cs = 40; goto _again;} } + { end_object(parser); } break; - case 26: -#line 1500 "upb/json/parser.rl" + case 17: +#line 1456 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; - case 27: -#line 1504 "upb/json/parser.rl" + case 18: +#line 1460 "upb/json/parser.rl" { end_array(parser); } break; - case 28: -#line 1509 "upb/json/parser.rl" + case 19: +#line 1465 "upb/json/parser.rl" { - if (does_number_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_number_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_number_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } start_number(parser, p); } break; - case 29: -#line 1516 "upb/json/parser.rl" + case 20: +#line 1475 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); if (does_number_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } break; - case 30: -#line 1524 "upb/json/parser.rl" + case 21: +#line 1485 "upb/json/parser.rl" { - if (does_string_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_string_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_string_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } CHECK_RETURN_TOP(start_stringval(parser)); } break; - case 31: -#line 1531 "upb/json/parser.rl" + case 22: +#line 1495 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); if (does_string_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } break; - case 32: -#line 1539 "upb/json/parser.rl" + case 23: +#line 1505 "upb/json/parser.rl" { - if (does_boolean_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_boolean_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } } break; - case 33: -#line 1545 "upb/json/parser.rl" + case 24: +#line 1514 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, true)); if (does_boolean_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } break; - case 34: -#line 1553 "upb/json/parser.rl" + case 25: +#line 1524 "upb/json/parser.rl" { - if (does_boolean_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_boolean_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } } break; - case 35: -#line 1559 "upb/json/parser.rl" + case 26: +#line 1533 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, false)); if (does_boolean_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } break; - case 36: -#line 1567 "upb/json/parser.rl" + case 27: +#line 1543 "upb/json/parser.rl" { /* null value */ } break; - case 37: -#line 1569 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_subobject(parser)); } + case 28: +#line 1545 "upb/json/parser.rl" + { + if (!is_top_level(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + } + } break; - case 38: -#line 1570 "upb/json/parser.rl" - { end_subobject(parser); } + case 29: +#line 1550 "upb/json/parser.rl" + { + if (!is_top_level(parser)) { + end_subobject(parser); + } + } break; - case 39: -#line 1575 "upb/json/parser.rl" + case 30: +#line 1559 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1914 "upb/json/parser.c" +#line 1876 "upb/json/parser.c" } } @@ -1926,37 +1888,63 @@ _again: unsigned int __nacts = (unsigned int) *__acts++; while ( __nacts-- > 0 ) { switch ( *__acts++ ) { - case 17: -#line 1442 "upb/json/parser.rl" + case 0: +#line 1394 "upb/json/parser.rl" + { + if (parser->ready_to_end) { + p--; {cs = stack[--top]; goto _again;} + } + } + break; + case 20: +#line 1475 "upb/json/parser.rl" + { + CHECK_RETURN_TOP(end_number(parser, p)); + if (does_number_wrapper_end(parser)) { + end_wrapper_object(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } + } + } + break; + case 24: +#line 1514 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, true)); - end_wrapper_object(parser); + if (does_boolean_wrapper_end(parser)) { + end_wrapper_object(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } + } } break; - case 19: -#line 1454 "upb/json/parser.rl" + case 26: +#line 1533 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, false)); - end_wrapper_object(parser); + if (does_boolean_wrapper_end(parser)) { + end_wrapper_object(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } + } } break; - case 21: -#line 1467 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(end_number(parser, p)); - end_wrapper_object(parser); - p--; {cs = stack[--top]; goto _again;} - } + case 27: +#line 1543 "upb/json/parser.rl" + { /* null value */ } break; - case 24: -#line 1488 "upb/json/parser.rl" + case 29: +#line 1550 "upb/json/parser.rl" { - CHECK_RETURN_TOP(end_stringval(parser)); - end_wrapper_object(parser); - p--; {cs = stack[--top]; goto _again;} - } + if (!is_top_level(parser)) { + end_subobject(parser); + } + } break; -#line 1960 "upb/json/parser.c" +#line 1948 "upb/json/parser.c" } } } @@ -1964,7 +1952,7 @@ _again: _out: {} } -#line 1606 "upb/json/parser.rl" +#line 1587 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -1982,18 +1970,21 @@ error: } bool end(void *closure, const void *hd) { - UPB_UNUSED(closure); - UPB_UNUSED(hd); + upb_json_parser *parser = closure; /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); - UPB_UNUSED(json_en_number_machine); - UPB_UNUSED(json_en_number_object_machine); - UPB_UNUSED(json_en_string_machine); - UPB_UNUSED(json_en_string_object_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); - return true; + + parser->ready_to_end = true; + parse(parser, hd, NULL, 0, NULL); + + return parser->current_state >= +#line 1985 "upb/json/parser.c" +70 +#line 1614 "upb/json/parser.rl" +; } static void json_parser_reset(upb_json_parser *p) { @@ -2007,13 +1998,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 2011 "upb/json/parser.c" +#line 2002 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1648 "upb/json/parser.rl" +#line 1628 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -2021,6 +2012,7 @@ static void json_parser_reset(upb_json_parser *p) { p->capture = NULL; p->accumulated = NULL; upb_status_clear(&p->status); + p->parsing_number = false; } static void visit_json_parsermethod(const upb_refcounted *r, @@ -2121,6 +2113,7 @@ upb_json_parser *upb_json_parser_create(upb_env *env, set_name_table(p, p->top); p->ignore_json_unknown = ignore_json_unknown; + p->ready_to_end = false; /* If this fails, uncomment and increase the value in parser.h. */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 389a09a869..9e60c5de0b 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -107,6 +107,12 @@ struct upb_json_parser { /* Whether to proceed if unknown field is met. */ bool ignore_json_unknown; + + /* Whether to end parsing. */ + bool ready_to_end; + + /* Indicate whehter last paring ended while parsing number */ + bool parsing_number; }; struct upb_json_parsermethod { @@ -616,6 +622,7 @@ static bool end_text(upb_json_parser *p, const char *ptr) { } static void start_number(upb_json_parser *p, const char *ptr) { + p->parsing_number = true; multipart_startaccum(p); capture_begin(p, ptr); } @@ -623,6 +630,7 @@ static void start_number(upb_json_parser *p, const char *ptr) { static bool parse_number(upb_json_parser *p, bool is_quoted); static bool end_number(upb_json_parser *p, const char *ptr) { + p->parsing_number = false; if (!capture_end(p, ptr)) { return false; } @@ -1306,6 +1314,10 @@ static void end_wrapper_object(upb_json_parser *p) { end_object(p); } +static bool is_top_level(upb_json_parser *p) { + return p->top == p->stack && p->top->f == NULL; +} + static bool does_number_wrapper_start(upb_json_parser *p) { return p->top->f != NULL && upb_fielddef_issubmsg(p->top->f) && @@ -1379,6 +1391,11 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { number_machine := ("-"? integer decimal? exponent?) + %/{ + if (parser->ready_to_end) { + fhold; fret; + } + } <: any >{ fhold; fret; } ; @@ -1433,67 +1450,6 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { >{ end_object(parser); } ; - true_object = - "true" - >{ - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } - %{ - CHECK_RETURN_TOP(parser_putbool(parser, true)); - end_wrapper_object(parser); - } - ; - - false_object = - "false" - >{ - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } - %{ - CHECK_RETURN_TOP(parser_putbool(parser, false)); - end_wrapper_object(parser); - } - ; - - number_object_machine := - ("-"? integer decimal? exponent?) - >{ - CHECK_RETURN_TOP(is_number_wrapper_object(parser)); - start_wrapper_object(parser); - start_number(parser, p); - } - { - fhold; fcall number_object_machine; - } - ; - - string_object_machine := - (text | unicode_char | escape_char)** - >{ - CHECK_RETURN_TOP(is_string_wrapper_object(parser)); - start_wrapper_object(parser); - CHECK_RETURN_TOP(start_stringval(parser)); - } - '"' - { - if (does_number_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_number_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_number_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } @@ -1517,12 +1476,17 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { CHECK_RETURN_TOP(end_number(parser, p)); if (does_number_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } | string >{ - if (does_string_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_string_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_string_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } @@ -1532,12 +1496,17 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { CHECK_RETURN_TOP(end_stringval(parser)); if (does_string_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } | "true" >{ - if (does_boolean_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_boolean_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } @@ -1546,12 +1515,17 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { CHECK_RETURN_TOP(parser_putbool(parser, true)); if (does_boolean_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } | "false" >{ - if (does_boolean_wrapper_start(parser)) { + if (is_top_level(parser)) { + CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); + start_wrapper_object(parser); + } else if (does_boolean_wrapper_start(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); start_wrapper_object(parser); } @@ -1560,24 +1534,31 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { CHECK_RETURN_TOP(parser_putbool(parser, false)); if (does_boolean_wrapper_end(parser)) { end_wrapper_object(parser); - end_subobject(parser); + if (!is_top_level(parser)) { + end_subobject(parser); + } } } | "null" %{ /* null value */ } | object - >{ CHECK_RETURN_TOP(start_subobject(parser)); } - %{ end_subobject(parser); } + >{ + if (!is_top_level(parser)) { + CHECK_RETURN_TOP(start_subobject(parser)); + } + } + %{ + if (!is_top_level(parser)) { + end_subobject(parser); + } + } | array; value_machine := value <: any >{ fhold; fret; } ; - main := - ws - (object | number_object | true_object | false_object | string_object) - ws; + main := ws value ws; }%% %% write data noerror nofinal; @@ -1620,18 +1601,17 @@ error: } bool end(void *closure, const void *hd) { - UPB_UNUSED(closure); - UPB_UNUSED(hd); + upb_json_parser *parser = closure; /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); - UPB_UNUSED(json_en_number_machine); - UPB_UNUSED(json_en_number_object_machine); - UPB_UNUSED(json_en_string_machine); - UPB_UNUSED(json_en_string_object_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); - return true; + + parser->ready_to_end = true; + parse(parser, hd, NULL, 0, NULL); + + return parser->current_state >= %%{ write first_final; }%%; } static void json_parser_reset(upb_json_parser *p) { @@ -1652,6 +1632,7 @@ static void json_parser_reset(upb_json_parser *p) { p->capture = NULL; p->accumulated = NULL; upb_status_clear(&p->status); + p->parsing_number = false; } static void visit_json_parsermethod(const upb_refcounted *r, @@ -1752,6 +1733,7 @@ upb_json_parser *upb_json_parser_create(upb_env *env, set_name_table(p, p->top); p->ignore_json_unknown = ignore_json_unknown; + p->ready_to_end = false; /* If this fails, uncomment and increase the value in parser.h. */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ From f1d7570f305c3cc7975a87edc1313f784fc06b2b Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Sun, 5 Aug 2018 23:24:48 +0000 Subject: [PATCH 6/8] Remove unused code --- upb/json/parser.c | 102 ++++++++++++++++++++++----------------------- upb/json/parser.rl | 8 +--- 2 files changed, 51 insertions(+), 59 deletions(-) diff --git a/upb/json/parser.c b/upb/json/parser.c index 7665e1f103..0f8fdfda47 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -112,9 +112,6 @@ struct upb_json_parser { /* Whether to end parsing. */ bool ready_to_end; - - /* Indicate whehter last paring ended while parsing number */ - bool parsing_number; }; struct upb_json_parsermethod { @@ -624,7 +621,6 @@ static bool end_text(upb_json_parser *p, const char *ptr) { } static void start_number(upb_json_parser *p, const char *ptr) { - p->parsing_number = true; multipart_startaccum(p); capture_begin(p, ptr); } @@ -632,7 +628,6 @@ static void start_number(upb_json_parser *p, const char *ptr) { static bool parse_number(upb_json_parser *p, bool is_quoted); static bool end_number(upb_json_parser *p, const char *ptr) { - p->parsing_number = false; if (!capture_end(p, ptr)) { return false; } @@ -1383,11 +1378,11 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1562 "upb/json/parser.rl" +#line 1557 "upb/json/parser.rl" -#line 1391 "upb/json/parser.c" +#line 1386 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -1581,7 +1576,7 @@ static const int json_en_value_machine = 41; static const int json_en_main = 1; -#line 1565 "upb/json/parser.rl" +#line 1560 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -1604,7 +1599,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1608 "upb/json/parser.c" +#line 1603 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -1679,79 +1674,79 @@ _match: switch ( *_acts++ ) { case 1: -#line 1400 "upb/json/parser.rl" +#line 1395 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1402 "upb/json/parser.rl" +#line 1397 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 24; goto _again;} } break; case 3: -#line 1406 "upb/json/parser.rl" +#line 1401 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 1407 "upb/json/parser.rl" +#line 1402 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 1413 "upb/json/parser.rl" +#line 1408 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 1414 "upb/json/parser.rl" +#line 1409 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 1415 "upb/json/parser.rl" +#line 1410 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 1421 "upb/json/parser.rl" +#line 1416 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 1427 "upb/json/parser.rl" +#line 1422 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 1430 "upb/json/parser.rl" +#line 1425 "upb/json/parser.rl" { {stack[top++] = cs; cs = 33; goto _again;} } break; case 11: -#line 1432 "upb/json/parser.rl" +#line 1427 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 41; goto _again;} } break; case 12: -#line 1437 "upb/json/parser.rl" +#line 1432 "upb/json/parser.rl" { start_member(parser); } break; case 13: -#line 1438 "upb/json/parser.rl" +#line 1433 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 14: -#line 1441 "upb/json/parser.rl" +#line 1436 "upb/json/parser.rl" { end_member(parser); } break; case 15: -#line 1447 "upb/json/parser.rl" +#line 1442 "upb/json/parser.rl" { start_object(parser); } break; case 16: -#line 1450 "upb/json/parser.rl" +#line 1445 "upb/json/parser.rl" { end_object(parser); } break; case 17: -#line 1456 "upb/json/parser.rl" +#line 1451 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 18: -#line 1460 "upb/json/parser.rl" +#line 1455 "upb/json/parser.rl" { end_array(parser); } break; case 19: -#line 1465 "upb/json/parser.rl" +#line 1460 "upb/json/parser.rl" { if (is_top_level(parser)) { CHECK_RETURN_TOP(is_number_wrapper_object(parser)); @@ -1764,7 +1759,7 @@ _match: } break; case 20: -#line 1475 "upb/json/parser.rl" +#line 1470 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); if (does_number_wrapper_end(parser)) { @@ -1776,7 +1771,7 @@ _match: } break; case 21: -#line 1485 "upb/json/parser.rl" +#line 1480 "upb/json/parser.rl" { if (is_top_level(parser)) { CHECK_RETURN_TOP(is_string_wrapper_object(parser)); @@ -1789,7 +1784,7 @@ _match: } break; case 22: -#line 1495 "upb/json/parser.rl" +#line 1490 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); if (does_string_wrapper_end(parser)) { @@ -1801,7 +1796,7 @@ _match: } break; case 23: -#line 1505 "upb/json/parser.rl" +#line 1500 "upb/json/parser.rl" { if (is_top_level(parser)) { CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); @@ -1813,7 +1808,7 @@ _match: } break; case 24: -#line 1514 "upb/json/parser.rl" +#line 1509 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, true)); if (does_boolean_wrapper_end(parser)) { @@ -1825,7 +1820,7 @@ _match: } break; case 25: -#line 1524 "upb/json/parser.rl" +#line 1519 "upb/json/parser.rl" { if (is_top_level(parser)) { CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); @@ -1837,7 +1832,7 @@ _match: } break; case 26: -#line 1533 "upb/json/parser.rl" +#line 1528 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, false)); if (does_boolean_wrapper_end(parser)) { @@ -1849,11 +1844,11 @@ _match: } break; case 27: -#line 1543 "upb/json/parser.rl" +#line 1538 "upb/json/parser.rl" { /* null value */ } break; case 28: -#line 1545 "upb/json/parser.rl" +#line 1540 "upb/json/parser.rl" { if (!is_top_level(parser)) { CHECK_RETURN_TOP(start_subobject(parser)); @@ -1861,7 +1856,7 @@ _match: } break; case 29: -#line 1550 "upb/json/parser.rl" +#line 1545 "upb/json/parser.rl" { if (!is_top_level(parser)) { end_subobject(parser); @@ -1869,10 +1864,10 @@ _match: } break; case 30: -#line 1559 "upb/json/parser.rl" +#line 1554 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1876 "upb/json/parser.c" +#line 1871 "upb/json/parser.c" } } @@ -1889,7 +1884,7 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 1394 "upb/json/parser.rl" +#line 1389 "upb/json/parser.rl" { if (parser->ready_to_end) { p--; {cs = stack[--top]; goto _again;} @@ -1897,7 +1892,7 @@ _again: } break; case 20: -#line 1475 "upb/json/parser.rl" +#line 1470 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); if (does_number_wrapper_end(parser)) { @@ -1909,7 +1904,7 @@ _again: } break; case 24: -#line 1514 "upb/json/parser.rl" +#line 1509 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, true)); if (does_boolean_wrapper_end(parser)) { @@ -1921,7 +1916,7 @@ _again: } break; case 26: -#line 1533 "upb/json/parser.rl" +#line 1528 "upb/json/parser.rl" { CHECK_RETURN_TOP(parser_putbool(parser, false)); if (does_boolean_wrapper_end(parser)) { @@ -1933,18 +1928,18 @@ _again: } break; case 27: -#line 1543 "upb/json/parser.rl" +#line 1538 "upb/json/parser.rl" { /* null value */ } break; case 29: -#line 1550 "upb/json/parser.rl" +#line 1545 "upb/json/parser.rl" { if (!is_top_level(parser)) { end_subobject(parser); } } break; -#line 1948 "upb/json/parser.c" +#line 1943 "upb/json/parser.c" } } } @@ -1952,7 +1947,7 @@ _again: _out: {} } -#line 1587 "upb/json/parser.rl" +#line 1582 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -1974,6 +1969,8 @@ bool end(void *closure, const void *hd) { /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); + UPB_UNUSED(json_en_number_machine); + UPB_UNUSED(json_en_string_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); @@ -1981,9 +1978,9 @@ bool end(void *closure, const void *hd) { parse(parser, hd, NULL, 0, NULL); return parser->current_state >= -#line 1985 "upb/json/parser.c" +#line 1982 "upb/json/parser.c" 70 -#line 1614 "upb/json/parser.rl" +#line 1611 "upb/json/parser.rl" ; } @@ -1998,13 +1995,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 2002 "upb/json/parser.c" +#line 1999 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1628 "upb/json/parser.rl" +#line 1625 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -2012,7 +2009,6 @@ static void json_parser_reset(upb_json_parser *p) { p->capture = NULL; p->accumulated = NULL; upb_status_clear(&p->status); - p->parsing_number = false; } static void visit_json_parsermethod(const upb_refcounted *r, diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 9e60c5de0b..368b11ded4 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -110,9 +110,6 @@ struct upb_json_parser { /* Whether to end parsing. */ bool ready_to_end; - - /* Indicate whehter last paring ended while parsing number */ - bool parsing_number; }; struct upb_json_parsermethod { @@ -622,7 +619,6 @@ static bool end_text(upb_json_parser *p, const char *ptr) { } static void start_number(upb_json_parser *p, const char *ptr) { - p->parsing_number = true; multipart_startaccum(p); capture_begin(p, ptr); } @@ -630,7 +626,6 @@ static void start_number(upb_json_parser *p, const char *ptr) { static bool parse_number(upb_json_parser *p, bool is_quoted); static bool end_number(upb_json_parser *p, const char *ptr) { - p->parsing_number = false; if (!capture_end(p, ptr)) { return false; } @@ -1605,6 +1600,8 @@ bool end(void *closure, const void *hd) { /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); + UPB_UNUSED(json_en_number_machine); + UPB_UNUSED(json_en_string_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); @@ -1632,7 +1629,6 @@ static void json_parser_reset(upb_json_parser *p) { p->capture = NULL; p->accumulated = NULL; upb_status_clear(&p->status); - p->parsing_number = false; } static void visit_json_parsermethod(const upb_refcounted *r, From d4caefaade5908ad079e98290c8a850ac981d05a Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Thu, 9 Aug 2018 04:17:06 +0000 Subject: [PATCH 7/8] Move logic away from ragel machine --- upb/json/parser.c | 388 ++++++++++++++++++++++----------------------- upb/json/parser.rl | 209 ++++++++++++++---------- 2 files changed, 316 insertions(+), 281 deletions(-) diff --git a/upb/json/parser.c b/upb/json/parser.c index 0f8fdfda47..a815c0d421 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -42,6 +42,27 @@ static const char *kBoolValueFullMessageName = "google.protobuf.BoolValue"; static const char *kStringValueFullMessageName = "google.protobuf.StringValue"; static const char *kBytesValueFullMessageName = "google.protobuf.BytesValue"; +/* Forward declare */ +static bool is_top_level(upb_json_parser *p); + +static bool is_number_wrapper_object(upb_json_parser *p); +static bool does_number_wrapper_start(upb_json_parser *p); +static bool does_number_wrapper_end(upb_json_parser *p); + +static bool is_string_wrapper_object(upb_json_parser *p); +static bool does_string_wrapper_start(upb_json_parser *p); +static bool does_string_wrapper_end(upb_json_parser *p); + +static bool is_boolean_wrapper_object(upb_json_parser *p); +static bool does_boolean_wrapper_start(upb_json_parser *p); +static bool does_boolean_wrapper_end(upb_json_parser *p); + +static void start_wrapper_object(upb_json_parser *p); +static void end_wrapper_object(upb_json_parser *p); + +static bool start_subobject(upb_json_parser *p); +static void end_subobject(upb_json_parser *p); + typedef struct { upb_sink sink; @@ -620,14 +641,27 @@ static bool end_text(upb_json_parser *p, const char *ptr) { return capture_end(p, ptr); } -static void start_number(upb_json_parser *p, const char *ptr) { +static bool start_number(upb_json_parser *p, const char *ptr) { + if (is_top_level(p)) { + if (!is_number_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_number_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + multipart_startaccum(p); capture_begin(p, ptr); + return true; } static bool parse_number(upb_json_parser *p, bool is_quoted); -static bool end_number(upb_json_parser *p, const char *ptr) { +static bool end_number_nontop(upb_json_parser *p, const char *ptr) { if (!capture_end(p, ptr)) { return false; } @@ -640,6 +674,21 @@ static bool end_number(upb_json_parser *p, const char *ptr) { return parse_number(p, false); } +static bool end_number(upb_json_parser *p, const char *ptr) { + if (!end_number_nontop(p, ptr)) { + return false; + } + + if (does_number_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + /* |buf| is NULL-terminated. |buf| itself will never include quotes; * |is_quoted| tells us whether this text originally appeared inside quotes. */ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, @@ -807,7 +856,46 @@ static bool parser_putbool(upb_json_parser *p, bool val) { return true; } +static bool end_bool(upb_json_parser *p, bool val) { + if (is_top_level(p)) { + if (!is_boolean_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_boolean_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + + if (!parser_putbool(p, val)) { + return false; + } + + if (does_boolean_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + static bool start_stringval(upb_json_parser *p) { + if (is_top_level(p)) { + if (!is_string_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_string_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + if (p->top->f == NULL) { multipart_startaccum(p); return true; @@ -859,7 +947,7 @@ static bool start_stringval(upb_json_parser *p) { } } -static bool end_stringval(upb_json_parser *p) { +static bool end_stringval_nontop(upb_json_parser *p) { bool ok = true; if (p->top->f == NULL) { @@ -926,6 +1014,21 @@ static bool end_stringval(upb_json_parser *p) { return ok; } +static bool end_stringval(upb_json_parser *p) { + if (!end_stringval_nontop(p)) { + return false; + } + + if (does_string_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + static void start_member(upb_json_parser *p) { UPB_ASSERT(!p->top->f); multipart_startaccum(p); @@ -1109,6 +1212,10 @@ static void end_member(upb_json_parser *p) { } static bool start_subobject(upb_json_parser *p) { + if (is_top_level(p)) { + return true; + } + if (p->top->f == NULL) { upb_jsonparser_frame *inner; if (!check_stack(p)) return false; @@ -1172,6 +1279,10 @@ static bool start_subobject(upb_json_parser *p) { } static void end_subobject(upb_json_parser *p) { + if (is_top_level(p)) { + return; + } + if (p->top->is_map) { upb_selector_t sel; p->top--; @@ -1378,25 +1489,24 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1557 "upb/json/parser.rl" +#line 1592 "upb/json/parser.rl" -#line 1386 "upb/json/parser.c" +#line 1497 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, 9, 1, 11, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 20, 1, 22, 1, 23, 1, 24, 1, - 25, 1, 26, 1, 27, 1, 28, 1, - 29, 1, 30, 2, 4, 9, 2, 5, - 6, 2, 7, 3, 2, 7, 9, 2, - 12, 10, 2, 14, 16, 2, 15, 16, - 2, 19, 2, 2, 20, 30, 2, 21, - 10, 2, 24, 30, 2, 26, 30, 2, - 27, 30, 2, 29, 30, 3, 15, 12, - 10 + 25, 1, 26, 1, 27, 1, 28, 2, + 4, 9, 2, 5, 6, 2, 7, 3, + 2, 7, 9, 2, 12, 10, 2, 14, + 16, 2, 15, 16, 2, 19, 2, 2, + 20, 28, 2, 21, 10, 2, 23, 28, + 2, 24, 28, 2, 25, 28, 2, 27, + 28, 3, 15, 12, 10 }; static const unsigned char _json_key_offsets[] = { @@ -1538,21 +1648,21 @@ static const char _json_trans_targs[] = { }; static const char _json_trans_actions[] = { - 0, 0, 78, 72, 27, 39, 0, 35, - 45, 33, 17, 0, 29, 0, 0, 0, + 0, 0, 74, 68, 27, 0, 0, 0, + 41, 33, 17, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 23, 93, 69, 0, 63, 25, 19, - 0, 0, 17, 21, 21, 66, 0, 0, + 0, 23, 89, 65, 0, 59, 25, 19, + 0, 0, 17, 21, 21, 62, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, - 0, 5, 15, 0, 0, 51, 7, 13, - 0, 54, 9, 9, 9, 57, 60, 11, - 78, 72, 27, 39, 0, 35, 45, 33, - 49, 75, 17, 0, 29, 0, 0, 0, - 0, 0, 0, 84, 0, 0, 0, 87, - 0, 0, 0, 81, 23, 93, 69, 0, - 63, 25, 19, 0, 0, 17, 21, 21, - 66, 0, 0, 90, 0, 31, 41, 43, - 37, 47 + 0, 5, 15, 0, 0, 47, 7, 13, + 0, 50, 9, 9, 9, 53, 56, 11, + 74, 68, 27, 0, 0, 0, 41, 33, + 45, 71, 17, 0, 29, 0, 0, 0, + 0, 0, 0, 80, 0, 0, 0, 83, + 0, 0, 0, 77, 23, 89, 65, 0, + 59, 25, 19, 0, 0, 17, 21, 21, + 62, 0, 0, 86, 0, 31, 37, 39, + 35, 43 }; static const char _json_eof_actions[] = { @@ -1565,7 +1675,7 @@ static const char _json_eof_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, - 41, 43, 37, 47, 0, 0, 0 + 37, 39, 35, 43, 0, 0, 0 }; static const int json_start = 1; @@ -1576,7 +1686,7 @@ static const int json_en_value_machine = 41; static const int json_en_main = 1; -#line 1560 "upb/json/parser.rl" +#line 1595 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -1599,7 +1709,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1603 "upb/json/parser.c" +#line 1713 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -1674,200 +1784,118 @@ _match: switch ( *_acts++ ) { case 1: -#line 1395 "upb/json/parser.rl" +#line 1506 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1397 "upb/json/parser.rl" +#line 1508 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 24; goto _again;} } break; case 3: -#line 1401 "upb/json/parser.rl" +#line 1512 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 1402 "upb/json/parser.rl" +#line 1513 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 1408 "upb/json/parser.rl" +#line 1519 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 1409 "upb/json/parser.rl" +#line 1520 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 1410 "upb/json/parser.rl" +#line 1521 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 1416 "upb/json/parser.rl" +#line 1527 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 1422 "upb/json/parser.rl" +#line 1533 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 1425 "upb/json/parser.rl" +#line 1536 "upb/json/parser.rl" { {stack[top++] = cs; cs = 33; goto _again;} } break; case 11: -#line 1427 "upb/json/parser.rl" +#line 1538 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 41; goto _again;} } break; case 12: -#line 1432 "upb/json/parser.rl" +#line 1543 "upb/json/parser.rl" { start_member(parser); } break; case 13: -#line 1433 "upb/json/parser.rl" +#line 1544 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 14: -#line 1436 "upb/json/parser.rl" +#line 1547 "upb/json/parser.rl" { end_member(parser); } break; case 15: -#line 1442 "upb/json/parser.rl" +#line 1553 "upb/json/parser.rl" { start_object(parser); } break; case 16: -#line 1445 "upb/json/parser.rl" +#line 1556 "upb/json/parser.rl" { end_object(parser); } break; case 17: -#line 1451 "upb/json/parser.rl" +#line 1562 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 18: -#line 1455 "upb/json/parser.rl" +#line 1566 "upb/json/parser.rl" { end_array(parser); } break; case 19: -#line 1460 "upb/json/parser.rl" - { - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_number_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_number_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - start_number(parser, p); - } +#line 1571 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 20: -#line 1470 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(end_number(parser, p)); - if (does_number_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } +#line 1572 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 21: -#line 1480 "upb/json/parser.rl" - { - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_string_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_string_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - CHECK_RETURN_TOP(start_stringval(parser)); - } +#line 1574 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 22: -#line 1490 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(end_stringval(parser)); - if (does_string_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } +#line 1575 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 23: -#line 1500 "upb/json/parser.rl" - { - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_boolean_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - } +#line 1577 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 24: -#line 1509 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(parser_putbool(parser, true)); - if (does_boolean_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } +#line 1579 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 25: -#line 1519 "upb/json/parser.rl" - { - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_boolean_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - } +#line 1581 "upb/json/parser.rl" + { /* null value */ } break; case 26: -#line 1528 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(parser_putbool(parser, false)); - if (does_boolean_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } +#line 1583 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_subobject(parser)); } break; case 27: -#line 1538 "upb/json/parser.rl" - { /* null value */ } +#line 1584 "upb/json/parser.rl" + { end_subobject(parser); } break; case 28: -#line 1540 "upb/json/parser.rl" - { - if (!is_top_level(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - } - } - break; - case 29: -#line 1545 "upb/json/parser.rl" - { - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - break; - case 30: -#line 1554 "upb/json/parser.rl" +#line 1589 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1871 "upb/json/parser.c" +#line 1899 "upb/json/parser.c" } } @@ -1884,7 +1912,7 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 1389 "upb/json/parser.rl" +#line 1500 "upb/json/parser.rl" { if (parser->ready_to_end) { p--; {cs = stack[--top]; goto _again;} @@ -1892,52 +1920,24 @@ _again: } break; case 20: -#line 1470 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(end_number(parser, p)); - if (does_number_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } +#line 1572 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_number(parser, p)); } break; - case 24: -#line 1509 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(parser_putbool(parser, true)); - if (does_boolean_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } + case 23: +#line 1577 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, true)); } break; - case 26: -#line 1528 "upb/json/parser.rl" - { - CHECK_RETURN_TOP(parser_putbool(parser, false)); - if (does_boolean_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } + case 24: +#line 1579 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, false)); } break; - case 27: -#line 1538 "upb/json/parser.rl" + case 25: +#line 1581 "upb/json/parser.rl" { /* null value */ } break; - case 29: -#line 1545 "upb/json/parser.rl" - { - if (!is_top_level(parser)) { - end_subobject(parser); - } - } + case 27: +#line 1584 "upb/json/parser.rl" + { end_subobject(parser); } break; #line 1943 "upb/json/parser.c" } @@ -1947,7 +1947,7 @@ _again: _out: {} } -#line 1582 "upb/json/parser.rl" +#line 1617 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -1980,7 +1980,7 @@ bool end(void *closure, const void *hd) { return parser->current_state >= #line 1982 "upb/json/parser.c" 70 -#line 1611 "upb/json/parser.rl" +#line 1646 "upb/json/parser.rl" ; } @@ -2001,7 +2001,7 @@ static void json_parser_reset(upb_json_parser *p) { top = 0; } -#line 1625 "upb/json/parser.rl" +#line 1660 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 368b11ded4..b1b0b94d65 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -40,6 +40,27 @@ static const char *kBoolValueFullMessageName = "google.protobuf.BoolValue"; static const char *kStringValueFullMessageName = "google.protobuf.StringValue"; static const char *kBytesValueFullMessageName = "google.protobuf.BytesValue"; +/* Forward declare */ +static bool is_top_level(upb_json_parser *p); + +static bool is_number_wrapper_object(upb_json_parser *p); +static bool does_number_wrapper_start(upb_json_parser *p); +static bool does_number_wrapper_end(upb_json_parser *p); + +static bool is_string_wrapper_object(upb_json_parser *p); +static bool does_string_wrapper_start(upb_json_parser *p); +static bool does_string_wrapper_end(upb_json_parser *p); + +static bool is_boolean_wrapper_object(upb_json_parser *p); +static bool does_boolean_wrapper_start(upb_json_parser *p); +static bool does_boolean_wrapper_end(upb_json_parser *p); + +static void start_wrapper_object(upb_json_parser *p); +static void end_wrapper_object(upb_json_parser *p); + +static bool start_subobject(upb_json_parser *p); +static void end_subobject(upb_json_parser *p); + typedef struct { upb_sink sink; @@ -618,14 +639,27 @@ static bool end_text(upb_json_parser *p, const char *ptr) { return capture_end(p, ptr); } -static void start_number(upb_json_parser *p, const char *ptr) { +static bool start_number(upb_json_parser *p, const char *ptr) { + if (is_top_level(p)) { + if (!is_number_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_number_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + multipart_startaccum(p); capture_begin(p, ptr); + return true; } static bool parse_number(upb_json_parser *p, bool is_quoted); -static bool end_number(upb_json_parser *p, const char *ptr) { +static bool end_number_nontop(upb_json_parser *p, const char *ptr) { if (!capture_end(p, ptr)) { return false; } @@ -638,6 +672,21 @@ static bool end_number(upb_json_parser *p, const char *ptr) { return parse_number(p, false); } +static bool end_number(upb_json_parser *p, const char *ptr) { + if (!end_number_nontop(p, ptr)) { + return false; + } + + if (does_number_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + /* |buf| is NULL-terminated. |buf| itself will never include quotes; * |is_quoted| tells us whether this text originally appeared inside quotes. */ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, @@ -805,7 +854,46 @@ static bool parser_putbool(upb_json_parser *p, bool val) { return true; } +static bool end_bool(upb_json_parser *p, bool val) { + if (is_top_level(p)) { + if (!is_boolean_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_boolean_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + + if (!parser_putbool(p, val)) { + return false; + } + + if (does_boolean_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + static bool start_stringval(upb_json_parser *p) { + if (is_top_level(p)) { + if (!is_string_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_string_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + if (p->top->f == NULL) { multipart_startaccum(p); return true; @@ -857,7 +945,7 @@ static bool start_stringval(upb_json_parser *p) { } } -static bool end_stringval(upb_json_parser *p) { +static bool end_stringval_nontop(upb_json_parser *p) { bool ok = true; if (p->top->f == NULL) { @@ -924,6 +1012,21 @@ static bool end_stringval(upb_json_parser *p) { return ok; } +static bool end_stringval(upb_json_parser *p) { + if (!end_stringval_nontop(p)) { + return false; + } + + if (does_string_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + static void start_member(upb_json_parser *p) { UPB_ASSERT(!p->top->f); multipart_startaccum(p); @@ -1107,6 +1210,10 @@ static void end_member(upb_json_parser *p) { } static bool start_subobject(upb_json_parser *p) { + if (is_top_level(p)) { + return true; + } + if (p->top->f == NULL) { upb_jsonparser_frame *inner; if (!check_stack(p)) return false; @@ -1170,6 +1277,10 @@ static bool start_subobject(upb_json_parser *p) { } static void end_subobject(upb_json_parser *p) { + if (is_top_level(p)) { + return; + } + if (p->top->is_map) { upb_selector_t sel; p->top--; @@ -1457,96 +1568,20 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { value = number - >{ - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_number_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_number_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - start_number(parser, p); - } - %{ - CHECK_RETURN_TOP(end_number(parser, p)); - if (does_number_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } + >{ CHECK_RETURN_TOP(start_number(parser, p)); } + %{ CHECK_RETURN_TOP(end_number(parser, p)); } | string - >{ - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_string_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_string_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - CHECK_RETURN_TOP(start_stringval(parser)); - } - @{ - CHECK_RETURN_TOP(end_stringval(parser)); - if (does_string_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } + >{ CHECK_RETURN_TOP(start_stringval(parser)); } + @{ CHECK_RETURN_TOP(end_stringval(parser)); } | "true" - >{ - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_boolean_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - } - %{ - CHECK_RETURN_TOP(parser_putbool(parser, true)); - if (does_boolean_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } + %{ CHECK_RETURN_TOP(end_bool(parser, true)); } | "false" - >{ - if (is_top_level(parser)) { - CHECK_RETURN_TOP(is_boolean_wrapper_object(parser)); - start_wrapper_object(parser); - } else if (does_boolean_wrapper_start(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - start_wrapper_object(parser); - } - } - %{ - CHECK_RETURN_TOP(parser_putbool(parser, false)); - if (does_boolean_wrapper_end(parser)) { - end_wrapper_object(parser); - if (!is_top_level(parser)) { - end_subobject(parser); - } - } - } + %{ CHECK_RETURN_TOP(end_bool(parser, false)); } | "null" %{ /* null value */ } | object - >{ - if (!is_top_level(parser)) { - CHECK_RETURN_TOP(start_subobject(parser)); - } - } - %{ - if (!is_top_level(parser)) { - end_subobject(parser); - } - } + >{ CHECK_RETURN_TOP(start_subobject(parser)); } + %{ end_subobject(parser); } | array; value_machine := From a8d1a5933deef2661ea8aa1cda7540760f873e7c Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Thu, 9 Aug 2018 20:40:23 +0000 Subject: [PATCH 8/8] Use special eof char to mark end of parsing --- upb/json/parser.c | 107 +++++++++++++++++++++------------------------ upb/json/parser.rl | 17 +++---- 2 files changed, 55 insertions(+), 69 deletions(-) diff --git a/upb/json/parser.c b/upb/json/parser.c index a815c0d421..01adb78b71 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -63,6 +63,8 @@ static void end_wrapper_object(upb_json_parser *p); static bool start_subobject(upb_json_parser *p); static void end_subobject(upb_json_parser *p); +static const char eof_ch = 'e'; + typedef struct { upb_sink sink; @@ -130,9 +132,6 @@ struct upb_json_parser { /* Whether to proceed if unknown field is met. */ bool ignore_json_unknown; - - /* Whether to end parsing. */ - bool ready_to_end; }; struct upb_json_parsermethod { @@ -1489,11 +1488,11 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1592 "upb/json/parser.rl" +#line 1587 "upb/json/parser.rl" -#line 1497 "upb/json/parser.c" +#line 1496 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -1686,7 +1685,7 @@ static const int json_en_value_machine = 41; static const int json_en_main = 1; -#line 1595 "upb/json/parser.rl" +#line 1590 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -1699,7 +1698,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, const char *p = buf; const char *pe = buf + size; - const char *eof = pe; + const char *eof = &eof_ch; parser->handle = handle; @@ -1709,7 +1708,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1713 "upb/json/parser.c" +#line 1712 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -1784,118 +1783,118 @@ _match: switch ( *_acts++ ) { case 1: -#line 1506 "upb/json/parser.rl" +#line 1501 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1508 "upb/json/parser.rl" +#line 1503 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 24; goto _again;} } break; case 3: -#line 1512 "upb/json/parser.rl" +#line 1507 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 1513 "upb/json/parser.rl" +#line 1508 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 1519 "upb/json/parser.rl" +#line 1514 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 1520 "upb/json/parser.rl" +#line 1515 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 1521 "upb/json/parser.rl" +#line 1516 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 1527 "upb/json/parser.rl" +#line 1522 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 1533 "upb/json/parser.rl" +#line 1528 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 1536 "upb/json/parser.rl" +#line 1531 "upb/json/parser.rl" { {stack[top++] = cs; cs = 33; goto _again;} } break; case 11: -#line 1538 "upb/json/parser.rl" +#line 1533 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 41; goto _again;} } break; case 12: -#line 1543 "upb/json/parser.rl" +#line 1538 "upb/json/parser.rl" { start_member(parser); } break; case 13: -#line 1544 "upb/json/parser.rl" +#line 1539 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 14: -#line 1547 "upb/json/parser.rl" +#line 1542 "upb/json/parser.rl" { end_member(parser); } break; case 15: -#line 1553 "upb/json/parser.rl" +#line 1548 "upb/json/parser.rl" { start_object(parser); } break; case 16: -#line 1556 "upb/json/parser.rl" +#line 1551 "upb/json/parser.rl" { end_object(parser); } break; case 17: -#line 1562 "upb/json/parser.rl" +#line 1557 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 18: -#line 1566 "upb/json/parser.rl" +#line 1561 "upb/json/parser.rl" { end_array(parser); } break; case 19: -#line 1571 "upb/json/parser.rl" +#line 1566 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 20: -#line 1572 "upb/json/parser.rl" +#line 1567 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 21: -#line 1574 "upb/json/parser.rl" +#line 1569 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 22: -#line 1575 "upb/json/parser.rl" +#line 1570 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 23: -#line 1577 "upb/json/parser.rl" +#line 1572 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 24: -#line 1579 "upb/json/parser.rl" +#line 1574 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 25: -#line 1581 "upb/json/parser.rl" +#line 1576 "upb/json/parser.rl" { /* null value */ } break; case 26: -#line 1583 "upb/json/parser.rl" +#line 1578 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject(parser)); } break; case 27: -#line 1584 "upb/json/parser.rl" +#line 1579 "upb/json/parser.rl" { end_subobject(parser); } break; case 28: -#line 1589 "upb/json/parser.rl" +#line 1584 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1899 "upb/json/parser.c" +#line 1898 "upb/json/parser.c" } } @@ -1912,34 +1911,30 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 1500 "upb/json/parser.rl" - { - if (parser->ready_to_end) { - p--; {cs = stack[--top]; goto _again;} - } - } +#line 1499 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 1572 "upb/json/parser.rl" +#line 1567 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 23: -#line 1577 "upb/json/parser.rl" +#line 1572 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 24: -#line 1579 "upb/json/parser.rl" +#line 1574 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 25: -#line 1581 "upb/json/parser.rl" +#line 1576 "upb/json/parser.rl" { /* null value */ } break; case 27: -#line 1584 "upb/json/parser.rl" +#line 1579 "upb/json/parser.rl" { end_subobject(parser); } break; -#line 1943 "upb/json/parser.c" +#line 1938 "upb/json/parser.c" } } } @@ -1947,7 +1942,7 @@ _again: _out: {} } -#line 1617 "upb/json/parser.rl" +#line 1612 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -1974,13 +1969,12 @@ bool end(void *closure, const void *hd) { UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); - parser->ready_to_end = true; - parse(parser, hd, NULL, 0, NULL); + parse(parser, hd, &eof_ch, 0, NULL); return parser->current_state >= -#line 1982 "upb/json/parser.c" +#line 1976 "upb/json/parser.c" 70 -#line 1646 "upb/json/parser.rl" +#line 1640 "upb/json/parser.rl" ; } @@ -1995,13 +1989,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 1999 "upb/json/parser.c" +#line 1993 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1660 "upb/json/parser.rl" +#line 1654 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -2109,7 +2103,6 @@ upb_json_parser *upb_json_parser_create(upb_env *env, set_name_table(p, p->top); p->ignore_json_unknown = ignore_json_unknown; - p->ready_to_end = false; /* If this fails, uncomment and increase the value in parser.h. */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ diff --git a/upb/json/parser.rl b/upb/json/parser.rl index b1b0b94d65..ae06ad5b0a 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -61,6 +61,8 @@ static void end_wrapper_object(upb_json_parser *p); static bool start_subobject(upb_json_parser *p); static void end_subobject(upb_json_parser *p); +static const char eof_ch = 'e'; + typedef struct { upb_sink sink; @@ -128,9 +130,6 @@ struct upb_json_parser { /* Whether to proceed if unknown field is met. */ bool ignore_json_unknown; - - /* Whether to end parsing. */ - bool ready_to_end; }; struct upb_json_parsermethod { @@ -1497,11 +1496,7 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { number_machine := ("-"? integer decimal? exponent?) - %/{ - if (parser->ready_to_end) { - fhold; fret; - } - } + %/{ fhold; fret; } <: any >{ fhold; fret; } ; @@ -1604,7 +1599,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, const char *p = buf; const char *pe = buf + size; - const char *eof = pe; + const char *eof = &eof_ch; parser->handle = handle; @@ -1640,8 +1635,7 @@ bool end(void *closure, const void *hd) { UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); - parser->ready_to_end = true; - parse(parser, hd, NULL, 0, NULL); + parse(parser, hd, &eof_ch, 0, NULL); return parser->current_state >= %%{ write first_final; }%%; } @@ -1764,7 +1758,6 @@ upb_json_parser *upb_json_parser_create(upb_env *env, set_name_table(p, p->top); p->ignore_json_unknown = ignore_json_unknown; - p->ready_to_end = false; /* If this fails, uncomment and increase the value in parser.h. */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */