xref: /sqlite-3.40.0/src/parse.y (revision 8a29dfde)
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.242 2008/03/29 12:50:33 rse 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   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
36   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
37   pParse->parseError = 1;
38 }
39 %stack_overflow {
40   sqlite3ErrorMsg(pParse, "parser stack overflow");
41   pParse->parseError = 1;
42 }
43 
44 // The name of the generated procedure that implements the parser
45 // is as follows:
46 %name sqlite3Parser
47 
48 // The following text is included near the beginning of the C source
49 // code file that implements the parser.
50 //
51 %include {
52 #include "sqliteInt.h"
53 
54 /*
55 ** An instance of this structure holds information about the
56 ** LIMIT clause of a SELECT statement.
57 */
58 struct LimitVal {
59   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
60   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
61 };
62 
63 /*
64 ** An instance of this structure is used to store the LIKE,
65 ** GLOB, NOT LIKE, and NOT GLOB operators.
66 */
67 struct LikeOp {
68   Token eOperator;  /* "like" or "glob" or "regexp" */
69   int not;         /* True if the NOT keyword is present */
70 };
71 
72 /*
73 ** An instance of the following structure describes the event of a
74 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
75 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
76 **
77 **      UPDATE ON (a,b,c)
78 **
79 ** Then the "b" IdList records the list "a,b,c".
80 */
81 struct TrigEvent { int a; IdList * b; };
82 
83 /*
84 ** An instance of this structure holds the ATTACH key and the key type.
85 */
86 struct AttachKey { int type;  Token key; };
87 
88 } // end %include
89 
90 // Input is a single SQL command
91 input ::= cmdlist.
92 cmdlist ::= cmdlist ecmd.
93 cmdlist ::= ecmd.
94 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
95 ecmd ::= SEMI.
96 ecmd ::= explain cmdx SEMI.
97 explain ::= .           { sqlite3BeginParse(pParse, 0); }
98 %ifndef SQLITE_OMIT_EXPLAIN
99 explain ::= EXPLAIN.              { sqlite3BeginParse(pParse, 1); }
100 explain ::= EXPLAIN QUERY PLAN.   { sqlite3BeginParse(pParse, 2); }
101 %endif  SQLITE_OMIT_EXPLAIN
102 
103 ///////////////////// Begin and end transactions. ////////////////////////////
104 //
105 
106 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
107 trans_opt ::= .
108 trans_opt ::= TRANSACTION.
109 trans_opt ::= TRANSACTION nm.
110 %type transtype {int}
111 transtype(A) ::= .             {A = TK_DEFERRED;}
112 transtype(A) ::= DEFERRED(X).  {A = @X;}
113 transtype(A) ::= IMMEDIATE(X). {A = @X;}
114 transtype(A) ::= EXCLUSIVE(X). {A = @X;}
115 cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
116 cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
117 cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
118 
119 ///////////////////// The CREATE TABLE statement ////////////////////////////
120 //
121 cmd ::= create_table create_table_args.
122 create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
123    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
124 }
125 %type ifnotexists {int}
126 ifnotexists(A) ::= .              {A = 0;}
127 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
128 %type temp {int}
129 %ifndef SQLITE_OMIT_TEMPDB
130 temp(A) ::= TEMP.  {A = 1;}
131 %endif  SQLITE_OMIT_TEMPDB
132 temp(A) ::= .      {A = 0;}
133 create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
134   sqlite3EndTable(pParse,&X,&Y,0);
135 }
136 create_table_args ::= AS select(S). {
137   sqlite3EndTable(pParse,0,0,S);
138   sqlite3SelectDelete(S);
139 }
140 columnlist ::= columnlist COMMA column.
141 columnlist ::= column.
142 
143 // A "column" is a complete description of a single column in a
144 // CREATE TABLE statement.  This includes the column name, its
145 // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
146 // NOT NULL and so forth.
147 //
148 column(A) ::= columnid(X) type carglist. {
149   A.z = X.z;
150   A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
151 }
152 columnid(A) ::= nm(X). {
153   sqlite3AddColumn(pParse,&X);
154   A = X;
155 }
156 
157 
158 // An IDENTIFIER can be a generic identifier, or one of several
159 // keywords.  Any non-standard keyword can also be an identifier.
160 //
161 %type id {Token}
162 id(A) ::= ID(X).         {A = X;}
163 
164 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
165 // fallback to ID if they will not parse as their original value.
166 // This obviates the need for the "id" nonterminal.
167 //
168 %fallback ID
169   ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT
170   DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
171   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN
172   QUERY KEY OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW
173   TEMP TRIGGER VACUUM VIEW VIRTUAL
174 %ifdef SQLITE_OMIT_COMPOUND_SELECT
175   EXCEPT INTERSECT UNION
176 %endif SQLITE_OMIT_COMPOUND_SELECT
177   REINDEX RENAME CTIME_KW IF
178   .
179 %wildcard ANY.
180 
181 // Define operator precedence early so that this is the first occurance
182 // of the operator tokens in the grammer.  Keeping the operators together
183 // causes them to be assigned integer values that are close together,
184 // which keeps parser tables smaller.
185 //
186 // The token values assigned to these symbols is determined by the order
187 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
188 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
189 // the sqlite3ExprIfFalse() routine for additional information on this
190 // constraint.
191 //
192 %left OR.
193 %left AND.
194 %right NOT.
195 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
196 %left GT LE LT GE.
197 %right ESCAPE.
198 %left BITAND BITOR LSHIFT RSHIFT.
199 %left PLUS MINUS.
200 %left STAR SLASH REM.
201 %left CONCAT.
202 %left COLLATE.
203 %right UMINUS UPLUS BITNOT.
204 
205 // And "ids" is an identifer-or-string.
206 //
207 %type ids {Token}
208 ids(A) ::= ID|STRING(X).   {A = X;}
209 
210 // The name of a column or table can be any of the following:
211 //
212 %type nm {Token}
213 nm(A) ::= ID(X).         {A = X;}
214 nm(A) ::= STRING(X).     {A = X;}
215 nm(A) ::= JOIN_KW(X).    {A = X;}
216 
217 // A typetoken is really one or more tokens that form a type name such
218 // as can be found after the column name in a CREATE TABLE statement.
219 // Multiple tokens are concatenated to form the value of the typetoken.
220 //
221 %type typetoken {Token}
222 type ::= .
223 type ::= typetoken(X).                   {sqlite3AddColumnType(pParse,&X);}
224 typetoken(A) ::= typename(X).   {A = X;}
225 typetoken(A) ::= typename(X) LP signed RP(Y). {
226   A.z = X.z;
227   A.n = &Y.z[Y.n] - X.z;
228 }
229 typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
230   A.z = X.z;
231   A.n = &Y.z[Y.n] - X.z;
232 }
233 %type typename {Token}
234 typename(A) ::= ids(X).             {A = X;}
235 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
236 signed ::= plus_num.
237 signed ::= minus_num.
238 
239 // "carglist" is a list of additional constraints that come after the
240 // column name and column type in a CREATE TABLE statement.
241 //
242 carglist ::= carglist carg.
243 carglist ::= .
244 carg ::= CONSTRAINT nm ccons.
245 carg ::= ccons.
246 ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,X);}
247 ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,X);}
248 ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,X);}
249 ccons ::= DEFAULT MINUS term(X).      {
250   Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0, 0);
251   sqlite3AddDefaultValue(pParse,p);
252 }
253 ccons ::= DEFAULT id(X).              {
254   Expr *p = sqlite3PExpr(pParse, TK_STRING, 0, 0, &X);
255   sqlite3AddDefaultValue(pParse,p);
256 }
257 
258 // In addition to the type name, we also care about the primary key and
259 // UNIQUE constraints.
260 //
261 ccons ::= NULL onconf.
262 ccons ::= NOT NULL onconf(R).               {sqlite3AddNotNull(pParse, R);}
263 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
264                                      {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
265 ccons ::= UNIQUE onconf(R).    {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
266 ccons ::= CHECK LP expr(X) RP.       {sqlite3AddCheckConstraint(pParse,X);}
267 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
268                                 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
269 ccons ::= defer_subclause(D).   {sqlite3DeferForeignKey(pParse,D);}
270 ccons ::= COLLATE ids(C).  {sqlite3AddCollateType(pParse, &C);}
271 
272 // The optional AUTOINCREMENT keyword
273 %type autoinc {int}
274 autoinc(X) ::= .          {X = 0;}
275 autoinc(X) ::= AUTOINCR.  {X = 1;}
276 
277 // The next group of rules parses the arguments to a REFERENCES clause
278 // that determine if the referential integrity checking is deferred or
279 // or immediate and which determine what action to take if a ref-integ
280 // check fails.
281 //
282 %type refargs {int}
283 refargs(A) ::= .                     { A = OE_Restrict * 0x010101; }
284 refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
285 %type refarg {struct {int value; int mask;}}
286 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
287 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
288 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
289 refarg(A) ::= ON INSERT refact(X).   { A.value = X<<16; A.mask = 0xff0000; }
290 %type refact {int}
291 refact(A) ::= SET NULL.              { A = OE_SetNull; }
292 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt; }
293 refact(A) ::= CASCADE.               { A = OE_Cascade; }
294 refact(A) ::= RESTRICT.              { A = OE_Restrict; }
295 %type defer_subclause {int}
296 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X).  {A = X;}
297 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
298 %type init_deferred_pred_opt {int}
299 init_deferred_pred_opt(A) ::= .                       {A = 0;}
300 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
301 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
302 
303 // For the time being, the only constraint we care about is the primary
304 // key and UNIQUE.  Both create indices.
305 //
306 conslist_opt(A) ::= .                   {A.n = 0; A.z = 0;}
307 conslist_opt(A) ::= COMMA(X) conslist.  {A = X;}
308 conslist ::= conslist COMMA tcons.
309 conslist ::= conslist tcons.
310 conslist ::= tcons.
311 tcons ::= CONSTRAINT nm.
312 tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
313                                          {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
314 tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
315                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
316 tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
317 tcons ::= FOREIGN KEY LP idxlist(FA) RP
318           REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
319     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
320     sqlite3DeferForeignKey(pParse, D);
321 }
322 %type defer_subclause_opt {int}
323 defer_subclause_opt(A) ::= .                    {A = 0;}
324 defer_subclause_opt(A) ::= defer_subclause(X).  {A = X;}
325 
326 // The following is a non-standard extension that allows us to declare the
327 // default behavior when there is a constraint conflict.
328 //
329 %type onconf {int}
330 %type orconf {int}
331 %type resolvetype {int}
332 onconf(A) ::= .                              {A = OE_Default;}
333 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
334 orconf(A) ::= .                              {A = OE_Default;}
335 orconf(A) ::= OR resolvetype(X).             {A = X;}
336 resolvetype(A) ::= raisetype(X).             {A = X;}
337 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
338 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
339 
340 ////////////////////////// The DROP TABLE /////////////////////////////////////
341 //
342 cmd ::= DROP TABLE ifexists(E) fullname(X). {
343   sqlite3DropTable(pParse, X, 0, E);
344 }
345 %type ifexists {int}
346 ifexists(A) ::= IF EXISTS.   {A = 1;}
347 ifexists(A) ::= .            {A = 0;}
348 
349 ///////////////////// The CREATE VIEW statement /////////////////////////////
350 //
351 %ifndef SQLITE_OMIT_VIEW
352 cmd ::= CREATE(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). {
353   sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E);
354 }
355 cmd ::= DROP VIEW ifexists(E) fullname(X). {
356   sqlite3DropTable(pParse, X, 1, E);
357 }
358 %endif  SQLITE_OMIT_VIEW
359 
360 //////////////////////// The SELECT statement /////////////////////////////////
361 //
362 cmd ::= select(X).  {
363   SelectDest dest = {SRT_Callback, 0, 0, 0, 0};
364   sqlite3Select(pParse, X, &dest, 0, 0, 0, 0);
365   sqlite3SelectDelete(X);
366 }
367 
368 %type select {Select*}
369 %destructor select {sqlite3SelectDelete($$);}
370 %type oneselect {Select*}
371 %destructor oneselect {sqlite3SelectDelete($$);}
372 
373 select(A) ::= oneselect(X).                      {A = X;}
374 %ifndef SQLITE_OMIT_COMPOUND_SELECT
375 select(A) ::= select(X) multiselect_op(Y) oneselect(Z).  {
376   if( Z ){
377     Z->op = Y;
378     Z->pPrior = X;
379   }else{
380     sqlite3SelectDelete(X);
381   }
382   A = Z;
383 }
384 %type multiselect_op {int}
385 multiselect_op(A) ::= UNION(OP).             {A = @OP;}
386 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
387 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
388 %endif SQLITE_OMIT_COMPOUND_SELECT
389 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
390                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
391   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
392 }
393 
394 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
395 // present and false (0) if it is not.
396 //
397 %type distinct {int}
398 distinct(A) ::= DISTINCT.   {A = 1;}
399 distinct(A) ::= ALL.        {A = 0;}
400 distinct(A) ::= .           {A = 0;}
401 
402 // selcollist is a list of expressions that are to become the return
403 // values of the SELECT statement.  The "*" in statements like
404 // "SELECT * FROM ..." is encoded as a special expression with an
405 // opcode of TK_ALL.
406 //
407 %type selcollist {ExprList*}
408 %destructor selcollist {sqlite3ExprListDelete($$);}
409 %type sclp {ExprList*}
410 %destructor sclp {sqlite3ExprListDelete($$);}
411 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
412 sclp(A) ::= .                                {A = 0;}
413 selcollist(A) ::= sclp(P) expr(X) as(Y).     {
414    A = sqlite3ExprListAppend(pParse,P,X,Y.n?&Y:0);
415 }
416 selcollist(A) ::= sclp(P) STAR. {
417   Expr *p = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
418   A = sqlite3ExprListAppend(pParse, P, p, 0);
419 }
420 selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
421   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
422   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
423   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
424   A = sqlite3ExprListAppend(pParse,P, pDot, 0);
425 }
426 
427 // An option "AS <id>" phrase that can follow one of the expressions that
428 // define the result set, or one of the tables in the FROM clause.
429 //
430 %type as {Token}
431 as(X) ::= AS nm(Y).    {X = Y;}
432 as(X) ::= ids(Y).      {X = Y;}
433 as(X) ::= .            {X.n = 0;}
434 
435 
436 %type seltablist {SrcList*}
437 %destructor seltablist {sqlite3SrcListDelete($$);}
438 %type stl_prefix {SrcList*}
439 %destructor stl_prefix {sqlite3SrcListDelete($$);}
440 %type from {SrcList*}
441 %destructor from {sqlite3SrcListDelete($$);}
442 
443 // A complete FROM clause.
444 //
445 from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
446 from(A) ::= FROM seltablist(X).  {
447   A = X;
448   sqlite3SrcListShiftJoinType(A);
449 }
450 
451 // "seltablist" is a "Select Table List" - the content of the FROM clause
452 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
453 //
454 stl_prefix(A) ::= seltablist(X) joinop(Y).    {
455    A = X;
456    if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
457 }
458 stl_prefix(A) ::= .                           {A = 0;}
459 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
460   A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U);
461 }
462 %ifndef SQLITE_OMIT_SUBQUERY
463   seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
464                     as(Z) on_opt(N) using_opt(U). {
465     A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,S,N,U);
466   }
467 
468   // A seltablist_paren nonterminal represents anything in a FROM that
469   // is contained inside parentheses.  This can be either a subquery or
470   // a grouping of table and subqueries.
471   //
472   %type seltablist_paren {Select*}
473   %destructor seltablist_paren {sqlite3SelectDelete($$);}
474   seltablist_paren(A) ::= select(S).      {A = S;}
475   seltablist_paren(A) ::= seltablist(F).  {
476      sqlite3SrcListShiftJoinType(F);
477      A = sqlite3SelectNew(pParse,0,F,0,0,0,0,0,0,0);
478   }
479 %endif  SQLITE_OMIT_SUBQUERY
480 
481 %type dbnm {Token}
482 dbnm(A) ::= .          {A.z=0; A.n=0;}
483 dbnm(A) ::= DOT nm(X). {A = X;}
484 
485 %type fullname {SrcList*}
486 %destructor fullname {sqlite3SrcListDelete($$);}
487 fullname(A) ::= nm(X) dbnm(Y).  {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y);}
488 
489 %type joinop {int}
490 %type joinop2 {int}
491 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
492 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
493 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
494 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
495                                        { X = sqlite3JoinType(pParse,&A,&B,&C); }
496 
497 %type on_opt {Expr*}
498 %destructor on_opt {sqlite3ExprDelete($$);}
499 on_opt(N) ::= ON expr(E).   {N = E;}
500 on_opt(N) ::= .             {N = 0;}
501 
502 %type using_opt {IdList*}
503 %destructor using_opt {sqlite3IdListDelete($$);}
504 using_opt(U) ::= USING LP inscollist(L) RP.  {U = L;}
505 using_opt(U) ::= .                        {U = 0;}
506 
507 
508 %type orderby_opt {ExprList*}
509 %destructor orderby_opt {sqlite3ExprListDelete($$);}
510 %type sortlist {ExprList*}
511 %destructor sortlist {sqlite3ExprListDelete($$);}
512 %type sortitem {Expr*}
513 %destructor sortitem {sqlite3ExprDelete($$);}
514 
515 orderby_opt(A) ::= .                          {A = 0;}
516 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
517 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
518   A = sqlite3ExprListAppend(pParse,X,Y,0);
519   if( A ) A->a[A->nExpr-1].sortOrder = Z;
520 }
521 sortlist(A) ::= sortitem(Y) sortorder(Z). {
522   A = sqlite3ExprListAppend(pParse,0,Y,0);
523   if( A && A->a ) A->a[0].sortOrder = Z;
524 }
525 sortitem(A) ::= expr(X).   {A = X;}
526 
527 %type sortorder {int}
528 
529 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
530 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
531 sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
532 
533 %type groupby_opt {ExprList*}
534 %destructor groupby_opt {sqlite3ExprListDelete($$);}
535 groupby_opt(A) ::= .                      {A = 0;}
536 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
537 
538 %type having_opt {Expr*}
539 %destructor having_opt {sqlite3ExprDelete($$);}
540 having_opt(A) ::= .                {A = 0;}
541 having_opt(A) ::= HAVING expr(X).  {A = X;}
542 
543 %type limit_opt {struct LimitVal}
544 
545 // The destructor for limit_opt will never fire in the current grammar.
546 // The limit_opt non-terminal only occurs at the end of a single production
547 // rule for SELECT statements.  As soon as the rule that create the
548 // limit_opt non-terminal reduces, the SELECT statement rule will also
549 // reduce.  So there is never a limit_opt non-terminal on the stack
550 // except as a transient.  So there is never anything to destroy.
551 //
552 //%destructor limit_opt {
553 //  sqlite3ExprDelete($$.pLimit);
554 //  sqlite3ExprDelete($$.pOffset);
555 //}
556 limit_opt(A) ::= .                     {A.pLimit = 0; A.pOffset = 0;}
557 limit_opt(A) ::= LIMIT expr(X).        {A.pLimit = X; A.pOffset = 0;}
558 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
559                                        {A.pLimit = X; A.pOffset = Y;}
560 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
561                                        {A.pOffset = X; A.pLimit = Y;}
562 
563 /////////////////////////// The DELETE statement /////////////////////////////
564 //
565 cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
566 
567 %type where_opt {Expr*}
568 %destructor where_opt {sqlite3ExprDelete($$);}
569 
570 where_opt(A) ::= .                    {A = 0;}
571 where_opt(A) ::= WHERE expr(X).       {A = X;}
572 
573 ////////////////////////// The UPDATE command ////////////////////////////////
574 //
575 cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).  {
576   sqlite3ExprListCheckLength(pParse,Y,"set list");
577   sqlite3Update(pParse,X,Y,Z,R);
578 }
579 
580 %type setlist {ExprList*}
581 %destructor setlist {sqlite3ExprListDelete($$);}
582 
583 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
584     {A = sqlite3ExprListAppend(pParse,Z,Y,&X);}
585 setlist(A) ::= nm(X) EQ expr(Y).
586     {A = sqlite3ExprListAppend(pParse,0,Y,&X);}
587 
588 ////////////////////////// The INSERT command /////////////////////////////////
589 //
590 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
591         VALUES LP itemlist(Y) RP.
592             {sqlite3Insert(pParse, X, Y, 0, F, R);}
593 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
594             {sqlite3Insert(pParse, X, 0, S, F, R);}
595 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
596             {sqlite3Insert(pParse, X, 0, 0, F, R);}
597 
598 %type insert_cmd {int}
599 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
600 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
601 
602 
603 %type itemlist {ExprList*}
604 %destructor itemlist {sqlite3ExprListDelete($$);}
605 
606 itemlist(A) ::= itemlist(X) COMMA expr(Y).
607     {A = sqlite3ExprListAppend(pParse,X,Y,0);}
608 itemlist(A) ::= expr(X).
609     {A = sqlite3ExprListAppend(pParse,0,X,0);}
610 
611 %type inscollist_opt {IdList*}
612 %destructor inscollist_opt {sqlite3IdListDelete($$);}
613 %type inscollist {IdList*}
614 %destructor inscollist {sqlite3IdListDelete($$);}
615 
616 inscollist_opt(A) ::= .                       {A = 0;}
617 inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
618 inscollist(A) ::= inscollist(X) COMMA nm(Y).
619     {A = sqlite3IdListAppend(pParse->db,X,&Y);}
620 inscollist(A) ::= nm(Y).
621     {A = sqlite3IdListAppend(pParse->db,0,&Y);}
622 
623 /////////////////////////// Expression Processing /////////////////////////////
624 //
625 
626 %type expr {Expr*}
627 %destructor expr {sqlite3ExprDelete($$);}
628 %type term {Expr*}
629 %destructor term {sqlite3ExprDelete($$);}
630 
631 expr(A) ::= term(X).             {A = X;}
632 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
633 term(A) ::= NULL(X).             {A = sqlite3PExpr(pParse, @X, 0, 0, &X);}
634 expr(A) ::= ID(X).               {A = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);}
635 expr(A) ::= JOIN_KW(X).          {A = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);}
636 expr(A) ::= nm(X) DOT nm(Y). {
637   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
638   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
639   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
640 }
641 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
642   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
643   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
644   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z);
645   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
646   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
647 }
648 term(A) ::= INTEGER|FLOAT|BLOB(X).  {A = sqlite3PExpr(pParse, @X, 0, 0, &X);}
649 term(A) ::= STRING(X).       {A = sqlite3PExpr(pParse, @X, 0, 0, &X);}
650 expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
651 expr(A) ::= VARIABLE(X).     {
652   Token *pToken = &X;
653   Expr *pExpr = A = sqlite3PExpr(pParse, TK_VARIABLE, 0, 0, pToken);
654   sqlite3ExprAssignVarNumber(pParse, pExpr);
655 }
656 expr(A) ::= expr(E) COLLATE ids(C). {
657   A = sqlite3ExprSetColl(pParse, E, &C);
658 }
659 %ifndef SQLITE_OMIT_CAST
660 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
661   A = sqlite3PExpr(pParse, TK_CAST, E, 0, &T);
662   sqlite3ExprSpan(A,&X,&Y);
663 }
664 %endif  SQLITE_OMIT_CAST
665 expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
666   if( Y && Y->nExpr>SQLITE_MAX_FUNCTION_ARG ){
667     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
668   }
669   A = sqlite3ExprFunction(pParse, Y, &X);
670   sqlite3ExprSpan(A,&X,&E);
671   if( D && A ){
672     A->flags |= EP_Distinct;
673   }
674 }
675 expr(A) ::= ID(X) LP STAR RP(E). {
676   A = sqlite3ExprFunction(pParse, 0, &X);
677   sqlite3ExprSpan(A,&X,&E);
678 }
679 term(A) ::= CTIME_KW(OP). {
680   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
681   ** treated as functions that return constants */
682   A = sqlite3ExprFunction(pParse, 0,&OP);
683   if( A ){
684     A->op = TK_CONST_FUNC;
685     A->span = OP;
686   }
687 }
688 expr(A) ::= expr(X) AND(OP) expr(Y).       {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
689 expr(A) ::= expr(X) OR(OP) expr(Y).        {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
690 expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).
691                                            {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
692 expr(A) ::= expr(X) EQ|NE(OP) expr(Y).     {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
693 expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
694                                            {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
695 expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).{A = sqlite3PExpr(pParse,@OP,X,Y,0);}
696 expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y).
697                                            {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
698 expr(A) ::= expr(X) CONCAT(OP) expr(Y).    {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
699 %type likeop {struct LikeOp}
700 likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.not = 0;}
701 likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
702 likeop(A) ::= MATCH(X).       {A.eOperator = X; A.not = 0;}
703 likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.not = 1;}
704 %type escape {Expr*}
705 %destructor escape {sqlite3ExprDelete($$);}
706 escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
707 escape(X) ::= .               [ESCAPE] {X = 0;}
708 expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E).  [LIKE_KW]  {
709   ExprList *pList;
710   pList = sqlite3ExprListAppend(pParse,0, Y, 0);
711   pList = sqlite3ExprListAppend(pParse,pList, X, 0);
712   if( E ){
713     pList = sqlite3ExprListAppend(pParse,pList, E, 0);
714   }
715   A = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
716   if( OP.not ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
717   sqlite3ExprSpan(A, &X->span, &Y->span);
718   if( A ) A->flags |= EP_InfixFunc;
719 }
720 
721 expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
722   A = sqlite3PExpr(pParse, @E, X, 0, 0);
723   sqlite3ExprSpan(A,&X->span,&E);
724 }
725 expr(A) ::= expr(X) IS NULL(E). {
726   A = sqlite3PExpr(pParse, TK_ISNULL, X, 0, 0);
727   sqlite3ExprSpan(A,&X->span,&E);
728 }
729 expr(A) ::= expr(X) NOT NULL(E). {
730   A = sqlite3PExpr(pParse, TK_NOTNULL, X, 0, 0);
731   sqlite3ExprSpan(A,&X->span,&E);
732 }
733 expr(A) ::= expr(X) IS NOT NULL(E). {
734   A = sqlite3PExpr(pParse, TK_NOTNULL, X, 0, 0);
735   sqlite3ExprSpan(A,&X->span,&E);
736 }
737 expr(A) ::= NOT(B) expr(X). {
738   A = sqlite3PExpr(pParse, @B, X, 0, 0);
739   sqlite3ExprSpan(A,&B,&X->span);
740 }
741 expr(A) ::= BITNOT(B) expr(X). {
742   A = sqlite3PExpr(pParse, @B, X, 0, 0);
743   sqlite3ExprSpan(A,&B,&X->span);
744 }
745 expr(A) ::= MINUS(B) expr(X). [UMINUS] {
746   A = sqlite3PExpr(pParse, TK_UMINUS, X, 0, 0);
747   sqlite3ExprSpan(A,&B,&X->span);
748 }
749 expr(A) ::= PLUS(B) expr(X). [UPLUS] {
750   A = sqlite3PExpr(pParse, TK_UPLUS, X, 0, 0);
751   sqlite3ExprSpan(A,&B,&X->span);
752 }
753 %type between_op {int}
754 between_op(A) ::= BETWEEN.     {A = 0;}
755 between_op(A) ::= NOT BETWEEN. {A = 1;}
756 expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
757   ExprList *pList = sqlite3ExprListAppend(pParse,0, X, 0);
758   pList = sqlite3ExprListAppend(pParse,pList, Y, 0);
759   A = sqlite3PExpr(pParse, TK_BETWEEN, W, 0, 0);
760   if( A ){
761     A->pList = pList;
762   }else{
763     sqlite3ExprListDelete(pList);
764   }
765   if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
766   sqlite3ExprSpan(A,&W->span,&Y->span);
767 }
768 %ifndef SQLITE_OMIT_SUBQUERY
769   %type in_op {int}
770   in_op(A) ::= IN.      {A = 0;}
771   in_op(A) ::= NOT IN.  {A = 1;}
772   expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
773     A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
774     if( A ){
775       A->pList = Y;
776       sqlite3ExprSetHeight(A);
777     }else{
778       sqlite3ExprListDelete(Y);
779     }
780     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
781     sqlite3ExprSpan(A,&X->span,&E);
782   }
783   expr(A) ::= LP(B) select(X) RP(E). {
784     A = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
785     if( A ){
786       A->pSelect = X;
787       sqlite3ExprSetHeight(A);
788     }else{
789       sqlite3SelectDelete(X);
790     }
791     sqlite3ExprSpan(A,&B,&E);
792   }
793   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
794     A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
795     if( A ){
796       A->pSelect = Y;
797       sqlite3ExprSetHeight(A);
798     }else{
799       sqlite3SelectDelete(Y);
800     }
801     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
802     sqlite3ExprSpan(A,&X->span,&E);
803   }
804   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
805     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
806     A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
807     if( A ){
808       A->pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
809       sqlite3ExprSetHeight(A);
810     }else{
811       sqlite3SrcListDelete(pSrc);
812     }
813     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
814     sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
815   }
816   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
817     Expr *p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
818     if( p ){
819       p->pSelect = Y;
820       sqlite3ExprSpan(p,&B,&E);
821       sqlite3ExprSetHeight(A);
822     }else{
823       sqlite3SelectDelete(Y);
824     }
825   }
826 %endif SQLITE_OMIT_SUBQUERY
827 
828 /* CASE expressions */
829 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
830   A = sqlite3PExpr(pParse, TK_CASE, X, Z, 0);
831   if( A ){
832     A->pList = Y;
833     sqlite3ExprSetHeight(A);
834   }else{
835     sqlite3ExprListDelete(Y);
836   }
837   sqlite3ExprSpan(A, &C, &E);
838 }
839 %type case_exprlist {ExprList*}
840 %destructor case_exprlist {sqlite3ExprListDelete($$);}
841 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
842   A = sqlite3ExprListAppend(pParse,X, Y, 0);
843   A = sqlite3ExprListAppend(pParse,A, Z, 0);
844 }
845 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
846   A = sqlite3ExprListAppend(pParse,0, Y, 0);
847   A = sqlite3ExprListAppend(pParse,A, Z, 0);
848 }
849 %type case_else {Expr*}
850 %destructor case_else {sqlite3ExprDelete($$);}
851 case_else(A) ::=  ELSE expr(X).         {A = X;}
852 case_else(A) ::=  .                     {A = 0;}
853 %type case_operand {Expr*}
854 %destructor case_operand {sqlite3ExprDelete($$);}
855 case_operand(A) ::= expr(X).            {A = X;}
856 case_operand(A) ::= .                   {A = 0;}
857 
858 %type exprlist {ExprList*}
859 %destructor exprlist {sqlite3ExprListDelete($$);}
860 %type nexprlist {ExprList*}
861 %destructor nexprlist {sqlite3ExprListDelete($$);}
862 
863 exprlist(A) ::= nexprlist(X).                {A = X;}
864 exprlist(A) ::= .                            {A = 0;}
865 nexprlist(A) ::= nexprlist(X) COMMA expr(Y).
866     {A = sqlite3ExprListAppend(pParse,X,Y,0);}
867 nexprlist(A) ::= expr(Y).
868     {A = sqlite3ExprListAppend(pParse,0,Y,0);}
869 
870 
871 ///////////////////////////// The CREATE INDEX command ///////////////////////
872 //
873 cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
874         ON nm(Y) LP idxlist(Z) RP(E). {
875   sqlite3CreateIndex(pParse, &X, &D,
876                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
877                       &S, &E, SQLITE_SO_ASC, NE);
878 }
879 
880 %type uniqueflag {int}
881 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
882 uniqueflag(A) ::= .        {A = OE_None;}
883 
884 %type idxlist {ExprList*}
885 %destructor idxlist {sqlite3ExprListDelete($$);}
886 %type idxlist_opt {ExprList*}
887 %destructor idxlist_opt {sqlite3ExprListDelete($$);}
888 %type idxitem {Token}
889 
890 idxlist_opt(A) ::= .                         {A = 0;}
891 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
892 idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z).  {
893   Expr *p = 0;
894   if( C.n>0 ){
895     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
896     sqlite3ExprSetColl(pParse, p, &C);
897   }
898   A = sqlite3ExprListAppend(pParse,X, p, &Y);
899   sqlite3ExprListCheckLength(pParse, A, "index");
900   if( A ) A->a[A->nExpr-1].sortOrder = Z;
901 }
902 idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). {
903   Expr *p = 0;
904   if( C.n>0 ){
905     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
906     sqlite3ExprSetColl(pParse, p, &C);
907   }
908   A = sqlite3ExprListAppend(pParse,0, p, &Y);
909   sqlite3ExprListCheckLength(pParse, A, "index");
910   if( A ) A->a[A->nExpr-1].sortOrder = Z;
911 }
912 idxitem(A) ::= nm(X).              {A = X;}
913 
914 %type collate {Token}
915 collate(C) ::= .                {C.z = 0; C.n = 0;}
916 collate(C) ::= COLLATE ids(X).   {C = X;}
917 
918 
919 ///////////////////////////// The DROP INDEX command /////////////////////////
920 //
921 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
922 
923 ///////////////////////////// The VACUUM command /////////////////////////////
924 //
925 %ifndef SQLITE_OMIT_VACUUM
926 %ifndef SQLITE_OMIT_ATTACH
927 cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
928 cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
929 %endif  SQLITE_OMIT_ATTACH
930 %endif  SQLITE_OMIT_VACUUM
931 
932 ///////////////////////////// The PRAGMA command /////////////////////////////
933 //
934 %ifndef SQLITE_OMIT_PRAGMA
935 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
936 cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
937 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
938   sqlite3Pragma(pParse,&X,&Z,&Y,1);
939 }
940 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
941 cmd ::= PRAGMA nm(X) dbnm(Z).             {sqlite3Pragma(pParse,&X,&Z,0,0);}
942 nmnum(A) ::= plus_num(X).             {A = X;}
943 nmnum(A) ::= nm(X).                   {A = X;}
944 %endif SQLITE_OMIT_PRAGMA
945 plus_num(A) ::= plus_opt number(X).   {A = X;}
946 minus_num(A) ::= MINUS number(X).     {A = X;}
947 number(A) ::= INTEGER|FLOAT(X).       {A = X;}
948 plus_opt ::= PLUS.
949 plus_opt ::= .
950 
951 //////////////////////////// The CREATE TRIGGER command /////////////////////
952 
953 %ifndef SQLITE_OMIT_TRIGGER
954 
955 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
956   Token all;
957   all.z = A.z;
958   all.n = (Z.z - A.z) + Z.n;
959   sqlite3FinishTrigger(pParse, S, &all);
960 }
961 
962 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
963                     trigger_time(C) trigger_event(D)
964                     ON fullname(E) foreach_clause when_clause(G). {
965   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
966   A = (Z.n==0?B:Z);
967 }
968 
969 %type trigger_time {int}
970 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
971 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
972 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
973 trigger_time(A) ::= .            { A = TK_BEFORE; }
974 
975 %type trigger_event {struct TrigEvent}
976 %destructor trigger_event {sqlite3IdListDelete($$.b);}
977 trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
978 trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
979 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
980 
981 foreach_clause ::= .
982 foreach_clause ::= FOR EACH ROW.
983 
984 %type when_clause {Expr*}
985 %destructor when_clause {sqlite3ExprDelete($$);}
986 when_clause(A) ::= .             { A = 0; }
987 when_clause(A) ::= WHEN expr(X). { A = X; }
988 
989 %type trigger_cmd_list {TriggerStep*}
990 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
991 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
992   if( Y ){
993     Y->pLast->pNext = X;
994   }else{
995     Y = X;
996   }
997   Y->pLast = X;
998   A = Y;
999 }
1000 trigger_cmd_list(A) ::= . { A = 0; }
1001 
1002 %type trigger_cmd {TriggerStep*}
1003 %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
1004 // UPDATE
1005 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
1006                { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); }
1007 
1008 // INSERT
1009 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
1010                    VALUES LP itemlist(Y) RP.
1011                {A = sqlite3TriggerInsertStep(pParse->db, &X, F, Y, 0, R);}
1012 
1013 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
1014                {A = sqlite3TriggerInsertStep(pParse->db, &X, F, 0, S, R);}
1015 
1016 // DELETE
1017 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
1018                {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
1019 
1020 // SELECT
1021 trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(pParse->db, X); }
1022 
1023 // The special RAISE expression that may occur in trigger programs
1024 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
1025   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
1026   if( A ){
1027     A->iColumn = OE_Ignore;
1028     sqlite3ExprSpan(A, &X, &Y);
1029   }
1030 }
1031 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
1032   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z);
1033   if( A ) {
1034     A->iColumn = T;
1035     sqlite3ExprSpan(A, &X, &Y);
1036   }
1037 }
1038 %endif  !SQLITE_OMIT_TRIGGER
1039 
1040 %type raisetype {int}
1041 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1042 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1043 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1044 
1045 
1046 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1047 %ifndef SQLITE_OMIT_TRIGGER
1048 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1049   sqlite3DropTrigger(pParse,X,NOERR);
1050 }
1051 %endif  !SQLITE_OMIT_TRIGGER
1052 
1053 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1054 %ifndef SQLITE_OMIT_ATTACH
1055 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1056   sqlite3Attach(pParse, F, D, K);
1057 }
1058 cmd ::= DETACH database_kw_opt expr(D). {
1059   sqlite3Detach(pParse, D);
1060 }
1061 
1062 %type key_opt {Expr*}
1063 %destructor key_opt {sqlite3ExprDelete($$);}
1064 key_opt(A) ::= .                     { A = 0; }
1065 key_opt(A) ::= KEY expr(X).          { A = X; }
1066 
1067 database_kw_opt ::= DATABASE.
1068 database_kw_opt ::= .
1069 %endif SQLITE_OMIT_ATTACH
1070 
1071 ////////////////////////// REINDEX collation //////////////////////////////////
1072 %ifndef SQLITE_OMIT_REINDEX
1073 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1074 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1075 %endif  SQLITE_OMIT_REINDEX
1076 
1077 /////////////////////////////////// ANALYZE ///////////////////////////////////
1078 %ifndef SQLITE_OMIT_ANALYZE
1079 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1080 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1081 %endif
1082 
1083 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1084 %ifndef SQLITE_OMIT_ALTERTABLE
1085 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1086   sqlite3AlterRenameTable(pParse,X,&Z);
1087 }
1088 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1089   sqlite3AlterFinishAddColumn(pParse, &Y);
1090 }
1091 add_column_fullname ::= fullname(X). {
1092   sqlite3AlterBeginAddColumn(pParse, X);
1093 }
1094 kwcolumn_opt ::= .
1095 kwcolumn_opt ::= COLUMNKW.
1096 %endif  SQLITE_OMIT_ALTERTABLE
1097 
1098 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1099 %ifndef SQLITE_OMIT_VIRTUALTABLE
1100 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1101 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1102 create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
1103     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
1104 }
1105 vtabarglist ::= vtabarg.
1106 vtabarglist ::= vtabarglist COMMA vtabarg.
1107 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1108 vtabarg ::= vtabarg vtabargtoken.
1109 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1110 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1111 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1112 anylist ::= .
1113 anylist ::= anylist ANY(X).         {sqlite3VtabArgExtend(pParse,&X);}
1114 %endif  SQLITE_OMIT_VIRTUALTABLE
1115