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