scanner: Introduce struct location for tracking source locations

This commit is contained in:
Kristian Høgsberg 2013-11-15 14:21:11 -08:00
parent 3470fa17b5
commit db8ae8903f

View file

@ -43,6 +43,11 @@ usage(int ret)
#define XML_BUFFER_SIZE 4096 #define XML_BUFFER_SIZE 4096
struct location {
const char *filename;
int line_number;
};
struct description { struct description {
char *summary; char *summary;
char *text; char *text;
@ -121,7 +126,7 @@ struct entry {
}; };
struct parse_context { struct parse_context {
const char *filename; struct location loc;
XML_Parser parser; XML_Parser parser;
struct protocol *protocol; struct protocol *protocol;
struct interface *interface; struct interface *interface;
@ -250,10 +255,10 @@ desc_dump(char *desc, const char *fmt, ...)
} }
static void static void
fail(struct parse_context *ctx, const char *msg) fail(struct location *loc, const char *msg)
{ {
fprintf(stderr, "%s:%ld: %s\n", fprintf(stderr, "%s:%d: error: %s\n",
ctx->filename, XML_GetCurrentLineNumber(ctx->parser), msg); loc->filename, loc->line_number, msg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -287,6 +292,7 @@ start_element(void *data, const char *element_name, const char **atts)
char *end; char *end;
int i, version; int i, version;
ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
name = NULL; name = NULL;
type = NULL; type = NULL;
version = 0; version = 0;
@ -318,7 +324,7 @@ start_element(void *data, const char *element_name, const char **atts)
ctx->character_data_length = 0; ctx->character_data_length = 0;
if (strcmp(element_name, "protocol") == 0) { if (strcmp(element_name, "protocol") == 0) {
if (name == NULL) if (name == NULL)
fail(ctx, "no protocol name given"); fail(&ctx->loc, "no protocol name given");
ctx->protocol->name = xstrdup(name); ctx->protocol->name = xstrdup(name);
ctx->protocol->uppercase_name = uppercase_dup(name); ctx->protocol->uppercase_name = uppercase_dup(name);
@ -327,10 +333,10 @@ start_element(void *data, const char *element_name, const char **atts)
} else if (strcmp(element_name, "interface") == 0) { } else if (strcmp(element_name, "interface") == 0) {
if (name == NULL) if (name == NULL)
fail(ctx, "no interface name given"); fail(&ctx->loc, "no interface name given");
if (version == 0) if (version == 0)
fail(ctx, "no interface version given"); fail(&ctx->loc, "no interface version given");
interface = xmalloc(sizeof *interface); interface = xmalloc(sizeof *interface);
interface->name = xstrdup(name); interface->name = xstrdup(name);
@ -347,7 +353,7 @@ start_element(void *data, const char *element_name, const char **atts)
} else if (strcmp(element_name, "request") == 0 || } else if (strcmp(element_name, "request") == 0 ||
strcmp(element_name, "event") == 0) { strcmp(element_name, "event") == 0) {
if (name == NULL) if (name == NULL)
fail(ctx, "no request name given"); fail(&ctx->loc, "no request name given");
message = xmalloc(sizeof *message); message = xmalloc(sizeof *message);
message->name = xstrdup(name); message->name = xstrdup(name);
@ -372,21 +378,21 @@ start_element(void *data, const char *element_name, const char **atts)
if (since != NULL) { if (since != NULL) {
version = strtol(since, &end, 0); version = strtol(since, &end, 0);
if (errno == EINVAL || end == since || *end != '\0') if (errno == EINVAL || end == since || *end != '\0')
fail(ctx, "invalid integer\n"); fail(&ctx->loc, "invalid integer\n");
if (version < ctx->interface->since) if (version < ctx->interface->since)
fail(ctx, "since version not increasing\n"); fail(&ctx->loc, "since version not increasing\n");
ctx->interface->since = version; ctx->interface->since = version;
} }
message->since = ctx->interface->since; message->since = ctx->interface->since;
if (strcmp(name, "destroy") == 0 && !message->destructor) if (strcmp(name, "destroy") == 0 && !message->destructor)
fail(ctx, "destroy request should be destructor type"); fail(&ctx->loc, "destroy request should be destructor type");
ctx->message = message; ctx->message = message;
} else if (strcmp(element_name, "arg") == 0) { } else if (strcmp(element_name, "arg") == 0) {
if (name == NULL) if (name == NULL)
fail(ctx, "no argument name given"); fail(&ctx->loc, "no argument name given");
arg = xmalloc(sizeof *arg); arg = xmalloc(sizeof *arg);
arg->name = xstrdup(name); arg->name = xstrdup(name);
@ -408,7 +414,7 @@ start_element(void *data, const char *element_name, const char **atts)
} else if (strcmp(type, "object") == 0) { } else if (strcmp(type, "object") == 0) {
arg->type = OBJECT; arg->type = OBJECT;
} else { } else {
fail(ctx, "unknown type"); fail(&ctx->loc, "unknown type");
} }
switch (arg->type) { switch (arg->type) {
@ -425,7 +431,7 @@ start_element(void *data, const char *element_name, const char **atts)
break; break;
default: default:
if (interface_name != NULL) if (interface_name != NULL)
fail(ctx, "interface not allowed"); fail(&ctx->loc, "interface not allowed");
break; break;
} }
@ -434,10 +440,10 @@ start_element(void *data, const char *element_name, const char **atts)
else if (strcmp(allow_null, "true") == 0) else if (strcmp(allow_null, "true") == 0)
arg->nullable = 1; arg->nullable = 1;
else else
fail(ctx, "invalid value for allow-null attribute"); fail(&ctx->loc, "invalid value for allow-null attribute");
if (allow_null != NULL && !is_nullable_type(arg)) if (allow_null != NULL && !is_nullable_type(arg))
fail(ctx, "allow-null is only valid for objects, strings, and arrays"); fail(&ctx->loc, "allow-null is only valid for objects, strings, and arrays");
arg->summary = NULL; arg->summary = NULL;
if (summary) if (summary)
@ -447,7 +453,7 @@ start_element(void *data, const char *element_name, const char **atts)
ctx->message->arg_count++; ctx->message->arg_count++;
} else if (strcmp(element_name, "enum") == 0) { } else if (strcmp(element_name, "enum") == 0) {
if (name == NULL) if (name == NULL)
fail(ctx, "no enum name given"); fail(&ctx->loc, "no enum name given");
enumeration = xmalloc(sizeof *enumeration); enumeration = xmalloc(sizeof *enumeration);
enumeration->name = xstrdup(name); enumeration->name = xstrdup(name);
@ -461,7 +467,7 @@ start_element(void *data, const char *element_name, const char **atts)
ctx->enumeration = enumeration; ctx->enumeration = enumeration;
} else if (strcmp(element_name, "entry") == 0) { } else if (strcmp(element_name, "entry") == 0) {
if (name == NULL) if (name == NULL)
fail(ctx, "no entry name given"); fail(&ctx->loc, "no entry name given");
entry = xmalloc(sizeof *entry); entry = xmalloc(sizeof *entry);
entry->name = xstrdup(name); entry->name = xstrdup(name);
@ -475,7 +481,7 @@ start_element(void *data, const char *element_name, const char **atts)
&entry->link); &entry->link);
} else if (strcmp(element_name, "description") == 0) { } else if (strcmp(element_name, "description") == 0) {
if (summary == NULL) if (summary == NULL)
fail(ctx, "description without summary"); fail(&ctx->loc, "description without summary");
description = xmalloc(sizeof *description); description = xmalloc(sizeof *description);
description->summary = xstrdup(summary); description->summary = xstrdup(summary);
@ -1202,7 +1208,7 @@ int main(int argc, char *argv[])
memset(&ctx, 0, sizeof ctx); memset(&ctx, 0, sizeof ctx);
ctx.protocol = &protocol; ctx.protocol = &protocol;
ctx.filename = "<stdin>"; ctx.loc.filename = "<stdin>";
ctx.parser = XML_ParserCreate(NULL); ctx.parser = XML_ParserCreate(NULL);
XML_SetUserData(ctx.parser, &ctx); XML_SetUserData(ctx.parser, &ctx);
if (ctx.parser == NULL) { if (ctx.parser == NULL) {