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