xref: /sqlite-3.40.0/src/parse.y (revision fd3b2226)
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 ** This file contains SQLite's grammar for SQL.  Process this file
13 ** using the lemon parser generator to generate C code that runs
14 ** the parser.  Lemon will also generate a header file containing
15 ** numeric codes for all of the tokens.
16 **
17 ** @(#) $Id: parse.y,v 1.286 2009/08/10 03:57:58 shane Exp $
18 */
19 
20 // All token codes are small integers with #defines that begin with "TK_"
21 %token_prefix TK_
22 
23 // The type of the data attached to each token is Token.  This is also the
24 // default type for non-terminals.
25 //
26 %token_type {Token}
27 %default_type {Token}
28 
29 // The generated parser function takes a 4th argument as follows:
30 %extra_argument {Parse *pParse}
31 
32 // This code runs whenever there is a syntax error
33 //
34 %syntax_error {
35   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
36   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
37   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
38   pParse->parseError = 1;
39 }
40 %stack_overflow {
41   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
42   sqlite3ErrorMsg(pParse, "parser stack overflow");
43   pParse->parseError = 1;
44 }
45 
46 // The name of the generated procedure that implements the parser
47 // is as follows:
48 %name sqlite3Parser
49 
50 // The following text is included near the beginning of the C source
51 // code file that implements the parser.
52 //
53 %include {
54 #include "sqliteInt.h"
55 
56 /*
57 ** Disable all error recovery processing in the parser push-down
58 ** automaton.
59 */
60 #define YYNOERRORRECOVERY 1
61 
62 /*
63 ** Make yytestcase() the same as testcase()
64 */
65 #define yytestcase(X) testcase(X)
66 
67 /*
68 ** An instance of this structure holds information about the
69 ** LIMIT clause of a SELECT statement.
70 */
71 struct LimitVal {
72   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
73   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
74 };
75 
76 /*
77 ** An instance of this structure is used to store the LIKE,
78 ** GLOB, NOT LIKE, and NOT GLOB operators.
79 */
80 struct LikeOp {
81   Token eOperator;  /* "like" or "glob" or "regexp" */
82   int not;         /* True if the NOT keyword is present */
83 };
84 
85 /*
86 ** An instance of the following structure describes the event of a
87 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
88 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
89 **
90 **      UPDATE ON (a,b,c)
91 **
92 ** Then the "b" IdList records the list "a,b,c".
93 */
94 struct TrigEvent { int a; IdList * b; };
95 
96 /*
97 ** An instance of this structure holds the ATTACH key and the key type.
98 */
99 struct AttachKey { int type;  Token key; };
100 
101 } // end %include
102 
103 // Input is a single SQL command
104 input ::= cmdlist.
105 cmdlist ::= cmdlist ecmd.
106 cmdlist ::= ecmd.
107 ecmd ::= SEMI.
108 ecmd ::= explain cmdx SEMI.
109 explain ::= .           { sqlite3BeginParse(pParse, 0); }
110 %ifndef SQLITE_OMIT_EXPLAIN
111 explain ::= EXPLAIN.              { sqlite3BeginParse(pParse, 1); }
112 explain ::= EXPLAIN QUERY PLAN.   { sqlite3BeginParse(pParse, 2); }
113 %endif  SQLITE_OMIT_EXPLAIN
114 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
115 
116 ///////////////////// Begin and end transactions. ////////////////////////////
117 //
118 
119 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
120 trans_opt ::= .
121 trans_opt ::= TRANSACTION.
122 trans_opt ::= TRANSACTION nm.
123 %type transtype {int}
124 transtype(A) ::= .             {A = TK_DEFERRED;}
125 transtype(A) ::= DEFERRED(X).  {A = @X;}
126 transtype(A) ::= IMMEDIATE(X). {A = @X;}
127 transtype(A) ::= EXCLUSIVE(X). {A = @X;}
128 cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
129 cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
130 cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
131 
132 savepoint_opt ::= SAVEPOINT.
133 savepoint_opt ::= .
134 cmd ::= SAVEPOINT nm(X). {
135   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
136 }
137 cmd ::= RELEASE savepoint_opt nm(X). {
138   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
139 }
140 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
141   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
142 }
143 
144 ///////////////////// The CREATE TABLE statement ////////////////////////////
145 //
146 cmd ::= create_table create_table_args.
147 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
148    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
149 }
150 createkw(A) ::= CREATE(X).  {
151   pParse->db->lookaside.bEnabled = 0;
152   A = X;
153 }
154 %type ifnotexists {int}
155 ifnotexists(A) ::= .              {A = 0;}
156 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
157 %type temp {int}
158 %ifndef SQLITE_OMIT_TEMPDB
159 temp(A) ::= TEMP.  {A = 1;}
160 %endif  SQLITE_OMIT_TEMPDB
161 temp(A) ::= .      {A = 0;}
162 create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
163   sqlite3EndTable(pParse,&X,&Y,0);
164 }
165 create_table_args ::= AS select(S). {
166   sqlite3EndTable(pParse,0,0,S);
167   sqlite3SelectDelete(pParse->db, S);
168 }
169 columnlist ::= columnlist COMMA column.
170 columnlist ::= column.
171 
172 // A "column" is a complete description of a single column in a
173 // CREATE TABLE statement.  This includes the column name, its
174 // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
175 // NOT NULL and so forth.
176 //
177 column(A) ::= columnid(X) type carglist. {
178   A.z = X.z;
179   A.n = (int)(pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
180 }
181 columnid(A) ::= nm(X). {
182   sqlite3AddColumn(pParse,&X);
183   A = X;
184 }
185 
186 
187 // An IDENTIFIER can be a generic identifier, or one of several
188 // keywords.  Any non-standard keyword can also be an identifier.
189 //
190 %type id {Token}
191 id(A) ::= ID(X).         {A = X;}
192 id(A) ::= INDEXED(X).    {A = X;}
193 
194 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
195 // fallback to ID if they will not parse as their original value.
196 // This obviates the need for the "id" nonterminal.
197 //
198 %fallback ID
199   ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
200   CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
201   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
202   QUERY KEY OF OFFSET PRAGMA RAISE RELEASE REPLACE RESTRICT ROW ROLLBACK
203   SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL
204 %ifdef SQLITE_OMIT_COMPOUND_SELECT
205   EXCEPT INTERSECT UNION
206 %endif SQLITE_OMIT_COMPOUND_SELECT
207   REINDEX RENAME CTIME_KW IF
208   .
209 %wildcard ANY.
210 
211 // Define operator precedence early so that this is the first occurance
212 // of the operator tokens in the grammer.  Keeping the operators together
213 // causes them to be assigned integer values that are close together,
214 // which keeps parser tables smaller.
215 //
216 // The token values assigned to these symbols is determined by the order
217 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
218 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
219 // the sqlite3ExprIfFalse() routine for additional information on this
220 // constraint.
221 //
222 %left OR.
223 %left AND.
224 %right NOT.
225 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
226 %left GT LE LT GE.
227 %right ESCAPE.
228 %left BITAND BITOR LSHIFT RSHIFT.
229 %left PLUS MINUS.
230 %left STAR SLASH REM.
231 %left CONCAT.
232 %left COLLATE.
233 %right BITNOT.
234 
235 // And "ids" is an identifer-or-string.
236 //
237 %type ids {Token}
238 ids(A) ::= ID|STRING(X).   {A = X;}
239 
240 // The name of a column or table can be any of the following:
241 //
242 %type nm {Token}
243 nm(A) ::= id(X).         {A = X;}
244 nm(A) ::= STRING(X).     {A = X;}
245 nm(A) ::= JOIN_KW(X).    {A = X;}
246 
247 // A typetoken is really one or more tokens that form a type name such
248 // as can be found after the column name in a CREATE TABLE statement.
249 // Multiple tokens are concatenated to form the value of the typetoken.
250 //
251 %type typetoken {Token}
252 type ::= .
253 type ::= typetoken(X).                   {sqlite3AddColumnType(pParse,&X);}
254 typetoken(A) ::= typename(X).   {A = X;}
255 typetoken(A) ::= typename(X) LP signed RP(Y). {
256   A.z = X.z;
257   A.n = (int)(&Y.z[Y.n] - X.z);
258 }
259 typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
260   A.z = X.z;
261   A.n = (int)(&Y.z[Y.n] - X.z);
262 }
263 %type typename {Token}
264 typename(A) ::= ids(X).             {A = X;}
265 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(int)(Y.z-X.z);}
266 signed ::= plus_num.
267 signed ::= minus_num.
268 
269 // "carglist" is a list of additional constraints that come after the
270 // column name and column type in a CREATE TABLE statement.
271 //
272 carglist ::= carglist carg.
273 carglist ::= .
274 carg ::= CONSTRAINT nm ccons.
275 carg ::= ccons.
276 ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
277 ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
278 ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
279 ccons ::= DEFAULT MINUS(A) term(X).      {
280   ExprSpan v;
281   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0);
282   v.zStart = A.z;
283   v.zEnd = X.zEnd;
284   sqlite3AddDefaultValue(pParse,&v);
285 }
286 ccons ::= DEFAULT id(X).              {
287   ExprSpan v;
288   spanExpr(&v, pParse, TK_STRING, &X);
289   sqlite3AddDefaultValue(pParse,&v);
290 }
291 
292 // In addition to the type name, we also care about the primary key and
293 // UNIQUE constraints.
294 //
295 ccons ::= NULL onconf.
296 ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
297 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
298                                  {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
299 ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
300 ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
301 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
302                                  {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
303 ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
304 ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
305 
306 // The optional AUTOINCREMENT keyword
307 %type autoinc {int}
308 autoinc(X) ::= .          {X = 0;}
309 autoinc(X) ::= AUTOINCR.  {X = 1;}
310 
311 // The next group of rules parses the arguments to a REFERENCES clause
312 // that determine if the referential integrity checking is deferred or
313 // or immediate and which determine what action to take if a ref-integ
314 // check fails.
315 //
316 %type refargs {int}
317 refargs(A) ::= .                     { A = OE_None * 0x000101; }
318 refargs(A) ::= refargs(X) refarg(Y). { A = (X & ~Y.mask) | Y.value; }
319 %type refarg {struct {int value; int mask;}}
320 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
321 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
322 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
323 %type refact {int}
324 refact(A) ::= SET NULL.              { A = OE_SetNull; }
325 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt; }
326 refact(A) ::= CASCADE.               { A = OE_Cascade; }
327 refact(A) ::= RESTRICT.              { A = OE_Restrict; }
328 refact(A) ::= NO ACTION.             { A = OE_None; }
329 %type defer_subclause {int}
330 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
331 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
332 %type init_deferred_pred_opt {int}
333 init_deferred_pred_opt(A) ::= .                       {A = 0;}
334 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
335 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
336 
337 // For the time being, the only constraint we care about is the primary
338 // key and UNIQUE.  Both create indices.
339 //
340 conslist_opt(A) ::= .                   {A.n = 0; A.z = 0;}
341 conslist_opt(A) ::= COMMA(X) conslist.  {A = X;}
342 conslist ::= conslist COMMA tcons.
343 conslist ::= conslist tcons.
344 conslist ::= tcons.
345 tcons ::= CONSTRAINT nm.
346 tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
347                                  {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
348 tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
349                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
350 tcons ::= CHECK LP expr(E) RP onconf.
351                                  {sqlite3AddCheckConstraint(pParse,E.pExpr);}
352 tcons ::= FOREIGN KEY LP idxlist(FA) RP
353           REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
354     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
355     sqlite3DeferForeignKey(pParse, D);
356 }
357 %type defer_subclause_opt {int}
358 defer_subclause_opt(A) ::= .                    {A = 0;}
359 defer_subclause_opt(A) ::= defer_subclause(X).  {A = X;}
360 
361 // The following is a non-standard extension that allows us to declare the
362 // default behavior when there is a constraint conflict.
363 //
364 %type onconf {int}
365 %type orconf {u8}
366 %type resolvetype {int}
367 onconf(A) ::= .                              {A = OE_Default;}
368 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
369 orconf(A) ::= .                              {A = OE_Default;}
370 orconf(A) ::= OR resolvetype(X).             {A = (u8)X;}
371 resolvetype(A) ::= raisetype(X).             {A = X;}
372 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
373 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
374 
375 ////////////////////////// The DROP TABLE /////////////////////////////////////
376 //
377 cmd ::= DROP TABLE ifexists(E) fullname(X). {
378   sqlite3DropTable(pParse, X, 0, E);
379 }
380 %type ifexists {int}
381 ifexists(A) ::= IF EXISTS.   {A = 1;}
382 ifexists(A) ::= .            {A = 0;}
383 
384 ///////////////////// The CREATE VIEW statement /////////////////////////////
385 //
386 %ifndef SQLITE_OMIT_VIEW
387 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). {
388   sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E);
389 }
390 cmd ::= DROP VIEW ifexists(E) fullname(X). {
391   sqlite3DropTable(pParse, X, 1, E);
392 }
393 %endif  SQLITE_OMIT_VIEW
394 
395 //////////////////////// The SELECT statement /////////////////////////////////
396 //
397 cmd ::= select(X).  {
398   SelectDest dest = {SRT_Output, 0, 0, 0, 0};
399   sqlite3Select(pParse, X, &dest);
400   sqlite3SelectDelete(pParse->db, X);
401 }
402 
403 %type select {Select*}
404 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
405 %type oneselect {Select*}
406 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
407 
408 select(A) ::= oneselect(X).                      {A = X;}
409 %ifndef SQLITE_OMIT_COMPOUND_SELECT
410 select(A) ::= select(X) multiselect_op(Y) oneselect(Z).  {
411   if( Z ){
412     Z->op = (u8)Y;
413     Z->pPrior = X;
414   }else{
415     sqlite3SelectDelete(pParse->db, X);
416   }
417   A = Z;
418 }
419 %type multiselect_op {int}
420 multiselect_op(A) ::= UNION(OP).             {A = @OP;}
421 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
422 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
423 %endif SQLITE_OMIT_COMPOUND_SELECT
424 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
425                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
426   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
427 }
428 
429 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
430 // present and false (0) if it is not.
431 //
432 %type distinct {int}
433 distinct(A) ::= DISTINCT.   {A = 1;}
434 distinct(A) ::= ALL.        {A = 0;}
435 distinct(A) ::= .           {A = 0;}
436 
437 // selcollist is a list of expressions that are to become the return
438 // values of the SELECT statement.  The "*" in statements like
439 // "SELECT * FROM ..." is encoded as a special expression with an
440 // opcode of TK_ALL.
441 //
442 %type selcollist {ExprList*}
443 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
444 %type sclp {ExprList*}
445 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
446 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
447 sclp(A) ::= .                                {A = 0;}
448 selcollist(A) ::= sclp(P) expr(X) as(Y).     {
449    A = sqlite3ExprListAppend(pParse, P, X.pExpr);
450    if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
451    sqlite3ExprListSetSpan(pParse,A,&X);
452 }
453 selcollist(A) ::= sclp(P) STAR. {
454   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
455   A = sqlite3ExprListAppend(pParse, P, p);
456 }
457 selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). {
458   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &Y);
459   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
460   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
461   A = sqlite3ExprListAppend(pParse,P, pDot);
462 }
463 
464 // An option "AS <id>" phrase that can follow one of the expressions that
465 // define the result set, or one of the tables in the FROM clause.
466 //
467 %type as {Token}
468 as(X) ::= AS nm(Y).    {X = Y;}
469 as(X) ::= ids(Y).      {X = Y;}
470 as(X) ::= .            {X.n = 0;}
471 
472 
473 %type seltablist {SrcList*}
474 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
475 %type stl_prefix {SrcList*}
476 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
477 %type from {SrcList*}
478 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
479 
480 // A complete FROM clause.
481 //
482 from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
483 from(A) ::= FROM seltablist(X). {
484   A = X;
485   sqlite3SrcListShiftJoinType(A);
486 }
487 
488 // "seltablist" is a "Select Table List" - the content of the FROM clause
489 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
490 //
491 stl_prefix(A) ::= seltablist(X) joinop(Y).    {
492    A = X;
493    if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].jointype = (u8)Y;
494 }
495 stl_prefix(A) ::= .                           {A = 0;}
496 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I) on_opt(N) using_opt(U). {
497   A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U);
498   sqlite3SrcListIndexedBy(pParse, A, &I);
499 }
500 %ifndef SQLITE_OMIT_SUBQUERY
501   seltablist(A) ::= stl_prefix(X) LP select(S) RP
502                     as(Z) on_opt(N) using_opt(U). {
503     A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,S,N,U);
504   }
505   seltablist(A) ::= stl_prefix(X) LP seltablist(F) RP
506                     as(Z) on_opt(N) using_opt(U). {
507     if( X==0 && Z.n==0 && N==0 && U==0 ){
508       A = F;
509     }else{
510       Select *pSubquery;
511       sqlite3SrcListShiftJoinType(F);
512       pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,0,0,0);
513       A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,pSubquery,N,U);
514     }
515   }
516 
517   // A seltablist_paren nonterminal represents anything in a FROM that
518   // is contained inside parentheses.  This can be either a subquery or
519   // a grouping of table and subqueries.
520   //
521 //  %type seltablist_paren {Select*}
522 //  %destructor seltablist_paren {sqlite3SelectDelete(pParse->db, $$);}
523 //  seltablist_paren(A) ::= select(S).      {A = S;}
524 //  seltablist_paren(A) ::= seltablist(F).  {
525 //     sqlite3SrcListShiftJoinType(F);
526 //     A = sqlite3SelectNew(pParse,0,F,0,0,0,0,0,0,0);
527 //  }
528 %endif  SQLITE_OMIT_SUBQUERY
529 
530 %type dbnm {Token}
531 dbnm(A) ::= .          {A.z=0; A.n=0;}
532 dbnm(A) ::= DOT nm(X). {A = X;}
533 
534 %type fullname {SrcList*}
535 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
536 fullname(A) ::= nm(X) dbnm(Y).  {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y);}
537 
538 %type joinop {int}
539 %type joinop2 {int}
540 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
541 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
542 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
543 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
544                                        { X = sqlite3JoinType(pParse,&A,&B,&C); }
545 
546 %type on_opt {Expr*}
547 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
548 on_opt(N) ::= ON expr(E).   {N = E.pExpr;}
549 on_opt(N) ::= .             {N = 0;}
550 
551 // Note that this block abuses the Token type just a little. If there is
552 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
553 // there is an INDEXED BY clause, then the token is populated as per normal,
554 // with z pointing to the token data and n containing the number of bytes
555 // in the token.
556 //
557 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
558 // normally illegal. The sqlite3SrcListIndexedBy() function
559 // recognizes and interprets this as a special case.
560 //
561 %type indexed_opt {Token}
562 indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
563 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
564 indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
565 
566 %type using_opt {IdList*}
567 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
568 using_opt(U) ::= USING LP inscollist(L) RP.  {U = L;}
569 using_opt(U) ::= .                        {U = 0;}
570 
571 
572 %type orderby_opt {ExprList*}
573 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
574 %type sortlist {ExprList*}
575 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
576 %type sortitem {Expr*}
577 %destructor sortitem {sqlite3ExprDelete(pParse->db, $$);}
578 
579 orderby_opt(A) ::= .                          {A = 0;}
580 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
581 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
582   A = sqlite3ExprListAppend(pParse,X,Y);
583   if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
584 }
585 sortlist(A) ::= sortitem(Y) sortorder(Z). {
586   A = sqlite3ExprListAppend(pParse,0,Y);
587   if( A && ALWAYS(A->a) ) A->a[0].sortOrder = (u8)Z;
588 }
589 sortitem(A) ::= expr(X).   {A = X.pExpr;}
590 
591 %type sortorder {int}
592 
593 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
594 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
595 sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
596 
597 %type groupby_opt {ExprList*}
598 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
599 groupby_opt(A) ::= .                      {A = 0;}
600 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
601 
602 %type having_opt {Expr*}
603 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
604 having_opt(A) ::= .                {A = 0;}
605 having_opt(A) ::= HAVING expr(X).  {A = X.pExpr;}
606 
607 %type limit_opt {struct LimitVal}
608 
609 // The destructor for limit_opt will never fire in the current grammar.
610 // The limit_opt non-terminal only occurs at the end of a single production
611 // rule for SELECT statements.  As soon as the rule that create the
612 // limit_opt non-terminal reduces, the SELECT statement rule will also
613 // reduce.  So there is never a limit_opt non-terminal on the stack
614 // except as a transient.  So there is never anything to destroy.
615 //
616 //%destructor limit_opt {
617 //  sqlite3ExprDelete(pParse->db, $$.pLimit);
618 //  sqlite3ExprDelete(pParse->db, $$.pOffset);
619 //}
620 limit_opt(A) ::= .                    {A.pLimit = 0; A.pOffset = 0;}
621 limit_opt(A) ::= LIMIT expr(X).       {A.pLimit = X.pExpr; A.pOffset = 0;}
622 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
623                                       {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
624 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
625                                       {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
626 
627 /////////////////////////// The DELETE statement /////////////////////////////
628 //
629 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
630 cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
631         orderby_opt(O) limit_opt(L). {
632   sqlite3SrcListIndexedBy(pParse, X, &I);
633   W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE");
634   sqlite3DeleteFrom(pParse,X,W);
635 }
636 %endif
637 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
638 cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
639   sqlite3SrcListIndexedBy(pParse, X, &I);
640   sqlite3DeleteFrom(pParse,X,W);
641 }
642 %endif
643 
644 %type where_opt {Expr*}
645 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
646 
647 where_opt(A) ::= .                    {A = 0;}
648 where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
649 
650 ////////////////////////// The UPDATE command ////////////////////////////////
651 //
652 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
653 cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W) orderby_opt(O) limit_opt(L).  {
654   sqlite3SrcListIndexedBy(pParse, X, &I);
655   sqlite3ExprListCheckLength(pParse,Y,"set list");
656   W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
657   sqlite3Update(pParse,X,Y,W,R);
658 }
659 %endif
660 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
661 cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W).  {
662   sqlite3SrcListIndexedBy(pParse, X, &I);
663   sqlite3ExprListCheckLength(pParse,Y,"set list");
664   sqlite3Update(pParse,X,Y,W,R);
665 }
666 %endif
667 
668 %type setlist {ExprList*}
669 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
670 
671 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). {
672   A = sqlite3ExprListAppend(pParse, Z, Y.pExpr);
673   sqlite3ExprListSetName(pParse, A, &X, 1);
674 }
675 setlist(A) ::= nm(X) EQ expr(Y). {
676   A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
677   sqlite3ExprListSetName(pParse, A, &X, 1);
678 }
679 
680 ////////////////////////// The INSERT command /////////////////////////////////
681 //
682 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
683         VALUES LP itemlist(Y) RP.
684             {sqlite3Insert(pParse, X, Y, 0, F, R);}
685 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
686             {sqlite3Insert(pParse, X, 0, S, F, R);}
687 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
688             {sqlite3Insert(pParse, X, 0, 0, F, R);}
689 
690 %type insert_cmd {u8}
691 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
692 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
693 
694 
695 %type itemlist {ExprList*}
696 %destructor itemlist {sqlite3ExprListDelete(pParse->db, $$);}
697 
698 itemlist(A) ::= itemlist(X) COMMA expr(Y).
699     {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);}
700 itemlist(A) ::= expr(X).
701     {A = sqlite3ExprListAppend(pParse,0,X.pExpr);}
702 
703 %type inscollist_opt {IdList*}
704 %destructor inscollist_opt {sqlite3IdListDelete(pParse->db, $$);}
705 %type inscollist {IdList*}
706 %destructor inscollist {sqlite3IdListDelete(pParse->db, $$);}
707 
708 inscollist_opt(A) ::= .                       {A = 0;}
709 inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
710 inscollist(A) ::= inscollist(X) COMMA nm(Y).
711     {A = sqlite3IdListAppend(pParse->db,X,&Y);}
712 inscollist(A) ::= nm(Y).
713     {A = sqlite3IdListAppend(pParse->db,0,&Y);}
714 
715 /////////////////////////// Expression Processing /////////////////////////////
716 //
717 
718 %type expr {ExprSpan}
719 %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
720 %type term {ExprSpan}
721 %destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
722 
723 %include {
724   /* This is a utility routine used to set the ExprSpan.zStart and
725   ** ExprSpan.zEnd values of pOut so that the span covers the complete
726   ** range of text beginning with pStart and going to the end of pEnd.
727   */
728   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
729     pOut->zStart = pStart->z;
730     pOut->zEnd = &pEnd->z[pEnd->n];
731   }
732 
733   /* Construct a new Expr object from a single identifier.  Use the
734   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
735   ** that created the expression.
736   */
737   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
738     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
739     pOut->zStart = pValue->z;
740     pOut->zEnd = &pValue->z[pValue->n];
741   }
742 }
743 
744 expr(A) ::= term(X).             {A = X;}
745 expr(A) ::= LP(B) expr(X) RP(E). {A.pExpr = X.pExpr; spanSet(&A,&B,&E);}
746 term(A) ::= NULL(X).             {spanExpr(&A, pParse, @X, &X);}
747 expr(A) ::= id(X).               {spanExpr(&A, pParse, TK_ID, &X);}
748 expr(A) ::= JOIN_KW(X).          {spanExpr(&A, pParse, TK_ID, &X);}
749 expr(A) ::= nm(X) DOT nm(Y). {
750   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
751   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
752   A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
753   spanSet(&A,&X,&Y);
754 }
755 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
756   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
757   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
758   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z);
759   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
760   A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
761   spanSet(&A,&X,&Z);
762 }
763 term(A) ::= INTEGER|FLOAT|BLOB(X).  {spanExpr(&A, pParse, @X, &X);}
764 term(A) ::= STRING(X).              {spanExpr(&A, pParse, @X, &X);}
765 expr(A) ::= REGISTER(X).     {
766   /* When doing a nested parse, one can include terms in an expression
767   ** that look like this:   #1 #2 ...  These terms refer to registers
768   ** in the virtual machine.  #N is the N-th register. */
769   if( pParse->nested==0 ){
770     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X);
771     A.pExpr = 0;
772   }else{
773     A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X);
774     if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable);
775   }
776   spanSet(&A, &X, &X);
777 }
778 expr(A) ::= VARIABLE(X).     {
779   spanExpr(&A, pParse, TK_VARIABLE, &X);
780   sqlite3ExprAssignVarNumber(pParse, A.pExpr);
781   spanSet(&A, &X, &X);
782 }
783 expr(A) ::= expr(E) COLLATE ids(C). {
784   A.pExpr = sqlite3ExprSetColl(pParse, E.pExpr, &C);
785   A.zStart = E.zStart;
786   A.zEnd = &C.z[C.n];
787 }
788 %ifndef SQLITE_OMIT_CAST
789 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
790   A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
791   spanSet(&A,&X,&Y);
792 }
793 %endif  SQLITE_OMIT_CAST
794 expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
795   if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
796     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
797   }
798   A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
799   spanSet(&A,&X,&E);
800   if( D && A.pExpr ){
801     A.pExpr->flags |= EP_Distinct;
802   }
803 }
804 expr(A) ::= ID(X) LP STAR RP(E). {
805   A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
806   spanSet(&A,&X,&E);
807 }
808 term(A) ::= CTIME_KW(OP). {
809   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
810   ** treated as functions that return constants */
811   A.pExpr = sqlite3ExprFunction(pParse, 0,&OP);
812   if( A.pExpr ){
813     A.pExpr->op = TK_CONST_FUNC;
814   }
815   spanSet(&A, &OP, &OP);
816 }
817 
818 %include {
819   /* This routine constructs a binary expression node out of two ExprSpan
820   ** objects and uses the result to populate a new ExprSpan object.
821   */
822   static void spanBinaryExpr(
823     ExprSpan *pOut,     /* Write the result here */
824     Parse *pParse,      /* The parsing context.  Errors accumulate here */
825     int op,             /* The binary operation */
826     ExprSpan *pLeft,    /* The left operand */
827     ExprSpan *pRight    /* The right operand */
828   ){
829     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
830     pOut->zStart = pLeft->zStart;
831     pOut->zEnd = pRight->zEnd;
832   }
833 }
834 
835 expr(A) ::= expr(X) AND(OP) expr(Y).    {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
836 expr(A) ::= expr(X) OR(OP) expr(Y).     {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
837 expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).
838                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
839 expr(A) ::= expr(X) EQ|NE(OP) expr(Y).  {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
840 expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
841                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
842 expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).
843                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
844 expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y).
845                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
846 expr(A) ::= expr(X) CONCAT(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
847 %type likeop {struct LikeOp}
848 likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.not = 0;}
849 likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
850 likeop(A) ::= MATCH(X).       {A.eOperator = X; A.not = 0;}
851 likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.not = 1;}
852 %type escape {ExprSpan}
853 %destructor escape {sqlite3ExprDelete(pParse->db, $$.pExpr);}
854 escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
855 escape(X) ::= .               [ESCAPE] {memset(&X,0,sizeof(X));}
856 expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E).  [LIKE_KW]  {
857   ExprList *pList;
858   pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
859   pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
860   if( E.pExpr ){
861     pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
862   }
863   A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
864   if( OP.not ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
865   A.zStart = X.zStart;
866   A.zEnd = Y.zEnd;
867   if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
868 }
869 
870 %include {
871   /* Construct an expression node for a unary postfix operator
872   */
873   static void spanUnaryPostfix(
874     ExprSpan *pOut,        /* Write the new expression node here */
875     Parse *pParse,         /* Parsing context to record errors */
876     int op,                /* The operator */
877     ExprSpan *pOperand,    /* The operand */
878     Token *pPostOp         /* The operand token for setting the span */
879   ){
880     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
881     pOut->zStart = pOperand->zStart;
882     pOut->zEnd = &pPostOp->z[pPostOp->n];
883   }
884 }
885 
886 expr(A) ::= expr(X) ISNULL|NOTNULL(E).   {spanUnaryPostfix(&A,pParse,@E,&X,&E);}
887 expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);}
888 
889 //    expr1 IS expr2
890 //    expr1 IS NOT expr2
891 //
892 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
893 // is any other expression, code as TK_IS or TK_ISNOT.
894 //
895 expr(A) ::= expr(X) IS expr(Y).     {
896   spanBinaryExpr(&A,pParse,TK_IS,&X,&Y);
897   if( pParse->db->mallocFailed==0  && Y.pExpr->op==TK_NULL ){
898     A.pExpr->op = TK_ISNULL;
899   }
900 }
901 expr(A) ::= expr(X) IS NOT expr(Y). {
902   spanBinaryExpr(&A,pParse,TK_ISNOT,&X,&Y);
903   if( pParse->db->mallocFailed==0  && Y.pExpr->op==TK_NULL ){
904     A.pExpr->op = TK_NOTNULL;
905   }
906 }
907 
908 %include {
909   /* Construct an expression node for a unary prefix operator
910   */
911   static void spanUnaryPrefix(
912     ExprSpan *pOut,        /* Write the new expression node here */
913     Parse *pParse,         /* Parsing context to record errors */
914     int op,                /* The operator */
915     ExprSpan *pOperand,    /* The operand */
916     Token *pPreOp         /* The operand token for setting the span */
917   ){
918     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
919     pOut->zStart = pPreOp->z;
920     pOut->zEnd = pOperand->zEnd;
921   }
922 }
923 
924 
925 
926 expr(A) ::= NOT(B) expr(X).    {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
927 expr(A) ::= BITNOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
928 expr(A) ::= MINUS(B) expr(X). [BITNOT]
929                                {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);}
930 expr(A) ::= PLUS(B) expr(X). [BITNOT]
931                                {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);}
932 
933 %type between_op {int}
934 between_op(A) ::= BETWEEN.     {A = 0;}
935 between_op(A) ::= NOT BETWEEN. {A = 1;}
936 expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
937   ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
938   pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
939   A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, W.pExpr, 0, 0);
940   if( A.pExpr ){
941     A.pExpr->x.pList = pList;
942   }else{
943     sqlite3ExprListDelete(pParse->db, pList);
944   }
945   if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
946   A.zStart = W.zStart;
947   A.zEnd = Y.zEnd;
948 }
949 %ifndef SQLITE_OMIT_SUBQUERY
950   %type in_op {int}
951   in_op(A) ::= IN.      {A = 0;}
952   in_op(A) ::= NOT IN.  {A = 1;}
953   expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
954     A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
955     if( A.pExpr ){
956       A.pExpr->x.pList = Y;
957       sqlite3ExprSetHeight(pParse, A.pExpr);
958     }else{
959       sqlite3ExprListDelete(pParse->db, Y);
960     }
961     if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
962     A.zStart = X.zStart;
963     A.zEnd = &E.z[E.n];
964   }
965   expr(A) ::= LP(B) select(X) RP(E). {
966     A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
967     if( A.pExpr ){
968       A.pExpr->x.pSelect = X;
969       ExprSetProperty(A.pExpr, EP_xIsSelect);
970       sqlite3ExprSetHeight(pParse, A.pExpr);
971     }else{
972       sqlite3SelectDelete(pParse->db, X);
973     }
974     A.zStart = B.z;
975     A.zEnd = &E.z[E.n];
976   }
977   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
978     A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
979     if( A.pExpr ){
980       A.pExpr->x.pSelect = Y;
981       ExprSetProperty(A.pExpr, EP_xIsSelect);
982       sqlite3ExprSetHeight(pParse, A.pExpr);
983     }else{
984       sqlite3SelectDelete(pParse->db, Y);
985     }
986     if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
987     A.zStart = X.zStart;
988     A.zEnd = &E.z[E.n];
989   }
990   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
991     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
992     A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
993     if( A.pExpr ){
994       A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
995       ExprSetProperty(A.pExpr, EP_xIsSelect);
996       sqlite3ExprSetHeight(pParse, A.pExpr);
997     }else{
998       sqlite3SrcListDelete(pParse->db, pSrc);
999     }
1000     if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
1001     A.zStart = X.zStart;
1002     A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
1003   }
1004   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
1005     Expr *p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
1006     if( p ){
1007       p->x.pSelect = Y;
1008       ExprSetProperty(p, EP_xIsSelect);
1009       sqlite3ExprSetHeight(pParse, p);
1010     }else{
1011       sqlite3SelectDelete(pParse->db, Y);
1012     }
1013     A.zStart = B.z;
1014     A.zEnd = &E.z[E.n];
1015   }
1016 %endif SQLITE_OMIT_SUBQUERY
1017 
1018 /* CASE expressions */
1019 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
1020   A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, Z, 0);
1021   if( A.pExpr ){
1022     A.pExpr->x.pList = Y;
1023     sqlite3ExprSetHeight(pParse, A.pExpr);
1024   }else{
1025     sqlite3ExprListDelete(pParse->db, Y);
1026   }
1027   A.zStart = C.z;
1028   A.zEnd = &E.z[E.n];
1029 }
1030 %type case_exprlist {ExprList*}
1031 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1032 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
1033   A = sqlite3ExprListAppend(pParse,X, Y.pExpr);
1034   A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
1035 }
1036 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1037   A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
1038   A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
1039 }
1040 %type case_else {Expr*}
1041 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1042 case_else(A) ::=  ELSE expr(X).         {A = X.pExpr;}
1043 case_else(A) ::=  .                     {A = 0;}
1044 %type case_operand {Expr*}
1045 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1046 case_operand(A) ::= expr(X).            {A = X.pExpr;}
1047 case_operand(A) ::= .                   {A = 0;}
1048 
1049 %type exprlist {ExprList*}
1050 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1051 %type nexprlist {ExprList*}
1052 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1053 
1054 exprlist(A) ::= nexprlist(X).                {A = X;}
1055 exprlist(A) ::= .                            {A = 0;}
1056 nexprlist(A) ::= nexprlist(X) COMMA expr(Y).
1057     {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);}
1058 nexprlist(A) ::= expr(Y).
1059     {A = sqlite3ExprListAppend(pParse,0,Y.pExpr);}
1060 
1061 
1062 ///////////////////////////// The CREATE INDEX command ///////////////////////
1063 //
1064 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1065         ON nm(Y) LP idxlist(Z) RP(E). {
1066   sqlite3CreateIndex(pParse, &X, &D,
1067                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
1068                       &S, &E, SQLITE_SO_ASC, NE);
1069 }
1070 
1071 %type uniqueflag {int}
1072 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
1073 uniqueflag(A) ::= .        {A = OE_None;}
1074 
1075 %type idxlist {ExprList*}
1076 %destructor idxlist {sqlite3ExprListDelete(pParse->db, $$);}
1077 %type idxlist_opt {ExprList*}
1078 %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1079 
1080 idxlist_opt(A) ::= .                         {A = 0;}
1081 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
1082 idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z).  {
1083   Expr *p = 0;
1084   if( C.n>0 ){
1085     p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
1086     sqlite3ExprSetColl(pParse, p, &C);
1087   }
1088   A = sqlite3ExprListAppend(pParse,X, p);
1089   sqlite3ExprListSetName(pParse,A,&Y,1);
1090   sqlite3ExprListCheckLength(pParse, A, "index");
1091   if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
1092 }
1093 idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1094   Expr *p = 0;
1095   if( C.n>0 ){
1096     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
1097     sqlite3ExprSetColl(pParse, p, &C);
1098   }
1099   A = sqlite3ExprListAppend(pParse,0, p);
1100   sqlite3ExprListSetName(pParse, A, &Y, 1);
1101   sqlite3ExprListCheckLength(pParse, A, "index");
1102   if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
1103 }
1104 
1105 %type collate {Token}
1106 collate(C) ::= .                 {C.z = 0; C.n = 0;}
1107 collate(C) ::= COLLATE ids(X).   {C = X;}
1108 
1109 
1110 ///////////////////////////// The DROP INDEX command /////////////////////////
1111 //
1112 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
1113 
1114 ///////////////////////////// The VACUUM command /////////////////////////////
1115 //
1116 %ifndef SQLITE_OMIT_VACUUM
1117 %ifndef SQLITE_OMIT_ATTACH
1118 cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
1119 cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
1120 %endif  SQLITE_OMIT_ATTACH
1121 %endif  SQLITE_OMIT_VACUUM
1122 
1123 ///////////////////////////// The PRAGMA command /////////////////////////////
1124 //
1125 %ifndef SQLITE_OMIT_PRAGMA
1126 cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
1127 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1128 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1129 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1130                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1131 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1132                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1133 
1134 nmnum(A) ::= plus_num(X).             {A = X;}
1135 nmnum(A) ::= nm(X).                   {A = X;}
1136 nmnum(A) ::= ON(X).                   {A = X;}
1137 nmnum(A) ::= DELETE(X).               {A = X;}
1138 nmnum(A) ::= DEFAULT(X).              {A = X;}
1139 %endif SQLITE_OMIT_PRAGMA
1140 plus_num(A) ::= plus_opt number(X).   {A = X;}
1141 minus_num(A) ::= MINUS number(X).     {A = X;}
1142 number(A) ::= INTEGER|FLOAT(X).       {A = X;}
1143 plus_opt ::= PLUS.
1144 plus_opt ::= .
1145 
1146 //////////////////////////// The CREATE TRIGGER command /////////////////////
1147 
1148 %ifndef SQLITE_OMIT_TRIGGER
1149 
1150 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1151   Token all;
1152   all.z = A.z;
1153   all.n = (int)(Z.z - A.z) + Z.n;
1154   sqlite3FinishTrigger(pParse, S, &all);
1155 }
1156 
1157 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1158                     trigger_time(C) trigger_event(D)
1159                     ON fullname(E) foreach_clause when_clause(G). {
1160   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1161   A = (Z.n==0?B:Z);
1162 }
1163 
1164 %type trigger_time {int}
1165 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
1166 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
1167 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1168 trigger_time(A) ::= .            { A = TK_BEFORE; }
1169 
1170 %type trigger_event {struct TrigEvent}
1171 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1172 trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
1173 trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
1174 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
1175 
1176 foreach_clause ::= .
1177 foreach_clause ::= FOR EACH ROW.
1178 
1179 %type when_clause {Expr*}
1180 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1181 when_clause(A) ::= .             { A = 0; }
1182 when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
1183 
1184 %type trigger_cmd_list {TriggerStep*}
1185 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1186 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
1187   assert( Y!=0 );
1188   Y->pLast->pNext = X;
1189   Y->pLast = X;
1190   A = Y;
1191 }
1192 trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. {
1193   assert( X!=0 );
1194   X->pLast = X;
1195   A = X;
1196 }
1197 
1198 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1199 // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in
1200 // the same database as the table that the trigger fires on.
1201 //
1202 %type trnm {Token}
1203 trnm(A) ::= nm(X).   {A = X;}
1204 trnm(A) ::= nm DOT nm(X). {
1205   A = X;
1206   sqlite3ErrorMsg(pParse,
1207         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1208         "statements within triggers");
1209 }
1210 
1211 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1212 // statements within triggers.  We make a specific error message for this
1213 // since it is an exception to the default grammar rules.
1214 //
1215 tridxby ::= .
1216 tridxby ::= INDEXED BY nm. {
1217   sqlite3ErrorMsg(pParse,
1218         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1219         "within triggers");
1220 }
1221 tridxby ::= NOT INDEXED. {
1222   sqlite3ErrorMsg(pParse,
1223         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1224         "within triggers");
1225 }
1226 
1227 
1228 
1229 %type trigger_cmd {TriggerStep*}
1230 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1231 // UPDATE
1232 trigger_cmd(A) ::=
1233    UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).
1234    { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); }
1235 
1236 // INSERT
1237 trigger_cmd(A) ::=
1238    insert_cmd(R) INTO trnm(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
1239    {A = sqlite3TriggerInsertStep(pParse->db, &X, F, Y, 0, R);}
1240 
1241 trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) inscollist_opt(F) select(S).
1242                {A = sqlite3TriggerInsertStep(pParse->db, &X, F, 0, S, R);}
1243 
1244 // DELETE
1245 trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
1246                {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
1247 
1248 // SELECT
1249 trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(pParse->db, X); }
1250 
1251 // The special RAISE expression that may occur in trigger programs
1252 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
1253   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
1254   if( A.pExpr ){
1255     A.pExpr->affinity = OE_Ignore;
1256   }
1257   A.zStart = X.z;
1258   A.zEnd = &Y.z[Y.n];
1259 }
1260 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
1261   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z);
1262   if( A.pExpr ) {
1263     A.pExpr->affinity = (char)T;
1264   }
1265   A.zStart = X.z;
1266   A.zEnd = &Y.z[Y.n];
1267 }
1268 %endif  !SQLITE_OMIT_TRIGGER
1269 
1270 %type raisetype {int}
1271 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1272 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1273 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1274 
1275 
1276 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1277 %ifndef SQLITE_OMIT_TRIGGER
1278 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1279   sqlite3DropTrigger(pParse,X,NOERR);
1280 }
1281 %endif  !SQLITE_OMIT_TRIGGER
1282 
1283 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1284 %ifndef SQLITE_OMIT_ATTACH
1285 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1286   sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
1287 }
1288 cmd ::= DETACH database_kw_opt expr(D). {
1289   sqlite3Detach(pParse, D.pExpr);
1290 }
1291 
1292 %type key_opt {Expr*}
1293 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1294 key_opt(A) ::= .                     { A = 0; }
1295 key_opt(A) ::= KEY expr(X).          { A = X.pExpr; }
1296 
1297 database_kw_opt ::= DATABASE.
1298 database_kw_opt ::= .
1299 %endif SQLITE_OMIT_ATTACH
1300 
1301 ////////////////////////// REINDEX collation //////////////////////////////////
1302 %ifndef SQLITE_OMIT_REINDEX
1303 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1304 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1305 %endif  SQLITE_OMIT_REINDEX
1306 
1307 /////////////////////////////////// ANALYZE ///////////////////////////////////
1308 %ifndef SQLITE_OMIT_ANALYZE
1309 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1310 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1311 %endif
1312 
1313 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1314 %ifndef SQLITE_OMIT_ALTERTABLE
1315 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1316   sqlite3AlterRenameTable(pParse,X,&Z);
1317 }
1318 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1319   sqlite3AlterFinishAddColumn(pParse, &Y);
1320 }
1321 add_column_fullname ::= fullname(X). {
1322   pParse->db->lookaside.bEnabled = 0;
1323   sqlite3AlterBeginAddColumn(pParse, X);
1324 }
1325 kwcolumn_opt ::= .
1326 kwcolumn_opt ::= COLUMNKW.
1327 %endif  SQLITE_OMIT_ALTERTABLE
1328 
1329 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1330 %ifndef SQLITE_OMIT_VIRTUALTABLE
1331 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1332 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1333 create_vtab ::= createkw VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
1334     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
1335 }
1336 vtabarglist ::= vtabarg.
1337 vtabarglist ::= vtabarglist COMMA vtabarg.
1338 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1339 vtabarg ::= vtabarg vtabargtoken.
1340 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1341 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1342 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1343 anylist ::= .
1344 anylist ::= anylist LP anylist RP.
1345 anylist ::= anylist ANY.
1346 %endif  SQLITE_OMIT_VIRTUALTABLE
1347