forked from Digilent/openscope-mz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexJSON.h
163 lines (142 loc) · 5.53 KB
/
LexJSON.h
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
/************************************************************************/
/* */
/* LexJSON.h */
/* */
/* Header for the JSON parser */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2016, Digilent Inc. */
/************************************************************************/
/*
*
* Copyright (c) 2013-2016, Digilent <www.digilentinc.com>
* Contact Digilent for the latest version.
*
* This program is free software; distributed under the terms of
* BSD 3-clause license ("Revised BSD License", "New BSD License", or "Modified BSD License")
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name(s) of the above-listed copyright holder(s) nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/************************************************************************/
/* Revision History: */
/* 7/11/2016(KeithV): Created */
/************************************************************************/
#ifndef JSONLex_h
#define JSONLex_h
#ifdef __cplusplus
class JSONCallBack
{
public:
typedef enum
{
tokNone = 0,
tokFalse,
tokNull,
tokTrue,
tokObject,
tokEndObject,
tokArray,
tokEndArray,
tokNumber,
tokMemberName,
tokStringValue,
tokNameSep,
tokValueSep,
tokJSONSyntaxError,
tokLexingError,
tokEndOfJSON,
tokAny
} JSONTOKEN;
virtual STATE ParseToken(char const * const szToken, uint32_t cbToken, JSONTOKEN jsonToken) = 0;
};
class JSON : public JSONCallBack
{
private:
static const uint32_t maxAggregate = 80; // depth is 1/2 == 40
static const uint32_t maxObjArray = 127; //int8_t positive number
static const uint32_t cAny = 0xFF;
typedef enum
{
parValue,
parName,
parValueSep,
parNameSep,
parArray
} PARCD;
STATE state;
STATE tokenLexState;
PARCD parState;
JSONTOKEN jsonToken;
int32_t cbToken;
int32_t cZero;
uint32_t iAggregate; // even object, odd array
int8_t aggregate[maxAggregate]; // even object cnt, odd array cnt
uint8_t fNegative;
uint8_t fFractional;
uint8_t fExponent;
uint8_t fNegativeExponent;
int32_t iInput;
int32_t cbTokenBuff;
int32_t iTokenBuff;
char szTokenBuff[0x100]; // for tokens
char const * SkipWhite(char const * sz, uint32_t cbsz);
protected:
bool IsWhite(char const ch);
STATE tokenErrorState;
char const * szMoveInput;
public:
JSON()
{
szMoveInput = NULL;
cbToken = 0;
Init();
}
void Init(void)
{
state = Idle;
tokenLexState = Idle;
tokenErrorState = Idle;
parState = parValue;
jsonToken = tokNone;
cbToken = 0;
cZero = 0;
iAggregate = 0;
fNegative = false;
fFractional = false;
fExponent = false;
fNegativeExponent = false;
memset(aggregate, 0, sizeof(aggregate));
iInput = 0;
cbTokenBuff = 0;
iTokenBuff = 0;
// this is so the HTTP code can move past the token
// even when done parsing the JSON
szMoveInput += cbToken;
cbToken = 0;
}
GCMD::ACTION LexJSON(char const * szInput, int32_t cbInput, int32_t& cbConsumed);
};
#endif
#endif