@@ -22,6 +22,7 @@ void jimp_unknown_member(Jimp *jimp);
22
22
bool jimp_array_begin (Jimp * jimp );
23
23
bool jimp_array_item (Jimp * jimp );
24
24
bool jimp_array_end (Jimp * jimp );
25
+ void jimp_diagf (Jimp * jimp , const char * fmt , ...);
25
26
26
27
#endif // JIMP_H_
27
28
@@ -31,6 +32,17 @@ static bool jimp__expect_token(Jimp *jimp, long token);
31
32
static bool jimp__get_and_expect_token (Jimp * jimp , long token );
32
33
static const char * jimp__token_kind (long token );
33
34
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
+
34
46
static const char * jimp__token_kind (long token )
35
47
{
36
48
switch (token ) {
@@ -97,9 +109,7 @@ bool jimp_array_item(Jimp *jimp)
97
109
98
110
void jimp_unknown_member (Jimp * jimp )
99
111
{
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 );
103
113
}
104
114
105
115
bool jimp_object_begin (Jimp * jimp )
@@ -147,9 +157,7 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
147
157
} else if (strcmp (jimp -> l .string , "false" ) == 0 ) {
148
158
* boolean = false;
149
159
} 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 ));
153
161
return false;
154
162
}
155
163
return true;
@@ -172,9 +180,7 @@ static bool jimp__get_and_expect_token(Jimp *jimp, long token)
172
180
static bool jimp__expect_token (Jimp * jimp , long token )
173
181
{
174
182
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 ));
178
184
return false;
179
185
}
180
186
return true;
0 commit comments