4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** 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.
11 *************************************************************************
12 ** An tokenizer for SQL
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.
20 #include "sqliteInt.h"
26 ** The charMap() macro maps alphabetic characters into their
27 ** lower-case ASCII equivalent. On ASCII machines, this is just
28 ** an upper-to-lower case map. On EBCDIC machines we also need
29 ** to adjust the encoding. Only alphabetic characters and underscores
30 ** need to be translated.
33 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
36 # define charMap(X) ebcdicToAscii[(unsigned char)X]
37 const unsigned char ebcdicToAscii[] = {
38 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
47 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
48 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
49 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
51 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
52 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
53 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
59 ** The sqlite3KeywordCode function looks up an identifier to determine if
60 ** it is a keyword. If it is a keyword, the token code of that keyword is
61 ** returned. If the input is not a keyword, TK_ID is returned.
63 ** The implementation of this routine was generated by a program,
64 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
65 ** The output of the mkkeywordhash.c program is written into a file
66 ** named keywordhash.h and then included into this source file by
67 ** the #include below.
69 #include "keywordhash.h"
73 ** If X is a character that can be used in an identifier then
74 ** IdChar(X) will be true. Otherwise it is false.
76 ** For ASCII, any character with the high-order bit set is
77 ** allowed in an identifier. For 7-bit characters,
78 ** sqlite3IsIdChar[X] must be 1.
80 ** For EBCDIC, the rules are more complex but have the same
83 ** Ticket #1066. the SQL standard does not allow '$' in the
84 ** middle of identfiers. But many SQL implementations do.
85 ** SQLite will allow '$' in identifiers for compatibility.
86 ** But the feature is undocumented.
89 const char sqlite3IsIdChar[] = {
90 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
91 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
92 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
93 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
95 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
98 #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20]))
101 const char sqlite3IsIdChar[] = {
102 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
103 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
104 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
105 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
106 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
107 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
108 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
109 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
111 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
112 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
113 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
116 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsIdChar[c-0x40]))
121 ** Return the length of the token that begins at z[0].
122 ** Store the token type in *tokenType before returning.
124 static int getToken(const unsigned char *z, int *tokenType){
127 case ' ': case '\t': case '\n': case '\f': case '\r': {
128 for(i=1; isspace(z[i]); i++){}
129 *tokenType = TK_SPACE;
134 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
135 *tokenType = TK_COMMENT;
138 *tokenType = TK_MINUS;
150 *tokenType = TK_SEMI;
154 *tokenType = TK_PLUS;
158 *tokenType = TK_STAR;
162 if( z[1]!='*' || z[2]==0 ){
163 *tokenType = TK_SLASH;
166 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
168 *tokenType = TK_COMMENT;
177 return 1 + (z[1]=='=');
187 *tokenType = TK_LSHIFT;
199 *tokenType = TK_RSHIFT;
208 *tokenType = TK_ILLEGAL;
217 *tokenType = TK_BITOR;
220 *tokenType = TK_CONCAT;
225 *tokenType = TK_COMMA;
229 *tokenType = TK_BITAND;
233 *tokenType = TK_BITNOT;
240 for(i=1; (c=z[i])!=0; i++){
250 *tokenType = TK_STRING;
253 *tokenType = TK_ILLEGAL;
258 #ifndef SQLITE_OMIT_FLOATING_POINT
265 /* If the next character is a digit, this is a floating point
266 ** number that begins with ".". Fall thru into the next case */
268 case '0': case '1': case '2': case '3': case '4':
269 case '5': case '6': case '7': case '8': case '9': {
270 *tokenType = TK_INTEGER;
271 for(i=0; isdigit(z[i]); i++){}
272 #ifndef SQLITE_OMIT_FLOATING_POINT
275 while( isdigit(z[i]) ){ i++; }
276 *tokenType = TK_FLOAT;
278 if( (z[i]=='e' || z[i]=='E') &&
280 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
284 while( isdigit(z[i]) ){ i++; }
285 *tokenType = TK_FLOAT;
288 while( IdChar(z[i]) ){
289 *tokenType = TK_ILLEGAL;
295 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
300 *tokenType = TK_VARIABLE;
301 for(i=1; isdigit(z[i]); i++){}
305 for(i=1; isdigit(z[i]); i++){}
307 /* Parameters of the form #NNN (where NNN is a number) are used
308 ** internally by sqlite3NestedParse. */
309 *tokenType = TK_REGISTER;
312 /* Fall through into the next case if the '#' is not followed by
313 ** a digit. Try to match #AAAA where AAAA is a parameter name. */
315 #ifndef SQLITE_OMIT_TCL_VARIABLE
318 case '@': /* For compatibility with MS SQL Server */
321 *tokenType = TK_VARIABLE;
322 for(i=1; (c=z[i])!=0; i++){
325 #ifndef SQLITE_OMIT_TCL_VARIABLE
326 }else if( c=='(' && n>0 ){
329 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
333 *tokenType = TK_ILLEGAL;
336 }else if( c==':' && z[i+1]==':' ){
343 if( n==0 ) *tokenType = TK_ILLEGAL;
346 #ifndef SQLITE_OMIT_BLOB_LITERAL
347 case 'x': case 'X': {
348 if( (c=z[1])=='\'' || c=='"' ){
350 *tokenType = TK_BLOB;
351 for(i=2; (c=z[i])!=0; i++){
353 if( i%2 ) *tokenType = TK_ILLEGAL;
357 *tokenType = TK_ILLEGAL;
364 /* Otherwise fall through to the next case */
371 for(i=1; IdChar(z[i]); i++){}
372 *tokenType = keywordCode((char*)z, i);
376 *tokenType = TK_ILLEGAL;
379 int sqlite3GetToken(const unsigned char *z, int *tokenType){
380 return getToken(z, tokenType);
384 ** The interface to the LEMON-generated parser
386 void *sqlite3ParserAlloc(void*(*)(size_t));
387 void sqlite3ParserFree(void*, void(*)(void*));
388 void sqlite3Parser(void*, int, Token, Parse*);
391 ** Run the parser on the given SQL string. The parser structure is
392 ** passed in. An SQLITE_ status code is returned. If an error occurs
393 ** and pzErrMsg!=NULL then an error message might be written into
394 ** memory obtained from malloc() and *pzErrMsg made to point to that
395 ** error message. Or maybe not.
397 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
402 int lastTokenParsed = -1;
403 sqlite3 *db = pParse->db;
405 if( db->activeVdbeCnt==0 ){
406 db->u1.isInterrupted = 0;
408 pParse->rc = SQLITE_OK;
410 pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3MallocX);
414 assert( pParse->sLastToken.dyn==0 );
415 assert( pParse->pNewTable==0 );
416 assert( pParse->pNewTrigger==0 );
417 assert( pParse->nVar==0 );
418 assert( pParse->nVarExpr==0 );
419 assert( pParse->nVarExprAlloc==0 );
420 assert( pParse->apVarExpr==0 );
421 pParse->zTail = pParse->zSql = zSql;
422 while( !sqlite3MallocFailed() && zSql[i]!=0 ){
424 pParse->sLastToken.z = (u8*)&zSql[i];
425 assert( pParse->sLastToken.dyn==0 );
426 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
427 i += pParse->sLastToken.n;
428 if( i>SQLITE_MAX_SQL_LENGTH ){
429 pParse->rc = SQLITE_TOOBIG;
435 if( db->u1.isInterrupted ){
436 pParse->rc = SQLITE_INTERRUPT;
437 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
444 sqliteFree(*pzErrMsg);
445 *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
446 &pParse->sLastToken);
452 pParse->zTail = &zSql[i];
453 /* Fall thru into the default case */
456 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
457 lastTokenParsed = tokenType;
458 if( pParse->rc!=SQLITE_OK ){
466 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
467 if( lastTokenParsed!=TK_SEMI ){
468 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
469 pParse->zTail = &zSql[i];
471 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
473 sqlite3ParserFree(pEngine, sqlite3FreeX);
474 if( sqlite3MallocFailed() ){
475 pParse->rc = SQLITE_NOMEM;
477 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
478 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
480 if( pParse->zErrMsg ){
481 if( pzErrMsg && *pzErrMsg==0 ){
482 *pzErrMsg = pParse->zErrMsg;
484 sqliteFree(pParse->zErrMsg);
489 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
490 sqlite3VdbeDelete(pParse->pVdbe);
493 #ifndef SQLITE_OMIT_SHARED_CACHE
494 if( pParse->nested==0 ){
495 sqliteFree(pParse->aTableLock);
496 pParse->aTableLock = 0;
497 pParse->nTableLock = 0;
501 if( !IN_DECLARE_VTAB ){
502 /* If the pParse->declareVtab flag is set, do not delete any table
503 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
504 ** will take responsibility for freeing the Table structure.
506 sqlite3DeleteTable(pParse->pNewTable);
509 sqlite3DeleteTrigger(pParse->pNewTrigger);
510 sqliteFree(pParse->apVarExpr);
511 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
512 pParse->rc = SQLITE_ERROR;