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.

Referenced by nodeRead(), and nodeTokenType().

◆ LEFT_PAREN

#define LEFT_PAREN   (1000000 + 2)

Definition at line 230 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

◆ OTHER_TOKEN

#define OTHER_TOKEN   (1000000 + 4)

Definition at line 232 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

◆ RIGHT_PAREN

#define RIGHT_PAREN   (1000000 + 1)

Definition at line 229 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

Function Documentation

◆ debackslash()

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

Definition at line 213 of file read.c.

References palloc().

Referenced by nodeRead().

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:949

◆ nodeRead()

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

Definition at line 316 of file read.c.

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

Referenced by nodeRead(), and stringToNodeInternal().

317 {
318  Node *result;
319  NodeTag type;
320 
321  if (token == NULL) /* need to read a token? */
322  {
323  token = pg_strtok(&tok_len);
324 
325  if (token == NULL) /* end of input */
326  return NULL;
327  }
328 
329  type = nodeTokenType(token, tok_len);
330 
331  switch ((int) type)
332  {
333  case LEFT_BRACE:
334  result = parseNodeString();
335  token = pg_strtok(&tok_len);
336  if (token == NULL || token[0] != '}')
337  elog(ERROR, "did not find '}' at end of input node");
338  break;
339  case LEFT_PAREN:
340  {
341  List *l = NIL;
342 
343  /*----------
344  * Could be an integer list: (i int int ...)
345  * or an OID list: (o int int ...)
346  * or a list of nodes/values: (node node ...)
347  *----------
348  */
349  token = pg_strtok(&tok_len);
350  if (token == NULL)
351  elog(ERROR, "unterminated List structure");
352  if (tok_len == 1 && token[0] == 'i')
353  {
354  /* List of integers */
355  for (;;)
356  {
357  int val;
358  char *endptr;
359 
360  token = pg_strtok(&tok_len);
361  if (token == NULL)
362  elog(ERROR, "unterminated List structure");
363  if (token[0] == ')')
364  break;
365  val = (int) strtol(token, &endptr, 10);
366  if (endptr != token + tok_len)
367  elog(ERROR, "unrecognized integer: \"%.*s\"",
368  tok_len, token);
369  l = lappend_int(l, val);
370  }
371  }
372  else if (tok_len == 1 && token[0] == 'o')
373  {
374  /* List of OIDs */
375  for (;;)
376  {
377  Oid val;
378  char *endptr;
379 
380  token = pg_strtok(&tok_len);
381  if (token == NULL)
382  elog(ERROR, "unterminated List structure");
383  if (token[0] == ')')
384  break;
385  val = (Oid) strtoul(token, &endptr, 10);
386  if (endptr != token + tok_len)
387  elog(ERROR, "unrecognized OID: \"%.*s\"",
388  tok_len, token);
389  l = lappend_oid(l, val);
390  }
391  }
392  else
393  {
394  /* List of other node types */
395  for (;;)
396  {
397  /* We have already scanned next token... */
398  if (token[0] == ')')
399  break;
400  l = lappend(l, nodeRead(token, tok_len));
401  token = pg_strtok(&tok_len);
402  if (token == NULL)
403  elog(ERROR, "unterminated List structure");
404  }
405  }
406  result = (Node *) l;
407  break;
408  }
409  case RIGHT_PAREN:
410  elog(ERROR, "unexpected right parenthesis");
411  result = NULL; /* keep compiler happy */
412  break;
413  case OTHER_TOKEN:
414  if (tok_len == 0)
415  {
416  /* must be "<>" --- represents a null pointer */
417  result = NULL;
418  }
419  else
420  {
421  elog(ERROR, "unrecognized token: \"%.*s\"", tok_len, token);
422  result = NULL; /* keep compiler happy */
423  }
424  break;
425  case T_Integer:
426 
427  /*
428  * we know that the token terminates on a char atoi will stop at
429  */
430  result = (Node *) makeInteger(atoi(token));
431  break;
432  case T_Float:
433  {
434  char *fval = (char *) palloc(tok_len + 1);
435 
436  memcpy(fval, token, tok_len);
437  fval[tok_len] = '\0';
438  result = (Node *) makeFloat(fval);
439  }
440  break;
441  case T_String:
442  /* need to remove leading and trailing quotes, and backslashes */
443  result = (Node *) makeString(debackslash(token + 1, tok_len - 2));
444  break;
445  case T_BitString:
446  {
447  char *val = palloc(tok_len);
448 
449  /* skip leading 'b' */
450  memcpy(val, token + 1, tok_len - 1);
451  val[tok_len - 1] = '\0';
452  result = (Node *) makeBitString(val);
453  break;
454  }
455  default:
456  elog(ERROR, "unrecognized node type: %d", (int) type);
457  result = NULL; /* keep compiler happy */
458  break;
459  }
460 
461  return (void *) result;
462 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
#define ERROR
Definition: elog.h:43
void * nodeRead(const char *token, int tok_len)
Definition: read.c:316
#define RIGHT_PAREN
Definition: read.c:229
char * debackslash(const char *token, int length)
Definition: read.c:213
Value * makeBitString(char *str)
Definition: value.c:68
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
Value * makeInteger(int i)
Definition: value.c:23
Value * makeFloat(char *numericStr)
Definition: value.c:38
#define LEFT_BRACE
Definition: read.c:231
Node * parseNodeString(void)
Definition: readfuncs.c:2562
static NodeTag nodeTokenType(const char *token, int length)
Definition: read.c:245
Definition: nodes.h:289
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:226
const char * pg_strtok(int *length)
Definition: read.c:152
#define OTHER_TOKEN
Definition: read.c:232
Definition: pg_list.h:50
long val
Definition: informix.c:684
#define LEFT_PAREN
Definition: read.c:230

◆ nodeTokenType()

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

Definition at line 245 of file read.c.

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

Referenced by nodeRead().

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 (*token == '"' && length > 1 && token[length - 1] == '"')
287  retval = T_String;
288  else if (*token == 'b')
289  retval = T_BitString;
290  else
291  retval = OTHER_TOKEN;
292  return retval;
293 }
NodeTag
Definition: nodes.h:26
#define RIGHT_PAREN
Definition: read.c:229
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
#define LEFT_BRACE
Definition: read.c:231
Definition: nodes.h:289
#define OTHER_TOKEN
Definition: read.c:232
#define LEFT_PAREN
Definition: read.c:230

◆ pg_strtok()

const char* pg_strtok ( int *  length)

Definition at line 152 of file read.c.

References pg_strtok_ptr.

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

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

◆ stringToNode()

◆ stringToNodeInternal()

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

Definition at line 48 of file read.c.

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

Referenced by stringToNode().

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 }
static const char * pg_strtok_ptr
Definition: read.c:31
void * nodeRead(const char *token, int tok_len)
Definition: read.c:316

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().