tokenize.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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.136 2007/08/27 23:26:59 drh Exp $
  19. */
  20. #include "sqliteInt.h"
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. /*
  24. ** The charMap() macro maps alphabetic characters into their
  25. ** lower-case ASCII equivalent. On ASCII machines, this is just
  26. ** an upper-to-lower case map. On EBCDIC machines we also need
  27. ** to adjust the encoding. Only alphabetic characters and underscores
  28. ** need to be translated.
  29. */
  30. #ifdef SQLITE_ASCII
  31. # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
  32. #endif
  33. #ifdef SQLITE_EBCDIC
  34. # define charMap(X) ebcdicToAscii[(unsigned char)X]
  35. const unsigned char ebcdicToAscii[] = {
  36. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  37. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
  45. 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
  46. 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
  47. 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
  49. 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
  50. 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
  51. 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
  53. };
  54. #endif
  55. /*
  56. ** The sqlite3KeywordCode function looks up an identifier to determine if
  57. ** it is a keyword. If it is a keyword, the token code of that keyword is
  58. ** returned. If the input is not a keyword, TK_ID is returned.
  59. **
  60. ** The implementation of this routine was generated by a program,
  61. ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
  62. ** The output of the mkkeywordhash.c program is written into a file
  63. ** named keywordhash.h and then included into this source file by
  64. ** the #include below.
  65. */
  66. #include "keywordhash.h"
  67. /*
  68. ** If X is a character that can be used in an identifier then
  69. ** IdChar(X) will be true. Otherwise it is false.
  70. **
  71. ** For ASCII, any character with the high-order bit set is
  72. ** allowed in an identifier. For 7-bit characters,
  73. ** sqlite3IsIdChar[X] must be 1.
  74. **
  75. ** For EBCDIC, the rules are more complex but have the same
  76. ** end result.
  77. **
  78. ** Ticket #1066. the SQL standard does not allow '$' in the
  79. ** middle of identfiers. But many SQL implementations do.
  80. ** SQLite will allow '$' in identifiers for compatibility.
  81. ** But the feature is undocumented.
  82. */
  83. #ifdef SQLITE_ASCII
  84. const char sqlite3IsAsciiIdChar[] = {
  85. /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
  86. 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
  88. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
  90. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
  91. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
  92. };
  93. #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
  94. #endif
  95. #ifdef SQLITE_EBCDIC
  96. const char sqlite3IsEbcdicIdChar[] = {
  97. /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
  98. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
  99. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
  100. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
  101. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
  102. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
  103. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
  104. 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
  106. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
  107. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
  108. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
  109. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
  110. };
  111. #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
  112. #endif
  113. /*
  114. ** Return the length of the token that begins at z[0].
  115. ** Store the token type in *tokenType before returning.
  116. */
  117. static int getToken(const unsigned char *z, int *tokenType){
  118. int i, c;
  119. switch( *z ){
  120. case ' ': case '\t': case '\n': case '\f': case '\r': {
  121. for(i=1; isspace(z[i]); i++){}
  122. *tokenType = TK_SPACE;
  123. return i;
  124. }
  125. case '-': {
  126. if( z[1]=='-' ){
  127. for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
  128. *tokenType = TK_COMMENT;
  129. return i;
  130. }
  131. *tokenType = TK_MINUS;
  132. return 1;
  133. }
  134. case '(': {
  135. *tokenType = TK_LP;
  136. return 1;
  137. }
  138. case ')': {
  139. *tokenType = TK_RP;
  140. return 1;
  141. }
  142. case ';': {
  143. *tokenType = TK_SEMI;
  144. return 1;
  145. }
  146. case '+': {
  147. *tokenType = TK_PLUS;
  148. return 1;
  149. }
  150. case '*': {
  151. *tokenType = TK_STAR;
  152. return 1;
  153. }
  154. case '/': {
  155. if( z[1]!='*' || z[2]==0 ){
  156. *tokenType = TK_SLASH;
  157. return 1;
  158. }
  159. for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
  160. if( c ) i++;
  161. *tokenType = TK_COMMENT;
  162. return i;
  163. }
  164. case '%': {
  165. *tokenType = TK_REM;
  166. return 1;
  167. }
  168. case '=': {
  169. *tokenType = TK_EQ;
  170. return 1 + (z[1]=='=');
  171. }
  172. case '<': {
  173. if( (c=z[1])=='=' ){
  174. *tokenType = TK_LE;
  175. return 2;
  176. }else if( c=='>' ){
  177. *tokenType = TK_NE;
  178. return 2;
  179. }else if( c=='<' ){
  180. *tokenType = TK_LSHIFT;
  181. return 2;
  182. }else{
  183. *tokenType = TK_LT;
  184. return 1;
  185. }
  186. }
  187. case '>': {
  188. if( (c=z[1])=='=' ){
  189. *tokenType = TK_GE;
  190. return 2;
  191. }else if( c=='>' ){
  192. *tokenType = TK_RSHIFT;
  193. return 2;
  194. }else{
  195. *tokenType = TK_GT;
  196. return 1;
  197. }
  198. }
  199. case '!': {
  200. if( z[1]!='=' ){
  201. *tokenType = TK_ILLEGAL;
  202. return 2;
  203. }else{
  204. *tokenType = TK_NE;
  205. return 2;
  206. }
  207. }
  208. case '|': {
  209. if( z[1]!='|' ){
  210. *tokenType = TK_BITOR;
  211. return 1;
  212. }else{
  213. *tokenType = TK_CONCAT;
  214. return 2;
  215. }
  216. }
  217. case ',': {
  218. *tokenType = TK_COMMA;
  219. return 1;
  220. }
  221. case '&': {
  222. *tokenType = TK_BITAND;
  223. return 1;
  224. }
  225. case '~': {
  226. *tokenType = TK_BITNOT;
  227. return 1;
  228. }
  229. case '`':
  230. case '\'':
  231. case '"': {
  232. int delim = z[0];
  233. for(i=1; (c=z[i])!=0; i++){
  234. if( c==delim ){
  235. if( z[i+1]==delim ){
  236. i++;
  237. }else{
  238. break;
  239. }
  240. }
  241. }
  242. if( c ){
  243. *tokenType = TK_STRING;
  244. return i+1;
  245. }else{
  246. *tokenType = TK_ILLEGAL;
  247. return i;
  248. }
  249. }
  250. case '.': {
  251. #ifndef SQLITE_OMIT_FLOATING_POINT
  252. if( !isdigit(z[1]) )
  253. #endif
  254. {
  255. *tokenType = TK_DOT;
  256. return 1;
  257. }
  258. /* If the next character is a digit, this is a floating point
  259. ** number that begins with ".". Fall thru into the next case */
  260. }
  261. case '0': case '1': case '2': case '3': case '4':
  262. case '5': case '6': case '7': case '8': case '9': {
  263. *tokenType = TK_INTEGER;
  264. for(i=0; isdigit(z[i]); i++){}
  265. #ifndef SQLITE_OMIT_FLOATING_POINT
  266. if( z[i]=='.' ){
  267. i++;
  268. while( isdigit(z[i]) ){ i++; }
  269. *tokenType = TK_FLOAT;
  270. }
  271. if( (z[i]=='e' || z[i]=='E') &&
  272. ( isdigit(z[i+1])
  273. || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
  274. )
  275. ){
  276. i += 2;
  277. while( isdigit(z[i]) ){ i++; }
  278. *tokenType = TK_FLOAT;
  279. }
  280. #endif
  281. while( IdChar(z[i]) ){
  282. *tokenType = TK_ILLEGAL;
  283. i++;
  284. }
  285. return i;
  286. }
  287. case '[': {
  288. for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
  289. *tokenType = TK_ID;
  290. return i;
  291. }
  292. case '?': {
  293. *tokenType = TK_VARIABLE;
  294. for(i=1; isdigit(z[i]); i++){}
  295. return i;
  296. }
  297. case '#': {
  298. for(i=1; isdigit(z[i]); i++){}
  299. if( i>1 ){
  300. /* Parameters of the form #NNN (where NNN is a number) are used
  301. ** internally by sqlite3NestedParse. */
  302. *tokenType = TK_REGISTER;
  303. return i;
  304. }
  305. /* Fall through into the next case if the '#' is not followed by
  306. ** a digit. Try to match #AAAA where AAAA is a parameter name. */
  307. }
  308. #ifndef SQLITE_OMIT_TCL_VARIABLE
  309. case '$':
  310. #endif
  311. case '@': /* For compatibility with MS SQL Server */
  312. case ':': {
  313. int n = 0;
  314. *tokenType = TK_VARIABLE;
  315. for(i=1; (c=z[i])!=0; i++){
  316. if( IdChar(c) ){
  317. n++;
  318. #ifndef SQLITE_OMIT_TCL_VARIABLE
  319. }else if( c=='(' && n>0 ){
  320. do{
  321. i++;
  322. }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
  323. if( c==')' ){
  324. i++;
  325. }else{
  326. *tokenType = TK_ILLEGAL;
  327. }
  328. break;
  329. }else if( c==':' && z[i+1]==':' ){
  330. i++;
  331. #endif
  332. }else{
  333. break;
  334. }
  335. }
  336. if( n==0 ) *tokenType = TK_ILLEGAL;
  337. return i;
  338. }
  339. #ifndef SQLITE_OMIT_BLOB_LITERAL
  340. case 'x': case 'X': {
  341. if( (c=z[1])=='\'' || c=='"' ){
  342. int delim = c;
  343. *tokenType = TK_BLOB;
  344. for(i=2; (c=z[i])!=0; i++){
  345. if( c==delim ){
  346. if( i%2 ) *tokenType = TK_ILLEGAL;
  347. break;
  348. }
  349. if( !isxdigit(c) ){
  350. *tokenType = TK_ILLEGAL;
  351. return i;
  352. }
  353. }
  354. if( c ) i++;
  355. return i;
  356. }
  357. /* Otherwise fall through to the next case */
  358. }
  359. #endif
  360. default: {
  361. if( !IdChar(*z) ){
  362. break;
  363. }
  364. for(i=1; IdChar(z[i]); i++){}
  365. *tokenType = keywordCode((char*)z, i);
  366. return i;
  367. }
  368. }
  369. *tokenType = TK_ILLEGAL;
  370. return 1;
  371. }
  372. int sqlite3GetToken(const unsigned char *z, int *tokenType){
  373. return getToken(z, tokenType);
  374. }
  375. /*
  376. ** Run the parser on the given SQL string. The parser structure is
  377. ** passed in. An SQLITE_ status code is returned. If an error occurs
  378. ** and pzErrMsg!=NULL then an error message might be written into
  379. ** memory obtained from sqlite3_malloc() and *pzErrMsg made to point to that
  380. ** error message. Or maybe not.
  381. */
  382. int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
  383. int nErr = 0;
  384. int i;
  385. void *pEngine;
  386. int tokenType;
  387. int lastTokenParsed = -1;
  388. sqlite3 *db = pParse->db;
  389. if( db->activeVdbeCnt==0 ){
  390. db->u1.isInterrupted = 0;
  391. }
  392. pParse->rc = SQLITE_OK;
  393. i = 0;
  394. pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3_malloc);
  395. if( pEngine==0 ){
  396. db->mallocFailed = 1;
  397. return SQLITE_NOMEM;
  398. }
  399. assert( pParse->sLastToken.dyn==0 );
  400. assert( pParse->pNewTable==0 );
  401. assert( pParse->pNewTrigger==0 );
  402. assert( pParse->nVar==0 );
  403. assert( pParse->nVarExpr==0 );
  404. assert( pParse->nVarExprAlloc==0 );
  405. assert( pParse->apVarExpr==0 );
  406. pParse->zTail = pParse->zSql = zSql;
  407. while( !db->mallocFailed && zSql[i]!=0 ){
  408. assert( i>=0 );
  409. pParse->sLastToken.z = (u8*)&zSql[i];
  410. assert( pParse->sLastToken.dyn==0 );
  411. pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
  412. i += pParse->sLastToken.n;
  413. if( i>SQLITE_MAX_SQL_LENGTH ){
  414. pParse->rc = SQLITE_TOOBIG;
  415. break;
  416. }
  417. switch( tokenType ){
  418. case TK_SPACE:
  419. case TK_COMMENT: {
  420. if( db->u1.isInterrupted ){
  421. pParse->rc = SQLITE_INTERRUPT;
  422. sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
  423. goto abort_parse;
  424. }
  425. break;
  426. }
  427. case TK_ILLEGAL: {
  428. if( pzErrMsg ){
  429. sqlite3_free(*pzErrMsg);
  430. *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
  431. &pParse->sLastToken);
  432. }
  433. nErr++;
  434. goto abort_parse;
  435. }
  436. case TK_SEMI: {
  437. pParse->zTail = &zSql[i];
  438. /* Fall thru into the default case */
  439. }
  440. default: {
  441. sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
  442. lastTokenParsed = tokenType;
  443. if( pParse->rc!=SQLITE_OK ){
  444. goto abort_parse;
  445. }
  446. break;
  447. }
  448. }
  449. }
  450. abort_parse:
  451. if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
  452. if( lastTokenParsed!=TK_SEMI ){
  453. sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
  454. pParse->zTail = &zSql[i];
  455. }
  456. sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
  457. }
  458. sqlite3ParserFree(pEngine, sqlite3_free);
  459. if( db->mallocFailed ){
  460. pParse->rc = SQLITE_NOMEM;
  461. }
  462. if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
  463. sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
  464. }
  465. if( pParse->zErrMsg ){
  466. if( pzErrMsg && *pzErrMsg==0 ){
  467. *pzErrMsg = pParse->zErrMsg;
  468. }else{
  469. sqlite3_free(pParse->zErrMsg);
  470. }
  471. pParse->zErrMsg = 0;
  472. if( !nErr ) nErr++;
  473. }
  474. if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
  475. sqlite3VdbeDelete(pParse->pVdbe);
  476. pParse->pVdbe = 0;
  477. }
  478. #ifndef SQLITE_OMIT_SHARED_CACHE
  479. if( pParse->nested==0 ){
  480. sqlite3_free(pParse->aTableLock);
  481. pParse->aTableLock = 0;
  482. pParse->nTableLock = 0;
  483. }
  484. #endif
  485. if( !IN_DECLARE_VTAB ){
  486. /* If the pParse->declareVtab flag is set, do not delete any table
  487. ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
  488. ** will take responsibility for freeing the Table structure.
  489. */
  490. sqlite3DeleteTable(pParse->pNewTable);
  491. }
  492. sqlite3DeleteTrigger(pParse->pNewTrigger);
  493. sqlite3_free(pParse->apVarExpr);
  494. if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
  495. pParse->rc = SQLITE_ERROR;
  496. }
  497. return nErr;
  498. }