xref: /sqlite-3.40.0/src/tokenize.c (revision f0f258b1)
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 **
18*f0f258b1Sdrh ** $Id: tokenize.c,v 1.58 2003/04/21 18:48:47 drh Exp $
1975897234Sdrh */
2075897234Sdrh #include "sqliteInt.h"
21ad75e987Sdrh #include "os.h"
2275897234Sdrh #include <ctype.h>
23dcc581ccSdrh #include <stdlib.h>
2475897234Sdrh 
2575897234Sdrh /*
2675897234Sdrh ** All the keywords of the SQL language are stored as in a hash
2775897234Sdrh ** table composed of instances of the following structure.
2875897234Sdrh */
2975897234Sdrh typedef struct Keyword Keyword;
3075897234Sdrh struct Keyword {
3175897234Sdrh   char *zName;             /* The keyword name */
3275897234Sdrh   int len;                 /* Number of characters in the keyword */
3375897234Sdrh   int tokenType;           /* The token value for this keyword */
3475897234Sdrh   Keyword *pNext;          /* Next keyword with the same hash */
3575897234Sdrh };
3675897234Sdrh 
3775897234Sdrh /*
3875897234Sdrh ** These are the keywords
3975897234Sdrh */
4075897234Sdrh static Keyword aKeywordTable[] = {
419cfcf5d4Sdrh   { "ABORT",             0, TK_ABORT,            0 },
42c3f9bad2Sdanielk1977   { "AFTER",             0, TK_AFTER,            0 },
43fef5208cSdrh   { "ALL",               0, TK_ALL,              0 },
4475897234Sdrh   { "AND",               0, TK_AND,              0 },
4575897234Sdrh   { "AS",                0, TK_AS,               0 },
4675897234Sdrh   { "ASC",               0, TK_ASC,              0 },
47113088ecSdrh   { "ATTACH",            0, TK_ATTACH,           0 },
48c3f9bad2Sdanielk1977   { "BEFORE",            0, TK_BEFORE,           0 },
49c4a3c779Sdrh   { "BEGIN",             0, TK_BEGIN,            0 },
50fef5208cSdrh   { "BETWEEN",           0, TK_BETWEEN,          0 },
5175897234Sdrh   { "BY",                0, TK_BY,               0 },
5204738cb9Sdrh   { "CASCADE",           0, TK_CASCADE,          0 },
5317a7f8ddSdrh   { "CASE",              0, TK_CASE,             0 },
5475897234Sdrh   { "CHECK",             0, TK_CHECK,            0 },
55f57b14a6Sdrh   { "CLUSTER",           0, TK_CLUSTER,          0 },
568e2ca029Sdrh   { "COLLATE",           0, TK_COLLATE,          0 },
57c4a3c779Sdrh   { "COMMIT",            0, TK_COMMIT,           0 },
589cfcf5d4Sdrh   { "CONFLICT",          0, TK_CONFLICT,         0 },
5975897234Sdrh   { "CONSTRAINT",        0, TK_CONSTRAINT,       0 },
60982cef7eSdrh   { "COPY",              0, TK_COPY,             0 },
6175897234Sdrh   { "CREATE",            0, TK_CREATE,           0 },
625ad1a6c8Sdrh   { "CROSS",             0, TK_JOIN_KW,          0 },
63113088ecSdrh   { "DATABASE",          0, TK_DATABASE,         0 },
6475897234Sdrh   { "DEFAULT",           0, TK_DEFAULT,          0 },
6504738cb9Sdrh   { "DEFERRED",          0, TK_DEFERRED,         0 },
6604738cb9Sdrh   { "DEFERRABLE",        0, TK_DEFERRABLE,       0 },
6775897234Sdrh   { "DELETE",            0, TK_DELETE,           0 },
68982cef7eSdrh   { "DELIMITERS",        0, TK_DELIMITERS,       0 },
6975897234Sdrh   { "DESC",              0, TK_DESC,             0 },
70113088ecSdrh   { "DETACH",            0, TK_DETACH,           0 },
71efb7251dSdrh   { "DISTINCT",          0, TK_DISTINCT,         0 },
7275897234Sdrh   { "DROP",              0, TK_DROP,             0 },
73c4a3c779Sdrh   { "END",               0, TK_END,              0 },
74c3f9bad2Sdanielk1977   { "EACH",              0, TK_EACH,             0 },
7517a7f8ddSdrh   { "ELSE",              0, TK_ELSE,             0 },
7682c3d636Sdrh   { "EXCEPT",            0, TK_EXCEPT,           0 },
7775897234Sdrh   { "EXPLAIN",           0, TK_EXPLAIN,          0 },
781c92853dSdrh   { "FAIL",              0, TK_FAIL,             0 },
79c3f9bad2Sdanielk1977   { "FOR",               0, TK_FOR,              0 },
8004738cb9Sdrh   { "FOREIGN",           0, TK_FOREIGN,          0 },
8175897234Sdrh   { "FROM",              0, TK_FROM,             0 },
825ad1a6c8Sdrh   { "FULL",              0, TK_JOIN_KW,          0 },
83dce2cbe6Sdrh   { "GLOB",              0, TK_GLOB,             0 },
842282792aSdrh   { "GROUP",             0, TK_GROUP,            0 },
852282792aSdrh   { "HAVING",            0, TK_HAVING,           0 },
869cfcf5d4Sdrh   { "IGNORE",            0, TK_IGNORE,           0 },
8704738cb9Sdrh   { "IMMEDIATE",         0, TK_IMMEDIATE,        0 },
88fef5208cSdrh   { "IN",                0, TK_IN,               0 },
8975897234Sdrh   { "INDEX",             0, TK_INDEX,            0 },
9004738cb9Sdrh   { "INITIALLY",         0, TK_INITIALLY,        0 },
915ad1a6c8Sdrh   { "INNER",             0, TK_JOIN_KW,          0 },
9275897234Sdrh   { "INSERT",            0, TK_INSERT,           0 },
93c3f9bad2Sdanielk1977   { "INSTEAD",           0, TK_INSTEAD,          0 },
9482c3d636Sdrh   { "INTERSECT",         0, TK_INTERSECT,        0 },
9575897234Sdrh   { "INTO",              0, TK_INTO,             0 },
9675897234Sdrh   { "IS",                0, TK_IS,               0 },
9775897234Sdrh   { "ISNULL",            0, TK_ISNULL,           0 },
9801f3f253Sdrh   { "JOIN",              0, TK_JOIN,             0 },
9975897234Sdrh   { "KEY",               0, TK_KEY,              0 },
1005ad1a6c8Sdrh   { "LEFT",              0, TK_JOIN_KW,          0 },
101dce2cbe6Sdrh   { "LIKE",              0, TK_LIKE,             0 },
1029bbca4c1Sdrh   { "LIMIT",             0, TK_LIMIT,            0 },
10304738cb9Sdrh   { "MATCH",             0, TK_MATCH,            0 },
1045ad1a6c8Sdrh   { "NATURAL",           0, TK_JOIN_KW,          0 },
10575897234Sdrh   { "NOT",               0, TK_NOT,              0 },
10675897234Sdrh   { "NOTNULL",           0, TK_NOTNULL,          0 },
10775897234Sdrh   { "NULL",              0, TK_NULL,             0 },
108c3f9bad2Sdanielk1977   { "OF",                0, TK_OF,               0 },
1099bbca4c1Sdrh   { "OFFSET",            0, TK_OFFSET,           0 },
11075897234Sdrh   { "ON",                0, TK_ON,               0 },
11175897234Sdrh   { "OR",                0, TK_OR,               0 },
11275897234Sdrh   { "ORDER",             0, TK_ORDER,            0 },
1135ad1a6c8Sdrh   { "OUTER",             0, TK_JOIN_KW,          0 },
114f57b14a6Sdrh   { "PRAGMA",            0, TK_PRAGMA,           0 },
11575897234Sdrh   { "PRIMARY",           0, TK_PRIMARY,          0 },
1166f34903eSdanielk1977   { "RAISE",             0, TK_RAISE,            0 },
11704738cb9Sdrh   { "REFERENCES",        0, TK_REFERENCES,       0 },
1189cfcf5d4Sdrh   { "REPLACE",           0, TK_REPLACE,          0 },
11904738cb9Sdrh   { "RESTRICT",          0, TK_RESTRICT,         0 },
1205ad1a6c8Sdrh   { "RIGHT",             0, TK_JOIN_KW,          0 },
121c4a3c779Sdrh   { "ROLLBACK",          0, TK_ROLLBACK,         0 },
122c3f9bad2Sdanielk1977   { "ROW",               0, TK_ROW,              0 },
12375897234Sdrh   { "SELECT",            0, TK_SELECT,           0 },
12475897234Sdrh   { "SET",               0, TK_SET,              0 },
1251873cd50Sdrh   { "STATEMENT",         0, TK_STATEMENT,        0 },
12675897234Sdrh   { "TABLE",             0, TK_TABLE,            0 },
127f57b3399Sdrh   { "TEMP",              0, TK_TEMP,             0 },
128f57b3399Sdrh   { "TEMPORARY",         0, TK_TEMP,             0 },
12917a7f8ddSdrh   { "THEN",              0, TK_THEN,             0 },
130c4a3c779Sdrh   { "TRANSACTION",       0, TK_TRANSACTION,      0 },
131c3f9bad2Sdanielk1977   { "TRIGGER",           0, TK_TRIGGER,          0 },
13282c3d636Sdrh   { "UNION",             0, TK_UNION,            0 },
13375897234Sdrh   { "UNIQUE",            0, TK_UNIQUE,           0 },
13475897234Sdrh   { "UPDATE",            0, TK_UPDATE,           0 },
135982cef7eSdrh   { "USING",             0, TK_USING,            0 },
136dce2cbe6Sdrh   { "VACUUM",            0, TK_VACUUM,           0 },
13775897234Sdrh   { "VALUES",            0, TK_VALUES,           0 },
138a76b5dfcSdrh   { "VIEW",              0, TK_VIEW,             0 },
13917a7f8ddSdrh   { "WHEN",              0, TK_WHEN,             0 },
14075897234Sdrh   { "WHERE",             0, TK_WHERE,            0 },
14175897234Sdrh };
14275897234Sdrh 
14375897234Sdrh /*
14475897234Sdrh ** This is the hash table
14575897234Sdrh */
146daffd0e5Sdrh #define KEY_HASH_SIZE 71
14775897234Sdrh static Keyword *apHashTable[KEY_HASH_SIZE];
14875897234Sdrh 
14975897234Sdrh 
15075897234Sdrh /*
15175897234Sdrh ** This function looks up an identifier to determine if it is a
15275897234Sdrh ** keyword.  If it is a keyword, the token code of that keyword is
15375897234Sdrh ** returned.  If the input is not a keyword, TK_ID is returned.
15475897234Sdrh */
15517f71934Sdrh int sqliteKeywordCode(const char *z, int n){
15675897234Sdrh   int h;
15775897234Sdrh   Keyword *p;
15875897234Sdrh   if( aKeywordTable[0].len==0 ){
15975897234Sdrh     /* Initialize the keyword hash table */
160ad75e987Sdrh     sqliteOsEnterMutex();
161ad75e987Sdrh     if( aKeywordTable[0].len==0 ){
16275897234Sdrh       int i;
16375897234Sdrh       int n;
16475897234Sdrh       n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
16575897234Sdrh       for(i=0; i<n; i++){
16675897234Sdrh         aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
16775897234Sdrh         h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
16875897234Sdrh         h %= KEY_HASH_SIZE;
16975897234Sdrh         aKeywordTable[i].pNext = apHashTable[h];
17075897234Sdrh         apHashTable[h] = &aKeywordTable[i];
17175897234Sdrh       }
17275897234Sdrh     }
173ad75e987Sdrh     sqliteOsLeaveMutex();
174ad75e987Sdrh   }
17575897234Sdrh   h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
17675897234Sdrh   for(p=apHashTable[h]; p; p=p->pNext){
17775897234Sdrh     if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){
17875897234Sdrh       return p->tokenType;
17975897234Sdrh     }
18075897234Sdrh   }
18175897234Sdrh   return TK_ID;
18275897234Sdrh }
18375897234Sdrh 
18498808babSdrh 
18598808babSdrh /*
18698808babSdrh ** If X is a character that can be used in an identifier then
18798808babSdrh ** isIdChar[X] will be 1.  Otherwise isIdChar[X] will be 0.
18898808babSdrh **
18998808babSdrh ** In this implementation, an identifier can be a string of
19098808babSdrh ** alphabetic characters, digits, and "_" plus any character
19198808babSdrh ** with the high-order bit set.  The latter rule means that
19298808babSdrh ** any sequence of UTF-8 characters or characters taken from
19398808babSdrh ** an extended ISO8859 character set can form an identifier.
19498808babSdrh */
19598808babSdrh static const char isIdChar[] = {
19698808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
19798808babSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
19898808babSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
19998808babSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
20098808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
20198808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
20298808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
20398808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
20498808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
20598808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 8x */
20698808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 9x */
20798808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ax */
20898808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Bx */
20998808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Cx */
21098808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Dx */
21198808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ex */
21298808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Fx */
21398808babSdrh };
21498808babSdrh 
21598808babSdrh 
21675897234Sdrh /*
21775897234Sdrh ** Return the length of the token that begins at z[0].  Return
21875897234Sdrh ** -1 if the token is (or might be) incomplete.  Store the token
21975897234Sdrh ** type in *tokenType before returning.
22075897234Sdrh */
22198808babSdrh static int sqliteGetToken(const unsigned char *z, int *tokenType){
22275897234Sdrh   int i;
22375897234Sdrh   switch( *z ){
22430cab809Sdrh     case ' ': case '\t': case '\n': case '\f': case '\r': {
22532eb7b47Sdrh       for(i=1; isspace(z[i]); i++){}
22675897234Sdrh       *tokenType = TK_SPACE;
22775897234Sdrh       return i;
22875897234Sdrh     }
22975897234Sdrh     case '-': {
23075897234Sdrh       if( z[1]==0 ) return -1;
23175897234Sdrh       if( z[1]=='-' ){
23275897234Sdrh         for(i=2; z[i] && z[i]!='\n'; i++){}
23375897234Sdrh         *tokenType = TK_COMMENT;
23475897234Sdrh         return i;
23575897234Sdrh       }
23675897234Sdrh       *tokenType = TK_MINUS;
23775897234Sdrh       return 1;
23875897234Sdrh     }
23975897234Sdrh     case '(': {
2401f16230bSdrh       if( z[1]=='+' && z[2]==')' ){
2411f16230bSdrh         *tokenType = TK_ORACLE_OUTER_JOIN;
2421f16230bSdrh         return 3;
2431f16230bSdrh       }else{
24475897234Sdrh         *tokenType = TK_LP;
24575897234Sdrh         return 1;
24675897234Sdrh       }
2471f16230bSdrh     }
24875897234Sdrh     case ')': {
24975897234Sdrh       *tokenType = TK_RP;
25075897234Sdrh       return 1;
25175897234Sdrh     }
25275897234Sdrh     case ';': {
25375897234Sdrh       *tokenType = TK_SEMI;
25475897234Sdrh       return 1;
25575897234Sdrh     }
25675897234Sdrh     case '+': {
25775897234Sdrh       *tokenType = TK_PLUS;
25875897234Sdrh       return 1;
25975897234Sdrh     }
26075897234Sdrh     case '*': {
26175897234Sdrh       *tokenType = TK_STAR;
26275897234Sdrh       return 1;
26375897234Sdrh     }
26475897234Sdrh     case '/': {
26566105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
26675897234Sdrh         *tokenType = TK_SLASH;
26775897234Sdrh         return 1;
26875897234Sdrh       }
26966105a8eSdrh       for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
27066105a8eSdrh       if( z[i] ) i++;
27166105a8eSdrh       *tokenType = TK_COMMENT;
27266105a8eSdrh       return i;
27366105a8eSdrh     }
274bf4133cbSdrh     case '%': {
275bf4133cbSdrh       *tokenType = TK_REM;
276bf4133cbSdrh       return 1;
277bf4133cbSdrh     }
27875897234Sdrh     case '=': {
27975897234Sdrh       *tokenType = TK_EQ;
28075897234Sdrh       return 1 + (z[1]=='=');
28175897234Sdrh     }
28275897234Sdrh     case '<': {
28375897234Sdrh       if( z[1]=='=' ){
28475897234Sdrh         *tokenType = TK_LE;
28575897234Sdrh         return 2;
28675897234Sdrh       }else if( z[1]=='>' ){
28775897234Sdrh         *tokenType = TK_NE;
28875897234Sdrh         return 2;
289bf4133cbSdrh       }else if( z[1]=='<' ){
290bf4133cbSdrh         *tokenType = TK_LSHIFT;
291bf4133cbSdrh         return 2;
29275897234Sdrh       }else{
29375897234Sdrh         *tokenType = TK_LT;
29475897234Sdrh         return 1;
29575897234Sdrh       }
29675897234Sdrh     }
29775897234Sdrh     case '>': {
29875897234Sdrh       if( z[1]=='=' ){
29975897234Sdrh         *tokenType = TK_GE;
30075897234Sdrh         return 2;
301bf4133cbSdrh       }else if( z[1]=='>' ){
302bf4133cbSdrh         *tokenType = TK_RSHIFT;
303bf4133cbSdrh         return 2;
30475897234Sdrh       }else{
30575897234Sdrh         *tokenType = TK_GT;
30675897234Sdrh         return 1;
30775897234Sdrh       }
30875897234Sdrh     }
30975897234Sdrh     case '!': {
31075897234Sdrh       if( z[1]!='=' ){
31175897234Sdrh         *tokenType = TK_ILLEGAL;
312c837e709Sdrh         return 2;
31375897234Sdrh       }else{
31475897234Sdrh         *tokenType = TK_NE;
31575897234Sdrh         return 2;
31675897234Sdrh       }
31775897234Sdrh     }
3180040077dSdrh     case '|': {
3190040077dSdrh       if( z[1]!='|' ){
320bf4133cbSdrh         *tokenType = TK_BITOR;
3210040077dSdrh         return 1;
3220040077dSdrh       }else{
3230040077dSdrh         *tokenType = TK_CONCAT;
3240040077dSdrh         return 2;
3250040077dSdrh       }
3260040077dSdrh     }
32775897234Sdrh     case ',': {
32875897234Sdrh       *tokenType = TK_COMMA;
32975897234Sdrh       return 1;
33075897234Sdrh     }
331bf4133cbSdrh     case '&': {
332bf4133cbSdrh       *tokenType = TK_BITAND;
333bf4133cbSdrh       return 1;
334bf4133cbSdrh     }
335bf4133cbSdrh     case '~': {
336bf4133cbSdrh       *tokenType = TK_BITNOT;
337bf4133cbSdrh       return 1;
338bf4133cbSdrh     }
33975897234Sdrh     case '\'': case '"': {
34075897234Sdrh       int delim = z[0];
34175897234Sdrh       for(i=1; z[i]; i++){
34275897234Sdrh         if( z[i]==delim ){
34375897234Sdrh           if( z[i+1]==delim ){
34475897234Sdrh             i++;
34575897234Sdrh           }else{
34675897234Sdrh             break;
34775897234Sdrh           }
34875897234Sdrh         }
34975897234Sdrh       }
35075897234Sdrh       if( z[i] ) i++;
35175897234Sdrh       *tokenType = TK_STRING;
35275897234Sdrh       return i;
35375897234Sdrh     }
35475897234Sdrh     case '.': {
35575897234Sdrh       *tokenType = TK_DOT;
35675897234Sdrh       return 1;
35775897234Sdrh     }
35875897234Sdrh     case '0': case '1': case '2': case '3': case '4':
35975897234Sdrh     case '5': case '6': case '7': case '8': case '9': {
360c837e709Sdrh       *tokenType = TK_INTEGER;
36132eb7b47Sdrh       for(i=1; isdigit(z[i]); i++){}
362bb07e9a3Sdrh       if( z[i]=='.' && isdigit(z[i+1]) ){
363bb07e9a3Sdrh         i += 2;
36432eb7b47Sdrh         while( isdigit(z[i]) ){ i++; }
365c837e709Sdrh         *tokenType = TK_FLOAT;
366c837e709Sdrh       }
36775897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
36875897234Sdrh            ( isdigit(z[i+1])
36975897234Sdrh             || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
37075897234Sdrh            )
37175897234Sdrh       ){
37275897234Sdrh         i += 2;
37332eb7b47Sdrh         while( isdigit(z[i]) ){ i++; }
37475897234Sdrh         *tokenType = TK_FLOAT;
37575897234Sdrh       }
37675897234Sdrh       return i;
37775897234Sdrh     }
3782f4392ffSdrh     case '[': {
3792f4392ffSdrh       for(i=1; z[i] && z[i-1]!=']'; i++){}
3802f4392ffSdrh       *tokenType = TK_ID;
3812f4392ffSdrh       return i;
3822f4392ffSdrh     }
38398808babSdrh     default: {
38498808babSdrh       if( !isIdChar[*z] ){
38598808babSdrh         break;
38698808babSdrh       }
38798808babSdrh       for(i=1; isIdChar[z[i]]; i++){}
3886a535340Sdrh       *tokenType = sqliteKeywordCode((char*)z, i);
38975897234Sdrh       return i;
39075897234Sdrh     }
39175897234Sdrh   }
39275897234Sdrh   *tokenType = TK_ILLEGAL;
39375897234Sdrh   return 1;
39475897234Sdrh }
39575897234Sdrh 
39675897234Sdrh /*
39775897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
398b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
399b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into
400b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that
401b19a2bc6Sdrh ** error message.  Or maybe not.
40275897234Sdrh */
40380ff32f5Sdrh int sqliteRunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
40475897234Sdrh   int nErr = 0;
40575897234Sdrh   int i;
40675897234Sdrh   void *pEngine;
407b86ccfb2Sdrh   int tokenType;
408b86ccfb2Sdrh   int lastTokenParsed = -1;
4096d4abfbeSdrh   sqlite *db = pParse->db;
41075897234Sdrh   extern void *sqliteParserAlloc(void*(*)(int));
41175897234Sdrh   extern void sqliteParserFree(void*, void(*)(void*));
412338ea13cSdrh   extern int sqliteParser(void*, int, Token, Parse*);
41375897234Sdrh 
4146d4abfbeSdrh   db->flags &= ~SQLITE_Interrupt;
4154c504391Sdrh   pParse->rc = SQLITE_OK;
41675897234Sdrh   i = 0;
417982cef7eSdrh   pEngine = sqliteParserAlloc((void*(*)(int))malloc);
41875897234Sdrh   if( pEngine==0 ){
41975897234Sdrh     sqliteSetString(pzErrMsg, "out of memory", 0);
42075897234Sdrh     return 1;
42175897234Sdrh   }
42232eb7b47Sdrh   pParse->sLastToken.dyn = 0;
423326dce74Sdrh   pParse->zTail = zSql;
42432eb7b47Sdrh   while( sqlite_malloc_failed==0 && zSql[i]!=0 ){
42575897234Sdrh 
42632eb7b47Sdrh     assert( i>=0 );
42775897234Sdrh     pParse->sLastToken.z = &zSql[i];
42832eb7b47Sdrh     assert( pParse->sLastToken.dyn==0 );
42998808babSdrh     pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType);
43075897234Sdrh     i += pParse->sLastToken.n;
43175897234Sdrh     switch( tokenType ){
43275897234Sdrh       case TK_SPACE:
43375897234Sdrh       case TK_COMMENT: {
43432eb7b47Sdrh         if( (db->flags & SQLITE_Interrupt)!=0 ){
43532eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
43632eb7b47Sdrh           sqliteSetString(pzErrMsg, "interrupt", 0);
43732eb7b47Sdrh           goto abort_parse;
43832eb7b47Sdrh         }
43975897234Sdrh         break;
44075897234Sdrh       }
44132eb7b47Sdrh       case TK_ILLEGAL: {
442c837e709Sdrh         sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1,
443c837e709Sdrh            pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0);
44475897234Sdrh         nErr++;
445caec2f12Sdrh         goto abort_parse;
44632eb7b47Sdrh       }
447326dce74Sdrh       case TK_SEMI: {
448326dce74Sdrh         pParse->zTail = &zSql[i];
449326dce74Sdrh         /* Fall thru into the default case */
450326dce74Sdrh       }
45132eb7b47Sdrh       default: {
45275897234Sdrh         sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse);
453b86ccfb2Sdrh         lastTokenParsed = tokenType;
454b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
45532eb7b47Sdrh           goto abort_parse;
45675897234Sdrh         }
45775897234Sdrh         break;
45875897234Sdrh       }
45975897234Sdrh     }
46032eb7b47Sdrh   }
46132eb7b47Sdrh abort_parse:
462b86ccfb2Sdrh   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
463b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
464094b2bbfSdrh       sqliteParser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
465326dce74Sdrh       pParse->zTail = &zSql[i];
46675897234Sdrh     }
467b86ccfb2Sdrh     sqliteParser(pEngine, 0, pParse->sLastToken, pParse);
46875897234Sdrh   }
469dcc581ccSdrh   sqliteParserFree(pEngine, free);
470b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
471b86ccfb2Sdrh     sqliteSetString(&pParse->zErrMsg, sqlite_error_string(pParse->rc), 0);
472b86ccfb2Sdrh   }
47375897234Sdrh   if( pParse->zErrMsg ){
474b86ccfb2Sdrh     if( pzErrMsg && *pzErrMsg==0 ){
47575897234Sdrh       *pzErrMsg = pParse->zErrMsg;
47675897234Sdrh     }else{
47775897234Sdrh       sqliteFree(pParse->zErrMsg);
47875897234Sdrh     }
479b86ccfb2Sdrh     pParse->zErrMsg = 0;
48075897234Sdrh     if( !nErr ) nErr++;
48175897234Sdrh   }
482b86ccfb2Sdrh   if( pParse->pVdbe && (pParse->useCallback || pParse->nErr>0) ){
48375897234Sdrh     sqliteVdbeDelete(pParse->pVdbe);
48475897234Sdrh     pParse->pVdbe = 0;
48575897234Sdrh   }
48675897234Sdrh   if( pParse->pNewTable ){
48775897234Sdrh     sqliteDeleteTable(pParse->db, pParse->pNewTable);
48875897234Sdrh     pParse->pNewTable = 0;
48975897234Sdrh   }
490*f0f258b1Sdrh   if( pParse->pNewTrigger ){
491*f0f258b1Sdrh     sqliteDeleteTrigger(pParse->pNewTrigger);
492*f0f258b1Sdrh     pParse->pNewTrigger = 0;
493*f0f258b1Sdrh   }
494b86ccfb2Sdrh   if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
4954c504391Sdrh     pParse->rc = SQLITE_ERROR;
4964c504391Sdrh   }
49775897234Sdrh   return nErr;
49875897234Sdrh }
499