Get read-only SHM file tests passing on Win32.
[sqlite.git] / src / tokenize.c
blob2aab334ae968e9b6a3c6bc450fddb4abe3994b60
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** 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.
11 *************************************************************************
12 ** An tokenizer for SQL
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.
18 #include "sqliteInt.h"
19 #include <stdlib.h>
21 /* Character classes for tokenizing
23 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
24 ** using a lookup table, whereas a switch() directly on c uses a binary search.
25 ** The lookup table is much faster. To maximize speed, and to ensure that
26 ** a lookup table is used, all of the classes need to be small integers and
27 ** all of them need to be used within the switch.
29 #define CC_X 0 /* The letter 'x', or start of BLOB literal */
30 #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
31 #define CC_ID 2 /* unicode characters usable in IDs */
32 #define CC_DIGIT 3 /* Digits */
33 #define CC_DOLLAR 4 /* '$' */
34 #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
35 #define CC_VARNUM 6 /* '?'. Numeric SQL variables */
36 #define CC_SPACE 7 /* Space characters */
37 #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
38 #define CC_QUOTE2 9 /* '['. [...] style quoted ids */
39 #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
40 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
41 #define CC_LT 12 /* '<'. Part of < or <= or <> */
42 #define CC_GT 13 /* '>'. Part of > or >= */
43 #define CC_EQ 14 /* '='. Part of = or == */
44 #define CC_BANG 15 /* '!'. Part of != */
45 #define CC_SLASH 16 /* '/'. / or c-style comment */
46 #define CC_LP 17 /* '(' */
47 #define CC_RP 18 /* ')' */
48 #define CC_SEMI 19 /* ';' */
49 #define CC_PLUS 20 /* '+' */
50 #define CC_STAR 21 /* '*' */
51 #define CC_PERCENT 22 /* '%' */
52 #define CC_COMMA 23 /* ',' */
53 #define CC_AND 24 /* '&' */
54 #define CC_TILDA 25 /* '~' */
55 #define CC_DOT 26 /* '.' */
56 #define CC_ILLEGAL 27 /* Illegal character */
58 static const unsigned char aiClass[] = {
59 #ifdef SQLITE_ASCII
60 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
61 /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
62 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
63 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
64 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
65 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
66 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
67 /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
68 /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
69 /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
70 /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73 /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
74 /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
75 /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
76 /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
77 #endif
78 #ifdef SQLITE_EBCDIC
79 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
80 /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
81 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
82 /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
83 /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
84 /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
85 /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
86 /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
87 /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
88 /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
89 /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
90 /* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
91 /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
92 /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
93 /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
94 /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
95 /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
96 #endif
100 ** The charMap() macro maps alphabetic characters (only) into their
101 ** lower-case ASCII equivalent. On ASCII machines, this is just
102 ** an upper-to-lower case map. On EBCDIC machines we also need
103 ** to adjust the encoding. The mapping is only valid for alphabetics
104 ** which are the only characters for which this feature is used.
106 ** Used by keywordhash.h
108 #ifdef SQLITE_ASCII
109 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
110 #endif
111 #ifdef SQLITE_EBCDIC
112 # define charMap(X) ebcdicToAscii[(unsigned char)X]
113 const unsigned char ebcdicToAscii[] = {
114 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
123 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
124 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
125 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
127 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
128 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
129 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
132 #endif
135 ** The sqlite3KeywordCode function looks up an identifier to determine if
136 ** it is a keyword. If it is a keyword, the token code of that keyword is
137 ** returned. If the input is not a keyword, TK_ID is returned.
139 ** The implementation of this routine was generated by a program,
140 ** mkkeywordhash.c, located in the tool subdirectory of the distribution.
141 ** The output of the mkkeywordhash.c program is written into a file
142 ** named keywordhash.h and then included into this source file by
143 ** the #include below.
145 #include "keywordhash.h"
149 ** If X is a character that can be used in an identifier then
150 ** IdChar(X) will be true. Otherwise it is false.
152 ** For ASCII, any character with the high-order bit set is
153 ** allowed in an identifier. For 7-bit characters,
154 ** sqlite3IsIdChar[X] must be 1.
156 ** For EBCDIC, the rules are more complex but have the same
157 ** end result.
159 ** Ticket #1066. the SQL standard does not allow '$' in the
160 ** middle of identifiers. But many SQL implementations do.
161 ** SQLite will allow '$' in identifiers for compatibility.
162 ** But the feature is undocumented.
164 #ifdef SQLITE_ASCII
165 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
166 #endif
167 #ifdef SQLITE_EBCDIC
168 const char sqlite3IsEbcdicIdChar[] = {
169 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
170 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
171 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
172 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
173 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
174 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
175 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
176 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
178 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
179 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
180 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
181 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
183 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
184 #endif
186 /* Make the IdChar function accessible from ctime.c */
187 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
188 int sqlite3IsIdChar(u8 c){ return IdChar(c); }
189 #endif
193 ** Return the length (in bytes) of the token that begins at z[0].
194 ** Store the token type in *tokenType before returning.
196 int sqlite3GetToken(const unsigned char *z, int *tokenType){
197 int i, c;
198 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
199 ** of the token. See the comment on the CC_ defines
200 ** above. */
201 case CC_SPACE: {
202 testcase( z[0]==' ' );
203 testcase( z[0]=='\t' );
204 testcase( z[0]=='\n' );
205 testcase( z[0]=='\f' );
206 testcase( z[0]=='\r' );
207 for(i=1; sqlite3Isspace(z[i]); i++){}
208 *tokenType = TK_SPACE;
209 return i;
211 case CC_MINUS: {
212 if( z[1]=='-' ){
213 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
214 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
215 return i;
217 *tokenType = TK_MINUS;
218 return 1;
220 case CC_LP: {
221 *tokenType = TK_LP;
222 return 1;
224 case CC_RP: {
225 *tokenType = TK_RP;
226 return 1;
228 case CC_SEMI: {
229 *tokenType = TK_SEMI;
230 return 1;
232 case CC_PLUS: {
233 *tokenType = TK_PLUS;
234 return 1;
236 case CC_STAR: {
237 *tokenType = TK_STAR;
238 return 1;
240 case CC_SLASH: {
241 if( z[1]!='*' || z[2]==0 ){
242 *tokenType = TK_SLASH;
243 return 1;
245 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
246 if( c ) i++;
247 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
248 return i;
250 case CC_PERCENT: {
251 *tokenType = TK_REM;
252 return 1;
254 case CC_EQ: {
255 *tokenType = TK_EQ;
256 return 1 + (z[1]=='=');
258 case CC_LT: {
259 if( (c=z[1])=='=' ){
260 *tokenType = TK_LE;
261 return 2;
262 }else if( c=='>' ){
263 *tokenType = TK_NE;
264 return 2;
265 }else if( c=='<' ){
266 *tokenType = TK_LSHIFT;
267 return 2;
268 }else{
269 *tokenType = TK_LT;
270 return 1;
273 case CC_GT: {
274 if( (c=z[1])=='=' ){
275 *tokenType = TK_GE;
276 return 2;
277 }else if( c=='>' ){
278 *tokenType = TK_RSHIFT;
279 return 2;
280 }else{
281 *tokenType = TK_GT;
282 return 1;
285 case CC_BANG: {
286 if( z[1]!='=' ){
287 *tokenType = TK_ILLEGAL;
288 return 1;
289 }else{
290 *tokenType = TK_NE;
291 return 2;
294 case CC_PIPE: {
295 if( z[1]!='|' ){
296 *tokenType = TK_BITOR;
297 return 1;
298 }else{
299 *tokenType = TK_CONCAT;
300 return 2;
303 case CC_COMMA: {
304 *tokenType = TK_COMMA;
305 return 1;
307 case CC_AND: {
308 *tokenType = TK_BITAND;
309 return 1;
311 case CC_TILDA: {
312 *tokenType = TK_BITNOT;
313 return 1;
315 case CC_QUOTE: {
316 int delim = z[0];
317 testcase( delim=='`' );
318 testcase( delim=='\'' );
319 testcase( delim=='"' );
320 for(i=1; (c=z[i])!=0; i++){
321 if( c==delim ){
322 if( z[i+1]==delim ){
323 i++;
324 }else{
325 break;
329 if( c=='\'' ){
330 *tokenType = TK_STRING;
331 return i+1;
332 }else if( c!=0 ){
333 *tokenType = TK_ID;
334 return i+1;
335 }else{
336 *tokenType = TK_ILLEGAL;
337 return i;
340 case CC_DOT: {
341 #ifndef SQLITE_OMIT_FLOATING_POINT
342 if( !sqlite3Isdigit(z[1]) )
343 #endif
345 *tokenType = TK_DOT;
346 return 1;
348 /* If the next character is a digit, this is a floating point
349 ** number that begins with ".". Fall thru into the next case */
351 case CC_DIGIT: {
352 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
353 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
354 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
355 testcase( z[0]=='9' );
356 *tokenType = TK_INTEGER;
357 #ifndef SQLITE_OMIT_HEX_INTEGER
358 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
359 for(i=3; sqlite3Isxdigit(z[i]); i++){}
360 return i;
362 #endif
363 for(i=0; sqlite3Isdigit(z[i]); i++){}
364 #ifndef SQLITE_OMIT_FLOATING_POINT
365 if( z[i]=='.' ){
366 i++;
367 while( sqlite3Isdigit(z[i]) ){ i++; }
368 *tokenType = TK_FLOAT;
370 if( (z[i]=='e' || z[i]=='E') &&
371 ( sqlite3Isdigit(z[i+1])
372 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
375 i += 2;
376 while( sqlite3Isdigit(z[i]) ){ i++; }
377 *tokenType = TK_FLOAT;
379 #endif
380 while( IdChar(z[i]) ){
381 *tokenType = TK_ILLEGAL;
382 i++;
384 return i;
386 case CC_QUOTE2: {
387 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
388 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
389 return i;
391 case CC_VARNUM: {
392 *tokenType = TK_VARIABLE;
393 for(i=1; sqlite3Isdigit(z[i]); i++){}
394 return i;
396 case CC_DOLLAR:
397 case CC_VARALPHA: {
398 int n = 0;
399 testcase( z[0]=='$' ); testcase( z[0]=='@' );
400 testcase( z[0]==':' ); testcase( z[0]=='#' );
401 *tokenType = TK_VARIABLE;
402 for(i=1; (c=z[i])!=0; i++){
403 if( IdChar(c) ){
404 n++;
405 #ifndef SQLITE_OMIT_TCL_VARIABLE
406 }else if( c=='(' && n>0 ){
408 i++;
409 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
410 if( c==')' ){
411 i++;
412 }else{
413 *tokenType = TK_ILLEGAL;
415 break;
416 }else if( c==':' && z[i+1]==':' ){
417 i++;
418 #endif
419 }else{
420 break;
423 if( n==0 ) *tokenType = TK_ILLEGAL;
424 return i;
426 case CC_KYWD: {
427 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
428 if( IdChar(z[i]) ){
429 /* This token started out using characters that can appear in keywords,
430 ** but z[i] is a character not allowed within keywords, so this must
431 ** be an identifier instead */
432 i++;
433 break;
435 *tokenType = TK_ID;
436 return keywordCode((char*)z, i, tokenType);
438 case CC_X: {
439 #ifndef SQLITE_OMIT_BLOB_LITERAL
440 testcase( z[0]=='x' ); testcase( z[0]=='X' );
441 if( z[1]=='\'' ){
442 *tokenType = TK_BLOB;
443 for(i=2; sqlite3Isxdigit(z[i]); i++){}
444 if( z[i]!='\'' || i%2 ){
445 *tokenType = TK_ILLEGAL;
446 while( z[i] && z[i]!='\'' ){ i++; }
448 if( z[i] ) i++;
449 return i;
451 #endif
452 /* If it is not a BLOB literal, then it must be an ID, since no
453 ** SQL keywords start with the letter 'x'. Fall through */
455 case CC_ID: {
456 i = 1;
457 break;
459 default: {
460 *tokenType = TK_ILLEGAL;
461 return 1;
464 while( IdChar(z[i]) ){ i++; }
465 *tokenType = TK_ID;
466 return i;
470 ** Run the parser on the given SQL string. The parser structure is
471 ** passed in. An SQLITE_ status code is returned. If an error occurs
472 ** then an and attempt is made to write an error message into
473 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
474 ** error message.
476 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
477 int nErr = 0; /* Number of errors encountered */
478 void *pEngine; /* The LEMON-generated LALR(1) parser */
479 int n = 0; /* Length of the next token token */
480 int tokenType; /* type of the next token */
481 int lastTokenParsed = -1; /* type of the previous token */
482 sqlite3 *db = pParse->db; /* The database connection */
483 int mxSqlLen; /* Max length of an SQL string */
484 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
485 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
486 #endif
488 assert( zSql!=0 );
489 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
490 if( db->nVdbeActive==0 ){
491 db->u1.isInterrupted = 0;
493 pParse->rc = SQLITE_OK;
494 pParse->zTail = zSql;
495 assert( pzErrMsg!=0 );
496 /* sqlite3ParserTrace(stdout, "parser: "); */
497 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
498 pEngine = &sEngine;
499 sqlite3ParserInit(pEngine);
500 #else
501 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
502 if( pEngine==0 ){
503 sqlite3OomFault(db);
504 return SQLITE_NOMEM_BKPT;
506 #endif
507 assert( pParse->pNewTable==0 );
508 assert( pParse->pNewTrigger==0 );
509 assert( pParse->nVar==0 );
510 assert( pParse->pVList==0 );
511 while( 1 ){
512 if( zSql[0]!=0 ){
513 n = sqlite3GetToken((u8*)zSql, &tokenType);
514 mxSqlLen -= n;
515 if( mxSqlLen<0 ){
516 pParse->rc = SQLITE_TOOBIG;
517 break;
519 }else{
520 /* Upon reaching the end of input, call the parser two more times
521 ** with tokens TK_SEMI and 0, in that order. */
522 if( lastTokenParsed==TK_SEMI ){
523 tokenType = 0;
524 }else if( lastTokenParsed==0 ){
525 break;
526 }else{
527 tokenType = TK_SEMI;
529 zSql -= n;
531 if( tokenType>=TK_SPACE ){
532 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
533 if( db->u1.isInterrupted ){
534 pParse->rc = SQLITE_INTERRUPT;
535 break;
537 if( tokenType==TK_ILLEGAL ){
538 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
539 break;
541 zSql += n;
542 }else{
543 pParse->sLastToken.z = zSql;
544 pParse->sLastToken.n = n;
545 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
546 lastTokenParsed = tokenType;
547 zSql += n;
548 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
551 assert( nErr==0 );
552 pParse->zTail = zSql;
553 #ifdef YYTRACKMAXSTACKDEPTH
554 sqlite3_mutex_enter(sqlite3MallocMutex());
555 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
556 sqlite3ParserStackPeak(pEngine)
558 sqlite3_mutex_leave(sqlite3MallocMutex());
559 #endif /* YYDEBUG */
560 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
561 sqlite3ParserFinalize(pEngine);
562 #else
563 sqlite3ParserFree(pEngine, sqlite3_free);
564 #endif
565 if( db->mallocFailed ){
566 pParse->rc = SQLITE_NOMEM_BKPT;
568 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
569 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
571 assert( pzErrMsg!=0 );
572 if( pParse->zErrMsg ){
573 *pzErrMsg = pParse->zErrMsg;
574 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
575 pParse->zErrMsg = 0;
576 nErr++;
578 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
579 sqlite3VdbeDelete(pParse->pVdbe);
580 pParse->pVdbe = 0;
582 #ifndef SQLITE_OMIT_SHARED_CACHE
583 if( pParse->nested==0 ){
584 sqlite3DbFree(db, pParse->aTableLock);
585 pParse->aTableLock = 0;
586 pParse->nTableLock = 0;
588 #endif
589 #ifndef SQLITE_OMIT_VIRTUALTABLE
590 sqlite3_free(pParse->apVtabLock);
591 #endif
593 if( !IN_DECLARE_VTAB ){
594 /* If the pParse->declareVtab flag is set, do not delete any table
595 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
596 ** will take responsibility for freeing the Table structure.
598 sqlite3DeleteTable(db, pParse->pNewTable);
601 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
602 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
603 sqlite3DbFree(db, pParse->pVList);
604 while( pParse->pAinc ){
605 AutoincInfo *p = pParse->pAinc;
606 pParse->pAinc = p->pNext;
607 sqlite3DbFreeNN(db, p);
609 while( pParse->pZombieTab ){
610 Table *p = pParse->pZombieTab;
611 pParse->pZombieTab = p->pNextZombie;
612 sqlite3DeleteTable(db, p);
614 assert( nErr==0 || pParse->rc!=SQLITE_OK );
615 return nErr;