blob: 3fdb38edebe74ec45be931cfae2530a7e57666ec [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**
drh0b9f50d2009-06-23 20:28:53 +000018** $Id: tokenize.c,v 1.162 2009/06/23 20:28:54 drh Exp $
drh75897232000-05-29 14:26:00 +000019*/
20#include "sqliteInt.h"
drhdcc581c2000-05-30 13:44:19 +000021#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000022
23/*
drh9b8f4472006-04-04 01:54:55 +000024** The charMap() macro maps alphabetic characters into their
25** lower-case ASCII equivalent. On ASCII machines, this is just
26** an upper-to-lower case map. On EBCDIC machines we also need
27** to adjust the encoding. Only alphabetic characters and underscores
28** need to be translated.
29*/
30#ifdef SQLITE_ASCII
31# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
32#endif
33#ifdef SQLITE_EBCDIC
34# define charMap(X) ebcdicToAscii[(unsigned char)X]
35const unsigned char ebcdicToAscii[] = {
36/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
45 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
46 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
47 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
49 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
50 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
51 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
53};
54#endif
55
56/*
drh52fb6d72004-11-03 03:59:57 +000057** The sqlite3KeywordCode function looks up an identifier to determine if
58** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +000059** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +000060**
61** The implementation of this routine was generated by a program,
drh73b211a2005-01-18 04:00:42 +000062** mkkeywordhash.h, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +000063** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +000064** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +000065** the #include below.
drh75897232000-05-29 14:26:00 +000066*/
drh73b211a2005-01-18 04:00:42 +000067#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +000068
drh40f20f72004-10-23 05:10:18 +000069
drh98808ba2001-10-18 12:34:46 +000070/*
drh9b8f4472006-04-04 01:54:55 +000071** If X is a character that can be used in an identifier then
72** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +000073**
drh9b8f4472006-04-04 01:54:55 +000074** For ASCII, any character with the high-order bit set is
75** allowed in an identifier. For 7-bit characters,
76** sqlite3IsIdChar[X] must be 1.
77**
78** For EBCDIC, the rules are more complex but have the same
79** end result.
drha0d1f662005-01-11 17:59:47 +000080**
81** Ticket #1066. the SQL standard does not allow '$' in the
82** middle of identfiers. But many SQL implementations do.
83** SQLite will allow '$' in identifiers for compatibility.
84** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +000085*/
drh9b8f4472006-04-04 01:54:55 +000086#ifdef SQLITE_ASCII
drh46c99e02007-08-27 23:26:59 +000087const char sqlite3IsAsciiIdChar[] = {
drh98808ba2001-10-18 12:34:46 +000088/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
drha0d1f662005-01-11 17:59:47 +000089 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
drh98808ba2001-10-18 12:34:46 +000090 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
91 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
92 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
93 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
drh98808ba2001-10-18 12:34:46 +000095};
drh46c99e02007-08-27 23:26:59 +000096#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
drh9b8f4472006-04-04 01:54:55 +000097#endif
98#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59 +000099const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55 +0000100/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
101 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
102 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
103 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
104 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
105 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
106 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
107 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
109 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
110 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
111 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
113};
drh46c99e02007-08-27 23:26:59 +0000114#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55 +0000115#endif
116
drh98808ba2001-10-18 12:34:46 +0000117
drh75897232000-05-29 14:26:00 +0000118/*
drh61b487d2003-09-12 02:08:14 +0000119** Return the length of the token that begins at z[0].
120** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000121*/
drh35b5a332008-04-05 18:41:42 +0000122int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000123 int i, c;
drh75897232000-05-29 14:26:00 +0000124 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000125 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh19f81f62009-06-09 18:01:37 +0000126 testcase( z[0]==' ' );
127 testcase( z[0]=='\t' );
128 testcase( z[0]=='\n' );
129 testcase( z[0]=='\f' );
130 testcase( z[0]=='\r' );
danielk197778ca0e72009-01-20 16:53:39 +0000131 for(i=1; sqlite3Isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000132 *tokenType = TK_SPACE;
133 return i;
134 }
135 case '-': {
drh75897232000-05-29 14:26:00 +0000136 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000137 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drh200a81d2008-08-08 14:19:41 +0000138 *tokenType = TK_SPACE;
drh75897232000-05-29 14:26:00 +0000139 return i;
140 }
141 *tokenType = TK_MINUS;
142 return 1;
143 }
144 case '(': {
drhdab35182003-09-27 13:39:38 +0000145 *tokenType = TK_LP;
146 return 1;
drh75897232000-05-29 14:26:00 +0000147 }
148 case ')': {
149 *tokenType = TK_RP;
150 return 1;
151 }
152 case ';': {
153 *tokenType = TK_SEMI;
154 return 1;
155 }
156 case '+': {
157 *tokenType = TK_PLUS;
158 return 1;
159 }
160 case '*': {
161 *tokenType = TK_STAR;
162 return 1;
163 }
164 case '/': {
drh66105a82002-08-27 14:28:29 +0000165 if( z[1]!='*' || z[2]==0 ){
166 *tokenType = TK_SLASH;
167 return 1;
168 }
drhaa756b02004-09-25 15:25:26 +0000169 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
170 if( c ) i++;
drh200a81d2008-08-08 14:19:41 +0000171 *tokenType = TK_SPACE;
drh66105a82002-08-27 14:28:29 +0000172 return i;
drh75897232000-05-29 14:26:00 +0000173 }
drhbf4133c2001-10-13 02:59:08 +0000174 case '%': {
175 *tokenType = TK_REM;
176 return 1;
177 }
drh75897232000-05-29 14:26:00 +0000178 case '=': {
179 *tokenType = TK_EQ;
180 return 1 + (z[1]=='=');
181 }
182 case '<': {
drhaa756b02004-09-25 15:25:26 +0000183 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000184 *tokenType = TK_LE;
185 return 2;
drhaa756b02004-09-25 15:25:26 +0000186 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000187 *tokenType = TK_NE;
188 return 2;
drhaa756b02004-09-25 15:25:26 +0000189 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000190 *tokenType = TK_LSHIFT;
191 return 2;
drh75897232000-05-29 14:26:00 +0000192 }else{
193 *tokenType = TK_LT;
194 return 1;
195 }
196 }
197 case '>': {
drhaa756b02004-09-25 15:25:26 +0000198 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000199 *tokenType = TK_GE;
200 return 2;
drhaa756b02004-09-25 15:25:26 +0000201 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000202 *tokenType = TK_RSHIFT;
203 return 2;
drh75897232000-05-29 14:26:00 +0000204 }else{
205 *tokenType = TK_GT;
206 return 1;
207 }
208 }
209 case '!': {
210 if( z[1]!='=' ){
211 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000212 return 2;
drh75897232000-05-29 14:26:00 +0000213 }else{
214 *tokenType = TK_NE;
215 return 2;
216 }
217 }
drh00400772000-06-16 20:51:26 +0000218 case '|': {
219 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000220 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000221 return 1;
222 }else{
223 *tokenType = TK_CONCAT;
224 return 2;
225 }
226 }
drh75897232000-05-29 14:26:00 +0000227 case ',': {
228 *tokenType = TK_COMMA;
229 return 1;
230 }
drhbf4133c2001-10-13 02:59:08 +0000231 case '&': {
232 *tokenType = TK_BITAND;
233 return 1;
234 }
235 case '~': {
236 *tokenType = TK_BITNOT;
237 return 1;
238 }
drh3d946622005-08-13 18:15:42 +0000239 case '`':
240 case '\'':
241 case '"': {
drh75897232000-05-29 14:26:00 +0000242 int delim = z[0];
drh19f81f62009-06-09 18:01:37 +0000243 testcase( delim=='`' );
244 testcase( delim=='\'' );
245 testcase( delim=='"' );
drhaa756b02004-09-25 15:25:26 +0000246 for(i=1; (c=z[i])!=0; i++){
247 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000248 if( z[i+1]==delim ){
249 i++;
250 }else{
251 break;
252 }
253 }
254 }
drha33cb5f2008-08-07 13:05:34 +0000255 if( c=='\'' ){
drheef8b552005-10-23 11:29:40 +0000256 *tokenType = TK_STRING;
257 return i+1;
drha33cb5f2008-08-07 13:05:34 +0000258 }else if( c!=0 ){
259 *tokenType = TK_ID;
260 return i+1;
drheef8b552005-10-23 11:29:40 +0000261 }else{
262 *tokenType = TK_ILLEGAL;
263 return i;
264 }
drh75897232000-05-29 14:26:00 +0000265 }
266 case '.': {
drh76816182005-08-23 11:31:26 +0000267#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197778ca0e72009-01-20 16:53:39 +0000268 if( !sqlite3Isdigit(z[1]) )
drh76816182005-08-23 11:31:26 +0000269#endif
270 {
271 *tokenType = TK_DOT;
272 return 1;
273 }
274 /* If the next character is a digit, this is a floating point
275 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000276 }
277 case '0': case '1': case '2': case '3': case '4':
278 case '5': case '6': case '7': case '8': case '9': {
drh19f81f62009-06-09 18:01:37 +0000279 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
280 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
281 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
282 testcase( z[0]=='9' );
drhc837e702000-06-08 16:26:24 +0000283 *tokenType = TK_INTEGER;
danielk197778ca0e72009-01-20 16:53:39 +0000284 for(i=0; sqlite3Isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000285#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000286 if( z[i]=='.' ){
287 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000288 while( sqlite3Isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000289 *tokenType = TK_FLOAT;
290 }
291 if( (z[i]=='e' || z[i]=='E') &&
danielk197778ca0e72009-01-20 16:53:39 +0000292 ( sqlite3Isdigit(z[i+1])
293 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
drh75897232000-05-29 14:26:00 +0000294 )
drhc837e702000-06-08 16:26:24 +0000295 ){
296 i += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000297 while( sqlite3Isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000298 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000299 }
drhb7f91642004-10-31 02:22:47 +0000300#endif
drh67dd9012006-08-12 12:33:14 +0000301 while( IdChar(z[i]) ){
302 *tokenType = TK_ILLEGAL;
303 i++;
304 }
drh75897232000-05-29 14:26:00 +0000305 return i;
306 }
drh2f4392f2002-02-14 21:42:51 +0000307 case '[': {
drhaa756b02004-09-25 15:25:26 +0000308 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09 +0000309 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51 +0000310 return i;
311 }
drh7c972de2003-09-06 22:18:07 +0000312 case '?': {
drh50457892003-09-06 01:10:47 +0000313 *tokenType = TK_VARIABLE;
danielk197778ca0e72009-01-20 16:53:39 +0000314 for(i=1; sqlite3Isdigit(z[i]); i++){}
drhfa6bc002004-09-07 16:19:52 +0000315 return i;
drh50457892003-09-06 01:10:47 +0000316 }
drh288d37f2005-06-22 08:48:06 +0000317 case '#': {
danielk197778ca0e72009-01-20 16:53:39 +0000318 for(i=1; sqlite3Isdigit(z[i]); i++){}
drh288d37f2005-06-22 08:48:06 +0000319 if( i>1 ){
320 /* Parameters of the form #NNN (where NNN is a number) are used
321 ** internally by sqlite3NestedParse. */
322 *tokenType = TK_REGISTER;
323 return i;
324 }
325 /* Fall through into the next case if the '#' is not followed by
326 ** a digit. Try to match #AAAA where AAAA is a parameter name. */
drh895d7472004-08-20 16:02:39 +0000327 }
drhb7f91642004-10-31 02:22:47 +0000328#ifndef SQLITE_OMIT_TCL_VARIABLE
drh288d37f2005-06-22 08:48:06 +0000329 case '$':
330#endif
drh0b2a5ee2006-02-09 22:24:41 +0000331 case '@': /* For compatibility with MS SQL Server */
drh288d37f2005-06-22 08:48:06 +0000332 case ':': {
333 int n = 0;
drh8be4f632009-06-12 11:42:11 +0000334 testcase( z[0]=='$' ); testcase( z[0]=='@' ); testcase( z[0]==':' );
drh9d74b4c2004-08-24 15:23:34 +0000335 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000336 for(i=1; (c=z[i])!=0; i++){
337 if( IdChar(c) ){
338 n++;
339#ifndef SQLITE_OMIT_TCL_VARIABLE
340 }else if( c=='(' && n>0 ){
341 do{
342 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000343 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
drh288d37f2005-06-22 08:48:06 +0000344 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000345 i++;
346 }else{
drh288d37f2005-06-22 08:48:06 +0000347 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000348 }
drh288d37f2005-06-22 08:48:06 +0000349 break;
350 }else if( c==':' && z[i+1]==':' ){
351 i++;
352#endif
353 }else{
354 break;
drh895d7472004-08-20 16:02:39 +0000355 }
356 }
drh288d37f2005-06-22 08:48:06 +0000357 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000358 return i;
drhb7f91642004-10-31 02:22:47 +0000359 }
drhb7f91642004-10-31 02:22:47 +0000360#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000361 case 'x': case 'X': {
drh19f81f62009-06-09 18:01:37 +0000362 testcase( z[0]=='x' ); testcase( z[0]=='X' );
drh4b2f9362008-01-22 23:37:09 +0000363 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19 +0000364 *tokenType = TK_BLOB;
drh4b2f9362008-01-22 23:37:09 +0000365 for(i=2; (c=z[i])!=0 && c!='\''; i++){
danielk197778ca0e72009-01-20 16:53:39 +0000366 if( !sqlite3Isxdigit(c) ){
danielk19773fd0a732004-05-27 13:35:19 +0000367 *tokenType = TK_ILLEGAL;
danielk19773fd0a732004-05-27 13:35:19 +0000368 }
danielk1977c572ef72004-05-27 09:28:41 +0000369 }
drh4b2f9362008-01-22 23:37:09 +0000370 if( i%2 || !c ) *tokenType = TK_ILLEGAL;
drhaa756b02004-09-25 15:25:26 +0000371 if( c ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000372 return i;
373 }
374 /* Otherwise fall through to the next case */
375 }
drhb7f91642004-10-31 02:22:47 +0000376#endif
drh98808ba2001-10-18 12:34:46 +0000377 default: {
drh9a087a92007-05-15 14:34:32 +0000378 if( !IdChar(*z) ){
drh98808ba2001-10-18 12:34:46 +0000379 break;
380 }
drhaa756b02004-09-25 15:25:26 +0000381 for(i=1; IdChar(z[i]); i++){}
danielk1977c60e9b82005-01-31 12:42:29 +0000382 *tokenType = keywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000383 return i;
384 }
drh75897232000-05-29 14:26:00 +0000385 }
386 *tokenType = TK_ILLEGAL;
387 return 1;
388}
389
390/*
391** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000392** passed in. An SQLITE_ status code is returned. If an error occurs
drhfb45d8c2008-07-08 00:06:49 +0000393** then an and attempt is made to write an error message into
394** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
395** error message.
drh75897232000-05-29 14:26:00 +0000396*/
danielk19774adee202004-05-08 08:23:19 +0000397int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drhd9da78a2009-03-24 15:08:09 +0000398 int nErr = 0; /* Number of errors encountered */
399 int i; /* Loop counter */
400 void *pEngine; /* The LEMON-generated LALR(1) parser */
401 int tokenType; /* type of the next token */
402 int lastTokenParsed = -1; /* type of the previous token */
shaneb08a67a2009-03-31 03:41:56 +0000403 u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
drhd9da78a2009-03-24 15:08:09 +0000404 sqlite3 *db = pParse->db; /* The database connection */
405 int mxSqlLen; /* Max length of an SQL string */
drh75897232000-05-29 14:26:00 +0000406
drhd9da78a2009-03-24 15:08:09 +0000407
408 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh15ca1df2006-07-26 13:43:30 +0000409 if( db->activeVdbeCnt==0 ){
410 db->u1.isInterrupted = 0;
411 }
drh4c504392000-10-16 22:06:40 +0000412 pParse->rc = SQLITE_OK;
drh4b1d66c2008-04-03 21:42:21 +0000413 pParse->zTail = pParse->zSql = zSql;
drh75897232000-05-29 14:26:00 +0000414 i = 0;
drhfb45d8c2008-07-08 00:06:49 +0000415 assert( pzErrMsg!=0 );
drhe5ae5732008-06-15 02:51:47 +0000416 pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
drh75897232000-05-29 14:26:00 +0000417 if( pEngine==0 ){
drhf3a65f72007-08-22 20:18:21 +0000418 db->mallocFailed = 1;
drhdefc9972005-06-06 14:45:42 +0000419 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000420 }
drhfa6bc002004-09-07 16:19:52 +0000421 assert( pParse->pNewTable==0 );
422 assert( pParse->pNewTrigger==0 );
423 assert( pParse->nVar==0 );
424 assert( pParse->nVarExpr==0 );
425 assert( pParse->nVarExprAlloc==0 );
426 assert( pParse->apVarExpr==0 );
drhd9da78a2009-03-24 15:08:09 +0000427 enableLookaside = db->lookaside.bEnabled;
428 if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
drh17435752007-08-16 04:30:38 +0000429 while( !db->mallocFailed && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000430 assert( i>=0 );
drhb7916a72009-05-27 10:31:29 +0000431 pParse->sLastToken.z = &zSql[i];
drh35b5a332008-04-05 18:41:42 +0000432 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000433 i += pParse->sLastToken.n;
drhbb4957f2008-03-20 14:03:29 +0000434 if( i>mxSqlLen ){
drhe5c941b2007-05-08 13:58:26 +0000435 pParse->rc = SQLITE_TOOBIG;
436 break;
437 }
drh75897232000-05-29 14:26:00 +0000438 switch( tokenType ){
drh200a81d2008-08-08 14:19:41 +0000439 case TK_SPACE: {
drh881feaa2006-07-26 01:39:30 +0000440 if( db->u1.isInterrupted ){
drh19f81f62009-06-09 18:01:37 +0000441 sqlite3ErrorMsg(pParse, "interrupt");
drh32eb7b42003-01-07 01:44:37 +0000442 pParse->rc = SQLITE_INTERRUPT;
drh32eb7b42003-01-07 01:44:37 +0000443 goto abort_parse;
444 }
drh75897232000-05-29 14:26:00 +0000445 break;
446 }
drh32eb7b42003-01-07 01:44:37 +0000447 case TK_ILLEGAL: {
drh633e6d52008-07-28 19:34:53 +0000448 sqlite3DbFree(db, *pzErrMsg);
drhfb45d8c2008-07-08 00:06:49 +0000449 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
450 &pParse->sLastToken);
drh75897232000-05-29 14:26:00 +0000451 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000452 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000453 }
drh326dce72003-01-29 14:06:07 +0000454 case TK_SEMI: {
455 pParse->zTail = &zSql[i];
456 /* Fall thru into the default case */
457 }
drh32eb7b42003-01-07 01:44:37 +0000458 default: {
danielk19774adee202004-05-08 08:23:19 +0000459 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000460 lastTokenParsed = tokenType;
461 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000462 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000463 }
464 break;
drh32eb7b42003-01-07 01:44:37 +0000465 }
drh75897232000-05-29 14:26:00 +0000466 }
467 }
drh32eb7b42003-01-07 01:44:37 +0000468abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000469 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
470 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000471 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000472 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000473 }
danielk19774adee202004-05-08 08:23:19 +0000474 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000475 }
drhec424a52008-07-25 15:39:03 +0000476#ifdef YYTRACKMAXSTACKDEPTH
477 sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
478 sqlite3ParserStackPeak(pEngine)
479 );
480#endif /* YYDEBUG */
drh17435752007-08-16 04:30:38 +0000481 sqlite3ParserFree(pEngine, sqlite3_free);
drhd9da78a2009-03-24 15:08:09 +0000482 db->lookaside.bEnabled = enableLookaside;
drh17435752007-08-16 04:30:38 +0000483 if( db->mallocFailed ){
drh71c697e2004-08-08 23:39:19 +0000484 pParse->rc = SQLITE_NOMEM;
485 }
drhb86ccfb2003-01-28 23:13:10 +0000486 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
drhf089aa42008-07-08 19:34:06 +0000487 sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
drhb86ccfb2003-01-28 23:13:10 +0000488 }
drh19f81f62009-06-09 18:01:37 +0000489 assert( pzErrMsg!=0 );
drh75897232000-05-29 14:26:00 +0000490 if( pParse->zErrMsg ){
drh19f81f62009-06-09 18:01:37 +0000491 *pzErrMsg = pParse->zErrMsg;
drhb86ccfb2003-01-28 23:13:10 +0000492 pParse->zErrMsg = 0;
drh4b2f9362008-01-22 23:37:09 +0000493 nErr++;
drh75897232000-05-29 14:26:00 +0000494 }
drh2958a4e2004-11-12 03:56:15 +0000495 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000496 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000497 pParse->pVdbe = 0;
498 }
danielk1977c00da102006-01-07 13:21:04 +0000499#ifndef SQLITE_OMIT_SHARED_CACHE
500 if( pParse->nested==0 ){
drh633e6d52008-07-28 19:34:53 +0000501 sqlite3DbFree(db, pParse->aTableLock);
danielk1977c00da102006-01-07 13:21:04 +0000502 pParse->aTableLock = 0;
503 pParse->nTableLock = 0;
504 }
505#endif
drh4f3dd152008-04-28 18:46:43 +0000506#ifndef SQLITE_OMIT_VIRTUALTABLE
drh633e6d52008-07-28 19:34:53 +0000507 sqlite3DbFree(db, pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43 +0000508#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000509
danielk19777e6ebfb2006-06-12 11:24:37 +0000510 if( !IN_DECLARE_VTAB ){
511 /* If the pParse->declareVtab flag is set, do not delete any table
512 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
513 ** will take responsibility for freeing the Table structure.
514 */
danielk1977a04a34f2007-04-16 15:06:25 +0000515 sqlite3DeleteTable(pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000516 }
517
drh633e6d52008-07-28 19:34:53 +0000518 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
519 sqlite3DbFree(db, pParse->apVarExpr);
drh8b213892008-08-29 02:14:02 +0000520 sqlite3DbFree(db, pParse->aAlias);
drh0b9f50d2009-06-23 20:28:53 +0000521 while( pParse->pAinc ){
522 AutoincInfo *p = pParse->pAinc;
523 pParse->pAinc = p->pNext;
524 sqlite3DbFree(db, p);
525 }
drh588a9a12008-09-01 15:52:10 +0000526 while( pParse->pZombieTab ){
527 Table *p = pParse->pZombieTab;
528 pParse->pZombieTab = p->pNextZombie;
529 sqlite3DeleteTable(p);
530 }
drhb5092d12009-06-17 01:17:13 +0000531 if( nErr>0 && pParse->rc==SQLITE_OK ){
drh4c504392000-10-16 22:06:40 +0000532 pParse->rc = SQLITE_ERROR;
533 }
drh75897232000-05-29 14:26:00 +0000534 return nErr;
535}