1 /*-------------------------------------------------------------------------
4 * routines to convert a string (legal ascii representation of node) back
7 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.21 2000/02/21 18:47:00 tgl 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"
30 * returns a Node with a given legal ASCII representation
33 stringToNode(char *str)
37 lsptok(str, NULL); /* set the string used in lsptok */
38 retval = nodeRead(true); /* start reading */
43 /*****************************************************************************
45 * the lisp token parser
47 *****************************************************************************/
50 * lsptok --- retrieve next "token" from a string.
52 * Works kinda like strtok, except it never modifies the source string.
53 * (Instead of storing nulls into the string, the length of the token
54 * is returned to the caller.)
55 * Also, the rules about what is a token are hard-wired rather than being
56 * configured by passing a set of terminating characters.
58 * The string is initially set by passing a non-NULL "string" value,
59 * and subsequent calls with string==NULL read the previously given value.
60 * (Pass length==NULL to set the string without reading its first token.)
62 * The rules for tokens are:
63 * * Whitespace (space, tab, newline) always separates tokens.
64 * * The characters '(', ')', '{', '}' form individual tokens even
65 * without any whitespace around them.
66 * * Otherwise, a token is all the characters up to the next whitespace
67 * or occurrence of one of the four special characters.
68 * * A backslash '\' can be used to quote whitespace or one of the four
69 * special characters, so that it is treated as a plain token character.
70 * Backslashes themselves must also be backslashed for consistency.
71 * Any other character can be, but need not be, backslashed as well.
72 * * If the resulting token is '<>' (with no backslash), it is returned
73 * as a non-NULL pointer to the token but with length == 0. Note that
74 * there is no other way to get a zero-length token.
76 * Returns a pointer to the start of the next token, and the length of the
77 * token (including any embedded backslashes!) in *length. If there are
78 * no more tokens, NULL and 0 are returned.
80 * NOTE: this routine doesn't remove backslashes; the caller must do so
81 * if necessary (see "debackslash").
83 * NOTE: prior to release 7.0, this routine also had a special case to treat
84 * a token starting with '"' as extending to the next '"'. This code was
85 * broken, however, since it would fail to cope with a string containing an
86 * embedded '"'. I have therefore removed this special case, and instead
87 * introduced rules for using backslashes to quote characters. Higher-level
88 * code should add backslashes to a string constant to ensure it is treated
92 lsptok(char *string, int *length)
94 static char *saved_str = NULL;
95 char *local_str; /* working pointer to string */
96 char *ret_str; /* start of token to return */
105 local_str = saved_str;
107 while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t')
110 if (*local_str == '\0')
113 saved_str = local_str;
114 return NULL; /* no more tokens */
118 * Now pointing at start of next token.
122 if (*local_str == '(' || *local_str == ')' ||
123 *local_str == '{' || *local_str == '}')
125 /* special 1-character token */
130 /* Normal token, possibly containing backslashes */
131 while (*local_str != '\0' &&
132 *local_str != ' ' && *local_str != '\n' &&
133 *local_str != '\t' &&
134 *local_str != '(' && *local_str != ')' &&
135 *local_str != '{' && *local_str != '}')
137 if (*local_str == '\\' && local_str[1] != '\0')
144 *length = local_str - ret_str;
146 /* Recognize special case for "empty" token */
147 if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>')
150 saved_str = local_str;
157 * create a palloc'd string holding the given token.
158 * any protective backslashes in the token are removed.
161 debackslash(char *token, int length)
163 char *result = palloc(length+1);
168 if (*token == '\\' && length > 1)
177 #define RIGHT_PAREN (1000000 + 1)
178 #define LEFT_PAREN (1000000 + 2)
179 #define PLAN_SYM (1000000 + 3)
180 #define AT_SYMBOL (1000000 + 4)
181 #define ATOM_TOKEN (1000000 + 5)
185 * returns the type of the node token contained in token.
186 * It returns one of the following valid NodeTags:
187 * T_Integer, T_Float, T_String
188 * and some of its own:
189 * RIGHT_PAREN, LEFT_PAREN, PLAN_SYM, AT_SYMBOL, ATOM_TOKEN
191 * Assumption: the ascii representation is legal
194 nodeTokenType(char *token, int length)
202 * Check if the token is a number
206 if (*numptr == '+' || *numptr == '-')
208 if ((numlen > 0 && isdigit(*numptr)) ||
209 (numlen > 1 && *numptr == '.' && isdigit(numptr[1])))
212 * Yes. Figure out whether it is integral or float;
213 * this requires both a syntax check and a range check.
214 * strtol() can do both for us.
215 * We know the token will end at a character that strtol will
216 * stop at, so we do not need to modify the string.
219 (void) strtol(token, &endptr, 10);
220 if (endptr != token+length || errno == ERANGE)
225 * these three cases do not need length checks, since lsptok()
226 * will always treat them as single-byte tokens
228 else if (*token == '(')
230 else if (*token == ')')
231 retval = RIGHT_PAREN;
232 else if (*token == '{')
234 else if (*token == '@' && length == 1)
236 else if (*token == '\"' && length > 1 && token[length-1] == '\"')
245 * Slightly higher-level reader.
247 * This routine applies some semantic knowledge on top of the purely
248 * lexical tokenizer lsptok(). It can read
249 * * Value token nodes (integers, floats, or strings);
250 * * Plan nodes (via parsePlanString() from readfuncs.c);
251 * * Lists of the above.
253 * Secrets: He assumes that lsptok already has the string (see above).
254 * Any callers should set read_car_only to true.
257 nodeRead(bool read_car_only)
264 bool make_dotted_pair_cell = false;
266 token = lsptok(NULL, &tok_len);
271 type = nodeTokenType(token, tok_len);
276 this_value = parsePlanString();
277 token = lsptok(NULL, &tok_len);
279 elog(ERROR, "nodeRead: did not find '}' at end of plan node");
281 make_dotted_pair_cell = true;
283 make_dotted_pair_cell = false;
288 List *l = makeNode(List);
290 lfirst(l) = nodeRead(false);
291 lnext(l) = nodeRead(false);
292 this_value = (Node *) l;
295 this_value = nodeRead(false);
309 * It might be NULL but it is an atom!
312 make_dotted_pair_cell = false;
314 make_dotted_pair_cell = true;
318 /* !attention! result is not a Node. Use with caution. */
319 this_value = (Node *) debackslash(token, tok_len);
320 make_dotted_pair_cell = true;
324 /* we know that the token terminates on a char atol will stop at */
325 this_value = (Node *) makeInteger(atol(token));
326 make_dotted_pair_cell = true;
330 char *fval = (char *) palloc(tok_len + 1);
332 memcpy(fval, token, tok_len);
333 fval[tok_len] = '\0';
334 this_value = (Node *) makeFloat(fval);
335 make_dotted_pair_cell = true;
339 /* need to remove leading and trailing quotes, and backslashes */
340 this_value = (Node *) makeString(debackslash(token+1, tok_len-2));
341 make_dotted_pair_cell = true;
344 elog(ERROR, "nodeRead: Bad type %d", type);
345 this_value = NULL; /* keep compiler happy */
348 if (make_dotted_pair_cell)
350 List *l = makeNode(List);
352 lfirst(l) = this_value;
355 lnext(l) = nodeRead(false);
358 return_value = (Node *) l;
361 return_value = this_value;