-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.cpp
147 lines (128 loc) · 2.61 KB
/
json.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Recursive-Decent Grammar Parser 4 Json
Author: xiatianxiao@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string>
#include <iostream>
/*
json_object : { name_pair }
name_pair : name_value | name_pair , name_value
name_value : str : json_value
json_value : json_jobject | json_array
: str | NUM | TRUE | FALSE | NULL
json_array : [ some_value ]
some_value : json_value | some_value , json_value
*/
const char* input_str = "{ \"name\" : \"skysmiler\", \"data\":[\"value\"]}";
static char look_ahead;
char read_next() {
static int pc = 0;
return input_str[pc++];
}
void match_space(){
do{
look_ahead = read_next();
}while(look_ahead == ' ' || look_ahead == '\t');
}
void match(char c) {
printf("try match %c == %c\n", look_ahead, c);
assert(look_ahead == c);
match_space();
}
std::string match_str(){
char buf[128];
assert(look_ahead == '\"');
memset(buf, 0, sizeof(buf));
int i = 0;
do {
look_ahead = read_next();
buf[i++] = look_ahead;
}while(look_ahead != '\"');
buf[i-1] = 0;
match_space();
return std::string(buf);
}
int match_bool(char c){
int ret;
assert(c == 't' || c == 'f');
if(c == 't'){
ret = 1;
}else if(c == 'f'){
ret = 0;
}
match_space();
return ret;
}
double match_digit(){
char buf[128];
int i = 0;
do {
look_ahead = read_next();
buf[i++] = look_ahead;
}while((look_ahead <='9' && look_ahead >='0') || look_ahead=='.');
buf[i] = 0;
match_space();
return atof(buf);
}
void name_value();
void name_pair();
void json_value();
void json_array();
void some_value();
void json_object () {
match('{');name_pair(); match('}');
}
void name_pair_(){
if(look_ahead == ',') {
match(',');name_value();name_pair_();
}else
;
}
void name_pair() {
name_value();name_pair_();
}
void name_value() {
std::string name;
name = match_str();match(':');json_value();
std::cout << "Get Name: " << name << std::endl;
}
void json_value() {
if(look_ahead == '{')
json_object();
else if(look_ahead == '[')
json_array();
else if(look_ahead == '\"'){
std::string d = match_str();
std::cout << "Get Value: " << d <<std::endl;
}else{
// number,true, false, null
if(look_ahead == '\"')
match_str();
else if(look_ahead == 't' || look_ahead == 'f'){
match_bool(look_ahead);
}else if(look_ahead<='9' && look_ahead >='0'){
match_digit();
}else
;
}
}
void json_array() {
match('['); some_value();match(']');
}
void some_value_() {
if(look_ahead == ','){
match(',');json_value();some_value_();
}else
;
}
void some_value() {
json_value();some_value_();
}
int main(){
look_ahead = read_next();
json_object();
printf("%s\n", input_str);
}