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