1 /*-------------------------------------------------------------------------
4 * routines to convert a string (legal ascii representation of node) back
7 * Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.9 1998/01/07 15:32:31 momjian Exp $
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Nov 2, 1994 file creation
17 *-------------------------------------------------------------------------
23 #include "nodes/pg_list.h"
24 #include "nodes/readfuncs.h"
25 #include "utils/elog.h"
29 * returns a Node with a given legal ascii representation
32 stringToNode(char *str)
36 lsptok(str, NULL); /* set the string used in lsptok */
37 retval = nodeRead(true); /* start reading */
42 /*****************************************************************************
44 * the lisp token parser
46 *****************************************************************************/
48 #define RIGHT_PAREN (1000000 + 1)
49 #define LEFT_PAREN (1000000 + 2)
50 #define PLAN_SYM (1000000 + 3)
51 #define AT_SYMBOL (1000000 + 4)
52 #define ATOM_TOKEN (1000000 + 5)
56 * returns the type of the node token contained in token.
57 * It returns one of the following valid NodeTags:
58 * T_Integer, T_Float, T_String
59 * and some of its own:
60 * RIGHT_PAREN, LEFT_PAREN, PLAN_SYM, AT_SYMBOL, ATOM_TOKEN
62 * Assumption: the ascii representation is legal
65 nodeTokenType(char *token, int length)
70 * Check if the token is a number (decimal or integer, positive or
73 if (isdigit(*token) ||
74 (length >= 2 && *token == '-' && isdigit(*(token + 1))))
78 * skip the optional '-' (i.e. negative number)
86 * See if there is a decimal point
89 for (; length && *token != '.'; token++, length--);
92 * if there isn't, token's an int, otherwise it's a float.
95 retval = (*token != '.') ? T_Integer : T_Float;
97 else if (isalpha(*token) || *token == '_' ||
98 (token[0] == '<' && token[1] == '>'))
100 else if (*token == '(')
102 else if (*token == ')')
103 retval = RIGHT_PAREN;
104 else if (*token == '@')
106 else if (*token == '\"')
108 else if (*token == '{')
114 * Works kinda like strtok, except it doesn't put nulls into string.
116 * Returns the length in length instead. The string can be set without
117 * returning a token by calling lsptok with length == NULL.
121 lsptok(char *string, int *length)
123 static char *local_str;
135 for (; *local_str == ' '
136 || *local_str == '\n'
137 || *local_str == '\t'; local_str++);
140 * Now pointing at next token.
142 ret_string = local_str;
143 if (*local_str == '\0')
147 if (*local_str == '"')
149 for (local_str++; *local_str != '"'; (*length)++, local_str++)
155 else if (local_str[0] == '<' && local_str[1] == '>' )
160 else if (*local_str == ')' || *local_str == '(' ||
161 *local_str == '}' || *local_str == '{')
167 for (; *local_str != ' '
168 && *local_str != '\n'
169 && *local_str != '\t'
173 && *local_str != ')'; local_str++, (*length)++);
180 * This guy does all the reading.
182 * Secrets: He assumes that lsptok already has the string (see below).
183 * Any callers should set read_car_only to true.
186 nodeRead(bool read_car_only)
190 Node *this_value = NULL,
191 *return_value = NULL;
194 bool make_dotted_pair_cell = false;
196 token = lsptok(NULL, &tok_len);
201 type = nodeTokenType(token, tok_len);
206 this_value = parsePlanString();
207 token = lsptok(NULL, &tok_len);
212 make_dotted_pair_cell = true;
214 make_dotted_pair_cell = false;
219 List *l = makeNode(List);
221 lfirst(l) = nodeRead(false);
222 lnext(l) = nodeRead(false);
223 this_value = (Node *) l;
227 this_value = nodeRead(false);
236 if (!strncmp(token, "<>", 2))
241 * It might be NULL but it is an atom!
245 make_dotted_pair_cell = false;
249 make_dotted_pair_cell = true;
254 tmp = token[tok_len];
255 token[tok_len] = '\0';
256 this_value = (Node *) pstrdup(token); /* !attention! not a
259 token[tok_len] = tmp;
260 make_dotted_pair_cell = true;
264 tmp = token[tok_len];
265 token[tok_len] = '\0';
266 this_value = (Node *) makeFloat(atof(token));
267 token[tok_len] = tmp;
268 make_dotted_pair_cell = true;
271 tmp = token[tok_len];
272 token[tok_len] = '\0';
273 this_value = (Node *) makeInteger(atoi(token));
274 token[tok_len] = tmp;
275 make_dotted_pair_cell = true;
278 tmp = token[tok_len - 1];
279 token[tok_len - 1] = '\0';
281 this_value = (Node *) makeString(token); /* !! not strdup'd */
282 token[tok_len - 2] = tmp;
283 make_dotted_pair_cell = true;
286 elog(ABORT, "nodeRead: Bad type %d", type);
289 if (make_dotted_pair_cell)
291 List *l = makeNode(List);
293 lfirst(l) = this_value;
297 lnext(l) = nodeRead(false);
303 return_value = (Node *) l;
307 return_value = this_value;
309 return (return_value);