Barracuda Application Server C/C++ Reference
NO
JParser.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: JParser.h 5387 2023-02-20 22:50:13Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2006-2023
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://realtimelogic.com
34 ****************************************************************************
35 *
36 */
37
38#ifndef __JParser_h
39#define __JParser_h
40
41
42#include <BaAtoi.h>
43#include <AllocatorIntf.h>
44
49struct JParserIntf;
50struct JParserVal;
51
57typedef int (*JParserIntf_Service)(
58 struct JParserIntf* o, struct JParserVal* v, int recLevel);
59
60
64typedef struct JParserIntf
65{
66#ifdef __cplusplus
70 JParserIntf(JParserIntf_Service s) { service = s; }
71
77 int serviceCB(JParserVal* v, int recLevel);
78
79 ~JParserIntf(){}
80 JParserIntf() {}
81#endif
82 JParserIntf_Service service;
84
85#define JParserIntf_constructor(o,serviceMA) (o)->service=serviceMA
86
87#define JParserIntf_serviceCB(o, v, recLev) (o)->service(o,v,recLev)
88
89#ifdef __cplusplus
90
91inline int JParserIntf::serviceCB(JParserVal* v, int recLevel) {
92 return JParserIntf_serviceCB(this,v, recLevel); }
93#endif
94 /* end of JSONCB */
96
103typedef enum {
128
131typedef enum
132{
151
154typedef struct JErr
155{
156#ifdef __cplusplus
158 JErr();
160 void reset();
162 bool isError();
164 bool noError();
166 JErrT getErrT();
170 JVType getExpT();
174 JVType getRecT();
175 int setTooFewParams();
176 int setTypeErr(JVType expT, JVType recT);
177 int setError(JErrT err,const char* msg);
178#endif
179 const char* msg;
180 JErrT err;
181 JVType expType;
182 JVType recType;
184#ifdef __cplusplus
185extern "C" {
186#endif
187#define JErr_constructor(o) (o)->err=JErrT_NoErr
188#define JErr_reset(o) (o)->err=JErrT_NoErr, (o)->msg=0
189#define JErr_isError(o) (o)->err!=JErrT_NoErr
190#define JErr_noError(o) (o)->err==JErrT_NoErr
191#define JErr_getErrT(o) (o)->err
192#define JErr_getExpT(o) (o)->expType
193#define JErr_getRecT(o) (o)->recType
194BA_API int JErr_setTooFewParams(JErr* o);
195BA_API int JErr_setTypeErr(JErr* o, JVType expT, JVType recT);
196BA_API int JErr_setError(JErr* o,JErrT err,const char* msg);
197#ifdef __cplusplus
198}
199inline JErr::JErr() {
200 JErr_constructor(this); }
201inline void JErr::reset() {
202 JErr_reset(this); }
203inline bool JErr::isError() {
204 return JErr_isError(this) ? true : false;}
205inline bool JErr::noError() {
206 return JErr_noError(this) ? true : false;}
208 return JErr_getErrT(this);}
210 return JErr_getExpT(this);}
212 return JErr_getRecT(this);}
213inline int JErr::setTooFewParams() {
214 return JErr_setTooFewParams(this);}
215inline int JErr::setTypeErr(JVType expT, JVType recT) {
216 return JErr_setTypeErr(this, expT, recT);}
217inline int JErr::setError(JErrT e,const char* message) {
218 return JErr_setError(this,e, message);}
219#endif
220
221
222
223#ifndef __DOXYGEN__
224typedef struct
225{
226 AllocatorIntf* alloc;
227 U8* buf;
228 U32 index;
229 size_t size;
230} JDBuf;
231
232
233/* JLextT: JSON Lexer Types.
234 The following types are used by the lexer and parser.
235*/
236typedef enum {
237 JLexerT_Null,
238 JLexerT_Boolean,
239 JLexerT_Number,
240 JLexerT_String,
241 JLexerT_BeginObject,
242 JLexerT_BeginArray,
243 JLexerT_EndObject,
244 JLexerT_EndArray,
245 JLexerT_Comma, /* ',' Array or object list comma. */
246 JLexerT_MemberSep, /* ':' for string : value */
247 JLexerT_NeedMoreData, /* Lexer not completed with current token. */
248 JLexerT_ParseErr,
249 JLexerT_MemErr
250} JLexerT;
251
252
253
254typedef enum {
255 JLexerSt_StartComment,
256 JLexerSt_EatComment,
257 JLexerSt_EndComment,
258 JLexerSt_EatCppComment,
259 JLexerSt_TrueFalseNull,
260 JLexerSt_String,
261 JLexerSt_StringEscape,
262 JLexerSt_StringUnicode,
263 JLexerSt_Number,
264 JLexerSt_GetNextToken
265} JLexerSt;
266
267
268typedef struct
269{
270 JDBuf* asmB;
271 const U8* bufStart;
272 const U8* bufEnd;
273 const U8* tokenPtr;
274
275 U32 unicode;
276 S16 unicodeShift;
277
278 /* typeChkPtr and retVal is used if the Lexer finds the start of
279 true, false, or null.
280 */
281 const U8* typeChkPtr;
282 U8 retVal;
283
284 U8 state; /* JLexerSt */
285
286 /* state for string or number.
287 If in state number, 0 means positive number and 255 means neg.
288 If in state String, sn is ' or ".
289 If in state Boolean: true or false.
290 */
291 U8 sn;
292 U8 isDouble; /* Used when reading a number */
293} JLexer;
294
295#endif /* __DOXYGEN__ */
296
299typedef enum {
310 JParserT_EndArray = ']'
312
316typedef struct JParserVal
317{
320 union
321 {
322 char* s;
323#ifndef NO_DOUBLE
324 double f;
325#endif
326 S32 d;
327 U64 l;
328 BaBool b;
329 } v;
330
348
349
352typedef enum {
353
357
362
363
366
370
374
378
382
383
384typedef enum {
385 JParserSt_StartObj,
386 JParserSt_BeginArray,
387 JParserSt_MemberName,
388 JParserSt_MemberSep,
389 JParserSt_Value,
390 JParserSt_EndObj,
391 JParserSt_Comma
392} JParserSt;
393
394
395#define JPARSER_STACK_LEN 8
396
399
406{
407#ifdef __cplusplus
422 JParser(JParserIntf* intf, char* nameBuf, int namebufSize,
423 AllocatorIntf* alloc, int extraStackLen=0);
424
435 int parse(const U8* buf, U32 size);
436
438 ~JParser();
439
445#endif
446 JLexer lexer;
447 JParserVal val;
448 JDBuf asmB; /* Assembling various values */
449 JDBuf mnameB; /* Assembling object member names */
450 JParserIntf* intf;
451 S16 stackIx;
452 S16 stackSize;
453 U8 status; /* JParsStat */
454 U8 state; /* JParserSt */
455 /* It's possible to extend the stack size by reserving
456 * N*JParserStackNode bytes immediately following the memory for
457 * this struct instance. N is then used as 'extraStackLen' in constructor.
458 */
459 JParserStackNode stack[JPARSER_STACK_LEN];
460};
461
462typedef struct JParser JParser;
463
464#ifdef __cplusplus
465extern "C" {
466#endif
467BA_API void JParser_constructor(JParser* o, JParserIntf* intf, char* nameBuf,
468 int namebufSize, AllocatorIntf* alloc,
469 int extraStackLen);
470BA_API int JParser_parse(JParser* o, const U8* buf, U32 size);
471BA_API void JParser_destructor(JParser* o);
472#define JParser_getStatus(o) ((JParsStat)(o)->status)
473#ifdef __cplusplus
474}
475inline JParser::JParser(JParserIntf* intf, char* nameBuf,
476 int namebufSize, AllocatorIntf* alloc,
477 int extraStackLen) {
478 JParser_constructor(this, intf, nameBuf, namebufSize, alloc, extraStackLen);
479}
480inline int JParser::parse(const U8* buf, U32 size) {
481 return JParser_parse(this, buf, size);}
483 JParser_destructor(this);}
485 return JParser_getStatus(this);}
486#endif
487 /* end of JSONRef */
489
490#endif
int serviceCB(JParserVal *v, int recLevel)
Calling the JParserIntf service function is typically performed in a design using a chained parser ca...
Definition: JParser.h:91
int(* JParserIntf_Service)(struct JParserIntf *o, struct JParserVal *v, int recLevel)
The parser callback function.
Definition: JParser.h:57
struct JParserIntf JParserIntf
The JParserIntf interface class is the interface between the parser and an object that implements the...
struct JParserVal JParserVal
The parser sets a JParserVal before calling the parser callback JParserIntf.
struct JErr JErr
The JSON error container object.
JParser(JParserIntf *intf, char *nameBuf, int namebufSize, AllocatorIntf *alloc, int extraStackLen=0)
Create a JSON parser object.
Definition: JParser.h:475
JParsStat getStatus()
Returns the parser status.
Definition: JParser.h:484
JParsStat
JSON Parser Status.
Definition: JParser.h:352
JVType getRecT()
Returns the parsed type if this is a parse error.
Definition: JParser.h:211
JErrT
JSON error codes.
Definition: JParser.h:132
~JParser()
Terminate and release the internal buffers.
Definition: JParser.h:482
void reset()
Reset – remove any error so the object can be reused.
Definition: JParser.h:201
bool noError()
Returns true if no error.
Definition: JParser.h:205
JErrT getErrT()
Returns the error type.
Definition: JParser.h:207
int parse(const U8 *buf, U32 size)
Parse a JSON text chunk.
Definition: JParser.h:480
JVType
The JSON types.
Definition: JParser.h:103
JParserT
Type 't' in JParserVal.
Definition: JParser.h:299
JVType getExpT()
Returns the expected type if this is a parse error.
Definition: JParser.h:209
U8 JParserStackNode
The stack used internally by JParser.
Definition: JParser.h:398
JErr()
Create a new object.
Definition: JParser.h:199
bool isError()
Returns true if the error flag is set.
Definition: JParser.h:203
@ JParsStat_ParseErr
JSON Parse error.
Definition: JParser.h:369
@ JParsStat_IntfErr
The JParserIntf interface reported a problem.
Definition: JParser.h:373
@ JParsStat_DoneEOS
The parser completed parsing a new JSON object.
Definition: JParser.h:356
@ JParsStat_NeedMoreData
The parser needs more data to complete.
Definition: JParser.h:365
@ JParsStat_StackOverflow
Object nested too deep.
Definition: JParser.h:380
@ JParsStat_Done
The parser completed parsing a new JSON object, but found the start of a new object in the provided b...
Definition: JParser.h:361
@ JParsStat_MemErr
Memory allocation error.
Definition: JParser.h:377
@ JErrT_InvalidMethodParams
Invalid RPC method parameters.
Definition: JParser.h:142
@ JErrT_NoErr
No error.
Definition: JParser.h:134
@ JErrT_MemErr
Memory allocation error.
Definition: JParser.h:147
@ JErrT_FmtValErr
A format error in JEncoder or JVal::get.
Definition: JParser.h:145
@ JErrT_WrongType
Unexpected JSON type when parsing.
Definition: JParser.h:140
@ JErrT_JsonErr
Generic error.
Definition: JParser.h:136
@ JErrT_IOErr
I/O error.
Definition: JParser.h:149
@ JVType_Null
JSON NULL value.
Definition: JParser.h:122
@ JVType_Int
JSON Number encoded as an integer.
Definition: JParser.h:115
@ JVType_Double
JSON Number encoded as a float.
Definition: JParser.h:112
@ JVType_Object
The JVAL is of type JSON object.
Definition: JParser.h:124
@ JVType_String
JSON string.
Definition: JParser.h:109
@ JVType_Long
JSON Number encoded as a long integer.
Definition: JParser.h:118
@ JVType_Array
The JVAL is of type JSON array.
Definition: JParser.h:126
@ JVType_Boolean
JSON boolean.
Definition: JParser.h:120
@ JVType_InvalidType
Invalid JSON type.
Definition: JParser.h:106
@ JParserT_BeginObject
BeginObject.
Definition: JParser.h:307
@ JParserT_Long
64 bit (long long)
Definition: JParser.h:305
@ JParserT_String
String.
Definition: JParser.h:302
@ JParserT_EndArray
EndArray.
Definition: JParser.h:310
@ JParserT_EndObject
EndObject.
Definition: JParser.h:309
@ JParserT_Boolean
Boolean.
Definition: JParser.h:306
@ JParserT_InvalidType
InvalidType.
Definition: JParser.h:300
@ JParserT_BeginArray
BeginArray.
Definition: JParser.h:308
@ JParserT_Null
Null.
Definition: JParser.h:301
@ JParserT_Double
Double.
Definition: JParser.h:303
@ JParserT_Int
Int.
Definition: JParser.h:304
Memory allocation and deallocation Interface class.
Definition: AllocatorIntf.h:83
The JSON error container object.
Definition: JParser.h:155
The JParserIntf interface class is the interface between the parser and an object that implements the...
Definition: JParser.h:65
JParserIntf(JParserIntf_Service s)
Create the callback interface object.
Definition: JParser.h:70
The parser sets a JParserVal before calling the parser callback JParserIntf.
Definition: JParser.h:317
union JParserVal::@1 v
A union controlled by the type 't'.
JParserT t
The type controlling 'v'.
Definition: JParser.h:346
S32 d
If integer.
Definition: JParser.h:326
char * memberName
object member name is set for objects.
Definition: JParser.h:345
U64 l
If long integer.
Definition: JParser.h:327
double f
If number of type double.
Definition: JParser.h:324
char * s
If string.
Definition: JParser.h:322
The JSON parser parses a JSON stream and calls the JParserIntf callback interface for each parsed obj...
Definition: JParser.h:406