Skip to content

Commit 2fd1199

Browse files
committed
jimp: factor out jimp_diagf()
1 parent 512ec29 commit 2fd1199

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

jimp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int main()
9292
if (!jimp_array_begin(&jimp)) return 1;
9393
while (jimp_array_item(&jimp)) {
9494
long x = 0;
95-
jimp_number(&jimp, &x);
95+
if (!jimp_number(&jimp, &x)) return 1;
9696
da_append(&xs, x);
9797
}
9898
if (!jimp_array_end(&jimp)) return 1;

jimp.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void jimp_unknown_member(Jimp *jimp);
2222
bool jimp_array_begin(Jimp *jimp);
2323
bool jimp_array_item(Jimp *jimp);
2424
bool jimp_array_end(Jimp *jimp);
25+
void jimp_diagf(Jimp *jimp, const char *fmt, ...);
2526

2627
#endif // JIMP_H_
2728

@@ -31,6 +32,17 @@ static bool jimp__expect_token(Jimp *jimp, long token);
3132
static bool jimp__get_and_expect_token(Jimp *jimp, long token);
3233
static const char *jimp__token_kind(long token);
3334

35+
void jimp_diagf(Jimp *jimp, const char *fmt, ...)
36+
{
37+
stb_lex_location loc = {0};
38+
stb_c_lexer_get_location(&jimp->l, jimp->l.where_firstchar, &loc);
39+
fprintf(stderr, "%s:%d:%d: ", jimp->file_path, loc.line_number, loc.line_offset + 1);
40+
va_list args;
41+
va_start(args, fmt);
42+
vfprintf(stderr, fmt, args);
43+
va_end(args);
44+
}
45+
3446
static const char *jimp__token_kind(long token)
3547
{
3648
switch (token) {
@@ -97,9 +109,7 @@ bool jimp_array_item(Jimp *jimp)
97109

98110
void jimp_unknown_member(Jimp *jimp)
99111
{
100-
stb_lex_location loc = {0};
101-
stb_c_lexer_get_location(&jimp->l, jimp->l.where_firstchar, &loc);
102-
fprintf(stderr, "%s:%d:%d: ERROR: Unexpected member `%s`\n", jimp->file_path, loc.line_number, loc.line_offset + 1, jimp->member);
112+
jimp_diagf(jimp, "ERROR: Unexpected object member `%s`\n", jimp->member);
103113
}
104114

105115
bool jimp_object_begin(Jimp *jimp)
@@ -147,9 +157,7 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
147157
} else if (strcmp(jimp->l.string, "false") == 0) {
148158
*boolean = false;
149159
} else {
150-
stb_lex_location loc = {0};
151-
stb_c_lexer_get_location(&jimp->l, jimp->l.where_firstchar, &loc);
152-
fprintf(stderr, "%s:%d:%d: ERROR: Expected boolean but got `%s`\n", jimp->file_path, loc.line_number, loc.line_offset + 1, jimp__token_kind(jimp->l.token));
160+
jimp_diagf(jimp, "ERROR: Expected boolean but got `%s`\n", jimp__token_kind(jimp->l.token));
153161
return false;
154162
}
155163
return true;
@@ -172,9 +180,7 @@ static bool jimp__get_and_expect_token(Jimp *jimp, long token)
172180
static bool jimp__expect_token(Jimp *jimp, long token)
173181
{
174182
if (jimp->l.token != token) {
175-
stb_lex_location loc = {0};
176-
stb_c_lexer_get_location(&jimp->l, jimp->l.where_firstchar, &loc);
177-
fprintf(stderr, "%s:%d:%d: ERROR: expected %s, but got %s\n", jimp->file_path, loc.line_number, loc.line_offset + 1, jimp__token_kind(token), jimp__token_kind(jimp->l.token));
183+
jimp_diagf(jimp, "ERROR: expected %s, but got %s\n", jimp__token_kind(token), jimp__token_kind(jimp->l.token));
178184
return false;
179185
}
180186
return true;

0 commit comments

Comments
 (0)