/src/postgres/src/backend/nodes/read.c
Line | Count | Source |
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 | | stringToNodeInternal(const char *str, bool restore_loc_fields) |
41 | 0 | { |
42 | 0 | ReadNodeContext ctx; |
43 | | |
44 | | /* initialize the context */ |
45 | 0 | ctx.str = str; |
46 | | #ifdef DEBUG_NODE_TESTS_ENABLED |
47 | | ctx.restore_location_fields = restore_loc_fields; |
48 | | #endif |
49 | | |
50 | | /* do the reading, and return */ |
51 | 0 | return nodeRead(&ctx, NULL, 0); |
52 | 0 | } |
53 | | |
54 | | /* |
55 | | * Externally visible entry points |
56 | | */ |
57 | | void * |
58 | | stringToNode(const char *str) |
59 | 0 | { |
60 | 0 | return stringToNodeInternal(str, false); |
61 | 0 | } |
62 | | |
63 | | #ifdef DEBUG_NODE_TESTS_ENABLED |
64 | | |
65 | | void * |
66 | | stringToNodeWithLocations(const char *str) |
67 | | { |
68 | | 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 | 0 | #define TOKCLASS_SPACE 0x01 /* ' ', '\n', '\t' */ |
82 | 0 | #define TOKCLASS_SPECIAL 0x02 /* '(', ')', '{', '}' */ |
83 | 0 | #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 | | pg_strtok(ReadNodeContext *ctx, int *length) |
137 | 0 | { |
138 | 0 | const char *local_str; /* working pointer to string */ |
139 | 0 | const char *ret_str; /* start of token to return */ |
140 | |
|
141 | 0 | local_str = ctx->str; |
142 | |
|
143 | 0 | while (pg_strtok_class[(unsigned char) *local_str] & TOKCLASS_SPACE) |
144 | 0 | local_str++; |
145 | |
|
146 | 0 | if (*local_str == '\0') |
147 | 0 | { |
148 | 0 | *length = 0; |
149 | 0 | ctx->str = local_str; |
150 | 0 | return NULL; /* no more tokens */ |
151 | 0 | } |
152 | | |
153 | | /* |
154 | | * Now pointing at start of next token. |
155 | | */ |
156 | 0 | ret_str = local_str; |
157 | |
|
158 | 0 | if (pg_strtok_class[(unsigned char) *local_str] & TOKCLASS_SPECIAL) |
159 | 0 | { |
160 | | /* special 1-character token */ |
161 | 0 | local_str++; |
162 | 0 | } |
163 | 0 | else |
164 | 0 | { |
165 | | /* Normal token, possibly containing backslashes */ |
166 | 0 | while (!(pg_strtok_class[(unsigned char) *local_str] & |
167 | 0 | (TOKCLASS_SPACE | TOKCLASS_SPECIAL | TOKCLASS_NUL))) |
168 | 0 | { |
169 | 0 | if (*local_str == '\\' && local_str[1] != '\0') |
170 | 0 | local_str += 2; |
171 | 0 | else |
172 | 0 | local_str++; |
173 | 0 | } |
174 | 0 | } |
175 | |
|
176 | 0 | *length = local_str - ret_str; |
177 | | |
178 | | /* Recognize special case for "empty" token */ |
179 | 0 | if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>') |
180 | 0 | *length = 0; |
181 | |
|
182 | 0 | ctx->str = local_str; |
183 | |
|
184 | 0 | return ret_str; |
185 | 0 | } |
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 | | debackslash(const char *token, int length) |
194 | 0 | { |
195 | 0 | char *result = palloc(length + 1); |
196 | 0 | char *ptr = result; |
197 | |
|
198 | 0 | while (length > 0) |
199 | 0 | { |
200 | 0 | if (*token == '\\' && length > 1) |
201 | 0 | token++, length--; |
202 | 0 | *ptr++ = *token++; |
203 | 0 | length--; |
204 | 0 | } |
205 | 0 | *ptr = '\0'; |
206 | 0 | return result; |
207 | 0 | } |
208 | | |
209 | 0 | #define RIGHT_PAREN (1000000 + 1) |
210 | 0 | #define LEFT_PAREN (1000000 + 2) |
211 | 0 | #define LEFT_BRACE (1000000 + 3) |
212 | 0 | #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 | | nodeTokenType(const char *token, int length) |
226 | 0 | { |
227 | 0 | NodeTag retval; |
228 | 0 | const char *numptr; |
229 | 0 | int numlen; |
230 | | |
231 | | /* |
232 | | * Check if the token is a number |
233 | | */ |
234 | 0 | numptr = token; |
235 | 0 | numlen = length; |
236 | 0 | if (*numptr == '+' || *numptr == '-') |
237 | 0 | numptr++, numlen--; |
238 | 0 | if ((numlen > 0 && isdigit((unsigned char) *numptr)) || |
239 | 0 | (numlen > 1 && *numptr == '.' && isdigit((unsigned char) numptr[1]))) |
240 | 0 | { |
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 | 0 | char *endptr; |
248 | |
|
249 | 0 | errno = 0; |
250 | 0 | (void) strtoint(numptr, &endptr, 10); |
251 | 0 | if (endptr != token + length || errno == ERANGE) |
252 | 0 | return T_Float; |
253 | 0 | return T_Integer; |
254 | 0 | } |
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 | 0 | else if (*token == '(') |
261 | 0 | retval = LEFT_PAREN; |
262 | 0 | else if (*token == ')') |
263 | 0 | retval = RIGHT_PAREN; |
264 | 0 | else if (*token == '{') |
265 | 0 | retval = LEFT_BRACE; |
266 | 0 | else if ((length == 4 && strncmp(token, "true", 4) == 0) || |
267 | 0 | (length == 5 && strncmp(token, "false", 5) == 0)) |
268 | 0 | retval = T_Boolean; |
269 | 0 | else if (*token == '"' && length > 1 && token[length - 1] == '"') |
270 | 0 | retval = T_String; |
271 | 0 | else if (*token == 'b' || *token == 'x') |
272 | 0 | retval = T_BitString; |
273 | 0 | else |
274 | 0 | retval = OTHER_TOKEN; |
275 | 0 | return retval; |
276 | 0 | } |
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 | | nodeRead(ReadNodeContext *ctx, const char *token, int tok_len) |
300 | 0 | { |
301 | 0 | Node *result; |
302 | 0 | NodeTag type; |
303 | |
|
304 | 0 | if (token == NULL) /* need to read a token? */ |
305 | 0 | { |
306 | 0 | token = pg_strtok(ctx, &tok_len); |
307 | |
|
308 | 0 | if (token == NULL) /* end of input */ |
309 | 0 | return NULL; |
310 | 0 | } |
311 | | |
312 | 0 | type = nodeTokenType(token, tok_len); |
313 | |
|
314 | 0 | switch ((int) type) |
315 | 0 | { |
316 | 0 | case LEFT_BRACE: |
317 | 0 | result = parseNodeString(ctx); |
318 | 0 | token = pg_strtok(ctx, &tok_len); |
319 | 0 | if (token == NULL || token[0] != '}') |
320 | 0 | elog(ERROR, "did not find '}' at end of input node"); |
321 | 0 | break; |
322 | 0 | case LEFT_PAREN: |
323 | 0 | { |
324 | 0 | 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 | 0 | token = pg_strtok(ctx, &tok_len); |
335 | 0 | if (token == NULL) |
336 | 0 | elog(ERROR, "unterminated List structure"); |
337 | 0 | if (tok_len == 1 && token[0] == 'i') |
338 | 0 | { |
339 | | /* List of integers */ |
340 | 0 | for (;;) |
341 | 0 | { |
342 | 0 | int val; |
343 | 0 | char *endptr; |
344 | |
|
345 | 0 | token = pg_strtok(ctx, &tok_len); |
346 | 0 | if (token == NULL) |
347 | 0 | elog(ERROR, "unterminated List structure"); |
348 | 0 | if (token[0] == ')') |
349 | 0 | break; |
350 | 0 | val = (int) strtol(token, &endptr, 10); |
351 | 0 | if (endptr != token + tok_len) |
352 | 0 | elog(ERROR, "unrecognized integer: \"%.*s\"", |
353 | 0 | tok_len, token); |
354 | 0 | l = lappend_int(l, val); |
355 | 0 | } |
356 | 0 | result = (Node *) l; |
357 | 0 | } |
358 | 0 | else if (tok_len == 1 && token[0] == 'o') |
359 | 0 | { |
360 | | /* List of OIDs */ |
361 | 0 | for (;;) |
362 | 0 | { |
363 | 0 | Oid val; |
364 | 0 | char *endptr; |
365 | |
|
366 | 0 | token = pg_strtok(ctx, &tok_len); |
367 | 0 | if (token == NULL) |
368 | 0 | elog(ERROR, "unterminated List structure"); |
369 | 0 | if (token[0] == ')') |
370 | 0 | break; |
371 | 0 | val = (Oid) strtoul(token, &endptr, 10); |
372 | 0 | if (endptr != token + tok_len) |
373 | 0 | elog(ERROR, "unrecognized OID: \"%.*s\"", |
374 | 0 | tok_len, token); |
375 | 0 | l = lappend_oid(l, val); |
376 | 0 | } |
377 | 0 | result = (Node *) l; |
378 | 0 | } |
379 | 0 | else if (tok_len == 1 && token[0] == 'x') |
380 | 0 | { |
381 | | /* List of TransactionIds */ |
382 | 0 | for (;;) |
383 | 0 | { |
384 | 0 | TransactionId val; |
385 | 0 | 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 | 0 | tok_len, token); |
396 | 0 | l = lappend_xid(l, val); |
397 | 0 | } |
398 | 0 | result = (Node *) l; |
399 | 0 | } |
400 | 0 | else if (tok_len == 1 && token[0] == 'b') |
401 | 0 | { |
402 | | /* Bitmapset -- see also _readBitmapset() */ |
403 | 0 | Bitmapset *bms = NULL; |
404 | |
|
405 | 0 | for (;;) |
406 | 0 | { |
407 | 0 | int val; |
408 | 0 | char *endptr; |
409 | |
|
410 | 0 | token = pg_strtok(ctx, &tok_len); |
411 | 0 | if (token == NULL) |
412 | 0 | elog(ERROR, "unterminated Bitmapset structure"); |
413 | 0 | if (tok_len == 1 && token[0] == ')') |
414 | 0 | break; |
415 | 0 | val = (int) strtol(token, &endptr, 10); |
416 | 0 | if (endptr != token + tok_len) |
417 | 0 | elog(ERROR, "unrecognized integer: \"%.*s\"", |
418 | 0 | tok_len, token); |
419 | 0 | bms = bms_add_member(bms, val); |
420 | 0 | } |
421 | 0 | result = (Node *) bms; |
422 | 0 | } |
423 | 0 | else |
424 | 0 | { |
425 | | /* List of other node types */ |
426 | 0 | for (;;) |
427 | 0 | { |
428 | | /* We have already scanned next token... */ |
429 | 0 | if (token[0] == ')') |
430 | 0 | break; |
431 | 0 | l = lappend(l, nodeRead(ctx, token, tok_len)); |
432 | 0 | token = pg_strtok(ctx, &tok_len); |
433 | 0 | if (token == NULL) |
434 | 0 | elog(ERROR, "unterminated List structure"); |
435 | 0 | } |
436 | 0 | result = (Node *) l; |
437 | 0 | } |
438 | 0 | break; |
439 | 0 | } |
440 | 0 | case RIGHT_PAREN: |
441 | 0 | elog(ERROR, "unexpected right parenthesis"); |
442 | 0 | result = NULL; /* keep compiler happy */ |
443 | 0 | break; |
444 | 0 | case OTHER_TOKEN: |
445 | 0 | if (tok_len == 0) |
446 | 0 | { |
447 | | /* must be "<>" --- represents a null pointer */ |
448 | 0 | result = NULL; |
449 | 0 | } |
450 | 0 | else |
451 | 0 | { |
452 | 0 | elog(ERROR, "unrecognized token: \"%.*s\"", tok_len, token); |
453 | 0 | result = NULL; /* keep compiler happy */ |
454 | 0 | } |
455 | 0 | break; |
456 | 0 | case T_Integer: |
457 | | |
458 | | /* |
459 | | * we know that the token terminates on a char atoi will stop at |
460 | | */ |
461 | 0 | result = (Node *) makeInteger(atoi(token)); |
462 | 0 | break; |
463 | 0 | case T_Float: |
464 | 0 | { |
465 | 0 | char *fval = (char *) palloc(tok_len + 1); |
466 | |
|
467 | 0 | memcpy(fval, token, tok_len); |
468 | 0 | fval[tok_len] = '\0'; |
469 | 0 | result = (Node *) makeFloat(fval); |
470 | 0 | } |
471 | 0 | break; |
472 | 0 | case T_Boolean: |
473 | 0 | result = (Node *) makeBoolean(token[0] == 't'); |
474 | 0 | break; |
475 | 0 | case T_String: |
476 | | /* need to remove leading and trailing quotes, and backslashes */ |
477 | 0 | result = (Node *) makeString(debackslash(token + 1, tok_len - 2)); |
478 | 0 | break; |
479 | 0 | case T_BitString: |
480 | | /* need to remove backslashes, but there are no quotes */ |
481 | 0 | result = (Node *) makeBitString(debackslash(token, tok_len)); |
482 | 0 | break; |
483 | 0 | default: |
484 | 0 | elog(ERROR, "unrecognized node type: %d", (int) type); |
485 | 0 | result = NULL; /* keep compiler happy */ |
486 | 0 | break; |
487 | 0 | } |
488 | | |
489 | 0 | return result; |
490 | 0 | } |