1/*-------------------------------------------------------------------------
4 * JSON parser and lexer interfaces
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/common/jsonapi.c
12 *-------------------------------------------------------------------------
24#ifdef JSONAPI_USE_PQEXPBUFFER
32 * By default, we will use palloc/pfree along with StringInfo. In libpq,
33 * use malloc and PQExpBuffer, and return JSON_OUT_OF_MEMORY on out-of-memory.
35#ifdef JSONAPI_USE_PQEXPBUFFER
37#define STRDUP(s) strdup(s)
38#define ALLOC(size) malloc(size)
39#define ALLOC0(size) calloc(1, size)
40#define REALLOC realloc
41#define FREE(s) free(s)
43#define jsonapi_appendStringInfo appendPQExpBuffer
44#define jsonapi_appendBinaryStringInfo appendBinaryPQExpBuffer
45#define jsonapi_appendStringInfoChar appendPQExpBufferChar
46/* XXX should we add a macro version to PQExpBuffer? */
47#define jsonapi_appendStringInfoCharMacro appendPQExpBufferChar
48#define jsonapi_makeStringInfo createPQExpBuffer
49#define jsonapi_initStringInfo initPQExpBuffer
50#define jsonapi_resetStringInfo resetPQExpBuffer
51#define jsonapi_termStringInfo termPQExpBuffer
52#define jsonapi_destroyStringInfo destroyPQExpBuffer
54#else /* !JSONAPI_USE_PQEXPBUFFER */
56 #define STRDUP(s) pstrdup(s)
57 #define ALLOC(size) palloc(size)
58 #define ALLOC0(size) palloc0(size)
59 #define REALLOC repalloc
65 * Backend pfree() doesn't handle NULL pointers like the frontend's does; smooth
66 * that over to reduce mental gymnastics. Avoid multiple evaluation of the macro
67 * argument to avoid future hair-pulling.
69 #define FREE(s) do { \
76 #define jsonapi_appendStringInfo appendStringInfo
77 #define jsonapi_appendBinaryStringInfo appendBinaryStringInfo
78 #define jsonapi_appendStringInfoChar appendStringInfoChar
79 #define jsonapi_appendStringInfoCharMacro appendStringInfoCharMacro
80 #define jsonapi_makeStringInfo makeStringInfo
81 #define jsonapi_initStringInfo initStringInfo
82 #define jsonapi_resetStringInfo resetStringInfo
83 #define jsonapi_termStringInfo(s) pfree((s)->data)
84 #define jsonapi_destroyStringInfo destroyStringInfo
86#endif /* JSONAPI_USE_PQEXPBUFFER */
89 * The context of the parser is maintained by the recursive descent
90 * mechanism, but is passed explicitly to the error reporting routine
91 * for better diagnostics.
93 typedef enum /* contexts of JSON parser */
107 * Setup for table-driven parser.
108 * These enums need to be separate from the JsonTokenType and from each other
109 * so we can have all of them on the prediction stack, which consists of
110 * tokens, non-terminals, and semantic action markers.
138 * struct containing the 3 stacks used in non-recursive parsing,
139 * and the token and value for scalars that need to be preserved
142 * typedef appears in jsonapi.h
149 /* these two are indexed by lex_level */
157 * struct containing state used when there is a possible partial token at the
158 * end of a json chunk when we are doing incremental parsing.
160 * typedef appears in jsonapi.h
171 * constants and macros used in the nonrecursive parser
173 #define JSON_NUM_TERMINALS 13
174 #define JSON_NUM_NONTERMINALS 5
175 #define JSON_NT_OFFSET JSON_NT_JSON
176/* for indexing the table */
177 #define OFS(NT) (NT) - JSON_NT_OFFSET
178/* classify items we get off the stack */
179 #define IS_SEM(x) ((x) & 0x40)
180 #define IS_NT(x) ((x) & 0x20)
183 * These productions are stored in reverse order right to left so that when
184 * they are pushed on the stack what we expect next is at the top of the stack.
203/* JSON -> '{' KEY_PAIRS '}' */
206/* JSON -> '[' ARRAY_ELEMENTS ']' */
209/* ARRAY_ELEMENTS -> JSON MORE_ARRAY_ELEMENTS */
212/* MORE_ARRAY_ELEMENTS -> ',' JSON MORE_ARRAY_ELEMENTS */
215/* KEY_PAIRS -> string ':' JSON MORE_KEY_PAIRS */
218/* MORE_KEY_PAIRS -> ',' string ':' JSON MORE_KEY_PAIRS */
222 * Note: there are also epsilon productions for ARRAY_ELEMENTS,
223 * MORE_ARRAY_ELEMENTS, KEY_PAIRS and MORE_KEY_PAIRS
224 * They are all the same as none require any semantic actions.
228 * Table connecting the productions with their director sets of
230 * Any combination not specified here represents an error.
239 #define TD_ENTRY(PROD) { sizeof(PROD) - 1, (PROD) }
260 /* MORE_ARRAY_ELEMENTS */
271/* the GOAL production. Not stored in the table, but will be the initial contents of the prediction stack */
276 bool *num_err,
size_t *total_len);
286/* the null action object used for pure validation */
289 NULL, NULL, NULL, NULL, NULL,
290 NULL, NULL, NULL, NULL, NULL
293/* sentinels used for out-of-memory conditions */
297/* Parser support routines */
302 * what is the current look_ahead token?
313 * move the lexer to the next token if the current look_ahead token matches
314 * the parameter token. Otherwise, report an error.
325/* chars to consider as part of an alphanumeric token */
326 #define JSON_ALPHANUMERIC_CHAR(c) \
327 (((c) >= 'a' && (c) <= 'z') || \
328 ((c) >= 'A' && (c) <= 'Z') || \
329 ((c) >= '0' && (c) <= '9') || \
334 * Utility function to check if a string is a valid JSON number.
336 * str is of length len, and need not be null-terminated.
349 * json_lex_number expects a leading '-' to have been eaten already.
351 * having to cast away the constness of str is ugly, but there's not much
369 return (!numeric_error) && (total_len == dummy_lex.
input_length);
373 * makeJsonLexContextCstringLen
374 * Initialize the given JsonLexContext object, or create one
376 * If a valid 'lex' pointer is given, it is initialized. This can
377 * be used for stack-allocated structs, saving overhead. If NULL is
378 * given, a new struct is allocated.
380 * If need_escapes is true, ->strval stores the unescaped lexemes.
381 * Unescaping is expensive, so only request it when necessary.
383 * If need_escapes is true or lex was given as NULL, then caller is
384 * responsible for freeing the returned struct, either by calling
385 * freeJsonLexContext() or (in backend environment) via memory context
388 * In shlib code, any out-of-memory failures will be deferred to time
389 * of use; this function is guaranteed to return a valid JsonLexContext.
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
426 * Allocates the internal bookkeeping structures for incremental parsing. This
427 * can only fail in-band with shlib code.
429 #define JS_STACK_CHUNK_SIZE 64
430 #define JS_MAX_PROD_LEN 10 /* more than we need */
431 #define JSON_TD_MAX_STACK 6400 /* hard coded for now - this is a REALLY high
447#ifdef JSONAPI_USE_PQEXPBUFFER
473 * fnames between 0 and lex_level must always be defined so that
474 * freeJsonLexContext() can handle them safely. inc/dec_lex_level() handle
486 * makeJsonLexContextIncremental
488 * Similar to above but set up for use in incremental parsing. That means we
489 * need explicit stacks for predictions, field names and null indicators, but
490 * we don't need the input, that will be handed in bit by bit to the
491 * parse routine. We also need an accumulator for partial tokens in case
492 * the boundary between chunks happens to fall in the middle of a token.
494 * In shlib code, any out-of-memory failures will be deferred to time of use;
495 * this function is guaranteed to return a valid JsonLexContext.
523 /* lex->inc_state tracks the OOM failure; we can return here. */
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
548 * Switching this flag after parsing has already started is a
555 if (owned_by_context)
558 lex->
flags &= ~JSONLEX_CTX_OWNS_TOKENS;
566 size_t new_stack_size;
567 char *new_prediction;
575#ifdef JSONAPI_USE_PQEXPBUFFER
582 new_stack_size *
sizeof(
char *));
583#ifdef JSONAPI_USE_PQEXPBUFFER
590#ifdef JSONAPI_USE_PQEXPBUFFER
604 * Ensure freeJsonLexContext() remains safe even if no fname is
605 * assigned at this level.
616 set_fname(lex, NULL);
/* free the current level's fname, if needed */
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).
681 * Free memory in a JsonLexContext.
683 * There's no need for this if a *lex pointer was given when the object was
684 * made, need_escapes was false, and json_errdetail() was not called; or if (in
685 * backend environment) a memory context delete/reset is imminent.
711 /* Clean up any tokens that were left behind. */
731 * Publicly visible entry point for the JSON parser.
733 * lex is a lexing context, set up for the json to be processed by calling
734 * makeJsonLexContext(). sem is a structure of function pointers to semantic
735 * action routines to be called at appropriate spots during parsing, and a
736 * pointer to a state object to be passed to those routines.
738 * If FORCE_JSON_PSTACK is defined then the routine will call the non-recursive
739 * JSON parser. This is a useful way to validate that it's doing the right
740 * thing at least for non-incremental cases. If this is on we expect to see
741 * regression diffs relating to error messages about stack depth, but no
747#ifdef FORCE_JSON_PSTACK
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.
768 /* get the initial token */
775 /* parse by recursive descent */
796 * json_count_array_elements
798 * Returns number of array elements in lex context at start of array token
799 * until end of array token at same nesting level.
801 * Designed to be called from array_start routines.
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.
819 copylex.
need_escapes =
false;
/* not interested in values here */
852 * pg_parse_json_incremental
854 * Routine for incremental parsing of json. This uses the non-recursive top
855 * down method of the Dragon Book Algorithm 4.3. It's somewhat slower than
856 * the Recursive Descent pattern used above, so we only use it for incremental
859 * The lexing context needs to be set up by a call to
860 * makeJsonLexContextIncremental(). sem is a structure of function pointers
861 * to semantic action routines, which should function exactly as those used
862 * in the recursive descent parser.
864 * This routine can be called repeatedly with chunks of JSON. On the final
865 * chunk is_last must be set to true. len is the length of the json chunk,
866 * which does not need to be null terminated.
890 /* get the initial token */
897 /* use prediction stack for incremental parsing */
912 * these first two branches are the guts of the Table Driven method
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.
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.
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.
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
1021 if ((ostart != NULL || oend != NULL) && lex->
need_escapes)
1033 * the current token should be the first token of the
1045 result = (*ostart) (
sem->
semstate, fname, isnull);
1104 * extract the de-escaped string value, or the raw
1108 * XXX copied from RD parser but looks like a
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.
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.
1166 /* should not happen */
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.
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.
1244 * Recursive Descent parse routines. There is one for each structural
1245 * element in a json document:
1246 * - scalar (string, number, true, false, null)
1260 /* a scalar must be a string, a number, true, false, or null */
1266 /* if no semantic function, just consume the token */
1270 /* extract the de-escaped string value, or the raw lexeme */
1292 /* consume the token */
1301 * invoke the callback, which may take ownership of val. For string
1302 * values, val is NULL if need_escapes is false.
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".
1330 if ((ostart != NULL || oend != NULL) && lex->
need_escapes)
1332 /* fname is NULL if need_escapes is false */
1356 result = (*ostart) (
sem->
semstate, fname, isnull);
1358 goto ofield_cleanup;
1373 goto ofield_cleanup;
1379 goto ofield_cleanup;
1392 * an object is a possibly empty sequence of object fields, separated by
1393 * commas and surrounded by curly braces.
1403 * TODO: clients need some way to put a bound on stack growth. Parse level
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
1445 /* case of an invalid initial token inside the object */
1485 /* an array element is any object, array or scalar */
1515 * an array is a possibly empty sequence of array elements, separated by
1516 * commas and surrounded by square brackets.
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
1574 * Lex one token from the input stream.
1576 * When doing incremental parsing, we can reach the end of the input string
1577 * without having (or knowing we have) a complete token. If it's not the
1578 * final chunk of input, the partial token is then saved to the lex
1579 * structure's ptok StringInfo. On subsequent calls input is appended to this
1580 * buffer until we have something that we think is a complete token,
1581 * which is then lexed using a recursive call to json_lex. Processing then
1582 * continues as normal on subsequent calls.
1584 * Note than when doing incremental processing, the lex.prev_token_terminator
1585 * should not be relied on. It could point into a previous input chunk or
1603 * We just lexed a completed partial token on the last call, so
1611#ifdef JSONAPI_USE_PQEXPBUFFER
1612 /* Make sure our partial token buffer is valid before using it below. */
1623 * We have a partial token. Extend it and if completed lex it by a
1628 bool tok_done =
false;
1632 if (ptok->data[0] ==
'"')
1635 * It's a string. Accumulate characters until we reach an
1640 for (
int i = ptok->len - 1;
i > 0;
i--)
1642 /* count the trailing backslashes on the partial token */
1643 if (ptok->data[
i] ==
'\\')
1655 if (
c ==
'"' && escapes % 2 == 0)
1669 char c = ptok->data[0];
1671 if (
c ==
'-' || (
c >=
'0' &&
c <=
'9'))
1673 /* for numbers look for possible numeric continuations */
1675 bool numend =
false;
1709 * Add any remaining alphanumeric chars. This takes care of the
1710 * {null, false, true} literals as well as any trailing
1711 * alphanumeric junk on non-string tokens.
1737 /* We should have consumed the whole chunk in this case. */
1743 /* json_errdetail() needs access to the accumulated token. */
1750 * Everything up to lex->input[added] has been added to the partial
1751 * token, so move the input past it.
1753 lex->
input += added;
1765 partial_result =
json_lex(&dummy_lex);
1768 * We either have a complete token or an error. In either case we need
1769 * to point to the partial token data for the semantic or error
1770 * routines. If it's not an error we'll readjust on the next call to
1777 * We know the prev_token_terminator must be back in some previous
1778 * piece of input, so we just make it NULL.
1783 * Normally token_start would be ptok->data, but it could be later,
1784 * see json_lex_string's handling of invalid escapes.
1790 /* make sure we've used all the input */
1799 return partial_result;
1800 /* end of partial token processing */
1803 /* Skip leading whitespace. */
1804 while (s < end && (*s ==
' ' || *s ==
'\t' || *s ==
'\n' || *s ==
'\r'))
1814 /* Determine token type. */
1826 /* Single-character token, some kind of punctuation mark. */
1865 /* Negative number. */
1881 /* Positive number. */
1892 * We're not dealing with a string, number, legal
1893 * punctuation mark, or end of string. The only legal
1894 * tokens we might find here are true, false, and null,
1895 * but for error reporting purposes we scan until we see a
1896 * non-alphanumeric character. That way, we can report
1897 * the whole word as an unexpected token, rather than just
1898 * some unintuitive prefix thereof.
1904 * We got some sort of unexpected punctuation or an
1905 * otherwise unexpected character, so just complain about
1906 * that one character.
1923 * We've got a real alphanumeric token here. If it
1924 * happens to be true, false, or null, all is well. If
1931 if (memcmp(s,
"true", 4) == 0)
1933 else if (memcmp(s,
"null", 4) == 0)
1938 else if (p - s == 5 && memcmp(s,
"false", 5) == 0)
1943 }
/* end of switch */
1953 * The next token in the input stream is known to be a string; lex it.
1955 * If lex->strval isn't NULL, fill it with the decoded string.
1956 * Set lex->token_terminator to the end of the decoded input, and in
1957 * success cases, transfer its previous value to lex->prev_token_terminator.
1958 * Return JSON_SUCCESS or an error code.
1960 * Note: be careful that all error exits advance lex->token_terminator
1961 * to the point after the character we detected the error on.
1968 int hi_surrogate = -1;
1970 /* Convenience macros for error exits */
1971#define FAIL_OR_INCOMPLETE_AT_CHAR_START(code) \
1973 if (lex->incremental && !lex->inc_state->is_last_chunk) \
1975 jsonapi_appendBinaryStringInfo(&lex->inc_state->partial_token, \
1977 end - lex->token_start); \
1978 return JSON_INCOMPLETE; \
1980 lex->token_terminator = s; \
1983#define FAIL_AT_CHAR_END(code) \
1985 ptrdiff_t remaining = end - s; \
1987 charlen = pg_encoding_mblen_or_incomplete(lex->input_encoding, \
1989 lex->token_terminator = (charlen <= remaining) ? s + charlen : end; \
1995#ifdef JSONAPI_USE_PQEXPBUFFER
1996 /* make sure initialization succeeded */
2008 /* Premature end of the string. */
2013 else if (*s ==
'\\')
2015 /* OK, we have an escape character. */
2024 for (
i = 1;
i <= 4;
i++)
2029 else if (*s >=
'0' && *s <=
'9')
2030 ch = (ch * 16) + (*s -
'0');
2031 else if (*s >=
'a' && *s <=
'f')
2032 ch = (ch * 16) + (*s -
'a') + 10;
2033 else if (*s >=
'A' && *s <=
'F')
2034 ch = (ch * 16) + (*s -
'A') + 10;
2041 * Combine surrogate pairs.
2045 if (hi_surrogate != -1)
2052 if (hi_surrogate == -1)
2058 if (hi_surrogate != -1)
2062 * Reject invalid cases. We can't have a value above
2063 * 0xFFFF here (since we only accepted 4 hex digits
2064 * above), so no need to test for out-of-range chars.
2068 /* We can't allow this, since our TEXT type doesn't */
2073 * Add the represented character to lex->strval. In the
2074 * backend, we can let pg_unicode_to_server_noerror()
2075 * handle any required character set conversion; in
2076 * frontend, we can only deal with trivial conversions.
2089 /* OK, we can map the code point to UTF8 easily */
2097 else if (ch <= 0x007f)
2099 /* The ASCII range is the same in all encodings */
2104#endif /* FRONTEND */
2109 if (hi_surrogate != -1)
2137 * Not a valid string escape, so signal error. We
2138 * adjust token_start so that just the escape sequence
2139 * is reported, not the whole string.
2145 else if (strchr(
"\"\\/bfnrt", *s) == NULL)
2148 * Simpler processing if we're not bothered about de-escaping
2150 * It's very tempting to remove the strchr() call here and
2151 * replace it with a switch statement, but testing so far has
2152 * shown it's not a performance win.
2162 if (hi_surrogate != -1)
2166 * Skip to the first byte that requires special handling, so we
2167 * can batch calls to jsonapi_appendBinaryStringInfo.
2169 while (p < end -
sizeof(
Vector8) &&
2175 for (; p < end; p++)
2177 if (*p ==
'\\' || *p ==
'"')
2179 else if ((
unsigned char) *p <= 31)
2181 /* Per RFC4627, these characters MUST be escaped. */
2183 * Since *p isn't printable, exclude it from the context
2195 * s will be incremented at the top of the loop, so set it to just
2196 * behind our lookahead position
2202 if (hi_surrogate != -1)
2208#ifdef JSONAPI_USE_PQEXPBUFFER
2213 /* Hooray, we found the end of the string! */
2218#undef FAIL_OR_INCOMPLETE_AT_CHAR_START
2219#undef FAIL_AT_CHAR_END
2223 * The next token in the input stream is known to be a number; lex it.
2225 * In JSON, a number consists of four parts:
2227 * (1) An optional minus sign ('-').
2229 * (2) Either a single '0', or a string of one or more digits that does not
2232 * (3) An optional decimal part, consisting of a period ('.') followed by
2233 * one or more digits. (Note: While this part can be omitted
2234 * completely, it's not OK to have only the decimal point without
2235 * any digits afterwards.)
2237 * (4) An optional exponent part, consisting of 'e' or 'E', optionally
2238 * followed by '+' or '-', followed by one or more digits. (Note:
2239 * As with the decimal part, if 'e' or 'E' is present, it must be
2240 * followed by at least one digit.)
2242 * The 's' argument to this function points to the ostensible beginning
2243 * of part 2 - i.e. the character after any optional minus sign, or the
2244 * first character of the string if there is none.
2246 * If num_err is not NULL, we return an error flag to *num_err rather than
2247 * raising an error for a badly-formed number. Also, if total_len is not NULL
2248 * the distance from lex->input to the token end+1 is returned to *total_len.
2252 bool *num_err,
size_t *total_len)
2257 /* Part (1): leading sign indicator. */
2258 /* Caller already did this for us; so do nothing. */
2260 /* Part (2): parse main digit string. */
2261 if (len < lex->input_length && *s ==
'0')
2266 else if (len < lex->input_length && *s >=
'1' && *s <=
'9')
2272 }
while (len < lex->input_length && *s >=
'0' && *s <=
'9');
2277 /* Part (3): parse optional decimal portion. */
2278 if (len < lex->input_length && *s ==
'.')
2290 }
while (len < lex->input_length && *s >=
'0' && *s <=
'9');
2294 /* Part (4): parse optional exponent. */
2295 if (len < lex->input_length && (*s ==
'e' || *s ==
'E'))
2299 if (len < lex->input_length && (*s ==
'+' || *s ==
'-'))
2312 }
while (len < lex->input_length && *s >=
'0' && *s <=
'9');
2317 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
2318 * here should be considered part of the token for error-reporting
2324 if (total_len != NULL)
2332 if (num_err != NULL)
2337 else if (num_err != NULL)
2339 /* let the caller handle any error */
2344 /* return token endpoint */
2347 /* handle error if any */
2356 * Report a parse error.
2358 * lex->token_start and lex->token_terminator must identify the current token.
2363 /* Handle case where the input ended prematurely. */
2367 /* Otherwise choose the error type based on the parsing context. */
2391 * We don't use a default: case, so that the compiler will warn about
2392 * unhandled enum values.
2399 * Construct an (already translated) detail message for a JSON error.
2401 * The returned pointer should not be freed, the allocation is either static
2402 * or owned by the JsonLexContext.
2409 /* Short circuit. Allocating anything for this case is unhelpful. */
2410 return _(
"out of memory");
2419 * A helper for error messages that should print the current token. The
2420 * format must contain exactly one %.*s specifier.
2422#define json_token_error(lex, format) \
2423 jsonapi_appendStringInfo((lex)->errormsg, _(format), \
2424 (int) ((lex)->token_terminator - (lex)->token_start), \
2425 (lex)->token_start);
2431 /* fall through to the error code after switch */
2435 return _(
"Recursive descent parser cannot use incremental lexer.");
2437 return _(
"Incremental parser requires incremental lexer.");
2439 return (
_(
"JSON nested too deep, maximum permitted depth is 6400."));
2445 _(
"Character with value 0x%02x must be escaped."),
2452 json_token_error(lex,
"Expected array element or \"]\", but found \"%.*s\".");
2464 return _(
"The input string ended unexpectedly.");
2478 /* should have been handled above; use the error path */
2481 return _(
"\\u0000 cannot be converted to text.");
2483 return _(
"\"\\u\" must be followed by four hexadecimal digits.");
2485 /* note: this case is only reachable in frontend not backend */
2486 return _(
"Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8.");
2490 * Note: this case is only reachable in backend and not frontend.
2491 * #ifdef it away so the frontend doesn't try to link against
2492 * backend functionality.
2495 return psprintf(
_(
"Unicode escape value could not be translated to the server's encoding %s."),
2502 return _(
"Unicode high surrogate must not follow a high surrogate.");
2504 return _(
"Unicode low surrogate must follow a high surrogate.");
2506 /* fall through to the error code after switch */
2509#undef json_token_error
2511 /* Note that lex->errormsg can be NULL in shlib code. */
2515 * We don't use a default: case, so that the compiler will warn about
2516 * unhandled enum values. But this needs to be here anyway to cover
2517 * the possibility of an incorrect input.
2520 "unexpected json parse error type: %d",
2524#ifdef JSONAPI_USE_PQEXPBUFFER
2526 return _(
"out of memory while constructing error description");
Assert(PointerIsAligned(start, uint64))
JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex, const JsonSemAction *sem, const char *json, size_t len, bool is_last)
#define JSON_TD_MAX_STACK
@ JSON_PARSE_OBJECT_LABEL
@ JSON_PARSE_OBJECT_START
@ JSON_PARSE_OBJECT_COMMA
JsonLexContext * makeJsonLexContextIncremental(JsonLexContext *lex, int encoding, bool need_escapes)
static void set_fnull(JsonLexContext *lex, bool fnull)
#define JSON_NUM_TERMINALS
static char JSON_PROD_MORE_KEY_PAIRS[]
bool IsValidJsonNumber(const char *str, size_t len)
#define jsonapi_destroyStringInfo
static JsonParseErrorType json_lex_string(JsonLexContext *lex)
#define JSON_ALPHANUMERIC_CHAR(c)
static char JSON_PROD_KEY_PAIRS[]
#define JSON_NUM_NONTERMINALS
static char JSON_PROD_SCALAR_STRING[]
JsonParseErrorType pg_parse_json(JsonLexContext *lex, const JsonSemAction *sem)
static bool inc_lex_level(JsonLexContext *lex)
static char JSON_PROD_ARRAY_ELEMENTS[]
static bool have_prediction(JsonParserStack *pstack)
static void set_fname(JsonLexContext *lex, char *fname)
static char JSON_PROD_SCALAR_NUMBER[]
#define json_token_error(lex, format)
static char next_prediction(JsonParserStack *pstack)
static void push_prediction(JsonParserStack *pstack, td_entry entry)
static JsonLexContext failed_oom
#define jsonapi_appendStringInfoCharMacro
static char * get_fname(JsonLexContext *lex)
static char JSON_PROD_GOAL[]
#define jsonapi_makeStringInfo
static JsonTokenType lex_peek(JsonLexContext *lex)
static char JSON_PROD_EPSILON[]
static JsonParseErrorType parse_object(JsonLexContext *lex, const JsonSemAction *sem)
#define jsonapi_initStringInfo
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
#define JS_STACK_CHUNK_SIZE
void setJsonLexContextOwnsTokens(JsonLexContext *lex, bool owned_by_context)
static char JSON_PROD_SCALAR_NULL[]
static bool allocate_incremental_state(JsonLexContext *lex)
#define jsonapi_resetStringInfo
static JsonParseErrorType report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
static JsonParseErrorType lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
static JsonIncrementalState failed_inc_oom
static JsonParseErrorType json_lex_number(JsonLexContext *lex, const char *s, bool *num_err, size_t *total_len)
static char JSON_PROD_MORE_ARRAY_ELEMENTS[]
const JsonSemAction nullSemAction
static td_entry td_parser_table[JSON_NUM_NONTERMINALS][JSON_NUM_TERMINALS]
static JsonParseErrorType parse_scalar(JsonLexContext *lex, const JsonSemAction *sem)
static char pop_prediction(JsonParserStack *pstack)
static JsonParseErrorType parse_object_field(JsonLexContext *lex, const JsonSemAction *sem)
#define jsonapi_termStringInfo(s)
#define jsonapi_appendBinaryStringInfo
static char JSON_PROD_SCALAR_FALSE[]
static bool get_fnull(JsonLexContext *lex)
JsonParseErrorType json_lex(JsonLexContext *lex)
#define jsonapi_appendStringInfoChar
static char JSON_PROD_OBJECT[]
#define jsonapi_appendStringInfo
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
JsonParseErrorType json_count_array_elements(JsonLexContext *lex, int *elements)
static JsonParseErrorType parse_array(JsonLexContext *lex, const JsonSemAction *sem)
static JsonParseErrorType parse_array_element(JsonLexContext *lex, const JsonSemAction *sem)
void freeJsonLexContext(JsonLexContext *lex)
@ JSON_NT_MORE_ARRAY_ELEMENTS
static char JSON_PROD_ARRAY[]
#define FAIL_OR_INCOMPLETE_AT_CHAR_START(code)
static char JSON_PROD_SCALAR_TRUE[]
#define FAIL_AT_CHAR_END(code)
static void dec_lex_level(JsonLexContext *lex)
JsonParseErrorType(* json_struct_action)(void *state)
JsonParseErrorType(* json_aelem_action)(void *state, bool isnull)
#define JSONLEX_FREE_STRVAL
#define jsonapi_StrValType
@ JSON_EXPECTED_ARRAY_FIRST
@ JSON_UNICODE_HIGH_SURROGATE
@ JSON_EXPECTED_OBJECT_FIRST
@ JSON_UNICODE_CODE_POINT_ZERO
@ JSON_INVALID_LEXER_TYPE
@ JSON_UNICODE_ESCAPE_FORMAT
@ JSON_UNICODE_UNTRANSLATABLE
@ JSON_EXPECTED_OBJECT_NEXT
@ JSON_EXPECTED_ARRAY_NEXT
@ JSON_UNICODE_HIGH_ESCAPE
@ JSON_UNICODE_LOW_SURROGATE
JsonParseErrorType(* json_ofield_action)(void *state, char *fname, bool isnull)
#define JSONLEX_FREE_STRUCT
@ JSON_TOKEN_OBJECT_START
#define JSONLEX_CTX_OWNS_TOKENS
JsonParseErrorType(* json_scalar_action)(void *state, char *token, JsonTokenType tokentype)
bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s)
const char * GetDatabaseEncodingName(void)
static bool pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
static bool pg_lfind8(uint8 key, uint8 *base, uint32 nelem)
static unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
#define MAX_UNICODE_EQUIVALENT_STRING
static pg_wchar surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second)
static bool is_utf16_surrogate_first(pg_wchar c)
static bool is_utf16_surrogate_second(pg_wchar c)
#define PQExpBufferBroken(str)
#define PQExpBufferDataBroken(buf)
char * psprintf(const char *fmt,...)
void check_stack_depth(void)
void appendStringInfoString(StringInfo str, const char *s)
jsonapi_StrValType partial_token
const char * prev_token_terminator
struct jsonapi_StrValType * strval
struct jsonapi_StrValType * errormsg
JsonIncrementalState * inc_state
const char * token_terminator
json_struct_action array_end
json_struct_action object_start
json_ofield_action object_field_start
json_aelem_action array_element_start
json_scalar_action scalar
json_aelem_action array_element_end
json_struct_action array_start
json_struct_action object_end
json_ofield_action object_field_end