|
@@ -4,6 +4,7 @@
|
|
|
#include "stb_c_lexer.h"
|
|
|
|
|
|
typedef struct {
|
|
|
+ // TODO: get rid of the dependency on stb_c_lexer.h
|
|
|
stb_lexer l;
|
|
|
const char *file_path;
|
|
|
const char *member;
|
|
@@ -22,16 +23,15 @@ bool jimp_array_begin(Jimp *jimp);
|
|
|
bool jimp_array_item(Jimp *jimp);
|
|
|
bool jimp_array_end(Jimp *jimp);
|
|
|
|
|
|
-// TODO: should be private
|
|
|
-bool jimp_expect_token(Jimp *jimp, long token);
|
|
|
-bool jimp_get_and_expect_token(Jimp *jimp, long token);
|
|
|
-const char *jimp_token_kind(long token);
|
|
|
-
|
|
|
#endif // JIMP_H_
|
|
|
|
|
|
#ifdef JIMP_IMPLEMENTATION
|
|
|
|
|
|
-const char *jimp_token_kind(long token)
|
|
|
+static bool jimp__expect_token(Jimp *jimp, long token);
|
|
|
+static bool jimp__get_and_expect_token(Jimp *jimp, long token);
|
|
|
+static const char *jimp__token_kind(long token);
|
|
|
+
|
|
|
+static const char *jimp__token_kind(long token)
|
|
|
{
|
|
|
switch (token) {
|
|
|
case CLEX_id : return("identifier");
|
|
@@ -74,12 +74,12 @@ const char *jimp_token_kind(long token)
|
|
|
|
|
|
bool jimp_array_begin(Jimp *jimp)
|
|
|
{
|
|
|
- return jimp_get_and_expect_token(jimp, '[');
|
|
|
+ return jimp__get_and_expect_token(jimp, '[');
|
|
|
}
|
|
|
|
|
|
bool jimp_array_end(Jimp *jimp)
|
|
|
{
|
|
|
- return jimp_get_and_expect_token(jimp, ']');
|
|
|
+ return jimp__get_and_expect_token(jimp, ']');
|
|
|
}
|
|
|
|
|
|
bool jimp_array_item(Jimp *jimp)
|
|
@@ -104,7 +104,7 @@ void jimp_unknown_member(Jimp *jimp)
|
|
|
|
|
|
bool jimp_object_begin(Jimp *jimp)
|
|
|
{
|
|
|
- return jimp_get_and_expect_token(jimp, '{');
|
|
|
+ return jimp__get_and_expect_token(jimp, '{');
|
|
|
}
|
|
|
|
|
|
bool jimp_object_member(Jimp *jimp)
|
|
@@ -112,36 +112,36 @@ bool jimp_object_member(Jimp *jimp)
|
|
|
char *point = jimp->l.parse_point;
|
|
|
if (!stb_c_lexer_get_token(&jimp->l)) return false;
|
|
|
if (jimp->l.token == ',') {
|
|
|
- if (!jimp_get_and_expect_token(jimp, CLEX_dqstring)) return false;
|
|
|
+ if (!jimp__get_and_expect_token(jimp, CLEX_dqstring)) return false;
|
|
|
jimp->member = strdup(jimp->l.string); // TODO: memory leak
|
|
|
- if (!jimp_get_and_expect_token(jimp, ':')) return false;
|
|
|
+ if (!jimp__get_and_expect_token(jimp, ':')) return false;
|
|
|
return true;
|
|
|
}
|
|
|
if (jimp->l.token == '}') {
|
|
|
jimp->l.parse_point = point;
|
|
|
return false;
|
|
|
}
|
|
|
- if (!jimp_expect_token(jimp, CLEX_dqstring)) return false;
|
|
|
+ if (!jimp__expect_token(jimp, CLEX_dqstring)) return false;
|
|
|
jimp->member = strdup(jimp->l.string); // TODO: memory leak
|
|
|
- if (!jimp_get_and_expect_token(jimp, ':')) return false;
|
|
|
+ if (!jimp__get_and_expect_token(jimp, ':')) return false;
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool jimp_object_end(Jimp *jimp)
|
|
|
{
|
|
|
- return jimp_get_and_expect_token(jimp, '}');
|
|
|
+ return jimp__get_and_expect_token(jimp, '}');
|
|
|
}
|
|
|
|
|
|
bool jimp_string(Jimp *jimp, const char **string)
|
|
|
{
|
|
|
- if (!jimp_get_and_expect_token(jimp, CLEX_dqstring)) return false;
|
|
|
+ if (!jimp__get_and_expect_token(jimp, CLEX_dqstring)) return false;
|
|
|
*string = strdup(jimp->l.string);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool jimp_bool(Jimp *jimp, bool *boolean)
|
|
|
{
|
|
|
- if (!jimp_get_and_expect_token(jimp, CLEX_id)) return false;
|
|
|
+ if (!jimp__get_and_expect_token(jimp, CLEX_id)) return false;
|
|
|
if (strcmp(jimp->l.string, "true") == 0) {
|
|
|
*boolean = true;
|
|
|
} else if (strcmp(jimp->l.string, "false") == 0) {
|
|
@@ -149,7 +149,7 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
|
|
|
} else {
|
|
|
stb_lex_location loc = {0};
|
|
|
stb_c_lexer_get_location(&jimp->l, jimp->l.where_firstchar, &loc);
|
|
|
- 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));
|
|
|
+ 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));
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
@@ -158,23 +158,23 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
|
|
|
bool jimp_number(Jimp *jimp, long *number)
|
|
|
{
|
|
|
// TODO: there are more things that constitude number in JSON, for example floats. Take all of them into account here.
|
|
|
- if (!jimp_get_and_expect_token(jimp, CLEX_intlit)) return false;
|
|
|
+ if (!jimp__get_and_expect_token(jimp, CLEX_intlit)) return false;
|
|
|
*number = jimp->l.int_number;
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool jimp_get_and_expect_token(Jimp *jimp, long token)
|
|
|
+static bool jimp__get_and_expect_token(Jimp *jimp, long token)
|
|
|
{
|
|
|
if (!stb_c_lexer_get_token(&jimp->l)) return false;
|
|
|
- return jimp_expect_token(jimp, token);
|
|
|
+ return jimp__expect_token(jimp, token);
|
|
|
}
|
|
|
|
|
|
-bool jimp_expect_token(Jimp *jimp, long token)
|
|
|
+static bool jimp__expect_token(Jimp *jimp, long token)
|
|
|
{
|
|
|
if (jimp->l.token != token) {
|
|
|
stb_lex_location loc = {0};
|
|
|
stb_c_lexer_get_location(&jimp->l, jimp->l.where_firstchar, &loc);
|
|
|
- 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));
|
|
|
+ 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));
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|