Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * read.c
4 : : * routines to convert a string (legal ascii representation of node) back
5 : : * to nodes
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : *
11 : : * IDENTIFICATION
12 : : * src/backend/nodes/read.c
13 : : *
14 : : * HISTORY
15 : : * AUTHOR DATE MAJOR EVENT
16 : : * Andrew Yu Nov 2, 1994 file creation
17 : : *
18 : : *-------------------------------------------------------------------------
19 : : */
20 : : #include "postgres.h"
21 : :
22 : : #include <ctype.h>
23 : :
24 : : #include "common/string.h"
25 : : #include "nodes/bitmapset.h"
26 : : #include "nodes/pg_list.h"
27 : : #include "nodes/readfuncs.h"
28 : : #include "nodes/value.h"
29 : :
30 : :
31 : : /*
32 : : * stringToNode -
33 : : * builds a Node tree from its string representation (assumed valid)
34 : : *
35 : : * restore_loc_fields instructs readfuncs.c whether to restore location
36 : : * fields rather than set them to -1. This is currently only supported
37 : : * in builds with DEBUG_NODE_TESTS_ENABLED defined.
38 : : */
39 : : static void *
40 : 1545835 : stringToNodeInternal(const char *str, bool restore_loc_fields)
41 : : {
42 : : ReadNodeContext ctx;
43 : :
44 : : /* initialize the context */
45 : 1545835 : ctx.str = str;
46 : : #ifdef DEBUG_NODE_TESTS_ENABLED
47 : 1545835 : ctx.restore_location_fields = restore_loc_fields;
48 : : #endif
49 : :
50 : : /* do the reading, and return */
51 : 1545835 : return nodeRead(&ctx, NULL, 0);
52 : : }
53 : :
54 : : /*
55 : : * Externally visible entry points
56 : : */
57 : : void *
58 : 272150 : stringToNode(const char *str)
59 : : {
60 : 272150 : return stringToNodeInternal(str, false);
61 : : }
62 : :
63 : : #ifdef DEBUG_NODE_TESTS_ENABLED
64 : :
65 : : void *
66 : 1273685 : stringToNodeWithLocations(const char *str)
67 : : {
68 : 1273685 : return stringToNodeInternal(str, true);
69 : : }
70 : :
71 : : #endif
72 : :
73 : :
74 : : /*****************************************************************************
75 : : *
76 : : * the lisp token parser
77 : : *
78 : : *****************************************************************************/
79 : :
80 : : /* Character classes for pg_strtok() */
81 : : #define TOKCLASS_SPACE 0x01 /* ' ', '\n', '\t' */
82 : : #define TOKCLASS_SPECIAL 0x02 /* '(', ')', '{', '}' */
83 : : #define TOKCLASS_NUL 0x04 /* '\0' */
84 : :
85 : : /* Character class lookup table for pg_strtok() */
86 : : static const uint8 pg_strtok_class[256] = {
87 : : ['\0'] = TOKCLASS_NUL,
88 : : [' '] = TOKCLASS_SPACE,
89 : : ['\n'] = TOKCLASS_SPACE,
90 : : ['\t'] = TOKCLASS_SPACE,
91 : : ['('] = TOKCLASS_SPECIAL,
92 : : [')'] = TOKCLASS_SPECIAL,
93 : : ['{'] = TOKCLASS_SPECIAL,
94 : : ['}'] = TOKCLASS_SPECIAL,
95 : : };
96 : :
97 : : /*
98 : : * pg_strtok --- retrieve next "token" from a string.
99 : : *
100 : : * Works kinda like strtok, except it never modifies the source string.
101 : : * (Instead of storing nulls into the string, the length of the token
102 : : * is returned to the caller.)
103 : : * Also, the rules about what is a token are hard-wired rather than being
104 : : * configured by passing a set of terminating characters.
105 : : *
106 : : * The rules for tokens are:
107 : : * * Whitespace (space, tab, newline) always separates tokens.
108 : : * * The characters '(', ')', '{', '}' form individual tokens even
109 : : * without any whitespace around them.
110 : : * * Otherwise, a token is all the characters up to the next whitespace
111 : : * or occurrence of one of the four special characters.
112 : : * * A backslash '\' can be used to quote whitespace or one of the four
113 : : * special characters, so that it is treated as a plain token character.
114 : : * Backslashes themselves must also be backslashed for consistency.
115 : : * Any other character can be, but need not be, backslashed as well.
116 : : * * If the resulting token is '<>' (with no backslash), it is returned
117 : : * as a non-NULL pointer to the token but with length == 0. Note that
118 : : * there is no other way to get a zero-length token.
119 : : *
120 : : * Returns a pointer to the start of the next token, and the length of the
121 : : * token (including any embedded backslashes!) in *length. If there are
122 : : * no more tokens, NULL and 0 are returned.
123 : : *
124 : : * NOTE: this routine doesn't remove backslashes; the caller must do so
125 : : * if necessary (see "debackslash").
126 : : *
127 : : * NOTE: prior to release 7.0, this routine also had a special case to treat
128 : : * a token starting with '"' as extending to the next '"'. This code was
129 : : * broken, however, since it would fail to cope with a string containing an
130 : : * embedded '"'. I have therefore removed this special case, and instead
131 : : * introduced rules for using backslashes to quote characters. Higher-level
132 : : * code should add backslashes to a string constant to ensure it is treated
133 : : * as a single token.
134 : : */
135 : : const char *
136 : 889725777 : pg_strtok(ReadNodeContext *ctx, int *length)
137 : : {
138 : : const char *local_str; /* working pointer to string */
139 : : const char *ret_str; /* start of token to return */
140 : :
141 : 889725777 : local_str = ctx->str;
142 : :
143 [ + + ]: 1651520310 : while (pg_strtok_class[(unsigned char) *local_str] & TOKCLASS_SPACE)
144 : 761794533 : local_str++;
145 : :
146 [ + + ]: 889725777 : if (*local_str == '\0')
147 : : {
148 : 2 : *length = 0;
149 : 2 : ctx->str = local_str;
150 : 2 : return NULL; /* no more tokens */
151 : : }
152 : :
153 : : /*
154 : : * Now pointing at start of next token.
155 : : */
156 : 889725775 : ret_str = local_str;
157 : :
158 [ + + ]: 889725775 : if (pg_strtok_class[(unsigned char) *local_str] & TOKCLASS_SPECIAL)
159 : : {
160 : : /* special 1-character token */
161 : 126715827 : local_str++;
162 : : }
163 : : else
164 : : {
165 : : /* Normal token, possibly containing backslashes */
166 [ + + ]: 5387846160 : while (!(pg_strtok_class[(unsigned char) *local_str] &
167 : : (TOKCLASS_SPACE | TOKCLASS_SPECIAL | TOKCLASS_NUL)))
168 : : {
169 [ + + + - ]: 4624836212 : if (*local_str == '\\' && local_str[1] != '\0')
170 : 1466987 : local_str += 2;
171 : : else
172 : 4623369225 : local_str++;
173 : : }
174 : : }
175 : :
176 : 889725775 : *length = local_str - ret_str;
177 : :
178 : : /* Recognize special case for "empty" token */
179 [ + + + + : 889725775 : if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>')
+ - ]
180 : 45045301 : *length = 0;
181 : :
182 : 889725775 : ctx->str = local_str;
183 : :
184 : 889725775 : return ret_str;
185 : : }
186 : :
187 : : /*
188 : : * debackslash -
189 : : * create a palloc'd string holding the given token.
190 : : * any protective backslashes in the token are removed.
191 : : */
192 : : char *
193 : 26023673 : debackslash(const char *token, int length)
194 : : {
195 : 26023673 : char *result = palloc(length + 1);
196 : 26023673 : char *ptr = result;
197 : :
198 [ + + ]: 249009965 : while (length > 0)
199 : : {
200 [ + + + - ]: 222986292 : if (*token == '\\' && length > 1)
201 : 1466987 : token++, length--;
202 : 222986292 : *ptr++ = *token++;
203 : 222986292 : length--;
204 : : }
205 : 26023673 : *ptr = '\0';
206 : 26023673 : return result;
207 : : }
208 : :
209 : : #define RIGHT_PAREN (1000000 + 1)
210 : : #define LEFT_PAREN (1000000 + 2)
211 : : #define LEFT_BRACE (1000000 + 3)
212 : : #define OTHER_TOKEN (1000000 + 4)
213 : :
214 : : /*
215 : : * nodeTokenType -
216 : : * returns the type of the node token contained in token.
217 : : * It returns one of the following valid NodeTags:
218 : : * T_Integer, T_Float, T_Boolean, T_String, T_BitString
219 : : * and some of its own:
220 : : * RIGHT_PAREN, LEFT_PAREN, LEFT_BRACE, OTHER_TOKEN
221 : : *
222 : : * Assumption: the ascii representation is legal
223 : : */
224 : : static NodeTag
225 : 105655722 : nodeTokenType(const char *token, int length)
226 : : {
227 : : NodeTag retval;
228 : : const char *numptr;
229 : : int numlen;
230 : :
231 : : /*
232 : : * Check if the token is a number
233 : : */
234 : 105655722 : numptr = token;
235 : 105655722 : numlen = length;
236 [ + - + + ]: 105655722 : if (*numptr == '+' || *numptr == '-')
237 : 20686 : numptr++, numlen--;
238 [ + + + + : 105655722 : if ((numlen > 0 && isdigit((unsigned char) *numptr)) ||
+ + ]
239 [ + + + - ]: 19677945 : (numlen > 1 && *numptr == '.' && isdigit((unsigned char) numptr[1])))
240 : : {
241 : : /*
242 : : * Yes. Figure out whether it is integral or float; this requires
243 : : * both a syntax check and a range check. strtoint() can do both for
244 : : * us. We know the token will end at a character that strtoint will
245 : : * stop at, so we do not need to modify the string.
246 : : */
247 : : char *endptr;
248 : :
249 : 484536 : errno = 0;
250 : 484536 : (void) strtoint(numptr, &endptr, 10);
251 [ + + + + ]: 484536 : if (endptr != token + length || errno == ERANGE)
252 : 8258 : return T_Float;
253 : 476278 : return T_Integer;
254 : : }
255 : :
256 : : /*
257 : : * these three cases do not need length checks, since pg_strtok() will
258 : : * always treat them as single-byte tokens
259 : : */
260 [ + + ]: 105171186 : else if (*token == '(')
261 : 13091615 : retval = LEFT_PAREN;
262 [ - + ]: 92079571 : else if (*token == ')')
263 : 0 : retval = RIGHT_PAREN;
264 [ + + ]: 92079571 : else if (*token == '{')
265 : 35201075 : retval = LEFT_BRACE;
266 [ + + + + : 56878496 : else if ((length == 4 && strncmp(token, "true", 4) == 0) ||
+ + ]
267 [ + + ]: 1232801 : (length == 5 && strncmp(token, "false", 5) == 0))
268 : 59757 : retval = T_Boolean;
269 [ + + + - : 56818739 : else if (*token == '"' && length > 1 && token[length - 1] == '"')
+ - ]
270 : 19615429 : retval = T_String;
271 [ + + + + ]: 37203310 : else if (*token == 'b' || *token == 'x')
272 : 2747 : retval = T_BitString;
273 : : else
274 : 37200563 : retval = OTHER_TOKEN;
275 : 105171186 : return retval;
276 : : }
277 : :
278 : : /*
279 : : * nodeRead -
280 : : * Slightly higher-level reader.
281 : : *
282 : : * This routine applies some semantic knowledge on top of the purely
283 : : * lexical tokenizer pg_strtok(). It can read
284 : : * * Value token nodes (integers, floats, booleans, or strings);
285 : : * * General nodes (via parseNodeString() from readfuncs.c);
286 : : * * Lists of the above;
287 : : * * Lists of integers, OIDs, or TransactionIds.
288 : : * The return value is declared void *, not Node *, to avoid having to
289 : : * cast it explicitly in callers that assign to fields of different types.
290 : : *
291 : : * External callers should always pass NULL/0 for the arguments. Internally
292 : : * a non-NULL token may be passed when the upper recursion level has already
293 : : * scanned the first token of a node's representation.
294 : : *
295 : : * We assume pg_strtok is already initialized with a string to read (hence
296 : : * this should only be invoked from within a stringToNode operation).
297 : : */
298 : : void *
299 : 105655722 : nodeRead(ReadNodeContext *ctx, const char *token, int tok_len)
300 : : {
301 : : Node *result;
302 : : NodeTag type;
303 : :
304 [ + + ]: 105655722 : if (token == NULL) /* need to read a token? */
305 : : {
306 : 66457597 : token = pg_strtok(ctx, &tok_len);
307 : :
308 [ - + ]: 66457597 : if (token == NULL) /* end of input */
309 : 0 : return NULL;
310 : : }
311 : :
312 : 105655722 : type = nodeTokenType(token, tok_len);
313 : :
314 [ + + - + : 105655722 : switch ((int) type)
+ + + + +
- ]
315 : : {
316 : 35201075 : case LEFT_BRACE:
317 : 35201075 : result = parseNodeString(ctx);
318 : 35201074 : token = pg_strtok(ctx, &tok_len);
319 [ + - - + ]: 35201074 : if (token == NULL || token[0] != '}')
320 [ # # ]: 0 : elog(ERROR, "did not find '}' at end of input node");
321 : 35201074 : break;
322 : 13091615 : case LEFT_PAREN:
323 : : {
324 : 13091615 : List *l = NIL;
325 : :
326 : : /*----------
327 : : * Could be an integer list: (i int int ...)
328 : : * or an OID list: (o int int ...)
329 : : * or an XID list: (x int int ...)
330 : : * or a bitmapset: (b int int ...)
331 : : * or a list of nodes/values: (node node ...)
332 : : *----------
333 : : */
334 : 13091615 : token = pg_strtok(ctx, &tok_len);
335 [ - + ]: 13091615 : if (token == NULL)
336 [ # # ]: 0 : elog(ERROR, "unterminated List structure");
337 [ + + + + ]: 13091615 : if (tok_len == 1 && token[0] == 'i')
338 : : {
339 : : /* List of integers */
340 : : for (;;)
341 : 3532285 : {
342 : : int val;
343 : : char *endptr;
344 : :
345 : 3847727 : token = pg_strtok(ctx, &tok_len);
346 [ - + ]: 3847727 : if (token == NULL)
347 [ # # ]: 0 : elog(ERROR, "unterminated List structure");
348 [ + + ]: 3847727 : if (token[0] == ')')
349 : 315442 : break;
350 : 3532285 : val = (int) strtol(token, &endptr, 10);
351 [ - + ]: 3532285 : if (endptr != token + tok_len)
352 [ # # ]: 0 : elog(ERROR, "unrecognized integer: \"%.*s\"",
353 : : tok_len, token);
354 : 3532285 : l = lappend_int(l, val);
355 : : }
356 : 315442 : result = (Node *) l;
357 : : }
358 [ + + + + ]: 12776173 : else if (tok_len == 1 && token[0] == 'o')
359 : : {
360 : : /* List of OIDs */
361 : : for (;;)
362 : 1071390 : {
363 : : Oid val;
364 : : char *endptr;
365 : :
366 : 1576419 : token = pg_strtok(ctx, &tok_len);
367 [ - + ]: 1576419 : if (token == NULL)
368 [ # # ]: 0 : elog(ERROR, "unterminated List structure");
369 [ + + ]: 1576419 : if (token[0] == ')')
370 : 505029 : break;
371 : 1071390 : val = (Oid) strtoul(token, &endptr, 10);
372 [ - + ]: 1071390 : if (endptr != token + tok_len)
373 [ # # ]: 0 : elog(ERROR, "unrecognized OID: \"%.*s\"",
374 : : tok_len, token);
375 : 1071390 : l = lappend_oid(l, val);
376 : : }
377 : 505029 : result = (Node *) l;
378 : : }
379 [ + + - + ]: 12271144 : else if (tok_len == 1 && token[0] == 'x')
380 : : {
381 : : /* List of TransactionIds */
382 : : for (;;)
383 : 0 : {
384 : : TransactionId val;
385 : : char *endptr;
386 : :
387 : 0 : token = pg_strtok(ctx, &tok_len);
388 [ # # ]: 0 : if (token == NULL)
389 [ # # ]: 0 : elog(ERROR, "unterminated List structure");
390 [ # # ]: 0 : if (token[0] == ')')
391 : 0 : break;
392 : 0 : val = (TransactionId) strtoul(token, &endptr, 10);
393 [ # # ]: 0 : if (endptr != token + tok_len)
394 [ # # ]: 0 : elog(ERROR, "unrecognized Xid: \"%.*s\"",
395 : : tok_len, token);
396 : 0 : l = lappend_xid(l, val);
397 : : }
398 : 0 : result = (Node *) l;
399 : : }
400 [ + + + + ]: 12271144 : else if (tok_len == 1 && token[0] == 'b')
401 : 2597 : {
402 : : /* Bitmapset -- see also _readBitmapset() */
403 : 2597 : Bitmapset *bms = NULL;
404 : :
405 : : for (;;)
406 : 3617 : {
407 : : int val;
408 : : char *endptr;
409 : :
410 : 6214 : token = pg_strtok(ctx, &tok_len);
411 [ - + ]: 6214 : if (token == NULL)
412 [ # # ]: 0 : elog(ERROR, "unterminated Bitmapset structure");
413 [ + + + + ]: 6214 : if (tok_len == 1 && token[0] == ')')
414 : 2597 : break;
415 : 3617 : val = (int) strtol(token, &endptr, 10);
416 [ - + ]: 3617 : if (endptr != token + tok_len)
417 [ # # ]: 0 : elog(ERROR, "unrecognized integer: \"%.*s\"",
418 : : tok_len, token);
419 : 3617 : bms = bms_add_member(bms, val);
420 : : }
421 : 2597 : result = (Node *) bms;
422 : : }
423 : : else
424 : : {
425 : : /* List of other node types */
426 : : for (;;)
427 : : {
428 : : /* We have already scanned next token... */
429 [ + + ]: 51466672 : if (token[0] == ')')
430 : 12268547 : break;
431 : 39198125 : l = lappend(l, nodeRead(ctx, token, tok_len));
432 : 39198125 : token = pg_strtok(ctx, &tok_len);
433 [ - + ]: 39198125 : if (token == NULL)
434 [ # # ]: 0 : elog(ERROR, "unterminated List structure");
435 : : }
436 : 12268547 : result = (Node *) l;
437 : : }
438 : 13091615 : break;
439 : : }
440 : 0 : case RIGHT_PAREN:
441 [ # # ]: 0 : elog(ERROR, "unexpected right parenthesis");
442 : : result = NULL; /* keep compiler happy */
443 : : break;
444 : 37200563 : case OTHER_TOKEN:
445 [ + - ]: 37200563 : if (tok_len == 0)
446 : : {
447 : : /* must be "<>" --- represents a null pointer */
448 : 37200563 : result = NULL;
449 : : }
450 : : else
451 : : {
452 [ # # ]: 0 : elog(ERROR, "unrecognized token: \"%.*s\"", tok_len, token);
453 : : result = NULL; /* keep compiler happy */
454 : : }
455 : 37200563 : break;
456 : 476278 : case T_Integer:
457 : :
458 : : /*
459 : : * we know that the token terminates on a char atoi will stop at
460 : : */
461 : 476278 : result = (Node *) makeInteger(atoi(token));
462 : 476278 : break;
463 : 8258 : case T_Float:
464 : : {
465 : 8258 : char *fval = (char *) palloc(tok_len + 1);
466 : :
467 : 8258 : memcpy(fval, token, tok_len);
468 : 8258 : fval[tok_len] = '\0';
469 : 8258 : result = (Node *) makeFloat(fval);
470 : : }
471 : 8258 : break;
472 : 59757 : case T_Boolean:
473 : 59757 : result = (Node *) makeBoolean(token[0] == 't');
474 : 59757 : break;
475 : 19615429 : case T_String:
476 : : /* need to remove leading and trailing quotes, and backslashes */
477 : 19615429 : result = (Node *) makeString(debackslash(token + 1, tok_len - 2));
478 : 19615429 : break;
479 : 2747 : case T_BitString:
480 : : /* need to remove backslashes, but there are no quotes */
481 : 2747 : result = (Node *) makeBitString(debackslash(token, tok_len));
482 : 2747 : break;
483 : 0 : default:
484 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d", (int) type);
485 : : result = NULL; /* keep compiler happy */
486 : : break;
487 : : }
488 : :
489 : 105655721 : return result;
490 : : }
|