xref: /sqlite-3.40.0/src/tokenize.c (revision 41ce47c4)
175897234Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
375897234Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
675897234Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
1075897234Sdrh **
1175897234Sdrh *************************************************************************
1275897234Sdrh ** An tokenizer for SQL
1375897234Sdrh **
1475897234Sdrh ** This file contains C code that splits an SQL input string up into
1575897234Sdrh ** individual tokens and sends those tokens one-by-one over to the
1675897234Sdrh ** parser for analysis.
1775897234Sdrh */
1875897234Sdrh #include "sqliteInt.h"
19dcc581ccSdrh #include <stdlib.h>
2075897234Sdrh 
2134dcee65Sdrh /* Character classes for tokenizing
2234dcee65Sdrh **
2334dcee65Sdrh ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
2434dcee65Sdrh ** using a lookup table, whereas a switch() directly on c uses a binary search.
2534dcee65Sdrh ** The lookup table is much faster.  To maximize speed, and to ensure that
2634dcee65Sdrh ** a lookup table is used, all of the classes need to be small integers and
2734dcee65Sdrh ** all of them need to be used within the switch.
2834dcee65Sdrh */
29e96f3619Sdrh #define CC_X          0    /* The letter 'x', or start of BLOB literal */
30d1032f95Sdrh #define CC_KYWD0      1    /* First letter of a keyword */
31d1032f95Sdrh #define CC_KYWD       2    /* Alphabetics or '_'.  Usable in a keyword */
328974331fSdrh #define CC_DIGIT      3    /* Digits */
338974331fSdrh #define CC_DOLLAR     4    /* '$' */
348974331fSdrh #define CC_VARALPHA   5    /* '@', '#', ':'.  Alphabetic SQL variables */
358974331fSdrh #define CC_VARNUM     6    /* '?'.  Numeric SQL variables */
368974331fSdrh #define CC_SPACE      7    /* Space characters */
378974331fSdrh #define CC_QUOTE      8    /* '"', '\'', or '`'.  String literals, quoted ids */
388974331fSdrh #define CC_QUOTE2     9    /* '['.   [...] style quoted ids */
398974331fSdrh #define CC_PIPE      10    /* '|'.   Bitwise OR or concatenate */
408974331fSdrh #define CC_MINUS     11    /* '-'.  Minus or SQL-style comment */
418974331fSdrh #define CC_LT        12    /* '<'.  Part of < or <= or <> */
428974331fSdrh #define CC_GT        13    /* '>'.  Part of > or >= */
438974331fSdrh #define CC_EQ        14    /* '='.  Part of = or == */
448974331fSdrh #define CC_BANG      15    /* '!'.  Part of != */
458974331fSdrh #define CC_SLASH     16    /* '/'.  / or c-style comment */
468974331fSdrh #define CC_LP        17    /* '(' */
478974331fSdrh #define CC_RP        18    /* ')' */
488974331fSdrh #define CC_SEMI      19    /* ';' */
498974331fSdrh #define CC_PLUS      20    /* '+' */
508974331fSdrh #define CC_STAR      21    /* '*' */
518974331fSdrh #define CC_PERCENT   22    /* '%' */
528974331fSdrh #define CC_COMMA     23    /* ',' */
538974331fSdrh #define CC_AND       24    /* '&' */
548974331fSdrh #define CC_TILDA     25    /* '~' */
558974331fSdrh #define CC_DOT       26    /* '.' */
56d1032f95Sdrh #define CC_ID        27    /* unicode characters usable in IDs */
57d1032f95Sdrh #define CC_ILLEGAL   28    /* Illegal character */
58d1032f95Sdrh #define CC_NUL       29    /* 0x00 */
59ba9ebc2dSdrh #define CC_BOM       30    /* First byte of UTF8 BOM:  0xEF 0xBB 0xBF */
608974331fSdrh 
618974331fSdrh static const unsigned char aiClass[] = {
6234dcee65Sdrh #ifdef SQLITE_ASCII
638974331fSdrh /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
64d1032f95Sdrh /* 0x */   29, 28, 28, 28, 28, 28, 28, 28, 28,  7,  7, 28,  7,  7, 28, 28,
65d1032f95Sdrh /* 1x */   28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
668974331fSdrh /* 2x */    7, 15,  8,  5,  4, 22, 24,  8, 17, 18, 21, 20, 23, 11, 26, 16,
678974331fSdrh /* 3x */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  5, 19, 12, 14, 13,  6,
688974331fSdrh /* 4x */    5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
69d1032f95Sdrh /* 5x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  2,  2,  9, 28, 28, 28,  2,
708974331fSdrh /* 6x */    8,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
71d1032f95Sdrh /* 7x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  2,  2, 28, 10, 28, 25, 28,
72ba9ebc2dSdrh /* 8x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
73ba9ebc2dSdrh /* 9x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
74ba9ebc2dSdrh /* Ax */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
75ba9ebc2dSdrh /* Bx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
76ba9ebc2dSdrh /* Cx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
77ba9ebc2dSdrh /* Dx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
78ba9ebc2dSdrh /* Ex */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
79ba9ebc2dSdrh /* Fx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
8034dcee65Sdrh #endif
8134dcee65Sdrh #ifdef SQLITE_EBCDIC
8234dcee65Sdrh /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
83d1032f95Sdrh /* 0x */   29, 28, 28, 28, 28,  7, 28, 28, 28, 28, 28, 28,  7,  7, 28, 28,
844cf34a5eSlarrybr /* 1x */   28, 28, 28, 28, 28,  7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
85d1032f95Sdrh /* 2x */   28, 28, 28, 28, 28,  7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
86d1032f95Sdrh /* 3x */   28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
87d1032f95Sdrh /* 4x */    7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
88d1032f95Sdrh /* 5x */   24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15,  4, 21, 18, 19, 28,
89d1032f95Sdrh /* 6x */   11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22,  2, 13,  6,
90d1032f95Sdrh /* 7x */   28, 28, 28, 28, 28, 28, 28, 28, 28,  8,  5,  5,  5,  8, 14,  8,
91d1032f95Sdrh /* 8x */   28,  1,  1,  1,  1,  1,  1,  1,  1,  1, 28, 28, 28, 28, 28, 28,
92d1032f95Sdrh /* 9x */   28,  1,  1,  1,  1,  1,  1,  1,  1,  1, 28, 28, 28, 28, 28, 28,
93d1032f95Sdrh /* Ax */   28, 25,  1,  1,  1,  1,  1,  0,  2,  2, 28, 28, 28, 28, 28, 28,
94d1032f95Sdrh /* Bx */   28, 28, 28, 28, 28, 28, 28, 28, 28, 28,  9, 28, 28, 28, 28, 28,
95d1032f95Sdrh /* Cx */   28,  1,  1,  1,  1,  1,  1,  1,  1,  1, 28, 28, 28, 28, 28, 28,
96d1032f95Sdrh /* Dx */   28,  1,  1,  1,  1,  1,  1,  1,  1,  1, 28, 28, 28, 28, 28, 28,
97d1032f95Sdrh /* Ex */   28, 28,  1,  1,  1,  1,  1,  0,  2,  2, 28, 28, 28, 28, 28, 28,
98d1032f95Sdrh /* Fx */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 28, 28, 28, 28, 28, 28,
9934dcee65Sdrh #endif
1008974331fSdrh };
1018974331fSdrh 
10275897234Sdrh /*
10334dcee65Sdrh ** The charMap() macro maps alphabetic characters (only) into their
1049b8f447bSdrh ** lower-case ASCII equivalent.  On ASCII machines, this is just
1059b8f447bSdrh ** an upper-to-lower case map.  On EBCDIC machines we also need
10634dcee65Sdrh ** to adjust the encoding.  The mapping is only valid for alphabetics
10734dcee65Sdrh ** which are the only characters for which this feature is used.
10834dcee65Sdrh **
10934dcee65Sdrh ** Used by keywordhash.h
1109b8f447bSdrh */
1119b8f447bSdrh #ifdef SQLITE_ASCII
1129b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
1139b8f447bSdrh #endif
1149b8f447bSdrh #ifdef SQLITE_EBCDIC
1159b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X]
1169b8f447bSdrh const unsigned char ebcdicToAscii[] = {
1179b8f447bSdrh /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
1189b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
1199b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
1209b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
1219b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
1229b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
1239b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
1249b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
1259b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
1269b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
1279b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
1289b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
1299b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
1309b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
1319b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
1329b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
1339b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
1349b8f447bSdrh };
1359b8f447bSdrh #endif
1369b8f447bSdrh 
1379b8f447bSdrh /*
13852fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if
13952fb6d71Sdrh ** it is a keyword.  If it is a keyword, the token code of that keyword is
14075897234Sdrh ** returned.  If the input is not a keyword, TK_ID is returned.
1412090a0edSdrh **
1422090a0edSdrh ** The implementation of this routine was generated by a program,
143e96f3619Sdrh ** mkkeywordhash.c, located in the tool subdirectory of the distribution.
14452fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file
14573b211abSdrh ** named keywordhash.h and then included into this source file by
14652fb6d71Sdrh ** the #include below.
14775897234Sdrh */
14873b211abSdrh #include "keywordhash.h"
14975897234Sdrh 
15040f20f7dSdrh 
15198808babSdrh /*
1529b8f447bSdrh ** If X is a character that can be used in an identifier then
1539b8f447bSdrh ** IdChar(X) will be true.  Otherwise it is false.
15498808babSdrh **
1559b8f447bSdrh ** For ASCII, any character with the high-order bit set is
1569b8f447bSdrh ** allowed in an identifier.  For 7-bit characters,
1579b8f447bSdrh ** sqlite3IsIdChar[X] must be 1.
1589b8f447bSdrh **
1599b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same
1609b8f447bSdrh ** end result.
161a0d1f663Sdrh **
162a0d1f663Sdrh ** Ticket #1066.  the SQL standard does not allow '$' in the
16360ec914cSpeter.d.reid ** middle of identifiers.  But many SQL implementations do.
164a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility.
165a0d1f663Sdrh ** But the feature is undocumented.
16698808babSdrh */
1679b8f447bSdrh #ifdef SQLITE_ASCII
168af2b5720Sdrh #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
1699b8f447bSdrh #endif
1709b8f447bSdrh #ifdef SQLITE_EBCDIC
17146c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = {
1729b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1739b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
1749b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
1759b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
1769b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
1779b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
1789b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
1799b8f447bSdrh     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
1809b8f447bSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
1819b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
1829b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
1839b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
1849b8f447bSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
1859b8f447bSdrh };
18646c99e0fSdrh #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
1879b8f447bSdrh #endif
188f5ed7ad6Sdrh 
18938d9964aSdrh /* Make the IdChar function accessible from ctime.c and alter.c */
sqlite3IsIdChar(u8 c)19097348b37Sdrh int sqlite3IsIdChar(u8 c){ return IdChar(c); }
1919b8f447bSdrh 
19234a7d790Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
19359ff4251Sdan /*
19459ff4251Sdan ** Return the id of the next token in string (*pz). Before returning, set
19559ff4251Sdan ** (*pz) to point to the byte following the parsed token.
19659ff4251Sdan */
getToken(const unsigned char ** pz)1976e2210e0Sdan static int getToken(const unsigned char **pz){
19859ff4251Sdan   const unsigned char *z = *pz;
1996e2210e0Sdan   int t;                          /* Token type to return */
2006e2210e0Sdan   do {
2016e2210e0Sdan     z += sqlite3GetToken(z, &t);
2026e2210e0Sdan   }while( t==TK_SPACE );
2036e2210e0Sdan   if( t==TK_ID
2046e2210e0Sdan    || t==TK_STRING
2056e2210e0Sdan    || t==TK_JOIN_KW
2066e2210e0Sdan    || t==TK_WINDOW
2076e2210e0Sdan    || t==TK_OVER
2086e2210e0Sdan    || sqlite3ParserFallback(t)==TK_ID
2096e2210e0Sdan   ){
2106e2210e0Sdan     t = TK_ID;
21159ff4251Sdan   }
21259ff4251Sdan   *pz = z;
2136e2210e0Sdan   return t;
21459ff4251Sdan }
21559ff4251Sdan 
21659ff4251Sdan /*
2176e2210e0Sdan ** The following three functions are called immediately after the tokenizer
2186e2210e0Sdan ** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
2196e2210e0Sdan ** whether the token should be treated as a keyword or an SQL identifier.
2206e2210e0Sdan ** This cannot be handled by the usual lemon %fallback method, due to
2216e2210e0Sdan ** the ambiguity in some constructions. e.g.
22259ff4251Sdan **
2236e2210e0Sdan **   SELECT sum(x) OVER ...
22459ff4251Sdan **
2256e2210e0Sdan ** In the above, "OVER" might be a keyword, or it might be an alias for the
2266e2210e0Sdan ** sum(x) expression. If a "%fallback ID OVER" directive were added to
2276e2210e0Sdan ** grammar, then SQLite would always treat "OVER" as an alias, making it
2286e2210e0Sdan ** impossible to call a window-function without a FILTER clause.
2296e2210e0Sdan **
2306e2210e0Sdan ** WINDOW is treated as a keyword if:
2316e2210e0Sdan **
2326e2210e0Sdan **   * the following token is an identifier, or a keyword that can fallback
2336e2210e0Sdan **     to being an identifier, and
2346e2210e0Sdan **   * the token after than one is TK_AS.
2356e2210e0Sdan **
2366e2210e0Sdan ** OVER is a keyword if:
2376e2210e0Sdan **
2386e2210e0Sdan **   * the previous token was TK_RP, and
2396e2210e0Sdan **   * the next token is either TK_LP or an identifier.
2406e2210e0Sdan **
2416e2210e0Sdan ** FILTER is a keyword if:
2426e2210e0Sdan **
2436e2210e0Sdan **   * the previous token was TK_RP, and
2446e2210e0Sdan **   * the next token is TK_LP.
24559ff4251Sdan */
analyzeWindowKeyword(const unsigned char * z)246769309fcSdan static int analyzeWindowKeyword(const unsigned char *z){
24759ff4251Sdan   int t;
2486e2210e0Sdan   t = getToken(&z);
2496e2210e0Sdan   if( t!=TK_ID ) return TK_ID;
2506e2210e0Sdan   t = getToken(&z);
2516e2210e0Sdan   if( t!=TK_AS ) return TK_ID;
2526e2210e0Sdan   return TK_WINDOW;
25359ff4251Sdan }
analyzeOverKeyword(const unsigned char * z,int lastToken)2546e2210e0Sdan static int analyzeOverKeyword(const unsigned char *z, int lastToken){
2556e2210e0Sdan   if( lastToken==TK_RP ){
2566e2210e0Sdan     int t = getToken(&z);
2576e2210e0Sdan     if( t==TK_LP || t==TK_ID ) return TK_OVER;
25859ff4251Sdan   }
2596e2210e0Sdan   return TK_ID;
2606e2210e0Sdan }
analyzeFilterKeyword(const unsigned char * z,int lastToken)2616e2210e0Sdan static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
2626e2210e0Sdan   if( lastToken==TK_RP && getToken(&z)==TK_LP ){
2636e2210e0Sdan     return TK_FILTER;
2646e2210e0Sdan   }
2656e2210e0Sdan   return TK_ID;
26659ff4251Sdan }
267c7bf5716Sdrh #endif /* SQLITE_OMIT_WINDOWFUNC */
26898808babSdrh 
26975897234Sdrh /*
270e96f3619Sdrh ** Return the length (in bytes) of the token that begins at z[0].
27161b487d0Sdrh ** Store the token type in *tokenType before returning.
27275897234Sdrh */
sqlite3GetToken(const unsigned char * z,int * tokenType)27335b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){
274aa756b09Sdrh   int i, c;
275e96f3619Sdrh   switch( aiClass[*z] ){  /* Switch on the character-class of the first byte
276e96f3619Sdrh                           ** of the token. See the comment on the CC_ defines
277e96f3619Sdrh                           ** above. */
2788974331fSdrh     case CC_SPACE: {
27919f81f6cSdrh       testcase( z[0]==' ' );
28019f81f6cSdrh       testcase( z[0]=='\t' );
28119f81f6cSdrh       testcase( z[0]=='\n' );
28219f81f6cSdrh       testcase( z[0]=='\f' );
28319f81f6cSdrh       testcase( z[0]=='\r' );
28478ca0e7eSdanielk1977       for(i=1; sqlite3Isspace(z[i]); i++){}
28575897234Sdrh       *tokenType = TK_SPACE;
28675897234Sdrh       return i;
28775897234Sdrh     }
2888974331fSdrh     case CC_MINUS: {
28975897234Sdrh       if( z[1]=='-' ){
290aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
291d3479b94Sdrh         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
29275897234Sdrh         return i;
293d5326c33Sdrh       }else if( z[1]=='>' ){
294d5326c33Sdrh         *tokenType = TK_PTR;
295d5326c33Sdrh         return 2 + (z[2]=='>');
29675897234Sdrh       }
29775897234Sdrh       *tokenType = TK_MINUS;
29875897234Sdrh       return 1;
29975897234Sdrh     }
3008974331fSdrh     case CC_LP: {
30175897234Sdrh       *tokenType = TK_LP;
30275897234Sdrh       return 1;
30375897234Sdrh     }
3048974331fSdrh     case CC_RP: {
30575897234Sdrh       *tokenType = TK_RP;
30675897234Sdrh       return 1;
30775897234Sdrh     }
3088974331fSdrh     case CC_SEMI: {
30975897234Sdrh       *tokenType = TK_SEMI;
31075897234Sdrh       return 1;
31175897234Sdrh     }
3128974331fSdrh     case CC_PLUS: {
31375897234Sdrh       *tokenType = TK_PLUS;
31475897234Sdrh       return 1;
31575897234Sdrh     }
3168974331fSdrh     case CC_STAR: {
31775897234Sdrh       *tokenType = TK_STAR;
31875897234Sdrh       return 1;
31975897234Sdrh     }
3208974331fSdrh     case CC_SLASH: {
32166105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
32275897234Sdrh         *tokenType = TK_SLASH;
32375897234Sdrh         return 1;
32475897234Sdrh       }
325aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
326aa756b09Sdrh       if( c ) i++;
327d3479b94Sdrh       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
32866105a8eSdrh       return i;
32966105a8eSdrh     }
3308974331fSdrh     case CC_PERCENT: {
331bf4133cbSdrh       *tokenType = TK_REM;
332bf4133cbSdrh       return 1;
333bf4133cbSdrh     }
3348974331fSdrh     case CC_EQ: {
33575897234Sdrh       *tokenType = TK_EQ;
33675897234Sdrh       return 1 + (z[1]=='=');
33775897234Sdrh     }
3388974331fSdrh     case CC_LT: {
339aa756b09Sdrh       if( (c=z[1])=='=' ){
34075897234Sdrh         *tokenType = TK_LE;
34175897234Sdrh         return 2;
342aa756b09Sdrh       }else if( c=='>' ){
34375897234Sdrh         *tokenType = TK_NE;
34475897234Sdrh         return 2;
345aa756b09Sdrh       }else if( c=='<' ){
346bf4133cbSdrh         *tokenType = TK_LSHIFT;
347bf4133cbSdrh         return 2;
34875897234Sdrh       }else{
34975897234Sdrh         *tokenType = TK_LT;
35075897234Sdrh         return 1;
35175897234Sdrh       }
35275897234Sdrh     }
3538974331fSdrh     case CC_GT: {
354aa756b09Sdrh       if( (c=z[1])=='=' ){
35575897234Sdrh         *tokenType = TK_GE;
35675897234Sdrh         return 2;
357aa756b09Sdrh       }else if( c=='>' ){
358bf4133cbSdrh         *tokenType = TK_RSHIFT;
359bf4133cbSdrh         return 2;
36075897234Sdrh       }else{
36175897234Sdrh         *tokenType = TK_GT;
36275897234Sdrh         return 1;
36375897234Sdrh       }
36475897234Sdrh     }
3658974331fSdrh     case CC_BANG: {
36675897234Sdrh       if( z[1]!='=' ){
36775897234Sdrh         *tokenType = TK_ILLEGAL;
368b2bddbbcSdrh         return 1;
36975897234Sdrh       }else{
37075897234Sdrh         *tokenType = TK_NE;
37175897234Sdrh         return 2;
37275897234Sdrh       }
37375897234Sdrh     }
3748974331fSdrh     case CC_PIPE: {
3750040077dSdrh       if( z[1]!='|' ){
376bf4133cbSdrh         *tokenType = TK_BITOR;
3770040077dSdrh         return 1;
3780040077dSdrh       }else{
3790040077dSdrh         *tokenType = TK_CONCAT;
3800040077dSdrh         return 2;
3810040077dSdrh       }
3820040077dSdrh     }
3838974331fSdrh     case CC_COMMA: {
38475897234Sdrh       *tokenType = TK_COMMA;
38575897234Sdrh       return 1;
38675897234Sdrh     }
3878974331fSdrh     case CC_AND: {
388bf4133cbSdrh       *tokenType = TK_BITAND;
389bf4133cbSdrh       return 1;
390bf4133cbSdrh     }
3918974331fSdrh     case CC_TILDA: {
392bf4133cbSdrh       *tokenType = TK_BITNOT;
393bf4133cbSdrh       return 1;
394bf4133cbSdrh     }
3958974331fSdrh     case CC_QUOTE: {
39675897234Sdrh       int delim = z[0];
39719f81f6cSdrh       testcase( delim=='`' );
39819f81f6cSdrh       testcase( delim=='\'' );
39919f81f6cSdrh       testcase( delim=='"' );
400aa756b09Sdrh       for(i=1; (c=z[i])!=0; i++){
401aa756b09Sdrh         if( c==delim ){
40275897234Sdrh           if( z[i+1]==delim ){
40375897234Sdrh             i++;
40475897234Sdrh           }else{
40575897234Sdrh             break;
40675897234Sdrh           }
40775897234Sdrh         }
40875897234Sdrh       }
409a33cb5f7Sdrh       if( c=='\'' ){
41075897234Sdrh         *tokenType = TK_STRING;
411eef8b558Sdrh         return i+1;
412a33cb5f7Sdrh       }else if( c!=0 ){
413a33cb5f7Sdrh         *tokenType = TK_ID;
414a33cb5f7Sdrh         return i+1;
415eef8b558Sdrh       }else{
416eef8b558Sdrh         *tokenType = TK_ILLEGAL;
41775897234Sdrh         return i;
41875897234Sdrh       }
419eef8b558Sdrh     }
4208974331fSdrh     case CC_DOT: {
4217681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
42278ca0e7eSdanielk1977       if( !sqlite3Isdigit(z[1]) )
4237681618cSdrh #endif
4247681618cSdrh       {
42575897234Sdrh         *tokenType = TK_DOT;
42675897234Sdrh         return 1;
42775897234Sdrh       }
4287681618cSdrh       /* If the next character is a digit, this is a floating point
4297681618cSdrh       ** number that begins with ".".  Fall thru into the next case */
43008b92086Sdrh       /* no break */ deliberate_fall_through
4317681618cSdrh     }
4328974331fSdrh     case CC_DIGIT: {
43319f81f6cSdrh       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
43419f81f6cSdrh       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
43519f81f6cSdrh       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
43619f81f6cSdrh       testcase( z[0]=='9' );
437c837e709Sdrh       *tokenType = TK_INTEGER;
43828e048c6Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER
43928e048c6Sdrh       if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
44028e048c6Sdrh         for(i=3; sqlite3Isxdigit(z[i]); i++){}
44128e048c6Sdrh         return i;
44228e048c6Sdrh       }
44328e048c6Sdrh #endif
44478ca0e7eSdanielk1977       for(i=0; sqlite3Isdigit(z[i]); i++){}
445b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
4467681618cSdrh       if( z[i]=='.' ){
4477681618cSdrh         i++;
44878ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
449c837e709Sdrh         *tokenType = TK_FLOAT;
450c837e709Sdrh       }
45175897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
45278ca0e7eSdanielk1977            ( sqlite3Isdigit(z[i+1])
45378ca0e7eSdanielk1977             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
45475897234Sdrh            )
45575897234Sdrh       ){
45675897234Sdrh         i += 2;
45778ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
45875897234Sdrh         *tokenType = TK_FLOAT;
45975897234Sdrh       }
460b7f9164eSdrh #endif
46167dd9011Sdrh       while( IdChar(z[i]) ){
46267dd9011Sdrh         *tokenType = TK_ILLEGAL;
46367dd9011Sdrh         i++;
46467dd9011Sdrh       }
46575897234Sdrh       return i;
46675897234Sdrh     }
4678974331fSdrh     case CC_QUOTE2: {
468aa756b09Sdrh       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
4694b2f9368Sdrh       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
4702f4392ffSdrh       return i;
4712f4392ffSdrh     }
4728974331fSdrh     case CC_VARNUM: {
47350457896Sdrh       *tokenType = TK_VARIABLE;
47478ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
475fa6bc000Sdrh       return i;
47650457896Sdrh     }
4778974331fSdrh     case CC_DOLLAR:
4788974331fSdrh     case CC_VARALPHA: {
479895d7472Sdrh       int n = 0;
480f59b12fbSdrh       testcase( z[0]=='$' );  testcase( z[0]=='@' );
481f59b12fbSdrh       testcase( z[0]==':' );  testcase( z[0]=='#' );
482288d37f1Sdrh       *tokenType = TK_VARIABLE;
483895d7472Sdrh       for(i=1; (c=z[i])!=0; i++){
484288d37f1Sdrh         if( IdChar(c) ){
485895d7472Sdrh           n++;
486288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
487895d7472Sdrh         }else if( c=='(' && n>0 ){
488895d7472Sdrh           do{
489895d7472Sdrh             i++;
49078ca0e7eSdanielk1977           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
491895d7472Sdrh           if( c==')' ){
492895d7472Sdrh             i++;
493895d7472Sdrh           }else{
494895d7472Sdrh             *tokenType = TK_ILLEGAL;
495895d7472Sdrh           }
496895d7472Sdrh           break;
497895d7472Sdrh         }else if( c==':' && z[i+1]==':' ){
498895d7472Sdrh           i++;
499288d37f1Sdrh #endif
500895d7472Sdrh         }else{
501895d7472Sdrh           break;
502895d7472Sdrh         }
503895d7472Sdrh       }
5049d74b4c5Sdrh       if( n==0 ) *tokenType = TK_ILLEGAL;
505895d7472Sdrh       return i;
506895d7472Sdrh     }
507d1032f95Sdrh     case CC_KYWD0: {
508e96f3619Sdrh       for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
509e96f3619Sdrh       if( IdChar(z[i]) ){
510e96f3619Sdrh         /* This token started out using characters that can appear in keywords,
511e96f3619Sdrh         ** but z[i] is a character not allowed within keywords, so this must
512e96f3619Sdrh         ** be an identifier instead */
513e96f3619Sdrh         i++;
514e96f3619Sdrh         break;
515e96f3619Sdrh       }
516e96f3619Sdrh       *tokenType = TK_ID;
517769309fcSdan       return keywordCode((char*)z, i, tokenType);
518e96f3619Sdrh     }
5198974331fSdrh     case CC_X: {
520a73086ddSdan #ifndef SQLITE_OMIT_BLOB_LITERAL
52119f81f6cSdrh       testcase( z[0]=='x' ); testcase( z[0]=='X' );
5224b2f9368Sdrh       if( z[1]=='\'' ){
5233fd0a736Sdanielk1977         *tokenType = TK_BLOB;
5243a61a5a2Sdrh         for(i=2; sqlite3Isxdigit(z[i]); i++){}
5253a61a5a2Sdrh         if( z[i]!='\'' || i%2 ){
5263fd0a736Sdanielk1977           *tokenType = TK_ILLEGAL;
5273a61a5a2Sdrh           while( z[i] && z[i]!='\'' ){ i++; }
5283fd0a736Sdanielk1977         }
5293a61a5a2Sdrh         if( z[i] ) i++;
530c572ef7fSdanielk1977         return i;
531c572ef7fSdanielk1977       }
532a73086ddSdan #endif
533e96f3619Sdrh       /* If it is not a BLOB literal, then it must be an ID, since no
534e96f3619Sdrh       ** SQL keywords start with the letter 'x'.  Fall through */
53508b92086Sdrh       /* no break */ deliberate_fall_through
536c572ef7fSdanielk1977     }
537d1032f95Sdrh     case CC_KYWD:
5388974331fSdrh     case CC_ID: {
5398974331fSdrh       i = 1;
54098808babSdrh       break;
54198808babSdrh     }
542ba9ebc2dSdrh     case CC_BOM: {
543ba9ebc2dSdrh       if( z[1]==0xbb && z[2]==0xbf ){
544ba9ebc2dSdrh         *tokenType = TK_SPACE;
545ba9ebc2dSdrh         return 3;
546ba9ebc2dSdrh       }
547ba9ebc2dSdrh       i = 1;
548ba9ebc2dSdrh       break;
549ba9ebc2dSdrh     }
5502b5f1526Sdan     case CC_NUL: {
5512b5f1526Sdan       *tokenType = TK_ILLEGAL;
5522b5f1526Sdan       return 0;
5532b5f1526Sdan     }
5548974331fSdrh     default: {
55575897234Sdrh       *tokenType = TK_ILLEGAL;
55675897234Sdrh       return 1;
55775897234Sdrh     }
5588974331fSdrh   }
55934dcee65Sdrh   while( IdChar(z[i]) ){ i++; }
5608974331fSdrh   *tokenType = TK_ID;
5618974331fSdrh   return i;
5628974331fSdrh }
56375897234Sdrh 
56475897234Sdrh /*
56554bc6381Sdrh ** Run the parser on the given SQL string.
56675897234Sdrh */
sqlite3RunParser(Parse * pParse,const char * zSql)56754bc6381Sdrh int sqlite3RunParser(Parse *pParse, const char *zSql){
568d9da78a2Sdrh   int nErr = 0;                   /* Number of errors encountered */
569d9da78a2Sdrh   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
5709b38d666Sdrh   int n = 0;                      /* Length of the next token token */
571d9da78a2Sdrh   int tokenType;                  /* type of the next token */
572d9da78a2Sdrh   int lastTokenParsed = -1;       /* type of the previous token */
573d9da78a2Sdrh   sqlite3 *db = pParse->db;       /* The database connection */
574d9da78a2Sdrh   int mxSqlLen;                   /* Max length of an SQL string */
575488b5585Sdan   Parse *pParentParse = 0;        /* Outer parse context, if any */
576d26cc541Sdrh #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
57753b2459aSdrh   yyParser sEngine;    /* Space to hold the Lemon-generated Parser object */
578d26cc541Sdrh #endif
5792b454e03Sdrh   VVA_ONLY( u8 startedWithOom = db->mallocFailed );
58075897234Sdrh 
58196c707a3Sdrh   assert( zSql!=0 );
582d9da78a2Sdrh   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
5834f7d3a5fSdrh   if( db->nVdbeActive==0 ){
584892edb69Sdan     AtomicStore(&db->u1.isInterrupted, 0);
58515ca1df1Sdrh   }
5864c504391Sdrh   pParse->rc = SQLITE_OK;
587e7f3f3eeSdrh   pParse->zTail = zSql;
5889b747068Sdrh #ifdef SQLITE_DEBUG
5899b747068Sdrh   if( db->flags & SQLITE_ParserTrace ){
5909b747068Sdrh     printf("parser: [[[%s]]]\n", zSql);
5919b747068Sdrh     sqlite3ParserTrace(stdout, "parser: ");
5929b747068Sdrh   }else{
5939b747068Sdrh     sqlite3ParserTrace(0, 0);
5949b747068Sdrh   }
5959b747068Sdrh #endif
596d26cc541Sdrh #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
59753b2459aSdrh   pEngine = &sEngine;
598fb32c44eSdrh   sqlite3ParserInit(pEngine, pParse);
599d26cc541Sdrh #else
600fb32c44eSdrh   pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
60175897234Sdrh   if( pEngine==0 ){
6024a642b60Sdrh     sqlite3OomFault(db);
603fad3039cSmistachkin     return SQLITE_NOMEM_BKPT;
60475897234Sdrh   }
605d26cc541Sdrh #endif
606fa6bc000Sdrh   assert( pParse->pNewTable==0 );
607fa6bc000Sdrh   assert( pParse->pNewTrigger==0 );
608fa6bc000Sdrh   assert( pParse->nVar==0 );
6099bf755ccSdrh   assert( pParse->pVList==0 );
610488b5585Sdan   pParentParse = db->pParse;
6111cf19758Sdrh   db->pParse = pParse;
612167fbbe1Sdrh   while( 1 ){
6139b38d666Sdrh     n = sqlite3GetToken((u8*)zSql, &tokenType);
6149b38d666Sdrh     mxSqlLen -= n;
6159b38d666Sdrh     if( mxSqlLen<0 ){
616e5c941b8Sdrh       pParse->rc = SQLITE_TOOBIG;
617cd9e8630Sdrh       pParse->nErr++;
618e5c941b8Sdrh       break;
619e5c941b8Sdrh     }
62034a7d790Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
62134a7d790Sdan     if( tokenType>=TK_WINDOW ){
6226e2210e0Sdan       assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
62334a7d790Sdan            || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
62434a7d790Sdan       );
62534a7d790Sdan #else
626d437ac0cSdan     if( tokenType>=TK_SPACE ){
627d437ac0cSdan       assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
628c7bf5716Sdrh #endif /* SQLITE_OMIT_WINDOWFUNC */
629892edb69Sdan       if( AtomicLoad(&db->u1.isInterrupted) ){
630d437ac0cSdan         pParse->rc = SQLITE_INTERRUPT;
63154bc6381Sdrh         pParse->nErr++;
632d437ac0cSdan         break;
633d437ac0cSdan       }
634d437ac0cSdan       if( tokenType==TK_SPACE ){
635d437ac0cSdan         zSql += n;
636d437ac0cSdan         continue;
637d437ac0cSdan       }
638d437ac0cSdan       if( zSql[0]==0 ){
639167fbbe1Sdrh         /* Upon reaching the end of input, call the parser two more times
640167fbbe1Sdrh         ** with tokens TK_SEMI and 0, in that order. */
641167fbbe1Sdrh         if( lastTokenParsed==TK_SEMI ){
642167fbbe1Sdrh           tokenType = 0;
643167fbbe1Sdrh         }else if( lastTokenParsed==0 ){
644167fbbe1Sdrh           break;
645167fbbe1Sdrh         }else{
646167fbbe1Sdrh           tokenType = TK_SEMI;
647167fbbe1Sdrh         }
6486116ee4eSdrh         n = 0;
64934a7d790Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
65034a7d790Sdan       }else if( tokenType==TK_WINDOW ){
6516e2210e0Sdan         assert( n==6 );
65234a7d790Sdan         tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
6536e2210e0Sdan       }else if( tokenType==TK_OVER ){
6546e2210e0Sdan         assert( n==4 );
6556e2210e0Sdan         tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
6566e2210e0Sdan       }else if( tokenType==TK_FILTER ){
6576e2210e0Sdan         assert( n==6 );
6586e2210e0Sdan         tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
659c7bf5716Sdrh #endif /* SQLITE_OMIT_WINDOWFUNC */
660d437ac0cSdan       }else{
6614e532958Sdrh         Token x;
6624e532958Sdrh         x.z = zSql;
6634e532958Sdrh         x.n = n;
6644e532958Sdrh         sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x);
66575897234Sdrh         break;
66675897234Sdrh       }
667d437ac0cSdan     }
6689b38d666Sdrh     pParse->sLastToken.z = zSql;
6699b38d666Sdrh     pParse->sLastToken.n = n;
670fb32c44eSdrh     sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
6710739e45bSdrh     lastTokenParsed = tokenType;
6729b38d666Sdrh     zSql += n;
6731cf19758Sdrh     assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
6741cf19758Sdrh     if( pParse->rc!=SQLITE_OK ) break;
67575897234Sdrh   }
6760be0cf67Sdrh   assert( nErr==0 );
677ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH
678af89fe66Sdrh   sqlite3_mutex_enter(sqlite3MallocMutex());
679b02392e6Sdrh   sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
680ec424a5bSdrh       sqlite3ParserStackPeak(pEngine)
681ec424a5bSdrh   );
682af89fe66Sdrh   sqlite3_mutex_leave(sqlite3MallocMutex());
683ec424a5bSdrh #endif /* YYDEBUG */
684d26cc541Sdrh #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
685d26cc541Sdrh   sqlite3ParserFinalize(pEngine);
686d26cc541Sdrh #else
68717435752Sdrh   sqlite3ParserFree(pEngine, sqlite3_free);
688d26cc541Sdrh #endif
68917435752Sdrh   if( db->mallocFailed ){
690fad3039cSmistachkin     pParse->rc = SQLITE_NOMEM_BKPT;
69171c697efSdrh   }
69216118265Sdrh   if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
69316118265Sdrh     if( pParse->zErrMsg==0 ){
69422c17b8bSdrh       pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
695b86ccfb2Sdrh     }
69654bc6381Sdrh     sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
6974b2f9368Sdrh     nErr++;
69875897234Sdrh   }
699875ad8ceSdrh   pParse->zTail = zSql;
7004f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
701b975598eSdrh   sqlite3_free(pParse->apVtabLock);
7024f3dd150Sdrh #endif
7037e6ebfb2Sdanielk1977 
70416118265Sdrh   if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
7057e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
7067e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
7077e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
7087e6ebfb2Sdanielk1977     */
7091feeaed2Sdan     sqlite3DeleteTable(db, pParse->pNewTable);
7107e6ebfb2Sdanielk1977   }
71116118265Sdrh   if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
7125496d6a2Sdan     sqlite3DeleteTrigger(db, pParse->pNewTrigger);
7135496d6a2Sdan   }
714*41ce47c4Sdrh   if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
715488b5585Sdan   db->pParse = pParentParse;
716f3151f0aSdrh   assert( nErr==0 || pParse->rc!=SQLITE_OK );
71775897234Sdrh   return nErr;
71875897234Sdrh }
719643d855dSdrh 
720643d855dSdrh 
721643d855dSdrh #ifdef SQLITE_ENABLE_NORMALIZE
722643d855dSdrh /*
723643d855dSdrh ** Insert a single space character into pStr if the current string
724643d855dSdrh ** ends with an identifier
725643d855dSdrh */
726643d855dSdrh static void addSpaceSeparator(sqlite3_str *pStr){
727643d855dSdrh   if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
728643d855dSdrh     sqlite3_str_append(pStr, " ", 1);
729643d855dSdrh   }
730643d855dSdrh }
731643d855dSdrh 
732643d855dSdrh /*
733643d855dSdrh ** Compute a normalization of the SQL given by zSql[0..nSql-1].  Return
734643d855dSdrh ** the normalization in space obtained from sqlite3DbMalloc().  Or return
735643d855dSdrh ** NULL if anything goes wrong or if zSql is NULL.
736643d855dSdrh */
737643d855dSdrh char *sqlite3Normalize(
738643d855dSdrh   Vdbe *pVdbe,       /* VM being reprepared */
7391a6c2b1dSdrh   const char *zSql   /* The original SQL string */
740643d855dSdrh ){
741643d855dSdrh   sqlite3 *db;       /* The database connection */
742643d855dSdrh   int i;             /* Next unread byte of zSql[] */
743643d855dSdrh   int n;             /* length of current token */
744643d855dSdrh   int tokenType;     /* type of current token */
745844b9004Smistachkin   int prevType = 0;  /* Previous non-whitespace token */
746643d855dSdrh   int nParen;        /* Number of nested levels of parentheses */
747643d855dSdrh   int iStartIN;      /* Start of RHS of IN operator in z[] */
748643d855dSdrh   int nParenAtIN;    /* Value of nParent at start of RHS of IN operator */
74916fd04cdSmistachkin   u32 j;             /* Bytes of normalized SQL generated so far */
750643d855dSdrh   sqlite3_str *pStr; /* The normalized SQL string under construction */
751643d855dSdrh 
752643d855dSdrh   db = sqlite3VdbeDb(pVdbe);
753643d855dSdrh   tokenType = -1;
754643d855dSdrh   nParen = iStartIN = nParenAtIN = 0;
755643d855dSdrh   pStr = sqlite3_str_new(db);
7561a6c2b1dSdrh   assert( pStr!=0 );  /* sqlite3_str_new() never returns NULL */
7571a6c2b1dSdrh   for(i=0; zSql[i] && pStr->accError==0; i+=n){
758643d855dSdrh     if( tokenType!=TK_SPACE ){
759643d855dSdrh       prevType = tokenType;
760643d855dSdrh     }
761643d855dSdrh     n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
762643d855dSdrh     if( NEVER(n<=0) ) break;
763643d855dSdrh     switch( tokenType ){
764643d855dSdrh       case TK_SPACE: {
765643d855dSdrh         break;
766643d855dSdrh       }
767643d855dSdrh       case TK_NULL: {
768643d855dSdrh         if( prevType==TK_IS || prevType==TK_NOT ){
769643d855dSdrh           sqlite3_str_append(pStr, " NULL", 5);
770643d855dSdrh           break;
771643d855dSdrh         }
772643d855dSdrh         /* Fall through */
773643d855dSdrh       }
774643d855dSdrh       case TK_STRING:
775643d855dSdrh       case TK_INTEGER:
776643d855dSdrh       case TK_FLOAT:
777643d855dSdrh       case TK_VARIABLE:
778643d855dSdrh       case TK_BLOB: {
779643d855dSdrh         sqlite3_str_append(pStr, "?", 1);
780643d855dSdrh         break;
781643d855dSdrh       }
782643d855dSdrh       case TK_LP: {
783643d855dSdrh         nParen++;
784643d855dSdrh         if( prevType==TK_IN ){
785643d855dSdrh           iStartIN = pStr->nChar;
786643d855dSdrh           nParenAtIN = nParen;
787643d855dSdrh         }
788643d855dSdrh         sqlite3_str_append(pStr, "(", 1);
789643d855dSdrh         break;
790643d855dSdrh       }
791643d855dSdrh       case TK_RP: {
792643d855dSdrh         if( iStartIN>0 && nParen==nParenAtIN ){
79316fd04cdSmistachkin           assert( pStr->nChar>=(u32)iStartIN );
794643d855dSdrh           pStr->nChar = iStartIN+1;
795643d855dSdrh           sqlite3_str_append(pStr, "?,?,?", 5);
796643d855dSdrh           iStartIN = 0;
797643d855dSdrh         }
798643d855dSdrh         nParen--;
799643d855dSdrh         sqlite3_str_append(pStr, ")", 1);
800643d855dSdrh         break;
801643d855dSdrh       }
802643d855dSdrh       case TK_ID: {
8039042ff21Sdrh         iStartIN = 0;
804643d855dSdrh         j = pStr->nChar;
805643d855dSdrh         if( sqlite3Isquote(zSql[i]) ){
806643d855dSdrh           char *zId = sqlite3DbStrNDup(db, zSql+i, n);
807643d855dSdrh           int nId;
808643d855dSdrh           int eType = 0;
809643d855dSdrh           if( zId==0 ) break;
810643d855dSdrh           sqlite3Dequote(zId);
811643d855dSdrh           if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
812643d855dSdrh             sqlite3_str_append(pStr, "?", 1);
813643d855dSdrh             sqlite3DbFree(db, zId);
814643d855dSdrh             break;
815643d855dSdrh           }
816643d855dSdrh           nId = sqlite3Strlen30(zId);
817643d855dSdrh           if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
818643d855dSdrh             addSpaceSeparator(pStr);
819643d855dSdrh             sqlite3_str_append(pStr, zId, nId);
820643d855dSdrh           }else{
821643d855dSdrh             sqlite3_str_appendf(pStr, "\"%w\"", zId);
822643d855dSdrh           }
823643d855dSdrh           sqlite3DbFree(db, zId);
824643d855dSdrh         }else{
825643d855dSdrh           addSpaceSeparator(pStr);
826643d855dSdrh           sqlite3_str_append(pStr, zSql+i, n);
827643d855dSdrh         }
828643d855dSdrh         while( j<pStr->nChar ){
829643d855dSdrh           pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
830643d855dSdrh           j++;
831643d855dSdrh         }
832643d855dSdrh         break;
833643d855dSdrh       }
8349042ff21Sdrh       case TK_SELECT: {
8359042ff21Sdrh         iStartIN = 0;
8369042ff21Sdrh         /* fall through */
8379042ff21Sdrh       }
838643d855dSdrh       default: {
839643d855dSdrh         if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
840643d855dSdrh         j = pStr->nChar;
841643d855dSdrh         sqlite3_str_append(pStr, zSql+i, n);
842643d855dSdrh         while( j<pStr->nChar ){
843643d855dSdrh           pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
844643d855dSdrh           j++;
845643d855dSdrh         }
846643d855dSdrh         break;
847643d855dSdrh       }
848643d855dSdrh     }
849643d855dSdrh   }
8509042ff21Sdrh   if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
851643d855dSdrh   return sqlite3_str_finish(pStr);
852643d855dSdrh }
853643d855dSdrh #endif /* SQLITE_ENABLE_NORMALIZE */
854