22#include <assert.h> /* assert() */
33#include <stdlib.h> /* NULL */
44
5+ /* 这个EXPECT有副作用啊,而不是单纯地assert,意味着EXPECT不能注释掉。*/
56#define EXPECT (c , ch ) do { assert(*c->json == (ch)); c->json++; } while(0)
67
8+ /* 因为此字符串多次使用,为了减少传递多个参数,故放到一个struct中 */
79typedef struct {
810 const char * json ;
911}lept_context ;
@@ -20,13 +22,49 @@ static int lept_parse_null(lept_context* c, lept_value* v) {
2022 if (c -> json [0 ] != 'u' || c -> json [1 ] != 'l' || c -> json [2 ] != 'l' )
2123 return LEPT_PARSE_INVALID_VALUE ;
2224 c -> json += 3 ;
25+
26+ lept_parse_whitespace (c );
27+ if (c -> json [0 ] != '\0' )
28+ return LEPT_PARSE_ROOT_NOT_SINGULAR ;
29+
2330 v -> type = LEPT_NULL ;
2431 return LEPT_PARSE_OK ;
2532}
2633
34+
35+ static int lept_parse_false (lept_context * c , lept_value * v ) {
36+ EXPECT (c , 'f' );
37+ if (c -> json [0 ] != 'a' || c -> json [1 ] != 'l' || c -> json [2 ] != 's' || c -> json [3 ] != 'e' )
38+ return LEPT_PARSE_INVALID_VALUE ;
39+ c -> json += 4 ;
40+
41+ lept_parse_whitespace (c );
42+ if (c -> json [0 ] != '\0' )
43+ return LEPT_PARSE_ROOT_NOT_SINGULAR ;
44+
45+ v -> type = LEPT_FALSE ;
46+ return LEPT_PARSE_OK ;
47+ }
48+
49+ static int lept_parse_true (lept_context * c , lept_value * v ) {
50+ EXPECT (c , 't' );
51+ if (c -> json [0 ] != 'r' || c -> json [1 ] != 'u' || c -> json [2 ] != 'e' )
52+ return LEPT_PARSE_INVALID_VALUE ;
53+ c -> json += 3 ;
54+
55+ lept_parse_whitespace (c );
56+ if (c -> json [0 ] != '\0' )
57+ return LEPT_PARSE_ROOT_NOT_SINGULAR ;
58+
59+ v -> type = LEPT_TRUE ;
60+ return LEPT_PARSE_OK ;
61+ }
62+
2763static int lept_parse_value (lept_context * c , lept_value * v ) {
2864 switch (* c -> json ) {
2965 case 'n' : return lept_parse_null (c , v );
66+ case 'f' : return lept_parse_false (c , v );
67+ case 't' : return lept_parse_true (c , v );
3068 case '\0' : return LEPT_PARSE_EXPECT_VALUE ;
3169 default : return LEPT_PARSE_INVALID_VALUE ;
3270 }
0 commit comments