Skip to content

Commit

Permalink
3/15/2022
Browse files Browse the repository at this point in the history
  • Loading branch information
mingo-liu committed Mar 15, 2022
1 parent e11b43d commit 9845926
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Binary file added tutorial01/a.out
Binary file not shown.
31 changes: 30 additions & 1 deletion tutorial01/leptjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,50 @@ static int lept_parse_null(lept_context* c, lept_value* v) {
return LEPT_PARSE_OK;
}

static int lept_parse_false(lept_context* c, lept_value* v)
{
EXPECT(c, 'f');
if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e')
return LEPT_PARSE_INVALID_VALUE;
c->json += 4;
v->type = LEPT_FALSE;
return LEPT_PARSE_OK;
}

static int lept_parse_true(lept_context* c, lept_value* v)
{
EXPECT(c, 't');
if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e')
return LEPT_PARSE_INVALID_VALUE;
c->json += 3;
v->type = LEPT_TRUE;
return LEPT_PARSE_OK;
}

static int lept_parse_value(lept_context* c, lept_value* v) {
switch (*c->json) {
case 'n': return lept_parse_null(c, v);
case 'f': return lept_parse_false(c, v);
case 't': return lept_parse_true(c, v);
case '\0': return LEPT_PARSE_EXPECT_VALUE;
default: return LEPT_PARSE_INVALID_VALUE;
}
}

int lept_parse(lept_value* v, const char* json) {
lept_context c;
int ret;
assert(v != NULL);
c.json = json;
v->type = LEPT_NULL;
lept_parse_whitespace(&c);
return lept_parse_value(&c, v);
if ((ret = lept_parse_value(&c, v)) == LEPT_PARSE_OK)
{
lept_parse_whitespace(&c);
if (*c.json != '\0')
ret = LEPT_PARSE_ROOT_NOT_SINGULAR;
}
return ret;
}

lept_type lept_get_type(const lept_value* v) {
Expand Down
16 changes: 16 additions & 0 deletions tutorial01/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ static void test_parse_null() {
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
}

static void test_parse_false(){
lept_value v;
v.type = LEPT_TRUE;
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false"));
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v));
}

static void test_parse_true(){
lept_value v;
v.type = LEPT_FALSE;
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true"));
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v));
}

static void test_parse_expect_value() {
lept_value v;

Expand Down Expand Up @@ -59,6 +73,8 @@ static void test_parse_root_not_singular() {

static void test_parse() {
test_parse_null();
test_parse_false();
test_parse_true();
test_parse_expect_value();
test_parse_invalid_value();
test_parse_root_not_singular();
Expand Down

0 comments on commit 9845926

Please sign in to comment.