-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleptjsoncpp.h
More file actions
167 lines (134 loc) · 2.79 KB
/
leptjsoncpp.h
File metadata and controls
167 lines (134 loc) · 2.79 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#pragma once
#include <string>
#include <cstdlib>
namespace lept
{
typedef enum {
PARSE_OK = 0,
PARSE_EXPECT_VALUE,
PARSE_INVALID_VALUE,
PARSE_ROOT_NOT_SINGULAR,
PARSE_NUMBER_TOO_BIG
}ParseResult;
#define MAX_STACK_SIZE (512)
class LeptStack
{
public:
LeptStack()
:p_base((char*)(malloc(MAX_STACK_SIZE))),
top(0),
capacity(MAX_STACK_SIZE)
{
}
~LeptStack()
{
if((p_base != nullptr) &&
(top == 0)) {
std::free(p_base);
}
}
LeptStack(const LeptStack&) = delete;
LeptStack(LeptStack&&) = delete;
LeptStack& operator=(const LeptStack&) = delete;
LeptStack& operator=(LeptStack&&) = delete;
template<typename T>
T* Push()
{
T* p = new (p_base+top) T();
if(p != nullptr) {
top += sizeof(T);
}
return p;
}
template<typename T>
T* Pop()
{
top -= sizeof(T);
return Top<T>();
}
template<typename T>
T* Top() const
{
return reinterpret_cast<T*>(p_base + top);
}
private:
char *p_base;
std::size_t top;
std::size_t capacity;
};
class LeptContext
{
public:
LeptContext(const char* j)
:json(j), offset(0)
{
}
LeptContext& operator=(const char* nj)
{
json = nj;
offset = 0;
return *this;
}
LeptContext& operator+=(std::size_t adv)
{
offset += adv;
return *this;
}
std::string GetOneToken(std::size_t count) const {
return std::string(CurrentJson(), count);
}
char CurrentChar() const {
return *CurrentJson();
}
const char* CurrentJson() const{
return json + offset;
}
const char* OriginJson() const {
return json;
}
private:
const char* json;
std::size_t offset;
};
class LeptValue
{
public:
typedef enum {
LEPT_NULL,
LEPT_FALSE,
LEPT_TRUE,
LEPT_NUMBER,
LEPT_STRING,
LEPT_ARRAY,
LEPT_OBJECT
}LeptType;
LeptValue(LeptType t = LEPT_NULL)
:type(t)
{
}
LeptType GetType() const {
return type;
}
void SetType(LeptType t) {
type = t;
}
double GetNumber() const;
void SetNumber(double num) {
number = num;
}
private:
LeptType type;
double number;
};
class LeptParser
{
public:
static ParseResult Parse(LeptValue &v, LeptContext &j);
private:
static ParseResult ParseValue(LeptValue &v, LeptContext &j);
static ParseResult ParseNumber(LeptValue &v, LeptContext &j);
static ParseResult ParseLiteral(LeptValue &v, LeptContext &j,
const std::string& literal, LeptValue::LeptType type);
static void ParseWhitespace(LeptContext &j);
};
}