PostgreSQL Source Code git master
Loading...
Searching...
No Matches
jsonapi.c File Reference
#include "postgres.h"
#include "common/jsonapi.h"
#include "mb/pg_wchar.h"
#include "port/pg_lfind.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
Include dependency graph for jsonapi.c:

Go to the source code of this file.

Data Structures

struct  JsonParserStack
 
struct  JsonIncrementalState
 
struct  td_entry
 

Macros

#define STRDUP(s)   pstrdup(s)
 
#define ALLOC(size)   palloc(size)
 
#define ALLOC0(size)   palloc0(size)
 
#define REALLOC   repalloc
 
#define FREE(s)
 
#define jsonapi_appendStringInfo   appendStringInfo
 
#define jsonapi_appendBinaryStringInfo   appendBinaryStringInfo
 
#define jsonapi_appendStringInfoChar   appendStringInfoChar
 
#define jsonapi_appendStringInfoCharMacro   appendStringInfoCharMacro
 
#define jsonapi_makeStringInfo   makeStringInfo
 
#define jsonapi_initStringInfo   initStringInfo
 
#define jsonapi_resetStringInfo   resetStringInfo
 
#define jsonapi_termStringInfo(s)   pfree((s)->data)
 
#define jsonapi_destroyStringInfo   destroyStringInfo
 
#define JSON_NUM_TERMINALS   13
 
#define JSON_NUM_NONTERMINALS   5
 
#define JSON_NT_OFFSET   JSON_NT_JSON
 
#define OFS(NT)   (NT) - JSON_NT_OFFSET
 
#define IS_SEM(x)   ((x) & 0x40)
 
#define IS_NT(x)   ((x) & 0x20)
 
#define TD_ENTRY(PROD)   { sizeof(PROD) - 1, (PROD) }
 
#define JSON_ALPHANUMERIC_CHAR(c)
 
#define JS_STACK_CHUNK_SIZE   64
 
#define JS_MAX_PROD_LEN   10 /* more than we need */
 
#define JSON_TD_MAX_STACK
 
#define FAIL_OR_INCOMPLETE_AT_CHAR_START(code)
 
#define FAIL_AT_CHAR_END(code)
 
#define json_token_error(lex, format)
 

Enumerations

enum  JsonParseContext {
  JSON_PARSE_VALUE , JSON_PARSE_STRING , JSON_PARSE_ARRAY_START , JSON_PARSE_ARRAY_NEXT ,
  JSON_PARSE_OBJECT_START , JSON_PARSE_OBJECT_LABEL , JSON_PARSE_OBJECT_NEXT , JSON_PARSE_OBJECT_COMMA ,
  JSON_PARSE_END
}
 
enum  JsonNonTerminal {
  JSON_NT_JSON = 32 , JSON_NT_ARRAY_ELEMENTS , JSON_NT_MORE_ARRAY_ELEMENTS , JSON_NT_KEY_PAIRS ,
  JSON_NT_MORE_KEY_PAIRS
}
 
enum  JsonParserSem {
  JSON_SEM_OSTART = 64 , JSON_SEM_OEND , JSON_SEM_ASTART , JSON_SEM_AEND ,
  JSON_SEM_OFIELD_INIT , JSON_SEM_OFIELD_START , JSON_SEM_OFIELD_END , JSON_SEM_AELEM_START ,
  JSON_SEM_AELEM_END , JSON_SEM_SCALAR_INIT , JSON_SEM_SCALAR_CALL
}
 

Functions

static JsonParseErrorType json_lex_string (JsonLexContext *lex)
 
static JsonParseErrorType json_lex_number (JsonLexContext *lex, const char *s, bool *num_err, size_t *total_len)
 
static JsonParseErrorType parse_scalar (JsonLexContext *lex, const JsonSemAction *sem)
 
static JsonParseErrorType parse_object_field (JsonLexContext *lex, const JsonSemAction *sem)
 
static JsonParseErrorType parse_object (JsonLexContext *lex, const JsonSemAction *sem)
 
static JsonParseErrorType parse_array_element (JsonLexContext *lex, const JsonSemAction *sem)
 
static JsonParseErrorType parse_array (JsonLexContext *lex, const JsonSemAction *sem)
 
static JsonParseErrorType report_parse_error (JsonParseContext ctx, JsonLexContext *lex)
 
static bool allocate_incremental_state (JsonLexContext *lex)
 
static void set_fname (JsonLexContext *lex, char *fname)
 
static JsonTokenType lex_peek (JsonLexContext *lex)
 
static JsonParseErrorType lex_expect (JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
 
bool IsValidJsonNumber (const char *str, size_t len)
 
JsonLexContextmakeJsonLexContextCstringLen (JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
 
JsonLexContextmakeJsonLexContextIncremental (JsonLexContext *lex, int encoding, bool need_escapes)
 
void setJsonLexContextOwnsTokens (JsonLexContext *lex, bool owned_by_context)
 
static bool inc_lex_level (JsonLexContext *lex)
 
static void dec_lex_level (JsonLexContext *lex)
 
static void push_prediction (JsonParserStack *pstack, td_entry entry)
 
static char pop_prediction (JsonParserStack *pstack)
 
static char next_prediction (JsonParserStack *pstack)
 
static bool have_prediction (JsonParserStack *pstack)
 
static charget_fname (JsonLexContext *lex)
 
static void set_fnull (JsonLexContext *lex, bool fnull)
 
static bool get_fnull (JsonLexContext *lex)
 
void freeJsonLexContext (JsonLexContext *lex)
 
JsonParseErrorType pg_parse_json (JsonLexContext *lex, const JsonSemAction *sem)
 
JsonParseErrorType json_count_array_elements (JsonLexContext *lex, int *elements)
 
JsonParseErrorType pg_parse_json_incremental (JsonLexContext *lex, const JsonSemAction *sem, const char *json, size_t len, bool is_last)
 
JsonParseErrorType json_lex (JsonLexContext *lex)
 
charjson_errdetail (JsonParseErrorType error, JsonLexContext *lex)
 

Variables

static char JSON_PROD_EPSILON [] = {0}
 
static char JSON_PROD_SCALAR_STRING [] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_STRING, JSON_SEM_SCALAR_INIT, 0}
 
static char JSON_PROD_SCALAR_NUMBER [] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_NUMBER, JSON_SEM_SCALAR_INIT, 0}
 
static char JSON_PROD_SCALAR_TRUE [] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_TRUE, JSON_SEM_SCALAR_INIT, 0}
 
static char JSON_PROD_SCALAR_FALSE [] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_FALSE, JSON_SEM_SCALAR_INIT, 0}
 
static char JSON_PROD_SCALAR_NULL [] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_NULL, JSON_SEM_SCALAR_INIT, 0}
 
static char JSON_PROD_OBJECT [] = {JSON_SEM_OEND, JSON_TOKEN_OBJECT_END, JSON_NT_KEY_PAIRS, JSON_TOKEN_OBJECT_START, JSON_SEM_OSTART, 0}
 
static char JSON_PROD_ARRAY [] = {JSON_SEM_AEND, JSON_TOKEN_ARRAY_END, JSON_NT_ARRAY_ELEMENTS, JSON_TOKEN_ARRAY_START, JSON_SEM_ASTART, 0}
 
static char JSON_PROD_ARRAY_ELEMENTS [] = {JSON_NT_MORE_ARRAY_ELEMENTS, JSON_SEM_AELEM_END, JSON_NT_JSON, JSON_SEM_AELEM_START, 0}
 
static char JSON_PROD_MORE_ARRAY_ELEMENTS [] = {JSON_NT_MORE_ARRAY_ELEMENTS, JSON_SEM_AELEM_END, JSON_NT_JSON, JSON_SEM_AELEM_START, JSON_TOKEN_COMMA, 0}
 
static char JSON_PROD_KEY_PAIRS [] = {JSON_NT_MORE_KEY_PAIRS, JSON_SEM_OFIELD_END, JSON_NT_JSON, JSON_SEM_OFIELD_START, JSON_TOKEN_COLON, JSON_TOKEN_STRING, JSON_SEM_OFIELD_INIT, 0}
 
static char JSON_PROD_MORE_KEY_PAIRS [] = {JSON_NT_MORE_KEY_PAIRS, JSON_SEM_OFIELD_END, JSON_NT_JSON, JSON_SEM_OFIELD_START, JSON_TOKEN_COLON, JSON_TOKEN_STRING, JSON_SEM_OFIELD_INIT, JSON_TOKEN_COMMA, 0}
 
static td_entry td_parser_table [JSON_NUM_NONTERMINALS][JSON_NUM_TERMINALS]
 
static char JSON_PROD_GOAL [] = {JSON_TOKEN_END, JSON_NT_JSON, 0}
 
const JsonSemAction nullSemAction
 
static JsonLexContext failed_oom
 
static JsonIncrementalState failed_inc_oom
 

Macro Definition Documentation

◆ ALLOC

#define ALLOC (   size)    palloc(size)

Definition at line 57 of file jsonapi.c.

◆ ALLOC0

#define ALLOC0 (   size)    palloc0(size)

Definition at line 58 of file jsonapi.c.

◆ FAIL_AT_CHAR_END

#define FAIL_AT_CHAR_END (   code)
Value:
do { \
ptrdiff_t remaining = end - s; \
int charlen; \
charlen = pg_encoding_mblen_or_incomplete(lex->input_encoding, \
s, remaining); \
lex->token_terminator = (charlen <= remaining) ? s + charlen : end; \
return code; \
} while (0)
int remaining
Definition informix.c:692
static int fb(int x)
int pg_encoding_mblen_or_incomplete(int encoding, const char *mbstr, size_t remaining)
Definition wchar.c:1946

◆ FAIL_OR_INCOMPLETE_AT_CHAR_START

#define FAIL_OR_INCOMPLETE_AT_CHAR_START (   code)
Value:
do { \
if (lex->incremental && !lex->inc_state->is_last_chunk) \
{ \
jsonapi_appendBinaryStringInfo(&lex->inc_state->partial_token, \
lex->token_start, \
end - lex->token_start); \
return JSON_INCOMPLETE; \
} \
lex->token_terminator = s; \
return code; \
} while (0)
@ JSON_INCOMPLETE
Definition jsonapi.h:37

◆ FREE

#define FREE (   s)
Value:
do { \
void *__v = (s); \
} while (0)

Definition at line 69 of file jsonapi.c.

69 { \
70 void *__v = (s); \
71 if (__v) \
72 pfree(__v); \
73} while (0)

◆ IS_NT

#define IS_NT (   x)    ((x) & 0x20)

Definition at line 180 of file jsonapi.c.

◆ IS_SEM

#define IS_SEM (   x)    ((x) & 0x40)

Definition at line 179 of file jsonapi.c.

◆ JS_MAX_PROD_LEN

#define JS_MAX_PROD_LEN   10 /* more than we need */

Definition at line 430 of file jsonapi.c.

◆ JS_STACK_CHUNK_SIZE

#define JS_STACK_CHUNK_SIZE   64

Definition at line 429 of file jsonapi.c.

◆ JSON_ALPHANUMERIC_CHAR

#define JSON_ALPHANUMERIC_CHAR (   c)
Value:
(((c) >= 'a' && (c) <= 'z') || \
((c) >= 'A' && (c) <= 'Z') || \
((c) >= '0' && (c) <= '9') || \
(c) == '_' || \
char * c

Definition at line 326 of file jsonapi.c.

◆ JSON_NT_OFFSET

#define JSON_NT_OFFSET   JSON_NT_JSON

Definition at line 175 of file jsonapi.c.

◆ JSON_NUM_NONTERMINALS

#define JSON_NUM_NONTERMINALS   5

Definition at line 174 of file jsonapi.c.

◆ JSON_NUM_TERMINALS

#define JSON_NUM_TERMINALS   13

Definition at line 173 of file jsonapi.c.

◆ JSON_TD_MAX_STACK

#define JSON_TD_MAX_STACK
Value:
6400 /* hard coded for now - this is a REALLY high
* number */

Definition at line 431 of file jsonapi.c.

◆ json_token_error

#define json_token_error (   lex,
  format 
)
Value:
jsonapi_appendStringInfo((lex)->errormsg, _(format), \
(int) ((lex)->token_terminator - (lex)->token_start), \
(lex)->token_start);
#define _(x)
Definition elog.c:96
#define jsonapi_appendStringInfo
Definition jsonapi.c:76
static char format
char * token_start
Definition pgc.l:66

◆ jsonapi_appendBinaryStringInfo

#define jsonapi_appendBinaryStringInfo   appendBinaryStringInfo

Definition at line 77 of file jsonapi.c.

◆ jsonapi_appendStringInfo

#define jsonapi_appendStringInfo   appendStringInfo

Definition at line 76 of file jsonapi.c.

◆ jsonapi_appendStringInfoChar

#define jsonapi_appendStringInfoChar   appendStringInfoChar

Definition at line 78 of file jsonapi.c.

◆ jsonapi_appendStringInfoCharMacro

#define jsonapi_appendStringInfoCharMacro   appendStringInfoCharMacro

Definition at line 79 of file jsonapi.c.

◆ jsonapi_destroyStringInfo

#define jsonapi_destroyStringInfo   destroyStringInfo

Definition at line 84 of file jsonapi.c.

◆ jsonapi_initStringInfo

