Skip to content

Commit

Permalink
builder: add an optional suffix string for INI parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoscano committed Apr 22, 2014
1 parent 3f4903e commit e7c7468
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
8 changes: 6 additions & 2 deletions builder/index-parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,16 @@ emptylines:
void
yyerror (YYLTYPE * yylloc, yyscan_t scanner, struct parse_context *context, const char *msg)
{
fprintf (stderr, "%s%s%s%ssyntax error at line %d: %s\n",
int has_suffix = context->error_suffix != NULL && context->error_suffix[0] != 0;

fprintf (stderr, "%s%s%s%ssyntax error at line %d: %s%s%s\n",
context->program_name ? context->program_name : "",
context->program_name ? ": " : "",
context->input_file ? context->input_file : "",
context->input_file ? ": " : "",
yylloc->first_line, msg);
yylloc->first_line, msg,
has_suffix ? " " : "",
has_suffix ? context->error_suffix : "");
}

int
Expand Down
3 changes: 2 additions & 1 deletion builder/index-parser-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern void unix_error (int errcode, char * cmdname, value arg) Noreturn;
extern int do_parse (struct parse_context *context, FILE *in);

value
virt_builder_parse_index (value progv, value filenamev)
virt_builder_parse_index (value progv, value error_suffixv, value filenamev)
{
CAMLparam2 (progv, filenamev);
CAMLlocal5 (rv, v, sv, sv2, fv);
Expand All @@ -58,6 +58,7 @@ virt_builder_parse_index (value progv, value filenamev)
parse_context_init (&context);
context.program_name = String_val (progv);
context.input_file = String_val (filenamev);
context.error_suffix = String_val (error_suffixv);

in = fopen (String_val (filenamev), "r");
if (in == NULL)
Expand Down
1 change: 1 addition & 0 deletions builder/index-struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct parse_context {
int seen_comments;
const char *input_file;
const char *program_name;
const char *error_suffix;
};

/* Initialize the content of a parse_context. */
Expand Down
6 changes: 3 additions & 3 deletions builder/ini_reader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ and c_section = string * c_fields (* [name] + fields *)
and c_fields = field array

(* Calls yyparse in the C code. *)
external parse_index : prog:string -> string -> c_sections = "virt_builder_parse_index"
external parse_index : prog:string -> error_suffix:string -> string -> c_sections = "virt_builder_parse_index"

let read_ini ~prog file =
let sections = parse_index ~prog file in
let read_ini ~prog ?(error_suffix = "") file =
let sections = parse_index ~prog ~error_suffix file in
let sections = Array.to_list sections in
List.map (
fun (n, fields) ->
Expand Down
2 changes: 1 addition & 1 deletion builder/ini_reader.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ and section = string * fields (* [name] + fields *)
and fields = field list
and field = string * string option * string (* key + subkey + value *)

val read_ini : prog:string -> string -> sections
val read_ini : prog:string -> ?error_suffix:string -> string -> sections

0 comments on commit e7c7468

Please sign in to comment.