From 0aab8f10983681baa2c92c1508fcf80f93b45eda Mon Sep 17 00:00:00 2001 From: natcoder Date: Tue, 20 Sep 2016 08:18:54 +0800 Subject: [PATCH] leptjson lesson1 --- tutorial01/leptjson.c | 29 +++++++++++++++++++++++- tutorial01/test.c | 52 +++++++++++++++++++++++++++++++++---------- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/tutorial01/leptjson.c b/tutorial01/leptjson.c index 5299fe1d..df29d169 100644 --- a/tutorial01/leptjson.c +++ b/tutorial01/leptjson.c @@ -24,21 +24,48 @@ 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_EXPECT_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_EXPECT_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) { + int ret; lept_context c; 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] != 0) + ret = LEPT_PARSE_ROOT_NOT_SINGULAR; + } + return ret; } lept_type lept_get_type(const lept_value* v) { diff --git a/tutorial01/test.c b/tutorial01/test.c index e7672181..92b0e2f8 100644 --- a/tutorial01/test.c +++ b/tutorial01/test.c @@ -9,25 +9,43 @@ static int test_pass = 0; #define EXPECT_EQ_BASE(equality, expect, actual, format) \ do {\ - test_count++;\ - if (equality)\ - test_pass++;\ + test_count++;\ + if (equality)\ + test_pass++;\ else {\ - fprintf(stderr, "%s:%d: expect: " format " actual: " format "\n", __FILE__, __LINE__, expect, actual);\ - main_ret = 1;\ - }\ + fprintf(stderr, "%s:%d: expect: " format " actual: " format "\n", __FILE__, __LINE__, expect, actual);\ + main_ret = 1;\ +}\ } while(0) #define EXPECT_EQ_INT(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d") -static void test_parse_null() { +static void test_parse_null() +{ lept_value v; v.type = LEPT_FALSE; EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "null")); EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); } -static void test_parse_expect_value() { +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; v.type = LEPT_FALSE; @@ -39,7 +57,8 @@ static void test_parse_expect_value() { EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); } -static void test_parse_invalid_value() { +static void test_parse_invalid_value() +{ lept_value v; v.type = LEPT_FALSE; EXPECT_EQ_INT(LEPT_PARSE_INVALID_VALUE, lept_parse(&v, "nul")); @@ -50,22 +69,31 @@ static void test_parse_invalid_value() { EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); } -static void test_parse_root_not_singular() { +static void test_parse_root_not_singular() +{ lept_value v; v.type = LEPT_FALSE; EXPECT_EQ_INT(LEPT_PARSE_ROOT_NOT_SINGULAR, lept_parse(&v, "null x")); EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); } -static void test_parse() { +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(); } -int main() { +int main() +{ + int n; + test_parse(); printf("%d/%d (%3.2f%%) passed\n", test_pass, test_count, test_pass * 100.0 / test_count); + + scanf("%d", &n); return main_ret; }