Interface refinement: rename some constants.

* UPB_STOP -> UPB_BREAK, better represents breaking
  out of a parsing loop.
* UPB_STATUS_OK -> UPB_OK, for all status codes, more
  concise at no readability cost (perhaps an improvement).
pull/13171/head
Joshua Haberman 14 years ago
parent a38742bbe1
commit 1dea81b1c2
  1. 5
      core/upb.c
  2. 16
      core/upb.h
  3. 22
      core/upb_def.c
  4. 6
      core/upb_stream.h

@ -60,9 +60,8 @@ void upb_copyerr(upb_status *to, upb_status *from)
} }
void upb_clearerr(upb_status *status) { void upb_clearerr(upb_status *status) {
status->code = UPB_STATUS_OK; status->code = UPB_OK;
upb_string_unref(status->str); upb_string_recycle(&status->str);
status->str = NULL;
} }
void upb_printerr(upb_status *status) { void upb_printerr(upb_status *status) {

@ -291,16 +291,16 @@ INLINE void upb_value_write(upb_valueptr ptr, upb_value val,
// resumed. // resumed.
enum upb_status_code { enum upb_status_code {
// The operation completed successfully. // The operation completed successfully.
UPB_STATUS_OK = 0, UPB_OK = 0,
// The bytesrc is at EOF and all data was read successfully. // The bytesrc is at EOF and all data was read successfully.
UPB_STATUS_EOF = 1, UPB_EOF = 1,
// A read or write from a streaming src/sink could not be completed right now. // A read or write from a streaming src/sink could not be completed right now.
UPB_STATUS_TRYAGAIN = 2, UPB_TRYAGAIN = 2,
// An unrecoverable error occurred. // An unrecoverable error occurred.
UPB_STATUS_ERROR = -1, UPB_ERROR = -1,
// A recoverable error occurred (for example, data of the wrong type was // A recoverable error occurred (for example, data of the wrong type was
// encountered which we can skip over). // encountered which we can skip over).
@ -308,21 +308,21 @@ enum upb_status_code {
}; };
// TODO: consider adding error space and code, to let ie. errno be stored // TODO: consider adding error space and code, to let ie. errno be stored
// as a proper code. // as a proper code, or application-specific error codes.
typedef struct { typedef struct {
char code; char code;
upb_string *str; upb_string *str;
} upb_status; } upb_status;
#define UPB_STATUS_INIT {UPB_STATUS_OK, NULL} #define UPB_STATUS_INIT {UPB_OK, NULL}
#define UPB_ERRORMSG_MAXLEN 256 #define UPB_ERRORMSG_MAXLEN 256
INLINE bool upb_ok(upb_status *status) { INLINE bool upb_ok(upb_status *status) {
return status->code == UPB_STATUS_OK; return status->code == UPB_OK;
} }
INLINE void upb_status_init(upb_status *status) { INLINE void upb_status_init(upb_status *status) {
status->code = UPB_STATUS_OK; status->code = UPB_OK;
status->str = NULL; status->str = NULL;
} }

@ -470,9 +470,8 @@ static upb_flow_t upb_enumdef_EnumValueDescriptorProto_value(void *_b,
static upb_flow_t upb_enumdef_EnumValueDescriptorProto_endmsg(void *_b) { static upb_flow_t upb_enumdef_EnumValueDescriptorProto_endmsg(void *_b) {
upb_defbuilder *b = _b; upb_defbuilder *b = _b;
if(!b->saw_number || !b->saw_name) { if(!b->saw_number || !b->saw_name) {
upb_seterr(&b->status, UPB_STATUS_ERROR, upb_seterr(&b->status, UPB_ERROR, "Enum value missing name or number.");
"Enum value missing name or number."); return UPB_BREAK;
return UPB_STOP;
} }
upb_ntoi_ent ntoi_ent = {{b->name, 0}, b->number}; upb_ntoi_ent ntoi_ent = {{b->name, 0}, b->number};
upb_iton_ent iton_ent = {{b->number, 0}, b->name}; upb_iton_ent iton_ent = {{b->number, 0}, b->name};
@ -629,7 +628,7 @@ static upb_flow_t upb_fielddef_value(void *_b, upb_fielddef *f, upb_value val) {
break; break;
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_FIELDNUM: { case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_FIELDNUM: {
upb_string *str = upb_string_new(); upb_string *str = upb_string_new();
if (!upb_value_getfullstr(val, str, NULL)) return UPB_STOP; if (!upb_value_getfullstr(val, str, NULL)) return UPB_BREAK;
if(b->f->def) upb_def_unref(b->f->def); if(b->f->def) upb_def_unref(b->f->def);
b->f->def = UPB_UPCAST(upb_unresolveddef_new(str)); b->f->def = UPB_UPCAST(upb_unresolveddef_new(str));
b->f->owned = true; b->f->owned = true;
@ -683,9 +682,8 @@ static upb_flow_t upb_msgdef_endmsg(void *_b) {
upb_defbuilder *b = _b; upb_defbuilder *b = _b;
upb_msgdef *m = upb_defbuilder_top(b); upb_msgdef *m = upb_defbuilder_top(b);
if(!m->base.fqname) { if(!m->base.fqname) {
upb_seterr(&b->status, UPB_STATUS_ERROR, upb_seterr(&b->status, UPB_ERROR, "Encountered message with no name.");
"Encountered message with no name."); return UPB_BREAK;
return UPB_STOP;
} }
// Create an ordering over the fields. // Create an ordering over the fields.
@ -864,7 +862,7 @@ static bool upb_symtab_findcycles(upb_msgdef *m, int depth, upb_status *status)
// where we recurse over the type tree (like for example, right now) and an // where we recurse over the type tree (like for example, right now) and an
// absurdly deep tree could cause us to stack overflow on systems with very // absurdly deep tree could cause us to stack overflow on systems with very
// limited stacks. // limited stacks.
upb_seterr(status, UPB_STATUS_ERROR, "Type " UPB_STRFMT " was found at " upb_seterr(status, UPB_ERROR, "Type " UPB_STRFMT " was found at "
"depth %d in the type graph, which exceeds the maximum type " "depth %d in the type graph, which exceeds the maximum type "
"depth of %d.", UPB_UPCAST(m)->fqname, depth, "depth of %d.", UPB_UPCAST(m)->fqname, depth,
UPB_MAX_TYPE_DEPTH); UPB_MAX_TYPE_DEPTH);
@ -873,7 +871,7 @@ static bool upb_symtab_findcycles(upb_msgdef *m, int depth, upb_status *status)
// Cycle! // Cycle!
int cycle_len = depth - 1; int cycle_len = depth - 1;
if(cycle_len > UPB_MAX_TYPE_CYCLE_LEN) { if(cycle_len > UPB_MAX_TYPE_CYCLE_LEN) {
upb_seterr(status, UPB_STATUS_ERROR, "Type " UPB_STRFMT " was involved " upb_seterr(status, UPB_ERROR, "Type " UPB_STRFMT " was involved "
"in a cycle of length %d, which exceeds the maximum type " "in a cycle of length %d, which exceeds the maximum type "
"cycle length of %d.", UPB_UPCAST(m)->fqname, cycle_len, "cycle length of %d.", UPB_UPCAST(m)->fqname, cycle_len,
UPB_MAX_TYPE_CYCLE_LEN); UPB_MAX_TYPE_CYCLE_LEN);
@ -931,7 +929,7 @@ bool upb_resolverefs(upb_strtable *tmptab, upb_strtable *symtab,
upb_symtab_ent *found; upb_symtab_ent *found;
if(!(found = upb_resolve(tmptab, base, name)) && if(!(found = upb_resolve(tmptab, base, name)) &&
!(found = upb_resolve(symtab, base, name))) { !(found = upb_resolve(symtab, base, name))) {
upb_seterr(status, UPB_STATUS_ERROR, upb_seterr(status, UPB_ERROR,
"could not resolve symbol '" UPB_STRFMT "'" "could not resolve symbol '" UPB_STRFMT "'"
" in context '" UPB_STRFMT "'", " in context '" UPB_STRFMT "'",
UPB_STRARG(name), UPB_STRARG(base)); UPB_STRARG(name), UPB_STRARG(base));
@ -941,7 +939,7 @@ bool upb_resolverefs(upb_strtable *tmptab, upb_strtable *symtab,
// Check the type of the found def. // Check the type of the found def.
upb_fieldtype_t expected = upb_issubmsg(f) ? UPB_DEF_MSG : UPB_DEF_ENUM; upb_fieldtype_t expected = upb_issubmsg(f) ? UPB_DEF_MSG : UPB_DEF_ENUM;
if(found->def->type != expected) { if(found->def->type != expected) {
upb_seterr(status, UPB_STATUS_ERROR, "Unexpected type"); upb_seterr(status, UPB_ERROR, "Unexpected type");
return false; return false;
} }
upb_msgdef_resolve(m, f, found->def); upb_msgdef_resolve(m, f, found->def);
@ -983,7 +981,7 @@ bool upb_symtab_add_defs(upb_symtab *s, upb_def **defs, int num_defs,
// allow_redef is set. // allow_redef is set.
if (upb_strtable_lookup(&tmptab, def->fqname) || if (upb_strtable_lookup(&tmptab, def->fqname) ||
(!allow_redef && upb_strtable_lookup(&s->symtab, def->fqname))) { (!allow_redef && upb_strtable_lookup(&s->symtab, def->fqname))) {
upb_seterr(status, UPB_STATUS_ERROR, "Redefinition of symbol " UPB_STRFMT, upb_seterr(status, UPB_ERROR, "Redefinition of symbol " UPB_STRFMT,
UPB_STRARG(def->fqname)); UPB_STRARG(def->fqname));
goto err; goto err;
} }

@ -39,8 +39,10 @@ typedef enum {
// Caller should continue sending values to the sink. // Caller should continue sending values to the sink.
UPB_CONTINUE, UPB_CONTINUE,
// Stop processing for now; check status for details. // Stop processing for now; check status for details. If no status was set,
UPB_STOP, // a generic error will be returned. If the error is resumable, processing
// will resume by delivering this callback again.
UPB_BREAK,
// Skips to the end of the current submessage (or if we are at the top // Skips to the end of the current submessage (or if we are at the top
// level, skips to the end of the entire message). // level, skips to the end of the entire message).

Loading…
Cancel
Save