Sync from Google-internal development.

Most notably, a JSON parser is now provided.
pull/13171/head
Josh Haberman 10 years ago
parent 177212c82f
commit 39c9a8bd5a
  1. 11
      Makefile
  2. 54
      tests/test_def.c
  3. 26
      upb/bindings/googlepb/proto2.cc
  4. 894
      upb/json/parser.c
  5. 112
      upb/json/parser.h
  6. 623
      upb/json/parser.rl
  7. 63
      upb/json/printer.c
  8. 97
      upb/json/printer.h
  9. 105
      upb/json/typed_printer.h
  10. 859
      upb/pb/compile_decoder_x64.h
  11. 2
      upb/sink.h
  12. 5
      upb/symtab.c

@ -21,7 +21,7 @@
# Prevents the deletion of intermediate files.
.SECONDARY:
UPB_MODULES = upb upb.pb upb.descriptor
UPB_MODULES = upb upb.pb upb.json upb.descriptor
UPB_LIBS = $(patsubst %,lib/lib%.a,$(UPB_MODULES))
UPB_PICLIBS = $(patsubst %,lib/lib%_pic.a,$(UPB_MODULES))
@ -155,7 +155,14 @@ upb/pb/compile_decoder_x64.h: upb/pb/compile_decoder_x64.dasc
endif
upb_json_SRCS = \
upb/json/typed_printer.c
upb/json/parser.c \
# disabled until we move off YAJL (which should be soon).
#upb/json/printer.c \
upb/json/parser.c: upb/json/parser.rl
$(E) RAGEL $<
$(Q) ragel -C -o upb/json/parser.c upb/json/parser.rl
# If the user doesn't specify an -O setting, we use -O3 for critical-path
# code and -Os for the rest.

