blob: 72f160d59b520fbbe9b415e720e574f409a37e6c [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
drh34dcee62016-02-08 19:15:48 +000021/* Character classes for tokenizing
22**
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.
28*/
drhe96f3612016-02-08 19:36:46 +000029#define CC_X 0 /* The letter 'x', or start of BLOB literal */
drh89743312016-02-08 02:30:50 +000030#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 */
57
58static const unsigned char aiClass[] = {
drh34dcee62016-02-08 19:15:48 +000059#ifdef SQLITE_ASCII
drh89743312016-02-08 02:30:50 +000060/* 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
drh34dcee62016-02-08 19:15:48 +000077#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,
drh585a5972016-12-12 01:53:36 +000084/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
drh34dcee62016-02-08 19:15:48 +000085/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
drh585a5972016-12-12 01:53:36 +000086/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
drh34dcee62016-02-08 19:15:48 +000087/* 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,
drh585a5972016-12-12 01:53:36 +000090/* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
drh34dcee62016-02-08 19:15:48 +000091/* 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
drh89743312016-02-08 02:30:50 +000097};
98
drh75897232000-05-29 14:26:00 +000099/*
drh34dcee62016-02-08 19:15:48 +0000100** The charMap() macro maps alphabetic characters (only) into their
drh9b8f4472006-04-04 01:54:55 +0000101** lower-case ASCII equivalent. On ASCII machines, this is just
102** an upper-to-lower case map. On EBCDIC machines we also need
drh34dcee62016-02-08 19:15:48 +0000103** to adjust the encoding. The mapping is only valid for alphabetics
104** which are the only characters for which this feature is used.
105**
106** Used by keywordhash.h
drh9b8f4472006-04-04 01:54:55 +0000107*/
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]
113const 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 */
131};
132#endif
133
134/*
drh52fb6d72004-11-03 03:59:57 +0000135** 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
drh75897232000-05-29 14:26:00 +0000137** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +0000138**
139** The implementation of this routine was generated by a program,
drhe96f3612016-02-08 19:36:46 +0000140** mkkeywordhash.c, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +0000141** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +0000142** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +0000143** the #include below.
drh75897232000-05-29 14:26:00 +0000144*/
drh73b211a2005-01-18 04:00:42 +0000145#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +0000146
drh40f20f72004-10-23 05:10:18 +0000147
drh98808ba2001-10-18 12:34:46 +0000148/*
drh9b8f4472006-04-04 01:54:55 +0000149** If X is a character that can be used in an identifier then
150** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +0000151**
drh9b8f4472006-04-04 01:54:55 +0000152** 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.
155**
156** For EBCDIC, the rules are more complex but have the same
157** end result.
drha0d1f662005-01-11 17:59:47 +0000158**
159** Ticket #1066. the SQL standard does not allow '$' in the
peter.d.reid60ec9142014-09-06 16:39:46 +0000160** middle of identifiers. But many SQL implementations do.
drha0d1f662005-01-11 17:59:47 +0000161** SQLite will allow '$' in identifiers for compatibility.
162** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +0000163*/
drh9b8f4472006-04-04 01:54:55 +0000164#ifdef SQLITE_ASCII
drhaf2b5722009-11-16 15:11:51 +0000165#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
drh9b8f4472006-04-04 01:54:55 +0000166#endif
167#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59 +0000168const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55 +0000169/* 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 */
182};
drh46c99e02007-08-27 23:26:59 +0000183#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55 +0000184#endif
drhf5ed7ad2015-06-15 14:43:25 +0000185
186/* Make the IdChar function accessible from ctime.c */
187#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
drh97348b32014-09-25 02:44:29 +0000188int sqlite3IsIdChar(u8 c){ return IdChar(c); }
drhf5ed7ad2015-06-15 14:43:25 +0000189#endif
drh9b8f4472006-04-04 01:54:55 +0000190
dan34a7d792018-06-29 20:43:33 +0000191#ifndef SQLITE_OMIT_WINDOWFUNC
dan59ff4252018-06-29 17:44:52 +0000192/*
193** Return the id of the next token in string (*pz). Before returning, set
194** (*pz) to point to the byte following the parsed token.
195**
196** This function assumes that any keywords that start with "w" are
197** actually TK_ID.
198*/
199static int windowGetToken(const unsigned char **pz){
200 int ret;
201 const unsigned char *z = *pz;
202 if( z[0]=='w' || z[0]=='W' ){
203 do { z++; }while( IdChar(z[0]) );
204 ret = TK_ID;
205 }else{
206 z += sqlite3GetToken(z, &ret);
207 }
208 *pz = z;
209 return ret;
210}
dan34a7d792018-06-29 20:43:33 +0000211#endif // SQLITE_OMIT_WINDOWFUNC
dan59ff4252018-06-29 17:44:52 +0000212
dan34a7d792018-06-29 20:43:33 +0000213#ifndef SQLITE_OMIT_WINDOWFUNC
dan59ff4252018-06-29 17:44:52 +0000214/*
215** The tokenizer has just parsed the keyword WINDOW. In this case the token
216** may really be the keyword (TK_WINDOW), or may be an identifier (TK_ID).
217** This function determines which it is by inspecting the next two tokens
218** in the input stream. Specifically, the token is TK_WINDOW if the following
219** two tokens are:
220**
221** * TK_ID, or something else that can be used as a window name, and
222** * TK_AS.
223**
224** Instead of using sqlite3GetToken() to parse tokens directly, this function
225** uses windowGetToken(). This is to avoid recursion if the input is similar
226** to "window window window window".
227*/
dan769309f2018-06-29 19:54:51 +0000228static int analyzeWindowKeyword(const unsigned char *z){
dan59ff4252018-06-29 17:44:52 +0000229 int t;
dan769309f2018-06-29 19:54:51 +0000230 int ret = TK_WINDOW;
dan59ff4252018-06-29 17:44:52 +0000231 while( (t = windowGetToken(&z))==TK_SPACE );
232 if( t!=TK_ID && t!=TK_STRING
233 && t!=TK_JOIN_KW && sqlite3ParserFallback(t)!=TK_ID
234 ){
dan769309f2018-06-29 19:54:51 +0000235 ret = TK_ID;
dan59ff4252018-06-29 17:44:52 +0000236 }else{
237 while( (t = windowGetToken(&z))==TK_SPACE );
238 if( t!=TK_AS ){
dan769309f2018-06-29 19:54:51 +0000239 ret = TK_ID;
dan59ff4252018-06-29 17:44:52 +0000240 }
241 }
dan769309f2018-06-29 19:54:51 +0000242 return ret;
dan59ff4252018-06-29 17:44:52 +0000243}
dan34a7d792018-06-29 20:43:33 +0000244#endif // SQLITE_OMIT_WINDOWFUNC
drh98808ba2001-10-18 12:34:46 +0000245
drh75897232000-05-29 14:26:00 +0000246/*
drhe96f3612016-02-08 19:36:46 +0000247** Return the length (in bytes) of the token that begins at z[0].
drh61b487d2003-09-12 02:08:14 +0000248** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000249*/
drh35b5a332008-04-05 18:41:42 +0000250int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000251 int i, c;
drhe96f3612016-02-08 19:36:46 +0000252 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
253 ** of the token. See the comment on the CC_ defines
254 ** above. */
drh89743312016-02-08 02:30:50 +0000255 case CC_SPACE: {
drh19f81f62009-06-09 18:01:37 +0000256 testcase( z[0]==' ' );
257 testcase( z[0]=='\t' );
258 testcase( z[0]=='\n' );
259 testcase( z[0]=='\f' );
260 testcase( z[0]=='\r' );
danielk197778ca0e72009-01-20 16:53:39 +0000261 for(i=1; sqlite3Isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000262 *tokenType = TK_SPACE;
263 return i;
264 }
drh89743312016-02-08 02:30:50 +0000265 case CC_MINUS: {
drh75897232000-05-29 14:26:00 +0000266 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000267 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drhd3479b92009-12-14 17:42:12 +0000268 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh75897232000-05-29 14:26:00 +0000269 return i;
270 }
271 *tokenType = TK_MINUS;
272 return 1;
273 }
drh89743312016-02-08 02:30:50 +0000274 case CC_LP: {
drhdab35182003-09-27 13:39:38 +0000275 *tokenType = TK_LP;
276 return 1;
drh75897232000-05-29 14:26:00 +0000277 }
drh89743312016-02-08 02:30:50 +0000278 case CC_RP: {
drh75897232000-05-29 14:26:00 +0000279 *tokenType = TK_RP;
280 return 1;
281 }
drh89743312016-02-08 02:30:50 +0000282 case CC_SEMI: {
drh75897232000-05-29 14:26:00 +0000283 *tokenType = TK_SEMI;
284 return 1;
285 }
drh89743312016-02-08 02:30:50 +0000286 case CC_PLUS: {
drh75897232000-05-29 14:26:00 +0000287 *tokenType = TK_PLUS;
288 return 1;
289 }
drh89743312016-02-08 02:30:50 +0000290 case CC_STAR: {
drh75897232000-05-29 14:26:00 +0000291 *tokenType = TK_STAR;
292 return 1;
293 }
drh89743312016-02-08 02:30:50 +0000294 case CC_SLASH: {
drh66105a82002-08-27 14:28:29 +0000295 if( z[1]!='*' || z[2]==0 ){
296 *tokenType = TK_SLASH;
297 return 1;
298 }
drhaa756b02004-09-25 15:25:26 +0000299 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
300 if( c ) i++;
drhd3479b92009-12-14 17:42:12 +0000301 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh66105a82002-08-27 14:28:29 +0000302 return i;
drh75897232000-05-29 14:26:00 +0000303 }
drh89743312016-02-08 02:30:50 +0000304 case CC_PERCENT: {
drhbf4133c2001-10-13 02:59:08 +0000305 *tokenType = TK_REM;
306 return 1;
307 }
drh89743312016-02-08 02:30:50 +0000308 case CC_EQ: {
drh75897232000-05-29 14:26:00 +0000309 *tokenType = TK_EQ;
310 return 1 + (z[1]=='=');
311 }
drh89743312016-02-08 02:30:50 +0000312 case CC_LT: {
drhaa756b02004-09-25 15:25:26 +0000313 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000314 *tokenType = TK_LE;
315 return 2;
drhaa756b02004-09-25 15:25:26 +0000316 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000317 *tokenType = TK_NE;
318 return 2;
drhaa756b02004-09-25 15:25:26 +0000319 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000320 *tokenType = TK_LSHIFT;
321 return 2;
drh75897232000-05-29 14:26:00 +0000322 }else{
323 *tokenType = TK_LT;
324 return 1;
325 }
326 }
drh89743312016-02-08 02:30:50 +0000327 case CC_GT: {
drhaa756b02004-09-25 15:25:26 +0000328 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000329 *tokenType = TK_GE;
330 return 2;
drhaa756b02004-09-25 15:25:26 +0000331 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000332 *tokenType = TK_RSHIFT;
333 return 2;
drh75897232000-05-29 14:26:00 +0000334 }else{
335 *tokenType = TK_GT;
336 return 1;
337 }
338 }
drh89743312016-02-08 02:30:50 +0000339 case CC_BANG: {
drh75897232000-05-29 14:26:00 +0000340 if( z[1]!='=' ){
341 *tokenType = TK_ILLEGAL;
drhb2bddbb2016-02-18 14:49:28 +0000342 return 1;
drh75897232000-05-29 14:26:00 +0000343 }else{
344 *tokenType = TK_NE;
345 return 2;
346 }
347 }
drh89743312016-02-08 02:30:50 +0000348 case CC_PIPE: {
drh00400772000-06-16 20:51:26 +0000349 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000350 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000351 return 1;
352 }else{
353 *tokenType = TK_CONCAT;
354 return 2;
355 }
356 }
drh89743312016-02-08 02:30:50 +0000357 case CC_COMMA: {
drh75897232000-05-29 14:26:00 +0000358 *tokenType = TK_COMMA;
359 return 1;
360 }
drh89743312016-02-08 02:30:50 +0000361 case CC_AND: {
drhbf4133c2001-10-13 02:59:08 +0000362 *tokenType = TK_BITAND;
363 return 1;
364 }
drh89743312016-02-08 02:30:50 +0000365 case CC_TILDA: {
drhbf4133c2001-10-13 02:59:08 +0000366 *tokenType = TK_BITNOT;
367 return 1;
368 }
drh89743312016-02-08 02:30:50 +0000369 case CC_QUOTE: {
drh75897232000-05-29 14:26:00 +0000370 int delim = z[0];
drh19f81f62009-06-09 18:01:37 +0000371 testcase( delim=='`' );
372 testcase( delim=='\'' );
373 testcase( delim=='"' );
drhaa756b02004-09-25 15:25:26 +0000374 for(i=1; (c=z[i])!=0; i++){
375 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000376 if( z[i+1]==delim ){
377 i++;
378 }else{
379 break;
380 }
381 }
382 }
drha33cb5f2008-08-07 13:05:34 +0000383 if( c=='\'' ){
drheef8b552005-10-23 11:29:40 +0000384 *tokenType = TK_STRING;
385 return i+1;
drha33cb5f2008-08-07 13:05:34 +0000386 }else if( c!=0 ){
387 *tokenType = TK_ID;
388 return i+1;
drheef8b552005-10-23 11:29:40 +0000389 }else{
390 *tokenType = TK_ILLEGAL;
391 return i;
392 }
drh75897232000-05-29 14:26:00 +0000393 }
drh89743312016-02-08 02:30:50 +0000394 case CC_DOT: {
drh76816182005-08-23 11:31:26 +0000395#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197778ca0e72009-01-20 16:53:39 +0000396 if( !sqlite3Isdigit(z[1]) )
drh76816182005-08-23 11:31:26 +0000397#endif
398 {
399 *tokenType = TK_DOT;
400 return 1;
401 }
402 /* If the next character is a digit, this is a floating point
403 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000404 }
drh89743312016-02-08 02:30:50 +0000405 case CC_DIGIT: {
drh19f81f62009-06-09 18:01:37 +0000406 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
407 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
408 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
409 testcase( z[0]=='9' );
drhc837e702000-06-08 16:26:24 +0000410 *tokenType = TK_INTEGER;
drh28e048c2014-07-23 01:26:51 +0000411#ifndef SQLITE_OMIT_HEX_INTEGER
412 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
413 for(i=3; sqlite3Isxdigit(z[i]); i++){}
414 return i;
415 }
416#endif
danielk197778ca0e72009-01-20 16:53:39 +0000417 for(i=0; sqlite3Isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000418#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000419 if( z[i]=='.' ){
420 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000421 while( sqlite3Isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000422 *tokenType = TK_FLOAT;
423 }
424 if( (z[i]=='e' || z[i]=='E') &&
danielk197778ca0e72009-01-20 16:53:39 +0000425 ( sqlite3Isdigit(z[i+1])
426 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
drh75897232000-05-29 14:26:00 +0000427 )
drhc837e702000-06-08 16:26:24 +0000428 ){
429 i += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000430 while( sqlite3Isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000431 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000432 }
drhb7f91642004-10-31 02:22:47 +0000433#endif
drh67dd9012006-08-12 12:33:14 +0000434 while( IdChar(z[i]) ){
435 *tokenType = TK_ILLEGAL;
436 i++;
437 }
drh75897232000-05-29 14:26:00 +0000438 return i;
439 }
drh89743312016-02-08 02:30:50 +0000440 case CC_QUOTE2: {
drhaa756b02004-09-25 15:25:26 +0000441 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09 +0000442 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51 +0000443 return i;
444 }
drh89743312016-02-08 02:30:50 +0000445 case CC_VARNUM: {
drh50457892003-09-06 01:10:47 +0000446 *tokenType = TK_VARIABLE;
danielk197778ca0e72009-01-20 16:53:39 +0000447 for(i=1; sqlite3Isdigit(z[i]); i++){}
drhfa6bc002004-09-07 16:19:52 +0000448 return i;
drh50457892003-09-06 01:10:47 +0000449 }
drh89743312016-02-08 02:30:50 +0000450 case CC_DOLLAR:
451 case CC_VARALPHA: {
drh288d37f2005-06-22 08:48:06 +0000452 int n = 0;
drhf59b12f2014-01-11 03:54:05 +0000453 testcase( z[0]=='$' ); testcase( z[0]=='@' );
454 testcase( z[0]==':' ); testcase( z[0]=='#' );
drh9d74b4c2004-08-24 15:23:34 +0000455 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000456 for(i=1; (c=z[i])!=0; i++){
457 if( IdChar(c) ){
458 n++;
459#ifndef SQLITE_OMIT_TCL_VARIABLE
460 }else if( c=='(' && n>0 ){
461 do{
462 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000463 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
drh288d37f2005-06-22 08:48:06 +0000464 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000465 i++;
466 }else{
drh288d37f2005-06-22 08:48:06 +0000467 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000468 }
drh288d37f2005-06-22 08:48:06 +0000469 break;
470 }else if( c==':' && z[i+1]==':' ){
471 i++;
472#endif
473 }else{
474 break;
drh895d7472004-08-20 16:02:39 +0000475 }
476 }
drh288d37f2005-06-22 08:48:06 +0000477 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000478 return i;
drhb7f91642004-10-31 02:22:47 +0000479 }
drhe96f3612016-02-08 19:36:46 +0000480 case CC_KYWD: {
481 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
482 if( IdChar(z[i]) ){
483 /* This token started out using characters that can appear in keywords,
484 ** but z[i] is a character not allowed within keywords, so this must
485 ** be an identifier instead */
486 i++;
487 break;
488 }
489 *tokenType = TK_ID;
dan769309f2018-06-29 19:54:51 +0000490 return keywordCode((char*)z, i, tokenType);
drhe96f3612016-02-08 19:36:46 +0000491 }
drh89743312016-02-08 02:30:50 +0000492 case CC_X: {
dana73086d2016-02-24 16:14:07 +0000493#ifndef SQLITE_OMIT_BLOB_LITERAL
drh19f81f62009-06-09 18:01:37 +0000494 testcase( z[0]=='x' ); testcase( z[0]=='X' );
drh4b2f9362008-01-22 23:37:09 +0000495 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19 +0000496 *tokenType = TK_BLOB;
drh3a61a5a2011-06-03 21:34:45 +0000497 for(i=2; sqlite3Isxdigit(z[i]); i++){}
498 if( z[i]!='\'' || i%2 ){
499 *tokenType = TK_ILLEGAL;
500 while( z[i] && z[i]!='\'' ){ i++; }
danielk1977c572ef72004-05-27 09:28:41 +0000501 }
drh3a61a5a2011-06-03 21:34:45 +0000502 if( z[i] ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000503 return i;
504 }
dana73086d2016-02-24 16:14:07 +0000505#endif
drhe96f3612016-02-08 19:36:46 +0000506 /* If it is not a BLOB literal, then it must be an ID, since no
507 ** SQL keywords start with the letter 'x'. Fall through */
danielk1977c572ef72004-05-27 09:28:41 +0000508 }
drh89743312016-02-08 02:30:50 +0000509 case CC_ID: {
510 i = 1;
511 break;
512 }
drh98808ba2001-10-18 12:34:46 +0000513 default: {
drh89743312016-02-08 02:30:50 +0000514 *tokenType = TK_ILLEGAL;
515 return 1;
drh75897232000-05-29 14:26:00 +0000516 }
drh75897232000-05-29 14:26:00 +0000517 }
drh34dcee62016-02-08 19:15:48 +0000518 while( IdChar(z[i]) ){ i++; }
drh89743312016-02-08 02:30:50 +0000519 *tokenType = TK_ID;
520 return i;
drh75897232000-05-29 14:26:00 +0000521}
522
523/*
524** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000525** passed in. An SQLITE_ status code is returned. If an error occurs
drhfb45d8c2008-07-08 00:06:49 +0000526** then an and attempt is made to write an error message into
527** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
528** error message.
drh75897232000-05-29 14:26:00 +0000529*/
danielk19774adee202004-05-08 08:23:19 +0000530int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drhd9da78a2009-03-24 15:08:09 +0000531 int nErr = 0; /* Number of errors encountered */
drhd9da78a2009-03-24 15:08:09 +0000532 void *pEngine; /* The LEMON-generated LALR(1) parser */
drh9b38d662017-03-07 14:38:52 +0000533 int n = 0; /* Length of the next token token */
drhd9da78a2009-03-24 15:08:09 +0000534 int tokenType; /* type of the next token */
535 int lastTokenParsed = -1; /* type of the previous token */
drhd9da78a2009-03-24 15:08:09 +0000536 sqlite3 *db = pParse->db; /* The database connection */
537 int mxSqlLen; /* Max length of an SQL string */
drhd26cc542017-01-28 20:46:37 +0000538#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
drh53b24592017-03-30 17:13:37 +0000539 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
drhd26cc542017-01-28 20:46:37 +0000540#endif
drh75897232000-05-29 14:26:00 +0000541
drh96c707a2015-02-13 16:36:14 +0000542 assert( zSql!=0 );
drhd9da78a2009-03-24 15:08:09 +0000543 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh4f7d3a52013-06-27 23:54:02 +0000544 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000545 db->u1.isInterrupted = 0;
546 }
drh4c504392000-10-16 22:06:40 +0000547 pParse->rc = SQLITE_OK;
drhe7f3f3e2009-07-03 22:54:36 +0000548 pParse->zTail = zSql;
drhfb45d8c2008-07-08 00:06:49 +0000549 assert( pzErrMsg!=0 );
drh3bd48ab2015-09-07 18:23:37 +0000550 /* sqlite3ParserTrace(stdout, "parser: "); */
drhd26cc542017-01-28 20:46:37 +0000551#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
drh53b24592017-03-30 17:13:37 +0000552 pEngine = &sEngine;
drhfb32c442018-04-21 13:51:42 +0000553 sqlite3ParserInit(pEngine, pParse);
drhd26cc542017-01-28 20:46:37 +0000554#else
drhfb32c442018-04-21 13:51:42 +0000555 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
drh75897232000-05-29 14:26:00 +0000556 if( pEngine==0 ){
drh4a642b62016-02-05 01:55:27 +0000557 sqlite3OomFault(db);
mistachkinfad30392016-02-13 23:43:46 +0000558 return SQLITE_NOMEM_BKPT;
drh75897232000-05-29 14:26:00 +0000559 }
drhd26cc542017-01-28 20:46:37 +0000560#endif
drhfa6bc002004-09-07 16:19:52 +0000561 assert( pParse->pNewTable==0 );
562 assert( pParse->pNewTrigger==0 );
563 assert( pParse->nVar==0 );
drh9bf755c2016-12-23 03:59:31 +0000564 assert( pParse->pVList==0 );
drh167fbbe2016-08-10 14:40:00 +0000565 while( 1 ){
dand437ac02018-06-29 20:21:24 +0000566 n = sqlite3GetToken((u8*)zSql, &tokenType);
567 mxSqlLen -= n;
568 if( mxSqlLen<0 ){
569 pParse->rc = SQLITE_TOOBIG;
570 break;
drhe5c941b2007-05-08 13:58:26 +0000571 }
dan34a7d792018-06-29 20:43:33 +0000572#ifndef SQLITE_OMIT_WINDOWFUNC
573 if( tokenType>=TK_WINDOW ){
574 assert( tokenType==TK_SPACE
575 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
576 );
577#else
drh0739e452015-11-09 02:08:09 +0000578 if( tokenType>=TK_SPACE ){
579 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
dan34a7d792018-06-29 20:43:33 +0000580#endif // SQLITE_OMIT_WINDOWFUNC
drh0739e452015-11-09 02:08:09 +0000581 if( db->u1.isInterrupted ){
drh0739e452015-11-09 02:08:09 +0000582 pParse->rc = SQLITE_INTERRUPT;
drh75897232000-05-29 14:26:00 +0000583 break;
584 }
dand437ac02018-06-29 20:21:24 +0000585 if( tokenType==TK_SPACE ){
586 zSql += n;
587 continue;
588 }
589 if( zSql[0]==0 ){
590 /* Upon reaching the end of input, call the parser two more times
591 ** with tokens TK_SEMI and 0, in that order. */
592 if( lastTokenParsed==TK_SEMI ){
593 tokenType = 0;
594 }else if( lastTokenParsed==0 ){
595 break;
596 }else{
597 tokenType = TK_SEMI;
598 }
599 n = 0;
dan34a7d792018-06-29 20:43:33 +0000600#ifndef SQLITE_OMIT_WINDOWFUNC
601 }else if( tokenType==TK_WINDOW ){
602 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
603#endif // SQLITE_OMIT_WINDOWFUNC
dand437ac02018-06-29 20:21:24 +0000604 }else{
drh9b38d662017-03-07 14:38:52 +0000605 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
drh75897232000-05-29 14:26:00 +0000606 break;
drh32eb7b42003-01-07 01:44:37 +0000607 }
drh75897232000-05-29 14:26:00 +0000608 }
dand437ac02018-06-29 20:21:24 +0000609 pParse->sLastToken.z = zSql;
610 pParse->sLastToken.n = n;
611 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
612 lastTokenParsed = tokenType;
613 zSql += n;
614 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
drh75897232000-05-29 14:26:00 +0000615 }
drh0be0cf62015-04-15 08:37:42 +0000616 assert( nErr==0 );
drhec424a52008-07-25 15:39:03 +0000617#ifdef YYTRACKMAXSTACKDEPTH
drhaf89fe62015-03-23 17:25:18 +0000618 sqlite3_mutex_enter(sqlite3MallocMutex());
drhb02392e2015-10-15 15:28:56 +0000619 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
drhec424a52008-07-25 15:39:03 +0000620 sqlite3ParserStackPeak(pEngine)
621 );
drhaf89fe62015-03-23 17:25:18 +0000622 sqlite3_mutex_leave(sqlite3MallocMutex());
drhec424a52008-07-25 15:39:03 +0000623#endif /* YYDEBUG */
drhd26cc542017-01-28 20:46:37 +0000624#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
625 sqlite3ParserFinalize(pEngine);
626#else
drh17435752007-08-16 04:30:38 +0000627 sqlite3ParserFree(pEngine, sqlite3_free);
drhd26cc542017-01-28 20:46:37 +0000628#endif
drh17435752007-08-16 04:30:38 +0000629 if( db->mallocFailed ){
mistachkinfad30392016-02-13 23:43:46 +0000630 pParse->rc = SQLITE_NOMEM_BKPT;
drh71c697e2004-08-08 23:39:19 +0000631 }
drhb86ccfb2003-01-28 23:13:10 +0000632 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
drh22c17b82015-05-15 04:13:15 +0000633 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
drhb86ccfb2003-01-28 23:13:10 +0000634 }
drh19f81f62009-06-09 18:01:37 +0000635 assert( pzErrMsg!=0 );
drh75897232000-05-29 14:26:00 +0000636 if( pParse->zErrMsg ){
drh19f81f62009-06-09 18:01:37 +0000637 *pzErrMsg = pParse->zErrMsg;
drh875ad8c2018-06-21 23:53:54 +0000638 sqlite3_log(pParse->rc, "%s in \"%s\"",
639 *pzErrMsg, pParse->zTail);
drhb86ccfb2003-01-28 23:13:10 +0000640 pParse->zErrMsg = 0;
drh4b2f9362008-01-22 23:37:09 +0000641 nErr++;
drh75897232000-05-29 14:26:00 +0000642 }
drh875ad8c2018-06-21 23:53:54 +0000643 pParse->zTail = zSql;
drh2958a4e2004-11-12 03:56:15 +0000644 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000645 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000646 pParse->pVdbe = 0;
647 }
danielk1977c00da102006-01-07 13:21:04 +0000648#ifndef SQLITE_OMIT_SHARED_CACHE
649 if( pParse->nested==0 ){
drh633e6d52008-07-28 19:34:53 +0000650 sqlite3DbFree(db, pParse->aTableLock);
danielk1977c00da102006-01-07 13:21:04 +0000651 pParse->aTableLock = 0;
652 pParse->nTableLock = 0;
653 }
654#endif
drh4f3dd152008-04-28 18:46:43 +0000655#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9755982010-07-24 16:34:37 +0000656 sqlite3_free(pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43 +0000657#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000658
danielk19777e6ebfb2006-06-12 11:24:37 +0000659 if( !IN_DECLARE_VTAB ){
660 /* If the pParse->declareVtab flag is set, do not delete any table
661 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
662 ** will take responsibility for freeing the Table structure.
663 */
dan1feeaed2010-07-23 15:41:47 +0000664 sqlite3DeleteTable(db, pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000665 }
666
drhf3c57ff2016-04-12 00:16:54 +0000667 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
drh633e6d52008-07-28 19:34:53 +0000668 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
drh9bf755c2016-12-23 03:59:31 +0000669 sqlite3DbFree(db, pParse->pVList);
drh0b9f50d2009-06-23 20:28:53 +0000670 while( pParse->pAinc ){
671 AutoincInfo *p = pParse->pAinc;
672 pParse->pAinc = p->pNext;
drhdbd6a7d2017-04-05 12:39:49 +0000673 sqlite3DbFreeNN(db, p);
drh0b9f50d2009-06-23 20:28:53 +0000674 }
drh588a9a12008-09-01 15:52:10 +0000675 while( pParse->pZombieTab ){
676 Table *p = pParse->pZombieTab;
677 pParse->pZombieTab = p->pNextZombie;
dan1feeaed2010-07-23 15:41:47 +0000678 sqlite3DeleteTable(db, p);
drh588a9a12008-09-01 15:52:10 +0000679 }
drhf3151f02015-04-16 20:27:09 +0000680 assert( nErr==0 || pParse->rc!=SQLITE_OK );
drh75897232000-05-29 14:26:00 +0000681 return nErr;
682}