-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleptjsoncpp.cpp
114 lines (99 loc) · 2.59 KB
/
leptjsoncpp.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
#include "leptjsoncpp.h"
#include <cassert>
#include <string>
#include <stdexcept>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
namespace lept
{
#define ISDIGIT1TO9(d) ('1' <= (d) && (d) <= '9')
#define ISDIGIT(d) ('0' <= (d) && (d) <= '9')
double LeptValue::GetNumber() const
{
assert(type == LEPT_NUMBER);
return number;
}
ParseResult LeptParser::Parse(LeptValue& v, LeptContext& j)
{
ParseResult ret;
v.SetType(LeptValue::LEPT_NULL);
ParseWhitespace(j);
if((ret = ParseValue(v, j)) == PARSE_OK) {
ParseWhitespace(j);
if(j.CurrentChar() != '\0') {
v.SetType(LeptValue::LEPT_NULL);
return PARSE_ROOT_NOT_SINGULAR;
}
}
return ret;
}
ParseResult LeptParser::ParseValue(LeptValue& v, LeptContext& j)
{
switch(j.CurrentChar())
{
case 'n': return ParseLiteral(v, j, "null", LeptValue::LEPT_NULL);
case 't': return ParseLiteral(v, j, "true", LeptValue::LEPT_TRUE);
case 'f': return ParseLiteral(v, j, "false", LeptValue::LEPT_FALSE);
default: return ParseNumber(v, j);
case '\0': return PARSE_EXPECT_VALUE;
}
}
ParseResult LeptParser::ParseNumber(LeptValue &v, LeptContext &j)
{
const char *p = j.CurrentJson();
if(*p == '-')
p++;
if(*p == '0')
p++;
else {
if(!ISDIGIT1TO9(*p)) return PARSE_INVALID_VALUE;
for(p++; ISDIGIT(*p); p++)
;
}
if(*p == '.') {
p++;
if(!ISDIGIT(*p)) return PARSE_INVALID_VALUE;
for(p++; ISDIGIT(*p); p++)
;
}
if(*p == 'e' || *p == 'E') {
p++;
if(*p == '+' || *p == '-')
p++;
if(!ISDIGIT(*p)) return PARSE_INVALID_VALUE;
for(p++; ISDIGIT(*p); p++)
;
}
errno = 0;
//use strtod not stod
double d = strtod(j.CurrentJson(), NULL);
if(errno == ERANGE && (d == HUGE_VAL || d == -HUGE_VAL))
return PARSE_NUMBER_TOO_BIG;
j += (p - j.CurrentJson());
v.SetNumber(d);
v.SetType(LeptValue::LEPT_NUMBER);
return PARSE_OK;
}
ParseResult LeptParser::ParseLiteral(LeptValue &v, LeptContext &j,
const string &literal, LeptValue::LeptType type)
{
size_t len = literal.size();
if(j.GetOneToken(len) != literal) {
return PARSE_INVALID_VALUE;
}
j += len;
v.SetType(type);
return PARSE_OK;
}
void LeptParser::ParseWhitespace(LeptContext& j)
{
while(j.CurrentChar() == ' ' ||
j.CurrentChar() == '\t' ||
j.CurrentChar() == '\n')
{
j += 1;
}
}
}