blob: d47476ff7b826b741598c7bece8eebbaf7f137a3 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** An tokenizer for SQL
25**
26** This file contains C code that splits an SQL input string up into
27** individual tokens and sends those tokens one-by-one over to the
28** parser for analysis.
29**
drhdce2cbe2000-05-31 02:27:49 +000030** $Id: tokenize.c,v 1.4 2000/05/31 02:27:49 drh Exp $
drh75897232000-05-29 14:26:00 +000031*/
32#include "sqliteInt.h"
33#include <ctype.h>
drhdcc581c2000-05-30 13:44:19 +000034#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000035
36/*
37** All the keywords of the SQL language are stored as in a hash
38** table composed of instances of the following structure.
39*/
40typedef struct Keyword Keyword;
41struct Keyword {
42 char *zName; /* The keyword name */
43 int len; /* Number of characters in the keyword */
44 int tokenType; /* The token value for this keyword */
45 Keyword *pNext; /* Next keyword with the same hash */
46};
47
48/*
49** These are the keywords
50*/
51static Keyword aKeywordTable[] = {
52 { "AND", 0, TK_AND, 0 },
53 { "AS", 0, TK_AS, 0 },
54 { "ASC", 0, TK_ASC, 0 },
55 { "BY", 0, TK_BY, 0 },
56 { "CHECK", 0, TK_CHECK, 0 },
57 { "CONSTRAINT", 0, TK_CONSTRAINT, 0 },
drh982cef72000-05-30 16:27:03 +000058 { "COPY", 0, TK_COPY, 0 },
drh75897232000-05-29 14:26:00 +000059 { "CREATE", 0, TK_CREATE, 0 },
60 { "DEFAULT", 0, TK_DEFAULT, 0 },
61 { "DELETE", 0, TK_DELETE, 0 },
drh982cef72000-05-30 16:27:03 +000062 { "DELIMITERS", 0, TK_DELIMITERS, 0 },
drh75897232000-05-29 14:26:00 +000063 { "DESC", 0, TK_DESC, 0 },
64 { "DROP", 0, TK_DROP, 0 },
65 { "EXPLAIN", 0, TK_EXPLAIN, 0 },
66 { "FROM", 0, TK_FROM, 0 },
drhdce2cbe2000-05-31 02:27:49 +000067 { "GLOB", 0, TK_GLOB, 0 },
drh75897232000-05-29 14:26:00 +000068 { "INDEX", 0, TK_INDEX, 0 },
69 { "INSERT", 0, TK_INSERT, 0 },
70 { "INTO", 0, TK_INTO, 0 },
71 { "IS", 0, TK_IS, 0 },
72 { "ISNULL", 0, TK_ISNULL, 0 },
73 { "KEY", 0, TK_KEY, 0 },
drhdce2cbe2000-05-31 02:27:49 +000074 { "LIKE", 0, TK_LIKE, 0 },
drh75897232000-05-29 14:26:00 +000075 { "NOT", 0, TK_NOT, 0 },
76 { "NOTNULL", 0, TK_NOTNULL, 0 },
77 { "NULL", 0, TK_NULL, 0 },
78 { "ON", 0, TK_ON, 0 },
79 { "OR", 0, TK_OR, 0 },
80 { "ORDER", 0, TK_ORDER, 0 },
81 { "PRIMARY", 0, TK_PRIMARY, 0 },
82 { "SELECT", 0, TK_SELECT, 0 },
83 { "SET", 0, TK_SET, 0 },
84 { "TABLE", 0, TK_TABLE, 0 },
85 { "UNIQUE", 0, TK_UNIQUE, 0 },
86 { "UPDATE", 0, TK_UPDATE, 0 },
drh982cef72000-05-30 16:27:03 +000087 { "USING", 0, TK_USING, 0 },
drhdce2cbe2000-05-31 02:27:49 +000088 { "VACUUM", 0, TK_VACUUM, 0 },
drh75897232000-05-29 14:26:00 +000089 { "VALUES", 0, TK_VALUES, 0 },
90 { "WHERE", 0, TK_WHERE, 0 },
91};
92
93/*
94** This is the hash table
95*/
96#define KEY_HASH_SIZE 37
97static Keyword *apHashTable[KEY_HASH_SIZE];
98
99
100/*
101** This function looks up an identifier to determine if it is a
102** keyword. If it is a keyword, the token code of that keyword is
103** returned. If the input is not a keyword, TK_ID is returned.
104*/
105static int sqliteKeywordCode(const char *z, int n){
106 int h;
107 Keyword *p;
108 if( aKeywordTable[0].len==0 ){
109 /* Initialize the keyword hash table */
110 int i;
111 int n;
112 n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
113 for(i=0; i<n; i++){
114 aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
115 h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
116 h %= KEY_HASH_SIZE;
117 aKeywordTable[i].pNext = apHashTable[h];
118 apHashTable[h] = &aKeywordTable[i];
119 }
120 }
121 h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
122 for(p=apHashTable[h]; p; p=p->pNext){
123 if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){
124 return p->tokenType;
125 }
126 }
127 return TK_ID;
128}
129
130/*
131** Return the length of the token that begins at z[0]. Return
132** -1 if the token is (or might be) incomplete. Store the token
133** type in *tokenType before returning.
134*/
135int sqliteGetToken(const char *z, int *tokenType){
136 int i;
137 switch( *z ){
138 case ' ': case '\t': case '\n': case '\f': {
139 for(i=1; z[i] && isspace(z[i]); i++){}
140 *tokenType = TK_SPACE;
141 return i;
142 }
143 case '-': {
144 if( z[1]==0 ) return -1;
145 if( z[1]=='-' ){
146 for(i=2; z[i] && z[i]!='\n'; i++){}
147 *tokenType = TK_COMMENT;
148 return i;
149 }
150 *tokenType = TK_MINUS;
151 return 1;
152 }
153 case '(': {
154 *tokenType = TK_LP;
155 return 1;
156 }
157 case ')': {
158 *tokenType = TK_RP;
159 return 1;
160 }
161 case ';': {
162 *tokenType = TK_SEMI;
163 return 1;
164 }
165 case '+': {
166 *tokenType = TK_PLUS;
167 return 1;
168 }
169 case '*': {
170 *tokenType = TK_STAR;
171 return 1;
172 }
173 case '/': {
174 *tokenType = TK_SLASH;
175 return 1;
176 }
177 case '=': {
178 *tokenType = TK_EQ;
179 return 1 + (z[1]=='=');
180 }
181 case '<': {
182 if( z[1]=='=' ){
183 *tokenType = TK_LE;
184 return 2;
185 }else if( z[1]=='>' ){
186 *tokenType = TK_NE;
187 return 2;
188 }else{
189 *tokenType = TK_LT;
190 return 1;
191 }
192 }
193 case '>': {
194 if( z[1]=='=' ){
195 *tokenType = TK_GE;
196 return 2;
197 }else{
198 *tokenType = TK_GT;
199 return 1;
200 }
201 }
202 case '!': {
203 if( z[1]!='=' ){
204 *tokenType = TK_ILLEGAL;
205 return 1;
206 }else{
207 *tokenType = TK_NE;
208 return 2;
209 }
210 }
211 case ',': {
212 *tokenType = TK_COMMA;
213 return 1;
214 }
215 case '\'': case '"': {
216 int delim = z[0];
217 for(i=1; z[i]; i++){
218 if( z[i]==delim ){
219 if( z[i+1]==delim ){
220 i++;
221 }else{
222 break;
223 }
224 }
225 }
226 if( z[i] ) i++;
227 *tokenType = TK_STRING;
228 return i;
229 }
230 case '.': {
231 if( !isdigit(z[1]) ){
232 *tokenType = TK_DOT;
233 return 1;
234 }
235 /* Fall thru into the next case */
236 }
237 case '0': case '1': case '2': case '3': case '4':
238 case '5': case '6': case '7': case '8': case '9': {
239 for(i=1; z[i] && isdigit(z[i]); i++){}
240 if( z[i]=='.' ){
241 i++;
242 while( z[i] && isdigit(z[i]) ){ i++; }
243 if( (z[i]=='e' || z[i]=='E') &&
244 ( isdigit(z[i+1])
245 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
246 )
247 ){
248 i += 2;
249 while( z[i] && isdigit(z[i]) ){ i++; }
250 }
251 *tokenType = TK_FLOAT;
252 }else if( z[0]=='.' ){
253 *tokenType = TK_FLOAT;
254 }else{
255 *tokenType = TK_INTEGER;
256 }
257 return i;
258 }
259 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
260 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
261 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
262 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
263 case 'y': case 'z': case '_':
264 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
265 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
266 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
267 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
268 case 'Y': case 'Z': {
269 for(i=1; z[i] && (isalnum(z[i]) || z[i]=='_'); i++){}
270 *tokenType = sqliteKeywordCode(z, i);
271 return i;
272 }
273 default: {
274 break;
275 }
276 }
277 *tokenType = TK_ILLEGAL;
278 return 1;
279}
280
281/*
282** Run the parser on the given SQL string. The parser structure is
283** passed in. Return the number of errors.
284*/
285int sqliteRunParser(Parse *pParse, char *zSql, char **pzErrMsg){
286 int nErr = 0;
287 int i;
288 void *pEngine;
289 int once = 1;
290 static FILE *trace = 0;
291 extern void *sqliteParserAlloc(void*(*)(int));
292 extern void sqliteParserFree(void*, void(*)(void*));
293 extern int sqliteParser(void*, int, ...);
294 extern void sqliteParserTrace(FILE*, char *);
295
296 i = 0;
drh982cef72000-05-30 16:27:03 +0000297 pEngine = sqliteParserAlloc((void*(*)(int))malloc);
drh75897232000-05-29 14:26:00 +0000298 if( pEngine==0 ){
299 sqliteSetString(pzErrMsg, "out of memory", 0);
300 return 1;
301 }
302 sqliteParserTrace(trace, "parser: ");
303 while( nErr==0 && i>=0 && zSql[i]!=0 ){
304 int tokenType;
305
306 pParse->sLastToken.z = &zSql[i];
307 pParse->sLastToken.n = sqliteGetToken(&zSql[i], &tokenType);
308 i += pParse->sLastToken.n;
309 if( once ){
310 pParse->sFirstToken = pParse->sLastToken;
311 once = 0;
312 }
313 switch( tokenType ){
314 case TK_SPACE:
315 break;
316 case TK_COMMENT: {
317 /* Various debugging modes can be turned on and off using
318 ** special SQL comments. Check for the special comments
319 ** here and take approriate action if found.
320 */
321 char *z = pParse->sLastToken.z;
322 if( sqliteStrNICmp(z,"--parser-trace-on--",19)==0 ){
323 trace = stderr;
324 sqliteParserTrace(trace, "parser: ");
325 }else if( sqliteStrNICmp(z,"--parser-trace-off--", 20)==0 ){
326 trace = 0;
327 sqliteParserTrace(trace, "parser: ");
328 }else if( sqliteStrNICmp(z,"--vdbe-trace-on--",17)==0 ){
329 pParse->db->flags |= SQLITE_VdbeTrace;
330 }else if( sqliteStrNICmp(z,"--vdbe-trace-off--", 19)==0 ){
331 pParse->db->flags &= ~SQLITE_VdbeTrace;
332 }
333 break;
334 }
335 case TK_ILLEGAL:
336 sqliteSetNString(pzErrMsg, "illegal token: \"", -1,
337 pParse->sLastToken.z, pParse->sLastToken.n, 0);
338 nErr++;
339 break;
340 default:
341 sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse);
342 if( pParse->zErrMsg ){
343 sqliteSetNString(pzErrMsg, "near \"", -1,
344 pParse->sErrToken.z, pParse->sErrToken.n,
345 "\": ", -1,
346 pParse->zErrMsg, -1,
347 0);
348 nErr++;
349 }
350 break;
351 }
352 }
353 if( nErr==0 ){
354 sqliteParser(pEngine, 0, pParse->sLastToken, pParse);
355 if( pParse->zErrMsg ){
356 sqliteSetNString(pzErrMsg, "near \"", -1,
357 pParse->sErrToken.z, pParse->sErrToken.n,
358 "\": ", -1,
359 pParse->zErrMsg, -1,
360 0);
361 nErr++;
362 }
363 }
drhdcc581c2000-05-30 13:44:19 +0000364 sqliteParserFree(pEngine, free);
drh75897232000-05-29 14:26:00 +0000365 if( pParse->zErrMsg ){
366 if( pzErrMsg ){
367 *pzErrMsg = pParse->zErrMsg;
368 }else{
369 sqliteFree(pParse->zErrMsg);
370 }
371 if( !nErr ) nErr++;
372 }
373 if( pParse->pVdbe ){
374 sqliteVdbeDelete(pParse->pVdbe);
375 pParse->pVdbe = 0;
376 }
377 if( pParse->pNewTable ){
378 sqliteDeleteTable(pParse->db, pParse->pNewTable);
379 pParse->pNewTable = 0;
380 }
381 return nErr;
382}