aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkoekeishiya <aasvi93@hotmail.com>2017-10-01 21:30:50 +0200
committerkoekeishiya <aasvi93@hotmail.com>2017-10-01 21:30:50 +0200
commita746b3451fe1e811f490df6f30d29250be284a65 (patch)
tree76cfabcee56ac418a9fbaf0ce9c46c158eb90f9d
parentf5a12091e4cb8153338924b12480d72b5dbb0d14 (diff)
downloadskhd-a746b3451fe1e811f490df6f30d29250be284a65.tar.gz
skhd-a746b3451fe1e811f490df6f30d29250be284a65.zip
cleanup error reporting
Diffstat (limited to '')
-rw-r--r--src/parse.c37
-rw-r--r--src/parse.h1
2 files changed, 18 insertions, 20 deletions
diff --git a/src/parse.c b/src/parse.c
index 54154c5..809b744 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <stdarg.h>
#include <stdbool.h>
#define internal static
@@ -137,10 +138,7 @@ parse_modifier(struct parser *parser)
if (parser_match(parser, Token_Modifier)) {
flags |= parse_modifier(parser);
} else {
- fprintf(stderr, "(#%d:%d) expected modifier, but got '%.*s'\n",
- parser->current_token.line, parser->current_token.cursor,
- parser->current_token.length, parser->current_token.text);
- parser->error = true;
+ parser_report_error(parser, "expected modifier");
}
}
@@ -167,10 +165,7 @@ parse_hotkey(struct parser *parser)
if (found_modifier) {
if (!parser_match(parser, Token_Dash)) {
- fprintf(stderr, "(#%d:%d) expected '-', but got '%.*s'\n",
- parser->current_token.line, parser->current_token.cursor,
- parser->current_token.length, parser->current_token.text);
- parser->error = true;
+ parser_report_error(parser, "expected '-'");
return NULL;
}
}
@@ -182,10 +177,7 @@ parse_hotkey(struct parser *parser)
} else if (parser_match(parser, Token_Literal)) {
parse_key_literal(parser, hotkey);
} else {
- fprintf(stderr, "(#%d:%d) expected key-literal, but got '%.*s'\n",
- parser->current_token.line, parser->current_token.cursor,
- parser->current_token.length, parser->current_token.text);
- parser->error = true;
+ parser_report_error(parser, "expected key-literal");
return NULL;
}
@@ -196,10 +188,7 @@ parse_hotkey(struct parser *parser)
if (parser_match(parser, Token_Command)) {
hotkey->command = parse_command(parser);
} else {
- fprintf(stderr, "(#%d:%d) expected ':' followed by command, but got '%.*s'\n",
- parser->current_token.line, parser->current_token.cursor,
- parser->current_token.length, parser->current_token.text);
- parser->error = true;
+ parser_report_error(parser, "expected ':' followed by command");
return NULL;
}
@@ -223,10 +212,7 @@ void parse_config(struct parser *parser, struct table *hotkey_map)
}
table_add(hotkey_map, hotkey, hotkey);
} else {
- fprintf(stderr, "(#%d:%d) expected modifier or key-literal, but got '%.*s'\n",
- parser->current_token.line, parser->current_token.cursor,
- parser->current_token.length, parser->current_token.text);
- parser->error = true;
+ parser_report_error(parser, "expected modifier or key-literal");
return;
}
}
@@ -276,6 +262,17 @@ bool parser_match(struct parser *parser, enum token_type type)
return false;
}
+void parser_report_error(struct parser *parser, const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ fprintf(stderr, "(#%d:%d) ", parser->current_token.line, parser->current_token.cursor);
+ vfprintf(stderr, format, args);
+ fprintf(stderr, ", but got '%.*s'\n", parser->current_token.length, parser->current_token.text);
+ va_end(args);
+ parser->error = true;
+}
+
bool parser_init(struct parser *parser, char *file)
{
memset(parser, 0, sizeof(struct parser));
diff --git a/src/parse.h b/src/parse.h
index 52d3aa9..241e7ff 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -23,5 +23,6 @@ bool parser_check(struct parser *parser, enum token_type type);
bool parser_match(struct parser *parser, enum token_type type);
bool parser_init(struct parser *parser, char *file);
void parser_destroy(struct parser *parser);
+void parser_report_error(struct parser *parser, const char *format, ...);
#endif