fetch_and_build only uses a virtual target
[AROS-Contrib.git] / sqlite3 / tokenize.c
blob0224468ddbab8b8409e553082a7e548e2360dd67
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.
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.
18 ** $Id$
20 #include "sqliteInt.h"
21 #include "os.h"
22 #include <ctype.h>
23 #include <stdlib.h>
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
28 ** returned. If the input is not a keyword, TK_ID is returned.
30 ** The implementation of this routine was generated by a program,
31 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
32 ** The output of the mkkeywordhash.c program is written into a file
33 ** named keywordhash.h and then included into this source file by
34 ** the #include below.
36 #include "keywordhash.h"
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.
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.
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.
58 static const char isIdChar[] = {
59 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
60 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
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 */
68 #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20]))
71 ** Return the length of the token that begins at z[0].
72 ** Store the token type in *tokenType before returning.
74 static int getToken(const unsigned char *z, int *tokenType){
75 int i, c;
76 switch( *z ){
77 case ' ': case '\t': case '\n': case '\f': case '\r': {
78 for(i=1; isspace(z[i]); i++){}
79 *tokenType = TK_SPACE;
80 return i;
82 case '-': {
83 if( z[1]=='-' ){
84 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
85 *tokenType = TK_COMMENT;
86 return i;
88 *tokenType = TK_MINUS;
89 return 1;
91 case '(': {
92 *tokenType = TK_LP;
93 return 1;
95 case ')': {
96 *tokenType = TK_RP;
97 return 1;
99 case ';': {
100 *tokenType = TK_SEMI;
101 return 1;
103 case '+': {
104 *tokenType = TK_PLUS;
105 return 1;
107 case '*': {
108 *tokenType = TK_STAR;
109 return 1;
111 case '/': {
112 if( z[1]!='*' || z[2]==0 ){
113 *tokenType = TK_SLASH;
114 return 1;
116 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
117 if( c ) i++;
118 *tokenType = TK_COMMENT;
119 return i;
121 case '%': {
122 *tokenType = TK_REM;
123 return 1;
125 case '=': {
126 *tokenType = TK_EQ;
127 return 1 + (z[1]=='=');
129 case '<': {
130 if( (c=z[1])=='=' ){
131 *tokenType = TK_LE;
132 return 2;
133 }else if( c=='>' ){
134 *tokenType = TK_NE;
135 return 2;
136 }else if( c=='<' ){
137 *tokenType = TK_LSHIFT;
138 return 2;
139 }else{
140 *tokenType = TK_LT;
141 return 1;
144 case '>': {
145 if( (c=z[1])=='=' ){
146 *tokenType = TK_GE;
147 return 2;
148 }else if( c=='>' ){
149 *tokenType = TK_RSHIFT;
150 return 2;
151 }else{
152 *tokenType = TK_GT;
153 return 1;
156 case '!': {
157 if( z[1]!='=' ){
158 *tokenType = TK_ILLEGAL;
159 return 2;
160 }else{
161 *tokenType = TK_NE;
162 return 2;
165 case '|': {
166 if( z[1]!='|' ){
167 *tokenType = TK_BITOR;
168 return 1;
169 }else{
170 *tokenType = TK_CONCAT;
171 return 2;
174 case ',': {
175 *tokenType = TK_COMMA;
176 return 1;
178 case '&': {
179 *tokenType = TK_BITAND;
180 return 1;
182 case '~': {
183 *tokenType = TK_BITNOT;
184 return 1;
186 case '#': {
187 for(i=1; isdigit(z[i]) || (i==1 && z[1]=='-'); i++){}
188 *tokenType = TK_REGISTER;
189 return i;
191 case '\'': case '"': {
192 int delim = z[0];
193 for(i=1; (c=z[i])!=0; i++){
194 if( c==delim ){
195 if( z[i+1]==delim ){
196 i++;
197 }else{
198 break;
202 if( c ) i++;
203 *tokenType = TK_STRING;
204 return i;
206 case '.': {
207 *tokenType = TK_DOT;
208 return 1;
210 case '0': case '1': case '2': case '3': case '4':
211 case '5': case '6': case '7': case '8': case '9': {
212 *tokenType = TK_INTEGER;
213 for(i=1; isdigit(z[i]); i++){}
214 #ifndef SQLITE_OMIT_FLOATING_POINT
215 if( z[i]=='.' && isdigit(z[i+1]) ){
216 i += 2;
217 while( isdigit(z[i]) ){ i++; }
218 *tokenType = TK_FLOAT;
220 if( (z[i]=='e' || z[i]=='E') &&
221 ( isdigit(z[i+1])
222 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
225 i += 2;
226 while( isdigit(z[i]) ){ i++; }
227 *tokenType = TK_FLOAT;
229 #endif
230 return i;
232 case '[': {
233 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
234 *tokenType = TK_ID;
235 return i;
237 case '?': {
238 *tokenType = TK_VARIABLE;
239 for(i=1; isdigit(z[i]); i++){}
240 return i;
242 case ':': {
243 for(i=1; IdChar(z[i]); i++){}
244 *tokenType = i>1 ? TK_VARIABLE : TK_ILLEGAL;
245 return i;
247 #ifndef SQLITE_OMIT_TCL_VARIABLE
248 case '$': {
249 *tokenType = TK_VARIABLE;
250 if( z[1]=='{' ){
251 int nBrace = 1;
252 for(i=2; (c=z[i])!=0 && nBrace; i++){
253 if( c=='{' ){
254 nBrace++;
255 }else if( c=='}' ){
256 nBrace--;
259 if( c==0 ) *tokenType = TK_ILLEGAL;
260 }else{
261 int n = 0;
262 for(i=1; (c=z[i])!=0; i++){
263 if( isalnum(c) || c=='_' ){
264 n++;
265 }else if( c=='(' && n>0 ){
267 i++;
268 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
269 if( c==')' ){
270 i++;
271 }else{
272 *tokenType = TK_ILLEGAL;
274 break;
275 }else if( c==':' && z[i+1]==':' ){
276 i++;
277 }else{
278 break;
281 if( n==0 ) *tokenType = TK_ILLEGAL;
283 return i;
285 #endif
286 #ifndef SQLITE_OMIT_BLOB_LITERAL
287 case 'x': case 'X': {
288 if( (c=z[1])=='\'' || c=='"' ){
289 int delim = c;
290 *tokenType = TK_BLOB;
291 for(i=2; (c=z[i])!=0; i++){
292 if( c==delim ){
293 if( i%2 ) *tokenType = TK_ILLEGAL;
294 break;
296 if( !isxdigit(c) ){
297 *tokenType = TK_ILLEGAL;
298 return i;
301 if( c ) i++;
302 return i;
304 /* Otherwise fall through to the next case */
306 #endif
307 default: {
308 if( !IdChar(*z) ){
309 break;
311 for(i=1; IdChar(z[i]); i++){}
312 *tokenType = keywordCode((char*)z, i);
313 return i;
316 *tokenType = TK_ILLEGAL;
317 return 1;
319 int sqlite3GetToken(const unsigned char *z, int *tokenType){
320 return getToken(z, tokenType);
324 ** Run the parser on the given SQL string. The parser structure is
325 ** passed in. An SQLITE_ status code is returned. If an error occurs
326 ** and pzErrMsg!=NULL then an error message might be written into
327 ** memory obtained from malloc() and *pzErrMsg made to point to that
328 ** error message. Or maybe not.
330 int sqlite3RunParser(Parse *pParse, const char *zSql, STRPTR *pzErrMsg){
331 int nErr = 0;
332 int i;
333 void *pEngine;
334 int tokenType;
335 int lastTokenParsed = -1;
336 sqlite3 *db = pParse->db;
337 extern void *sqlite3ParserAlloc(void*(*)(int));
338 extern void sqlite3ParserFree(void*, void(*)(void*));
339 extern int sqlite3Parser(void*, int, Token, Parse*);
341 db->flags &= ~SQLITE_Interrupt;
342 pParse->rc = SQLITE_OK;
343 i = 0;
344 pEngine = sqlite3ParserAlloc((void*(*)(int))sqlite3MallocX);
345 if( pEngine==0 ){
346 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
347 return SQLITE_NOMEM;
349 assert( pParse->sLastToken.dyn==0 );
350 assert( pParse->pNewTable==0 );
351 assert( pParse->pNewTrigger==0 );
352 assert( pParse->nVar==0 );
353 assert( pParse->nVarExpr==0 );
354 assert( pParse->nVarExprAlloc==0 );
355 assert( pParse->apVarExpr==0 );
356 pParse->zTail = pParse->zSql = zSql;
357 while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){
358 assert( i>=0 );
359 pParse->sLastToken.z = &zSql[i];
360 assert( pParse->sLastToken.dyn==0 );
361 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
362 i += pParse->sLastToken.n;
363 switch( tokenType ){
364 case TK_SPACE:
365 case TK_COMMENT: {
366 if( (db->flags & SQLITE_Interrupt)!=0 ){
367 pParse->rc = SQLITE_INTERRUPT;
368 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
369 goto abort_parse;
371 break;
373 case TK_ILLEGAL: {
374 if( pzErrMsg ){
375 sqliteFree(*pzErrMsg);
376 *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
377 &pParse->sLastToken);
379 nErr++;
380 goto abort_parse;
382 case TK_SEMI: {
383 pParse->zTail = &zSql[i];
384 /* Fall thru into the default case */
386 default: {
387 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
388 lastTokenParsed = tokenType;
389 if( pParse->rc!=SQLITE_OK ){
390 goto abort_parse;
392 break;
396 abort_parse:
397 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
398 if( lastTokenParsed!=TK_SEMI ){
399 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
400 pParse->zTail = &zSql[i];
402 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
404 sqlite3ParserFree(pEngine, sqlite3FreeX);
405 if( sqlite3_malloc_failed ){
406 pParse->rc = SQLITE_NOMEM;
408 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
409 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc),
410 (char*)0);
412 if( pParse->zErrMsg ){
413 if( pzErrMsg && *pzErrMsg==0 ){
414 *pzErrMsg = pParse->zErrMsg;
415 }else{
416 sqliteFree(pParse->zErrMsg);
418 pParse->zErrMsg = 0;
419 if( !nErr ) nErr++;
421 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
422 sqlite3VdbeDelete(pParse->pVdbe);
423 pParse->pVdbe = 0;
425 sqlite3DeleteTable(pParse->db, pParse->pNewTable);
426 sqlite3DeleteTrigger(pParse->pNewTrigger);
427 sqliteFree(pParse->apVarExpr);
428 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
429 pParse->rc = SQLITE_ERROR;
431 return nErr;
434 /* The sqlite3_complete() API may be omitted (to save code space) by
435 ** defining the following symbol.
437 #ifndef SQLITE_OMIT_COMPLETE
440 ** Token types used by the sqlite3_complete() routine. See the header
441 ** comments on that procedure for additional information.
443 #define tkSEMI 0
444 #define tkWS 1
445 #define tkOTHER 2
446 #define tkEXPLAIN 3
447 #define tkCREATE 4
448 #define tkTEMP 5
449 #define tkTRIGGER 6
450 #define tkEND 7
453 ** Return TRUE if the given SQL string ends in a semicolon.
455 ** Special handling is require for CREATE TRIGGER statements.
456 ** Whenever the CREATE TRIGGER keywords are seen, the statement
457 ** must end with ";END;".
459 ** This implementation uses a state machine with 7 states:
461 ** (0) START At the beginning or end of an SQL statement. This routine
462 ** returns 1 if it ends in the START state and 0 if it ends
463 ** in any other state.
465 ** (1) NORMAL We are in the middle of statement which ends with a single
466 ** semicolon.
468 ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
469 ** a statement.
471 ** (3) CREATE The keyword CREATE has been seen at the beginning of a
472 ** statement, possibly preceeded by EXPLAIN and/or followed by
473 ** TEMP or TEMPORARY
475 ** (4) TRIGGER We are in the middle of a trigger definition that must be
476 ** ended by a semicolon, the keyword END, and another semicolon.
478 ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
479 ** the end of a trigger definition.
481 ** (6) END We've seen the ";END" of the ";END;" that occurs at the end
482 ** of a trigger difinition.
484 ** Transitions between states above are determined by tokens extracted
485 ** from the input. The following tokens are significant:
487 ** (0) tkSEMI A semicolon.
488 ** (1) tkWS Whitespace
489 ** (2) tkOTHER Any other SQL token.
490 ** (3) tkEXPLAIN The "explain" keyword.
491 ** (4) tkCREATE The "create" keyword.
492 ** (5) tkTEMP The "temp" or "temporary" keyword.
493 ** (6) tkTRIGGER The "trigger" keyword.
494 ** (7) tkEND The "end" keyword.
496 ** Whitespace never causes a state transition and is always ignored.
498 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
499 ** to recognize the end of a trigger can be omitted. All we have to do
500 ** is look for a semicolon that is not part of an string or comment.
502 int sqlite3_complete(const char *zSql){
503 u8 state = 0; /* Current state, using numbers defined in header comment */
504 u8 token; /* Value of the next token */
506 #ifndef SQLITE_OMIT_TRIGGER
507 /* A complex statement machine used to detect the end of a CREATE TRIGGER
508 ** statement. This is the normal case.
510 static const u8 trans[7][8] = {
511 /* Token: */
512 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
513 /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, },
514 /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, },
515 /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, },
516 /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, },
517 /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, },
518 /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, },
519 /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, },
521 #else
522 /* If triggers are not suppored by this compile then the statement machine
523 ** used to detect the end of a statement is much simplier
525 static const u8 trans[2][3] = {
526 /* Token: */
527 /* State: ** SEMI WS OTHER */
528 /* 0 START: */ { 0, 0, 1, },
529 /* 1 NORMAL: */ { 0, 1, 1, },
531 #endif /* SQLITE_OMIT_TRIGGER */
533 while( *zSql ){
534 switch( *zSql ){
535 case ';': { /* A semicolon */
536 token = tkSEMI;
537 break;
539 case ' ':
540 case '\r':
541 case '\t':
542 case '\n':
543 case '\f': { /* White space is ignored */
544 token = tkWS;
545 break;
547 case '/': { /* C-style comments */
548 if( zSql[1]!='*' ){
549 token = tkOTHER;
550 break;
552 zSql += 2;
553 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
554 if( zSql[0]==0 ) return 0;
555 zSql++;
556 token = tkWS;
557 break;
559 case '-': { /* SQL-style comments from "--" to end of line */
560 if( zSql[1]!='-' ){
561 token = tkOTHER;
562 break;
564 while( *zSql && *zSql!='\n' ){ zSql++; }
565 if( *zSql==0 ) return state==0;
566 token = tkWS;
567 break;
569 case '[': { /* Microsoft-style identifiers in [...] */
570 zSql++;
571 while( *zSql && *zSql!=']' ){ zSql++; }
572 if( *zSql==0 ) return 0;
573 token = tkOTHER;
574 break;
576 case '"': /* single- and double-quoted strings */
577 case '\'': {
578 int c = *zSql;
579 zSql++;
580 while( *zSql && *zSql!=c ){ zSql++; }
581 if( *zSql==0 ) return 0;
582 token = tkOTHER;
583 break;
585 default: {
586 int c;
587 if( IdChar((u8)*zSql) ){
588 /* Keywords and unquoted identifiers */
589 int nId;
590 for(nId=1; IdChar(zSql[nId]); nId++){}
591 #ifdef SQLITE_OMIT_TRIGGER
592 token = tkOTHER;
593 #else
594 switch( *zSql ){
595 case 'c': case 'C': {
596 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
597 token = tkCREATE;
598 }else{
599 token = tkOTHER;
601 break;
603 case 't': case 'T': {
604 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
605 token = tkTRIGGER;
606 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
607 token = tkTEMP;
608 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
609 token = tkTEMP;
610 }else{
611 token = tkOTHER;
613 break;
615 case 'e': case 'E': {
616 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
617 token = tkEND;
618 }else
619 #ifndef SQLITE_OMIT_EXPLAIN
620 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
621 token = tkEXPLAIN;
622 }else
623 #endif
625 token = tkOTHER;
627 break;
629 default: {
630 token = tkOTHER;
631 break;
634 #endif /* SQLITE_OMIT_TRIGGER */
635 zSql += nId-1;
636 }else{
637 /* Operators and special symbols */
638 token = tkOTHER;
640 break;
643 state = trans[state][token];
644 zSql++;
646 return state==0;
649 #ifndef SQLITE_OMIT_UTF16
651 ** This routine is the same as the sqlite3_complete() routine described
652 ** above, except that the parameter is required to be UTF-16 encoded, not
653 ** UTF-8.
655 int sqlite3_complete16(const void *zSql){
656 sqlite3_value *pVal;
657 char const *zSql8;
658 int rc = 0;
660 pVal = sqlite3ValueNew();
661 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
662 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
663 if( zSql8 ){
664 rc = sqlite3_complete(zSql8);
666 sqlite3ValueFree(pVal);
667 return rc;
669 #endif /* SQLITE_OMIT_UTF16 */
670 #endif /* SQLITE_OMIT_COMPLETE */