tokenize.c 15 KB

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