@ -73,6 +73,8 @@ static void test_cycles() {
const upb_fielddef *f = upb_msgdef_itof(m, 1);
ASSERT(f);
ASSERT(upb_fielddef_hassubdef(f));
ASSERT(upb_msgdef_ntofz(m, "b") == f);
ASSERT(upb_msgdef_ntof(m, "b", 1) == f);
const upb_def *def2 = upb_fielddef_subdef(f);
ASSERT(upb_downcast_msgdef(def2));
ASSERT(strcmp(upb_def_fullname(def2), "B") == 0);
@ -85,6 +87,57 @@ static void test_cycles() {
upb_def_unref(def2, &def2);
}
static void test_symbol_resolution() {
upb_status s = UPB_STATUS_INIT;
upb_symtab *symtab = upb_symtab_new(&symtab);
ASSERT(symtab);
// m1 has name "A.B.C" and no fields. We'll add it to the symtab now.
upb_msgdef *m1 = upb_msgdef_new(&m1);
ASSERT(m1);
ASSERT_STATUS(upb_msgdef_setfullname(m1, "A.B.C", &s), &s);
ASSERT_STATUS(upb_symtab_add(symtab, (upb_def**)&m1, 1,
NULL, &s), &s);
// m2 has name "D.E" and no fields. We'll add it in the same batch as m3
// below.
upb_msgdef *m2 = upb_msgdef_new(&m2);
ASSERT(m2);
ASSERT_STATUS(upb_msgdef_setfullname(m2, "D.E", &s), &s);
// m3 has name "F.G" and two fields, of type A.B.C and D.E respectively. We'll
// add it in the same batch as m2 above.
upb_msgdef *m3 = upb_msgdef_new(&m3);
ASSERT(m3);
ASSERT_STATUS(upb_msgdef_setfullname(m3, "F.G", &s), &s);
upb_fielddef *m3_field1 = upb_fielddef_new(&m3_field1);
ASSERT_STATUS(upb_fielddef_setname(m3_field1, "field1", &s), &s);
ASSERT_STATUS(upb_fielddef_setnumber(m3_field1, 1, &s), &s);
upb_fielddef_setlabel(m3_field1, UPB_LABEL_OPTIONAL);
upb_fielddef_settype(m3_field1, UPB_TYPE_MESSAGE);
ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field1, ".A.B.C", &s), &s);
ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field1, NULL, &s), &s);
upb_fielddef *m3_field2 = upb_fielddef_new(&m3_field2);
ASSERT_STATUS(upb_fielddef_setname(m3_field2, "field2", &s), &s);
ASSERT_STATUS(upb_fielddef_setnumber(m3_field2, 2, &s), &s);
upb_fielddef_setlabel(m3_field2, UPB_LABEL_OPTIONAL);
upb_fielddef_settype(m3_field2, UPB_TYPE_MESSAGE);
ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field2, ".D.E", &s), &s);
ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field2, NULL, &s), &s);
upb_def *defs[2] = { (upb_def*)m2, (upb_def*)m3 };
ASSERT_STATUS(upb_symtab_add(symtab, defs, 2, NULL, &s), &s);
upb_fielddef_unref(m3_field2, &m3_field2);
upb_fielddef_unref(m3_field1, &m3_field1);
upb_msgdef_unref(m3, &m3);
upb_msgdef_unref(m2, &m2);
upb_msgdef_unref(m1, &m1);
upb_symtab_unref(symtab, &symtab);
}
static void test_fielddef_unref() {
upb_symtab *s = load_test_proto(&s);
const upb_msgdef *md = upb_symtab_lookupmsg(s, "A");
@ -283,6 +336,7 @@ int run_tests(int argc, char *argv[]) {
descriptor_file = argv[1];
test_empty_symtab();
test_cycles();
test_symbol_resolution();
test_fielddef_accessors();
test_fielddef_unref();
test_replacement();

@ -978,32 +978,32 @@ case goog::FieldDescriptor::cpptype: \
}
};
#ifdef GOOGLE_PROTOBUF_HAS_ARENAS
// Closure is a RepeatedPtrField<SubMessageType>*, but we access it through
// its base class RepeatedPtrFieldBase*.
static goog::Message* StartRepeatedSubMessage(
goog::internal::RepeatedPtrFieldBase* r,
const SubMessageHandlerData* data) {
goog::Message* submsg = data->prototype()->New(r->GetArenaNoVirtual());
r->AddAllocated<RepeatedMessageTypeHandler>(submsg);
return submsg;
}
#else // ifdef GOOGLE_PROTOBUF_HAS_ARENAS
// Closure is a RepeatedPtrField<SubMessageType>*, but we access it through
// its base class RepeatedPtrFieldBase*.
static goog::Message* StartRepeatedSubMessage(
goog::internal::RepeatedPtrFieldBase* r,
const SubMessageHandlerData* data) {
#ifdef GOOGLE_PROTOBUF_HAS_ARENAS
return r->Add<RepeatedMessageTypeHandler>(
const_cast<goog::Message*>(data->prototype()));
#else
// This code path is required not because of arena-related API changes but
// because the variant of Add<>() that takes a prototype object was added
// only recently. Without the prototype, there's no way for Add<>() to
// create a new submessage with out typehandler implementation because we
// don't have New() (because we don't template-specialize our typehandler
// class on concrete message types). So we have to implement the runtime
// polymorphism externally (in this function) and then use AddAllocated to
// insert the pointer.
goog::Message* submsg = r->AddFromCleared<RepeatedMessageTypeHandler>();
if (!submsg) {
submsg = data->prototype()->New();
r->AddAllocated<RepeatedMessageTypeHandler>(submsg);
}
return submsg;
#endif
}
#endif // ifdef GOOGLE_PROTOBUF_HAS_ARENAS
#ifdef GOOGLE_PROTOBUF_HAS_ONEOF
static goog::Message* StartOneofSubMessage(
goog::Message* m, const OneofSubMessageHandlerData* data) {

@ -0,0 +1,894 @@
#line 1 "upb/json/parser.rl"
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2014 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* A parser that uses the Ragel State Machine Compiler to generate
* the finite automata.
*
* Ragel only natively handles regular languages, but we can manually
* program it a bit to handle context-free languages like JSON, by using
* the "fcall" and "fret" constructs.
*
* This parser can handle the basics, but needs several things to be fleshed
* out:
*
* - handling of unicode escape sequences (including high surrogate pairs).
* - properly check and report errors for unknown fields, stack overflow,
* improper array nesting (or lack of nesting).
* - handling of base64 sequences with padding characters.
* - handling of push-back (non-success returns from sink functions).
* - handling of keys/escape-sequences/etc that span input buffers.
*/
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "upb/json/parser.h"
#define CHECK_RETURN(x) if (!(x)) return false
static upb_selector_t getsel_for_handlertype(upb_json_parser *p,
upb_handlertype_t type) {
upb_selector_t sel;
bool ok = upb_handlers_getselector(p->top->f, type, &sel);
UPB_ASSERT_VAR(ok, ok);
return sel;
}
static upb_selector_t getsel(upb_json_parser *p) {
return getsel_for_handlertype(
p, upb_handlers_getprimitivehandlertype(p->top->f));
}
static void start_member(upb_json_parser *p) {
assert(!p->top->f);
assert(!p->accumulated);
p->accumulated_len = 0;
}
static bool end_member(upb_json_parser *p) {
// TODO(haberman): support keys that span buffers or have escape sequences.
assert(!p->top->f);
assert(p->accumulated);
const upb_fielddef *f =
upb_msgdef_ntof(p->top->m, p->accumulated, p->accumulated_len);
if (!f) {
// TODO(haberman): Ignore unknown fields if requested/configured to do so.
upb_status_seterrf(p->status, "No such field: %.*s\n",
(int)p->accumulated_len, p->accumulated);
return false;
}
p->top->f = f;
p->accumulated = NULL;
return true;
}
static void start_object(upb_json_parser *p) {
upb_sink_startmsg(&p->top->sink);
}
static void end_object(upb_json_parser *p) {
upb_status status;
upb_sink_endmsg(&p->top->sink, &status);
}
static bool check_stack(upb_json_parser *p) {
if ((p->top + 1) == p->limit) {
upb_status_seterrmsg(p->status, "Nesting too deep");
return false;
}
return true;
}
static bool start_subobject(upb_json_parser *p) {
assert(p->top->f);
if (!upb_fielddef_issubmsg(p->top->f)) {
upb_status_seterrf(p->status,
"Object specified for non-message/group field: %s",
upb_fielddef_name(p->top->f));
return false;
}
if (!check_stack(p)) return false;
upb_jsonparser_frame *inner = p->top + 1;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink);
inner->m = upb_fielddef_msgsubdef(p->top->f);
inner->f = NULL;
p->top = inner;
return true;
}
static void end_subobject(upb_json_parser *p) {
p->top--;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG);
upb_sink_endsubmsg(&p->top->sink, sel);
}
static bool start_array(upb_json_parser *p) {
assert(p->top->f);
if (!upb_fielddef_isseq(p->top->f)) {
upb_status_seterrf(p->status,
"Array specified for non-repeated field: %s",
upb_fielddef_name(p->top->f));
return false;
}
if (!check_stack(p)) return false;
upb_jsonparser_frame *inner = p->top + 1;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
upb_sink_startseq(&p->top->sink, sel, &inner->sink);
inner->m = p->top->m;
inner->f = p->top->f;
p->top = inner;
return true;
}
static void end_array(upb_json_parser *p) {
assert(p->top > p->stack);
p->top--;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
upb_sink_endseq(&p->top->sink, sel);
}
static void clear_member(upb_json_parser *p) { p->top->f = NULL; }
static bool putbool(upb_json_parser *p, bool val) {
if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) {
upb_status_seterrf(p->status,
"Boolean value specified for non-bool field: %s",
upb_fielddef_name(p->top->f));
return false;
}
bool ok = upb_sink_putbool(&p->top->sink, getsel(p), val);
UPB_ASSERT_VAR(ok, ok);
return true;
}
static void start_text(upb_json_parser *p, const char *ptr) {
p->text_begin = ptr;
}
static const signed char b64table[] = {
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */,
52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
-1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1,
-1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1
};
// Returns the table value sign-extended to 32 bits. Knowing that the upper
// bits will be 1 for unrecognized characters makes it easier to check for
// this error condition later (see below).
int32_t b64lookup(unsigned char ch) { return b64table[ch]; }
// Returns true if the given character is not a valid base64 character or
// padding.
bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; }
static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr,
size_t len) {
const char *limit = ptr + len;
for (; ptr < limit; ptr += 4) {
if (limit - ptr < 4) {
upb_status_seterrf(p->status,
"Base64 input for bytes field not a multiple of 4: %s",
upb_fielddef_name(p->top->f));
return false;
}
uint32_t val = b64lookup(ptr[0]) << 18 |
b64lookup(ptr[1]) << 12 |
b64lookup(ptr[2]) << 6 |
b64lookup(ptr[3]);
// Test the upper bit; returns true if any of the characters returned -1.
if (val & 0x80000000) {
goto otherchar;
}
char output[3];
output[0] = val >> 16;
output[1] = (val >> 8) & 0xff;
output[2] = val & 0xff;
upb_sink_putstring(&p->top->sink, sel, output, 3, NULL);
}
return true;
otherchar:
if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) ||
nonbase64(ptr[3]) ) {
upb_status_seterrf(p->status,
"Non-base64 characters in bytes field: %s",
upb_fielddef_name(p->top->f));
return false;
} if (ptr[2] == '=') {
// Last group contains only two input bytes, one output byte.
if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') {
goto badpadding;
}
uint32_t val = b64lookup(ptr[0]) << 18 |
b64lookup(ptr[1]) << 12;
assert(!(val & 0x80000000));
char output = val >> 16;
upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL);
return true;
} else {
// Last group contains only three input bytes, two output bytes.
if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') {
goto badpadding;
}
uint32_t val = b64lookup(ptr[0]) << 18 |
b64lookup(ptr[1]) << 12 |
b64lookup(ptr[2]) << 6;
char output[2];
output[0] = val >> 16;
output[1] = (val >> 8) & 0xff;
upb_sink_putstring(&p->top->sink, sel, output, 2, NULL);
return true;
}
badpadding:
upb_status_seterrf(p->status,
"Incorrect base64 padding for field: %s (%.*s)",
upb_fielddef_name(p->top->f),
4, ptr);
return false;
}
static bool end_text(upb_json_parser *p, const char *ptr) {
assert(!p->accumulated); // TODO: handle this case.
p->accumulated = p->text_begin;
p->accumulated_len = ptr - p->text_begin;
if (p->top->f && upb_fielddef_isstring(p->top->f)) {
// This is a string field (as opposed to a member name).
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
if (upb_fielddef_type(p->top->f) == UPB_TYPE_BYTES) {
CHECK_RETURN(base64_push(p, sel, p->accumulated, p->accumulated_len));
} else {
upb_sink_putstring(&p->top->sink, sel, p->accumulated, p->accumulated_len, NULL);
}
p->accumulated = NULL;
}
return true;
}
static bool start_stringval(upb_json_parser *p) {
assert(p->top->f);
if (!upb_fielddef_isstring(p->top->f)) {
upb_status_seterrf(p->status,
"String specified for non-string field: %s",
upb_fielddef_name(p->top->f));
return false;
}
if (!check_stack(p)) return false;
upb_jsonparser_frame *inner = p->top + 1; // TODO: check for overflow.
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
inner->m = p->top->m;
inner->f = p->top->f;
p->top = inner;
return true;
}
static void end_stringval(upb_json_parser *p) {
p->top--;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
upb_sink_endstr(&p->top->sink, sel);
}
static void start_number(upb_json_parser *p, const char *ptr) {
start_text(p, ptr);
assert(p->accumulated == NULL);
}
static void end_number(upb_json_parser *p, const char *ptr) {
end_text(p, ptr);
const char *myend = p->accumulated + p->accumulated_len;
char *end;
switch (upb_fielddef_type(p->top->f)) {
case UPB_TYPE_ENUM:
case UPB_TYPE_INT32: {
long val = strtol(p->accumulated, &end, 0);
if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putint32(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_INT64: {
long long val = strtoll(p->accumulated, &end, 0);
if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putint64(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_UINT32: {
unsigned long val = strtoul(p->accumulated, &end, 0);
if (val > UINT32_MAX || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putuint32(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_UINT64: {
unsigned long long val = strtoull(p->accumulated, &end, 0);
if (val > UINT64_MAX || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putuint64(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_DOUBLE: {
double val = strtod(p->accumulated, &end);
if (errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putdouble(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_FLOAT: {
float val = strtof(p->accumulated, &end);
if (errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putfloat(&p->top->sink, getsel(p), val);
break;
}
default:
assert(false);
}
p->accumulated = NULL;
}
static char escape_char(char in) {
switch (in) {
case 'r': return '\r';
case 't': return '\t';
case 'n': return '\n';
case 'f': return '\f';
case 'b': return '\b';
case '/': return '/';
case '"': return '"';
case '\\': return '\\';
default:
assert(0);
return 'x';
}
}
static void escape(upb_json_parser *p, const char *ptr) {
char ch = escape_char(*ptr);
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
upb_sink_putstring(&p->top->sink, sel, &ch, 1, NULL);
}
static uint8_t hexdigit(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
} else {
assert(ch >= 'A' && ch <= 'F');
return ch - 'A' + 10;
}
}
static void start_hex(upb_json_parser *p, const char *ptr) {
start_text(p, ptr);
}
static void hex(upb_json_parser *p, const char *end) {
UPB_UNUSED(end);
const char *start = p->text_begin;
assert(end - start == 4);
uint16_t codepoint =
(hexdigit(start[0]) << 12) |
(hexdigit(start[1]) << 8) |
(hexdigit(start[2]) << 4) |
hexdigit(start[3]);
// TODO(haberman): convert to UTF-8 and emit (though if it is a high surrogate
// we have to wait for the next escape to get the full code point).
UPB_UNUSED(codepoint);
}
#define CHECK_RETURN_TOP(x) if (!(x)) goto error
// What follows is the Ragel parser itself. The language is specified in Ragel
// and the actions call our C functions above.
#line 548 "upb/json/parser.rl"
#line 466 "upb/json/parser.c"
static const char _json_actions[] = {
0, 1, 0, 1, 2, 1, 3, 1,
4, 1, 5, 1, 6, 1, 7, 1,
9, 1, 11, 1, 12, 1, 13, 1,
14, 1, 15, 1, 16, 1, 24, 1,
26, 2, 3, 7, 2, 5, 2, 2,
5, 7, 2, 10, 8, 2, 12, 14,
2, 13, 14, 2, 17, 1, 2, 18,
26, 2, 19, 8, 2, 20, 26, 2,
21, 26, 2, 22, 26, 2, 23, 26,
2, 25, 26, 3, 13, 10, 8
};
static const unsigned char _json_key_offsets[] = {
0, 0, 4, 9, 14, 18, 22, 27,
32, 37, 41, 45, 48, 51, 53, 57,
61, 63, 65, 70, 72, 74, 83, 89,
95, 101, 107, 109, 118, 118, 118, 123,
128, 133, 133, 134, 135, 136, 137, 137,
138, 139, 140, 140, 141, 142, 143, 143,
148, 153, 157, 161, 166, 171, 176, 180,
180, 183, 183, 183
};
static const char _json_trans_keys[] = {
32, 123, 9, 13, 32, 34, 125, 9,
13, 32, 34, 125, 9, 13, 32, 58,
9, 13, 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, 34, 45, 91,
102, 110, 116, 123, 48, 57, 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, 32, 58, 9, 13, 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
};
static const char _json_single_lengths[] = {
0, 2, 3, 3, 2, 2, 3, 3,
3, 2, 2, 1, 3, 0, 2, 2,
0, 0, 3, 2, 2, 9, 0, 0,
0, 0, 2, 7, 0, 0, 3, 3,
3, 0, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1, 1, 1, 0, 3,
3, 2, 2, 3, 3, 3, 2, 0,
1, 0, 0, 0
};
static const char _json_range_lengths[] = {
0, 1, 1, 1, 1, 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, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 0,
1, 0, 0, 0
};
static const short _json_index_offsets[] = {
0, 0, 4, 9, 14, 18, 22, 27,
32, 37, 41, 45, 48, 52, 54, 58,
62, 64, 66, 71, 74, 77, 87, 91,
95, 99, 103, 106, 115, 116, 117, 122,
127, 132, 133, 135, 137, 139, 141, 142,
144, 146, 148, 149, 151, 153, 155, 156,
161, 166, 170, 174, 179, 184, 189, 193,
194, 197, 198, 199
};
static const char _json_indicies[] = {
0, 2, 0, 1, 3, 4, 5, 3,
1, 6, 7, 8, 6, 1, 9, 10,
9, 1, 11, 12, 11, 1, 12, 1,
1, 12, 13, 14, 15, 16, 14, 1,
17, 18, 8, 17, 1, 18, 7, 18,
1, 19, 20, 21, 1, 20, 21, 1,
23, 24, 24, 22, 25, 1, 24, 24,
25, 22, 26, 26, 27, 1, 27, 1,
27, 22, 23, 24, 24, 21, 22, 29,
30, 28, 32, 33, 31, 34, 34, 34,
34, 34, 34, 34, 34, 35, 1, 36,
36, 36, 1, 37, 37, 37, 1, 38,
38, 38, 1, 39, 39, 39, 1, 41,
42, 40, 43, 44, 45, 46, 47, 48,
49, 44, 1, 50, 51, 53, 54, 1,
53, 52, 55, 56, 54, 55, 1, 56,
1, 1, 56, 52, 57, 58, 1, 59,
1, 60, 1, 61, 1, 62, 63, 1,
64, 1, 65, 1, 66, 67, 1, 68,
1, 69, 1, 70, 71, 72, 73, 71,
1, 74, 75, 76, 74, 1, 77, 78,
77, 1, 79, 80, 79, 1, 80, 1,
1, 80, 81, 82, 83, 84, 82, 1,
85, 86, 76, 85, 1, 86, 75, 86,
1, 87, 88, 88, 1, 1, 1, 1,
0
};
static const char _json_trans_targs[] = {
1, 0, 2, 3, 4, 56, 3, 4,
56, 5, 6, 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, 29, 30, 34, 39,
43, 47, 59, 59, 31, 30, 33, 31,
32, 59, 35, 36, 37, 38, 59, 40,
41, 42, 59, 44, 45, 46, 59, 48,
49, 55, 48, 49, 55, 50, 51, 50,
51, 52, 53, 54, 55, 53, 54, 59,
56
};
static const char _json_trans_actions[] = {
0, 0, 0, 21, 75, 48, 0, 42,
23, 17, 17, 0, 0, 15, 19, 19,
45, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 3, 13, 0, 0,
33, 5, 11, 0, 7, 0, 0, 0,
36, 39, 9, 57, 51, 25, 0, 0,
0, 29, 60, 54, 15, 0, 27, 0,
0, 31, 0, 0, 0, 0, 66, 0,
0, 0, 69, 0, 0, 0, 63, 21,
75, 48, 0, 42, 23, 17, 17, 0,
0, 15, 19, 19, 45, 0, 0, 72,
0
};
static const int json_start = 1;
static const int json_first_final = 56;
static const int json_error = 0;
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_main = 1;
#line 551 "upb/json/parser.rl"
size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const upb_bufhandle *handle) {
UPB_UNUSED(hd);
UPB_UNUSED(handle);
upb_json_parser *parser = closure;
// Variables used by Ragel's generated code.
int cs = parser->current_state;
int *stack = parser->parser_stack;
int top = parser->parser_top;
const char *p = buf;
const char *pe = buf + size;
#line 636 "upb/json/parser.c"
{
int _klen;
unsigned int _trans;
const char *_acts;
unsigned int _nacts;
const char *_keys;
if ( p == pe )
goto _test_eof;
if ( cs == 0 )
goto _out;
_resume:
_keys = _json_trans_keys + _json_key_offsets[cs];
_trans = _json_index_offsets[cs];
_klen = _json_single_lengths[cs];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + _klen - 1;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + ((_upper-_lower) >> 1);
if ( (*p) < *_mid )
_upper = _mid - 1;
else if ( (*p) > *_mid )
_lower = _mid + 1;
else {
_trans += (unsigned int)(_mid - _keys);
goto _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = _json_range_lengths[cs];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + (_klen<<1) - 2;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( (*p) < _mid[0] )
_upper = _mid - 2;
else if ( (*p) > _mid[1] )
_lower = _mid + 2;
else {
_trans += (unsigned int)((_mid - _keys)>>1);
goto _match;
}
}
_trans += _klen;
}
_match:
_trans = _json_indicies[_trans];
cs = _json_trans_targs[_trans];
if ( _json_trans_actions[_trans] == 0 )
goto _again;
_acts = _json_actions + _json_trans_actions[_trans];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
{
switch ( *_acts++ )
{
case 0:
#line 469 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 1:
#line 470 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 10; goto _again;} }
break;
case 2:
#line 474 "upb/json/parser.rl"
{ start_text(parser, p); }
break;
case 3:
#line 475 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_text(parser, p)); }
break;
case 4:
#line 481 "upb/json/parser.rl"
{ start_hex(parser, p); }
break;
case 5:
#line 482 "upb/json/parser.rl"
{ hex(parser, p); }
break;
case 6:
#line 488 "upb/json/parser.rl"
{ escape(parser, p); }
break;
case 7:
#line 491 "upb/json/parser.rl"
{ {cs = stack[--top]; goto _again;} }
break;
case 8:
#line 492 "upb/json/parser.rl"
{ {stack[top++] = cs; cs = 19; goto _again;} }
break;
case 9:
#line 494 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 27; goto _again;} }
break;
case 10:
#line 499 "upb/json/parser.rl"
{ start_member(parser); }
break;
case 11:
#line 500 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_member(parser)); }
break;
case 12:
#line 503 "upb/json/parser.rl"
{ clear_member(parser); }
break;
case 13:
#line 509 "upb/json/parser.rl"
{ start_object(parser); }
break;
case 14:
#line 512 "upb/json/parser.rl"
{ end_object(parser); }
break;
case 15:
#line 518 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_array(parser)); }
break;
case 16:
#line 522 "upb/json/parser.rl"
{ end_array(parser); }
break;
case 17:
#line 527 "upb/json/parser.rl"
{ start_number(parser, p); }
break;
case 18:
#line 528 "upb/json/parser.rl"
{ end_number(parser, p); }
break;
case 19:
#line 530 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_stringval(parser)); }
break;
case 20:
#line 531 "upb/json/parser.rl"
{ end_stringval(parser); }
break;
case 21:
#line 533 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(putbool(parser, true)); }
break;
case 22:
#line 535 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(putbool(parser, false)); }
break;
case 23:
#line 537 "upb/json/parser.rl"
{ /* null value */ }
break;
case 24:
#line 539 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_subobject(parser)); }
break;
case 25:
#line 540 "upb/json/parser.rl"
{ end_subobject(parser); }
break;
case 26:
#line 545 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
#line 818 "upb/json/parser.c"
}
}
_again:
if ( cs == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_test_eof: {}
_out: {}
}
#line 567 "upb/json/parser.rl"
if (p != pe) {
upb_status_seterrf(parser->status, "Parse error at %s\n", p);
}
error:
// Save parsing state back to parser.
parser->current_state = cs;
parser->parser_top = top;
return p - buf;
}
bool end(void *closure, const void *hd) {
UPB_UNUSED(closure);
UPB_UNUSED(hd);
return true;
}
void upb_json_parser_init(upb_json_parser *p, upb_status *status) {
p->limit = p->stack + UPB_JSON_MAX_DEPTH;
upb_byteshandler_init(&p->input_handler_);
upb_byteshandler_setstring(&p->input_handler_, parse, NULL);
upb_byteshandler_setendstr(&p->input_handler_, end, NULL);
upb_bytessink_reset(&p->input_, &p->input_handler_, p);
p->status = status;
}
void upb_json_parser_uninit(upb_json_parser *p) {
upb_byteshandler_uninit(&p->input_handler_);
}
void upb_json_parser_reset(upb_json_parser *p) {
p->top = p->stack;
p->top->f = NULL;
int cs;
int top;
// Emit Ragel initialization of the parser.
#line 872 "upb/json/parser.c"
{
cs = json_start;
top = 0;
}
#line 607 "upb/json/parser.rl"
p->current_state = cs;
p->parser_top = top;
p->text_begin = NULL;
p->accumulated = NULL;
p->accumulated_len = 0;
}
void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *sink) {
upb_json_parser_reset(p);
upb_sink_reset(&p->top->sink, sink->handlers, sink->closure);
p->top->m = upb_handlers_msgdef(sink->handlers);
p->accumulated = NULL;
}
upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
return &p->input_;
}

@ -0,0 +1,112 @@
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2014 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* upb::json::Parser can parse JSON according to a specific schema.
* Support for parsing arbitrary JSON (schema-less) will be added later.
*/
#ifndef UPB_JSON_PARSER_H_
#define UPB_JSON_PARSER_H_
#include "upb/sink.h"
#ifdef __cplusplus
namespace upb {
namespace json {
class Parser;
} // namespace json
} // namespace upb
#endif
UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser);
// Internal-only struct used by the parser.
typedef struct {
UPB_PRIVATE_FOR_CPP
upb_sink sink;
const upb_msgdef *m;
const upb_fielddef *f;
} upb_jsonparser_frame;
/* upb::json::Parser **********************************************************/
#define UPB_JSON_MAX_DEPTH 64
// Parses an incoming BytesStream, pushing the results to the destination sink.
UPB_DEFINE_CLASS0(upb::json::Parser,
public:
Parser(Status* status);
~Parser();
// Resets the state of the printer, so that it will expect to begin a new
// document.
void Reset();
// Resets the output pointer which will serve as our closure. Implies
// Reset().
void ResetOutput(Sink* output);
// The input to the printer.
BytesSink* input();
,
UPB_DEFINE_STRUCT0(upb_json_parser,
upb_byteshandler input_handler_;
upb_bytessink input_;
// Stack to track the JSON scopes we are in.
upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH];
upb_jsonparser_frame *top;
upb_jsonparser_frame *limit;
upb_status *status;
// Ragel's internal parsing stack for the parsing state machine.
int current_state;
int parser_stack[UPB_JSON_MAX_DEPTH];
int parser_top;
// A pointer to the beginning of whatever text we are currently parsing.
const char *text_begin;
// We have to accumulate text for member names, integers, unicode escapes, and
// base64 partial results.
const char *accumulated;
size_t accumulated_len;
// TODO: add members and code for allocating a buffer when necessary (when the
// member spans input buffers or contains escapes).
));
UPB_BEGIN_EXTERN_C
void upb_json_parser_init(upb_json_parser *p, upb_status *status);
void upb_json_parser_uninit(upb_json_parser *p);
void upb_json_parser_reset(upb_json_parser *p);
void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *output);
upb_bytessink *upb_json_parser_input(upb_json_parser *p);
UPB_END_EXTERN_C
#ifdef __cplusplus
namespace upb {
namespace json {
inline Parser::Parser(Status* status) { upb_json_parser_init(this, status); }
inline Parser::~Parser() { upb_json_parser_uninit(this); }
inline void Parser::Reset() { upb_json_parser_reset(this); }
inline void Parser::ResetOutput(Sink* output) {
upb_json_parser_resetoutput(this, output);
}
inline BytesSink* Parser::input() {
return upb_json_parser_input(this);
}
} // namespace json
} // namespace upb
#endif
#endif // UPB_JSON_PARSER_H_

@ -0,0 +1,623 @@
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2014 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* A parser that uses the Ragel State Machine Compiler to generate
* the finite automata.
*
* Ragel only natively handles regular languages, but we can manually
* program it a bit to handle context-free languages like JSON, by using
* the "fcall" and "fret" constructs.
*
* This parser can handle the basics, but needs several things to be fleshed
* out:
*
* - handling of unicode escape sequences (including high surrogate pairs).
* - properly check and report errors for unknown fields, stack overflow,
* improper array nesting (or lack of nesting).
* - handling of base64 sequences with padding characters.
* - handling of push-back (non-success returns from sink functions).
* - handling of keys/escape-sequences/etc that span input buffers.
*/
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "upb/json/parser.h"
#define CHECK_RETURN(x) if (!(x)) return false
static upb_selector_t getsel_for_handlertype(upb_json_parser *p,
upb_handlertype_t type) {
upb_selector_t sel;
bool ok = upb_handlers_getselector(p->top->f, type, &sel);
UPB_ASSERT_VAR(ok, ok);
return sel;
}
static upb_selector_t getsel(upb_json_parser *p) {
return getsel_for_handlertype(
p, upb_handlers_getprimitivehandlertype(p->top->f));
}
static void start_member(upb_json_parser *p) {
assert(!p->top->f);
assert(!p->accumulated);
p->accumulated_len = 0;
}
static bool end_member(upb_json_parser *p) {
// TODO(haberman): support keys that span buffers or have escape sequences.
assert(!p->top->f);
assert(p->accumulated);
const upb_fielddef *f =
upb_msgdef_ntof(p->top->m, p->accumulated, p->accumulated_len);
if (!f) {
// TODO(haberman): Ignore unknown fields if requested/configured to do so.
upb_status_seterrf(p->status, "No such field: %.*s\n",
(int)p->accumulated_len, p->accumulated);
return false;
}
p->top->f = f;
p->accumulated = NULL;
return true;
}
static void start_object(upb_json_parser *p) {
upb_sink_startmsg(&p->top->sink);
}
static void end_object(upb_json_parser *p) {
upb_status status;
upb_sink_endmsg(&p->top->sink, &status);
}
static bool check_stack(upb_json_parser *p) {
if ((p->top + 1) == p->limit) {
upb_status_seterrmsg(p->status, "Nesting too deep");
return false;
}
return true;
}
static bool start_subobject(upb_json_parser *p) {
assert(p->top->f);
if (!upb_fielddef_issubmsg(p->top->f)) {
upb_status_seterrf(p->status,
"Object specified for non-message/group field: %s",
upb_fielddef_name(p->top->f));
return false;
}
if (!check_stack(p)) return false;
upb_jsonparser_frame *inner = p->top + 1;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink);
inner->m = upb_fielddef_msgsubdef(p->top->f);
inner->f = NULL;
p->top = inner;
return true;
}
static void end_subobject(upb_json_parser *p) {
p->top--;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG);
upb_sink_endsubmsg(&p->top->sink, sel);
}
static bool start_array(upb_json_parser *p) {
assert(p->top->f);
if (!upb_fielddef_isseq(p->top->f)) {
upb_status_seterrf(p->status,
"Array specified for non-repeated field: %s",
upb_fielddef_name(p->top->f));
return false;
}
if (!check_stack(p)) return false;
upb_jsonparser_frame *inner = p->top + 1;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
upb_sink_startseq(&p->top->sink, sel, &inner->sink);
inner->m = p->top->m;
inner->f = p->top->f;
p->top = inner;
return true;
}
static void end_array(upb_json_parser *p) {
assert(p->top > p->stack);
p->top--;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
upb_sink_endseq(&p->top->sink, sel);
}
static void clear_member(upb_json_parser *p) { p->top->f = NULL; }
static bool putbool(upb_json_parser *p, bool val) {
if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) {
upb_status_seterrf(p->status,
"Boolean value specified for non-bool field: %s",
upb_fielddef_name(p->top->f));
return false;
}
bool ok = upb_sink_putbool(&p->top->sink, getsel(p), val);
UPB_ASSERT_VAR(ok, ok);
return true;
}
static void start_text(upb_json_parser *p, const char *ptr) {
p->text_begin = ptr;
}
static const signed char b64table[] = {
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */,
52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
-1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1,
-1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1
};
// Returns the table value sign-extended to 32 bits. Knowing that the upper
// bits will be 1 for unrecognized characters makes it easier to check for
// this error condition later (see below).
int32_t b64lookup(unsigned char ch) { return b64table[ch]; }
// Returns true if the given character is not a valid base64 character or
// padding.
bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; }
static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr,
size_t len) {
const char *limit = ptr + len;
for (; ptr < limit; ptr += 4) {
if (limit - ptr < 4) {
upb_status_seterrf(p->status,
"Base64 input for bytes field not a multiple of 4: %s",
upb_fielddef_name(p->top->f));
return false;
}
uint32_t val = b64lookup(ptr[0]) << 18 |
b64lookup(ptr[1]) << 12 |
b64lookup(ptr[2]) << 6 |
b64lookup(ptr[3]);
// Test the upper bit; returns true if any of the characters returned -1.
if (val & 0x80000000) {
goto otherchar;
}
char output[3];
output[0] = val >> 16;
output[1] = (val >> 8) & 0xff;
output[2] = val & 0xff;
upb_sink_putstring(&p->top->sink, sel, output, 3, NULL);
}
return true;
otherchar:
if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) ||
nonbase64(ptr[3]) ) {
upb_status_seterrf(p->status,
"Non-base64 characters in bytes field: %s",
upb_fielddef_name(p->top->f));
return false;
} if (ptr[2] == '=') {
// Last group contains only two input bytes, one output byte.
if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') {
goto badpadding;
}
uint32_t val = b64lookup(ptr[0]) << 18 |
b64lookup(ptr[1]) << 12;
assert(!(val & 0x80000000));
char output = val >> 16;
upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL);
return true;
} else {
// Last group contains only three input bytes, two output bytes.
if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') {
goto badpadding;
}
uint32_t val = b64lookup(ptr[0]) << 18 |
b64lookup(ptr[1]) << 12 |
b64lookup(ptr[2]) << 6;
char output[2];
output[0] = val >> 16;
output[1] = (val >> 8) & 0xff;
upb_sink_putstring(&p->top->sink, sel, output, 2, NULL);
return true;
}
badpadding:
upb_status_seterrf(p->status,
"Incorrect base64 padding for field: %s (%.*s)",
upb_fielddef_name(p->top->f),
4, ptr);
return false;
}
static bool end_text(upb_json_parser *p, const char *ptr) {
assert(!p->accumulated); // TODO: handle this case.
p->accumulated = p->text_begin;
p->accumulated_len = ptr - p->text_begin;
if (p->top->f && upb_fielddef_isstring(p->top->f)) {
// This is a string field (as opposed to a member name).
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
if (upb_fielddef_type(p->top->f) == UPB_TYPE_BYTES) {
CHECK_RETURN(base64_push(p, sel, p->accumulated, p->accumulated_len));
} else {
upb_sink_putstring(&p->top->sink, sel, p->accumulated, p->accumulated_len, NULL);
}
p->accumulated = NULL;
}
return true;
}
static bool start_stringval(upb_json_parser *p) {
assert(p->top->f);
if (!upb_fielddef_isstring(p->top->f)) {
upb_status_seterrf(p->status,
"String specified for non-string field: %s",
upb_fielddef_name(p->top->f));
return false;
}
if (!check_stack(p)) return false;
upb_jsonparser_frame *inner = p->top + 1; // TODO: check for overflow.
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
inner->m = p->top->m;
inner->f = p->top->f;
p->top = inner;
return true;
}
static void end_stringval(upb_json_parser *p) {
p->top--;
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
upb_sink_endstr(&p->top->sink, sel);
}
static void start_number(upb_json_parser *p, const char *ptr) {
start_text(p, ptr);
assert(p->accumulated == NULL);
}
static void end_number(upb_json_parser *p, const char *ptr) {
end_text(p, ptr);
const char *myend = p->accumulated + p->accumulated_len;
char *end;
switch (upb_fielddef_type(p->top->f)) {
case UPB_TYPE_ENUM:
case UPB_TYPE_INT32: {
long val = strtol(p->accumulated, &end, 0);
if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putint32(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_INT64: {
long long val = strtoll(p->accumulated, &end, 0);
if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putint64(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_UINT32: {
unsigned long val = strtoul(p->accumulated, &end, 0);
if (val > UINT32_MAX || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putuint32(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_UINT64: {
unsigned long long val = strtoull(p->accumulated, &end, 0);
if (val > UINT64_MAX || errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putuint64(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_DOUBLE: {
double val = strtod(p->accumulated, &end);
if (errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putdouble(&p->top->sink, getsel(p), val);
break;
}
case UPB_TYPE_FLOAT: {
float val = strtof(p->accumulated, &end);
if (errno == ERANGE || end != myend)
assert(false);
else
upb_sink_putfloat(&p->top->sink, getsel(p), val);
break;
}
default:
assert(false);
}
p->accumulated = NULL;
}
static char escape_char(char in) {
switch (in) {
case 'r': return '\r';
case 't': return '\t';
case 'n': return '\n';
case 'f': return '\f';
case 'b': return '\b';
case '/': return '/';
case '"': return '"';
case '\\': return '\\';
default:
assert(0);
return 'x';
}
}
static void escape(upb_json_parser *p, const char *ptr) {
char ch = escape_char(*ptr);
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
upb_sink_putstring(&p->top->sink, sel, &ch, 1, NULL);
}
static uint8_t hexdigit(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
} else {
assert(ch >= 'A' && ch <= 'F');
return ch - 'A' + 10;
}
}
static void start_hex(upb_json_parser *p, const char *ptr) {
start_text(p, ptr);
}
static void hex(upb_json_parser *p, const char *end) {
UPB_UNUSED(end);
const char *start = p->text_begin;
assert(end - start == 4);
uint16_t codepoint =
(hexdigit(start[0]) << 12) |
(hexdigit(start[1]) << 8) |
(hexdigit(start[2]) << 4) |
hexdigit(start[3]);
// TODO(haberman): convert to UTF-8 and emit (though if it is a high surrogate
// we have to wait for the next escape to get the full code point).
UPB_UNUSED(codepoint);
}
#define CHECK_RETURN_TOP(x) if (!(x)) goto error
// What follows is the Ragel parser itself. The language is specified in Ragel
// and the actions call our C functions above.
%%{
machine json;
ws = space*;
integer = "0" | /[1-9]/ /[0-9]/*;
decimal = "." /[0-9]/+;
exponent = /[eE]/ /[+\-]/? /[0-9]/+;
number_machine :=
("-"? integer decimal? exponent?)
<: any >{ fhold; fret; };
number = /[0-9\-]/ >{ fhold; fcall number_machine; };
text =
/[^\\"]/+
>{ start_text(parser, p); }
%{ CHECK_RETURN_TOP(end_text(parser, p)); }
;
unicode_char =
"\\u"
/[0-9A-Fa-f]/{4}
>{ start_hex(parser, p); }
%{ hex(parser, p); }
;
escape_char =
"\\"
/[rtbfn"\/\\]/
>{ escape(parser, p); }
;
string_machine := (text | unicode_char | escape_char)** '"' @{ fret; } ;
string = '"' @{ fcall string_machine; };
value2 = ^(space | "]" | "}") >{ fhold; fcall value_machine; } ;
member =
ws
string
>{ start_member(parser); }
%{ CHECK_RETURN_TOP(end_member(parser)); }
ws ":" ws
value2
%{ clear_member(parser); }
ws;
object =
"{"
ws
>{ start_object(parser); }
(member ("," member)*)?
"}"
>{ end_object(parser); }
;
element = ws value2 ws;
array =
"["
>{ CHECK_RETURN_TOP(start_array(parser)); }
ws
(element ("," element)*)?
"]"
>{ end_array(parser); }
;
value =
number
>{ start_number(parser, p); }
%{ end_number(parser, p); }
| string
>{ CHECK_RETURN_TOP(start_stringval(parser)); }
%{ end_stringval(parser); }
| "true"
%{ CHECK_RETURN_TOP(putbool(parser, true)); }
| "false"
%{ CHECK_RETURN_TOP(putbool(parser, false)); }
| "null"
%{ /* null value */ }
| object
>{ CHECK_RETURN_TOP(start_subobject(parser)); }
%{ end_subobject(parser); }
| array;
value_machine :=
value
<: any >{ fhold; fret; } ;
main := ws object ws;
}%%
%% write data;
size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const upb_bufhandle *handle) {
UPB_UNUSED(hd);
UPB_UNUSED(handle);
upb_json_parser *parser = closure;
// Variables used by Ragel's generated code.
int cs = parser->current_state;
int *stack = parser->parser_stack;
int top = parser->parser_top;
const char *p = buf;
const char *pe = buf + size;
%% write exec;
if (p != pe) {
upb_status_seterrf(parser->status, "Parse error at %s\n", p);
}
error:
// Save parsing state back to parser.
parser->current_state = cs;
parser->parser_top = top;
return p - buf;
}
bool end(void *closure, const void *hd) {
UPB_UNUSED(closure);
UPB_UNUSED(hd);
return true;
}
void upb_json_parser_init(upb_json_parser *p, upb_status *status) {
p->limit = p->stack + UPB_JSON_MAX_DEPTH;
upb_byteshandler_init(&p->input_handler_);
upb_byteshandler_setstring(&p->input_handler_, parse, NULL);
upb_byteshandler_setendstr(&p->input_handler_, end, NULL);
upb_bytessink_reset(&p->input_, &p->input_handler_, p);
p->status = status;
}
void upb_json_parser_uninit(upb_json_parser *p) {
upb_byteshandler_uninit(&p->input_handler_);
}
void upb_json_parser_reset(upb_json_parser *p) {
p->top = p->stack;
p->top->f = NULL;
int cs;
int top;
// Emit Ragel initialization of the parser.
%% write init;
p->current_state = cs;
p->parser_top = top;
p->text_begin = NULL;
p->accumulated = NULL;
p->accumulated_len = 0;
}
void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *sink) {
upb_json_parser_reset(p);
upb_sink_reset(&p->top->sink, sink->handlers, sink->closure);
p->top->m = upb_handlers_msgdef(sink->handlers);
p->accumulated = NULL;
}
upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
return &p->input_;
}

@ -9,7 +9,7 @@
*
*/
#include "upb/json/typed_printer.h"
#include "upb/json/printer.h"
#include <stdlib.h>
#include <stdio.h>
@ -18,7 +18,7 @@
#include <yajl/yajl_gen.h>
static void doprint(void *_p, const char *buf, unsigned int len) {
upb_json_typedprinter *p = _p;
upb_json_printer *p = _p;
// YAJL doesn't support returning an error status here, so we can't properly
// support clients who return a value other than "len" here.
size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL);
@ -77,7 +77,7 @@ static yajl_gen_status upbyajl_gen_uint64(yajl_gen yajl,
}
static bool putkey(void *closure, const void *handler_data) {
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
const strpc *key = handler_data;
CHKYAJL(yajl_gen_string2(p->yajl_gen_, key->ptr, key->len));
return true;
@ -85,7 +85,7 @@ static bool putkey(void *closure, const void *handler_data) {
#define TYPE_HANDLERS(type, yajlfunc) \
static bool put##type(void *closure, const void *handler_data, type val) { \
upb_json_typedprinter *p = closure; \
upb_json_printer *p = closure; \
UPB_UNUSED(handler_data); \
CHKYAJL(yajlfunc(p->yajl_gen_, val)); \
return true; \
@ -113,7 +113,7 @@ static void *startsubmsg(void *closure, const void *handler_data) {
static bool startmap(void *closure, const void *handler_data) {
UPB_UNUSED(handler_data);
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
if (p->depth_++ == 0) {
upb_bytessink_start(p->output_, 0, &p->subc_);
}
@ -124,7 +124,7 @@ static bool startmap(void *closure, const void *handler_data) {
static bool endmap(void *closure, const void *handler_data, upb_status *s) {
UPB_UNUSED(handler_data);
UPB_UNUSED(s);
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
if (--p->depth_ == 0) {
upb_bytessink_end(p->output_);
}
@ -133,7 +133,7 @@ static bool endmap(void *closure, const void *handler_data, upb_status *s) {
}
static void *startseq(void *closure, const void *handler_data) {
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
CHK(putkey(closure, handler_data));
CHKYAJL(yajl_gen_array_open(p->yajl_gen_));
return closure;
@ -141,7 +141,7 @@ static void *startseq(void *closure, const void *handler_data) {
static bool endseq(void *closure, const void *handler_data) {
UPB_UNUSED(handler_data);
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
CHKYAJL(yajl_gen_array_close(p->yajl_gen_));
return true;
}
@ -149,7 +149,7 @@ static bool endseq(void *closure, const void *handler_data) {
static size_t putstr(void *closure, const void *handler_data, const char *str,
size_t len, const upb_bufhandle *handle) {
UPB_UNUSED(handle);
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
CHKYAJL(yajl_gen_string2(p->yajl_gen_, str, len));
return len;
}
@ -158,7 +158,7 @@ static size_t putstr(void *closure, const void *handler_data, const char *str,
static size_t putbytes(void *closure, const void *handler_data, const char *str,
size_t len, const upb_bufhandle *handle) {
UPB_UNUSED(handle);
upb_json_typedprinter *p = closure;
upb_json_printer *p = closure;
// This is the regular base64, not the "web-safe" version.
static const char base64[] =
@ -175,8 +175,8 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str,
UPB_ASSERT_VAR(limit, (limit - to) >= 4);
to[0] = base64[from[0] >> 2];
to[1] = base64[((from[0] & 0x3) << 4) + (from[1] >> 4)];
to[2] = base64[((from[1] & 0xf) << 2) + (from[2] >> 6)];
to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)];
to[3] = base64[from[2] & 0x3f];
remaining -= 3;
@ -184,6 +184,25 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str,
from += 3;
}
switch (remaining) {
case 2:
to[0] = base64[from[0] >> 2];
to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
to[2] = base64[(from[1] & 0xf) << 2];
to[3] = '=';
to += 4;
from += 2;
break;
case 1:
to[0] = base64[from[0] >> 2];
to[1] = base64[((from[0] & 0x3) << 4)];
to[2] = '=';
to[3] = '=';
to += 4;
from += 1;
break;
}
size_t bytes = to - data;
if (yajl_gen_string2(p->yajl_gen_, data, bytes) != yajl_gen_status_ok) {
return 0;
@ -278,7 +297,7 @@ void sethandlers(const void *closure, upb_handlers *h) {
// YAJL unfortunately does not support stack allocation, nor resetting an
// allocated object, so we have to allocate on the heap and reallocate whenever
// there is a reset.
static void reset(upb_json_typedprinter *p, bool free) {
static void reset(upb_json_printer *p, bool free) {
if (free) {
yajl_gen_free(p->yajl_gen_);
}
@ -289,34 +308,32 @@ static void reset(upb_json_typedprinter *p, bool free) {
/* Public API *****************************************************************/
void upb_json_typedprinter_init(upb_json_typedprinter *p,
const upb_handlers *h) {
void upb_json_printer_init(upb_json_printer *p, const upb_handlers *h) {
p->output_ = NULL;
p->depth_ = 0;
reset(p, false);
upb_sink_reset(&p->input_, h, p);
}
void upb_json_typedprinter_uninit(upb_json_typedprinter *p) {
void upb_json_printer_uninit(upb_json_printer *p) {
yajl_gen_free(p->yajl_gen_);
}
void upb_json_typedprinter_reset(upb_json_typedprinter *p) {
void upb_json_printer_reset(upb_json_printer *p) {
p->depth_ = 0;
reset(p, true);
}
void upb_json_typedprinter_resetoutput(upb_json_typedprinter *p,
upb_bytessink *output) {
upb_json_typedprinter_reset(p);
void upb_json_printer_resetoutput(upb_json_printer *p, upb_bytessink *output) {
upb_json_printer_reset(p);
p->output_ = output;
}
upb_sink *upb_json_typedprinter_input(upb_json_typedprinter *p) {
upb_sink *upb_json_printer_input(upb_json_printer *p) {
return &p->input_;
}
const upb_handlers *upb_json_typedprinter_newhandlers(const upb_msgdef *md,
const void *owner) {
const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
const void *owner) {
return upb_handlers_newfrozen(md, owner, sethandlers, NULL);
}

@ -0,0 +1,97 @@
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2014 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* upb::json::Printer allows you to create handlers that emit JSON
* according to a specific protobuf schema.
*/
#ifndef UPB_JSON_TYPED_PRINTER_H_
#define UPB_JSON_TYPED_PRINTER_H_
#include "upb/sink.h"
#ifdef __cplusplus
namespace upb {
namespace json {
class Printer;
} // namespace json
} // namespace upb
#endif
UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer);
/* upb::json::Printer *********************************************************/
// Prints an incoming stream of data to a BytesSink in JSON format.
UPB_DEFINE_CLASS0(upb::json::Printer,
public:
Printer(const upb::Handlers* handlers);
~Printer();
// Resets the state of the printer, so that it will expect to begin a new
// document.
void Reset();
// Resets the output pointer which will serve as our closure. Implies
// Reset().
void ResetOutput(BytesSink* output);
// The input to the printer.
Sink* input();
// Returns handlers for printing according to the specified schema.
static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
,
UPB_DEFINE_STRUCT0(upb_json_printer,
upb_sink input_;
// Pointer to yajl_gen; void* here so we don't have to include YAJL headers.
void *yajl_gen_;
void *subc_;
upb_bytessink *output_;
// We track the depth so that we know when to emit startstr/endstr on the
// output.
int depth_;
));
UPB_BEGIN_EXTERN_C // {
// Native C API.
void upb_json_printer_init(upb_json_printer *p, const upb_handlers *h);
void upb_json_printer_uninit(upb_json_printer *p);
void upb_json_printer_reset(upb_json_printer *p);
void upb_json_printer_resetoutput(upb_json_printer *p, upb_bytessink *output);
upb_sink *upb_json_printer_input(upb_json_printer *p);
const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
const void *owner);
UPB_END_EXTERN_C // }
#ifdef __cplusplus
namespace upb {
namespace json {
inline Printer::Printer(const upb::Handlers* handlers) {
upb_json_printer_init(this, handlers);
}
inline Printer::~Printer() { upb_json_printer_uninit(this); }
inline void Printer::Reset() { upb_json_printer_reset(this); }
inline void Printer::ResetOutput(BytesSink* output) {
upb_json_printer_resetoutput(this, output);
}
inline Sink* Printer::input() { return upb_json_printer_input(this); }
inline reffed_ptr<const Handlers> Printer::NewHandlers(
const upb::MessageDef *md) {
const Handlers* h = upb_json_printer_newhandlers(md, &h);
return reffed_ptr<const Handlers>(h, &h);
}
} // namespace json
} // namespace upb
#endif
#endif // UPB_JSON_TYPED_PRINTER_H_

@ -1,105 +0,0 @@
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2014 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* upb::json::TypedPrinter allows you to create handlers that emit JSON
* according to a specific protobuf schema.
*/
#ifndef UPB_JSON_TYPED_PRINTER_H_
#define UPB_JSON_TYPED_PRINTER_H_
#include "upb/sink.h"
#ifdef __cplusplus
namespace upb {
namespace json {
class TypedPrinter;
} // namespace json
} // namespace upb
#endif
UPB_DECLARE_TYPE(upb::json::TypedPrinter, upb_json_typedprinter);
/* upb::json::TypedPrinter ****************************************************/
// Prints an incoming stream of data to a BytesSink in JSON format.
UPB_DEFINE_CLASS0(upb::json::TypedPrinter,
public:
TypedPrinter(const upb::Handlers* handlers);
~TypedPrinter();
// Resets the state of the printer, so that it will expect to begin a new
// document.
void Reset();
// Resets the output pointer which will serve as our closure. Implies
// Reset().
void ResetOutput(BytesSink* output);
// The input to the printer.
Sink* input();
// Returns handlers for printing according to the specified schema.
static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
,
UPB_DEFINE_STRUCT0(upb_json_typedprinter,
upb_sink input_;
// Pointer to yajl_gen; void* here so we don't have to include YAJL headers.
void *yajl_gen_;
void *subc_;
upb_bytessink *output_;
// We track the depth so that we know when to emit startstr/endstr on the
// output.
int depth_;
));
UPB_BEGIN_EXTERN_C // {
// Native C API.
void upb_json_typedprinter_init(upb_json_typedprinter *p,
const upb_handlers *h);
void upb_json_typedprinter_uninit(upb_json_typedprinter *p);
void upb_json_typedprinter_reset(upb_json_typedprinter *p);
void upb_json_typedprinter_resetoutput(upb_json_typedprinter *p,
upb_bytessink *output);
upb_sink *upb_json_typedprinter_input(upb_json_typedprinter *p);
const upb_handlers *upb_json_typedprinter_newhandlers(const upb_msgdef *md,
const void *owner);
UPB_END_EXTERN_C // }
#ifdef __cplusplus
namespace upb {
namespace json {
inline TypedPrinter::TypedPrinter(const upb::Handlers* handlers) {
upb_json_typedprinter_init(this, handlers);
}
inline TypedPrinter::~TypedPrinter() {
upb_json_typedprinter_uninit(this);
}
inline void TypedPrinter::Reset() {
upb_json_typedprinter_reset(this);
}
inline void TypedPrinter::ResetOutput(BytesSink* output) {
upb_json_typedprinter_resetoutput(this, output);
}
inline Sink* TypedPrinter::input() {
return upb_json_typedprinter_input(this);
}
inline reffed_ptr<const Handlers> TypedPrinter::NewHandlers(
const upb::MessageDef *md) {
const Handlers* h = upb_json_typedprinter_newhandlers(md, &h);
return reffed_ptr<const Handlers>(h, &h);
}
} // namespace json
} // namespace upb
#endif
#endif // UPB_JSON_TYPED_PRINTER_H_

File diff suppressed because it is too large Load Diff

@ -236,7 +236,7 @@ UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
void **subc) {
*subc = NULL;
*subc = s->closure;
if (!s->handler) return true;
upb_startstr_handlerfunc *start =
(upb_startstr_handlerfunc *)s->handler->table[UPB_STARTSTR_SELECTOR].func;

@ -298,7 +298,12 @@ bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, int n, void *ref_donor,
upb_fielddef *f = upb_msg_iter_field(&j);
const char *name = upb_fielddef_subdefname(f);
if (name && !upb_fielddef_subdef(f)) {
// Try the lookup in the current set of to-be-added defs first. If not
// there, try existing defs.
upb_def *subdef = upb_resolvename(&addtab, base, name);
if (subdef == NULL) {
subdef = upb_resolvename(&s->symtab, base, name);
}
if (subdef == NULL) {
upb_status_seterrf(
status, "couldn't resolve name '%s' in message '%s'", name, base);

Loading…
Cancel
Save