blob: 5424eb0476ba8914b96f94d95597f47f3bf5ddbb [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** An tokenizer for SQL
13**
14** This file contains C code that splits an SQL input string up into
15** individual tokens and sends those tokens one-by-one over to the
16** parser for analysis.
17**
drhaa756b02004-09-25 15:25:26 +000018** $Id: tokenize.c,v 1.89 2004/09/25 15:25:26 drh Exp $
drh75897232000-05-29 14:26:00 +000019*/
20#include "sqliteInt.h"
drhad75e982001-10-09 04:19:46 +000021#include "os.h"
drh75897232000-05-29 14:26:00 +000022#include <ctype.h>
drhdcc581c2000-05-30 13:44:19 +000023#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000024
25/*
26** All the keywords of the SQL language are stored as in a hash
27** table composed of instances of the following structure.
28*/
29typedef struct Keyword Keyword;
30struct Keyword {
31 char *zName; /* The keyword name */
drhba212562004-01-08 02:17:31 +000032 u8 tokenType; /* Token value for this keyword */
33 u8 len; /* Length of this keyword */
34 u8 iNext; /* Index in aKeywordTable[] of next with same hash */
drh75897232000-05-29 14:26:00 +000035};
36
37/*
38** These are the keywords
39*/
40static Keyword aKeywordTable[] = {
drhba212562004-01-08 02:17:31 +000041 { "ABORT", TK_ABORT, },
42 { "AFTER", TK_AFTER, },
43 { "ALL", TK_ALL, },
44 { "AND", TK_AND, },
45 { "AS", TK_AS, },
46 { "ASC", TK_ASC, },
47 { "ATTACH", TK_ATTACH, },
48 { "BEFORE", TK_BEFORE, },
49 { "BEGIN", TK_BEGIN, },
50 { "BETWEEN", TK_BETWEEN, },
51 { "BY", TK_BY, },
52 { "CASCADE", TK_CASCADE, },
53 { "CASE", TK_CASE, },
54 { "CHECK", TK_CHECK, },
drhba212562004-01-08 02:17:31 +000055 { "COLLATE", TK_COLLATE, },
56 { "COMMIT", TK_COMMIT, },
57 { "CONFLICT", TK_CONFLICT, },
58 { "CONSTRAINT", TK_CONSTRAINT, },
drhba212562004-01-08 02:17:31 +000059 { "CREATE", TK_CREATE, },
60 { "CROSS", TK_JOIN_KW, },
61 { "DATABASE", TK_DATABASE, },
62 { "DEFAULT", TK_DEFAULT, },
63 { "DEFERRED", TK_DEFERRED, },
64 { "DEFERRABLE", TK_DEFERRABLE, },
65 { "DELETE", TK_DELETE, },
drhba212562004-01-08 02:17:31 +000066 { "DESC", TK_DESC, },
67 { "DETACH", TK_DETACH, },
68 { "DISTINCT", TK_DISTINCT, },
69 { "DROP", TK_DROP, },
70 { "END", TK_END, },
71 { "EACH", TK_EACH, },
72 { "ELSE", TK_ELSE, },
73 { "EXCEPT", TK_EXCEPT, },
74 { "EXPLAIN", TK_EXPLAIN, },
75 { "FAIL", TK_FAIL, },
76 { "FOR", TK_FOR, },
77 { "FOREIGN", TK_FOREIGN, },
78 { "FROM", TK_FROM, },
79 { "FULL", TK_JOIN_KW, },
80 { "GLOB", TK_GLOB, },
81 { "GROUP", TK_GROUP, },
82 { "HAVING", TK_HAVING, },
83 { "IGNORE", TK_IGNORE, },
84 { "IMMEDIATE", TK_IMMEDIATE, },
85 { "IN", TK_IN, },
86 { "INDEX", TK_INDEX, },
87 { "INITIALLY", TK_INITIALLY, },
88 { "INNER", TK_JOIN_KW, },
89 { "INSERT", TK_INSERT, },
90 { "INSTEAD", TK_INSTEAD, },
91 { "INTERSECT", TK_INTERSECT, },
92 { "INTO", TK_INTO, },
93 { "IS", TK_IS, },
94 { "ISNULL", TK_ISNULL, },
95 { "JOIN", TK_JOIN, },
96 { "KEY", TK_KEY, },
97 { "LEFT", TK_JOIN_KW, },
98 { "LIKE", TK_LIKE, },
99 { "LIMIT", TK_LIMIT, },
100 { "MATCH", TK_MATCH, },
101 { "NATURAL", TK_JOIN_KW, },
102 { "NOT", TK_NOT, },
103 { "NOTNULL", TK_NOTNULL, },
104 { "NULL", TK_NULL, },
105 { "OF", TK_OF, },
106 { "OFFSET", TK_OFFSET, },
107 { "ON", TK_ON, },
108 { "OR", TK_OR, },
109 { "ORDER", TK_ORDER, },
110 { "OUTER", TK_JOIN_KW, },
111 { "PRAGMA", TK_PRAGMA, },
112 { "PRIMARY", TK_PRIMARY, },
113 { "RAISE", TK_RAISE, },
114 { "REFERENCES", TK_REFERENCES, },
115 { "REPLACE", TK_REPLACE, },
116 { "RESTRICT", TK_RESTRICT, },
117 { "RIGHT", TK_JOIN_KW, },
118 { "ROLLBACK", TK_ROLLBACK, },
119 { "ROW", TK_ROW, },
120 { "SELECT", TK_SELECT, },
121 { "SET", TK_SET, },
122 { "STATEMENT", TK_STATEMENT, },
123 { "TABLE", TK_TABLE, },
124 { "TEMP", TK_TEMP, },
125 { "TEMPORARY", TK_TEMP, },
126 { "THEN", TK_THEN, },
127 { "TRANSACTION", TK_TRANSACTION, },
128 { "TRIGGER", TK_TRIGGER, },
129 { "UNION", TK_UNION, },
130 { "UNIQUE", TK_UNIQUE, },
131 { "UPDATE", TK_UPDATE, },
132 { "USING", TK_USING, },
133 { "VACUUM", TK_VACUUM, },
134 { "VALUES", TK_VALUES, },
135 { "VIEW", TK_VIEW, },
136 { "WHEN", TK_WHEN, },
137 { "WHERE", TK_WHERE, },
drh75897232000-05-29 14:26:00 +0000138};
139
140/*
141** This is the hash table
142*/
drhba212562004-01-08 02:17:31 +0000143#define KEY_HASH_SIZE 101
144static u8 aiHashTable[KEY_HASH_SIZE];
drh75897232000-05-29 14:26:00 +0000145
146
147/*
148** This function looks up an identifier to determine if it is a
149** keyword. If it is a keyword, the token code of that keyword is
150** returned. If the input is not a keyword, TK_ID is returned.
151*/
danielk19774adee202004-05-08 08:23:19 +0000152int sqlite3KeywordCode(const char *z, int n){
drhba212562004-01-08 02:17:31 +0000153 int h, i;
drh75897232000-05-29 14:26:00 +0000154 Keyword *p;
drh93a5c6b2003-12-23 02:17:35 +0000155 static char needInit = 1;
156 if( needInit ){
drh75897232000-05-29 14:26:00 +0000157 /* Initialize the keyword hash table */
danielk19774adee202004-05-08 08:23:19 +0000158 sqlite3OsEnterMutex();
drh93a5c6b2003-12-23 02:17:35 +0000159 if( needInit ){
drhba212562004-01-08 02:17:31 +0000160 int nk;
161 nk = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
drhaa756b02004-09-25 15:25:26 +0000162 for(i=0, p=aKeywordTable; i<nk; i++, p++){
163 const char *zName = p->zName;
164 int len = p->len = strlen(zName);
165 h = sqlite3HashNoCase(zName, len) % KEY_HASH_SIZE;
166 p->iNext = aiHashTable[h];
drhba212562004-01-08 02:17:31 +0000167 aiHashTable[h] = i+1;
drhad75e982001-10-09 04:19:46 +0000168 }
drh93a5c6b2003-12-23 02:17:35 +0000169 needInit = 0;
drh75897232000-05-29 14:26:00 +0000170 }
danielk19774adee202004-05-08 08:23:19 +0000171 sqlite3OsLeaveMutex();
drh75897232000-05-29 14:26:00 +0000172 }
danielk19774adee202004-05-08 08:23:19 +0000173 h = sqlite3HashNoCase(z, n) % KEY_HASH_SIZE;
drhba212562004-01-08 02:17:31 +0000174 for(i=aiHashTable[h]; i; i=p->iNext){
175 p = &aKeywordTable[i-1];
danielk19774adee202004-05-08 08:23:19 +0000176 if( p->len==n && sqlite3StrNICmp(p->zName, z, n)==0 ){
drh75897232000-05-29 14:26:00 +0000177 return p->tokenType;
178 }
179 }
180 return TK_ID;
181}
182
drh98808ba2001-10-18 12:34:46 +0000183
184/*
drhba212562004-01-08 02:17:31 +0000185** If X is a character that can be used in an identifier and
186** X&0x80==0 then isIdChar[X] will be 1. If X&0x80==0x80 then
187** X is always an identifier character. (Hence all UTF-8
188** characters can be part of an identifier). isIdChar[X] will
189** be 0 for every character in the lower 128 ASCII characters
190** that cannot be used as part of an identifier.
drh98808ba2001-10-18 12:34:46 +0000191**
192** In this implementation, an identifier can be a string of
193** alphabetic characters, digits, and "_" plus any character
194** with the high-order bit set. The latter rule means that
195** any sequence of UTF-8 characters or characters taken from
196** an extended ISO8859 character set can form an identifier.
197*/
198static const char isIdChar[] = {
199/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
drh98808ba2001-10-18 12:34:46 +0000200 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
201 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
202 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
203 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
204 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
drh98808ba2001-10-18 12:34:46 +0000205};
206
drhaa756b02004-09-25 15:25:26 +0000207#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x2f && isIdChar[c-0x30]))
drh98808ba2001-10-18 12:34:46 +0000208
drh75897232000-05-29 14:26:00 +0000209/*
drh61b487d2003-09-12 02:08:14 +0000210** Return the length of the token that begins at z[0].
211** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000212*/
drh98808ba2001-10-18 12:34:46 +0000213static int sqliteGetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000214 int i, c;
drh75897232000-05-29 14:26:00 +0000215 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000216 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh32eb7b42003-01-07 01:44:37 +0000217 for(i=1; isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000218 *tokenType = TK_SPACE;
219 return i;
220 }
221 case '-': {
drh75897232000-05-29 14:26:00 +0000222 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000223 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drh75897232000-05-29 14:26:00 +0000224 *tokenType = TK_COMMENT;
225 return i;
226 }
227 *tokenType = TK_MINUS;
228 return 1;
229 }
230 case '(': {
drhdab35182003-09-27 13:39:38 +0000231 *tokenType = TK_LP;
232 return 1;
drh75897232000-05-29 14:26:00 +0000233 }
234 case ')': {
235 *tokenType = TK_RP;
236 return 1;
237 }
238 case ';': {
239 *tokenType = TK_SEMI;
240 return 1;
241 }
242 case '+': {
243 *tokenType = TK_PLUS;
244 return 1;
245 }
246 case '*': {
247 *tokenType = TK_STAR;
248 return 1;
249 }
250 case '/': {
drh66105a82002-08-27 14:28:29 +0000251 if( z[1]!='*' || z[2]==0 ){
252 *tokenType = TK_SLASH;
253 return 1;
254 }
drhaa756b02004-09-25 15:25:26 +0000255 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
256 if( c ) i++;
drh66105a82002-08-27 14:28:29 +0000257 *tokenType = TK_COMMENT;
258 return i;
drh75897232000-05-29 14:26:00 +0000259 }
drhbf4133c2001-10-13 02:59:08 +0000260 case '%': {
261 *tokenType = TK_REM;
262 return 1;
263 }
drh75897232000-05-29 14:26:00 +0000264 case '=': {
265 *tokenType = TK_EQ;
266 return 1 + (z[1]=='=');
267 }
268 case '<': {
drhaa756b02004-09-25 15:25:26 +0000269 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000270 *tokenType = TK_LE;
271 return 2;
drhaa756b02004-09-25 15:25:26 +0000272 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000273 *tokenType = TK_NE;
274 return 2;
drhaa756b02004-09-25 15:25:26 +0000275 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000276 *tokenType = TK_LSHIFT;
277 return 2;
drh75897232000-05-29 14:26:00 +0000278 }else{
279 *tokenType = TK_LT;
280 return 1;
281 }
282 }
283 case '>': {
drhaa756b02004-09-25 15:25:26 +0000284 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000285 *tokenType = TK_GE;
286 return 2;
drhaa756b02004-09-25 15:25:26 +0000287 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000288 *tokenType = TK_RSHIFT;
289 return 2;
drh75897232000-05-29 14:26:00 +0000290 }else{
291 *tokenType = TK_GT;
292 return 1;
293 }
294 }
295 case '!': {
296 if( z[1]!='=' ){
297 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000298 return 2;
drh75897232000-05-29 14:26:00 +0000299 }else{
300 *tokenType = TK_NE;
301 return 2;
302 }
303 }
drh00400772000-06-16 20:51:26 +0000304 case '|': {
305 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000306 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000307 return 1;
308 }else{
309 *tokenType = TK_CONCAT;
310 return 2;
311 }
312 }
drh75897232000-05-29 14:26:00 +0000313 case ',': {
314 *tokenType = TK_COMMA;
315 return 1;
316 }
drhbf4133c2001-10-13 02:59:08 +0000317 case '&': {
318 *tokenType = TK_BITAND;
319 return 1;
320 }
321 case '~': {
322 *tokenType = TK_BITNOT;
323 return 1;
324 }
drh75897232000-05-29 14:26:00 +0000325 case '\'': case '"': {
326 int delim = z[0];
drhaa756b02004-09-25 15:25:26 +0000327 for(i=1; (c=z[i])!=0; i++){
328 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000329 if( z[i+1]==delim ){
330 i++;
331 }else{
332 break;
333 }
334 }
335 }
drhaa756b02004-09-25 15:25:26 +0000336 if( c ) i++;
drh75897232000-05-29 14:26:00 +0000337 *tokenType = TK_STRING;
338 return i;
339 }
340 case '.': {
drhbb07e9a2003-04-16 02:17:35 +0000341 *tokenType = TK_DOT;
342 return 1;
drh75897232000-05-29 14:26:00 +0000343 }
344 case '0': case '1': case '2': case '3': case '4':
345 case '5': case '6': case '7': case '8': case '9': {
drhc837e702000-06-08 16:26:24 +0000346 *tokenType = TK_INTEGER;
drh32eb7b42003-01-07 01:44:37 +0000347 for(i=1; isdigit(z[i]); i++){}
drhbb07e9a2003-04-16 02:17:35 +0000348 if( z[i]=='.' && isdigit(z[i+1]) ){
349 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000350 while( isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000351 *tokenType = TK_FLOAT;
352 }
353 if( (z[i]=='e' || z[i]=='E') &&
drh75897232000-05-29 14:26:00 +0000354 ( isdigit(z[i+1])
355 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
356 )
drhc837e702000-06-08 16:26:24 +0000357 ){
358 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000359 while( isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000360 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000361 }
362 return i;
363 }
drh2f4392f2002-02-14 21:42:51 +0000364 case '[': {
drhaa756b02004-09-25 15:25:26 +0000365 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh2f4392f2002-02-14 21:42:51 +0000366 *tokenType = TK_ID;
367 return i;
368 }
drh7c972de2003-09-06 22:18:07 +0000369 case '?': {
drh50457892003-09-06 01:10:47 +0000370 *tokenType = TK_VARIABLE;
drhfa6bc002004-09-07 16:19:52 +0000371 for(i=1; isdigit(z[i]); i++){}
372 return i;
drh50457892003-09-06 01:10:47 +0000373 }
drh895d7472004-08-20 16:02:39 +0000374 case ':': {
drhaa756b02004-09-25 15:25:26 +0000375 for(i=1; IdChar(z[i]); i++){}
drh2c6674c2004-08-25 04:07:01 +0000376 *tokenType = i>1 ? TK_VARIABLE : TK_ILLEGAL;
377 return i;
drh895d7472004-08-20 16:02:39 +0000378 }
379 case '$': {
drh9d74b4c2004-08-24 15:23:34 +0000380 *tokenType = TK_VARIABLE;
drh895d7472004-08-20 16:02:39 +0000381 if( z[1]=='{' ){
382 int nBrace = 1;
383 for(i=2; (c=z[i])!=0 && nBrace; i++){
384 if( c=='{' ){
385 nBrace++;
386 }else if( c=='}' ){
387 nBrace--;
388 }
389 }
drh9d74b4c2004-08-24 15:23:34 +0000390 if( c==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000391 }else{
392 int n = 0;
393 for(i=1; (c=z[i])!=0; i++){
394 if( isalnum(c) || c=='_' ){
395 n++;
396 }else if( c=='(' && n>0 ){
397 do{
398 i++;
399 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
400 if( c==')' ){
401 i++;
drh895d7472004-08-20 16:02:39 +0000402 }else{
403 *tokenType = TK_ILLEGAL;
404 }
405 break;
406 }else if( c==':' && z[i+1]==':' ){
407 i++;
408 }else{
drh895d7472004-08-20 16:02:39 +0000409 break;
410 }
411 }
drh9d74b4c2004-08-24 15:23:34 +0000412 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000413 }
414 return i;
415 }
danielk1977c572ef72004-05-27 09:28:41 +0000416 case 'x': case 'X': {
drhaa756b02004-09-25 15:25:26 +0000417 if( (c=z[1])=='\'' || c=='"' ){
418 int delim = c;
danielk19773fd0a732004-05-27 13:35:19 +0000419 *tokenType = TK_BLOB;
drhaa756b02004-09-25 15:25:26 +0000420 for(i=2; (c=z[i])!=0; i++){
421 if( c==delim ){
danielk19773fd0a732004-05-27 13:35:19 +0000422 if( i%2 ) *tokenType = TK_ILLEGAL;
danielk1977c572ef72004-05-27 09:28:41 +0000423 break;
424 }
drhaa756b02004-09-25 15:25:26 +0000425 if( !isxdigit(c) ){
danielk19773fd0a732004-05-27 13:35:19 +0000426 *tokenType = TK_ILLEGAL;
427 return i;
428 }
danielk1977c572ef72004-05-27 09:28:41 +0000429 }
drhaa756b02004-09-25 15:25:26 +0000430 if( c ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000431 return i;
432 }
433 /* Otherwise fall through to the next case */
434 }
drh98808ba2001-10-18 12:34:46 +0000435 default: {
drhaa756b02004-09-25 15:25:26 +0000436 if( !IdChar(*z) ){
drh98808ba2001-10-18 12:34:46 +0000437 break;
438 }
drhaa756b02004-09-25 15:25:26 +0000439 for(i=1; IdChar(z[i]); i++){}
danielk19774adee202004-05-08 08:23:19 +0000440 *tokenType = sqlite3KeywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000441 return i;
442 }
drh75897232000-05-29 14:26:00 +0000443 }
444 *tokenType = TK_ILLEGAL;
445 return 1;
446}
447
448/*
449** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000450** passed in. An SQLITE_ status code is returned. If an error occurs
451** and pzErrMsg!=NULL then an error message might be written into
452** memory obtained from malloc() and *pzErrMsg made to point to that
453** error message. Or maybe not.
drh75897232000-05-29 14:26:00 +0000454*/
danielk19774adee202004-05-08 08:23:19 +0000455int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000456 int nErr = 0;
457 int i;
458 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000459 int tokenType;
460 int lastTokenParsed = -1;
drh9bb575f2004-09-06 17:24:11 +0000461 sqlite3 *db = pParse->db;
danielk19774adee202004-05-08 08:23:19 +0000462 extern void *sqlite3ParserAlloc(void*(*)(int));
463 extern void sqlite3ParserFree(void*, void(*)(void*));
464 extern int sqlite3Parser(void*, int, Token, Parse*);
drh75897232000-05-29 14:26:00 +0000465
drh6d4abfb2001-10-22 02:58:08 +0000466 db->flags &= ~SQLITE_Interrupt;
drh4c504392000-10-16 22:06:40 +0000467 pParse->rc = SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000468 i = 0;
danielk19774adee202004-05-08 08:23:19 +0000469 pEngine = sqlite3ParserAlloc((void*(*)(int))malloc);
drh75897232000-05-29 14:26:00 +0000470 if( pEngine==0 ){
danielk19774adee202004-05-08 08:23:19 +0000471 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh75897232000-05-29 14:26:00 +0000472 return 1;
473 }
drhfa6bc002004-09-07 16:19:52 +0000474 assert( pParse->sLastToken.dyn==0 );
475 assert( pParse->pNewTable==0 );
476 assert( pParse->pNewTrigger==0 );
477 assert( pParse->nVar==0 );
478 assert( pParse->nVarExpr==0 );
479 assert( pParse->nVarExprAlloc==0 );
480 assert( pParse->apVarExpr==0 );
drh3f7d4e42004-07-24 14:35:58 +0000481 pParse->zTail = pParse->zSql = zSql;
danielk19776f8a5032004-05-10 10:34:51 +0000482 while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000483 assert( i>=0 );
drh75897232000-05-29 14:26:00 +0000484 pParse->sLastToken.z = &zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000485 assert( pParse->sLastToken.dyn==0 );
drh98808ba2001-10-18 12:34:46 +0000486 pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType);
drh75897232000-05-29 14:26:00 +0000487 i += pParse->sLastToken.n;
drh75897232000-05-29 14:26:00 +0000488 switch( tokenType ){
489 case TK_SPACE:
drh75897232000-05-29 14:26:00 +0000490 case TK_COMMENT: {
drh32eb7b42003-01-07 01:44:37 +0000491 if( (db->flags & SQLITE_Interrupt)!=0 ){
492 pParse->rc = SQLITE_INTERRUPT;
danielk19774adee202004-05-08 08:23:19 +0000493 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
drh32eb7b42003-01-07 01:44:37 +0000494 goto abort_parse;
495 }
drh75897232000-05-29 14:26:00 +0000496 break;
497 }
drh32eb7b42003-01-07 01:44:37 +0000498 case TK_ILLEGAL: {
drhae29ffb2004-09-25 14:39:18 +0000499 if( pzErrMsg ){
500 sqliteFree(*pzErrMsg);
501 *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
502 &pParse->sLastToken);
503 }
drh75897232000-05-29 14:26:00 +0000504 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000505 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000506 }
drh326dce72003-01-29 14:06:07 +0000507 case TK_SEMI: {
508 pParse->zTail = &zSql[i];
509 /* Fall thru into the default case */
510 }
drh32eb7b42003-01-07 01:44:37 +0000511 default: {
danielk19774adee202004-05-08 08:23:19 +0000512 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000513 lastTokenParsed = tokenType;
514 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000515 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000516 }
517 break;
drh32eb7b42003-01-07 01:44:37 +0000518 }
drh75897232000-05-29 14:26:00 +0000519 }
520 }
drh32eb7b42003-01-07 01:44:37 +0000521abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000522 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
523 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000524 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000525 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000526 }
danielk19774adee202004-05-08 08:23:19 +0000527 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000528 }
danielk19774adee202004-05-08 08:23:19 +0000529 sqlite3ParserFree(pEngine, free);
drh71c697e2004-08-08 23:39:19 +0000530 if( sqlite3_malloc_failed ){
531 pParse->rc = SQLITE_NOMEM;
532 }
drhb86ccfb2003-01-28 23:13:10 +0000533 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
danielk1977f20b21c2004-05-31 23:56:42 +0000534 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc),
drh41743982003-12-06 21:43:55 +0000535 (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000536 }
drh75897232000-05-29 14:26:00 +0000537 if( pParse->zErrMsg ){
drhb86ccfb2003-01-28 23:13:10 +0000538 if( pzErrMsg && *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000539 *pzErrMsg = pParse->zErrMsg;
540 }else{
541 sqliteFree(pParse->zErrMsg);
542 }
drhb86ccfb2003-01-28 23:13:10 +0000543 pParse->zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000544 if( !nErr ) nErr++;
545 }
drh826fb5a2004-02-14 23:59:57 +0000546 if( pParse->pVdbe && pParse->nErr>0 ){
danielk19774adee202004-05-08 08:23:19 +0000547 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000548 pParse->pVdbe = 0;
549 }
drhfa6bc002004-09-07 16:19:52 +0000550 sqlite3DeleteTable(pParse->db, pParse->pNewTable);
551 sqlite3DeleteTrigger(pParse->pNewTrigger);
552 sqliteFree(pParse->apVarExpr);
drhb86ccfb2003-01-28 23:13:10 +0000553 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000554 pParse->rc = SQLITE_ERROR;
555 }
drh75897232000-05-29 14:26:00 +0000556 return nErr;
557}
drh7ad43342003-05-04 17:58:25 +0000558
559/*
danielk19776f8a5032004-05-10 10:34:51 +0000560** Token types used by the sqlite3_complete() routine. See the header
drh7ad43342003-05-04 17:58:25 +0000561** comments on that procedure for additional information.
562*/
563#define tkEXPLAIN 0
564#define tkCREATE 1
565#define tkTEMP 2
566#define tkTRIGGER 3
567#define tkEND 4
568#define tkSEMI 5
569#define tkWS 6
570#define tkOTHER 7
571
572/*
573** Return TRUE if the given SQL string ends in a semicolon.
574**
575** Special handling is require for CREATE TRIGGER statements.
576** Whenever the CREATE TRIGGER keywords are seen, the statement
577** must end with ";END;".
578**
579** This implementation uses a state machine with 7 states:
580**
581** (0) START At the beginning or end of an SQL statement. This routine
582** returns 1 if it ends in the START state and 0 if it ends
583** in any other state.
584**
585** (1) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
586** a statement.
587**
588** (2) CREATE The keyword CREATE has been seen at the beginning of a
589** statement, possibly preceeded by EXPLAIN and/or followed by
590** TEMP or TEMPORARY
591**
592** (3) NORMAL We are in the middle of statement which ends with a single
593** semicolon.
594**
595** (4) TRIGGER We are in the middle of a trigger definition that must be
596** ended by a semicolon, the keyword END, and another semicolon.
597**
598** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
599** the end of a trigger definition.
600**
601** (6) END We've seen the ";END" of the ";END;" that occurs at the end
602** of a trigger difinition.
603**
604** Transitions between states above are determined by tokens extracted
605** from the input. The following tokens are significant:
606**
607** (0) tkEXPLAIN The "explain" keyword.
608** (1) tkCREATE The "create" keyword.
609** (2) tkTEMP The "temp" or "temporary" keyword.
610** (3) tkTRIGGER The "trigger" keyword.
611** (4) tkEND The "end" keyword.
612** (5) tkSEMI A semicolon.
613** (6) tkWS Whitespace
614** (7) tkOTHER Any other SQL token.
615**
616** Whitespace never causes a state transition and is always ignored.
617*/
danielk19776f8a5032004-05-10 10:34:51 +0000618int sqlite3_complete(const char *zSql){
drh7ad43342003-05-04 17:58:25 +0000619 u8 state = 0; /* Current state, using numbers defined in header comment */
620 u8 token; /* Value of the next token */
621
622 /* The following matrix defines the transition from one state to another
623 ** according to what token is seen. trans[state][token] returns the
624 ** next state.
625 */
626 static const u8 trans[7][8] = {
drhe1e38c42003-05-04 18:30:59 +0000627 /* Token: */
drh7ad43342003-05-04 17:58:25 +0000628 /* State: ** EXPLAIN CREATE TEMP TRIGGER END SEMI WS OTHER */
629 /* 0 START: */ { 1, 2, 3, 3, 3, 0, 0, 3, },
630 /* 1 EXPLAIN: */ { 3, 2, 3, 3, 3, 0, 1, 3, },
631 /* 2 CREATE: */ { 3, 3, 2, 4, 3, 0, 2, 3, },
632 /* 3 NORMAL: */ { 3, 3, 3, 3, 3, 0, 3, 3, },
633 /* 4 TRIGGER: */ { 4, 4, 4, 4, 4, 5, 4, 4, },
634 /* 5 SEMI: */ { 4, 4, 4, 4, 6, 5, 5, 4, },
635 /* 6 END: */ { 4, 4, 4, 4, 4, 0, 6, 4, },
636 };
637
638 while( *zSql ){
639 switch( *zSql ){
640 case ';': { /* A semicolon */
641 token = tkSEMI;
642 break;
643 }
644 case ' ':
645 case '\r':
646 case '\t':
647 case '\n':
648 case '\f': { /* White space is ignored */
649 token = tkWS;
650 break;
651 }
652 case '/': { /* C-style comments */
653 if( zSql[1]!='*' ){
654 token = tkOTHER;
655 break;
656 }
657 zSql += 2;
658 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
659 if( zSql[0]==0 ) return 0;
660 zSql++;
661 token = tkWS;
662 break;
663 }
664 case '-': { /* SQL-style comments from "--" to end of line */
665 if( zSql[1]!='-' ){
666 token = tkOTHER;
667 break;
668 }
669 while( *zSql && *zSql!='\n' ){ zSql++; }
670 if( *zSql==0 ) return state==0;
671 token = tkWS;
672 break;
673 }
674 case '[': { /* Microsoft-style identifiers in [...] */
675 zSql++;
676 while( *zSql && *zSql!=']' ){ zSql++; }
677 if( *zSql==0 ) return 0;
678 token = tkOTHER;
679 break;
680 }
681 case '"': /* single- and double-quoted strings */
682 case '\'': {
683 int c = *zSql;
684 zSql++;
685 while( *zSql && *zSql!=c ){ zSql++; }
686 if( *zSql==0 ) return 0;
687 token = tkOTHER;
688 break;
689 }
690 default: {
drhaa756b02004-09-25 15:25:26 +0000691 int c;
692 if( IdChar((u8)*zSql) ){
drh7ad43342003-05-04 17:58:25 +0000693 /* Keywords and unquoted identifiers */
694 int nId;
drhaa756b02004-09-25 15:25:26 +0000695 for(nId=1; IdChar(zSql[nId]); nId++){}
drh7ad43342003-05-04 17:58:25 +0000696 switch( *zSql ){
697 case 'c': case 'C': {
danielk19774adee202004-05-08 08:23:19 +0000698 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
drh7ad43342003-05-04 17:58:25 +0000699 token = tkCREATE;
700 }else{
701 token = tkOTHER;
702 }
703 break;
704 }
705 case 't': case 'T': {
danielk19774adee202004-05-08 08:23:19 +0000706 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
drh7ad43342003-05-04 17:58:25 +0000707 token = tkTRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000708 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
drh7ad43342003-05-04 17:58:25 +0000709 token = tkTEMP;
danielk19774adee202004-05-08 08:23:19 +0000710 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
drh7ad43342003-05-04 17:58:25 +0000711 token = tkTEMP;
712 }else{
713 token = tkOTHER;
714 }
715 break;
716 }
717 case 'e': case 'E': {
danielk19774adee202004-05-08 08:23:19 +0000718 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
drh7ad43342003-05-04 17:58:25 +0000719 token = tkEND;
danielk19774adee202004-05-08 08:23:19 +0000720 }else if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
drh7ad43342003-05-04 17:58:25 +0000721 token = tkEXPLAIN;
722 }else{
723 token = tkOTHER;
724 }
725 break;
726 }
727 default: {
728 token = tkOTHER;
729 break;
730 }
731 }
732 zSql += nId-1;
733 }else{
734 /* Operators and special symbols */
735 token = tkOTHER;
736 }
737 break;
738 }
739 }
740 state = trans[state][token];
741 zSql++;
742 }
743 return state==0;
744}
danielk197761de0d12004-05-27 23:56:16 +0000745
746/*
747** This routine is the same as the sqlite3_complete() routine described
748** above, except that the parameter is required to be UTF-16 encoded, not
749** UTF-8.
750*/
751int sqlite3_complete16(const void *zSql){
danielk1977bfd6cce2004-06-18 04:24:54 +0000752 sqlite3_value *pVal;
danielk19775314c4d2004-06-18 06:02:35 +0000753 char const *zSql8;
danielk1977bfd6cce2004-06-18 04:24:54 +0000754 int rc = 0;
755
756 pVal = sqlite3ValueNew();
757 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
758 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
759 if( zSql8 ){
760 rc = sqlite3_complete(zSql8);
danielk1977bfd6cce2004-06-18 04:24:54 +0000761 }
762 sqlite3ValueFree(pVal);
danielk197761de0d12004-05-27 23:56:16 +0000763 return rc;
764}