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