drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 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 | ** |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 18 | ** $Id: tokenize.c,v 1.104 2005/06/22 08:48:06 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include "sqliteInt.h" |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 21 | #include "os.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 26 | ** The sqlite3KeywordCode function looks up an identifier to determine if |
| 27 | ** it is a keyword. If it is a keyword, the token code of that keyword is |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 28 | ** returned. If the input is not a keyword, TK_ID is returned. |
drh | 2090a0e | 2004-10-07 19:03:01 +0000 | [diff] [blame] | 29 | ** |
| 30 | ** The implementation of this routine was generated by a program, |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 31 | ** mkkeywordhash.h, located in the tool subdirectory of the distribution. |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 32 | ** The output of the mkkeywordhash.c program is written into a file |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 33 | ** named keywordhash.h and then included into this source file by |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 34 | ** the #include below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 35 | */ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 36 | #include "keywordhash.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | |
drh | 40f20f7 | 2004-10-23 05:10:18 +0000 | [diff] [blame] | 38 | |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 39 | /* |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 40 | ** If X is a character that can be used in an identifier and |
| 41 | ** X&0x80==0 then isIdChar[X] will be 1. If X&0x80==0x80 then |
| 42 | ** X is always an identifier character. (Hence all UTF-8 |
| 43 | ** characters can be part of an identifier). isIdChar[X] will |
| 44 | ** be 0 for every character in the lower 128 ASCII characters |
| 45 | ** that cannot be used as part of an identifier. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 46 | ** |
| 47 | ** In this implementation, an identifier can be a string of |
| 48 | ** alphabetic characters, digits, and "_" plus any character |
| 49 | ** with the high-order bit set. The latter rule means that |
| 50 | ** any sequence of UTF-8 characters or characters taken from |
| 51 | ** an extended ISO8859 character set can form an identifier. |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 52 | ** |
| 53 | ** Ticket #1066. the SQL standard does not allow '$' in the |
| 54 | ** middle of identfiers. But many SQL implementations do. |
| 55 | ** SQLite will allow '$' in identifiers for compatibility. |
| 56 | ** But the feature is undocumented. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 57 | */ |
| 58 | static const char isIdChar[] = { |
| 59 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 60 | 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 61 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ |
| 62 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ |
| 63 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ |
| 64 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ |
| 65 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 68 | #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20])) |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 69 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 70 | /* |
drh | 61b487d | 2003-09-12 02:08:14 +0000 | [diff] [blame] | 71 | ** Return the length of the token that begins at z[0]. |
| 72 | ** Store the token type in *tokenType before returning. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 73 | */ |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 74 | static int getToken(const unsigned char *z, int *tokenType){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 75 | int i, c; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 76 | switch( *z ){ |
drh | 30cab80 | 2000-08-09 17:17:25 +0000 | [diff] [blame] | 77 | case ' ': case '\t': case '\n': case '\f': case '\r': { |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 78 | for(i=1; isspace(z[i]); i++){} |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 79 | *tokenType = TK_SPACE; |
| 80 | return i; |
| 81 | } |
| 82 | case '-': { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | if( z[1]=='-' ){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 84 | for(i=2; (c=z[i])!=0 && c!='\n'; i++){} |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 85 | *tokenType = TK_COMMENT; |
| 86 | return i; |
| 87 | } |
| 88 | *tokenType = TK_MINUS; |
| 89 | return 1; |
| 90 | } |
| 91 | case '(': { |
drh | dab3518 | 2003-09-27 13:39:38 +0000 | [diff] [blame] | 92 | *tokenType = TK_LP; |
| 93 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 94 | } |
| 95 | case ')': { |
| 96 | *tokenType = TK_RP; |
| 97 | return 1; |
| 98 | } |
| 99 | case ';': { |
| 100 | *tokenType = TK_SEMI; |
| 101 | return 1; |
| 102 | } |
| 103 | case '+': { |
| 104 | *tokenType = TK_PLUS; |
| 105 | return 1; |
| 106 | } |
| 107 | case '*': { |
| 108 | *tokenType = TK_STAR; |
| 109 | return 1; |
| 110 | } |
| 111 | case '/': { |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 112 | if( z[1]!='*' || z[2]==0 ){ |
| 113 | *tokenType = TK_SLASH; |
| 114 | return 1; |
| 115 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 116 | for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} |
| 117 | if( c ) i++; |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 118 | *tokenType = TK_COMMENT; |
| 119 | return i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 120 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 121 | case '%': { |
| 122 | *tokenType = TK_REM; |
| 123 | return 1; |
| 124 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 125 | case '=': { |
| 126 | *tokenType = TK_EQ; |
| 127 | return 1 + (z[1]=='='); |
| 128 | } |
| 129 | case '<': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 130 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 131 | *tokenType = TK_LE; |
| 132 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 133 | }else if( c=='>' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 134 | *tokenType = TK_NE; |
| 135 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 136 | }else if( c=='<' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 137 | *tokenType = TK_LSHIFT; |
| 138 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 139 | }else{ |
| 140 | *tokenType = TK_LT; |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | case '>': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 145 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 146 | *tokenType = TK_GE; |
| 147 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 148 | }else if( c=='>' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 149 | *tokenType = TK_RSHIFT; |
| 150 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 151 | }else{ |
| 152 | *tokenType = TK_GT; |
| 153 | return 1; |
| 154 | } |
| 155 | } |
| 156 | case '!': { |
| 157 | if( z[1]!='=' ){ |
| 158 | *tokenType = TK_ILLEGAL; |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 159 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 160 | }else{ |
| 161 | *tokenType = TK_NE; |
| 162 | return 2; |
| 163 | } |
| 164 | } |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 165 | case '|': { |
| 166 | if( z[1]!='|' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 167 | *tokenType = TK_BITOR; |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 168 | return 1; |
| 169 | }else{ |
| 170 | *tokenType = TK_CONCAT; |
| 171 | return 2; |
| 172 | } |
| 173 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 174 | case ',': { |
| 175 | *tokenType = TK_COMMA; |
| 176 | return 1; |
| 177 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 178 | case '&': { |
| 179 | *tokenType = TK_BITAND; |
| 180 | return 1; |
| 181 | } |
| 182 | case '~': { |
| 183 | *tokenType = TK_BITNOT; |
| 184 | return 1; |
| 185 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 186 | case '\'': case '"': { |
| 187 | int delim = z[0]; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 188 | for(i=1; (c=z[i])!=0; i++){ |
| 189 | if( c==delim ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 190 | if( z[i+1]==delim ){ |
| 191 | i++; |
| 192 | }else{ |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 197 | if( c ) i++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 198 | *tokenType = TK_STRING; |
| 199 | return i; |
| 200 | } |
| 201 | case '.': { |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 202 | *tokenType = TK_DOT; |
| 203 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 204 | } |
| 205 | case '0': case '1': case '2': case '3': case '4': |
| 206 | case '5': case '6': case '7': case '8': case '9': { |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 207 | *tokenType = TK_INTEGER; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 208 | for(i=1; isdigit(z[i]); i++){} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 209 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 210 | if( z[i]=='.' && isdigit(z[i+1]) ){ |
| 211 | i += 2; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 212 | while( isdigit(z[i]) ){ i++; } |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 213 | *tokenType = TK_FLOAT; |
| 214 | } |
| 215 | if( (z[i]=='e' || z[i]=='E') && |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | ( isdigit(z[i+1]) |
| 217 | || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) |
| 218 | ) |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 219 | ){ |
| 220 | i += 2; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 221 | while( isdigit(z[i]) ){ i++; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 222 | *tokenType = TK_FLOAT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 223 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 224 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 225 | return i; |
| 226 | } |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 227 | case '[': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 228 | for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 229 | *tokenType = TK_ID; |
| 230 | return i; |
| 231 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 232 | case '?': { |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 233 | *tokenType = TK_VARIABLE; |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 234 | for(i=1; isdigit(z[i]); i++){} |
| 235 | return i; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 236 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 237 | case '#': { |
| 238 | for(i=1; isdigit(z[i]); i++){} |
| 239 | if( i>1 ){ |
| 240 | /* Parameters of the form #NNN (where NNN is a number) are used |
| 241 | ** internally by sqlite3NestedParse. */ |
| 242 | *tokenType = TK_REGISTER; |
| 243 | return i; |
| 244 | } |
| 245 | /* Fall through into the next case if the '#' is not followed by |
| 246 | ** a digit. Try to match #AAAA where AAAA is a parameter name. */ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 247 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 248 | #ifndef SQLITE_OMIT_TCL_VARIABLE |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 249 | case '$': |
| 250 | #endif |
| 251 | case ':': { |
| 252 | int n = 0; |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 253 | *tokenType = TK_VARIABLE; |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 254 | for(i=1; (c=z[i])!=0; i++){ |
| 255 | if( IdChar(c) ){ |
| 256 | n++; |
| 257 | #ifndef SQLITE_OMIT_TCL_VARIABLE |
| 258 | }else if( c=='(' && n>0 ){ |
| 259 | do{ |
| 260 | i++; |
| 261 | }while( (c=z[i])!=0 && !isspace(c) && c!=')' ); |
| 262 | if( c==')' ){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 263 | i++; |
| 264 | }else{ |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 265 | *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 266 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 267 | break; |
| 268 | }else if( c==':' && z[i+1]==':' ){ |
| 269 | i++; |
| 270 | #endif |
| 271 | }else{ |
| 272 | break; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame^] | 275 | if( n==0 ) *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 276 | return i; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 277 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 278 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 279 | case 'x': case 'X': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 280 | if( (c=z[1])=='\'' || c=='"' ){ |
| 281 | int delim = c; |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 282 | *tokenType = TK_BLOB; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 283 | for(i=2; (c=z[i])!=0; i++){ |
| 284 | if( c==delim ){ |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 285 | if( i%2 ) *tokenType = TK_ILLEGAL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 286 | break; |
| 287 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 288 | if( !isxdigit(c) ){ |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 289 | *tokenType = TK_ILLEGAL; |
| 290 | return i; |
| 291 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 292 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 293 | if( c ) i++; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 294 | return i; |
| 295 | } |
| 296 | /* Otherwise fall through to the next case */ |
| 297 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 298 | #endif |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 299 | default: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 300 | if( !IdChar(*z) ){ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 301 | break; |
| 302 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 303 | for(i=1; IdChar(z[i]); i++){} |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 304 | *tokenType = keywordCode((char*)z, i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 305 | return i; |
| 306 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 307 | } |
| 308 | *tokenType = TK_ILLEGAL; |
| 309 | return 1; |
| 310 | } |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 311 | int sqlite3GetToken(const unsigned char *z, int *tokenType){ |
| 312 | return getToken(z, tokenType); |
| 313 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 314 | |
| 315 | /* |
| 316 | ** Run the parser on the given SQL string. The parser structure is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 317 | ** passed in. An SQLITE_ status code is returned. If an error occurs |
| 318 | ** and pzErrMsg!=NULL then an error message might be written into |
| 319 | ** memory obtained from malloc() and *pzErrMsg made to point to that |
| 320 | ** error message. Or maybe not. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 321 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 322 | int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | int nErr = 0; |
| 324 | int i; |
| 325 | void *pEngine; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 326 | int tokenType; |
| 327 | int lastTokenParsed = -1; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 328 | sqlite3 *db = pParse->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 329 | extern void *sqlite3ParserAlloc(void*(*)(int)); |
| 330 | extern void sqlite3ParserFree(void*, void(*)(void*)); |
| 331 | extern int sqlite3Parser(void*, int, Token, Parse*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 332 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 333 | db->flags &= ~SQLITE_Interrupt; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 334 | pParse->rc = SQLITE_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 335 | i = 0; |
drh | 132d8d6 | 2005-05-22 20:12:37 +0000 | [diff] [blame] | 336 | pEngine = sqlite3ParserAlloc((void*(*)(int))sqlite3MallocX); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 337 | if( pEngine==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 338 | sqlite3SetString(pzErrMsg, "out of memory", (char*)0); |
drh | defc997 | 2005-06-06 14:45:42 +0000 | [diff] [blame] | 339 | return SQLITE_NOMEM; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 341 | assert( pParse->sLastToken.dyn==0 ); |
| 342 | assert( pParse->pNewTable==0 ); |
| 343 | assert( pParse->pNewTrigger==0 ); |
| 344 | assert( pParse->nVar==0 ); |
| 345 | assert( pParse->nVarExpr==0 ); |
| 346 | assert( pParse->nVarExprAlloc==0 ); |
| 347 | assert( pParse->apVarExpr==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 348 | pParse->zTail = pParse->zSql = zSql; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 349 | while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){ |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 350 | assert( i>=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 351 | pParse->sLastToken.z = &zSql[i]; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 352 | assert( pParse->sLastToken.dyn==0 ); |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 353 | pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 354 | i += pParse->sLastToken.n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 355 | switch( tokenType ){ |
| 356 | case TK_SPACE: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 357 | case TK_COMMENT: { |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 358 | if( (db->flags & SQLITE_Interrupt)!=0 ){ |
| 359 | pParse->rc = SQLITE_INTERRUPT; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 360 | sqlite3SetString(pzErrMsg, "interrupt", (char*)0); |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 361 | goto abort_parse; |
| 362 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 365 | case TK_ILLEGAL: { |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 366 | if( pzErrMsg ){ |
| 367 | sqliteFree(*pzErrMsg); |
| 368 | *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"", |
| 369 | &pParse->sLastToken); |
| 370 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | nErr++; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 372 | goto abort_parse; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 373 | } |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 374 | case TK_SEMI: { |
| 375 | pParse->zTail = &zSql[i]; |
| 376 | /* Fall thru into the default case */ |
| 377 | } |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 378 | default: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 379 | sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 380 | lastTokenParsed = tokenType; |
| 381 | if( pParse->rc!=SQLITE_OK ){ |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 382 | goto abort_parse; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 383 | } |
| 384 | break; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 385 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 388 | abort_parse: |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 389 | if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ |
| 390 | if( lastTokenParsed!=TK_SEMI ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 391 | sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 392 | pParse->zTail = &zSql[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 394 | sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 395 | } |
drh | 132d8d6 | 2005-05-22 20:12:37 +0000 | [diff] [blame] | 396 | sqlite3ParserFree(pEngine, sqlite3FreeX); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 397 | if( sqlite3_malloc_failed ){ |
| 398 | pParse->rc = SQLITE_NOMEM; |
| 399 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 400 | if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 401 | sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), |
drh | 4174398 | 2003-12-06 21:43:55 +0000 | [diff] [blame] | 402 | (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 403 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 404 | if( pParse->zErrMsg ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 405 | if( pzErrMsg && *pzErrMsg==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 406 | *pzErrMsg = pParse->zErrMsg; |
| 407 | }else{ |
| 408 | sqliteFree(pParse->zErrMsg); |
| 409 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 410 | pParse->zErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | if( !nErr ) nErr++; |
| 412 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 413 | if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 414 | sqlite3VdbeDelete(pParse->pVdbe); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 415 | pParse->pVdbe = 0; |
| 416 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 417 | sqlite3DeleteTable(pParse->db, pParse->pNewTable); |
| 418 | sqlite3DeleteTrigger(pParse->pNewTrigger); |
| 419 | sqliteFree(pParse->apVarExpr); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 420 | if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 421 | pParse->rc = SQLITE_ERROR; |
| 422 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 423 | return nErr; |
| 424 | } |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 425 | |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 426 | /* The sqlite3_complete() API may be omitted (to save code space) by |
| 427 | ** defining the following symbol. |
| 428 | */ |
| 429 | #ifndef SQLITE_OMIT_COMPLETE |
| 430 | |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 431 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 432 | ** Token types used by the sqlite3_complete() routine. See the header |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 433 | ** comments on that procedure for additional information. |
| 434 | */ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 435 | #define tkSEMI 0 |
| 436 | #define tkWS 1 |
| 437 | #define tkOTHER 2 |
| 438 | #define tkEXPLAIN 3 |
| 439 | #define tkCREATE 4 |
| 440 | #define tkTEMP 5 |
| 441 | #define tkTRIGGER 6 |
| 442 | #define tkEND 7 |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 443 | |
| 444 | /* |
| 445 | ** Return TRUE if the given SQL string ends in a semicolon. |
| 446 | ** |
| 447 | ** Special handling is require for CREATE TRIGGER statements. |
| 448 | ** Whenever the CREATE TRIGGER keywords are seen, the statement |
| 449 | ** must end with ";END;". |
| 450 | ** |
| 451 | ** This implementation uses a state machine with 7 states: |
| 452 | ** |
| 453 | ** (0) START At the beginning or end of an SQL statement. This routine |
| 454 | ** returns 1 if it ends in the START state and 0 if it ends |
| 455 | ** in any other state. |
| 456 | ** |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 457 | ** (1) NORMAL We are in the middle of statement which ends with a single |
| 458 | ** semicolon. |
| 459 | ** |
| 460 | ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 461 | ** a statement. |
| 462 | ** |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 463 | ** (3) CREATE The keyword CREATE has been seen at the beginning of a |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 464 | ** statement, possibly preceeded by EXPLAIN and/or followed by |
| 465 | ** TEMP or TEMPORARY |
| 466 | ** |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 467 | ** (4) TRIGGER We are in the middle of a trigger definition that must be |
| 468 | ** ended by a semicolon, the keyword END, and another semicolon. |
| 469 | ** |
| 470 | ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at |
| 471 | ** the end of a trigger definition. |
| 472 | ** |
| 473 | ** (6) END We've seen the ";END" of the ";END;" that occurs at the end |
| 474 | ** of a trigger difinition. |
| 475 | ** |
| 476 | ** Transitions between states above are determined by tokens extracted |
| 477 | ** from the input. The following tokens are significant: |
| 478 | ** |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 479 | ** (0) tkSEMI A semicolon. |
| 480 | ** (1) tkWS Whitespace |
| 481 | ** (2) tkOTHER Any other SQL token. |
| 482 | ** (3) tkEXPLAIN The "explain" keyword. |
| 483 | ** (4) tkCREATE The "create" keyword. |
| 484 | ** (5) tkTEMP The "temp" or "temporary" keyword. |
| 485 | ** (6) tkTRIGGER The "trigger" keyword. |
| 486 | ** (7) tkEND The "end" keyword. |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 487 | ** |
| 488 | ** Whitespace never causes a state transition and is always ignored. |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 489 | ** |
| 490 | ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed |
| 491 | ** to recognize the end of a trigger can be omitted. All we have to do |
| 492 | ** is look for a semicolon that is not part of an string or comment. |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 493 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 494 | int sqlite3_complete(const char *zSql){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 495 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 496 | u8 token; /* Value of the next token */ |
| 497 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 498 | #ifndef SQLITE_OMIT_TRIGGER |
| 499 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 500 | ** statement. This is the normal case. |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 501 | */ |
| 502 | static const u8 trans[7][8] = { |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 503 | /* Token: */ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 504 | /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ |
| 505 | /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, }, |
| 506 | /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, }, |
| 507 | /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, }, |
| 508 | /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, }, |
| 509 | /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, }, |
| 510 | /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, }, |
| 511 | /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, }, |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 512 | }; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 513 | #else |
| 514 | /* If triggers are not suppored by this compile then the statement machine |
| 515 | ** used to detect the end of a statement is much simplier |
| 516 | */ |
| 517 | static const u8 trans[2][3] = { |
| 518 | /* Token: */ |
| 519 | /* State: ** SEMI WS OTHER */ |
| 520 | /* 0 START: */ { 0, 0, 1, }, |
| 521 | /* 1 NORMAL: */ { 0, 1, 1, }, |
| 522 | }; |
| 523 | #endif /* SQLITE_OMIT_TRIGGER */ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 524 | |
| 525 | while( *zSql ){ |
| 526 | switch( *zSql ){ |
| 527 | case ';': { /* A semicolon */ |
| 528 | token = tkSEMI; |
| 529 | break; |
| 530 | } |
| 531 | case ' ': |
| 532 | case '\r': |
| 533 | case '\t': |
| 534 | case '\n': |
| 535 | case '\f': { /* White space is ignored */ |
| 536 | token = tkWS; |
| 537 | break; |
| 538 | } |
| 539 | case '/': { /* C-style comments */ |
| 540 | if( zSql[1]!='*' ){ |
| 541 | token = tkOTHER; |
| 542 | break; |
| 543 | } |
| 544 | zSql += 2; |
| 545 | while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } |
| 546 | if( zSql[0]==0 ) return 0; |
| 547 | zSql++; |
| 548 | token = tkWS; |
| 549 | break; |
| 550 | } |
| 551 | case '-': { /* SQL-style comments from "--" to end of line */ |
| 552 | if( zSql[1]!='-' ){ |
| 553 | token = tkOTHER; |
| 554 | break; |
| 555 | } |
| 556 | while( *zSql && *zSql!='\n' ){ zSql++; } |
| 557 | if( *zSql==0 ) return state==0; |
| 558 | token = tkWS; |
| 559 | break; |
| 560 | } |
| 561 | case '[': { /* Microsoft-style identifiers in [...] */ |
| 562 | zSql++; |
| 563 | while( *zSql && *zSql!=']' ){ zSql++; } |
| 564 | if( *zSql==0 ) return 0; |
| 565 | token = tkOTHER; |
| 566 | break; |
| 567 | } |
| 568 | case '"': /* single- and double-quoted strings */ |
| 569 | case '\'': { |
| 570 | int c = *zSql; |
| 571 | zSql++; |
| 572 | while( *zSql && *zSql!=c ){ zSql++; } |
| 573 | if( *zSql==0 ) return 0; |
| 574 | token = tkOTHER; |
| 575 | break; |
| 576 | } |
| 577 | default: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 578 | int c; |
| 579 | if( IdChar((u8)*zSql) ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 580 | /* Keywords and unquoted identifiers */ |
| 581 | int nId; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 582 | for(nId=1; IdChar(zSql[nId]); nId++){} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 583 | #ifdef SQLITE_OMIT_TRIGGER |
| 584 | token = tkOTHER; |
| 585 | #else |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 586 | switch( *zSql ){ |
| 587 | case 'c': case 'C': { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 588 | if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 589 | token = tkCREATE; |
| 590 | }else{ |
| 591 | token = tkOTHER; |
| 592 | } |
| 593 | break; |
| 594 | } |
| 595 | case 't': case 'T': { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 596 | if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 597 | token = tkTRIGGER; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 598 | }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 599 | token = tkTEMP; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 600 | }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 601 | token = tkTEMP; |
| 602 | }else{ |
| 603 | token = tkOTHER; |
| 604 | } |
| 605 | break; |
| 606 | } |
| 607 | case 'e': case 'E': { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 608 | if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 609 | token = tkEND; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 610 | }else |
| 611 | #ifndef SQLITE_OMIT_EXPLAIN |
| 612 | if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 613 | token = tkEXPLAIN; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 614 | }else |
| 615 | #endif |
| 616 | { |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 617 | token = tkOTHER; |
| 618 | } |
| 619 | break; |
| 620 | } |
| 621 | default: { |
| 622 | token = tkOTHER; |
| 623 | break; |
| 624 | } |
| 625 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 626 | #endif /* SQLITE_OMIT_TRIGGER */ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 627 | zSql += nId-1; |
| 628 | }else{ |
| 629 | /* Operators and special symbols */ |
| 630 | token = tkOTHER; |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | state = trans[state][token]; |
| 636 | zSql++; |
| 637 | } |
| 638 | return state==0; |
| 639 | } |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 640 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 641 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 642 | /* |
| 643 | ** This routine is the same as the sqlite3_complete() routine described |
| 644 | ** above, except that the parameter is required to be UTF-16 encoded, not |
| 645 | ** UTF-8. |
| 646 | */ |
| 647 | int sqlite3_complete16(const void *zSql){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 648 | sqlite3_value *pVal; |
danielk1977 | 5314c4d | 2004-06-18 06:02:35 +0000 | [diff] [blame] | 649 | char const *zSql8; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 650 | int rc = 0; |
| 651 | |
| 652 | pVal = sqlite3ValueNew(); |
| 653 | sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 654 | zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 655 | if( zSql8 ){ |
| 656 | rc = sqlite3_complete(zSql8); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 657 | } |
| 658 | sqlite3ValueFree(pVal); |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 659 | return rc; |
| 660 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 661 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 662 | #endif /* SQLITE_OMIT_COMPLETE */ |