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