Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/sqlite/src/src/tokenize.c

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
11 ************************************************************************* 11 *************************************************************************
12 ** An tokenizer for SQL 12 ** An tokenizer for SQL
13 ** 13 **
14 ** This file contains C code that splits an SQL input string up into 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 15 ** individual tokens and sends those tokens one-by-one over to the
16 ** parser for analysis. 16 ** parser for analysis.
17 */ 17 */
18 #include "sqliteInt.h" 18 #include "sqliteInt.h"
19 #include <stdlib.h> 19 #include <stdlib.h>
20 20
21 /* Character classes for tokenizing
22 **
23 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
24 ** using a lookup table, whereas a switch() directly on c uses a binary search.
25 ** The lookup table is much faster. To maximize speed, and to ensure that
26 ** a lookup table is used, all of the classes need to be small integers and
27 ** all of them need to be used within the switch.
28 */
29 #define CC_X 0 /* The letter 'x', or start of BLOB literal */
30 #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
31 #define CC_ID 2 /* unicode characters usable in IDs */
32 #define CC_DIGIT 3 /* Digits */
33 #define CC_DOLLAR 4 /* '$' */
34 #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
35 #define CC_VARNUM 6 /* '?'. Numeric SQL variables */
36 #define CC_SPACE 7 /* Space characters */
37 #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
38 #define CC_QUOTE2 9 /* '['. [...] style quoted ids */
39 #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
40 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
41 #define CC_LT 12 /* '<'. Part of < or <= or <> */
42 #define CC_GT 13 /* '>'. Part of > or >= */
43 #define CC_EQ 14 /* '='. Part of = or == */
44 #define CC_BANG 15 /* '!'. Part of != */
45 #define CC_SLASH 16 /* '/'. / or c-style comment */
46 #define CC_LP 17 /* '(' */
47 #define CC_RP 18 /* ')' */
48 #define CC_SEMI 19 /* ';' */
49 #define CC_PLUS 20 /* '+' */
50 #define CC_STAR 21 /* '*' */
51 #define CC_PERCENT 22 /* '%' */
52 #define CC_COMMA 23 /* ',' */
53 #define CC_AND 24 /* '&' */
54 #define CC_TILDA 25 /* '~' */
55 #define CC_DOT 26 /* '.' */
56 #define CC_ILLEGAL 27 /* Illegal character */
57
58 static const unsigned char aiClass[] = {
59 #ifdef SQLITE_ASCII
60 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
61 /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
62 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
63 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
64 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
65 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
66 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
67 /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
68 /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
69 /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
70 /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73 /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
74 /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
75 /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
76 /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
77 #endif
78 #ifdef SQLITE_EBCDIC
79 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
80 /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
81 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
82 /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
83 /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
84 /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
85 /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
86 /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
87 /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
88 /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
89 /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
90 /* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
91 /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
92 /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
93 /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
94 /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
95 /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
96 #endif
97 };
98
21 /* 99 /*
22 ** The charMap() macro maps alphabetic characters into their 100 ** The charMap() macro maps alphabetic characters (only) into their
23 ** lower-case ASCII equivalent. On ASCII machines, this is just 101 ** lower-case ASCII equivalent. On ASCII machines, this is just
24 ** an upper-to-lower case map. On EBCDIC machines we also need 102 ** an upper-to-lower case map. On EBCDIC machines we also need
25 ** to adjust the encoding. Only alphabetic characters and underscores 103 ** to adjust the encoding. The mapping is only valid for alphabetics
26 ** need to be translated. 104 ** which are the only characters for which this feature is used.
105 **
106 ** Used by keywordhash.h
27 */ 107 */
28 #ifdef SQLITE_ASCII 108 #ifdef SQLITE_ASCII
29 # define charMap(X) sqlite3UpperToLower[(unsigned char)X] 109 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
30 #endif 110 #endif
31 #ifdef SQLITE_EBCDIC 111 #ifdef SQLITE_EBCDIC
32 # define charMap(X) ebcdicToAscii[(unsigned char)X] 112 # define charMap(X) ebcdicToAscii[(unsigned char)X]
33 const unsigned char ebcdicToAscii[] = { 113 const unsigned char ebcdicToAscii[] = {
34 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 114 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
(...skipping 13 matching lines...) Expand all
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ 130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
51 }; 131 };
52 #endif 132 #endif
53 133
54 /* 134 /*
55 ** The sqlite3KeywordCode function looks up an identifier to determine if 135 ** The sqlite3KeywordCode function looks up an identifier to determine if
56 ** it is a keyword. If it is a keyword, the token code of that keyword is 136 ** it is a keyword. If it is a keyword, the token code of that keyword is
57 ** returned. If the input is not a keyword, TK_ID is returned. 137 ** returned. If the input is not a keyword, TK_ID is returned.
58 ** 138 **
59 ** The implementation of this routine was generated by a program, 139 ** The implementation of this routine was generated by a program,
60 ** mkkeywordhash.h, located in the tool subdirectory of the distribution. 140 ** mkkeywordhash.c, located in the tool subdirectory of the distribution.
61 ** The output of the mkkeywordhash.c program is written into a file 141 ** The output of the mkkeywordhash.c program is written into a file
62 ** named keywordhash.h and then included into this source file by 142 ** named keywordhash.h and then included into this source file by
63 ** the #include below. 143 ** the #include below.
64 */ 144 */
65 #include "keywordhash.h" 145 #include "keywordhash.h"
66 146
67 147
68 /* 148 /*
69 ** If X is a character that can be used in an identifier then 149 ** If X is a character that can be used in an identifier then
70 ** IdChar(X) will be true. Otherwise it is false. 150 ** IdChar(X) will be true. Otherwise it is false.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) 183 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
104 #endif 184 #endif
105 185
106 /* Make the IdChar function accessible from ctime.c */ 186 /* Make the IdChar function accessible from ctime.c */
107 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 187 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108 int sqlite3IsIdChar(u8 c){ return IdChar(c); } 188 int sqlite3IsIdChar(u8 c){ return IdChar(c); }
109 #endif 189 #endif
110 190
111 191
112 /* 192 /*
113 ** Return the length of the token that begins at z[0]. 193 ** Return the length (in bytes) of the token that begins at z[0].
114 ** Store the token type in *tokenType before returning. 194 ** Store the token type in *tokenType before returning.
115 */ 195 */
116 int sqlite3GetToken(const unsigned char *z, int *tokenType){ 196 int sqlite3GetToken(const unsigned char *z, int *tokenType){
117 int i, c; 197 int i, c;
118 switch( *z ){ 198 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
119 case ' ': case '\t': case '\n': case '\f': case '\r': { 199 ** of the token. See the comment on the CC_ defines
200 ** above. */
201 case CC_SPACE: {
120 testcase( z[0]==' ' ); 202 testcase( z[0]==' ' );
121 testcase( z[0]=='\t' ); 203 testcase( z[0]=='\t' );
122 testcase( z[0]=='\n' ); 204 testcase( z[0]=='\n' );
123 testcase( z[0]=='\f' ); 205 testcase( z[0]=='\f' );
124 testcase( z[0]=='\r' ); 206 testcase( z[0]=='\r' );
125 for(i=1; sqlite3Isspace(z[i]); i++){} 207 for(i=1; sqlite3Isspace(z[i]); i++){}
126 *tokenType = TK_SPACE; 208 *tokenType = TK_SPACE;
127 return i; 209 return i;
128 } 210 }
129 case '-': { 211 case CC_MINUS: {
130 if( z[1]=='-' ){ 212 if( z[1]=='-' ){
131 for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 213 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
132 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 214 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
133 return i; 215 return i;
134 } 216 }
135 *tokenType = TK_MINUS; 217 *tokenType = TK_MINUS;
136 return 1; 218 return 1;
137 } 219 }
138 case '(': { 220 case CC_LP: {
139 *tokenType = TK_LP; 221 *tokenType = TK_LP;
140 return 1; 222 return 1;
141 } 223 }
142 case ')': { 224 case CC_RP: {
143 *tokenType = TK_RP; 225 *tokenType = TK_RP;
144 return 1; 226 return 1;
145 } 227 }
146 case ';': { 228 case CC_SEMI: {
147 *tokenType = TK_SEMI; 229 *tokenType = TK_SEMI;
148 return 1; 230 return 1;
149 } 231 }
150 case '+': { 232 case CC_PLUS: {
151 *tokenType = TK_PLUS; 233 *tokenType = TK_PLUS;
152 return 1; 234 return 1;
153 } 235 }
154 case '*': { 236 case CC_STAR: {
155 *tokenType = TK_STAR; 237 *tokenType = TK_STAR;
156 return 1; 238 return 1;
157 } 239 }
158 case '/': { 240 case CC_SLASH: {
159 if( z[1]!='*' || z[2]==0 ){ 241 if( z[1]!='*' || z[2]==0 ){
160 *tokenType = TK_SLASH; 242 *tokenType = TK_SLASH;
161 return 1; 243 return 1;
162 } 244 }
163 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 245 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
164 if( c ) i++; 246 if( c ) i++;
165 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 247 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
166 return i; 248 return i;
167 } 249 }
168 case '%': { 250 case CC_PERCENT: {
169 *tokenType = TK_REM; 251 *tokenType = TK_REM;
170 return 1; 252 return 1;
171 } 253 }
172 case '=': { 254 case CC_EQ: {
173 *tokenType = TK_EQ; 255 *tokenType = TK_EQ;
174 return 1 + (z[1]=='='); 256 return 1 + (z[1]=='=');
175 } 257 }
176 case '<': { 258 case CC_LT: {
177 if( (c=z[1])=='=' ){ 259 if( (c=z[1])=='=' ){
178 *tokenType = TK_LE; 260 *tokenType = TK_LE;
179 return 2; 261 return 2;
180 }else if( c=='>' ){ 262 }else if( c=='>' ){
181 *tokenType = TK_NE; 263 *tokenType = TK_NE;
182 return 2; 264 return 2;
183 }else if( c=='<' ){ 265 }else if( c=='<' ){
184 *tokenType = TK_LSHIFT; 266 *tokenType = TK_LSHIFT;
185 return 2; 267 return 2;
186 }else{ 268 }else{
187 *tokenType = TK_LT; 269 *tokenType = TK_LT;
188 return 1; 270 return 1;
189 } 271 }
190 } 272 }
191 case '>': { 273 case CC_GT: {
192 if( (c=z[1])=='=' ){ 274 if( (c=z[1])=='=' ){
193 *tokenType = TK_GE; 275 *tokenType = TK_GE;
194 return 2; 276 return 2;
195 }else if( c=='>' ){ 277 }else if( c=='>' ){
196 *tokenType = TK_RSHIFT; 278 *tokenType = TK_RSHIFT;
197 return 2; 279 return 2;
198 }else{ 280 }else{
199 *tokenType = TK_GT; 281 *tokenType = TK_GT;
200 return 1; 282 return 1;
201 } 283 }
202 } 284 }
203 case '!': { 285 case CC_BANG: {
204 if( z[1]!='=' ){ 286 if( z[1]!='=' ){
205 *tokenType = TK_ILLEGAL; 287 *tokenType = TK_ILLEGAL;
206 return 2; 288 return 1;
207 }else{ 289 }else{
208 *tokenType = TK_NE; 290 *tokenType = TK_NE;
209 return 2; 291 return 2;
210 } 292 }
211 } 293 }
212 case '|': { 294 case CC_PIPE: {
213 if( z[1]!='|' ){ 295 if( z[1]!='|' ){
214 *tokenType = TK_BITOR; 296 *tokenType = TK_BITOR;
215 return 1; 297 return 1;
216 }else{ 298 }else{
217 *tokenType = TK_CONCAT; 299 *tokenType = TK_CONCAT;
218 return 2; 300 return 2;
219 } 301 }
220 } 302 }
221 case ',': { 303 case CC_COMMA: {
222 *tokenType = TK_COMMA; 304 *tokenType = TK_COMMA;
223 return 1; 305 return 1;
224 } 306 }
225 case '&': { 307 case CC_AND: {
226 *tokenType = TK_BITAND; 308 *tokenType = TK_BITAND;
227 return 1; 309 return 1;
228 } 310 }
229 case '~': { 311 case CC_TILDA: {
230 *tokenType = TK_BITNOT; 312 *tokenType = TK_BITNOT;
231 return 1; 313 return 1;
232 } 314 }
233 case '`': 315 case CC_QUOTE: {
234 case '\'':
235 case '"': {
236 int delim = z[0]; 316 int delim = z[0];
237 testcase( delim=='`' ); 317 testcase( delim=='`' );
238 testcase( delim=='\'' ); 318 testcase( delim=='\'' );
239 testcase( delim=='"' ); 319 testcase( delim=='"' );
240 for(i=1; (c=z[i])!=0; i++){ 320 for(i=1; (c=z[i])!=0; i++){
241 if( c==delim ){ 321 if( c==delim ){
242 if( z[i+1]==delim ){ 322 if( z[i+1]==delim ){
243 i++; 323 i++;
244 }else{ 324 }else{
245 break; 325 break;
246 } 326 }
247 } 327 }
248 } 328 }
249 if( c=='\'' ){ 329 if( c=='\'' ){
250 *tokenType = TK_STRING; 330 *tokenType = TK_STRING;
251 return i+1; 331 return i+1;
252 }else if( c!=0 ){ 332 }else if( c!=0 ){
253 *tokenType = TK_ID; 333 *tokenType = TK_ID;
254 return i+1; 334 return i+1;
255 }else{ 335 }else{
256 *tokenType = TK_ILLEGAL; 336 *tokenType = TK_ILLEGAL;
257 return i; 337 return i;
258 } 338 }
259 } 339 }
260 case '.': { 340 case CC_DOT: {
261 #ifndef SQLITE_OMIT_FLOATING_POINT 341 #ifndef SQLITE_OMIT_FLOATING_POINT
262 if( !sqlite3Isdigit(z[1]) ) 342 if( !sqlite3Isdigit(z[1]) )
263 #endif 343 #endif
264 { 344 {
265 *tokenType = TK_DOT; 345 *tokenType = TK_DOT;
266 return 1; 346 return 1;
267 } 347 }
268 /* If the next character is a digit, this is a floating point 348 /* If the next character is a digit, this is a floating point
269 ** number that begins with ".". Fall thru into the next case */ 349 ** number that begins with ".". Fall thru into the next case */
270 } 350 }
271 case '0': case '1': case '2': case '3': case '4': 351 case CC_DIGIT: {
272 case '5': case '6': case '7': case '8': case '9': {
273 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); 352 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
274 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); 353 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
275 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); 354 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
276 testcase( z[0]=='9' ); 355 testcase( z[0]=='9' );
277 *tokenType = TK_INTEGER; 356 *tokenType = TK_INTEGER;
278 #ifndef SQLITE_OMIT_HEX_INTEGER 357 #ifndef SQLITE_OMIT_HEX_INTEGER
279 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ 358 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
280 for(i=3; sqlite3Isxdigit(z[i]); i++){} 359 for(i=3; sqlite3Isxdigit(z[i]); i++){}
281 return i; 360 return i;
282 } 361 }
(...skipping 14 matching lines...) Expand all
297 while( sqlite3Isdigit(z[i]) ){ i++; } 376 while( sqlite3Isdigit(z[i]) ){ i++; }
298 *tokenType = TK_FLOAT; 377 *tokenType = TK_FLOAT;
299 } 378 }
300 #endif 379 #endif
301 while( IdChar(z[i]) ){ 380 while( IdChar(z[i]) ){
302 *tokenType = TK_ILLEGAL; 381 *tokenType = TK_ILLEGAL;
303 i++; 382 i++;
304 } 383 }
305 return i; 384 return i;
306 } 385 }
307 case '[': { 386 case CC_QUOTE2: {
308 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 387 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
309 *tokenType = c==']' ? TK_ID : TK_ILLEGAL; 388 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
310 return i; 389 return i;
311 } 390 }
312 case '?': { 391 case CC_VARNUM: {
313 *tokenType = TK_VARIABLE; 392 *tokenType = TK_VARIABLE;
314 for(i=1; sqlite3Isdigit(z[i]); i++){} 393 for(i=1; sqlite3Isdigit(z[i]); i++){}
315 return i; 394 return i;
316 } 395 }
317 #ifndef SQLITE_OMIT_TCL_VARIABLE 396 case CC_DOLLAR:
318 case '$': 397 case CC_VARALPHA: {
319 #endif
320 case '@': /* For compatibility with MS SQL Server */
321 case '#':
322 case ':': {
323 int n = 0; 398 int n = 0;
324 testcase( z[0]=='$' ); testcase( z[0]=='@' ); 399 testcase( z[0]=='$' ); testcase( z[0]=='@' );
325 testcase( z[0]==':' ); testcase( z[0]=='#' ); 400 testcase( z[0]==':' ); testcase( z[0]=='#' );
326 *tokenType = TK_VARIABLE; 401 *tokenType = TK_VARIABLE;
327 for(i=1; (c=z[i])!=0; i++){ 402 for(i=1; (c=z[i])!=0; i++){
328 if( IdChar(c) ){ 403 if( IdChar(c) ){
329 n++; 404 n++;
330 #ifndef SQLITE_OMIT_TCL_VARIABLE 405 #ifndef SQLITE_OMIT_TCL_VARIABLE
331 }else if( c=='(' && n>0 ){ 406 }else if( c=='(' && n>0 ){
332 do{ 407 do{
333 i++; 408 i++;
334 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); 409 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
335 if( c==')' ){ 410 if( c==')' ){
336 i++; 411 i++;
337 }else{ 412 }else{
338 *tokenType = TK_ILLEGAL; 413 *tokenType = TK_ILLEGAL;
339 } 414 }
340 break; 415 break;
341 }else if( c==':' && z[i+1]==':' ){ 416 }else if( c==':' && z[i+1]==':' ){
342 i++; 417 i++;
343 #endif 418 #endif
344 }else{ 419 }else{
345 break; 420 break;
346 } 421 }
347 } 422 }
348 if( n==0 ) *tokenType = TK_ILLEGAL; 423 if( n==0 ) *tokenType = TK_ILLEGAL;
349 return i; 424 return i;
350 } 425 }
426 case CC_KYWD: {
427 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
428 if( IdChar(z[i]) ){
429 /* This token started out using characters that can appear in keywords,
430 ** but z[i] is a character not allowed within keywords, so this must
431 ** be an identifier instead */
432 i++;
433 break;
434 }
435 *tokenType = TK_ID;
436 return keywordCode((char*)z, i, tokenType);
437 }
438 case CC_X: {
351 #ifndef SQLITE_OMIT_BLOB_LITERAL 439 #ifndef SQLITE_OMIT_BLOB_LITERAL
352 case 'x': case 'X': {
353 testcase( z[0]=='x' ); testcase( z[0]=='X' ); 440 testcase( z[0]=='x' ); testcase( z[0]=='X' );
354 if( z[1]=='\'' ){ 441 if( z[1]=='\'' ){
355 *tokenType = TK_BLOB; 442 *tokenType = TK_BLOB;
356 for(i=2; sqlite3Isxdigit(z[i]); i++){} 443 for(i=2; sqlite3Isxdigit(z[i]); i++){}
357 if( z[i]!='\'' || i%2 ){ 444 if( z[i]!='\'' || i%2 ){
358 *tokenType = TK_ILLEGAL; 445 *tokenType = TK_ILLEGAL;
359 while( z[i] && z[i]!='\'' ){ i++; } 446 while( z[i] && z[i]!='\'' ){ i++; }
360 } 447 }
361 if( z[i] ) i++; 448 if( z[i] ) i++;
362 return i; 449 return i;
363 } 450 }
364 /* Otherwise fall through to the next case */ 451 #endif
452 /* If it is not a BLOB literal, then it must be an ID, since no
453 ** SQL keywords start with the letter 'x'. Fall through */
365 } 454 }
366 #endif 455 case CC_ID: {
456 i = 1;
457 break;
458 }
367 default: { 459 default: {
368 if( !IdChar(*z) ){ 460 *tokenType = TK_ILLEGAL;
369 break; 461 return 1;
370 }
371 for(i=1; IdChar(z[i]); i++){}
372 *tokenType = TK_ID;
373 return keywordCode((char*)z, i, tokenType);
374 } 462 }
375 } 463 }
376 *tokenType = TK_ILLEGAL; 464 while( IdChar(z[i]) ){ i++; }
377 return 1; 465 *tokenType = TK_ID;
466 return i;
378 } 467 }
379 468
380 /* 469 /*
381 ** Run the parser on the given SQL string. The parser structure is 470 ** Run the parser on the given SQL string. The parser structure is
382 ** passed in. An SQLITE_ status code is returned. If an error occurs 471 ** passed in. An SQLITE_ status code is returned. If an error occurs
383 ** then an and attempt is made to write an error message into 472 ** then an and attempt is made to write an error message into
384 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that 473 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
385 ** error message. 474 ** error message.
386 */ 475 */
387 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 476 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
388 int nErr = 0; /* Number of errors encountered */ 477 int nErr = 0; /* Number of errors encountered */
389 int i; /* Loop counter */ 478 int i; /* Loop counter */
390 void *pEngine; /* The LEMON-generated LALR(1) parser */ 479 void *pEngine; /* The LEMON-generated LALR(1) parser */
391 int tokenType; /* type of the next token */ 480 int tokenType; /* type of the next token */
392 int lastTokenParsed = -1; /* type of the previous token */ 481 int lastTokenParsed = -1; /* type of the previous token */
393 u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
394 sqlite3 *db = pParse->db; /* The database connection */ 482 sqlite3 *db = pParse->db; /* The database connection */
395 int mxSqlLen; /* Max length of an SQL string */ 483 int mxSqlLen; /* Max length of an SQL string */
484 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
485 unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */
486 #endif
396 487
397 assert( zSql!=0 ); 488 assert( zSql!=0 );
398 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 489 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
399 if( db->nVdbeActive==0 ){ 490 if( db->nVdbeActive==0 ){
400 db->u1.isInterrupted = 0; 491 db->u1.isInterrupted = 0;
401 } 492 }
402 pParse->rc = SQLITE_OK; 493 pParse->rc = SQLITE_OK;
403 pParse->zTail = zSql; 494 pParse->zTail = zSql;
404 i = 0; 495 i = 0;
405 assert( pzErrMsg!=0 ); 496 assert( pzErrMsg!=0 );
406 /* sqlite3ParserTrace(stdout, "parser: "); */ 497 /* sqlite3ParserTrace(stdout, "parser: "); */
498 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
499 pEngine = zSpace;
500 sqlite3ParserInit(pEngine);
501 #else
407 pEngine = sqlite3ParserAlloc(sqlite3Malloc); 502 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
408 if( pEngine==0 ){ 503 if( pEngine==0 ){
409 db->mallocFailed = 1; 504 sqlite3OomFault(db);
410 return SQLITE_NOMEM; 505 return SQLITE_NOMEM_BKPT;
411 } 506 }
507 #endif
412 assert( pParse->pNewTable==0 ); 508 assert( pParse->pNewTable==0 );
413 assert( pParse->pNewTrigger==0 ); 509 assert( pParse->pNewTrigger==0 );
414 assert( pParse->nVar==0 ); 510 assert( pParse->nVar==0 );
415 assert( pParse->nzVar==0 ); 511 assert( pParse->pVList==0 );
416 assert( pParse->azVar==0 ); 512 while( 1 ){
417 enableLookaside = db->lookaside.bEnabled;
418 if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
419 while( zSql[i]!=0 ){
420 assert( i>=0 ); 513 assert( i>=0 );
421 pParse->sLastToken.z = &zSql[i]; 514 if( zSql[i]!=0 ){
422 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); 515 pParse->sLastToken.z = &zSql[i];
423 i += pParse->sLastToken.n; 516 pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType);
424 if( i>mxSqlLen ){ 517 i += pParse->sLastToken.n;
425 pParse->rc = SQLITE_TOOBIG; 518 if( i>mxSqlLen ){
426 break; 519 pParse->rc = SQLITE_TOOBIG;
520 break;
521 }
522 }else{
523 /* Upon reaching the end of input, call the parser two more times
524 ** with tokens TK_SEMI and 0, in that order. */
525 if( lastTokenParsed==TK_SEMI ){
526 tokenType = 0;
527 }else if( lastTokenParsed==0 ){
528 break;
529 }else{
530 tokenType = TK_SEMI;
531 }
427 } 532 }
428 if( tokenType>=TK_SPACE ){ 533 if( tokenType>=TK_SPACE ){
429 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); 534 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
430 if( db->u1.isInterrupted ){ 535 if( db->u1.isInterrupted ){
431 sqlite3ErrorMsg(pParse, "interrupt");
432 pParse->rc = SQLITE_INTERRUPT; 536 pParse->rc = SQLITE_INTERRUPT;
433 break; 537 break;
434 } 538 }
435 if( tokenType==TK_ILLEGAL ){ 539 if( tokenType==TK_ILLEGAL ){
436 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", 540 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
437 &pParse->sLastToken); 541 &pParse->sLastToken);
438 break; 542 break;
439 } 543 }
440 }else{ 544 }else{
441 if( tokenType==TK_SEMI ) pParse->zTail = &zSql[i];
442 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 545 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
443 lastTokenParsed = tokenType; 546 lastTokenParsed = tokenType;
444 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; 547 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
445 } 548 }
446 } 549 }
447 assert( nErr==0 ); 550 assert( nErr==0 );
448 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ 551 pParse->zTail = &zSql[i];
449 assert( zSql[i]==0 );
450 if( lastTokenParsed!=TK_SEMI ){
451 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
452 pParse->zTail = &zSql[i];
453 }
454 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
455 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
456 }
457 }
458 #ifdef YYTRACKMAXSTACKDEPTH 552 #ifdef YYTRACKMAXSTACKDEPTH
459 sqlite3_mutex_enter(sqlite3MallocMutex()); 553 sqlite3_mutex_enter(sqlite3MallocMutex());
460 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, 554 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
461 sqlite3ParserStackPeak(pEngine) 555 sqlite3ParserStackPeak(pEngine)
462 ); 556 );
463 sqlite3_mutex_leave(sqlite3MallocMutex()); 557 sqlite3_mutex_leave(sqlite3MallocMutex());
464 #endif /* YYDEBUG */ 558 #endif /* YYDEBUG */
559 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
560 sqlite3ParserFinalize(pEngine);
561 #else
465 sqlite3ParserFree(pEngine, sqlite3_free); 562 sqlite3ParserFree(pEngine, sqlite3_free);
466 db->lookaside.bEnabled = enableLookaside; 563 #endif
467 if( db->mallocFailed ){ 564 if( db->mallocFailed ){
468 pParse->rc = SQLITE_NOMEM; 565 pParse->rc = SQLITE_NOMEM_BKPT;
469 } 566 }
470 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 567 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
471 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); 568 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
472 } 569 }
473 assert( pzErrMsg!=0 ); 570 assert( pzErrMsg!=0 );
474 if( pParse->zErrMsg ){ 571 if( pParse->zErrMsg ){
475 *pzErrMsg = pParse->zErrMsg; 572 *pzErrMsg = pParse->zErrMsg;
476 sqlite3_log(pParse->rc, "%s", *pzErrMsg); 573 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
477 pParse->zErrMsg = 0; 574 pParse->zErrMsg = 0;
478 nErr++; 575 nErr++;
(...skipping 14 matching lines...) Expand all
493 #endif 590 #endif
494 591
495 if( !IN_DECLARE_VTAB ){ 592 if( !IN_DECLARE_VTAB ){
496 /* If the pParse->declareVtab flag is set, do not delete any table 593 /* If the pParse->declareVtab flag is set, do not delete any table
497 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 594 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
498 ** will take responsibility for freeing the Table structure. 595 ** will take responsibility for freeing the Table structure.
499 */ 596 */
500 sqlite3DeleteTable(db, pParse->pNewTable); 597 sqlite3DeleteTable(db, pParse->pNewTable);
501 } 598 }
502 599
503 sqlite3WithDelete(db, pParse->pWithToFree); 600 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
504 sqlite3DeleteTrigger(db, pParse->pNewTrigger); 601 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
505 for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); 602 sqlite3DbFree(db, pParse->pVList);
506 sqlite3DbFree(db, pParse->azVar);
507 while( pParse->pAinc ){ 603 while( pParse->pAinc ){
508 AutoincInfo *p = pParse->pAinc; 604 AutoincInfo *p = pParse->pAinc;
509 pParse->pAinc = p->pNext; 605 pParse->pAinc = p->pNext;
510 sqlite3DbFree(db, p); 606 sqlite3DbFree(db, p);
511 } 607 }
512 while( pParse->pZombieTab ){ 608 while( pParse->pZombieTab ){
513 Table *p = pParse->pZombieTab; 609 Table *p = pParse->pZombieTab;
514 pParse->pZombieTab = p->pNextZombie; 610 pParse->pZombieTab = p->pNextZombie;
515 sqlite3DeleteTable(db, p); 611 sqlite3DeleteTable(db, p);
516 } 612 }
517 assert( nErr==0 || pParse->rc!=SQLITE_OK ); 613 assert( nErr==0 || pParse->rc!=SQLITE_OK );
518 return nErr; 614 return nErr;
519 } 615 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698