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
struct location {
const char *filename;
int line_number;
};
struct description {
char *summary;
char *text;
@ -121,7 +126,7 @@ struct entry {
};
struct parse_context {
const char *filename;
struct location loc;
XML_Parser parser;
struct protocol *protocol;
struct interface *interface;
@ -250,10 +255,10 @@ desc_dump(char *desc, const char *fmt, ...)
}
static void
fail(struct parse_context *ctx, const char *msg)
fail(struct location *loc, const char *msg)
{
fprintf(stderr, "%s:%ld: %s\n",
ctx->filename, XML_GetCurrentLineNumber(ctx->parser), msg);
fprintf(stderr, "%s:%d: error: %s\n",
loc->filename, loc->line_number, msg);
exit(EXIT_FAILURE);
}
@ -287,6 +292,7 @@ start_element(void *data, const char *element_name, const char **atts)
char *end;
int i, version;
ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
name = NULL;
type = NULL;
version = 0;
@ -318,7 +324,7 @@ start_element(void *data, const char *element_name, const char **atts)
ctx->character_data_length = 0;
if (strcmp(element_name, "protocol") == 0) {
if (name == NULL)
fail(ctx, "no protocol name given");
fail(&ctx->loc, "no protocol name given");
ctx->protocol->name = xstrdup(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) {
if (name == NULL)
fail(ctx, "no interface name given");
fail(&ctx->loc, "no interface name given");
if (version == 0)
fail(ctx, "no interface version given");
fail(&ctx->loc, "no interface version given");
interface = xmalloc(sizeof *interface);
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 ||
strcmp(element_name, "event") == 0) {
if (name == NULL)
fail(ctx, "no request name given");
fail(&ctx->loc, "no request name given");
message = xmalloc(sizeof *message);
message->name = xstrdup(name);
@ -372,21 +378,21 @@ start_element(void *data, const char *element_name, const char **atts)
if (since != NULL) {
version = strtol(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)
fail(ctx, "since version not increasing\n");
fail(&ctx->loc, "since version not increasing\n");
ctx->interface->since = version;
}
message->since = ctx->interface->since;
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;
} else if (strcmp(element_name, "arg") == 0) {
if (name == NULL)
fail(ctx, "no argument name given");
fail(&ctx->loc, "no argument name given");
arg = xmalloc(sizeof *arg);
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) {
arg->type = OBJECT;
} else {
fail(ctx, "unknown type");
fail(&ctx->loc, "unknown type");
}
switch (arg->type) {
@ -425,7 +431,7 @@ start_element(void *data, const char *element_name, const char **atts)
break;
default:
if (interface_name != NULL)
fail(ctx, "interface not allowed");
fail(&ctx->loc, "interface not allowed");
break;
}
@ -434,10 +440,10 @@ start_element(void *data, const char *element_name, const char **atts)
else if (strcmp(allow_null, "true") == 0)
arg->nullable = 1;
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))
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;
if (summary)
@ -447,7 +453,7 @@ start_element(void *data, const char *element_name, const char **atts)
ctx->message->arg_count++;
} else if (strcmp(element_name, "enum") == 0) {
if (name == NULL)
fail(ctx, "no enum name given");
fail(&ctx->loc, "no enum name given");
enumeration = xmalloc(sizeof *enumeration);
enumeration->name = xstrdup(name);
@ -461,7 +467,7 @@ start_element(void *data, const char *element_name, const char **atts)
ctx->enumeration = enumeration;
} else if (strcmp(element_name, "entry") == 0) {
if (name == NULL)
fail(ctx, "no entry name given");
fail(&ctx->loc, "no entry name given");
entry = xmalloc(sizeof *entry);
entry->name = xstrdup(name);
@ -475,7 +481,7 @@ start_element(void *data, const char *element_name, const char **atts)
&entry->link);
} else if (strcmp(element_name, "description") == 0) {
if (summary == NULL)
fail(ctx, "description without summary");
fail(&ctx->loc, "description without summary");
description = xmalloc(sizeof *description);
description->summary = xstrdup(summary);
@ -1202,7 +1208,7 @@ int main(int argc, char *argv[])
memset(&ctx, 0, sizeof ctx);
ctx.protocol = &protocol;
ctx.filename = "<stdin>";
ctx.loc.filename = "<stdin>";
ctx.parser = XML_ParserCreate(NULL);
XML_SetUserData(ctx.parser, &ctx);
if (ctx.parser == NULL) {