blob: c4b36c475831dd605c0c98e0aa0c98d79a718801 [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.
drh75897232000-05-29 14:26:00 +000017*/
18#include "sqliteInt.h"
drhdcc581c2000-05-30 13:44:19 +000019#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000020
drh89743312016-02-08 02:30:50 +000021/* Character classes for tokenizing */
22#define CC_X 0 /* The letter 'x' or 'X'. Start of x'01234fed' */
23#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
24#define CC_ID 2 /* unicode characters usable in IDs */
25#define CC_DIGIT 3 /* Digits */
26#define CC_DOLLAR 4 /* '$' */
27#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
28#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
29#define CC_SPACE 7 /* Space characters */
30#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
31#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
32#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
33#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
34#define CC_LT 12 /* '<'. Part of < or <= or <> */
35#define CC_GT 13 /* '>'. Part of > or >= */
36#define CC_EQ 14 /* '='. Part of = or == */
37#define CC_BANG 15 /* '!'. Part of != */
38#define CC_SLASH 16 /* '/'. / or c-style comment */
39#define CC_LP 17 /* '(' */
40#define CC_RP 18 /* ')' */
41#define CC_SEMI 19 /* ';' */
42#define CC_PLUS 20 /* '+' */
43#define CC_STAR 21 /* '*' */
44#define CC_PERCENT 22 /* '%' */
45#define CC_COMMA 23 /* ',' */
46#define CC_AND 24 /* '&' */
47#define CC_TILDA 25 /* '~' */
48#define CC_DOT 26 /* '.' */
49#define CC_ILLEGAL 27 /* Illegal character */
50
51static const unsigned char aiClass[] = {
52/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
53/* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
54/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
55/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
56/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
57/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
58/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
59/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
61/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
62/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
63/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
64/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
65/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
66/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
67/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
68/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
69};
70
drh75897232000-05-29 14:26:00 +000071/*
drh9b8f4472006-04-04 01:54:55 +000072** The charMap() macro maps alphabetic characters into their
73** lower-case ASCII equivalent. On ASCII machines, this is just
74** an upper-to-lower case map. On EBCDIC machines we also need
75** to adjust the encoding. Only alphabetic characters and underscores
76** need to be translated.
77*/
78#ifdef SQLITE_ASCII
79# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
80#endif
81#ifdef SQLITE_EBCDIC
82# define charMap(X) ebcdicToAscii[(unsigned char)X]
83const unsigned char ebcdicToAscii[] = {
84/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
93 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
94 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
95 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
97 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
98 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
99 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
101};
102#endif
103
104/*
drh52fb6d72004-11-03 03:59:57 +0000105** The sqlite3KeywordCode function looks up an identifier to determine if
106** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +0000107** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +0000108**
109** The implementation of this routine was generated by a program,
drh73b211a2005-01-18 04:00:42 +0000110** mkkeywordhash.h, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +0000111** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +0000112** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +0000113** the #include below.
drh75897232000-05-29 14:26:00 +0000114*/
drh73b211a2005-01-18 04:00:42 +0000115#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +0000116
drh40f20f72004-10-23 05:10:18 +0000117
drh98808ba2001-10-18 12:34:46 +0000118/*
drh9b8f4472006-04-04 01:54:55 +0000119** If X is a character that can be used in an identifier then
120** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +0000121**
drh9b8f4472006-04-04 01:54:55 +0000122** For ASCII, any character with the high-order bit set is
123** allowed in an identifier. For 7-bit characters,
124** sqlite3IsIdChar[X] must be 1.
125**
126** For EBCDIC, the rules are more complex but have the same
127** end result.
drha0d1f662005-01-11 17:59:47 +0000128**
129** Ticket #1066. the SQL standard does not allow '$' in the
peter.d.reid60ec9142014-09-06 16:39:46 +0000130** middle of identifiers. But many SQL implementations do.
drha0d1f662005-01-11 17:59:47 +0000131** SQLite will allow '$' in identifiers for compatibility.
132** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +0000133*/
drh9b8f4472006-04-04 01:54:55 +0000134#ifdef SQLITE_ASCII
drhaf2b5722009-11-16 15:11:51 +0000135#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
drh9b8f4472006-04-04 01:54:55 +0000136#endif
137#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59 +0000138const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55 +0000139/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
140 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
141 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
142 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
143 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
144 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
145 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
146 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
148 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
149 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
150 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
151 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
152};
drh46c99e02007-08-27 23:26:59 +0000153#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55 +0000154#endif
drhf5ed7ad2015-06-15 14:43:25 +0000155
156/* Make the IdChar function accessible from ctime.c */
157#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
drh97348b32014-09-25 02:44:29 +0000158int sqlite3IsIdChar(u8 c){ return IdChar(c); }
drhf5ed7ad2015-06-15 14:43:25 +0000159#endif
drh9b8f4472006-04-04 01:54:55 +0000160
drh98808ba2001-10-18 12:34:46 +0000161
drh75897232000-05-29 14:26:00 +0000162/*
drh61b487d2003-09-12 02:08:14 +0000163** Return the length of the token that begins at z[0].
164** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000165*/
drh35b5a332008-04-05 18:41:42 +0000166int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000167 int i, c;
drh89743312016-02-08 02:30:50 +0000168 switch( aiClass[*z] ){
169 case CC_SPACE: {
drh19f81f62009-06-09 18:01:37 +0000170 testcase( z[0]==' ' );
171 testcase( z[0]=='\t' );
172 testcase( z[0]=='\n' );
173 testcase( z[0]=='\f' );
174 testcase( z[0]=='\r' );
danielk197778ca0e72009-01-20 16:53:39 +0000175 for(i=1; sqlite3Isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000176 *tokenType = TK_SPACE;
177 return i;
178 }
drh89743312016-02-08 02:30:50 +0000179 case CC_MINUS: {
drh75897232000-05-29 14:26:00 +0000180 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000181 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drhd3479b92009-12-14 17:42:12 +0000182 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh75897232000-05-29 14:26:00 +0000183 return i;
184 }
185 *tokenType = TK_MINUS;
186 return 1;
187 }
drh89743312016-02-08 02:30:50 +0000188 case CC_LP: {
drhdab35182003-09-27 13:39:38 +0000189 *tokenType = TK_LP;
190 return 1;
drh75897232000-05-29 14:26:00 +0000191 }
drh89743312016-02-08 02:30:50 +0000192 case CC_RP: {
drh75897232000-05-29 14:26:00 +0000193 *tokenType = TK_RP;
194 return 1;
195 }
drh89743312016-02-08 02:30:50 +0000196 case CC_SEMI: {
drh75897232000-05-29 14:26:00 +0000197 *tokenType = TK_SEMI;
198 return 1;
199 }
drh89743312016-02-08 02:30:50 +0000200 case CC_PLUS: {
drh75897232000-05-29 14:26:00 +0000201 *tokenType = TK_PLUS;
202 return 1;
203 }
drh89743312016-02-08 02:30:50 +0000204 case CC_STAR: {
drh75897232000-05-29 14:26:00 +0000205 *tokenType = TK_STAR;
206 return 1;
207 }
drh89743312016-02-08 02:30:50 +0000208 case CC_SLASH: {
drh66105a82002-08-27 14:28:29 +0000209 if( z[1]!='*' || z[2]==0 ){
210 *tokenType = TK_SLASH;
211 return 1;
212 }
drhaa756b02004-09-25 15:25:26 +0000213 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
214 if( c ) i++;
drhd3479b92009-12-14 17:42:12 +0000215 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh66105a82002-08-27 14:28:29 +0000216 return i;
drh75897232000-05-29 14:26:00 +0000217 }
drh89743312016-02-08 02:30:50 +0000218 case CC_PERCENT: {
drhbf4133c2001-10-13 02:59:08 +0000219 *tokenType = TK_REM;
220 return 1;
221 }
drh89743312016-02-08 02:30:50 +0000222 case CC_EQ: {
drh75897232000-05-29 14:26:00 +0000223 *tokenType = TK_EQ;
224 return 1 + (z[1]=='=');
225 }
drh89743312016-02-08 02:30:50 +0000226 case CC_LT: {
drhaa756b02004-09-25 15:25:26 +0000227 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000228 *tokenType = TK_LE;
229 return 2;
drhaa756b02004-09-25 15:25:26 +0000230 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000231 *tokenType = TK_NE;
232 return 2;
drhaa756b02004-09-25 15:25:26 +0000233 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000234 *tokenType = TK_LSHIFT;
235 return 2;
drh75897232000-05-29 14:26:00 +0000236 }else{
237 *tokenType = TK_LT;
238 return 1;
239 }
240 }
drh89743312016-02-08 02:30:50 +0000241 case CC_GT: {
drhaa756b02004-09-25 15:25:26 +0000242 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000243 *tokenType = TK_GE;
244 return 2;
drhaa756b02004-09-25 15:25:26 +0000245 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000246 *tokenType = TK_RSHIFT;
247 return 2;
drh75897232000-05-29 14:26:00 +0000248 }else{
249 *tokenType = TK_GT;
250 return 1;
251 }
252 }
drh89743312016-02-08 02:30:50 +0000253 case CC_BANG: {
drh75897232000-05-29 14:26:00 +0000254 if( z[1]!='=' ){
255 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000256 return 2;
drh75897232000-05-29 14:26:00 +0000257 }else{
258 *tokenType = TK_NE;
259 return 2;
260 }
261 }
drh89743312016-02-08 02:30:50 +0000262 case CC_PIPE: {
drh00400772000-06-16 20:51:26 +0000263 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000264 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000265 return 1;
266 }else{
267 *tokenType = TK_CONCAT;
268 return 2;
269 }
270 }
drh89743312016-02-08 02:30:50 +0000271 case CC_COMMA: {
drh75897232000-05-29 14:26:00 +0000272 *tokenType = TK_COMMA;
273 return 1;
274 }
drh89743312016-02-08 02:30:50 +0000275 case CC_AND: {
drhbf4133c2001-10-13 02:59:08 +0000276 *tokenType = TK_BITAND;
277 return 1;
278 }
drh89743312016-02-08 02:30:50 +0000279 case CC_TILDA: {
drhbf4133c2001-10-13 02:59:08 +0000280 *tokenType = TK_BITNOT;
281 return 1;
282 }
drh89743312016-02-08 02:30:50 +0000283 case CC_QUOTE: {
drh75897232000-05-29 14:26:00 +0000284 int delim = z[0];
drh19f81f62009-06-09 18:01:37 +0000285 testcase( delim=='`' );
286 testcase( delim=='\'' );
287 testcase( delim=='"' );
drhaa756b02004-09-25 15:25:26 +0000288 for(i=1; (c=z[i])!=0; i++){
289 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000290 if( z[i+1]==delim ){
291 i++;
292 }else{
293 break;
294 }
295 }
296 }
drha33cb5f2008-08-07 13:05:34 +0000297 if( c=='\'' ){
drheef8b552005-10-23 11:29:40 +0000298 *tokenType = TK_STRING;
299 return i+1;
drha33cb5f2008-08-07 13:05:34 +0000300 }else if( c!=0 ){
301 *tokenType = TK_ID;
302 return i+1;
drheef8b552005-10-23 11:29:40 +0000303 }else{
304 *tokenType = TK_ILLEGAL;
305 return i;
306 }
drh75897232000-05-29 14:26:00 +0000307 }
drh89743312016-02-08 02:30:50 +0000308 case CC_DOT: {
drh76816182005-08-23 11:31:26 +0000309#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197778ca0e72009-01-20 16:53:39 +0000310 if( !sqlite3Isdigit(z[1]) )
drh76816182005-08-23 11:31:26 +0000311#endif
312 {
313 *tokenType = TK_DOT;
314 return 1;
315 }
316 /* If the next character is a digit, this is a floating point
317 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000318 }
drh89743312016-02-08 02:30:50 +0000319 case CC_DIGIT: {
drh19f81f62009-06-09 18:01:37 +0000320 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
321 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
322 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
323 testcase( z[0]=='9' );
drhc837e702000-06-08 16:26:24 +0000324 *tokenType = TK_INTEGER;
drh28e048c2014-07-23 01:26:51 +0000325#ifndef SQLITE_OMIT_HEX_INTEGER
326 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
327 for(i=3; sqlite3Isxdigit(z[i]); i++){}
328 return i;
329 }
330#endif
danielk197778ca0e72009-01-20 16:53:39 +0000331 for(i=0; sqlite3Isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000332#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000333 if( z[i]=='.' ){
334 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000335 while( sqlite3Isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000336 *tokenType = TK_FLOAT;
337 }
338 if( (z[i]=='e' || z[i]=='E') &&
danielk197778ca0e72009-01-20 16:53:39 +0000339 ( sqlite3Isdigit(z[i+1])
340 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
drh75897232000-05-29 14:26:00 +0000341 )
drhc837e702000-06-08 16:26:24 +0000342 ){
343 i += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000344 while( sqlite3Isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000345 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000346 }
drhb7f91642004-10-31 02:22:47 +0000347#endif
drh67dd9012006-08-12 12:33:14 +0000348 while( IdChar(z[i]) ){
349 *tokenType = TK_ILLEGAL;
350 i++;
351 }
drh75897232000-05-29 14:26:00 +0000352 return i;
353 }
drh89743312016-02-08 02:30:50 +0000354 case CC_QUOTE2: {
drhaa756b02004-09-25 15:25:26 +0000355 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09 +0000356 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51 +0000357 return i;
358 }
drh89743312016-02-08 02:30:50 +0000359 case CC_VARNUM: {
drh50457892003-09-06 01:10:47 +0000360 *tokenType = TK_VARIABLE;
danielk197778ca0e72009-01-20 16:53:39 +0000361 for(i=1; sqlite3Isdigit(z[i]); i++){}
drhfa6bc002004-09-07 16:19:52 +0000362 return i;
drh50457892003-09-06 01:10:47 +0000363 }
drh89743312016-02-08 02:30:50 +0000364 case CC_DOLLAR:
365 case CC_VARALPHA: {
drh288d37f2005-06-22 08:48:06 +0000366 int n = 0;
drhf59b12f2014-01-11 03:54:05 +0000367 testcase( z[0]=='$' ); testcase( z[0]=='@' );
368 testcase( z[0]==':' ); testcase( z[0]=='#' );
drh9d74b4c2004-08-24 15:23:34 +0000369 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000370 for(i=1; (c=z[i])!=0; i++){
371 if( IdChar(c) ){
372 n++;
373#ifndef SQLITE_OMIT_TCL_VARIABLE
374 }else if( c=='(' && n>0 ){
375 do{
376 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000377 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
drh288d37f2005-06-22 08:48:06 +0000378 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000379 i++;
380 }else{
drh288d37f2005-06-22 08:48:06 +0000381 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000382 }
drh288d37f2005-06-22 08:48:06 +0000383 break;
384 }else if( c==':' && z[i+1]==':' ){
385 i++;
386#endif
387 }else{
388 break;
drh895d7472004-08-20 16:02:39 +0000389 }
390 }
drh288d37f2005-06-22 08:48:06 +0000391 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000392 return i;
drhb7f91642004-10-31 02:22:47 +0000393 }
drhb7f91642004-10-31 02:22:47 +0000394#ifndef SQLITE_OMIT_BLOB_LITERAL
drh89743312016-02-08 02:30:50 +0000395 case CC_X: {
drh19f81f62009-06-09 18:01:37 +0000396 testcase( z[0]=='x' ); testcase( z[0]=='X' );
drh4b2f9362008-01-22 23:37:09 +0000397 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19 +0000398 *tokenType = TK_BLOB;
drh3a61a5a2011-06-03 21:34:45 +0000399 for(i=2; sqlite3Isxdigit(z[i]); i++){}
400 if( z[i]!='\'' || i%2 ){
401 *tokenType = TK_ILLEGAL;
402 while( z[i] && z[i]!='\'' ){ i++; }
danielk1977c572ef72004-05-27 09:28:41 +0000403 }
drh3a61a5a2011-06-03 21:34:45 +0000404 if( z[i] ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000405 return i;
406 }
drh89743312016-02-08 02:30:50 +0000407 i = 1;
408 break;
danielk1977c572ef72004-05-27 09:28:41 +0000409 }
drhb7f91642004-10-31 02:22:47 +0000410#endif
drh89743312016-02-08 02:30:50 +0000411 case CC_KYWD: {
412 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
413 if( aiClass[z[i]]<=CC_DOLLAR ){ i++; break; }
drh54bb56d2015-11-10 03:30:51 +0000414 *tokenType = TK_ID;
415 return keywordCode((char*)z, i, tokenType);
drh75897232000-05-29 14:26:00 +0000416 }
drh89743312016-02-08 02:30:50 +0000417 case CC_ID: {
418 i = 1;
419 break;
420 }
421 default: {
422 *tokenType = TK_ILLEGAL;
423 return 1;
424 }
drh75897232000-05-29 14:26:00 +0000425 }
drh89743312016-02-08 02:30:50 +0000426 while( aiClass[z[i]]<=CC_DOLLAR ){ i++; }
427 *tokenType = TK_ID;
428 return i;
drh75897232000-05-29 14:26:00 +0000429}
430
431/*
432** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000433** passed in. An SQLITE_ status code is returned. If an error occurs
drhfb45d8c2008-07-08 00:06:49 +0000434** then an and attempt is made to write an error message into
435** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
436** error message.
drh75897232000-05-29 14:26:00 +0000437*/
danielk19774adee202004-05-08 08:23:19 +0000438int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drhd9da78a2009-03-24 15:08:09 +0000439 int nErr = 0; /* Number of errors encountered */
440 int i; /* Loop counter */
441 void *pEngine; /* The LEMON-generated LALR(1) parser */
442 int tokenType; /* type of the next token */
443 int lastTokenParsed = -1; /* type of the previous token */
drhd9da78a2009-03-24 15:08:09 +0000444 sqlite3 *db = pParse->db; /* The database connection */
445 int mxSqlLen; /* Max length of an SQL string */
drh75897232000-05-29 14:26:00 +0000446
drh96c707a2015-02-13 16:36:14 +0000447 assert( zSql!=0 );
drhd9da78a2009-03-24 15:08:09 +0000448 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh4f7d3a52013-06-27 23:54:02 +0000449 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000450 db->u1.isInterrupted = 0;
451 }
drh4c504392000-10-16 22:06:40 +0000452 pParse->rc = SQLITE_OK;
drhe7f3f3e2009-07-03 22:54:36 +0000453 pParse->zTail = zSql;
drh75897232000-05-29 14:26:00 +0000454 i = 0;
drhfb45d8c2008-07-08 00:06:49 +0000455 assert( pzErrMsg!=0 );
drh3bd48ab2015-09-07 18:23:37 +0000456 /* sqlite3ParserTrace(stdout, "parser: "); */
drhfb046e72014-09-12 04:28:33 +0000457 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
drh75897232000-05-29 14:26:00 +0000458 if( pEngine==0 ){
drh4a642b62016-02-05 01:55:27 +0000459 sqlite3OomFault(db);
drhdefc9972005-06-06 14:45:42 +0000460 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000461 }
drhfa6bc002004-09-07 16:19:52 +0000462 assert( pParse->pNewTable==0 );
463 assert( pParse->pNewTrigger==0 );
464 assert( pParse->nVar==0 );
drh124c0b42011-06-01 18:15:55 +0000465 assert( pParse->nzVar==0 );
466 assert( pParse->azVar==0 );
drh0739e452015-11-09 02:08:09 +0000467 while( zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000468 assert( i>=0 );
drhb7916a72009-05-27 10:31:29 +0000469 pParse->sLastToken.z = &zSql[i];
drh35b5a332008-04-05 18:41:42 +0000470 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000471 i += pParse->sLastToken.n;
drhbb4957f2008-03-20 14:03:29 +0000472 if( i>mxSqlLen ){
drhe5c941b2007-05-08 13:58:26 +0000473 pParse->rc = SQLITE_TOOBIG;
474 break;
475 }
drh0739e452015-11-09 02:08:09 +0000476 if( tokenType>=TK_SPACE ){
477 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
478 if( db->u1.isInterrupted ){
drh0739e452015-11-09 02:08:09 +0000479 pParse->rc = SQLITE_INTERRUPT;
drh75897232000-05-29 14:26:00 +0000480 break;
481 }
drh0739e452015-11-09 02:08:09 +0000482 if( tokenType==TK_ILLEGAL ){
drh347bdc32015-04-15 07:57:27 +0000483 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
drhfb45d8c2008-07-08 00:06:49 +0000484 &pParse->sLastToken);
drh75897232000-05-29 14:26:00 +0000485 break;
drh32eb7b42003-01-07 01:44:37 +0000486 }
drh0739e452015-11-09 02:08:09 +0000487 }else{
488 if( tokenType==TK_SEMI ) pParse->zTail = &zSql[i];
489 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
490 lastTokenParsed = tokenType;
491 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
drh75897232000-05-29 14:26:00 +0000492 }
493 }
drh0be0cf62015-04-15 08:37:42 +0000494 assert( nErr==0 );
drhe7266222015-05-29 17:51:16 +0000495 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
496 assert( zSql[i]==0 );
drhb86ccfb2003-01-28 23:13:10 +0000497 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000498 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drha248a722015-09-07 19:52:55 +0000499 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000500 }
drh9a9705d2015-04-25 00:32:30 +0000501 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
502 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
503 }
drh75897232000-05-29 14:26:00 +0000504 }
drhec424a52008-07-25 15:39:03 +0000505#ifdef YYTRACKMAXSTACKDEPTH
drhaf89fe62015-03-23 17:25:18 +0000506 sqlite3_mutex_enter(sqlite3MallocMutex());
drhb02392e2015-10-15 15:28:56 +0000507 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
drhec424a52008-07-25 15:39:03 +0000508 sqlite3ParserStackPeak(pEngine)
509 );
drhaf89fe62015-03-23 17:25:18 +0000510 sqlite3_mutex_leave(sqlite3MallocMutex());
drhec424a52008-07-25 15:39:03 +0000511#endif /* YYDEBUG */
drh17435752007-08-16 04:30:38 +0000512 sqlite3ParserFree(pEngine, sqlite3_free);
513 if( db->mallocFailed ){
drh71c697e2004-08-08 23:39:19 +0000514 pParse->rc = SQLITE_NOMEM;
515 }
drhb86ccfb2003-01-28 23:13:10 +0000516 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
drh22c17b82015-05-15 04:13:15 +0000517 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
drhb86ccfb2003-01-28 23:13:10 +0000518 }
drh19f81f62009-06-09 18:01:37 +0000519 assert( pzErrMsg!=0 );
drh75897232000-05-29 14:26:00 +0000520 if( pParse->zErrMsg ){
drh19f81f62009-06-09 18:01:37 +0000521 *pzErrMsg = pParse->zErrMsg;
drh413c3d32010-02-23 20:11:56 +0000522 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
drhb86ccfb2003-01-28 23:13:10 +0000523 pParse->zErrMsg = 0;
drh4b2f9362008-01-22 23:37:09 +0000524 nErr++;
drh75897232000-05-29 14:26:00 +0000525 }
drh2958a4e2004-11-12 03:56:15 +0000526 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000527 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000528 pParse->pVdbe = 0;
529 }
danielk1977c00da102006-01-07 13:21:04 +0000530#ifndef SQLITE_OMIT_SHARED_CACHE
531 if( pParse->nested==0 ){
drh633e6d52008-07-28 19:34:53 +0000532 sqlite3DbFree(db, pParse->aTableLock);
danielk1977c00da102006-01-07 13:21:04 +0000533 pParse->aTableLock = 0;
534 pParse->nTableLock = 0;
535 }
536#endif
drh4f3dd152008-04-28 18:46:43 +0000537#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9755982010-07-24 16:34:37 +0000538 sqlite3_free(pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43 +0000539#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000540
danielk19777e6ebfb2006-06-12 11:24:37 +0000541 if( !IN_DECLARE_VTAB ){
542 /* If the pParse->declareVtab flag is set, do not delete any table
543 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
544 ** will take responsibility for freeing the Table structure.
545 */
dan1feeaed2010-07-23 15:41:47 +0000546 sqlite3DeleteTable(db, pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000547 }
548
drh6e772262015-11-07 17:48:21 +0000549 sqlite3WithDelete(db, pParse->pWithToFree);
drh633e6d52008-07-28 19:34:53 +0000550 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
drh124c0b42011-06-01 18:15:55 +0000551 for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
552 sqlite3DbFree(db, pParse->azVar);
drh0b9f50d2009-06-23 20:28:53 +0000553 while( pParse->pAinc ){
554 AutoincInfo *p = pParse->pAinc;
555 pParse->pAinc = p->pNext;
556 sqlite3DbFree(db, p);
557 }
drh588a9a12008-09-01 15:52:10 +0000558 while( pParse->pZombieTab ){
559 Table *p = pParse->pZombieTab;
560 pParse->pZombieTab = p->pNextZombie;
dan1feeaed2010-07-23 15:41:47 +0000561 sqlite3DeleteTable(db, p);
drh588a9a12008-09-01 15:52:10 +0000562 }
drhf3151f02015-04-16 20:27:09 +0000563 assert( nErr==0 || pParse->rc!=SQLITE_OK );
drh75897232000-05-29 14:26:00 +0000564 return nErr;
565}