@@ -22,6 +22,7 @@ void jimp_unknown_member(Jimp *jimp);
2222bool jimp_array_begin (Jimp * jimp );
2323bool jimp_array_item (Jimp * jimp );
2424bool 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);
3132static bool jimp__get_and_expect_token (Jimp * jimp , long token );
3233static 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+
3446static const char * jimp__token_kind (long token )
3547{
3648 switch (token ) {
@@ -97,9 +109,7 @@ bool jimp_array_item(Jimp *jimp)
97109
98110void 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
105115bool 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)
172180static 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