xref: /sqlite-3.40.0/src/parse.y (revision be217793)
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.270 2009/03/05 03:48:07 shane Exp $
18 */
19 
20 // All token codes are small integers with #defines that begin with "TK_"
21 %token_prefix TK_
22 
23 // The type of the data attached to each token is Token.  This is also the
24 // default type for non-terminals.
25 //
26 %token_type {Token}
27 %default_type {Token}
28 
29 // The generated parser function takes a 4th argument as follows:
30 %extra_argument {Parse *pParse}
31 
32 // This code runs whenever there is a syntax error
33 //
34 %syntax_error {
35   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
36   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
37   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
38   pParse->parseError = 1;
39 }
40 %stack_overflow {
41   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
42   sqlite3ErrorMsg(pParse, "parser stack overflow");
43   pParse->parseError = 1;
44 }
45 
46 // The name of the generated procedure that implements the parser
47 // is as follows:
48 %name sqlite3Parser
49 
50 // The following text is included near the beginning of the C source
51 // code file that implements the parser.
52 //
53 %include {
54 #include "sqliteInt.h"
55 
56 /*
57 ** 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 BY CASCADE CAST COLUMNKW 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 RELEASE REPLACE RESTRICT ROW ROLLBACK
187   SAVEPOINT 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->x.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->x.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->x.pSelect = X;
853       ExprSetProperty(A, EP_xIsSelect);
854       sqlite3ExprSetHeight(pParse, A);
855     }else{
856       sqlite3SelectDelete(pParse->db, X);
857     }
858     sqlite3ExprSpan(A,&B,&E);
859   }
860   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
861     A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
862     if( A ){
863       A->x.pSelect = Y;
864       ExprSetProperty(A, EP_xIsSelect);
865       sqlite3ExprSetHeight(pParse, A);
866     }else{
867       sqlite3SelectDelete(pParse->db, Y);
868     }
869     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
870     sqlite3ExprSpan(A,&X->span,&E);
871   }
872   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
873     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
874     A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
875     if( A ){
876       A->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
877       ExprSetProperty(A, EP_xIsSelect);
878       sqlite3ExprSetHeight(pParse, A);
879     }else{
880       sqlite3SrcListDelete(pParse->db, pSrc);
881     }
882     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
883     sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
884   }
885   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
886     Expr *p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
887     if( p ){
888       p->x.pSelect = Y;
889       ExprSetProperty(A, EP_xIsSelect);
890       sqlite3ExprSpan(p,&B,&E);
891       sqlite3ExprSetHeight(pParse, A);
892     }else{
893       sqlite3SelectDelete(pParse->db, Y);
894     }
895   }
896 %endif SQLITE_OMIT_SUBQUERY
897 
898 /* CASE expressions */
899 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
900   A = sqlite3PExpr(pParse, TK_CASE, X, Z, 0);
901   if( A ){
902     A->x.pList = Y;
903     sqlite3ExprSetHeight(pParse, A);
904   }else{
905     sqlite3ExprListDelete(pParse->db, Y);
906   }
907   sqlite3ExprSpan(A, &C, &E);
908 }
909 %type case_exprlist {ExprList*}
910 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
911 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
912   A = sqlite3ExprListAppend(pParse,X, Y, 0);
913   A = sqlite3ExprListAppend(pParse,A, Z, 0);
914 }
915 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
916   A = sqlite3ExprListAppend(pParse,0, Y, 0);
917   A = sqlite3ExprListAppend(pParse,A, Z, 0);
918 }
919 %type case_else {Expr*}
920 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
921 case_else(A) ::=  ELSE expr(X).         {A = X;}
922 case_else(A) ::=  .                     {A = 0;}
923 %type case_operand {Expr*}
924 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
925 case_operand(A) ::= expr(X).            {A = X;}
926 case_operand(A) ::= .                   {A = 0;}
927 
928 %type exprlist {ExprList*}
929 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
930 %type nexprlist {ExprList*}
931 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
932 
933 exprlist(A) ::= nexprlist(X).                {A = X;}
934 exprlist(A) ::= .                            {A = 0;}
935 nexprlist(A) ::= nexprlist(X) COMMA expr(Y).
936     {A = sqlite3ExprListAppend(pParse,X,Y,0);}
937 nexprlist(A) ::= expr(Y).
938     {A = sqlite3ExprListAppend(pParse,0,Y,0);}
939 
940 
941 ///////////////////////////// The CREATE INDEX command ///////////////////////
942 //
943 cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
944         ON nm(Y) LP idxlist(Z) RP(E). {
945   sqlite3CreateIndex(pParse, &X, &D,
946                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
947                       &S, &E, SQLITE_SO_ASC, NE);
948 }
949 
950 %type uniqueflag {int}
951 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
952 uniqueflag(A) ::= .        {A = OE_None;}
953 
954 %type idxlist {ExprList*}
955 %destructor idxlist {sqlite3ExprListDelete(pParse->db, $$);}
956 %type idxlist_opt {ExprList*}
957 %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
958 
959 idxlist_opt(A) ::= .                         {A = 0;}
960 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
961 idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z).  {
962   Expr *p = 0;
963   if( C.n>0 ){
964     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
965     sqlite3ExprSetColl(pParse, p, &C);
966   }
967   A = sqlite3ExprListAppend(pParse,X, p, &Y);
968   sqlite3ExprListCheckLength(pParse, A, "index");
969   if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
970 }
971 idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
972   Expr *p = 0;
973   if( C.n>0 ){
974     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
975     sqlite3ExprSetColl(pParse, p, &C);
976   }
977   A = sqlite3ExprListAppend(pParse,0, p, &Y);
978   sqlite3ExprListCheckLength(pParse, A, "index");
979   if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
980 }
981 
982 %type collate {Token}
983 collate(C) ::= .                {C.z = 0; C.n = 0;}
984 collate(C) ::= COLLATE ids(X).   {C = X;}
985 
986 
987 ///////////////////////////// The DROP INDEX command /////////////////////////
988 //
989 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
990 
991 ///////////////////////////// The VACUUM command /////////////////////////////
992 //
993 %ifndef SQLITE_OMIT_VACUUM
994 %ifndef SQLITE_OMIT_ATTACH
995 cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
996 cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
997 %endif  SQLITE_OMIT_ATTACH
998 %endif  SQLITE_OMIT_VACUUM
999 
1000 ///////////////////////////// The PRAGMA command /////////////////////////////
1001 //
1002 %ifndef SQLITE_OMIT_PARSER
1003 %ifndef SQLITE_OMIT_PRAGMA
1004 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).   {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1005 cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y).      {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1006 cmd ::= PRAGMA nm(X) dbnm(Z) EQ DELETE(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1007 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
1008   sqlite3Pragma(pParse,&X,&Z,&Y,1);
1009 }
1010 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1011 cmd ::= PRAGMA nm(X) dbnm(Z).             {sqlite3Pragma(pParse,&X,&Z,0,0);}
1012 nmnum(A) ::= plus_num(X).             {A = X;}
1013 nmnum(A) ::= nm(X).                   {A = X;}
1014 %endif SQLITE_OMIT_PRAGMA
1015 %endif SQLITE_OMIT_PARSER
1016 plus_num(A) ::= plus_opt number(X).   {A = X;}
1017 minus_num(A) ::= MINUS number(X).     {A = X;}
1018 number(A) ::= INTEGER|FLOAT(X).       {A = X;}
1019 plus_opt ::= PLUS.
1020 plus_opt ::= .
1021 
1022 //////////////////////////// The CREATE TRIGGER command /////////////////////
1023 
1024 %ifndef SQLITE_OMIT_TRIGGER
1025 
1026 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1027   Token all;
1028   all.z = A.z;
1029   all.n = (int)(Z.z - A.z) + Z.n;
1030   sqlite3FinishTrigger(pParse, S, &all);
1031 }
1032 
1033 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1034                     trigger_time(C) trigger_event(D)
1035                     ON fullname(E) foreach_clause when_clause(G). {
1036   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1037   A = (Z.n==0?B:Z);
1038 }
1039 
1040 %type trigger_time {int}
1041 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
1042 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
1043 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1044 trigger_time(A) ::= .            { A = TK_BEFORE; }
1045 
1046 %type trigger_event {struct TrigEvent}
1047 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1048 trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
1049 trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
1050 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
1051 
1052 foreach_clause ::= .
1053 foreach_clause ::= FOR EACH ROW.
1054 
1055 %type when_clause {Expr*}
1056 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1057 when_clause(A) ::= .             { A = 0; }
1058 when_clause(A) ::= WHEN expr(X). { A = X; }
1059 
1060 %type trigger_cmd_list {TriggerStep*}
1061 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1062 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
1063 /*
1064   if( Y ){
1065     Y->pLast->pNext = X;
1066   }else{
1067     Y = X;
1068   }
1069 */
1070   assert( Y!=0 );
1071   Y->pLast->pNext = X;
1072   Y->pLast = X;
1073   A = Y;
1074 }
1075 trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. {
1076   /* if( X ) */
1077   assert( X!=0 );
1078   X->pLast = X;
1079   A = X;
1080 }
1081 
1082 %type trigger_cmd {TriggerStep*}
1083 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1084 // UPDATE
1085 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
1086                { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); }
1087 
1088 // INSERT
1089 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
1090                    VALUES LP itemlist(Y) RP.
1091                {A = sqlite3TriggerInsertStep(pParse->db, &X, F, Y, 0, R);}
1092 
1093 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
1094                {A = sqlite3TriggerInsertStep(pParse->db, &X, F, 0, S, R);}
1095 
1096 // DELETE
1097 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
1098                {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
1099 
1100 // SELECT
1101 trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(pParse->db, X); }
1102 
1103 // The special RAISE expression that may occur in trigger programs
1104 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
1105   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
1106   if( A ){
1107     A->affinity = OE_Ignore;
1108     sqlite3ExprSpan(A, &X, &Y);
1109   }
1110 }
1111 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
1112   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z);
1113   if( A ) {
1114     A->affinity = (char)T;
1115     sqlite3ExprSpan(A, &X, &Y);
1116   }
1117 }
1118 %endif  !SQLITE_OMIT_TRIGGER
1119 
1120 %type raisetype {int}
1121 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1122 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1123 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1124 
1125 
1126 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1127 %ifndef SQLITE_OMIT_TRIGGER
1128 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1129   sqlite3DropTrigger(pParse,X,NOERR);
1130 }
1131 %endif  !SQLITE_OMIT_TRIGGER
1132 
1133 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1134 %ifndef SQLITE_OMIT_ATTACH
1135 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1136   sqlite3Attach(pParse, F, D, K);
1137 }
1138 cmd ::= DETACH database_kw_opt expr(D). {
1139   sqlite3Detach(pParse, D);
1140 }
1141 
1142 %type key_opt {Expr*}
1143 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1144 key_opt(A) ::= .                     { A = 0; }
1145 key_opt(A) ::= KEY expr(X).          { A = X; }
1146 
1147 database_kw_opt ::= DATABASE.
1148 database_kw_opt ::= .
1149 %endif SQLITE_OMIT_ATTACH
1150 
1151 ////////////////////////// REINDEX collation //////////////////////////////////
1152 %ifndef SQLITE_OMIT_REINDEX
1153 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1154 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1155 %endif  SQLITE_OMIT_REINDEX
1156 
1157 /////////////////////////////////// ANALYZE ///////////////////////////////////
1158 %ifndef SQLITE_OMIT_ANALYZE
1159 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1160 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1161 %endif
1162 
1163 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1164 %ifndef SQLITE_OMIT_ALTERTABLE
1165 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1166   sqlite3AlterRenameTable(pParse,X,&Z);
1167 }
1168 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1169   sqlite3AlterFinishAddColumn(pParse, &Y);
1170 }
1171 add_column_fullname ::= fullname(X). {
1172   sqlite3AlterBeginAddColumn(pParse, X);
1173 }
1174 kwcolumn_opt ::= .
1175 kwcolumn_opt ::= COLUMNKW.
1176 %endif  SQLITE_OMIT_ALTERTABLE
1177 
1178 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1179 %ifndef SQLITE_OMIT_VIRTUALTABLE
1180 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1181 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1182 create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
1183     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
1184 }
1185 vtabarglist ::= vtabarg.
1186 vtabarglist ::= vtabarglist COMMA vtabarg.
1187 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1188 vtabarg ::= vtabarg vtabargtoken.
1189 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1190 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1191 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1192 anylist ::= .
1193 anylist ::= anylist ANY(X).         {sqlite3VtabArgExtend(pParse,&X);}
1194 %endif  SQLITE_OMIT_VIRTUALTABLE
1195