PostgreSQL Source Code  git master
read.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "common/string.h"
#include "nodes/pg_list.h"
#include "nodes/readfuncs.h"
#include "nodes/value.h"
Include dependency graph for read.c:

Go to the source code of this file.

Macros

#define RIGHT_PAREN   (1000000 + 1)
 
#define LEFT_PAREN   (1000000 + 2)
 
#define LEFT_BRACE   (1000000 + 3)
 
#define OTHER_TOKEN   (1000000 + 4)
 

Functions

static void * stringToNodeInternal (const char *str, bool restore_loc_fields)
 
void * stringToNode (const char *str)
 
const char * pg_strtok (int *length)
 
char * debackslash (const char *token, int length)
 
static NodeTag nodeTokenType (const char *token, int length)
 
void * nodeRead (const char *token, int tok_len)
 

Variables

static const char * pg_strtok_ptr = NULL
 

Macro Definition Documentation

◆ LEFT_BRACE

#define LEFT_BRACE   (1000000 + 3)

Definition at line 231 of file read.c.

◆ LEFT_PAREN

#define LEFT_PAREN   (1000000 + 2)

Definition at line 230 of file read.c.

◆ OTHER_TOKEN

#define OTHER_TOKEN   (1000000 + 4)

Definition at line 232 of file read.c.

◆ RIGHT_PAREN

#define RIGHT_PAREN   (1000000 + 1)

Definition at line 229 of file read.c.

Function Documentation

◆ debackslash()

char* debackslash ( const char *  token,
int  length 
)

Definition at line 213 of file read.c.

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 }
void * palloc(Size size)
Definition: mcxt.c:1068

References palloc().

Referenced by nodeRead().

◆ nodeRead()

void* nodeRead ( const char *  token,
int  tok_len 
)

Definition at line 319 of file read.c.

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
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
#define RIGHT_PAREN
Definition: read.c:229
char * debackslash(const char *token, int length)
Definition: read.c:213
void * nodeRead(const char *token, int tok_len)
Definition: read.c:319
Node * parseNodeString(void)
Definition: readfuncs.c:2950
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

References debackslash(), elog, ERROR, lappend(), lappend_int(), lappend_oid(), LEFT_BRACE, LEFT_PAREN, makeBitString(), makeBoolean(), makeFloat(), makeInteger(), makeString(), NIL, nodeTokenType(), OTHER_TOKEN, palloc(), parseNodeString(), pg_strtok(), RIGHT_PAREN, T_BitString, T_Boolean, T_Float, T_Integer, T_String, generate_unaccent_rules::type, and val.

Referenced by stringToNodeInternal().

◆ nodeTokenType()

static NodeTag nodeTokenType ( const char *  token,
int  length 
)
static

Definition at line 245 of file read.c.

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 }
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50

References LEFT_BRACE, LEFT_PAREN, OTHER_TOKEN, RIGHT_PAREN, strtoint(), T_BitString, T_Boolean, T_Float, T_Integer, and T_String.

Referenced by nodeRead().

◆ pg_strtok()

const char* pg_strtok ( int *  length)

Definition at line 152 of file read.c.

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 }
static const char * pg_strtok_ptr
Definition: read.c:31

References pg_strtok_ptr.

Referenced by _readBitmapset(), _readBoolExpr(), _readConst(), _readCustomScan(), _readExtensibleNode(), nodeRead(), parseNodeString(), readAttrNumberCols(), readBoolCols(), readDatum(), readIntCols(), and readOidCols().

◆ stringToNode()

void* stringToNode ( const char *  str)

Definition at line 89 of file read.c.

90 {
91  return stringToNodeInternal(str, false);
92 }
static void * stringToNodeInternal(const char *str, bool restore_loc_fields)
Definition: read.c:48

References generate_unaccent_rules::str, and stringToNodeInternal().

Referenced by AddRelationNewConstraints(), AlterPolicy(), AlterPublicationTables(), ATExecValidateConstraint(), build_column_default(), CloneRowTriggersToPartition(), ConstraintImpliedByRelConstraint(), ExecParallelGetQueryDesc(), ExecRelCheck(), expandTableLikeClause(), fetch_function_defaults(), fetch_statentries_for_relation(), fill_extraUpdatedCols(), fmgr_sql_validator(), func_get_detail(), generate_partition_qual(), generateClonedExtStatsStmt(), generateClonedIndexStmt(), GenerateTypeDependencies(), get_qual_for_range(), get_relation_constraints(), get_relation_statistics(), get_typdefault(), index_concurrently_create_copy(), init_sql_fcache(), inline_function(), inline_set_returning_function(), load_domaintype_info(), make_ruledef(), make_viewdef(), MergeAttributes(), MergeWithExistingConstraint(), pg_get_constraintdef_worker(), pg_get_expr_worker(), pg_get_function_arg_default(), pg_get_indexdef_worker(), pg_get_partkeydef_worker(), pg_get_statisticsobj_worker(), pg_get_statisticsobjdef_expressions(), pg_get_triggerdef_worker(), pgoutput_row_filter_init(), print_function_arguments(), print_function_sqlbody(), ProcedureCreate(), pub_rf_contains_invalid_column(), RelationBuildPartitionDesc(), RelationBuildPartitionKey(), RelationBuildRowSecurity(), RelationBuildRuleLock(), RelationGetDummyIndexExpressions(), RelationGetIndexAttrBitmap(), RelationGetIndexExpressions(), RelationGetIndexPredicate(), TriggerEnabled(), TypeCreate(), and validateDomainConstraint().

◆ stringToNodeInternal()

static void* stringToNodeInternal ( const char *  str,
bool  restore_loc_fields 
)
static

Definition at line 48 of file read.c.

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 }

References nodeRead(), pg_strtok_ptr, and generate_unaccent_rules::str.

Referenced by stringToNode().

Variable Documentation

◆ pg_strtok_ptr

const char* pg_strtok_ptr = NULL
static

Definition at line 31 of file read.c.

Referenced by pg_strtok(), and stringToNodeInternal().