xref: /sqlite-3.40.0/src/tokenize.c (revision ef5ecb41)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
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.
10 **
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.
17 **
18 ** $Id: tokenize.c,v 1.76 2004/05/31 23:56:43 danielk1977 Exp $
19 */
20 #include "sqliteInt.h"
21 #include "os.h"
22 #include <ctype.h>
23 #include <stdlib.h>
24 
25 /*
26 ** All the keywords of the SQL language are stored as in a hash
27 ** table composed of instances of the following structure.
28 */
29 typedef struct Keyword Keyword;
30 struct Keyword {
31   char *zName;             /* The keyword name */
32   u8 tokenType;            /* Token value for this keyword */
33   u8 len;                  /* Length of this keyword */
34   u8 iNext;                /* Index in aKeywordTable[] of next with same hash */
35 };
36 
37 /*
38 ** These are the keywords
39 */
40 static Keyword aKeywordTable[] = {
41   { "ABORT",             TK_ABORT,        },
42   { "AFTER",             TK_AFTER,        },
43   { "ALL",               TK_ALL,          },
44   { "AND",               TK_AND,          },
45   { "AS",                TK_AS,           },
46   { "ASC",               TK_ASC,          },
47   { "ATTACH",            TK_ATTACH,       },
48   { "BEFORE",            TK_BEFORE,       },
49   { "BEGIN",             TK_BEGIN,        },
50   { "BETWEEN",           TK_BETWEEN,      },
51   { "BY",                TK_BY,           },
52   { "CASCADE",           TK_CASCADE,      },
53   { "CASE",              TK_CASE,         },
54   { "CHECK",             TK_CHECK,        },
55   { "CLUSTER",           TK_CLUSTER,      },
56   { "COLLATE",           TK_COLLATE,      },
57   { "COMMIT",            TK_COMMIT,       },
58   { "CONFLICT",          TK_CONFLICT,     },
59   { "CONSTRAINT",        TK_CONSTRAINT,   },
60   { "CREATE",            TK_CREATE,       },
61   { "CROSS",             TK_JOIN_KW,      },
62   { "DATABASE",          TK_DATABASE,     },
63   { "DEFAULT",           TK_DEFAULT,      },
64   { "DEFERRED",          TK_DEFERRED,     },
65   { "DEFERRABLE",        TK_DEFERRABLE,   },
66   { "DELETE",            TK_DELETE,       },
67   { "DESC",              TK_DESC,         },
68   { "DETACH",            TK_DETACH,       },
69   { "DISTINCT",          TK_DISTINCT,     },
70   { "DROP",              TK_DROP,         },
71   { "END",               TK_END,          },
72   { "EACH",              TK_EACH,         },
73   { "ELSE",              TK_ELSE,         },
74   { "EXCEPT",            TK_EXCEPT,       },
75   { "EXPLAIN",           TK_EXPLAIN,      },
76   { "FAIL",              TK_FAIL,         },
77   { "FOR",               TK_FOR,          },
78   { "FOREIGN",           TK_FOREIGN,      },
79   { "FROM",              TK_FROM,         },
80   { "FULL",              TK_JOIN_KW,      },
81   { "GLOB",              TK_GLOB,         },
82   { "GROUP",             TK_GROUP,        },
83   { "HAVING",            TK_HAVING,       },
84   { "IGNORE",            TK_IGNORE,       },
85   { "IMMEDIATE",         TK_IMMEDIATE,    },
86   { "IN",                TK_IN,           },
87   { "INDEX",             TK_INDEX,        },
88   { "INITIALLY",         TK_INITIALLY,    },
89   { "INNER",             TK_JOIN_KW,      },
90   { "INSERT",            TK_INSERT,       },
91   { "INSTEAD",           TK_INSTEAD,      },
92   { "INTERSECT",         TK_INTERSECT,    },
93   { "INTO",              TK_INTO,         },
94   { "IS",                TK_IS,           },
95   { "ISNULL",            TK_ISNULL,       },
96   { "JOIN",              TK_JOIN,         },
97   { "KEY",               TK_KEY,          },
98   { "LEFT",              TK_JOIN_KW,      },
99   { "LIKE",              TK_LIKE,         },
100   { "LIMIT",             TK_LIMIT,        },
101   { "MATCH",             TK_MATCH,        },
102   { "NATURAL",           TK_JOIN_KW,      },
103   { "NOT",               TK_NOT,          },
104   { "NOTNULL",           TK_NOTNULL,      },
105   { "NULL",              TK_NULL,         },
106   { "OF",                TK_OF,           },
107   { "OFFSET",            TK_OFFSET,       },
108   { "ON",                TK_ON,           },
109   { "OR",                TK_OR,           },
110   { "ORDER",             TK_ORDER,        },
111   { "OUTER",             TK_JOIN_KW,      },
112   { "PRAGMA",            TK_PRAGMA,       },
113   { "PRIMARY",           TK_PRIMARY,      },
114   { "RAISE",             TK_RAISE,        },
115   { "REFERENCES",        TK_REFERENCES,   },
116   { "REPLACE",           TK_REPLACE,      },
117   { "RESTRICT",          TK_RESTRICT,     },
118   { "RIGHT",             TK_JOIN_KW,      },
119   { "ROLLBACK",          TK_ROLLBACK,     },
120   { "ROW",               TK_ROW,          },
121   { "SELECT",            TK_SELECT,       },
122   { "SET",               TK_SET,          },
123   { "STATEMENT",         TK_STATEMENT,    },
124   { "TABLE",             TK_TABLE,        },
125   { "TEMP",              TK_TEMP,         },
126   { "TEMPORARY",         TK_TEMP,         },
127   { "THEN",              TK_THEN,         },
128   { "TRANSACTION",       TK_TRANSACTION,  },
129   { "TRIGGER",           TK_TRIGGER,      },
130   { "UNION",             TK_UNION,        },
131   { "UNIQUE",            TK_UNIQUE,       },
132   { "UPDATE",            TK_UPDATE,       },
133   { "USING",             TK_USING,        },
134   { "VACUUM",            TK_VACUUM,       },
135   { "VALUES",            TK_VALUES,       },
136   { "VIEW",              TK_VIEW,         },
137   { "WHEN",              TK_WHEN,         },
138   { "WHERE",             TK_WHERE,        },
139 };
140 
141 /*
142 ** This is the hash table
143 */
144 #define KEY_HASH_SIZE 101
145 static u8 aiHashTable[KEY_HASH_SIZE];
146 
147 
148 /*
149 ** This function looks up an identifier to determine if it is a
150 ** keyword.  If it is a keyword, the token code of that keyword is
151 ** returned.  If the input is not a keyword, TK_ID is returned.
152 */
153 int sqlite3KeywordCode(const char *z, int n){
154   int h, i;
155   Keyword *p;
156   static char needInit = 1;
157   if( needInit ){
158     /* Initialize the keyword hash table */
159     sqlite3OsEnterMutex();
160     if( needInit ){
161       int nk;
162       nk = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
163       for(i=0; i<nk; i++){
164         aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
165         h = sqlite3HashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
166         h %= KEY_HASH_SIZE;
167         aKeywordTable[i].iNext = aiHashTable[h];
168         aiHashTable[h] = i+1;
169       }
170       needInit = 0;
171     }
172     sqlite3OsLeaveMutex();
173   }
174   h = sqlite3HashNoCase(z, n) % KEY_HASH_SIZE;
175   for(i=aiHashTable[h]; i; i=p->iNext){
176     p = &aKeywordTable[i-1];
177     if( p->len==n && sqlite3StrNICmp(p->zName, z, n)==0 ){
178       return p->tokenType;
179     }
180   }
181   return TK_ID;
182 }
183 
184 
185 /*
186 ** If X is a character that can be used in an identifier and
187 ** X&0x80==0 then isIdChar[X] will be 1.  If X&0x80==0x80 then
188 ** X is always an identifier character.  (Hence all UTF-8
189 ** characters can be part of an identifier).  isIdChar[X] will
190 ** be 0 for every character in the lower 128 ASCII characters
191 ** that cannot be used as part of an identifier.
192 **
193 ** In this implementation, an identifier can be a string of
194 ** alphabetic characters, digits, and "_" plus any character
195 ** with the high-order bit set.  The latter rule means that
196 ** any sequence of UTF-8 characters or characters taken from
197 ** an extended ISO8859 character set can form an identifier.
198 */
199 static const char isIdChar[] = {
200 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
201     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
202     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
203     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
204     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
205     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
206     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
207     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
208     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
209 };
210 
211 
212 /*
213 ** Return the length of the token that begins at z[0].
214 ** Store the token type in *tokenType before returning.
215 */
216 static int sqliteGetToken(const unsigned char *z, int *tokenType){
217   int i;
218   switch( *z ){
219     case ' ': case '\t': case '\n': case '\f': case '\r': {
220       for(i=1; isspace(z[i]); i++){}
221       *tokenType = TK_SPACE;
222       return i;
223     }
224     case '-': {
225       if( z[1]=='-' ){
226         for(i=2; z[i] && z[i]!='\n'; i++){}
227         *tokenType = TK_COMMENT;
228         return i;
229       }
230       *tokenType = TK_MINUS;
231       return 1;
232     }
233     case '(': {
234       *tokenType = TK_LP;
235       return 1;
236     }
237     case ')': {
238       *tokenType = TK_RP;
239       return 1;
240     }
241     case ';': {
242       *tokenType = TK_SEMI;
243       return 1;
244     }
245     case '+': {
246       *tokenType = TK_PLUS;
247       return 1;
248     }
249     case '*': {
250       *tokenType = TK_STAR;
251       return 1;
252     }
253     case '/': {
254       if( z[1]!='*' || z[2]==0 ){
255         *tokenType = TK_SLASH;
256         return 1;
257       }
258       for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
259       if( z[i] ) i++;
260       *tokenType = TK_COMMENT;
261       return i;
262     }
263     case '%': {
264       *tokenType = TK_REM;
265       return 1;
266     }
267     case '=': {
268       *tokenType = TK_EQ;
269       return 1 + (z[1]=='=');
270     }
271     case '<': {
272       if( z[1]=='=' ){
273         *tokenType = TK_LE;
274         return 2;
275       }else if( z[1]=='>' ){
276         *tokenType = TK_NE;
277         return 2;
278       }else if( z[1]=='<' ){
279         *tokenType = TK_LSHIFT;
280         return 2;
281       }else{
282         *tokenType = TK_LT;
283         return 1;
284       }
285     }
286     case '>': {
287       if( z[1]=='=' ){
288         *tokenType = TK_GE;
289         return 2;
290       }else if( z[1]=='>' ){
291         *tokenType = TK_RSHIFT;
292         return 2;
293       }else{
294         *tokenType = TK_GT;
295         return 1;
296       }
297     }
298     case '!': {
299       if( z[1]!='=' ){
300         *tokenType = TK_ILLEGAL;
301         return 2;
302       }else{
303         *tokenType = TK_NE;
304         return 2;
305       }
306     }
307     case '|': {
308       if( z[1]!='|' ){
309         *tokenType = TK_BITOR;
310         return 1;
311       }else{
312         *tokenType = TK_CONCAT;
313         return 2;
314       }
315     }
316     case ',': {
317       *tokenType = TK_COMMA;
318       return 1;
319     }
320     case '&': {
321       *tokenType = TK_BITAND;
322       return 1;
323     }
324     case '~': {
325       *tokenType = TK_BITNOT;
326       return 1;
327     }
328     case '\'': case '"': {
329       int delim = z[0];
330       for(i=1; z[i]; i++){
331         if( z[i]==delim ){
332           if( z[i+1]==delim ){
333             i++;
334           }else{
335             break;
336           }
337         }
338       }
339       if( z[i] ) i++;
340       *tokenType = TK_STRING;
341       return i;
342     }
343     case '.': {
344       *tokenType = TK_DOT;
345       return 1;
346     }
347     case '0': case '1': case '2': case '3': case '4':
348     case '5': case '6': case '7': case '8': case '9': {
349       *tokenType = TK_INTEGER;
350       for(i=1; isdigit(z[i]); i++){}
351       if( z[i]=='.' && isdigit(z[i+1]) ){
352         i += 2;
353         while( isdigit(z[i]) ){ i++; }
354         *tokenType = TK_FLOAT;
355       }
356       if( (z[i]=='e' || z[i]=='E') &&
357            ( isdigit(z[i+1])
358             || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
359            )
360       ){
361         i += 2;
362         while( isdigit(z[i]) ){ i++; }
363         *tokenType = TK_FLOAT;
364       }
365       return i;
366     }
367     case '[': {
368       for(i=1; z[i] && z[i-1]!=']'; i++){}
369       *tokenType = TK_ID;
370       return i;
371     }
372     case '?': {
373       *tokenType = TK_VARIABLE;
374       return 1;
375     }
376     case 'x': case 'X': {
377       if( z[1]=='\'' || z[1]=='"' ){
378         int delim = z[1];
379         *tokenType = TK_BLOB;
380         for(i=2; z[i]; i++){
381           if( z[i]==delim ){
382             if( i%2 ) *tokenType = TK_ILLEGAL;
383             break;
384           }
385           if( !isxdigit(z[i]) ){
386             *tokenType = TK_ILLEGAL;
387             return i;
388           }
389         }
390         if( z[i] ) i++;
391         return i;
392       }
393       /* Otherwise fall through to the next case */
394     }
395     default: {
396       if( (*z&0x80)==0 && !isIdChar[*z] ){
397         break;
398       }
399       for(i=1; (z[i]&0x80)!=0 || isIdChar[z[i]]; i++){}
400       *tokenType = sqlite3KeywordCode((char*)z, i);
401       return i;
402     }
403   }
404   *tokenType = TK_ILLEGAL;
405   return 1;
406 }
407 
408 /*
409 ** Run the parser on the given SQL string.  The parser structure is
410 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
411 ** and pzErrMsg!=NULL then an error message might be written into
412 ** memory obtained from malloc() and *pzErrMsg made to point to that
413 ** error message.  Or maybe not.
414 */
415 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
416   int nErr = 0;
417   int i;
418   void *pEngine;
419   int tokenType;
420   int lastTokenParsed = -1;
421   sqlite *db = pParse->db;
422   extern void *sqlite3ParserAlloc(void*(*)(int));
423   extern void sqlite3ParserFree(void*, void(*)(void*));
424   extern int sqlite3Parser(void*, int, Token, Parse*);
425 
426   db->flags &= ~SQLITE_Interrupt;
427   pParse->rc = SQLITE_OK;
428   i = 0;
429   pEngine = sqlite3ParserAlloc((void*(*)(int))malloc);
430   if( pEngine==0 ){
431     sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
432     return 1;
433   }
434   pParse->sLastToken.dyn = 0;
435   pParse->zTail = zSql;
436   while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){
437     assert( i>=0 );
438     pParse->sLastToken.z = &zSql[i];
439     assert( pParse->sLastToken.dyn==0 );
440     pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType);
441     i += pParse->sLastToken.n;
442     switch( tokenType ){
443       case TK_SPACE:
444       case TK_COMMENT: {
445         if( (db->flags & SQLITE_Interrupt)!=0 ){
446           pParse->rc = SQLITE_INTERRUPT;
447           sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
448           goto abort_parse;
449         }
450         break;
451       }
452       case TK_ILLEGAL: {
453         sqlite3SetNString(pzErrMsg, "unrecognized token: \"", -1,
454            pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0);
455         nErr++;
456         goto abort_parse;
457       }
458       case TK_SEMI: {
459         pParse->zTail = &zSql[i];
460         /* Fall thru into the default case */
461       }
462       default: {
463         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
464         lastTokenParsed = tokenType;
465         if( pParse->rc!=SQLITE_OK ){
466           goto abort_parse;
467         }
468         break;
469       }
470     }
471   }
472 abort_parse:
473   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
474     if( lastTokenParsed!=TK_SEMI ){
475       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
476       pParse->zTail = &zSql[i];
477     }
478     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
479   }
480   sqlite3ParserFree(pEngine, free);
481   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
482     sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc),
483                     (char*)0);
484   }
485   if( pParse->zErrMsg ){
486     if( pzErrMsg && *pzErrMsg==0 ){
487       *pzErrMsg = pParse->zErrMsg;
488     }else{
489       sqliteFree(pParse->zErrMsg);
490     }
491     pParse->zErrMsg = 0;
492     if( !nErr ) nErr++;
493   }
494   if( pParse->pVdbe && pParse->nErr>0 ){
495     sqlite3VdbeDelete(pParse->pVdbe);
496     pParse->pVdbe = 0;
497   }
498   if( pParse->pNewTable ){
499     sqlite3DeleteTable(pParse->db, pParse->pNewTable);
500     pParse->pNewTable = 0;
501   }
502   if( pParse->pNewTrigger ){
503     sqlite3DeleteTrigger(pParse->pNewTrigger);
504     pParse->pNewTrigger = 0;
505   }
506   if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
507     pParse->rc = SQLITE_ERROR;
508   }
509   return nErr;
510 }
511 
512 /*
513 ** Token types used by the sqlite3_complete() routine.  See the header
514 ** comments on that procedure for additional information.
515 */
516 #define tkEXPLAIN 0
517 #define tkCREATE  1
518 #define tkTEMP    2
519 #define tkTRIGGER 3
520 #define tkEND     4
521 #define tkSEMI    5
522 #define tkWS      6
523 #define tkOTHER   7
524 
525 /*
526 ** Return TRUE if the given SQL string ends in a semicolon.
527 **
528 ** Special handling is require for CREATE TRIGGER statements.
529 ** Whenever the CREATE TRIGGER keywords are seen, the statement
530 ** must end with ";END;".
531 **
532 ** This implementation uses a state machine with 7 states:
533 **
534 **   (0) START     At the beginning or end of an SQL statement.  This routine
535 **                 returns 1 if it ends in the START state and 0 if it ends
536 **                 in any other state.
537 **
538 **   (1) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
539 **                 a statement.
540 **
541 **   (2) CREATE    The keyword CREATE has been seen at the beginning of a
542 **                 statement, possibly preceeded by EXPLAIN and/or followed by
543 **                 TEMP or TEMPORARY
544 **
545 **   (3) NORMAL    We are in the middle of statement which ends with a single
546 **                 semicolon.
547 **
548 **   (4) TRIGGER   We are in the middle of a trigger definition that must be
549 **                 ended by a semicolon, the keyword END, and another semicolon.
550 **
551 **   (5) SEMI      We've seen the first semicolon in the ";END;" that occurs at
552 **                 the end of a trigger definition.
553 **
554 **   (6) END       We've seen the ";END" of the ";END;" that occurs at the end
555 **                 of a trigger difinition.
556 **
557 ** Transitions between states above are determined by tokens extracted
558 ** from the input.  The following tokens are significant:
559 **
560 **   (0) tkEXPLAIN   The "explain" keyword.
561 **   (1) tkCREATE    The "create" keyword.
562 **   (2) tkTEMP      The "temp" or "temporary" keyword.
563 **   (3) tkTRIGGER   The "trigger" keyword.
564 **   (4) tkEND       The "end" keyword.
565 **   (5) tkSEMI      A semicolon.
566 **   (6) tkWS        Whitespace
567 **   (7) tkOTHER     Any other SQL token.
568 **
569 ** Whitespace never causes a state transition and is always ignored.
570 */
571 int sqlite3_complete(const char *zSql){
572   u8 state = 0;   /* Current state, using numbers defined in header comment */
573   u8 token;       /* Value of the next token */
574 
575   /* The following matrix defines the transition from one state to another
576   ** according to what token is seen.  trans[state][token] returns the
577   ** next state.
578   */
579   static const u8 trans[7][8] = {
580                      /* Token:                                                */
581      /* State:       **  EXPLAIN  CREATE  TEMP  TRIGGER  END  SEMI  WS  OTHER */
582      /* 0   START: */ {       1,      2,    3,       3,   3,    0,  0,     3, },
583      /* 1 EXPLAIN: */ {       3,      2,    3,       3,   3,    0,  1,     3, },
584      /* 2  CREATE: */ {       3,      3,    2,       4,   3,    0,  2,     3, },
585      /* 3  NORMAL: */ {       3,      3,    3,       3,   3,    0,  3,     3, },
586      /* 4 TRIGGER: */ {       4,      4,    4,       4,   4,    5,  4,     4, },
587      /* 5    SEMI: */ {       4,      4,    4,       4,   6,    5,  5,     4, },
588      /* 6     END: */ {       4,      4,    4,       4,   4,    0,  6,     4, },
589   };
590 
591   while( *zSql ){
592     switch( *zSql ){
593       case ';': {  /* A semicolon */
594         token = tkSEMI;
595         break;
596       }
597       case ' ':
598       case '\r':
599       case '\t':
600       case '\n':
601       case '\f': {  /* White space is ignored */
602         token = tkWS;
603         break;
604       }
605       case '/': {   /* C-style comments */
606         if( zSql[1]!='*' ){
607           token = tkOTHER;
608           break;
609         }
610         zSql += 2;
611         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
612         if( zSql[0]==0 ) return 0;
613         zSql++;
614         token = tkWS;
615         break;
616       }
617       case '-': {   /* SQL-style comments from "--" to end of line */
618         if( zSql[1]!='-' ){
619           token = tkOTHER;
620           break;
621         }
622         while( *zSql && *zSql!='\n' ){ zSql++; }
623         if( *zSql==0 ) return state==0;
624         token = tkWS;
625         break;
626       }
627       case '[': {   /* Microsoft-style identifiers in [...] */
628         zSql++;
629         while( *zSql && *zSql!=']' ){ zSql++; }
630         if( *zSql==0 ) return 0;
631         token = tkOTHER;
632         break;
633       }
634       case '"':     /* single- and double-quoted strings */
635       case '\'': {
636         int c = *zSql;
637         zSql++;
638         while( *zSql && *zSql!=c ){ zSql++; }
639         if( *zSql==0 ) return 0;
640         token = tkOTHER;
641         break;
642       }
643       default: {
644         if( isIdChar[(u8)*zSql] ){
645           /* Keywords and unquoted identifiers */
646           int nId;
647           for(nId=1; isIdChar[(u8)zSql[nId]]; nId++){}
648           switch( *zSql ){
649             case 'c': case 'C': {
650               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
651                 token = tkCREATE;
652               }else{
653                 token = tkOTHER;
654               }
655               break;
656             }
657             case 't': case 'T': {
658               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
659                 token = tkTRIGGER;
660               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
661                 token = tkTEMP;
662               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
663                 token = tkTEMP;
664               }else{
665                 token = tkOTHER;
666               }
667               break;
668             }
669             case 'e':  case 'E': {
670               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
671                 token = tkEND;
672               }else if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
673                 token = tkEXPLAIN;
674               }else{
675                 token = tkOTHER;
676               }
677               break;
678             }
679             default: {
680               token = tkOTHER;
681               break;
682             }
683           }
684           zSql += nId-1;
685         }else{
686           /* Operators and special symbols */
687           token = tkOTHER;
688         }
689         break;
690       }
691     }
692     state = trans[state][token];
693     zSql++;
694   }
695   return state==0;
696 }
697 
698 /*
699 ** This routine is the same as the sqlite3_complete() routine described
700 ** above, except that the parameter is required to be UTF-16 encoded, not
701 ** UTF-8.
702 */
703 int sqlite3_complete16(const void *zSql){
704   int rc;
705   char *zSql8 = sqlite3utf16to8(zSql, -1, SQLITE_BIGENDIAN);
706   if( !zSql8 ) return 0;
707   rc = sqlite3_complete(zSql8);
708   sqliteFree(zSql8);
709   return rc;
710 }
711