aboutsummaryrefslogtreecommitdiff
path: root/src/backend/nodes/read.c
blob: dbcc59a6f93fb064e04b81aab4f8e4f47c567c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*-------------------------------------------------------------------------
 *
 * read.c--
 *    routines to convert a string (legal ascii representation of node) back
 *    to nodes
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.1.1.1 1996/07/09 06:21:33 scrappy Exp $
 *
 * HISTORY
 *    AUTHOR		DATE		MAJOR EVENT
 *    Andrew Yu		Nov 2, 1994	file creation
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "postgres.h"
#include "nodes/pg_list.h"
#include "nodes/readfuncs.h"
#include "utils/elog.h"

/*
 * stringToNode -
 *    returns a Node with a given legal ascii representation
 */
void *
stringToNode(char *str)
{
    void *retval;

    (void) lsptok(str, NULL);	/* set the string used in lsptok */
    retval = nodeRead(true);	/* start reading */

    return retval;
}

/*****************************************************************************
 *
 * the lisp token parser
 *
 *****************************************************************************/

#define RIGHT_PAREN (1000000 + 1)
#define LEFT_PAREN  (1000000 + 2)
#define PLAN_SYM    (1000000 + 3)
#define AT_SYMBOL   (1000000 + 4)
#define ATOM_TOKEN  (1000000 + 5)

/*
 * nodeTokenType -
 *    returns the type of the node token contained in token.
 *    It returns one of the following valid NodeTags:
 *	T_Integer, T_Float, T_String
 *    and some of its own:
 * 	RIGHT_PAREN, LEFT_PAREN, PLAN_SYM, AT_SYMBOL, ATOM_TOKEN
 *
 *    Assumption: the ascii representation is legal
 */
static NodeTag
nodeTokenType(char *token, int length)
{
    NodeTag retval;
    
    /*
     * Check if the token is a number (decimal or integer,
     * positive or negative
     */
    if (isdigit(*token) ||
	(length>=2 && *token=='-' && isdigit(*(token+1)) ))
	{
	    /*
	     * skip the optional '-' (i.e. negative number)
	     */
	    if (*token == '-') {
		token++;
	    }
	    
	    /*
	     * See if there is a decimal point
	     */
	    
	    for (; length && *token != '.'; token++, length--);
	    
	    /*
	     * if there isn't, token's an int, otherwise it's a float.
	     */
	    
	    retval = (*token != '.') ? T_Integer : T_Float;
	}
    else if (isalpha(*token))
	retval = ATOM_TOKEN;
    else if (*token == '(')
	retval = LEFT_PAREN;
    else if (*token == ')')
	retval = RIGHT_PAREN;
    else if (*token == '@')
	retval = AT_SYMBOL;
    else if (*token == '\"')
	retval = T_String;
    else if (*token == '{')
	retval = PLAN_SYM;
    return(retval);
}

/*
 * Works kinda like strtok, except it doesn't put nulls into string.
 * 
 * Returns the length in length instead.  The string can be set without
 * returning a token by calling lsptok with length == NULL.
 *
 */
char *
lsptok(char *string, int *length)
{
    static char *local_str;
    char *ret_string;
    
    if (string != NULL) {
	local_str = string;
	if (length == NULL) {
	    return(NULL);
	}
    }
    
    for (; *local_str == ' '
	 || *local_str == '\n'
	 || *local_str == '\t'; local_str++);
    
    /*
     * Now pointing at next token.
     */
    ret_string = local_str;
    if (*local_str == '\0') return(NULL);
    *length = 1;
    
    if (*local_str == '\"') {
	for (local_str++; *local_str != '\"'; (*length)++, local_str++);
	(*length)++; local_str++;
    }else if (*local_str == ')' || *local_str == '(' ||
	      *local_str == '}' || *local_str == '{') {
	local_str++;
    }else {
	for (; *local_str != ' '
	     && *local_str != '\n'
	     && *local_str != '\t'
	     && *local_str != '{'
	     && *local_str != '}'
	     && *local_str != '('
	     && *local_str != ')'; local_str++, (*length)++);
	(*length)--;
    }
    return(ret_string);
}

/*
 * This guy does all the reading.
 *
 * Secrets:  He assumes that lsptok already has the string (see below).
 * Any callers should set read_car_only to true.
 */
void *
nodeRead(bool read_car_only)
{
    char *token;
    NodeTag type;
    Node *this_value, *return_value;
    int tok_len;
    char tmp;
    bool make_dotted_pair_cell = false;
    
    token = lsptok(NULL, &tok_len);
    
    if (token == NULL) return(NULL);
    
    type = nodeTokenType(token, tok_len);
    
    switch(type) {
    case PLAN_SYM:
	this_value = parsePlanString();
	token = lsptok(NULL, &tok_len);
	if (token[0] != '}') return(NULL);

	if (!read_car_only)
	    make_dotted_pair_cell = true;
	else
	    make_dotted_pair_cell = false;
	break;
    case LEFT_PAREN:
	if (!read_car_only) {
	    List *l = makeNode(List);

	    lfirst(l) = nodeRead(false);
	    lnext(l) = nodeRead(false);
	    this_value = (Node*)l;
	}else {
	    this_value = nodeRead(false);
	}
	break;
    case RIGHT_PAREN:
	this_value = NULL;
	break;
    case AT_SYMBOL:
	break;
    case ATOM_TOKEN:
	if (!strncmp(token, "nil", 3)) {
	    this_value = NULL;
	    /*
	     * It might be "nil" but it is an atom!
	     */
	    if (read_car_only) {
		make_dotted_pair_cell = false;
	    } else {
		make_dotted_pair_cell = true;
	    }
	}else {
	    tmp = token[tok_len];
	    token[tok_len] = '\0';
	    this_value = (Node*)pstrdup(token);	/* !attention! not a Node.
						   use with caution */
	    token[tok_len] = tmp;
	    make_dotted_pair_cell = true;
	}
	break;
    case T_Float:
	tmp = token[tok_len];
	token[tok_len] = '\0';
	this_value = (Node*)makeFloat(atof(token));
	token[tok_len] = tmp;
	make_dotted_pair_cell = true;
	break;
    case T_Integer:
	tmp = token[tok_len];
	token[tok_len] = '\0';
	this_value = (Node*)makeInteger(atoi(token));
	token[tok_len] = tmp;
	make_dotted_pair_cell = true;
	break;
    case T_String:
	tmp = token[tok_len - 1];
	token[tok_len - 1] = '\0';
	token++;
	this_value = (Node*)makeString(token);		/* !! not strdup'd */
	token[tok_len - 2] = tmp;
	make_dotted_pair_cell = true;
	break;
    default:
	elog(WARN, "nodeRead: Bad type %d", type);
	break;
    }
    if (make_dotted_pair_cell) {
	List *l = makeNode(List);

	lfirst(l) = this_value;
	if (!read_car_only) {
	    lnext(l) = nodeRead(false);
	}else {
	    lnext(l) = NULL;
	}
	return_value = (Node*)l;
    }else {
	return_value = this_value;
    }
    return(return_value);
}