1 /*-------------------------------------------------------------------------
4 * routines to convert a string (legal ascii representation of node) back
7 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/nodes/read.c,v 1.39 2004/01/09 03:07:32 momjian Exp $
15 * AUTHOR DATE MAJOR EVENT
16 * Andrew Yu Nov 2, 1994 file creation
18 *-------------------------------------------------------------------------
25 #include "nodes/pg_list.h"
26 #include "nodes/readfuncs.h"
27 #include "nodes/value.h"
30 /* Static state for pg_strtok */
31 static char *pg_strtok_ptr = NULL;
36 * returns a Node with a given legal ASCII representation
39 stringToNode(char *str)
45 * We save and restore the pre-existing state of pg_strtok. This makes
46 * the world safe for re-entrant invocation of stringToNode, without
47 * incurring a lot of notational overhead by having to pass the
48 * next-character pointer around through all the readfuncs.c code.
50 save_strtok = pg_strtok_ptr;
52 pg_strtok_ptr = str; /* point pg_strtok at the string to read */
54 retval = nodeRead(true); /* do the reading */
56 pg_strtok_ptr = save_strtok;
61 /*****************************************************************************
63 * the lisp token parser
65 *****************************************************************************/
68 * pg_strtok --- retrieve next "token" from a string.
70 * Works kinda like strtok, except it never modifies the source string.
71 * (Instead of storing nulls into the string, the length of the token
72 * is returned to the caller.)
73 * Also, the rules about what is a token are hard-wired rather than being
74 * configured by passing a set of terminating characters.
76 * The string is assumed to have been initialized already by stringToNode.
78 * The rules for tokens are:
79 * * Whitespace (space, tab, newline) always separates tokens.
80 * * The characters '(', ')', '{', '}' form individual tokens even
81 * without any whitespace around them.
82 * * Otherwise, a token is all the characters up to the next whitespace
83 * or occurrence of one of the four special characters.
84 * * A backslash '\' can be used to quote whitespace or one of the four
85 * special characters, so that it is treated as a plain token character.
86 * Backslashes themselves must also be backslashed for consistency.
87 * Any other character can be, but need not be, backslashed as well.
88 * * If the resulting token is '<>' (with no backslash), it is returned
89 * as a non-NULL pointer to the token but with length == 0. Note that
90 * there is no other way to get a zero-length token.
92 * Returns a pointer to the start of the next token, and the length of the
93 * token (including any embedded backslashes!) in *length. If there are
94 * no more tokens, NULL and 0 are returned.
96 * NOTE: this routine doesn't remove backslashes; the caller must do so
97 * if necessary (see "debackslash").
99 * NOTE: prior to release 7.0, this routine also had a special case to treat
100 * a token starting with '"' as extending to the next '"'. This code was
101 * broken, however, since it would fail to cope with a string containing an
102 * embedded '"'. I have therefore removed this special case, and instead
103 * introduced rules for using backslashes to quote characters. Higher-level
104 * code should add backslashes to a string constant to ensure it is treated
108 pg_strtok(int *length)
110 char *local_str; /* working pointer to string */
111 char *ret_str; /* start of token to return */
113 local_str = pg_strtok_ptr;
115 while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t')
118 if (*local_str == '\0')
121 pg_strtok_ptr = local_str;
122 return NULL; /* no more tokens */
126 * Now pointing at start of next token.
130 if (*local_str == '(' || *local_str == ')' ||
131 *local_str == '{' || *local_str == '}')
133 /* special 1-character token */
138 /* Normal token, possibly containing backslashes */
139 while (*local_str != '\0' &&
140 *local_str != ' ' && *local_str != '\n' &&
141 *local_str != '\t' &&
142 *local_str != '(' && *local_str != ')' &&
143 *local_str != '{' && *local_str != '}')
145 if (*local_str == '\\' && local_str[1] != '\0')
152 *length = local_str - ret_str;
154 /* Recognize special case for "empty" token */
155 if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>')
158 pg_strtok_ptr = local_str;
165 * create a palloc'd string holding the given token.
166 * any protective backslashes in the token are removed.
169 debackslash(char *token, int length)
171 char *result = palloc(length + 1);
176 if (*token == '\\' && length > 1)
185 #define RIGHT_PAREN (1000000 + 1)
186 #define LEFT_PAREN (1000000 + 2)
187 #define NODE_SYM (1000000 + 3)
188 #define AT_SYMBOL (1000000 + 4)
189 #define ATOM_TOKEN (1000000 + 5)
193 * returns the type of the node token contained in token.
194 * It returns one of the following valid NodeTags:
195 * T_Integer, T_Float, T_String, T_BitString
196 * and some of its own:
197 * RIGHT_PAREN, LEFT_PAREN, NODE_SYM, AT_SYMBOL, ATOM_TOKEN
199 * Assumption: the ascii representation is legal
202 nodeTokenType(char *token, int length)
209 * Check if the token is a number
213 if (*numptr == '+' || *numptr == '-')
215 if ((numlen > 0 && isdigit((unsigned char) *numptr)) ||
216 (numlen > 1 && *numptr == '.' && isdigit((unsigned char) numptr[1])))
219 * Yes. Figure out whether it is integral or float; this requires
220 * both a syntax check and a range check. strtol() can do both for
221 * us. We know the token will end at a character that strtol will
222 * stop at, so we do not need to modify the string.
228 val = strtol(token, &endptr, 10);
229 if (endptr != token + length || errno == ERANGE
230 #ifdef HAVE_LONG_INT_64
231 /* if long > 32 bits, check for overflow of int4 */
232 || val != (long) ((int32) val)
240 * these three cases do not need length checks, since pg_strtok() will
241 * always treat them as single-byte tokens
243 else if (*token == '(')
245 else if (*token == ')')
246 retval = RIGHT_PAREN;
247 else if (*token == '{')
249 else if (*token == '@' && length == 1)
251 else if (*token == '\"' && length > 1 && token[length - 1] == '\"')
253 else if (*token == 'b')
254 retval = T_BitString;
262 * Slightly higher-level reader.
264 * This routine applies some semantic knowledge on top of the purely
265 * lexical tokenizer pg_strtok(). It can read
266 * * Value token nodes (integers, floats, or strings);
267 * * General nodes (via parseNodeString() from readfuncs.c);
268 * * Lists of the above.
270 * We assume pg_strtok is already initialized with a string to read (hence
271 * this should only be invoked from within a stringToNode operation).
272 * Any callers should set read_car_only to true.
275 nodeRead(bool read_car_only)
282 bool make_dotted_pair_cell = false;
284 token = pg_strtok(&tok_len);
289 type = nodeTokenType(token, tok_len);
294 this_value = parseNodeString();
295 token = pg_strtok(&tok_len);
296 if (token == NULL || token[0] != '}')
297 elog(ERROR, "did not find '}' at end of input node");
299 make_dotted_pair_cell = true;
301 make_dotted_pair_cell = false;
306 List *l = makeNode(List);
308 lfirst(l) = nodeRead(false);
309 lnext(l) = nodeRead(false);
310 this_value = (Node *) l;
313 this_value = nodeRead(false);
328 * It might be NULL but it is an atom!
331 make_dotted_pair_cell = false;
333 make_dotted_pair_cell = true;
337 /* !attention! result is not a Node. Use with caution. */
338 this_value = (Node *) debackslash(token, tok_len);
339 make_dotted_pair_cell = true;
345 * we know that the token terminates on a char atol will stop
348 this_value = (Node *) makeInteger(atol(token));
349 make_dotted_pair_cell = true;
353 char *fval = (char *) palloc(tok_len + 1);
355 memcpy(fval, token, tok_len);
356 fval[tok_len] = '\0';
357 this_value = (Node *) makeFloat(fval);
358 make_dotted_pair_cell = true;
362 /* need to remove leading and trailing quotes, and backslashes */
363 this_value = (Node *) makeString(debackslash(token + 1, tok_len - 2));
364 make_dotted_pair_cell = true;
368 char *val = palloc(tok_len);
370 /* skip leading 'b' */
371 strncpy(val, token + 1, tok_len - 1);
372 val[tok_len - 1] = '\0';
373 this_value = (Node *) makeBitString(val);
377 elog(ERROR, "unrecognized node type: %d", (int) type);
378 this_value = NULL; /* keep compiler happy */
381 if (make_dotted_pair_cell)
383 List *l = makeNode(List);
385 lfirst(l) = this_value;
388 lnext(l) = nodeRead(false);
391 return_value = (Node *) l;
394 return_value = this_value;