#define jsonapi_initStringInfo   initStringInfo

Definition at line 81 of file jsonapi.c.

◆ jsonapi_makeStringInfo

#define jsonapi_makeStringInfo   makeStringInfo

Definition at line 80 of file jsonapi.c.

◆ jsonapi_resetStringInfo

#define jsonapi_resetStringInfo   resetStringInfo

Definition at line 82 of file jsonapi.c.

◆ jsonapi_termStringInfo

#define jsonapi_termStringInfo (   s)    pfree((s)->data)

Definition at line 83 of file jsonapi.c.

◆ OFS

#define OFS (   NT)    (NT) - JSON_NT_OFFSET

Definition at line 177 of file jsonapi.c.

◆ REALLOC

#define REALLOC   repalloc

Definition at line 59 of file jsonapi.c.

◆ STRDUP

#define STRDUP (   s)    pstrdup(s)

Definition at line 56 of file jsonapi.c.

◆ TD_ENTRY

#define TD_ENTRY (   PROD)    { sizeof(PROD) - 1, (PROD) }

Definition at line 239 of file jsonapi.c.

Enumeration Type Documentation

◆ JsonNonTerminal

Enumerator
JSON_NT_JSON 
JSON_NT_ARRAY_ELEMENTS 
JSON_NT_MORE_ARRAY_ELEMENTS 
JSON_NT_KEY_PAIRS 
JSON_NT_MORE_KEY_PAIRS 

Definition at line 113 of file jsonapi.c.

114{
115 JSON_NT_JSON = 32,
120};
@ JSON_NT_JSON
Definition jsonapi.c:115
@ JSON_NT_MORE_ARRAY_ELEMENTS
Definition jsonapi.c:117
@ JSON_NT_MORE_KEY_PAIRS
Definition jsonapi.c:119
@ JSON_NT_ARRAY_ELEMENTS
Definition jsonapi.c:116
@ JSON_NT_KEY_PAIRS
Definition jsonapi.c:118

◆ JsonParseContext

Enumerator
JSON_PARSE_VALUE 
JSON_PARSE_STRING 
JSON_PARSE_ARRAY_START 
JSON_PARSE_ARRAY_NEXT 
JSON_PARSE_OBJECT_START 
JSON_PARSE_OBJECT_LABEL 
JSON_PARSE_OBJECT_NEXT 
JSON_PARSE_OBJECT_COMMA 
JSON_PARSE_END 

Definition at line 93 of file jsonapi.c.

