4
4
#include "stb_c_lexer.h"
5
5
6
6
typedef struct {
7
+ // TODO: get rid of the dependency on stb_c_lexer.h
7
8
stb_lexer l ;
8
9
const char * file_path ;
9
10
const char * member ;
@@ -22,16 +23,15 @@ bool jimp_array_begin(Jimp *jimp);
22
23
bool jimp_array_item (Jimp * jimp );
23
24
bool jimp_array_end (Jimp * jimp );
24
25
25
- // TODO: should be private
26
- bool jimp_expect_token (Jimp * jimp , long token );
27
- bool jimp_get_and_expect_token (Jimp * jimp , long token );
28
- const char * jimp_token_kind (long token );
29
-
30
26
#endif // JIMP_H_
31
27
32
28
#ifdef JIMP_IMPLEMENTATION
33
29
34
- const char * jimp_token_kind (long token )
30
+ static bool jimp__expect_token (Jimp * jimp , long token );
31
+ static bool jimp__get_and_expect_token (Jimp * jimp , long token );
32
+ static const char * jimp__token_kind (long token );
33
+
34
+ static const char * jimp__token_kind (long token )
35
35
{
36
36
switch (token ) {
37
37
case CLEX_id : return ("identifier" );
@@ -74,12 +74,12 @@ const char *jimp_token_kind(long token)
74
74
75
75
bool jimp_array_begin (Jimp * jimp )
76
76
{
77
- return jimp_get_and_expect_token (jimp , '[' );
77
+ return jimp__get_and_expect_token (jimp , '[' );
78
78
}
79
79
80
80
bool jimp_array_end (Jimp * jimp )
81
81
{
82
- return jimp_get_and_expect_token (jimp , ']' );
82
+ return jimp__get_and_expect_token (jimp , ']' );
83
83
}
84
84
85
85
bool jimp_array_item (Jimp * jimp )
@@ -104,52 +104,52 @@ void jimp_unknown_member(Jimp *jimp)
104
104
105
105
bool jimp_object_begin (Jimp * jimp )
106
106
{
107
- return jimp_get_and_expect_token (jimp , '{' );
107
+ return jimp__get_and_expect_token (jimp , '{' );
108
108
}
109
109
110
110
bool jimp_object_member (Jimp * jimp )
111
111
{
112
112
char * point = jimp -> l .parse_point ;
113
113
if (!stb_c_lexer_get_token (& jimp -> l )) return false;
114
114
if (jimp -> l .token == ',' ) {
115
- if (!jimp_get_and_expect_token (jimp , CLEX_dqstring )) return false;
115
+ if (!jimp__get_and_expect_token (jimp , CLEX_dqstring )) return false;
116
116
jimp -> member = strdup (jimp -> l .string ); // TODO: memory leak
117
- if (!jimp_get_and_expect_token (jimp , ':' )) return false;
117
+ if (!jimp__get_and_expect_token (jimp , ':' )) return false;
118
118
return true;
119
119
}
120
120
if (jimp -> l .token == '}' ) {
121
121
jimp -> l .parse_point = point ;
122
122
return false;
123
123
}
124
- if (!jimp_expect_token (jimp , CLEX_dqstring )) return false;
124
+ if (!jimp__expect_token (jimp , CLEX_dqstring )) return false;
125
125
jimp -> member = strdup (jimp -> l .string ); // TODO: memory leak
126
- if (!jimp_get_and_expect_token (jimp , ':' )) return false;
126
+ if (!jimp__get_and_expect_token (jimp , ':' )) return false;
127
127
return true;
128
128
}
129
129
130
130
bool jimp_object_end (Jimp * jimp )
131
131
{
132
- return jimp_get_and_expect_token (jimp , '}' );
132
+ return jimp__get_and_expect_token (jimp , '}' );
133
133
}
134
134
135
135
bool jimp_string (Jimp * jimp , const char * * string )
136
136
{
137
- if (!jimp_get_and_expect_token (jimp , CLEX_dqstring )) return false;
137
+ if (!jimp__get_and_expect_token (jimp , CLEX_dqstring )) return false;
138
138
* string = strdup (jimp -> l .string );
139
139
return true;
140
140
}
141
141
142
142
bool jimp_bool (Jimp * jimp , bool * boolean )
143
143
{
144
- if (!jimp_get_and_expect_token (jimp , CLEX_id )) return false;
144
+ if (!jimp__get_and_expect_token (jimp , CLEX_id )) return false;
145
145
if (strcmp (jimp -> l .string , "true" ) == 0 ) {
146
146
* boolean = true;
147
147
} else if (strcmp (jimp -> l .string , "false" ) == 0 ) {
148
148
* boolean = false;
149
149
} else {
150
150
stb_lex_location loc = {0 };
151
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 ));
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 ));
153
153
return false;
154
154
}
155
155
return true;
@@ -158,23 +158,23 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
158
158
bool jimp_number (Jimp * jimp , long * number )
159
159
{
160
160
// TODO: there are more things that constitude number in JSON, for example floats. Take all of them into account here.
161
- if (!jimp_get_and_expect_token (jimp , CLEX_intlit )) return false;
161
+ if (!jimp__get_and_expect_token (jimp , CLEX_intlit )) return false;
162
162
* number = jimp -> l .int_number ;
163
163
return true;
164
164
}
165
165
166
- bool jimp_get_and_expect_token (Jimp * jimp , long token )
166
+ static bool jimp__get_and_expect_token (Jimp * jimp , long token )
167
167
{
168
168
if (!stb_c_lexer_get_token (& jimp -> l )) return false;
169
- return jimp_expect_token (jimp , token );
169
+ return jimp__expect_token (jimp , token );
170
170
}
171
171
172
- bool jimp_expect_token (Jimp * jimp , long token )
172
+ static bool jimp__expect_token (Jimp * jimp , long token )
173
173
{
174
174
if (jimp -> l .token != token ) {
175
175
stb_lex_location loc = {0 };
176
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 ));
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 ));
178
178
return false;
179
179
}
180
180
return true;
0 commit comments