PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/0.5.5/krunning/sqlite/tokenize.c

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