94{
95 JSON_PARSE_VALUE, /* expecting a value */
96 JSON_PARSE_STRING, /* expecting a string (for a field name) */
97 JSON_PARSE_ARRAY_START, /* saw '[', expecting value or ']' */
98 JSON_PARSE_ARRAY_NEXT, /* saw array element, expecting ',' or ']' */
99 JSON_PARSE_OBJECT_START, /* saw '{', expecting label or '}' */
100 JSON_PARSE_OBJECT_LABEL, /* saw object label, expecting ':' */
101 JSON_PARSE_OBJECT_NEXT, /* saw object value, expecting ',' or '}' */
102 JSON_PARSE_OBJECT_COMMA, /* saw object ',', expecting next label */
103 JSON_PARSE_END, /* saw the end of a document, expect nothing */
JsonParseContext
Definition jsonapi.c:94
@ JSON_PARSE_OBJECT_LABEL
Definition jsonapi.c:100
@ JSON_PARSE_VALUE
Definition jsonapi.c:95
@ JSON_PARSE_OBJECT_START
Definition jsonapi.c:99
@ JSON_PARSE_ARRAY_START
Definition jsonapi.c:97
@ JSON_PARSE_END
Definition jsonapi.c:103
@ JSON_PARSE_OBJECT_NEXT
Definition jsonapi.c:101
@ JSON_PARSE_ARRAY_NEXT
Definition jsonapi.c:98
@ JSON_PARSE_OBJECT_COMMA
Definition jsonapi.c:102
@ JSON_PARSE_STRING
Definition jsonapi.c:96

◆ JsonParserSem

Enumerator
JSON_SEM_OSTART 
JSON_SEM_OEND 
JSON_SEM_ASTART 
JSON_SEM_AEND 
JSON_SEM_OFIELD_INIT 
JSON_SEM_OFIELD_START 
JSON_SEM_OFIELD_END 
JSON_SEM_AELEM_START 
JSON_SEM_AELEM_END 
JSON_SEM_SCALAR_INIT 
JSON_SEM_SCALAR_CALL 

Definition at line 122 of file jsonapi.c.

123{
124 JSON_SEM_OSTART = 64,
135};
@ JSON_SEM_SCALAR_CALL
Definition jsonapi.c:134
@ JSON_SEM_OSTART
Definition jsonapi.c:124
@ JSON_SEM_AELEM_START
Definition jsonapi.c:131
@ JSON_SEM_AELEM_END
Definition jsonapi.c:132
@ JSON_SEM_SCALAR_INIT
Definition jsonapi.c:133
@ JSON_SEM_ASTART
Definition jsonapi.c:126
@ JSON_SEM_OFIELD_INIT
Definition jsonapi.c:128
@ JSON_SEM_OFIELD_END
Definition jsonapi.c:130
@ JSON_SEM_OEND
Definition jsonapi.c:125
@ JSON_SEM_OFIELD_START
Definition jsonapi.c:129
@ JSON_SEM_AEND
Definition jsonapi.c:127

Function Documentation

◆ allocate_incremental_state()

static bool allocate_incremental_state ( JsonLexContext lex)
static

Definition at line 433 of file jsonapi.c.

435{
436 void *pstack,
437 *prediction,
438 *fnames,
439 *fnull;
440
441 lex->inc_state = ALLOC0(sizeof(JsonIncrementalState));
442 pstack = ALLOC0(sizeof(JsonParserStack));
444 fnames = ALLOC(JS_STACK_CHUNK_SIZE * sizeof(char *));
445 fnull = ALLOC(JS_STACK_CHUNK_SIZE * sizeof(bool));
446
447#ifdef JSONAPI_USE_PQEXPBUFFER
448 if (!lex->inc_state
449 || !pstack
450 || !prediction
451 || !fnames
452 || !fnull)
453 {
454 FREE(lex->inc_state);
455 FREE(pstack);
456 FREE(prediction);
457 FREE(fnames);
458 FREE(fnull);
459
461 return false;
462 }
463#endif
464
466 lex->pstack = pstack;
468 lex->pstack->prediction = prediction;
469 lex->pstack->fnames = fnames;
470 lex->pstack->fnull = fnull;
471
472 /*
473 * fnames between 0 and lex_level must always be defined so that
474 * freeJsonLexContext() can handle them safely. inc/dec_lex_level() handle
475 * the rest.
476 */
477 Assert(lex->lex_level == 0);
478 lex->pstack->fnames[0] = NULL;
479
480 lex->incremental = true;
481 return true;
#define Assert(condition)
Definition c.h:943
#define JS_MAX_PROD_LEN
Definition jsonapi.c:430
#define ALLOC0(size)
Definition jsonapi.c:58
#define jsonapi_initStringInfo
Definition jsonapi.c:81
#define JS_STACK_CHUNK_SIZE
Definition jsonapi.c:429
static JsonIncrementalState failed_inc_oom
Definition jsonapi.c:295
#define ALLOC(size)
Definition jsonapi.c:57
#define FREE(s)
Definition jsonapi.c:69
jsonapi_StrValType partial_token
Definition jsonapi.c:167
JsonParserStack * pstack
Definition jsonapi.h:114
JsonIncrementalState * inc_state
Definition jsonapi.h:115
bool incremental
Definition jsonapi.h:108
char * prediction
Definition jsonapi.c:147
char ** fnames
Definition jsonapi.c:150

References ALLOC, ALLOC0, Assert, failed_inc_oom, fb(), JsonParserStack::fnames, JsonParserStack::fnull, FREE, JsonLexContext::inc_state, JsonLexContext::incremental, JS_MAX_PROD_LEN, JS_STACK_CHUNK_SIZE, jsonapi_initStringInfo, JsonLexContext::lex_level, JsonIncrementalState::partial_token, JsonParserStack::prediction, JsonLexContext::pstack, and JsonParserStack::stack_size.

Referenced by makeJsonLexContextIncremental(), and pg_parse_json().

◆ dec_lex_level()

static void dec_lex_level ( JsonLexContext lex)
inlinestatic

Definition at line 613 of file jsonapi.c.

615{
616 set_fname(lex, NULL); /* free the current level's fname, if needed */
617 lex->lex_level -= 1;
static void set_fname(JsonLexContext *lex, char *fname)
Definition jsonapi.c:647

References fb(), JsonLexContext::lex_level, and set_fname().

Referenced by pg_parse_json_incremental().

◆ freeJsonLexContext()

void freeJsonLexContext ( JsonLexContext lex)

Definition at line 687 of file jsonapi.c.

689{
690 static const JsonLexContext empty = {0};
691
692 if (!lex || lex == &failed_oom)
693 return;
694
695 if (lex->flags & JSONLEX_FREE_STRVAL)
697
698 if (lex->errormsg)
700
701 if (lex->incremental)
702 {
704 FREE(lex->inc_state);
705 FREE(lex->pstack->prediction);
706
708 {
709 int i;
710
711 /* Clean up any tokens that were left behind. */
712 for (i = 0; i <= lex->lex_level; i++)
713 FREE(lex->pstack->fnames[i]);
714 }
715
716 FREE(lex->pstack->fnames);
717 FREE(lex->pstack->fnull);
718 FREE(lex->pstack->scalar_val);
719 FREE(lex->pstack);
720 }
721
722 if (lex->flags & JSONLEX_FREE_STRUCT)
723 FREE(lex);
724 else
725 *lex = empty;
int i
Definition isn.c:77
#define jsonapi_destroyStringInfo
Definition jsonapi.c:84
static JsonLexContext failed_oom
Definition jsonapi.c:294
#define jsonapi_termStringInfo(s)
Definition jsonapi.c:83
#define JSONLEX_FREE_STRVAL
Definition jsonapi.h:98
#define JSONLEX_FREE_STRUCT
Definition jsonapi.h:97
#define JSONLEX_CTX_OWNS_TOKENS
Definition jsonapi.h:99
uint32 flags
Definition jsonapi.h:111
struct jsonapi_StrValType * strval
Definition jsonapi.h:117
struct jsonapi_StrValType * errormsg
Definition jsonapi.h:118
char * scalar_val
Definition jsonapi.c:153

References JsonLexContext::errormsg, failed_oom, JsonLexContext::flags, JsonParserStack::fnames, JsonParserStack::fnull, FREE, i, JsonLexContext::inc_state, JsonLexContext::incremental, jsonapi_destroyStringInfo, jsonapi_termStringInfo, JSONLEX_CTX_OWNS_TOKENS, JSONLEX_FREE_STRUCT, JSONLEX_FREE_STRVAL, JsonLexContext::lex_level, JsonIncrementalState::partial_token, JsonParserStack::prediction, JsonLexContext::pstack, JsonParserStack::scalar_val, and JsonLexContext::strval.

Referenced by datum_to_jsonb_internal(), each_worker(), elements_worker(), get_json_object_as_hash(), get_worker(), handle_oauth_sasl_error(), iterate_json_values(), json_object_keys(), json_parse_manifest(), json_parse_manifest_incremental_shutdown(), json_validate(), main(), parse_oauth_json(), pg_dependencies_in(), pg_ndistinct_in(), populate_array_json(), populate_recordset_worker(), test_gb18030_json(), and transform_json_string_values().

◆ get_fname()

static char * get_fname ( JsonLexContext lex)
inlinestatic

Definition at line 662 of file jsonapi.c.

664{
665 return lex->pstack->fnames[lex->lex_level];

References JsonParserStack::fnames, JsonLexContext::lex_level, and JsonLexContext::pstack.

Referenced by pg_parse_json_incremental().

◆ get_fnull()

static bool get_fnull ( JsonLexContext lex)
inlinestatic

Definition at line 674 of file jsonapi.c.

676{
677 return lex->pstack->fnull[lex->lex_level];

References JsonParserStack::fnull, JsonLexContext::lex_level, and JsonLexContext::pstack.

Referenced by pg_parse_json_incremental().

◆ have_prediction()

static bool have_prediction ( JsonParserStack pstack)
inlinestatic

Definition at line 641 of file jsonapi.c.

643{
644 return pstack->pred_index > 0;
size_t pred_index
Definition jsonapi.c:148

References JsonParserStack::pred_index.

Referenced by pg_parse_json_incremental().

◆ inc_lex_level()

static bool inc_lex_level ( JsonLexContext lex)
inlinestatic

Definition at line 561 of file jsonapi.c.

563{
564 if (lex->incremental && (lex->lex_level + 1) >= lex->pstack->stack_size)
565 {
566 size_t new_stack_size;
567 char *new_prediction;
568 char **new_fnames;
569 bool *new_fnull;
570
572
575#ifdef JSONAPI_USE_PQEXPBUFFER
576 if (!new_prediction)
577 return false;
578#endif
580
582 new_stack_size * sizeof(char *));
583#ifdef JSONAPI_USE_PQEXPBUFFER
584 if (!new_fnames)
585 return false;
586#endif
587 lex->pstack->fnames = new_fnames;
588
589 new_fnull = REALLOC(lex->pstack->fnull, new_stack_size * sizeof(bool));
590#ifdef JSONAPI_USE_PQEXPBUFFER
591 if (!new_fnull)
592 return false;
593#endif
594 lex->pstack->fnull = new_fnull;
595
597 }
598
599 lex->lex_level += 1;
600
601 if (lex->incremental)
602 {
603 /*
604 * Ensure freeJsonLexContext() remains safe even if no fname is
605 * assigned at this level.
606 */
607 lex->pstack->fnames[lex->lex_level] = NULL;
608 }
609
610 return true;
#define REALLOC
Definition jsonapi.c:59

References fb(), JsonParserStack::fnames, JsonParserStack::fnull, JsonLexContext::incremental, JS_MAX_PROD_LEN, JS_STACK_CHUNK_SIZE, JsonLexContext::lex_level, JsonParserStack::prediction, JsonLexContext::pstack, REALLOC, and JsonParserStack::stack_size.

Referenced by pg_parse_json_incremental().

◆ IsValidJsonNumber()

bool IsValidJsonNumber ( const char str,
size_t  len 
)

Definition at line 339 of file jsonapi.c.

340{
341 bool numeric_error;
342 size_t total_len;
344
345 if (len <= 0)
346 return false;
347
348 /*
349 * json_lex_number expects a leading '-' to have been eaten already.
350 *
351 * having to cast away the constness of str is ugly, but there's not much
352 * easy alternative.
353 */
354 if (*str == '-')
355 {
356 dummy_lex.input = str + 1;
357 dummy_lex.input_length = len - 1;
358 }
359 else
360 {
361 dummy_lex.input = str;
362 dummy_lex.input_length = len;
363 }
364
365 dummy_lex.token_start = dummy_lex.input;
366
367 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error, &total_len);
368
369 return (!numeric_error) && (total_len == dummy_lex.input_length);
370}
const char * str
static JsonParseErrorType json_lex_number(JsonLexContext *lex, const char *s, bool *num_err, size_t *total_len)
Definition jsonapi.c:2299
const void size_t len
const char * input
Definition jsonapi.h:102

References fb(), JsonLexContext::input, json_lex_number(), len, and str.

Referenced by hstore_to_json_loose(), and hstore_to_jsonb_loose().

◆ json_count_array_elements()

JsonParseErrorType json_count_array_elements ( JsonLexContext lex,
int elements 
)

Definition at line 803 of file jsonapi.c.

805{
807 int count;
809
810 if (lex == &failed_oom)
811 return JSON_OUT_OF_MEMORY;
812
813 /*
814 * It's safe to do this with a shallow copy because the lexical routines
815 * don't scribble on the input. They do scribble on the other pointers
816 * etc, so doing this with a copy makes that safe.
817 */
818 memcpy(&copylex, lex, sizeof(JsonLexContext));
819 copylex.need_escapes = false; /* not interested in values here */
820 copylex.lex_level++;
821
822 count = 0;
825 if (result != JSON_SUCCESS)
826 return result;
828 {
829 while (1)
830 {
831 count++;
833 if (result != JSON_SUCCESS)
834 return result;
835 if (copylex.token_type != JSON_TOKEN_COMMA)
836 break;
838 if (result != JSON_SUCCESS)
839 return result;
840 }
841 }
844 if (result != JSON_SUCCESS)
845 return result;
846
847 *elements = count;
848 return JSON_SUCCESS;
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition jsonapi.c:305
static JsonParseErrorType lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition jsonapi.c:317
const JsonSemAction nullSemAction
Definition jsonapi.c:287
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition jsonapi.c:1588
static JsonParseErrorType parse_array_element(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:1467
JsonParseErrorType
Definition jsonapi.h:35
@ JSON_OUT_OF_MEMORY
Definition jsonapi.h:52
@ JSON_SUCCESS
Definition jsonapi.h:36
@ JSON_TOKEN_COMMA
Definition jsonapi.h:26
@ JSON_TOKEN_ARRAY_END
Definition jsonapi.h:25
@ JSON_TOKEN_ARRAY_START
Definition jsonapi.h:24

References failed_oom, fb(), json_lex(), JSON_OUT_OF_MEMORY, JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_SUCCESS, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COMMA, lex_expect(), lex_peek(), memcpy(), nullSemAction, parse_array_element(), and result.

Referenced by get_array_start().

◆ json_errdetail()

char * json_errdetail ( JsonParseErrorType  error,
JsonLexContext lex 
)

Definition at line 2453 of file jsonapi.c.

2455{
2456 if (error == JSON_OUT_OF_MEMORY || lex == &failed_oom)
2457 {
2458 /* Short circuit. Allocating anything for this case is unhelpful. */
2459 return _("out of memory");
2460 }
2461
2462 if (lex->errormsg)
2464 else
2466
2467 /*
2468 * A helper for error messages that should print the current token. The
2469 * format must contain exactly one %.*s specifier.
2470 */
2471#define json_token_error(lex, format) \
2472 jsonapi_appendStringInfo((lex)->errormsg, _(format), \
2473 (int) ((lex)->token_terminator - (lex)->token_start), \
2474 (lex)->token_start);
2475
2476 switch (error)
2477 {
2478 case JSON_INCOMPLETE:
2479 case JSON_SUCCESS:
2480 /* fall through to the error code after switch */
2481 break;
2483 if (lex->incremental)
2484 return _("Recursive descent parser cannot use incremental lexer.");
2485 else
2486 return _("Incremental parser requires incremental lexer.");
2488 return (_("JSON nested too deep, maximum permitted depth is 6400."));
2490 json_token_error(lex, "Escape sequence \"\\%.*s\" is invalid.");
2491 break;
2494 _("Character with value 0x%02x must be escaped."),
2495 (unsigned char) *(lex->token_terminator));
2496 break;
2497 case JSON_EXPECTED_END:
2498 json_token_error(lex, "Expected end of input, but found \"%.*s\".");
2499 break;
2501 json_token_error(lex, "Expected array element or \"]\", but found \"%.*s\".");
2502 break;
2504 json_token_error(lex, "Expected \",\" or \"]\", but found \"%.*s\".");
2505 break;
2507 json_token_error(lex, "Expected \":\", but found \"%.*s\".");
2508 break;
2509 case JSON_EXPECTED_JSON:
2510 json_token_error(lex, "Expected JSON value, but found \"%.*s\".");
2511 break;
2512 case JSON_EXPECTED_MORE:
2513 return _("The input string ended unexpectedly.");
2515 json_token_error(lex, "Expected string or \"}\", but found \"%.*s\".");
2516 break;
2518 json_token_error(lex, "Expected \",\" or \"}\", but found \"%.*s\".");
2519 break;
2521 json_token_error(lex, "Expected string, but found \"%.*s\".");
2522 break;
2523 case JSON_INVALID_TOKEN:
2524 json_token_error(lex, "Token \"%.*s\" is invalid.");
2525 break;
2526 case JSON_OUT_OF_MEMORY:
2527 /* should have been handled above; use the error path */
2528 break;
2530 return _("\\u0000 cannot be converted to text.");
2532 return _("\"\\u\" must be followed by four hexadecimal digits.");
2534 /* note: this case is only reachable in frontend not backend */
2535 return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8.");
2537
2538 /*
2539 * Note: this case is only reachable in backend and not frontend.
2540 * #ifdef it away so the frontend doesn't try to link against
2541 * backend functionality.
2542 */
2543#ifndef FRONTEND
2544 return psprintf(_("Unicode escape value could not be translated to the server's encoding %s."),
2546#else
2547 Assert(false);
2548 break;
2549#endif
2551 return _("Unicode high surrogate must not follow a high surrogate.");
2553 return _("Unicode low surrogate must follow a high surrogate.");
2555 /* fall through to the error code after switch */
2556 break;
2557 }
2558#undef json_token_error
2559
2560 /* Note that lex->errormsg can be NULL in shlib code. */
2561 if (lex->errormsg && lex->errormsg->len == 0)
2562 {
2563 /*
2564 * We don't use a default: case, so that the compiler will warn about
2565 * unhandled enum values. But this needs to be here anyway to cover
2566 * the possibility of an incorrect input.
2567 */
2569 "unexpected json parse error type: %d",
2570 (int) error);
2571 }
2572
2573#ifdef JSONAPI_USE_PQEXPBUFFER
2574 if (PQExpBufferBroken(lex->errormsg))
2575 return _("out of memory while constructing error description");
2576#endif
2577
2578 return lex->errormsg->data;
#define json_token_error(lex, format)
#define jsonapi_makeStringInfo
Definition jsonapi.c:80
#define jsonapi_resetStringInfo
Definition jsonapi.c:82
@ JSON_SEM_ACTION_FAILED
Definition jsonapi.h:59
@ JSON_EXPECTED_ARRAY_FIRST
Definition jsonapi.h:42
@ JSON_EXPECTED_MORE
Definition jsonapi.h:47
@ JSON_UNICODE_HIGH_SURROGATE
Definition jsonapi.h:57
@ JSON_EXPECTED_COLON
Definition jsonapi.h:44
@ JSON_EXPECTED_OBJECT_FIRST
Definition jsonapi.h:48
@ JSON_UNICODE_CODE_POINT_ZERO
Definition jsonapi.h:53
@ JSON_INVALID_LEXER_TYPE
Definition jsonapi.h:38
@ JSON_EXPECTED_STRING
Definition jsonapi.h:50
@ JSON_UNICODE_ESCAPE_FORMAT
Definition jsonapi.h:54
@ JSON_UNICODE_UNTRANSLATABLE
Definition jsonapi.h:56
@ JSON_EXPECTED_OBJECT_NEXT
Definition jsonapi.h:49
@ JSON_ESCAPING_REQUIRED
Definition jsonapi.h:41
@ JSON_EXPECTED_JSON
Definition jsonapi.h:46
@ JSON_INVALID_TOKEN
Definition jsonapi.h:51
@ JSON_ESCAPING_INVALID
Definition jsonapi.h:40
@ JSON_EXPECTED_END
Definition jsonapi.h:45
@ JSON_EXPECTED_ARRAY_NEXT
Definition jsonapi.h:43
@ JSON_UNICODE_HIGH_ESCAPE
Definition jsonapi.h:55
@ JSON_NESTING_TOO_DEEP
Definition jsonapi.h:39
@ JSON_UNICODE_LOW_SURROGATE
Definition jsonapi.h:58
const char * GetDatabaseEncodingName(void)
Definition mbutils.c:1394
#define PQExpBufferBroken(str)
Definition pqexpbuffer.h:59
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
static void error(void)
const char * token_terminator
Definition jsonapi.h:106

References _, Assert, error(), JsonLexContext::errormsg, failed_oom, GetDatabaseEncodingName(), JsonLexContext::incremental, JSON_ESCAPING_INVALID, JSON_ESCAPING_REQUIRED, JSON_EXPECTED_ARRAY_FIRST, JSON_EXPECTED_ARRAY_NEXT, JSON_EXPECTED_COLON, JSON_EXPECTED_END, JSON_EXPECTED_JSON, JSON_EXPECTED_MORE, JSON_EXPECTED_OBJECT_FIRST, JSON_EXPECTED_OBJECT_NEXT, JSON_EXPECTED_STRING, JSON_INCOMPLETE, JSON_INVALID_LEXER_TYPE, JSON_INVALID_TOKEN, JSON_NESTING_TOO_DEEP, JSON_OUT_OF_MEMORY, JSON_SEM_ACTION_FAILED, JSON_SUCCESS, json_token_error, JSON_UNICODE_CODE_POINT_ZERO, JSON_UNICODE_ESCAPE_FORMAT, JSON_UNICODE_HIGH_ESCAPE, JSON_UNICODE_HIGH_SURROGATE, JSON_UNICODE_LOW_SURROGATE, JSON_UNICODE_UNTRANSLATABLE, jsonapi_appendStringInfo, jsonapi_makeStringInfo, jsonapi_resetStringInfo, PQExpBufferBroken, psprintf(), and JsonLexContext::token_terminator.

Referenced by handle_oauth_sasl_error(), json_errsave_error(), json_parse_manifest(), json_parse_manifest_incremental_chunk(), main(), parse_oauth_json(), and test_gb18030_json().

◆ json_lex()

JsonParseErrorType json_lex ( JsonLexContext lex)

Definition at line 1588 of file jsonapi.c.

1590{
1591 const char *s;
1592 const char *const end = lex->input + lex->input_length;
1594
1595 if (lex == &failed_oom || lex->inc_state == &failed_inc_oom)
1596 return JSON_OUT_OF_MEMORY;
1597
1598 if (lex->incremental)
1599 {
1600 if (lex->inc_state->partial_completed)
1601 {
1602 /*
1603 * We just lexed a completed partial token on the last call, so
1604 * reset everything
1605 */
1607 lex->token_terminator = lex->input;
1608 lex->inc_state->partial_completed = false;
1609 }
1610
1611#ifdef JSONAPI_USE_PQEXPBUFFER
1612 /* Make sure our partial token buffer is valid before using it below. */
1614 return JSON_OUT_OF_MEMORY;
1615#endif
1616 }
1617
1618 s = lex->token_terminator;
1619
1620 if (lex->incremental && lex->inc_state->partial_token.len)
1621 {
1622 /*
1623 * We have a partial token. Extend it and if completed lex it by a
1624 * recursive call
1625 */
1627 size_t added = 0;
1628 bool tok_done = false;
1631
1632 if (ptok->data[0] == '"')
1633 {
1634 /*
1635 * It's a string. Accumulate characters until we reach an
1636 * unescaped '"'.
1637 */
1638 int escapes = 0;
1639
1640 for (int i = ptok->len - 1; i > 0; i--)
1641 {
1642 /* count the trailing backslashes on the partial token */
1643 if (ptok->data[i] == '\\')
1644 escapes++;
1645 else
1646 break;
1647 }
1648
1649 for (size_t i = 0; i < lex->input_length; i++)
1650 {
1651 char c = lex->input[i];
1652
1654 added++;
1655 if (c == '"' && escapes % 2 == 0)
1656 {
1657 tok_done = true;
1658 break;
1659 }
1660 if (c == '\\')
1661 escapes++;
1662 else
1663 escapes = 0;
1664 }
1665 }
1666 else
1667 {
1668 /* not a string */
1669 char c = ptok->data[0];
1670
1671 if (c == '-' || (c >= '0' && c <= '9'))
1672 {
1673 /*
1674 * Accumulate numeric continuations, respecting JSON number
1675 * grammar: -? int [frac] [exp]
1676 *
1677 * We must track what parts of the number we've already seen
1678 * so we don't over-consume. '.' is valid only once and not
1679 * after 'e'/'E'; 'e'/'E' is valid only once; '+'/'-' are
1680 * valid only immediately after 'e'/'E'.
1681 */
1682 bool numend = false;
1683 bool seen_dot = false;
1684 bool seen_exp = false;
1685 char prev;
1686
1687 /* Scan existing partial token for state */
1688 for (int j = 0; j < ptok->len; j++)
1689 {
1690 char pc = ptok->data[j];
1691
1692 if (pc == '.')
1693 seen_dot = true;
1694 else if (pc == 'e' || pc == 'E')
1695 seen_exp = true;
1696 }
1697 prev = ptok->data[ptok->len - 1];
1698
1699 for (size_t i = 0; i < lex->input_length && !numend; i++)
1700 {
1701 char cc = lex->input[i];
1702
1703 switch (cc)
1704 {
1705 case '+':
1706 case '-':
1707 if (prev != 'e' && prev != 'E')
1708 {
1709 numend = true;
1710 break;
1711 }
1713 added++;
1714 break;
1715 case '.':
1716 if (seen_dot || seen_exp)
1717 {
1718 numend = true;
1719 break;
1720 }
1721 seen_dot = true;
1723 added++;
1724 break;
1725 case 'e':
1726 case 'E':
1727 if (seen_exp)
1728 {
1729 numend = true;
1730 break;
1731 }
1732 seen_exp = true;
1734 added++;
1735 break;
1736 case '0':
1737 case '1':
1738 case '2':
1739 case '3':
1740 case '4':
1741 case '5':
1742 case '6':
1743 case '7':
1744 case '8':
1745 case '9':
1747 added++;
1748 break;
1749 default:
1750 numend = true;
1751 }
1752 if (!numend)
1753 prev = cc;
1754 }
1755 }
1756
1757 /*
1758 * Add any remaining alphanumeric chars. This takes care of the
1759 * {null, false, true} literals as well as any trailing
1760 * alphanumeric junk on non-string tokens.
1761 */
1762 for (size_t i = added; i < lex->input_length; i++)
1763 {
1764 char cc = lex->input[i];
1765
1766 if (JSON_ALPHANUMERIC_CHAR(cc))
1767 {
1769 added++;
1770 }
1771 else
1772 {
1773 tok_done = true;
1774 break;
1775 }
1776 }
1777 if (added == lex->input_length &&
1779 {
1780 tok_done = true;
1781 }
1782 }
1783
1784 if (!tok_done)
1785 {
1786 /* We should have consumed the whole chunk in this case. */
1787 Assert(added == lex->input_length);
1788
1789 if (!lex->inc_state->is_last_chunk)
1790 return JSON_INCOMPLETE;
1791
1792 /* json_errdetail() needs access to the accumulated token. */
1793 lex->token_start = ptok->data;
1794 lex->token_terminator = ptok->data + ptok->len;
1795 return JSON_INVALID_TOKEN;
1796 }
1797
1798 /*
1799 * Everything up to lex->input[added] has been added to the partial
1800 * token, so move the input past it.
1801 */
1802 lex->input += added;
1803 lex->input_length -= added;
1804
1805 dummy_lex.input = dummy_lex.token_terminator =
1806 dummy_lex.line_start = ptok->data;
1807 dummy_lex.line_number = lex->line_number;
1808 dummy_lex.input_length = ptok->len;
1809 dummy_lex.input_encoding = lex->input_encoding;
1810 dummy_lex.incremental = false;
1811 dummy_lex.need_escapes = lex->need_escapes;
1812 dummy_lex.strval = lex->strval;
1813
1815
1816 /*
1817 * We either have a complete token or an error. In either case we need
1818 * to point to the partial token data for the semantic or error
1819 * routines. If it's not an error we'll readjust on the next call to
1820 * json_lex.
1821 */
1822 lex->token_type = dummy_lex.token_type;
1823 lex->line_number = dummy_lex.line_number;
1824
1825 /*
1826 * We know the prev_token_terminator must be back in some previous
1827 * piece of input, so we just make it NULL.
1828 */
1830
1831 /*
1832 * Normally token_start would be ptok->data, but it could be later,
1833 * see json_lex_string's handling of invalid escapes.
1834 */
1835 lex->token_start = dummy_lex.token_start;
1836 lex->token_terminator = dummy_lex.token_terminator;
1838 {
1839 /* make sure we've used all the input */
1840 if (lex->token_terminator - lex->token_start != ptok->len)
1841 {
1842 Assert(false);
1843 return JSON_INVALID_TOKEN;
1844 }
1845
1846 lex->inc_state->partial_completed = true;
1847 }
1848 return partial_result;
1849 /* end of partial token processing */
1850 }
1851
1852 /* Skip leading whitespace. */
1853 while (s < end && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
1854 {
1855 if (*s++ == '\n')
1856 {
1857 ++lex->line_number;
1858 lex->line_start = s;
1859 }
1860 }
1861 lex->token_start = s;
1862
1863 /* Determine token type. */
1864 if (s >= end)
1865 {
1866 lex->token_start = NULL;
1868 lex->token_terminator = s;
1870 }
1871 else
1872 {
1873 switch (*s)
1874 {
1875 /* Single-character token, some kind of punctuation mark. */
1876 case '{':
1878 lex->token_terminator = s + 1;
1880 break;
1881 case '}':
1883 lex->token_terminator = s + 1;
1885 break;
1886 case '[':
1888 lex->token_terminator = s + 1;
1890 break;
1891 case ']':
1893 lex->token_terminator = s + 1;
1895 break;
1896 case ',':
1898 lex->token_terminator = s + 1;
1900 break;
1901 case ':':
1903 lex->token_terminator = s + 1;
1905 break;
1906 case '"':
1907 /* string */
1908 result = json_lex_string(lex);
1909 if (result != JSON_SUCCESS)
1910 return result;
1912 break;
1913 case '-':
1914 /* Negative number. */
1915 result = json_lex_number(lex, s + 1, NULL, NULL);
1916 if (result != JSON_SUCCESS)
1917 return result;
1919 break;
1920 case '0':
1921 case '1':
1922 case '2':
1923 case '3':
1924 case '4':
1925 case '5':
1926 case '6':
1927 case '7':
1928 case '8':
1929 case '9':
1930 /* Positive number. */
1931 result = json_lex_number(lex, s, NULL, NULL);
1932 if (result != JSON_SUCCESS)
1933 return result;
1935 break;
1936 default:
1937 {
1938 const char *p;
1939
1940 /*
1941 * We're not dealing with a string, number, legal
1942 * punctuation mark, or end of string. The only legal
1943 * tokens we might find here are true, false, and null,
1944 * but for error reporting purposes we scan until we see a
1945 * non-alphanumeric character. That way, we can report
1946 * the whole word as an unexpected token, rather than just
1947 * some unintuitive prefix thereof.
1948 */
1949 for (p = s; p < end && JSON_ALPHANUMERIC_CHAR(*p); p++)
1950 /* skip */ ;
1951
1952 /*
1953 * We got some sort of unexpected punctuation or an
1954 * otherwise unexpected character, so just complain about
1955 * that one character.
1956 */
1957 if (p == s)
1958 {
1960 lex->token_terminator = s + 1;
1961 return JSON_INVALID_TOKEN;
1962 }
1963
1964 if (lex->incremental && !lex->inc_state->is_last_chunk &&
1965 p == lex->input + lex->input_length)
1966 {
1968 return JSON_INCOMPLETE;
1969 }
1970
1971 /*
1972 * We've got a real alphanumeric token here. If it
1973 * happens to be true, false, or null, all is well. If
1974 * not, error out.
1975 */
1977 lex->token_terminator = p;
1978 if (p - s == 4)
1979 {
1980 if (memcmp(s, "true", 4) == 0)
1982 else if (memcmp(s, "null", 4) == 0)
1984 else
1985 return JSON_INVALID_TOKEN;
1986 }
1987 else if (p - s == 5 && memcmp(s, "false", 5) == 0)
1989 else
1990 return JSON_INVALID_TOKEN;
1991 }
1992 } /* end of switch */
1993 }
1994
1995 if (lex->incremental && lex->token_type == JSON_TOKEN_END && !lex->inc_state->is_last_chunk)
1996 return JSON_INCOMPLETE;
1997 else
1998 return JSON_SUCCESS;
int j
Definition isn.c:78
static JsonParseErrorType json_lex_string(JsonLexContext *lex)
Definition jsonapi.c:2012
#define JSON_ALPHANUMERIC_CHAR(c)
Definition jsonapi.c:326
#define jsonapi_appendStringInfoCharMacro
Definition jsonapi.c:79
#define jsonapi_appendBinaryStringInfo
Definition jsonapi.c:77
#define jsonapi_StrValType
Definition jsonapi.h:73
@ JSON_TOKEN_FALSE
Definition jsonapi.h:29
@ JSON_TOKEN_END
Definition jsonapi.h:31
@ JSON_TOKEN_TRUE
Definition jsonapi.h:28
@ JSON_TOKEN_OBJECT_END
Definition jsonapi.h:23
@ JSON_TOKEN_NULL
Definition jsonapi.h:30
@ JSON_TOKEN_OBJECT_START
Definition jsonapi.h:22
@ JSON_TOKEN_NUMBER
Definition jsonapi.h:21
@ JSON_TOKEN_STRING
Definition jsonapi.h:20
@ JSON_TOKEN_COLON
Definition jsonapi.h:27
#define PQExpBufferDataBroken(buf)
Definition pqexpbuffer.h:67
int input_encoding
Definition jsonapi.h:104
const char * prev_token_terminator
Definition jsonapi.h:107
bool need_escapes
Definition jsonapi.h:116
const char * token_start
Definition jsonapi.h:105
size_t input_length
Definition jsonapi.h:103
const char * line_start
Definition jsonapi.h:113
JsonTokenType token_type
Definition jsonapi.h:109

References Assert, failed_inc_oom, failed_oom, fb(), i, JsonLexContext::inc_state, JsonLexContext::incremental, JsonLexContext::input, JsonLexContext::input_encoding, JsonLexContext::input_length, JsonIncrementalState::is_last_chunk, j, JSON_ALPHANUMERIC_CHAR, JSON_INCOMPLETE, JSON_INVALID_TOKEN, json_lex(), json_lex_number(), json_lex_string(), JSON_OUT_OF_MEMORY, JSON_SUCCESS, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COLON, JSON_TOKEN_COMMA, JSON_TOKEN_END, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_END, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, jsonapi_appendBinaryStringInfo, jsonapi_appendStringInfoCharMacro, jsonapi_resetStringInfo, jsonapi_StrValType, JsonLexContext::line_number, JsonLexContext::line_start, JsonLexContext::need_escapes, JsonIncrementalState::partial_completed, JsonIncrementalState::partial_token, PQExpBufferDataBroken, JsonLexContext::prev_token_terminator, result, JsonLexContext::strval, JsonLexContext::token_start, JsonLexContext::token_terminator, and JsonLexContext::token_type.

Referenced by json_count_array_elements(), json_get_first_token(), json_lex(), json_typeof(), lex_expect(), parse_array(), parse_object(), parse_object_field(), parse_scalar(), pg_parse_json(), and pg_parse_json_incremental().

◆ json_lex_number()

static JsonParseErrorType json_lex_number ( JsonLexContext lex,
const char s,
bool num_err,
size_t total_len 
)
inlinestatic

Definition at line 2299 of file jsonapi.c.

2302{
2303 bool error = false;
2304 int len = s - lex->input;
2305
2306 /* Part (1): leading sign indicator. */
2307 /* Caller already did this for us; so do nothing. */
2308
2309 /* Part (2): parse main digit string. */
2310 if (len < lex->input_length && *s == '0')
2311 {
2312 s++;
2313 len++;
2314 }
2315 else if (len < lex->input_length && *s >= '1' && *s <= '9')
2316 {
2317 do
2318 {
2319 s++;
2320 len++;
2321 } while (len < lex->input_length && *s >= '0' && *s <= '9');
2322 }
2323 else
2324 error = true;
2325
2326 /* Part (3): parse optional decimal portion. */
2327 if (len < lex->input_length && *s == '.')
2328 {
2329 s++;
2330 len++;
2331 if (len == lex->input_length || *s < '0' || *s > '9')
2332 error = true;
2333 else
2334 {
2335 do
2336 {
2337 s++;
2338 len++;
2339 } while (len < lex->input_length && *s >= '0' && *s <= '9');
2340 }
2341 }
2342
2343 /* Part (4): parse optional exponent. */
2344 if (len < lex->input_length && (*s == 'e' || *s == 'E'))
2345 {
2346 s++;
2347 len++;
2348 if (len < lex->input_length && (*s == '+' || *s == '-'))
2349 {
2350 s++;
2351 len++;
2352 }
2353 if (len == lex->input_length || *s < '0' || *s > '9')
2354 error = true;
2355 else
2356 {
2357 do
2358 {
2359 s++;
2360 len++;
2361 } while (len < lex->input_length && *s >= '0' && *s <= '9');
2362 }
2363 }
2364
2365 /*
2366 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
2367 * here should be considered part of the token for error-reporting
2368 * purposes.
2369 */
2370 for (; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*s); s++, len++)
2371 error = true;
2372
2373 if (total_len != NULL)
2374 *total_len = len;
2375
2376 if (lex->incremental && !lex->inc_state->is_last_chunk &&
2377 len >= lex->input_length)
2378 {
2380 lex->token_start, s - lex->token_start);
2381 if (num_err != NULL)
2382 *num_err = error;
2383
2384 return JSON_INCOMPLETE;
2385 }
2386 else if (num_err != NULL)
2387 {
2388 /* let the caller handle any error */
2389 *num_err = error;
2390 }
2391 else
2392 {
2393 /* return token endpoint */
2395 lex->token_terminator = s;
2396 /* handle error if any */
2397 if (error)
2398 return JSON_INVALID_TOKEN;
2399 }
2400
2401 return JSON_SUCCESS;

References error(), fb(), JsonLexContext::inc_state, JsonLexContext::incremental, JsonLexContext::input, JsonLexContext::input_length, JsonIncrementalState::is_last_chunk, JSON_ALPHANUMERIC_CHAR, JSON_INCOMPLETE, JSON_INVALID_TOKEN, JSON_SUCCESS, jsonapi_appendBinaryStringInfo, len, JsonIncrementalState::partial_token, JsonLexContext::prev_token_terminator, JsonLexContext::token_start, and JsonLexContext::token_terminator.

Referenced by IsValidJsonNumber(), and json_lex().

◆ json_lex_string()

static JsonParseErrorType json_lex_string ( JsonLexContext lex)
inlinestatic

Definition at line 2012 of file jsonapi.c.

2014{
2015 const char *s;
2016 const char *const end = lex->input + lex->input_length;
2017 int hi_surrogate = -1;
2018
2019 /* Convenience macros for error exits */
2020#define FAIL_OR_INCOMPLETE_AT_CHAR_START(code) \
2021 do { \
2022 if (lex->incremental && !lex->inc_state->is_last_chunk) \
2023 { \
2024 jsonapi_appendBinaryStringInfo(&lex->inc_state->partial_token, \
2025 lex->token_start, \
2026 end - lex->token_start); \
2027 return JSON_INCOMPLETE; \
2028 } \
2029 lex->token_terminator = s; \
2030 return code; \
2031 } while (0)
2032#define FAIL_AT_CHAR_END(code) \
2033 do { \
2034 ptrdiff_t remaining = end - s; \
2035 int charlen; \
2036 charlen = pg_encoding_mblen_or_incomplete(lex->input_encoding, \
2037 s, remaining); \
2038 lex->token_terminator = (charlen <= remaining) ? s + charlen : end; \
2039 return code; \
2040 } while (0)
2041
2042 if (lex->need_escapes)
2043 {
2044#ifdef JSONAPI_USE_PQEXPBUFFER
2045 /* make sure initialization succeeded */
2046 if (lex->strval == NULL)
2047 return JSON_OUT_OF_MEMORY;
2048#endif
2050 }
2051
2052 Assert(lex->input_length > 0);
2053 s = lex->token_start;
2054 for (;;)
2055 {
2056 s++;
2057 /* Premature end of the string. */
2058 if (s >= end)
2060 else if (*s == '"')
2061 break;
2062 else if (*s == '\\')
2063 {
2064 /* OK, we have an escape character. */
2065 s++;
2066 if (s >= end)
2068 else if (*s == 'u')
2069 {
2070 int i;
2071 int ch = 0;
2072
2073 for (i = 1; i <= 4; i++)
2074 {
2075 s++;
2076 if (s >= end)
2078 else if (*s >= '0' && *s <= '9')
2079 ch = (ch * 16) + (*s - '0');
2080 else if (*s >= 'a' && *s <= 'f')
2081 ch = (ch * 16) + (*s - 'a') + 10;
2082 else if (*s >= 'A' && *s <= 'F')
2083 ch = (ch * 16) + (*s - 'A') + 10;
2084 else
2086 }
2087 if (lex->need_escapes)
2088 {
2089 /*
2090 * Combine surrogate pairs.
2091 */
2093 {
2094 if (hi_surrogate != -1)
2096 hi_surrogate = ch;
2097 continue;
2098 }
2099 else if (is_utf16_surrogate_second(ch))
2100 {
2101 if (hi_surrogate == -1)
2104 hi_surrogate = -1;
2105 }
2106
2107 if (hi_surrogate != -1)
2109
2110 /*
2111 * Reject invalid cases. We can't have a value above
2112 * 0xFFFF here (since we only accepted 4 hex digits
2113 * above), so no need to test for out-of-range chars.
2114 */
2115 if (ch == 0)
2116 {
2117 /* We can't allow this, since our TEXT type doesn't */
2119 }
2120
2121 /*
2122 * Add the represented character to lex->strval. In the
2123 * backend, we can let pg_unicode_to_server_noerror()
2124 * handle any required character set conversion; in
2125 * frontend, we can only deal with trivial conversions.
2126 */
2127#ifndef FRONTEND
2128 {
2130
2131 if (!pg_unicode_to_server_noerror(ch, (unsigned char *) cbuf))
2134 }
2135#else
2136 if (lex->input_encoding == PG_UTF8)
2137 {
2138 /* OK, we can map the code point to UTF8 easily */
2139 char utf8str[5];
2140 int utf8len;
2141
2142 unicode_to_utf8(ch, (unsigned char *) utf8str);
2143 utf8len = pg_utf_mblen((unsigned char *) utf8str);
2145 }
2146 else if (ch <= 0x007f)
2147 {
2148 /* The ASCII range is the same in all encodings */
2150 }
2151 else
2153#endif /* FRONTEND */
2154 }
2155 }
2156 else if (lex->need_escapes)
2157 {
2158 if (hi_surrogate != -1)
2160
2161 switch (*s)
2162 {
2163 case '"':
2164 case '\\':
2165 case '/':
2167 break;
2168 case 'b':
2170 break;
2171 case 'f':
2173 break;
2174 case 'n':
2176 break;
2177 case 'r':
2179 break;
2180 case 't':
2182 break;
2183 default:
2184
2185 /*
2186 * Not a valid string escape, so signal error. We
2187 * adjust token_start so that just the escape sequence
2188 * is reported, not the whole string.
2189 */
2190 lex->token_start = s;
2192 }
2193 }
2194 else if (strchr("\"\\/bfnrt", *s) == NULL)
2195 {
2196 /*
2197 * Simpler processing if we're not bothered about de-escaping
2198 *
2199 * It's very tempting to remove the strchr() call here and
2200 * replace it with a switch statement, but testing so far has
2201 * shown it's not a performance win.
2202 */
2203 lex->token_start = s;
2205 }
2206 }
2207 else
2208 {
2209 const char *p = s;
2210
2211 if (hi_surrogate != -1)
2213
2214 /*
2215 * Skip to the first byte that requires special handling, so we
2216 * can batch calls to jsonapi_appendBinaryStringInfo.
2217 */
2218 while (p < end - sizeof(Vector8) &&
2219 !pg_lfind8('\\', (const uint8 *) p, sizeof(Vector8)) &&
2220 !pg_lfind8('"', (const uint8 *) p, sizeof(Vector8)) &&
2221 !pg_lfind8_le(31, (const uint8 *) p, sizeof(Vector8)))
2222 p += sizeof(Vector8);
2223
2224 for (; p < end; p++)
2225 {
2226 if (*p == '\\' || *p == '"')
2227 break;
2228 else if ((unsigned char) *p <= 31)
2229 {
2230 /* Per RFC4627, these characters MUST be escaped. */
2231 /*
2232 * Since *p isn't printable, exclude it from the context
2233 * string
2234 */
2235 lex->token_terminator = p;
2237 }
2238 }
2239
2240 if (lex->need_escapes)
2241 jsonapi_appendBinaryStringInfo(lex->strval, s, p - s);
2242
2243 /*
2244 * s will be incremented at the top of the loop, so set it to just
2245 * behind our lookahead position
2246 */
2247 s = p - 1;
2248 }
2249 }
2250
2251 if (hi_surrogate != -1)
2252 {
2253 lex->token_terminator = s + 1;
2255 }
2256
2257#ifdef JSONAPI_USE_PQEXPBUFFER
2258 if (lex->need_escapes && PQExpBufferBroken(lex->strval))
2259 return JSON_OUT_OF_MEMORY;
2260#endif
2261
2262 /* Hooray, we found the end of the string! */
2264 lex->token_terminator = s + 1;
2265 return JSON_SUCCESS;
2266
2267#undef FAIL_OR_INCOMPLETE_AT_CHAR_START
2268#undef FAIL_AT_CHAR_END
uint8_t uint8
Definition c.h:622
#define jsonapi_appendStringInfoChar
Definition jsonapi.c:78
#define FAIL_OR_INCOMPLETE_AT_CHAR_START(code)
#define FAIL_AT_CHAR_END(code)
#define PG_UTF8
Definition mbprint.c:43
bool pg_unicode_to_server_noerror(char32_t c, unsigned char *s)
Definition mbutils.c:937
static bool pg_lfind8(uint8 key, const uint8 *base, uint32 nelem)
Definition pg_lfind.h:26
static bool pg_lfind8_le(uint8 key, const uint8 *base, uint32 nelem)
Definition pg_lfind.h:58
#define pg_utf_mblen
Definition pg_wchar.h:486
static bool is_utf16_surrogate_first(char32_t c)
Definition pg_wchar.h:378
static unsigned char * unicode_to_utf8(char32_t c, unsigned char *utf8string)
Definition pg_wchar.h:428
static bool is_utf16_surrogate_second(char32_t c)
Definition pg_wchar.h:384
static char32_t surrogate_pair_to_codepoint(char16_t first, char16_t second)
Definition pg_wchar.h:390
#define MAX_UNICODE_EQUIVALENT_STRING
Definition pg_wchar.h:182
uint64 Vector8
Definition simd.h:47
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230

References appendStringInfoString(), Assert, FAIL_AT_CHAR_END, FAIL_OR_INCOMPLETE_AT_CHAR_START, fb(), i, JsonLexContext::input, JsonLexContext::input_encoding, JsonLexContext::input_length, is_utf16_surrogate_first(), is_utf16_surrogate_second(), JSON_ESCAPING_INVALID, JSON_ESCAPING_REQUIRED, JSON_INVALID_TOKEN, JSON_OUT_OF_MEMORY, JSON_SUCCESS, JSON_UNICODE_CODE_POINT_ZERO, JSON_UNICODE_ESCAPE_FORMAT, JSON_UNICODE_HIGH_ESCAPE, JSON_UNICODE_HIGH_SURROGATE, JSON_UNICODE_LOW_SURROGATE, JSON_UNICODE_UNTRANSLATABLE, jsonapi_appendBinaryStringInfo, jsonapi_appendStringInfoChar, jsonapi_resetStringInfo, MAX_UNICODE_EQUIVALENT_STRING, JsonLexContext::need_escapes, pg_lfind8(), pg_lfind8_le(), pg_unicode_to_server_noerror(), PG_UTF8, pg_utf_mblen, PQExpBufferBroken, JsonLexContext::prev_token_terminator, JsonLexContext::strval, surrogate_pair_to_codepoint(), JsonLexContext::token_start, JsonLexContext::token_terminator, and unicode_to_utf8().

Referenced by json_lex().

◆ lex_expect()

static JsonParseErrorType lex_expect ( JsonParseContext  ctx,
JsonLexContext lex,
JsonTokenType  token 
)
inlinestatic

Definition at line 317 of file jsonapi.c.

318{
319 if (lex_peek(lex) == token)
320 return json_lex(lex);
321 else
322 return report_parse_error(ctx, lex);
323}
static JsonParseErrorType report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
Definition jsonapi.c:2409

References json_lex(), lex_peek(), and report_parse_error().

Referenced by json_count_array_elements(), parse_array(), parse_object(), parse_object_field(), and pg_parse_json().

◆ lex_peek()

◆ makeJsonLexContextCstringLen()

JsonLexContext * makeJsonLexContextCstringLen ( JsonLexContext lex,
const char json,
size_t  len,
int  encoding,
bool  need_escapes 
)

Definition at line 392 of file jsonapi.c.

394{
395 if (lex == NULL)
396 {
397 lex = ALLOC0(sizeof(JsonLexContext));
398 if (!lex)
399 return &failed_oom;
401 }
402 else
403 memset(lex, 0, sizeof(JsonLexContext));
404
405 lex->errormsg = NULL;
406 lex->input = lex->token_terminator = lex->line_start = json;
407 lex->line_number = 1;
408 lex->input_length = len;
410 lex->need_escapes = need_escapes;
411 if (need_escapes)
412 {
413 /*
414 * This call can fail in shlib code. We defer error handling to time
415 * of use (json_lex_string()) since we might not need to parse any
416 * strings anyway.
417 */
420 }
421
422 return lex;
423}
static char * encoding
Definition initdb.c:139

References ALLOC0, encoding, JsonLexContext::errormsg, failed_oom, fb(), JsonLexContext::flags, JsonLexContext::input, JsonLexContext::input_encoding, JsonLexContext::input_length, jsonapi_makeStringInfo, JSONLEX_FREE_STRUCT, JSONLEX_FREE_STRVAL, len, JsonLexContext::line_number, JsonLexContext::line_start, JsonLexContext::need_escapes, JsonLexContext::strval, and JsonLexContext::token_terminator.

Referenced by get_json_object_as_hash(), handle_oauth_sasl_error(), json_parse_manifest(), json_recv(), jsonb_from_cstring(), main(), makeJsonLexContext(), parse_oauth_json(), pg_dependencies_in(), pg_ndistinct_in(), populate_array_json(), and test_gb18030_json().

◆ makeJsonLexContextIncremental()

JsonLexContext * makeJsonLexContextIncremental ( JsonLexContext lex,
int  encoding,
bool  need_escapes 
)

Definition at line 497 of file jsonapi.c.

500{
501 if (lex == NULL)
502 {
503 lex = ALLOC0(sizeof(JsonLexContext));
504 if (!lex)
505 return &failed_oom;
506
508 }
509 else
510 memset(lex, 0, sizeof(JsonLexContext));
511
512 lex->line_number = 1;
514
516 {
517 if (lex->flags & JSONLEX_FREE_STRUCT)
518 {
519 FREE(lex);
520 return &failed_oom;
521 }
522
523 /* lex->inc_state tracks the OOM failure; we can return here. */
524 return lex;
525 }
526
527 lex->need_escapes = need_escapes;
528 if (need_escapes)
529 {
530 /*
531 * This call can fail in shlib code. We defer error handling to time
532 * of use (json_lex_string()) since we might not need to parse any
533 * strings anyway.
534 */
537 }
538
539 return lex;
static bool allocate_incremental_state(JsonLexContext *lex)
Definition jsonapi.c:433

References ALLOC0, allocate_incremental_state(), encoding, failed_oom, fb(), JsonLexContext::flags, FREE, JsonLexContext::input_encoding, jsonapi_makeStringInfo, JSONLEX_FREE_STRUCT, JSONLEX_FREE_STRVAL, JsonLexContext::line_number, JsonLexContext::need_escapes, and JsonLexContext::strval.

Referenced by json_parse_manifest_incremental_init(), and main().

◆ next_prediction()

static char next_prediction ( JsonParserStack pstack)
inlinestatic

Definition at line 634 of file jsonapi.c.

636{
637 Assert(pstack->pred_index > 0);
638 return pstack->prediction[pstack->pred_index - 1];

References Assert, JsonParserStack::pred_index, and JsonParserStack::prediction.

Referenced by pg_parse_json_incremental().

◆ parse_array()

static JsonParseErrorType parse_array ( JsonLexContext lex,
const JsonSemAction sem 
)
static

Definition at line 1511 of file jsonapi.c.

1513{
1514 /*
1515 * an array is a possibly empty sequence of array elements, separated by
1516 * commas and surrounded by square brackets.
1517 */
1521
1522#ifndef FRONTEND
1524#endif
1525
1526 if (astart != NULL)
1527 {
1528 result = (*astart) (sem->semstate);
1529 if (result != JSON_SUCCESS)
1530 return result;
1531 }
1532
1533 /*
1534 * Data inside an array is at a higher nesting level than the array
1535 * itself. Note that we increment this after we call the semantic routine
1536 * for the array start and restore it before we call the routine for the
1537 * array end.
1538 */
1539 lex->lex_level++;
1540
1543 {
1545
1546 while (result == JSON_SUCCESS && lex_peek(lex) == JSON_TOKEN_COMMA)
1547 {
1548 result = json_lex(lex);
1549 if (result != JSON_SUCCESS)
1550 break;
1552 }
1553 }
1554 if (result != JSON_SUCCESS)
1555 return result;
1556
1558 if (result != JSON_SUCCESS)
1559 return result;
1560
1561 lex->lex_level--;
1562
1563 if (aend != NULL)
1564 {
1565 result = (*aend) (sem->semstate);
1566 if (result != JSON_SUCCESS)
1567 return result;
1568 }
1569
1570 return JSON_SUCCESS;
JsonParseErrorType(* json_struct_action)(void *state)
Definition jsonapi.h:127
void check_stack_depth(void)
Definition stack_depth.c:95
json_struct_action array_end
Definition jsonapi.h:157
void * semstate
Definition jsonapi.h:153
json_struct_action array_start
Definition jsonapi.h:156
static JsonSemAction sem

References JsonSemAction::array_end, JsonSemAction::array_start, check_stack_depth(), fb(), json_lex(), JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_SUCCESS, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COMMA, lex_expect(), JsonLexContext::lex_level, lex_peek(), parse_array_element(), result, sem, and JsonSemAction::semstate.

Referenced by parse_array_element(), parse_object_field(), and pg_parse_json().

◆ parse_array_element()

static JsonParseErrorType parse_array_element ( JsonLexContext lex,
const JsonSemAction sem 
)
static

Definition at line 1467 of file jsonapi.c.

1469{
1472 JsonTokenType tok = lex_peek(lex);
1474 bool isnull;
1475
1476 isnull = tok == JSON_TOKEN_NULL;
1477
1478 if (astart != NULL)
1479 {
1480 result = (*astart) (sem->semstate, isnull);
1481 if (result != JSON_SUCCESS)
1482 return result;
1483 }
1484
1485 /* an array element is any object, array or scalar */
1486 switch (tok)
1487 {
1489 result = parse_object(lex, sem);
1490 break;
1492 result = parse_array(lex, sem);
1493 break;
1494 default:
1495 result = parse_scalar(lex, sem);
1496 }
1497
1498 if (result != JSON_SUCCESS)
1499 return result;
1500
1501 if (aend != NULL)
1502 {
1503 result = (*aend) (sem->semstate, isnull);
1504 if (result != JSON_SUCCESS)
1505 return result;
1506 }
1507
1508 return JSON_SUCCESS;
static JsonParseErrorType parse_object(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:1388
static JsonParseErrorType parse_scalar(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:1252
static JsonParseErrorType parse_array(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:1511
JsonParseErrorType(* json_aelem_action)(void *state, bool isnull)
Definition jsonapi.h:129
JsonTokenType
Definition jsonapi.h:18
json_aelem_action array_element_start
Definition jsonapi.h:160
json_aelem_action array_element_end
Definition jsonapi.h:161

References JsonSemAction::array_element_end, JsonSemAction::array_element_start, fb(), JSON_SUCCESS, JSON_TOKEN_ARRAY_START, JSON_TOKEN_NULL, JSON_TOKEN_OBJECT_START, lex_peek(), parse_array(), parse_object(), parse_scalar(), result, sem, and JsonSemAction::semstate.

Referenced by json_count_array_elements(), and parse_array().

◆ parse_object()

static JsonParseErrorType parse_object ( JsonLexContext lex,
const JsonSemAction sem 
)
static

Definition at line 1388 of file jsonapi.c.

1390{
1391 /*
1392 * an object is a possibly empty sequence of object fields, separated by
1393 * commas and surrounded by curly braces.
1394 */
1399
1400#ifndef FRONTEND
1401
1402 /*
1403 * TODO: clients need some way to put a bound on stack growth. Parse level
1404 * limits maybe?
1405 */
1407#endif
1408
1409 if (ostart != NULL)
1410 {
1411 result = (*ostart) (sem->semstate);
1412 if (result != JSON_SUCCESS)
1413 return result;
1414 }
1415
1416 /*
1417 * Data inside an object is at a higher nesting level than the object
1418 * itself. Note that we increment this after we call the semantic routine
1419 * for the object start and restore it before we call the routine for the
1420 * object end.
1421 */
1422 lex->lex_level++;
1423
1425 result = json_lex(lex);
1426 if (result != JSON_SUCCESS)
1427 return result;
1428
1429 tok = lex_peek(lex);
1430 switch (tok)
1431 {
1432 case JSON_TOKEN_STRING:
1434 while (result == JSON_SUCCESS && lex_peek(lex) == JSON_TOKEN_COMMA)
1435 {
1436 result = json_lex(lex);
1437 if (result != JSON_SUCCESS)
1438 break;
1440 }
1441 break;
1443 break;
1444 default:
1445 /* case of an invalid initial token inside the object */
1447 }
1448 if (result != JSON_SUCCESS)
1449 return result;
1450
1452 if (result != JSON_SUCCESS)
1453 return result;
1454
1455 lex->lex_level--;
1456
1457 if (oend != NULL)
1458 {
1459 result = (*oend) (sem->semstate);
1460 if (result != JSON_SUCCESS)
1461 return result;
1462 }
1463
1464 return JSON_SUCCESS;
static JsonParseErrorType parse_object_field(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:1312
json_struct_action object_start
Definition jsonapi.h:154
json_struct_action object_end
Definition jsonapi.h:155

References Assert, check_stack_depth(), fb(), json_lex(), JSON_PARSE_OBJECT_NEXT, JSON_PARSE_OBJECT_START, JSON_SUCCESS, JSON_TOKEN_COMMA, JSON_TOKEN_OBJECT_END, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, lex_expect(), JsonLexContext::lex_level, lex_peek(), JsonSemAction::object_end, JsonSemAction::object_start, parse_object_field(), report_parse_error(), result, sem, and JsonSemAction::semstate.

Referenced by parse_array_element(), parse_object_field(), and pg_parse_json().

◆ parse_object_field()

static JsonParseErrorType parse_object_field ( JsonLexContext lex,
const JsonSemAction sem 
)
static

Definition at line 1312 of file jsonapi.c.

1314{
1315 /*
1316 * An object field is "fieldname" : value where value can be a scalar,
1317 * object or array. Note: in user-facing docs and error messages, we
1318 * generally call a field name a "key".
1319 */
1320
1321 char *fname = NULL;
1324 bool isnull;
1327
1328 if (lex_peek(lex) != JSON_TOKEN_STRING)
1330 if ((ostart != NULL || oend != NULL) && lex->need_escapes)
1331 {
1332 /* fname is NULL if need_escapes is false */
1333 fname = STRDUP(lex->strval->data);
1334 if (fname == NULL)
1335 return JSON_OUT_OF_MEMORY;
1336 }
1337 result = json_lex(lex);
1338 if (result != JSON_SUCCESS)
1339 {
1340 FREE(fname);
1341 return result;
1342 }
1343
1345 if (result != JSON_SUCCESS)
1346 {
1347 FREE(fname);
1348 return result;
1349 }
1350
1351 tok = lex_peek(lex);
1352 isnull = tok == JSON_TOKEN_NULL;
1353
1354 if (ostart != NULL)
1355 {
1356 result = (*ostart) (sem->semstate, fname, isnull);
1357 if (result != JSON_SUCCESS)
1358 goto ofield_cleanup;
1359 }
1360
1361 switch (tok)
1362 {
1364 result = parse_object(lex, sem);
1365 break;
1367 result = parse_array(lex, sem);
1368 break;
1369 default:
1370 result = parse_scalar(lex, sem);
1371 }
1372 if (result != JSON_SUCCESS)
1373 goto ofield_cleanup;
1374
1375 if (oend != NULL)
1376 {
1377 result = (*oend) (sem->semstate, fname, isnull);
1378 if (result != JSON_SUCCESS)
1379 goto ofield_cleanup;
1380 }
1381
1383 if (lex->flags & JSONLEX_CTX_OWNS_TOKENS)
1384 FREE(fname);
1385 return result;
#define STRDUP(s)
Definition jsonapi.c:56
JsonParseErrorType(* json_ofield_action)(void *state, char *fname, bool isnull)
Definition jsonapi.h:128
json_ofield_action object_field_start
Definition jsonapi.h:158
json_ofield_action object_field_end
Definition jsonapi.h:159

References fb(), JsonLexContext::flags, FREE, json_lex(), JSON_OUT_OF_MEMORY, JSON_PARSE_OBJECT_LABEL, JSON_PARSE_STRING, JSON_SUCCESS, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COLON, JSON_TOKEN_NULL, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSONLEX_CTX_OWNS_TOKENS, lex_expect(), lex_peek(), JsonLexContext::need_escapes, JsonSemAction::object_field_end, JsonSemAction::object_field_start, parse_array(), parse_object(), parse_scalar(), report_parse_error(), result, sem, JsonSemAction::semstate, STRDUP, and JsonLexContext::strval.

Referenced by parse_object().

◆ parse_scalar()

static JsonParseErrorType parse_scalar ( JsonLexContext lex,
const JsonSemAction sem 
)
inlinestatic

Definition at line 1252 of file jsonapi.c.

1254{
1255 char *val = NULL;
1257 JsonTokenType tok = lex_peek(lex);
1259
1260 /* a scalar must be a string, a number, true, false, or null */
1265
1266 /* if no semantic function, just consume the token */
1267 if (sfunc == NULL)
1268 return json_lex(lex);
1269
1270 /* extract the de-escaped string value, or the raw lexeme */
1271 if (lex_peek(lex) == JSON_TOKEN_STRING)
1272 {
1273 if (lex->need_escapes)
1274 {
1275 val = STRDUP(lex->strval->data);
1276 if (val == NULL)
1277 return JSON_OUT_OF_MEMORY;
1278 }
1279 }
1280 else
1281 {
1282 int len = (lex->token_terminator - lex->token_start);
1283
1284 val = ALLOC(len + 1);
1285 if (val == NULL)
1286 return JSON_OUT_OF_MEMORY;
1287
1288 memcpy(val, lex->token_start, len);
1289 val[len] = '\0';
1290 }
1291
1292 /* consume the token */
1293 result = json_lex(lex);
1294 if (result != JSON_SUCCESS)
1295 {
1296 FREE(val);
1297 return result;
1298 }
1299
1300 /*
1301 * invoke the callback, which may take ownership of val. For string
1302 * values, val is NULL if need_escapes is false.
1303 */
1304 result = (*sfunc) (sem->semstate, val, tok);
1305
1306 if (lex->flags & JSONLEX_CTX_OWNS_TOKENS)
1307 FREE(val);
1308
1309 return result;
long val
Definition informix.c:689
JsonParseErrorType(* json_scalar_action)(void *state, char *token, JsonTokenType tokentype)
Definition jsonapi.h:130
json_scalar_action scalar
Definition jsonapi.h:162

References ALLOC, fb(), JsonLexContext::flags, FREE, json_lex(), JSON_OUT_OF_MEMORY, JSON_PARSE_VALUE, JSON_SUCCESS, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, JSONLEX_CTX_OWNS_TOKENS, len, lex_peek(), memcpy(), JsonLexContext::need_escapes, report_parse_error(), result, JsonSemAction::scalar, sem, JsonSemAction::semstate, STRDUP, JsonLexContext::strval, JsonLexContext::token_start, JsonLexContext::token_terminator, and val.

Referenced by parse_array_element(), parse_object_field(), and pg_parse_json().

◆ pg_parse_json()

JsonParseErrorType pg_parse_json ( JsonLexContext lex,
const JsonSemAction sem 
)

Definition at line 744 of file jsonapi.c.

746{
747#ifdef FORCE_JSON_PSTACK
748 /*
749 * We don't need partial token processing, there is only one chunk. But we
750 * still need to init the partial token string so that freeJsonLexContext
751 * works, so perform the full incremental initialization.
752 */
754 return JSON_OUT_OF_MEMORY;
755
756 return pg_parse_json_incremental(lex, sem, lex->input, lex->input_length, true);
757
758#else
759
762
763 if (lex == &failed_oom)
764 return JSON_OUT_OF_MEMORY;
765 if (lex->incremental)
767
768 /* get the initial token */
769 result = json_lex(lex);
770 if (result != JSON_SUCCESS)
771 return result;
772
773 tok = lex_peek(lex);
774
775 /* parse by recursive descent */
776 switch (tok)
777 {
779 result = parse_object(lex, sem);
780 break;
782 result = parse_array(lex, sem);
783 break;
784 default:
785 result = parse_scalar(lex, sem); /* json can be a bare scalar */
786 }
787
788 if (result == JSON_SUCCESS)
790
791 return result;
792#endif
JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex, const JsonSemAction *sem, const char *json, size_t len, bool is_last)
Definition jsonapi.c:868

References allocate_incremental_state(), failed_oom, fb(), JsonLexContext::incremental, JsonLexContext::input, JsonLexContext::input_length, JSON_INVALID_LEXER_TYPE, json_lex(), JSON_OUT_OF_MEMORY, JSON_PARSE_END, JSON_SUCCESS, JSON_TOKEN_ARRAY_START, JSON_TOKEN_END, JSON_TOKEN_OBJECT_START, lex_expect(), lex_peek(), parse_array(), parse_object(), parse_scalar(), pg_parse_json_incremental(), result, and sem.

Referenced by handle_oauth_sasl_error(), json_parse_manifest(), json_validate(), main(), parse_oauth_json(), pg_dependencies_in(), pg_ndistinct_in(), pg_parse_json_or_errsave(), and test_gb18030_json().

◆ pg_parse_json_incremental()

JsonParseErrorType pg_parse_json_incremental ( JsonLexContext lex,
const JsonSemAction sem,
const char json,
size_t  len,
bool  is_last 
)

Definition at line 868 of file jsonapi.c.

874{
878 JsonParserStack *pstack = lex->pstack;
879
880 if (lex == &failed_oom || lex->inc_state == &failed_inc_oom)
881 return JSON_OUT_OF_MEMORY;
882 if (!lex->incremental)
884
885 lex->input = lex->token_terminator = lex->line_start = json;
886 lex->input_length = len;
888 lex->inc_state->started = true;
889
890 /* get the initial token */
891 result = json_lex(lex);
892 if (result != JSON_SUCCESS)
893 return result;
894
895 tok = lex_peek(lex);
896
897 /* use prediction stack for incremental parsing */
898
899 if (!have_prediction(pstack))
900 {
902
903 push_prediction(pstack, goal);
904 }
905
906 while (have_prediction(pstack))
907 {
908 char top = pop_prediction(pstack);
909 td_entry entry;
910
911 /*
912 * these first two branches are the guts of the Table Driven method
913 */
914 if (top == tok)
915 {
916 /*
917 * tok can only be a terminal symbol, so top must be too. the
918 * token matches the top of the stack, so get the next token.
919 */
920 if (tok < JSON_TOKEN_END)
921 {
922 result = json_lex(lex);
923 if (result != JSON_SUCCESS)
924 return result;
925 tok = lex_peek(lex);
926 }
927 }
928 else if (IS_NT(top) && (entry = td_parser_table[OFS(top)][tok]).prod != NULL)
929 {
930 /*
931 * the token is in the director set for a production of the
932 * non-terminal at the top of the stack, so push the reversed RHS
933 * of the production onto the stack.
934 */
935 push_prediction(pstack, entry);
936 }
937 else if (IS_SEM(top))
938 {
939 /*
940 * top is a semantic action marker, so take action accordingly.
941 * It's important to have these markers in the prediction stack
942 * before any token they might need so we don't advance the token
943 * prematurely. Note in a couple of cases we need to do something
944 * both before and after the token.
945 */
946 switch (top)
947 {
948 case JSON_SEM_OSTART:
949 {
951
952 if (lex->lex_level >= JSON_TD_MAX_STACK)
954
955 if (ostart != NULL)
956 {
957 result = (*ostart) (sem->semstate);
958 if (result != JSON_SUCCESS)
959 return result;
960 }
961
962 if (!inc_lex_level(lex))
963 return JSON_OUT_OF_MEMORY;
964 }
965 break;
966 case JSON_SEM_OEND:
967 {
969
970 dec_lex_level(lex);
971 if (oend != NULL)
972 {
973 result = (*oend) (sem->semstate);
974 if (result != JSON_SUCCESS)
975 return result;
976 }
977 }
978 break;
979 case JSON_SEM_ASTART:
980 {
982
983 if (lex->lex_level >= JSON_TD_MAX_STACK)
985
986 if (astart != NULL)
987 {
988 result = (*astart) (sem->semstate);
989 if (result != JSON_SUCCESS)
990 return result;
991 }
992
993 if (!inc_lex_level(lex))
994 return JSON_OUT_OF_MEMORY;
995 }
996 break;
997 case JSON_SEM_AEND:
998 {
1000
1001 dec_lex_level(lex);
1002 if (aend != NULL)
1003 {
1004 result = (*aend) (sem->semstate);
1005 if (result != JSON_SUCCESS)
1006 return result;
1007 }
1008 }
1009 break;
1011 {
1012 /*
1013 * all we do here is save out the field name. We have
1014 * to wait to get past the ':' to see if the next
1015 * value is null so we can call the semantic routine
1016 */
1017 char *fname = NULL;
1020
1021 if ((ostart != NULL || oend != NULL) && lex->need_escapes)
1022 {
1023 fname = STRDUP(lex->strval->data);
1024 if (fname == NULL)
1025 return JSON_OUT_OF_MEMORY;
1026 }
1027 set_fname(lex, fname);
1028 }
1029 break;
1031 {
1032 /*
1033 * the current token should be the first token of the
1034 * value
1035 */
1036 bool isnull = tok == JSON_TOKEN_NULL;
1038
1039 set_fnull(lex, isnull);
1040
1041 if (ostart != NULL)
1042 {
1043 char *fname = get_fname(lex);
1044
1045 result = (*ostart) (sem->semstate, fname, isnull);
1046 if (result != JSON_SUCCESS)
1047 return result;
1048 }
1049 }
1050 break;
1052 {
1054
1055 if (oend != NULL)
1056 {
1057 char *fname = get_fname(lex);
1058 bool isnull = get_fnull(lex);
1059
1060 result = (*oend) (sem->semstate, fname, isnull);
1061 if (result != JSON_SUCCESS)
1062 return result;
1063 }
1064 }
1065 break;
1067 {
1069 bool isnull = tok == JSON_TOKEN_NULL;
1070
1071 set_fnull(lex, isnull);
1072
1073 if (astart != NULL)
1074 {
1075 result = (*astart) (sem->semstate, isnull);
1076 if (result != JSON_SUCCESS)
1077 return result;
1078 }
1079 }
1080 break;
1081 case JSON_SEM_AELEM_END:
1082 {
1084
1085 if (aend != NULL)
1086 {
1087 bool isnull = get_fnull(lex);
1088
1089 result = (*aend) (sem->semstate, isnull);
1090 if (result != JSON_SUCCESS)
1091 return result;
1092 }
1093 }
1094 break;
1096 {
1098
1099 pstack->scalar_val = NULL;
1100
1101 if (sfunc != NULL)
1102 {
1103 /*
1104 * extract the de-escaped string value, or the raw
1105 * lexeme
1106 */
1107 /*
1108 * XXX copied from RD parser but looks like a
1109 * buglet
1110 */
1111 if (tok == JSON_TOKEN_STRING)
1112 {
1113 if (lex->need_escapes)
1114 {
1115 pstack->scalar_val = STRDUP(lex->strval->data);
1116 if (pstack->scalar_val == NULL)
1117 return JSON_OUT_OF_MEMORY;
1118 }
1119 }
1120 else
1121 {
1123
1124 pstack->scalar_val = ALLOC(tlen + 1);
1125 if (pstack->scalar_val == NULL)
1126 return JSON_OUT_OF_MEMORY;
1127
1128 memcpy(pstack->scalar_val, lex->token_start, tlen);
1129 pstack->scalar_val[tlen] = '\0';
1130 }
1131 pstack->scalar_tok = tok;
1132 }
1133 }
1134 break;
1136 {
1137 /*
1138 * We'd like to be able to get rid of this business of
1139 * two bits of scalar action, but we can't. It breaks
1140 * certain semantic actions which expect that when
1141 * called the lexer has consumed the item. See for
1142 * example get_scalar() in jsonfuncs.c.
1143 */
1145
1146 if (sfunc != NULL)
1147 {
1148 result = (*sfunc) (sem->semstate, pstack->scalar_val, pstack->scalar_tok);
1149
1150 /*
1151 * Either ownership of the token passed to the
1152 * callback, or we need to free it now. Either
1153 * way, clear our pointer to it so it doesn't get
1154 * freed in the future.
1155 */
1156 if (lex->flags & JSONLEX_CTX_OWNS_TOKENS)
1157 FREE(pstack->scalar_val);
1158 pstack->scalar_val = NULL;
1159
1160 if (result != JSON_SUCCESS)
1161 return result;
1162 }
1163 }
1164 break;
1165 default:
1166 /* should not happen */
1167 break;
1168 }
1169 }
1170 else
1171 {
1172 /*
1173 * The token didn't match the stack top if it's a terminal nor a
1174 * production for the stack top if it's a non-terminal.
1175 *
1176 * Various cases here are Asserted to be not possible, as the
1177 * token would not appear at the top of the prediction stack
1178 * unless the lookahead matched.
1179 */
1180 switch (top)
1181 {
1182 case JSON_TOKEN_STRING:
1183 if (next_prediction(pstack) == JSON_TOKEN_COLON)
1184 ctx = JSON_PARSE_STRING;
1185 else
1186 {
1187 Assert(false);
1188 ctx = JSON_PARSE_VALUE;
1189 }
1190 break;
1191 case JSON_TOKEN_NUMBER:
1192 case JSON_TOKEN_TRUE:
1193 case JSON_TOKEN_FALSE:
1194 case JSON_TOKEN_NULL:
1197 Assert(false);
1198 ctx = JSON_PARSE_VALUE;
1199 break;
1201 Assert(false);
1203 break;
1205 Assert(false);
1207 break;
1208 case JSON_TOKEN_COMMA:
1209 Assert(false);
1210 if (next_prediction(pstack) == JSON_TOKEN_STRING)
1212 else
1214 break;
1215 case JSON_TOKEN_COLON:
1217 break;
1218 case JSON_TOKEN_END:
1219 ctx = JSON_PARSE_END;
1220 break;
1223 break;
1226 break;
1229 break;
1230 case JSON_NT_KEY_PAIRS:
1232 break;
1233 default:
1234 ctx = JSON_PARSE_VALUE;
1235 }
1236 return report_parse_error(ctx, lex);
1237 }
1238 }
1239
1240 return JSON_SUCCESS;
#define JSON_TD_MAX_STACK
Definition jsonapi.c:431
#define TD_ENTRY(PROD)
Definition jsonapi.c:239
static void set_fnull(JsonLexContext *lex, bool fnull)
Definition jsonapi.c:668
#define OFS(NT)
Definition jsonapi.c:177
static bool inc_lex_level(JsonLexContext *lex)
Definition jsonapi.c:561
static bool have_prediction(JsonParserStack *pstack)
Definition jsonapi.c:641
static char next_prediction(JsonParserStack *pstack)
Definition jsonapi.c:634
static void push_prediction(JsonParserStack *pstack, td_entry entry)
Definition jsonapi.c:620
#define IS_NT(x)
Definition jsonapi.c:180
static char * get_fname(JsonLexContext *lex)
Definition jsonapi.c:662
static char JSON_PROD_GOAL[]
Definition jsonapi.c:272
#define IS_SEM(x)
Definition jsonapi.c:179
static td_entry td_parser_table[JSON_NUM_NONTERMINALS][JSON_NUM_TERMINALS]
Definition jsonapi.c:241
static char pop_prediction(JsonParserStack *pstack)
Definition jsonapi.c:627
static bool get_fnull(JsonLexContext *lex)
Definition jsonapi.c:674
static void dec_lex_level(JsonLexContext *lex)
Definition jsonapi.c:613
JsonTokenType scalar_tok
Definition jsonapi.c:152
Definition jsonapi.c:234

References ALLOC, JsonSemAction::array_element_end, JsonSemAction::array_element_start, JsonSemAction::array_end, JsonSemAction::array_start, Assert, dec_lex_level(), failed_inc_oom, failed_oom, fb(), JsonLexContext::flags, FREE, get_fname(), get_fnull(), have_prediction(), inc_lex_level(), JsonLexContext::inc_state, JsonLexContext::incremental, JsonLexContext::input, JsonLexContext::input_length, JsonIncrementalState::is_last_chunk, IS_NT, IS_SEM, JSON_INVALID_LEXER_TYPE, json_lex(), JSON_NESTING_TOO_DEEP, JSON_NT_ARRAY_ELEMENTS, JSON_NT_KEY_PAIRS, JSON_NT_MORE_ARRAY_ELEMENTS, JSON_NT_MORE_KEY_PAIRS, JSON_OUT_OF_MEMORY, JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_PARSE_END, JSON_PARSE_OBJECT_LABEL, JSON_PARSE_OBJECT_NEXT, JSON_PARSE_OBJECT_START, JSON_PARSE_STRING, JSON_PARSE_VALUE, JSON_PROD_GOAL, JSON_SEM_AELEM_END, JSON_SEM_AELEM_START, JSON_SEM_AEND, JSON_SEM_ASTART, JSON_SEM_OEND, JSON_SEM_OFIELD_END, JSON_SEM_OFIELD_INIT, JSON_SEM_OFIELD_START, JSON_SEM_OSTART, JSON_SEM_SCALAR_CALL, JSON_SEM_SCALAR_INIT, JSON_SUCCESS, JSON_TD_MAX_STACK, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COLON, JSON_TOKEN_COMMA, JSON_TOKEN_END, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_END, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, JSONLEX_CTX_OWNS_TOKENS, len, JsonLexContext::lex_level, lex_peek(), JsonLexContext::line_start, memcpy(), JsonLexContext::need_escapes, next_prediction(), JsonSemAction::object_end, JsonSemAction::object_field_end, JsonSemAction::object_field_start, JsonSemAction::object_start, OFS, pop_prediction(), JsonLexContext::pstack, push_prediction(), report_parse_error(), result, JsonSemAction::scalar, JsonParserStack::scalar_tok, JsonParserStack::scalar_val, sem, JsonSemAction::semstate, set_fname(), set_fnull(), JsonIncrementalState::started, STRDUP, JsonLexContext::strval, TD_ENTRY, td_parser_table, JsonLexContext::token_start, and JsonLexContext::token_terminator.

Referenced by json_parse_manifest_incremental_chunk(), main(), and pg_parse_json().

◆ pop_prediction()

static char pop_prediction ( JsonParserStack pstack)
inlinestatic

Definition at line 627 of file jsonapi.c.

629{
630 Assert(pstack->pred_index > 0);
631 return pstack->prediction[--pstack->pred_index];

References Assert, JsonParserStack::pred_index, and JsonParserStack::prediction.

Referenced by pg_parse_json_incremental().

◆ push_prediction()

static void push_prediction ( JsonParserStack pstack,
td_entry  entry 
)
inlinestatic

Definition at line 620 of file jsonapi.c.

622{
623 memcpy(pstack->prediction + pstack->pred_index, entry.prod, entry.len);
624 pstack->pred_index += entry.len;
char * prod
Definition jsonapi.c:236
size_t len
Definition jsonapi.c:235

References td_entry::len, memcpy(), JsonParserStack::pred_index, JsonParserStack::prediction, and td_entry::prod.

Referenced by pg_parse_json_incremental().

◆ report_parse_error()

static JsonParseErrorType report_parse_error ( JsonParseContext  ctx,
JsonLexContext lex 
)
static

Definition at line 2409 of file jsonapi.c.

2411{
2412 /* Handle case where the input ended prematurely. */
2413 if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
2414 return JSON_EXPECTED_MORE;
2415
2416 /* Otherwise choose the error type based on the parsing context. */
2417 switch (ctx)
2418 {
2419 case JSON_PARSE_END:
2420 return JSON_EXPECTED_END;
2421 case JSON_PARSE_VALUE:
2422 return JSON_EXPECTED_JSON;
2423 case JSON_PARSE_STRING:
2424 return JSON_EXPECTED_STRING;
2432 return JSON_EXPECTED_COLON;
2436 return JSON_EXPECTED_STRING;
2437 }
2438
2439 /*
2440 * We don't use a default: case, so that the compiler will warn about
2441 * unhandled enum values.
2442 */
2443 Assert(false);
2444 return JSON_SUCCESS; /* silence stupider compilers */

References Assert, fb(), JSON_EXPECTED_ARRAY_FIRST, JSON_EXPECTED_ARRAY_NEXT, JSON_EXPECTED_COLON, JSON_EXPECTED_END, JSON_EXPECTED_JSON, JSON_EXPECTED_MORE, JSON_EXPECTED_OBJECT_FIRST, JSON_EXPECTED_OBJECT_NEXT, JSON_EXPECTED_STRING, JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_PARSE_END, JSON_PARSE_OBJECT_COMMA, JSON_PARSE_OBJECT_LABEL, JSON_PARSE_OBJECT_NEXT, JSON_PARSE_OBJECT_START, JSON_PARSE_STRING, JSON_PARSE_VALUE, JSON_SUCCESS, JSON_TOKEN_END, JsonLexContext::token_start, and JsonLexContext::token_type.

Referenced by lex_expect(), parse_object(), parse_object_field(), parse_scalar(), and pg_parse_json_incremental().

◆ set_fname()

static void set_fname ( JsonLexContext lex,
char fname 
)
inlinestatic

Definition at line 647 of file jsonapi.c.

649{
651 {
652 /*
653 * Don't leak prior fnames. If one hasn't been assigned yet,
654 * inc_lex_level ensured that it's NULL (and therefore safe to free).
655 */
656 FREE(lex->pstack->fnames[lex->lex_level]);
657 }
658
659 lex->pstack->fnames[lex->lex_level] = fname;

References JsonLexContext::flags, JsonParserStack::fnames, FREE, JSONLEX_CTX_OWNS_TOKENS, JsonLexContext::lex_level, and JsonLexContext::pstack.

Referenced by dec_lex_level(), and pg_parse_json_incremental().

◆ set_fnull()

static void set_fnull ( JsonLexContext lex,
bool  fnull 
)
inlinestatic

Definition at line 668 of file jsonapi.c.

670{
671 lex->pstack->fnull[lex->lex_level] = fnull;

References JsonParserStack::fnull, JsonLexContext::lex_level, and JsonLexContext::pstack.

Referenced by pg_parse_json_incremental().

◆ setJsonLexContextOwnsTokens()

void setJsonLexContextOwnsTokens ( JsonLexContext lex,
bool  owned_by_context 
)

Definition at line 542 of file jsonapi.c.

544{
545 if (lex->incremental && lex->inc_state->started)
546 {
547 /*
548 * Switching this flag after parsing has already started is a
549 * programming error.
550 */
551 Assert(false);
552 return;
553 }
554
557 else

References Assert, fb(), JsonLexContext::flags, JsonLexContext::inc_state, JsonLexContext::incremental, JSONLEX_CTX_OWNS_TOKENS, and JsonIncrementalState::started.

Referenced by handle_oauth_sasl_error(), main(), and parse_oauth_json().

Variable Documentation

◆ failed_inc_oom

JsonIncrementalState failed_inc_oom
static

Definition at line 295 of file jsonapi.c.

Referenced by allocate_incremental_state(), json_lex(), and pg_parse_json_incremental().

◆ failed_oom

◆ JSON_PROD_ARRAY

◆ JSON_PROD_ARRAY_ELEMENTS

◆ JSON_PROD_EPSILON

char JSON_PROD_EPSILON[] = {0}
static

Definition at line 186 of file jsonapi.c.

186{0}; /* epsilon - an empty production */

◆ JSON_PROD_GOAL

char JSON_PROD_GOAL[] = {JSON_TOKEN_END, JSON_NT_JSON, 0}
static

Definition at line 272 of file jsonapi.c.

Referenced by pg_parse_json_incremental().

◆ JSON_PROD_KEY_PAIRS

◆ JSON_PROD_MORE_ARRAY_ELEMENTS

◆ JSON_PROD_MORE_KEY_PAIRS

◆ JSON_PROD_OBJECT

◆ JSON_PROD_SCALAR_FALSE

char JSON_PROD_SCALAR_FALSE[] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_FALSE, JSON_SEM_SCALAR_INIT, 0}
static

◆ JSON_PROD_SCALAR_NULL

char JSON_PROD_SCALAR_NULL[] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_NULL, JSON_SEM_SCALAR_INIT, 0}
static

Definition at line 201 of file jsonapi.c.

◆ JSON_PROD_SCALAR_NUMBER

char JSON_PROD_SCALAR_NUMBER[] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_NUMBER, JSON_SEM_SCALAR_INIT, 0}
static

◆ JSON_PROD_SCALAR_STRING

char JSON_PROD_SCALAR_STRING[] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_STRING, JSON_SEM_SCALAR_INIT, 0}
static

◆ JSON_PROD_SCALAR_TRUE

char JSON_PROD_SCALAR_TRUE[] = {JSON_SEM_SCALAR_CALL, JSON_TOKEN_TRUE, JSON_SEM_SCALAR_INIT, 0}
static

Definition at line 195 of file jsonapi.c.

◆ nullSemAction

const JsonSemAction nullSemAction
Initial value:

Definition at line 287 of file jsonapi.c.

288{
289 NULL, NULL, NULL, NULL, NULL,
291};

Referenced by json_count_array_elements(), json_in(), json_recv(), json_validate(), and main().

◆ td_parser_table

Initial value:
=
{
}
static char JSON_PROD_MORE_KEY_PAIRS[]
Definition jsonapi.c:219
static char JSON_PROD_KEY_PAIRS[]
Definition jsonapi.c:216
static char JSON_PROD_SCALAR_STRING[]
Definition jsonapi.c:189
static char JSON_PROD_ARRAY_ELEMENTS[]
Definition jsonapi.c:210
static char JSON_PROD_SCALAR_NUMBER[]
Definition jsonapi.c:192
static char JSON_PROD_EPSILON[]
Definition jsonapi.c:186
static char JSON_PROD_SCALAR_NULL[]
Definition jsonapi.c:201
static char JSON_PROD_MORE_ARRAY_ELEMENTS[]
Definition jsonapi.c:213
static char JSON_PROD_SCALAR_FALSE[]
Definition jsonapi.c:198
static char JSON_PROD_OBJECT[]
Definition jsonapi.c:204
static char JSON_PROD_ARRAY[]
Definition jsonapi.c:207
static char JSON_PROD_SCALAR_TRUE[]
Definition jsonapi.c:195

Definition at line 241 of file jsonapi.c.

242{
243 /* JSON */
251 /* ARRAY_ELEMENTS */
260 /* MORE_ARRAY_ELEMENTS */
263 /* KEY_PAIRS */
266 /* MORE_KEY_PAIRS */
269};

Referenced by pg_parse_json_incremental().