Refactor a couple of parsing functions to be simpler to follow. Avoid huge

2008-08-24  Johan Dahlin  <johan@gnome.org>

    * girepository/girparser.c (start_glib_boxed), (start_function),
    (start_field), (start_alias):
    Refactor a couple of parsing functions to be simpler to follow.
    Avoid huge ifs.


svn path=/trunk/; revision=481
This commit is contained in:
Johan Dahlin 2008-08-24 11:01:44 +00:00 committed by Johan Dahlin
parent 78f4434ef6
commit ed13aae5f2

View File

@ -480,50 +480,54 @@ start_glib_boxed (GMarkupParseContext *context,
ParseContext *ctx, ParseContext *ctx,
GError **error) GError **error)
{ {
if (strcmp (element_name, "glib:boxed") == 0 && const gchar *name;
ctx->state == STATE_NAMESPACE) const gchar *typename;
{ const gchar *typeinit;
const gchar *name; const gchar *deprecated;
const gchar *typename; GIrNodeBoxed *boxed;
const gchar *typeinit;
const gchar *deprecated;
name = find_attribute ("glib:name", attribute_names, attribute_values);
typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
if (name == NULL)
MISSING_ATTRIBUTE (context, error, element_name, "glib:name");
else if (typename == NULL)
MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
else if (typeinit == NULL)
MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
else
{
GIrNodeBoxed *boxed;
boxed = (GIrNodeBoxed *) g_ir_node_new (G_IR_NODE_BOXED); if (!(strcmp (element_name, "glib:boxed") == 0 &&
ctx->state == STATE_NAMESPACE))
((GIrNode *)boxed)->name = g_strdup (name); return FALSE;
boxed->gtype_name = g_strdup (typename);
boxed->gtype_init = g_strdup (typeinit); name = find_attribute ("glib:name", attribute_names, attribute_values);
if (deprecated && strcmp (deprecated, "1") == 0) typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
boxed->deprecated = TRUE; typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
else deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
boxed->deprecated = FALSE;
if (name == NULL)
ctx->current_node = (GIrNode *)boxed; {
ctx->current_module->entries = MISSING_ATTRIBUTE (context, error, element_name, "glib:name");
g_list_append (ctx->current_module->entries, boxed); return FALSE;
}
state_switch (ctx, STATE_BOXED); else if (typename == NULL)
} {
MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
return TRUE; return FALSE;
}
else if (typeinit == NULL)
{
MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
return FALSE;
} }
return FALSE; boxed = (GIrNodeBoxed *) g_ir_node_new (G_IR_NODE_BOXED);
((GIrNode *)boxed)->name = g_strdup (name);
boxed->gtype_name = g_strdup (typename);
boxed->gtype_init = g_strdup (typeinit);
if (deprecated && strcmp (deprecated, "1") == 0)
boxed->deprecated = TRUE;
else
boxed->deprecated = FALSE;
ctx->current_node = (GIrNode *)boxed;
ctx->current_module->entries =
g_list_append (ctx->current_module->entries, boxed);
state_switch (ctx, STATE_BOXED);
return TRUE;
} }
static gboolean static gboolean
@ -534,118 +538,129 @@ start_function (GMarkupParseContext *context,
ParseContext *ctx, ParseContext *ctx,
GError **error) GError **error)
{ {
if ((ctx->state == STATE_NAMESPACE && const gchar *name;
(strcmp (element_name, "function") == 0 || const gchar *symbol;
strcmp (element_name, "callback") == 0)) || const gchar *deprecated;
((ctx->state == STATE_CLASS || GIrNodeFunction *function;
ctx->state == STATE_INTERFACE || gboolean found = FALSE;
ctx->state == STATE_BOXED ||
ctx->state == STATE_UNION) && switch (ctx->state)
(strcmp (element_name, "method") == 0 ||
strcmp (element_name, "callback") == 0)) ||
((ctx->state == STATE_CLASS ||
ctx->state == STATE_BOXED) &&
(strcmp (element_name, "constructor") == 0)) ||
(ctx->state == STATE_STRUCT && strcmp (element_name, "callback") == 0))
{ {
const gchar *name; case STATE_NAMESPACE:
const gchar *symbol; found = (strcmp (element_name, "function") == 0 ||
const gchar *deprecated; strcmp (element_name, "callback") == 0);
break;
name = find_attribute ("name", attribute_names, attribute_values); case STATE_CLASS:
symbol = find_attribute ("c:identifier", attribute_names, attribute_values); case STATE_BOXED:
deprecated = find_attribute ("deprecated", attribute_names, attribute_values); case STATE_STRUCT:
case STATE_UNION:
if (name == NULL) found = strcmp (element_name, "constructor") == 0;
MISSING_ATTRIBUTE (context, error, element_name, "name"); /* fallthrough */
else if (strcmp (element_name, "callback") != 0 && symbol == NULL) case STATE_INTERFACE:
MISSING_ATTRIBUTE (context, error, element_name, "c:identifier"); found = (found ||
else strcmp (element_name, "method") == 0 ||
{ strcmp (element_name, "callback") == 0);
GIrNodeFunction *function; break;
default:
function = (GIrNodeFunction *) g_ir_node_new (G_IR_NODE_FUNCTION); break;
((GIrNode *)function)->name = g_strdup (name);
function->symbol = g_strdup (symbol);
function->parameters = NULL;
if (deprecated && strcmp (deprecated, "1") == 0)
function->deprecated = TRUE;
else
function->deprecated = FALSE;
if (strcmp (element_name, "method") == 0 ||
strcmp (element_name, "constructor") == 0)
{
function->is_method = TRUE;
if (strcmp (element_name, "constructor") == 0)
function->is_constructor = TRUE;
else
function->is_constructor = FALSE;
}
else
{
function->is_method = FALSE;
function->is_setter = FALSE;
function->is_getter = FALSE;
function->is_constructor = FALSE;
if (strcmp (element_name, "callback") == 0)
((GIrNode *)function)->type = G_IR_NODE_CALLBACK;
}
if (ctx->current_node == NULL)
{
ctx->current_module->entries =
g_list_append (ctx->current_module->entries, function);
}
else
switch (ctx->current_node->type)
{
case G_IR_NODE_INTERFACE:
case G_IR_NODE_OBJECT:
{
GIrNodeInterface *iface;
iface = (GIrNodeInterface *)ctx->current_node;
iface->members = g_list_append (iface->members, function);
}
break;
case G_IR_NODE_BOXED:
{
GIrNodeBoxed *boxed;
boxed = (GIrNodeBoxed *)ctx->current_node;
boxed->members = g_list_append (boxed->members, function);
}
break;
case G_IR_NODE_STRUCT:
{
GIrNodeStruct *struct_;
struct_ = (GIrNodeStruct *)ctx->current_node;
struct_->members = g_list_append (struct_->members, function); }
break;
case G_IR_NODE_UNION:
{
GIrNodeUnion *union_;
union_ = (GIrNodeUnion *)ctx->current_node;
union_->members = g_list_append (union_->members, function);
}
break;
default:
g_assert_not_reached ();
}
ctx->current_node = (GIrNode *)function;
state_switch (ctx, STATE_FUNCTION);
return TRUE;
}
} }
return FALSE; if (!found)
return FALSE;
name = find_attribute ("name", attribute_names, attribute_values);
symbol = find_attribute ("c:identifier", attribute_names, attribute_values);
deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
if (name == NULL)
{
MISSING_ATTRIBUTE (context, error, element_name, "name");
return FALSE;
}
else if (strcmp (element_name, "callback") != 0 && symbol == NULL)
{
MISSING_ATTRIBUTE (context, error, element_name, "c:identifier");
return FALSE;
}
function = (GIrNodeFunction *) g_ir_node_new (G_IR_NODE_FUNCTION);
((GIrNode *)function)->name = g_strdup (name);
function->symbol = g_strdup (symbol);
function->parameters = NULL;
if (deprecated && strcmp (deprecated, "1") == 0)
function->deprecated = TRUE;
else
function->deprecated = FALSE;
if (strcmp (element_name, "method") == 0 ||
strcmp (element_name, "constructor") == 0)
{
function->is_method = TRUE;
if (strcmp (element_name, "constructor") == 0)
function->is_constructor = TRUE;
else
function->is_constructor = FALSE;
}
else
{
function->is_method = FALSE;
function->is_setter = FALSE;
function->is_getter = FALSE;
function->is_constructor = FALSE;
if (strcmp (element_name, "callback") == 0)
((GIrNode *)function)->type = G_IR_NODE_CALLBACK;
}
if (ctx->current_node == NULL)
{
ctx->current_module->entries =
g_list_append (ctx->current_module->entries, function);
}
else
switch (ctx->current_node->type)
{
case G_IR_NODE_INTERFACE:
case G_IR_NODE_OBJECT:
{
GIrNodeInterface *iface;
iface = (GIrNodeInterface *)ctx->current_node;
iface->members = g_list_append (iface->members, function);
}
break;
case G_IR_NODE_BOXED:
{
GIrNodeBoxed *boxed;
boxed = (GIrNodeBoxed *)ctx->current_node;
boxed->members = g_list_append (boxed->members, function);
}
break;
case G_IR_NODE_STRUCT:
{
GIrNodeStruct *struct_;
struct_ = (GIrNodeStruct *)ctx->current_node;
struct_->members = g_list_append (struct_->members, function); }
break;
case G_IR_NODE_UNION:
{
GIrNodeUnion *union_;
union_ = (GIrNodeUnion *)ctx->current_node;
union_->members = g_list_append (union_->members, function);
}
break;
default:
g_assert_not_reached ();
}
ctx->current_node = (GIrNode *)function;
state_switch (ctx, STATE_FUNCTION);
return TRUE;
} }
static gboolean static gboolean
@ -793,123 +808,129 @@ start_field (GMarkupParseContext *context,
ParseContext *ctx, ParseContext *ctx,
GError **error) GError **error)
{ {
if (strcmp (element_name, "field") == 0 && const gchar *name;
(ctx->state == STATE_CLASS || const gchar *readable;
ctx->state == STATE_BOXED || const gchar *writable;
ctx->state == STATE_STRUCT || const gchar *bits;
ctx->state == STATE_UNION || const gchar *branch;
ctx->state == STATE_INTERFACE)) const gchar *offset;
GIrNodeField *field;
switch (ctx->state)
{ {
const gchar *name; case STATE_CLASS:
const gchar *readable; case STATE_BOXED:
const gchar *writable; case STATE_STRUCT:
const gchar *bits; case STATE_UNION:
const gchar *branch; case STATE_INTERFACE:
const gchar *offset; break;
default:
name = find_attribute ("name", attribute_names, attribute_values); return FALSE;
readable = find_attribute ("readable", attribute_names, attribute_values);
writable = find_attribute ("writable", attribute_names, attribute_values);
bits = find_attribute ("bits", attribute_names, attribute_values);
branch = find_attribute ("branch", attribute_names, attribute_values);
offset = find_attribute ("offset", attribute_names, attribute_values);
if (name == NULL)
MISSING_ATTRIBUTE (context, error, element_name, "name");
else
{
GIrNodeField *field;
field = (GIrNodeField *)g_ir_node_new (G_IR_NODE_FIELD);
ctx->current_typed = (GIrNode*) field;
((GIrNode *)field)->name = g_strdup (name);
if (readable && strcmp (readable, "1") == 0)
field->readable = TRUE;
else
field->readable = FALSE;
if (writable && strcmp (writable, "1") == 0)
field->writable = TRUE;
else
field->writable = FALSE;
if (bits)
field->bits = atoi (bits);
else
field->bits = 0;
if (offset)
field->offset = atoi (offset);
else
field->offset = 0;
switch (ctx->current_node->type)
{
case G_IR_NODE_OBJECT:
{
GIrNodeInterface *iface;
iface = (GIrNodeInterface *)ctx->current_node;
iface->members = g_list_append (iface->members, field);
state_switch (ctx, STATE_CLASS_FIELD);
}
break;
case G_IR_NODE_INTERFACE:
{
GIrNodeInterface *iface;
iface = (GIrNodeInterface *)ctx->current_node;
iface->members = g_list_append (iface->members, field);
state_switch (ctx, STATE_INTERFACE_FIELD);
}
break;
case G_IR_NODE_BOXED:
{
GIrNodeBoxed *boxed;
boxed = (GIrNodeBoxed *)ctx->current_node;
boxed->members = g_list_append (boxed->members, field);
state_switch (ctx, STATE_BOXED_FIELD);
}
break;
case G_IR_NODE_STRUCT:
{
GIrNodeStruct *struct_;
struct_ = (GIrNodeStruct *)ctx->current_node;
struct_->members = g_list_append (struct_->members, field);
state_switch (ctx, STATE_STRUCT_FIELD);
}
break;
case G_IR_NODE_UNION:
{
GIrNodeUnion *union_;
union_ = (GIrNodeUnion *)ctx->current_node;
union_->members = g_list_append (union_->members, field);
if (branch)
{
GIrNodeConstant *constant;
constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
((GIrNode *)constant)->name = g_strdup (name);
constant->value = g_strdup (branch);
constant->type = union_->discriminator_type;
constant->deprecated = FALSE;
union_->discriminators = g_list_append (union_->discriminators, constant);
}
state_switch (ctx, STATE_UNION_FIELD);
}
break;
default:
g_assert_not_reached ();
}
}
return TRUE;
} }
return FALSE; if (strcmp (element_name, "field") != 0)
return FALSE;
name = find_attribute ("name", attribute_names, attribute_values);
readable = find_attribute ("readable", attribute_names, attribute_values);
writable = find_attribute ("writable", attribute_names, attribute_values);
bits = find_attribute ("bits", attribute_names, attribute_values);
branch = find_attribute ("branch", attribute_names, attribute_values);
offset = find_attribute ("offset", attribute_names, attribute_values);
if (name == NULL)
{
MISSING_ATTRIBUTE (context, error, element_name, "name");
return FALSE;
}
field = (GIrNodeField *)g_ir_node_new (G_IR_NODE_FIELD);
ctx->current_typed = (GIrNode*) field;
((GIrNode *)field)->name = g_strdup (name);
if (readable && strcmp (readable, "1") == 0)
field->readable = TRUE;
else
field->readable = FALSE;
if (writable && strcmp (writable, "1") == 0)
field->writable = TRUE;
else
field->writable = FALSE;
if (bits)
field->bits = atoi (bits);
else
field->bits = 0;
if (offset)
field->offset = atoi (offset);
else
field->offset = 0;
switch (ctx->current_node->type)
{
case G_IR_NODE_OBJECT:
{
GIrNodeInterface *iface;
iface = (GIrNodeInterface *)ctx->current_node;
iface->members = g_list_append (iface->members, field);
state_switch (ctx, STATE_CLASS_FIELD);
}
break;
case G_IR_NODE_INTERFACE:
{
GIrNodeInterface *iface;
iface = (GIrNodeInterface *)ctx->current_node;
iface->members = g_list_append (iface->members, field);
state_switch (ctx, STATE_INTERFACE_FIELD);
}
break;
case G_IR_NODE_BOXED:
{
GIrNodeBoxed *boxed;
boxed = (GIrNodeBoxed *)ctx->current_node;
boxed->members = g_list_append (boxed->members, field);
state_switch (ctx, STATE_BOXED_FIELD);
}
break;
case G_IR_NODE_STRUCT:
{
GIrNodeStruct *struct_;
struct_ = (GIrNodeStruct *)ctx->current_node;
struct_->members = g_list_append (struct_->members, field);
state_switch (ctx, STATE_STRUCT_FIELD);
}
break;
case G_IR_NODE_UNION:
{
GIrNodeUnion *union_;
union_ = (GIrNodeUnion *)ctx->current_node;
union_->members = g_list_append (union_->members, field);
if (branch)
{
GIrNodeConstant *constant;
constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
((GIrNode *)constant)->name = g_strdup (name);
constant->value = g_strdup (branch);
constant->type = union_->discriminator_type;
constant->deprecated = FALSE;
union_->discriminators = g_list_append (union_->discriminators, constant);
}
state_switch (ctx, STATE_UNION_FIELD);
}
break;
default:
g_assert_not_reached ();
}
return TRUE;
} }
static gboolean static gboolean
@ -925,18 +946,21 @@ start_alias (GMarkupParseContext *context,
const gchar *type; const gchar *type;
name = find_attribute ("name", attribute_names, attribute_values); name = find_attribute ("name", attribute_names, attribute_values);
if (name == NULL) { if (name == NULL)
MISSING_ATTRIBUTE (context, error, element_name, "name"); {
return FALSE; MISSING_ATTRIBUTE (context, error, element_name, "name");
} return FALSE;
}
target = find_attribute ("target", attribute_names, attribute_values); target = find_attribute ("target", attribute_names, attribute_values);
if (name == NULL) { if (name == NULL)
MISSING_ATTRIBUTE (context, error, element_name, "target"); {
return FALSE; MISSING_ATTRIBUTE (context, error, element_name, "target");
} return FALSE;
}
g_hash_table_insert (ctx->aliases, g_strdup (name), g_strdup (target)); g_hash_table_insert (ctx->aliases, g_strdup (name), g_strdup (target));
return TRUE; return TRUE;
} }