xref: /sqlite-3.40.0/src/parse.y (revision dfe4e6bb)
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 
18 // All token codes are small integers with #defines that begin with "TK_"
19 %token_prefix TK_
20 
21 // The type of the data attached to each token is Token.  This is also the
22 // default type for non-terminals.
23 //
24 %token_type {Token}
25 %default_type {Token}
26 
27 // The generated parser function takes a 4th argument as follows:
28 %extra_argument {Parse *pParse}
29 
30 // This code runs whenever there is a syntax error
31 //
32 %syntax_error {
33   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
34   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
35   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
36 }
37 %stack_overflow {
38   sqlite3ErrorMsg(pParse, "parser stack overflow");
39 }
40 
41 // The name of the generated procedure that implements the parser
42 // is as follows:
43 %name sqlite3Parser
44 
45 // The following text is included near the beginning of the C source
46 // code file that implements the parser.
47 //
48 %include {
49 #include "sqliteInt.h"
50 
51 /*
52 ** Disable all error recovery processing in the parser push-down
53 ** automaton.
54 */
55 #define YYNOERRORRECOVERY 1
56 
57 /*
58 ** Make yytestcase() the same as testcase()
59 */
60 #define yytestcase(X) testcase(X)
61 
62 /*
63 ** Indicate that sqlite3ParserFree() will never be called with a null
64 ** pointer.
65 */
66 #define YYPARSEFREENEVERNULL 1
67 
68 /*
69 ** Alternative datatype for the argument to the malloc() routine passed
70 ** into sqlite3ParserAlloc().  The default is size_t.
71 */
72 #define YYMALLOCARGTYPE  u64
73 
74 /*
75 ** An instance of this structure holds information about the
76 ** LIMIT clause of a SELECT statement.
77 */
78 struct LimitVal {
79   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
80   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
81 };
82 
83 /*
84 ** An instance of the following structure describes the event of a
85 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
86 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
87 **
88 **      UPDATE ON (a,b,c)
89 **
90 ** Then the "b" IdList records the list "a,b,c".
91 */
92 struct TrigEvent { int a; IdList * b; };
93 
94 /*
95 ** Disable lookaside memory allocation for objects that might be
96 ** shared across database connections.
97 */
98 static void disableLookaside(Parse *pParse){
99   pParse->disableLookaside++;
100   pParse->db->lookaside.bDisable++;
101 }
102 
103 } // end %include
104 
105 // Input is a single SQL command
106 input ::= cmdlist.
107 cmdlist ::= cmdlist ecmd.
108 cmdlist ::= ecmd.
109 ecmd ::= SEMI.
110 ecmd ::= explain cmdx SEMI.
111 explain ::= .
112 %ifndef SQLITE_OMIT_EXPLAIN
113 explain ::= EXPLAIN.              { pParse->explain = 1; }
114 explain ::= EXPLAIN QUERY PLAN.   { pParse->explain = 2; }
115 %endif  SQLITE_OMIT_EXPLAIN
116 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
117 
118 ///////////////////// Begin and end transactions. ////////////////////////////
119 //
120 
121 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
122 trans_opt ::= .
123 trans_opt ::= TRANSACTION.
124 trans_opt ::= TRANSACTION nm.
125 %type transtype {int}
126 transtype(A) ::= .             {A = TK_DEFERRED;}
127 transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}
128 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
129 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
130 cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
131 cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
132 cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
133 
134 savepoint_opt ::= SAVEPOINT.
135 savepoint_opt ::= .
136 cmd ::= SAVEPOINT nm(X). {
137   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
138 }
139 cmd ::= RELEASE savepoint_opt nm(X). {
140   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
141 }
142 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
143   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
144 }
145 
146 ///////////////////// The CREATE TABLE statement ////////////////////////////
147 //
148 cmd ::= create_table create_table_args.
149 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
150    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
151 }
152 createkw(A) ::= CREATE(A).  {disableLookaside(pParse);}
153 
154 %type ifnotexists {int}
155 ifnotexists(A) ::= .              {A = 0;}
156 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
157 %type temp {int}
158 %ifndef SQLITE_OMIT_TEMPDB
159 temp(A) ::= TEMP.  {A = 1;}
160 %endif  SQLITE_OMIT_TEMPDB
161 temp(A) ::= .      {A = 0;}
162 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
163   sqlite3EndTable(pParse,&X,&E,F,0);
164 }
165 create_table_args ::= AS select(S). {
166   sqlite3EndTable(pParse,0,0,0,S);
167   sqlite3SelectDelete(pParse->db, S);
168 }
169 %type table_options {int}
170 table_options(A) ::= .    {A = 0;}
171 table_options(A) ::= WITHOUT nm(X). {
172   if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
173     A = TF_WithoutRowid | TF_NoVisibleRowid;
174   }else{
175     A = 0;
176     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
177   }
178 }
179 columnlist ::= columnlist COMMA columnname carglist.
180 columnlist ::= columnname carglist.
181 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
182 
183 // Define operator precedence early so that this is the first occurrence
184 // of the operator tokens in the grammer.  Keeping the operators together
185 // causes them to be assigned integer values that are close together,
186 // which keeps parser tables smaller.
187 //
188 // The token values assigned to these symbols is determined by the order
189 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
190 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
191 // the sqlite3ExprIfFalse() routine for additional information on this
192 // constraint.
193 //
194 %left OR.
195 %left AND.
196 %right NOT.
197 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
198 %left GT LE LT GE.
199 %right ESCAPE.
200 %left BITAND BITOR LSHIFT RSHIFT.
201 %left PLUS MINUS.
202 %left STAR SLASH REM.
203 %left CONCAT.
204 %left COLLATE.
205 %right BITNOT.
206 
207 // An IDENTIFIER can be a generic identifier, or one of several
208 // keywords.  Any non-standard keyword can also be an identifier.
209 //
210 %token_class id  ID|INDEXED.
211 
212 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
213 // fallback to ID if they will not parse as their original value.
214 // This obviates the need for the "id" nonterminal.
215 //
216 %fallback ID
217   ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
218   CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
219   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
220   QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
221   ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
222 %ifdef SQLITE_OMIT_COMPOUND_SELECT
223   EXCEPT INTERSECT UNION
224 %endif SQLITE_OMIT_COMPOUND_SELECT
225   REINDEX RENAME CTIME_KW IF
226   .
227 %wildcard ANY.
228 
229 
230 // And "ids" is an identifer-or-string.
231 //
232 %token_class ids  ID|STRING.
233 
234 // The name of a column or table can be any of the following:
235 //
236 %type nm {Token}
237 nm(A) ::= id(A).
238 nm(A) ::= STRING(A).
239 nm(A) ::= JOIN_KW(A).
240 
241 // A typetoken is really zero or more tokens that form a type name such
242 // as can be found after the column name in a CREATE TABLE statement.
243 // Multiple tokens are concatenated to form the value of the typetoken.
244 //
245 %type typetoken {Token}
246 typetoken(A) ::= .   {A.n = 0; A.z = 0;}
247 typetoken(A) ::= typename(A).
248 typetoken(A) ::= typename(A) LP signed RP(Y). {
249   A.n = (int)(&Y.z[Y.n] - A.z);
250 }
251 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
252   A.n = (int)(&Y.z[Y.n] - A.z);
253 }
254 %type typename {Token}
255 typename(A) ::= ids(A).
256 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.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 ccons.
264 carglist ::= .
265 ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
266 ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
267 ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
268 ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
269 ccons ::= DEFAULT MINUS(A) term(X).      {
270   ExprSpan v;
271   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0);
272   v.zStart = A.z;
273   v.zEnd = X.zEnd;
274   sqlite3AddDefaultValue(pParse,&v);
275 }
276 ccons ::= DEFAULT id(X).              {
277   ExprSpan v;
278   spanExpr(&v, pParse, TK_STRING, X);
279   sqlite3AddDefaultValue(pParse,&v);
280 }
281 
282 // In addition to the type name, we also care about the primary key and
283 // UNIQUE constraints.
284 //
285 ccons ::= NULL onconf.
286 ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
287 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
288                                  {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
289 ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
290                                    SQLITE_IDXTYPE_UNIQUE);}
291 ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
292 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
293                                  {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
294 ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
295 ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
296 
297 // The optional AUTOINCREMENT keyword
298 %type autoinc {int}
299 autoinc(X) ::= .          {X = 0;}
300 autoinc(X) ::= AUTOINCR.  {X = 1;}
301 
302 // The next group of rules parses the arguments to a REFERENCES clause
303 // that determine if the referential integrity checking is deferred or
304 // or immediate and which determine what action to take if a ref-integ
305 // check fails.
306 //
307 %type refargs {int}
308 refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
309 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
310 %type refarg {struct {int value; int mask;}}
311 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
312 refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
313 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
314 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
315 %type refact {int}
316 refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
317 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
318 refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
319 refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
320 refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
321 %type defer_subclause {int}
322 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
323 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
324 %type init_deferred_pred_opt {int}
325 init_deferred_pred_opt(A) ::= .                       {A = 0;}
326 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
327 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
328 
329 conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
330 conslist_opt(A) ::= COMMA(A) conslist.
331 conslist ::= conslist tconscomma tcons.
332 conslist ::= tcons.
333 tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
334 tconscomma ::= .
335 tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
336 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
337                                  {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
338 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
339                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
340                                        SQLITE_IDXTYPE_UNIQUE);}
341 tcons ::= CHECK LP expr(E) RP onconf.
342                                  {sqlite3AddCheckConstraint(pParse,E.pExpr);}
343 tcons ::= FOREIGN KEY LP eidlist(FA) RP
344           REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
345     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
346     sqlite3DeferForeignKey(pParse, D);
347 }
348 %type defer_subclause_opt {int}
349 defer_subclause_opt(A) ::= .                    {A = 0;}
350 defer_subclause_opt(A) ::= defer_subclause(A).
351 
352 // The following is a non-standard extension that allows us to declare the
353 // default behavior when there is a constraint conflict.
354 //
355 %type onconf {int}
356 %type orconf {int}
357 %type resolvetype {int}
358 onconf(A) ::= .                              {A = OE_Default;}
359 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
360 orconf(A) ::= .                              {A = OE_Default;}
361 orconf(A) ::= OR resolvetype(X).             {A = X;}
362 resolvetype(A) ::= raisetype(A).
363 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
364 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
365 
366 ////////////////////////// The DROP TABLE /////////////////////////////////////
367 //
368 cmd ::= DROP TABLE ifexists(E) fullname(X). {
369   sqlite3DropTable(pParse, X, 0, E);
370 }
371 %type ifexists {int}
372 ifexists(A) ::= IF EXISTS.   {A = 1;}
373 ifexists(A) ::= .            {A = 0;}
374 
375 ///////////////////// The CREATE VIEW statement /////////////////////////////
376 //
377 %ifndef SQLITE_OMIT_VIEW
378 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
379           AS select(S). {
380   sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
381 }
382 cmd ::= DROP VIEW ifexists(E) fullname(X). {
383   sqlite3DropTable(pParse, X, 1, E);
384 }
385 %endif  SQLITE_OMIT_VIEW
386 
387 //////////////////////// The SELECT statement /////////////////////////////////
388 //
389 cmd ::= select(X).  {
390   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
391   sqlite3Select(pParse, X, &dest);
392   sqlite3SelectDelete(pParse->db, X);
393 }
394 
395 %type select {Select*}
396 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
397 %type selectnowith {Select*}
398 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
399 %type oneselect {Select*}
400 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
401 
402 %include {
403   /*
404   ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
405   ** all elements in the list.  And make sure list length does not exceed
406   ** SQLITE_LIMIT_COMPOUND_SELECT.
407   */
408   static void parserDoubleLinkSelect(Parse *pParse, Select *p){
409     if( p->pPrior ){
410       Select *pNext = 0, *pLoop;
411       int mxSelect, cnt = 0;
412       for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
413         pLoop->pNext = pNext;
414         pLoop->selFlags |= SF_Compound;
415       }
416       if( (p->selFlags & SF_MultiValue)==0 &&
417         (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
418         cnt>mxSelect
419       ){
420         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
421       }
422     }
423   }
424 }
425 
426 select(A) ::= with(W) selectnowith(X). {
427   Select *p = X;
428   if( p ){
429     p->pWith = W;
430     parserDoubleLinkSelect(pParse, p);
431   }else{
432     sqlite3WithDelete(pParse->db, W);
433   }
434   A = p; /*A-overwrites-W*/
435 }
436 
437 selectnowith(A) ::= oneselect(A).
438 %ifndef SQLITE_OMIT_COMPOUND_SELECT
439 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {
440   Select *pRhs = Z;
441   Select *pLhs = A;
442   if( pRhs && pRhs->pPrior ){
443     SrcList *pFrom;
444     Token x;
445     x.n = 0;
446     parserDoubleLinkSelect(pParse, pRhs);
447     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
448     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
449   }
450   if( pRhs ){
451     pRhs->op = (u8)Y;
452     pRhs->pPrior = pLhs;
453     if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
454     pRhs->selFlags &= ~SF_MultiValue;
455     if( Y!=TK_ALL ) pParse->hasCompound = 1;
456   }else{
457     sqlite3SelectDelete(pParse->db, pLhs);
458   }
459   A = pRhs;
460 }
461 %type multiselect_op {int}
462 multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}
463 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
464 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
465 %endif SQLITE_OMIT_COMPOUND_SELECT
466 oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
467                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
468 #if SELECTTRACE_ENABLED
469   Token s = S; /*A-overwrites-S*/
470 #endif
471   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
472 #if SELECTTRACE_ENABLED
473   /* Populate the Select.zSelName[] string that is used to help with
474   ** query planner debugging, to differentiate between multiple Select
475   ** objects in a complex query.
476   **
477   ** If the SELECT keyword is immediately followed by a C-style comment
478   ** then extract the first few alphanumeric characters from within that
479   ** comment to be the zSelName value.  Otherwise, the label is #N where
480   ** is an integer that is incremented with each SELECT statement seen.
481   */
482   if( A!=0 ){
483     const char *z = s.z+6;
484     int i;
485     sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
486                      ++pParse->nSelect);
487     while( z[0]==' ' ) z++;
488     if( z[0]=='/' && z[1]=='*' ){
489       z += 2;
490       while( z[0]==' ' ) z++;
491       for(i=0; sqlite3Isalnum(z[i]); i++){}
492       sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
493     }
494   }
495 #endif /* SELECTRACE_ENABLED */
496 }
497 oneselect(A) ::= values(A).
498 
499 %type values {Select*}
500 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
501 values(A) ::= VALUES LP nexprlist(X) RP. {
502   A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0);
503 }
504 values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
505   Select *pRight, *pLeft = A;
506   pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
507   if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
508   if( pRight ){
509     pRight->op = TK_ALL;
510     pRight->pPrior = pLeft;
511     A = pRight;
512   }else{
513     A = pLeft;
514   }
515 }
516 
517 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
518 // present and false (0) if it is not.
519 //
520 %type distinct {int}
521 distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
522 distinct(A) ::= ALL.        {A = SF_All;}
523 distinct(A) ::= .           {A = 0;}
524 
525 // selcollist is a list of expressions that are to become the return
526 // values of the SELECT statement.  The "*" in statements like
527 // "SELECT * FROM ..." is encoded as a special expression with an
528 // opcode of TK_ASTERISK.
529 //
530 %type selcollist {ExprList*}
531 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
532 %type sclp {ExprList*}
533 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
534 sclp(A) ::= selcollist(A) COMMA.
535 sclp(A) ::= .                                {A = 0;}
536 selcollist(A) ::= sclp(A) expr(X) as(Y).     {
537    A = sqlite3ExprListAppend(pParse, A, X.pExpr);
538    if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
539    sqlite3ExprListSetSpan(pParse,A,&X);
540 }
541 selcollist(A) ::= sclp(A) STAR. {
542   Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
543   A = sqlite3ExprListAppend(pParse, A, p);
544 }
545 selcollist(A) ::= sclp(A) nm(X) DOT STAR. {
546   Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, 0);
547   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
548   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
549   A = sqlite3ExprListAppend(pParse,A, pDot);
550 }
551 
552 // An option "AS <id>" phrase that can follow one of the expressions that
553 // define the result set, or one of the tables in the FROM clause.
554 //
555 %type as {Token}
556 as(X) ::= AS nm(Y).    {X = Y;}
557 as(X) ::= ids(X).
558 as(X) ::= .            {X.n = 0; X.z = 0;}
559 
560 
561 %type seltablist {SrcList*}
562 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
563 %type stl_prefix {SrcList*}
564 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
565 %type from {SrcList*}
566 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
567 
568 // A complete FROM clause.
569 //
570 from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
571 from(A) ::= FROM seltablist(X). {
572   A = X;
573   sqlite3SrcListShiftJoinType(A);
574 }
575 
576 // "seltablist" is a "Select Table List" - the content of the FROM clause
577 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
578 //
579 stl_prefix(A) ::= seltablist(A) joinop(Y).    {
580    if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
581 }
582 stl_prefix(A) ::= .                           {A = 0;}
583 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
584                   on_opt(N) using_opt(U). {
585   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
586   sqlite3SrcListIndexedBy(pParse, A, &I);
587 }
588 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
589                   on_opt(N) using_opt(U). {
590   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
591   sqlite3SrcListFuncArgs(pParse, A, E);
592 }
593 %ifndef SQLITE_OMIT_SUBQUERY
594   seltablist(A) ::= stl_prefix(A) LP select(S) RP
595                     as(Z) on_opt(N) using_opt(U). {
596     A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
597   }
598   seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
599                     as(Z) on_opt(N) using_opt(U). {
600     if( A==0 && Z.n==0 && N==0 && U==0 ){
601       A = F;
602     }else if( F->nSrc==1 ){
603       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
604       if( A ){
605         struct SrcList_item *pNew = &A->a[A->nSrc-1];
606         struct SrcList_item *pOld = F->a;
607         pNew->zName = pOld->zName;
608         pNew->zDatabase = pOld->zDatabase;
609         pNew->pSelect = pOld->pSelect;
610         pOld->zName = pOld->zDatabase = 0;
611         pOld->pSelect = 0;
612       }
613       sqlite3SrcListDelete(pParse->db, F);
614     }else{
615       Select *pSubquery;
616       sqlite3SrcListShiftJoinType(F);
617       pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0);
618       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
619     }
620   }
621 %endif  SQLITE_OMIT_SUBQUERY
622 
623 %type dbnm {Token}
624 dbnm(A) ::= .          {A.z=0; A.n=0;}
625 dbnm(A) ::= DOT nm(X). {A = X;}
626 
627 %type fullname {SrcList*}
628 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
629 fullname(A) ::= nm(X) dbnm(Y).
630    {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
631 
632 %type joinop {int}
633 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
634 joinop(X) ::= JOIN_KW(A) JOIN.
635                   {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}
636 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
637                   {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
638 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
639                   {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
640 
641 %type on_opt {Expr*}
642 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
643 on_opt(N) ::= ON expr(E).   {N = E.pExpr;}
644 on_opt(N) ::= .             {N = 0;}
645 
646 // Note that this block abuses the Token type just a little. If there is
647 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
648 // there is an INDEXED BY clause, then the token is populated as per normal,
649 // with z pointing to the token data and n containing the number of bytes
650 // in the token.
651 //
652 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
653 // normally illegal. The sqlite3SrcListIndexedBy() function
654 // recognizes and interprets this as a special case.
655 //
656 %type indexed_opt {Token}
657 indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
658 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
659 indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
660 
661 %type using_opt {IdList*}
662 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
663 using_opt(U) ::= USING LP idlist(L) RP.  {U = L;}
664 using_opt(U) ::= .                        {U = 0;}
665 
666 
667 %type orderby_opt {ExprList*}
668 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
669 
670 // the sortlist non-terminal stores a list of expression where each
671 // expression is optionally followed by ASC or DESC to indicate the
672 // sort order.
673 //
674 %type sortlist {ExprList*}
675 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
676 
677 orderby_opt(A) ::= .                          {A = 0;}
678 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
679 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
680   A = sqlite3ExprListAppend(pParse,A,Y.pExpr);
681   sqlite3ExprListSetSortOrder(A,Z);
682 }
683 sortlist(A) ::= expr(Y) sortorder(Z). {
684   A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/
685   sqlite3ExprListSetSortOrder(A,Z);
686 }
687 
688 %type sortorder {int}
689 
690 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
691 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
692 sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
693 
694 %type groupby_opt {ExprList*}
695 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
696 groupby_opt(A) ::= .                      {A = 0;}
697 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
698 
699 %type having_opt {Expr*}
700 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
701 having_opt(A) ::= .                {A = 0;}
702 having_opt(A) ::= HAVING expr(X).  {A = X.pExpr;}
703 
704 %type limit_opt {struct LimitVal}
705 
706 // The destructor for limit_opt will never fire in the current grammar.
707 // The limit_opt non-terminal only occurs at the end of a single production
708 // rule for SELECT statements.  As soon as the rule that create the
709 // limit_opt non-terminal reduces, the SELECT statement rule will also
710 // reduce.  So there is never a limit_opt non-terminal on the stack
711 // except as a transient.  So there is never anything to destroy.
712 //
713 //%destructor limit_opt {
714 //  sqlite3ExprDelete(pParse->db, $$.pLimit);
715 //  sqlite3ExprDelete(pParse->db, $$.pOffset);
716 //}
717 limit_opt(A) ::= .                    {A.pLimit = 0; A.pOffset = 0;}
718 limit_opt(A) ::= LIMIT expr(X).       {A.pLimit = X.pExpr; A.pOffset = 0;}
719 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
720                                       {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
721 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
722                                       {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
723 
724 /////////////////////////// The DELETE statement /////////////////////////////
725 //
726 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
727 cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
728         orderby_opt(O) limit_opt(L). {
729   sqlite3WithPush(pParse, C, 1);
730   sqlite3SrcListIndexedBy(pParse, X, &I);
731   W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE");
732   sqlite3DeleteFrom(pParse,X,W);
733 }
734 %endif
735 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
736 cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
737   sqlite3WithPush(pParse, C, 1);
738   sqlite3SrcListIndexedBy(pParse, X, &I);
739   sqlite3DeleteFrom(pParse,X,W);
740 }
741 %endif
742 
743 %type where_opt {Expr*}
744 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
745 
746 where_opt(A) ::= .                    {A = 0;}
747 where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
748 
749 ////////////////////////// The UPDATE command ////////////////////////////////
750 //
751 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
752 cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
753         where_opt(W) orderby_opt(O) limit_opt(L).  {
754   sqlite3WithPush(pParse, C, 1);
755   sqlite3SrcListIndexedBy(pParse, X, &I);
756   sqlite3ExprListCheckLength(pParse,Y,"set list");
757   W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
758   sqlite3Update(pParse,X,Y,W,R);
759 }
760 %endif
761 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
762 cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
763         where_opt(W).  {
764   sqlite3WithPush(pParse, C, 1);
765   sqlite3SrcListIndexedBy(pParse, X, &I);
766   sqlite3ExprListCheckLength(pParse,Y,"set list");
767   sqlite3Update(pParse,X,Y,W,R);
768 }
769 %endif
770 
771 %type setlist {ExprList*}
772 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
773 
774 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
775   A = sqlite3ExprListAppend(pParse, A, Y.pExpr);
776   sqlite3ExprListSetName(pParse, A, &X, 1);
777 }
778 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
779   A = sqlite3ExprListAppendVector(pParse, A, X, Y.pExpr);
780 }
781 setlist(A) ::= nm(X) EQ expr(Y). {
782   A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
783   sqlite3ExprListSetName(pParse, A, &X, 1);
784 }
785 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
786   A = sqlite3ExprListAppendVector(pParse, 0, X, Y.pExpr);
787 }
788 
789 ////////////////////////// The INSERT command /////////////////////////////////
790 //
791 cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). {
792   sqlite3WithPush(pParse, W, 1);
793   sqlite3Insert(pParse, X, S, F, R);
794 }
795 cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
796 {
797   sqlite3WithPush(pParse, W, 1);
798   sqlite3Insert(pParse, X, 0, F, R);
799 }
800 
801 %type insert_cmd {int}
802 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
803 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
804 
805 %type idlist_opt {IdList*}
806 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
807 %type idlist {IdList*}
808 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
809 
810 idlist_opt(A) ::= .                       {A = 0;}
811 idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
812 idlist(A) ::= idlist(A) COMMA nm(Y).
813     {A = sqlite3IdListAppend(pParse->db,A,&Y);}
814 idlist(A) ::= nm(Y).
815     {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
816 
817 /////////////////////////// Expression Processing /////////////////////////////
818 //
819 
820 %type expr {ExprSpan}
821 %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
822 %type term {ExprSpan}
823 %destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
824 
825 %include {
826   /* This is a utility routine used to set the ExprSpan.zStart and
827   ** ExprSpan.zEnd values of pOut so that the span covers the complete
828   ** range of text beginning with pStart and going to the end of pEnd.
829   */
830   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
831     pOut->zStart = pStart->z;
832     pOut->zEnd = &pEnd->z[pEnd->n];
833   }
834 
835   /* Construct a new Expr object from a single identifier.  Use the
836   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
837   ** that created the expression.
838   */
839   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){
840     Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
841     if( p ){
842       memset(p, 0, sizeof(Expr));
843       p->op = (u8)op;
844       p->flags = EP_Leaf;
845       p->iAgg = -1;
846       p->u.zToken = (char*)&p[1];
847       memcpy(p->u.zToken, t.z, t.n);
848       p->u.zToken[t.n] = 0;
849       if( sqlite3Isquote(p->u.zToken[0]) ){
850         if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
851         sqlite3Dequote(p->u.zToken);
852       }
853 #if SQLITE_MAX_EXPR_DEPTH>0
854       p->nHeight = 1;
855 #endif
856     }
857     pOut->pExpr = p;
858     pOut->zStart = t.z;
859     pOut->zEnd = &t.z[t.n];
860   }
861 }
862 
863 expr(A) ::= term(A).
864 expr(A) ::= LP(B) expr(X) RP(E).
865             {spanSet(&A,&B,&E); /*A-overwrites-B*/  A.pExpr = X.pExpr;}
866 term(A) ::= NULL(X).        {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
867 expr(A) ::= id(X).          {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
868 expr(A) ::= JOIN_KW(X).     {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
869 expr(A) ::= nm(X) DOT nm(Y). {
870   Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
871   Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
872   spanSet(&A,&X,&Y); /*A-overwrites-X*/
873   A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
874 }
875 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
876   Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
877   Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
878   Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
879   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
880   spanSet(&A,&X,&Z); /*A-overwrites-X*/
881   A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
882 }
883 term(A) ::= FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
884 term(A) ::= STRING(X).     {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
885 term(A) ::= INTEGER(X). {
886   A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
887   A.zStart = X.z;
888   A.zEnd = X.z + X.n;
889   if( A.pExpr ) A.pExpr->flags |= EP_Leaf;
890 }
891 expr(A) ::= VARIABLE(X).     {
892   if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
893     u32 n = X.n;
894     spanExpr(&A, pParse, TK_VARIABLE, X);
895     sqlite3ExprAssignVarNumber(pParse, A.pExpr, n);
896   }else{
897     /* When doing a nested parse, one can include terms in an expression
898     ** that look like this:   #1 #2 ...  These terms refer to registers
899     ** in the virtual machine.  #N is the N-th register. */
900     Token t = X; /*A-overwrites-X*/
901     assert( t.n>=2 );
902     spanSet(&A, &t, &t);
903     if( pParse->nested==0 ){
904       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
905       A.pExpr = 0;
906     }else{
907       A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, 0);
908       if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable);
909     }
910   }
911 }
912 expr(A) ::= expr(A) COLLATE ids(C). {
913   A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1);
914   A.zEnd = &C.z[C.n];
915 }
916 %ifndef SQLITE_OMIT_CAST
917 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
918   spanSet(&A,&X,&Y); /*A-overwrites-X*/
919   A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
920 }
921 %endif  SQLITE_OMIT_CAST
922 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). {
923   if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
924     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
925   }
926   A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
927   spanSet(&A,&X,&E);
928   if( D==SF_Distinct && A.pExpr ){
929     A.pExpr->flags |= EP_Distinct;
930   }
931 }
932 expr(A) ::= id(X) LP STAR RP(E). {
933   A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
934   spanSet(&A,&X,&E);
935 }
936 term(A) ::= CTIME_KW(OP). {
937   A.pExpr = sqlite3ExprFunction(pParse, 0, &OP);
938   spanSet(&A, &OP, &OP);
939 }
940 
941 %include {
942   /* This routine constructs a binary expression node out of two ExprSpan
943   ** objects and uses the result to populate a new ExprSpan object.
944   */
945   static void spanBinaryExpr(
946     Parse *pParse,      /* The parsing context.  Errors accumulate here */
947     int op,             /* The binary operation */
948     ExprSpan *pLeft,    /* The left operand, and output */
949     ExprSpan *pRight    /* The right operand */
950   ){
951     pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
952     pLeft->zEnd = pRight->zEnd;
953   }
954 
955   /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
956   ** outside of *ppExpr.
957   */
958   static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){
959     if( doNot ){
960       pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0, 0);
961     }
962   }
963 }
964 
965 expr(A) ::= LP(L) nexprlist(X) COMMA expr(Y) RP(R). {
966   ExprList *pList = sqlite3ExprListAppend(pParse, X, Y.pExpr);
967   A.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0, 0);
968   if( A.pExpr ){
969     A.pExpr->x.pList = pList;
970     spanSet(&A, &L, &R);
971   }else{
972     sqlite3ExprListDelete(pParse->db, pList);
973   }
974 }
975 
976 expr(A) ::= expr(A) AND(OP) expr(Y).    {spanBinaryExpr(pParse,@OP,&A,&Y);}
977 expr(A) ::= expr(A) OR(OP) expr(Y).     {spanBinaryExpr(pParse,@OP,&A,&Y);}
978 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
979                                         {spanBinaryExpr(pParse,@OP,&A,&Y);}
980 expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {spanBinaryExpr(pParse,@OP,&A,&Y);}
981 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
982                                         {spanBinaryExpr(pParse,@OP,&A,&Y);}
983 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
984                                         {spanBinaryExpr(pParse,@OP,&A,&Y);}
985 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
986                                         {spanBinaryExpr(pParse,@OP,&A,&Y);}
987 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);}
988 %type likeop {Token}
989 likeop(A) ::= LIKE_KW|MATCH(X).     {A=X;/*A-overwrites-X*/}
990 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
991 expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
992   ExprList *pList;
993   int bNot = OP.n & 0x80000000;
994   OP.n &= 0x7fffffff;
995   pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
996   pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);
997   A.pExpr = sqlite3ExprFunction(pParse, pList, &OP);
998   exprNot(pParse, bNot, &A);
999   A.zEnd = Y.zEnd;
1000   if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
1001 }
1002 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
1003   ExprList *pList;
1004   int bNot = OP.n & 0x80000000;
1005   OP.n &= 0x7fffffff;
1006   pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
1007   pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);
1008   pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
1009   A.pExpr = sqlite3ExprFunction(pParse, pList, &OP);
1010   exprNot(pParse, bNot, &A);
1011   A.zEnd = E.zEnd;
1012   if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
1013 }
1014 
1015 %include {
1016   /* Construct an expression node for a unary postfix operator
1017   */
1018   static void spanUnaryPostfix(
1019     Parse *pParse,         /* Parsing context to record errors */
1020     int op,                /* The operator */
1021     ExprSpan *pOperand,    /* The operand, and output */
1022     Token *pPostOp         /* The operand token for setting the span */
1023   ){
1024     pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
1025     pOperand->zEnd = &pPostOp->z[pPostOp->n];
1026   }
1027 }
1028 
1029 expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {spanUnaryPostfix(pParse,@E,&A,&E);}
1030 expr(A) ::= expr(A) NOT NULL(E). {spanUnaryPostfix(pParse,TK_NOTNULL,&A,&E);}
1031 
1032 %include {
1033   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1034   ** unary TK_ISNULL or TK_NOTNULL expression. */
1035   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1036     sqlite3 *db = pParse->db;
1037     if( pA && pY && pY->op==TK_NULL ){
1038       pA->op = (u8)op;
1039       sqlite3ExprDelete(db, pA->pRight);
1040       pA->pRight = 0;
1041     }
1042   }
1043 }
1044 
1045 //    expr1 IS expr2
1046 //    expr1 IS NOT expr2
1047 //
1048 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
1049 // is any other expression, code as TK_IS or TK_ISNOT.
1050 //
1051 expr(A) ::= expr(A) IS expr(Y).     {
1052   spanBinaryExpr(pParse,TK_IS,&A,&Y);
1053   binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL);
1054 }
1055 expr(A) ::= expr(A) IS NOT expr(Y). {
1056   spanBinaryExpr(pParse,TK_ISNOT,&A,&Y);
1057   binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL);
1058 }
1059 
1060 %include {
1061   /* Construct an expression node for a unary prefix operator
1062   */
1063   static void spanUnaryPrefix(
1064     ExprSpan *pOut,        /* Write the new expression node here */
1065     Parse *pParse,         /* Parsing context to record errors */
1066     int op,                /* The operator */
1067     ExprSpan *pOperand,    /* The operand */
1068     Token *pPreOp         /* The operand token for setting the span */
1069   ){
1070     pOut->zStart = pPreOp->z;
1071     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
1072     pOut->zEnd = pOperand->zEnd;
1073   }
1074 }
1075 
1076 
1077 
1078 expr(A) ::= NOT(B) expr(X).
1079               {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/}
1080 expr(A) ::= BITNOT(B) expr(X).
1081               {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/}
1082 expr(A) ::= MINUS(B) expr(X). [BITNOT]
1083               {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);/*A-overwrites-B*/}
1084 expr(A) ::= PLUS(B) expr(X). [BITNOT]
1085               {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);/*A-overwrites-B*/}
1086 
1087 %type between_op {int}
1088 between_op(A) ::= BETWEEN.     {A = 0;}
1089 between_op(A) ::= NOT BETWEEN. {A = 1;}
1090 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1091   ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
1092   pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
1093   A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0, 0);
1094   if( A.pExpr ){
1095     A.pExpr->x.pList = pList;
1096   }else{
1097     sqlite3ExprListDelete(pParse->db, pList);
1098   }
1099   exprNot(pParse, N, &A);
1100   A.zEnd = Y.zEnd;
1101 }
1102 %ifndef SQLITE_OMIT_SUBQUERY
1103   %type in_op {int}
1104   in_op(A) ::= IN.      {A = 0;}
1105   in_op(A) ::= NOT IN.  {A = 1;}
1106   expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP(E). [IN] {
1107     if( Y==0 ){
1108       /* Expressions of the form
1109       **
1110       **      expr1 IN ()
1111       **      expr1 NOT IN ()
1112       **
1113       ** simplify to constants 0 (false) and 1 (true), respectively,
1114       ** regardless of the value of expr1.
1115       */
1116       sqlite3ExprDelete(pParse->db, A.pExpr);
1117       A.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[N]);
1118     }else if( Y->nExpr==1 ){
1119       /* Expressions of the form:
1120       **
1121       **      expr1 IN (?1)
1122       **      expr1 NOT IN (?2)
1123       **
1124       ** with exactly one value on the RHS can be simplified to something
1125       ** like this:
1126       **
1127       **      expr1 == ?1
1128       **      expr1 <> ?2
1129       **
1130       ** But, the RHS of the == or <> is marked with the EP_Generic flag
1131       ** so that it may not contribute to the computation of comparison
1132       ** affinity or the collating sequence to use for comparison.  Otherwise,
1133       ** the semantics would be subtly different from IN or NOT IN.
1134       */
1135       Expr *pRHS = Y->a[0].pExpr;
1136       Y->a[0].pExpr = 0;
1137       sqlite3ExprListDelete(pParse->db, Y);
1138       /* pRHS cannot be NULL because a malloc error would have been detected
1139       ** before now and control would have never reached this point */
1140       if( ALWAYS(pRHS) ){
1141         pRHS->flags &= ~EP_Collate;
1142         pRHS->flags |= EP_Generic;
1143       }
1144       A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS, 0);
1145     }else{
1146       A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
1147       if( A.pExpr ){
1148         A.pExpr->x.pList = Y;
1149         sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
1150       }else{
1151         sqlite3ExprListDelete(pParse->db, Y);
1152       }
1153       exprNot(pParse, N, &A);
1154     }
1155     A.zEnd = &E.z[E.n];
1156   }
1157   expr(A) ::= LP(B) select(X) RP(E). {
1158     spanSet(&A,&B,&E); /*A-overwrites-B*/
1159     A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
1160     sqlite3PExprAddSelect(pParse, A.pExpr, X);
1161   }
1162   expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E).  [IN] {
1163     A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
1164     sqlite3PExprAddSelect(pParse, A.pExpr, Y);
1165     exprNot(pParse, N, &A);
1166     A.zEnd = &E.z[E.n];
1167   }
1168   expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1169     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
1170     Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
1171     if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1172     A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
1173     sqlite3PExprAddSelect(pParse, A.pExpr, pSelect);
1174     exprNot(pParse, N, &A);
1175     A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
1176   }
1177   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
1178     Expr *p;
1179     spanSet(&A,&B,&E); /*A-overwrites-B*/
1180     p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
1181     sqlite3PExprAddSelect(pParse, p, Y);
1182   }
1183 %endif SQLITE_OMIT_SUBQUERY
1184 
1185 /* CASE expressions */
1186 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
1187   spanSet(&A,&C,&E);  /*A-overwrites-C*/
1188   A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0);
1189   if( A.pExpr ){
1190     A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1191     sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
1192   }else{
1193     sqlite3ExprListDelete(pParse->db, Y);
1194     sqlite3ExprDelete(pParse->db, Z);
1195   }
1196 }
1197 %type case_exprlist {ExprList*}
1198 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1199 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1200   A = sqlite3ExprListAppend(pParse,A, Y.pExpr);
1201   A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
1202 }
1203 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1204   A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
1205   A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
1206 }
1207 %type case_else {Expr*}
1208 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1209 case_else(A) ::=  ELSE expr(X).         {A = X.pExpr;}
1210 case_else(A) ::=  .                     {A = 0;}
1211 %type case_operand {Expr*}
1212 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1213 case_operand(A) ::= expr(X).            {A = X.pExpr; /*A-overwrites-X*/}
1214 case_operand(A) ::= .                   {A = 0;}
1215 
1216 %type exprlist {ExprList*}
1217 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1218 %type nexprlist {ExprList*}
1219 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1220 
1221 exprlist(A) ::= nexprlist(A).
1222 exprlist(A) ::= .                            {A = 0;}
1223 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1224     {A = sqlite3ExprListAppend(pParse,A,Y.pExpr);}
1225 nexprlist(A) ::= expr(Y).
1226     {A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/}
1227 
1228 %ifndef SQLITE_OMIT_SUBQUERY
1229 /* A paren_exprlist is an optional expression list contained inside
1230 ** of parenthesis */
1231 %type paren_exprlist {ExprList*}
1232 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1233 paren_exprlist(A) ::= .   {A = 0;}
1234 paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
1235 %endif SQLITE_OMIT_SUBQUERY
1236 
1237 
1238 ///////////////////////////// The CREATE INDEX command ///////////////////////
1239 //
1240 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1241         ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1242   sqlite3CreateIndex(pParse, &X, &D,
1243                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
1244                       &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1245 }
1246 
1247 %type uniqueflag {int}
1248 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
1249 uniqueflag(A) ::= .        {A = OE_None;}
1250 
1251 
1252 // The eidlist non-terminal (Expression Id List) generates an ExprList
1253 // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
1254 // This list is stored in an ExprList rather than an IdList so that it
1255 // can be easily sent to sqlite3ColumnsExprList().
1256 //
1257 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1258 // used for the arguments to an index.  That is just an historical accident.
1259 //
1260 // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
1261 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1262 // places - places that might have been stored in the sqlite_master schema.
1263 // Those extra features were ignored.  But because they might be in some
1264 // (busted) old databases, we need to continue parsing them when loading
1265 // historical schemas.
1266 //
1267 %type eidlist {ExprList*}
1268 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1269 %type eidlist_opt {ExprList*}
1270 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1271 
1272 %include {
1273   /* Add a single new term to an ExprList that is used to store a
1274   ** list of identifiers.  Report an error if the ID list contains
1275   ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1276   ** error while parsing a legacy schema.
1277   */
1278   static ExprList *parserAddExprIdListTerm(
1279     Parse *pParse,
1280     ExprList *pPrior,
1281     Token *pIdToken,
1282     int hasCollate,
1283     int sortOrder
1284   ){
1285     ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1286     if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1287         && pParse->db->init.busy==0
1288     ){
1289       sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1290                          pIdToken->n, pIdToken->z);
1291     }
1292     sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1293     return p;
1294   }
1295 } // end %include
1296 
1297 eidlist_opt(A) ::= .                         {A = 0;}
1298 eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
1299 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
1300   A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1301 }
1302 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1303   A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1304 }
1305 
1306 %type collate {int}
1307 collate(C) ::= .              {C = 0;}
1308 collate(C) ::= COLLATE ids.   {C = 1;}
1309 
1310 
1311 ///////////////////////////// The DROP INDEX command /////////////////////////
1312 //
1313 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
1314 
1315 ///////////////////////////// The VACUUM command /////////////////////////////
1316 //
1317 %ifndef SQLITE_OMIT_VACUUM
1318 %ifndef SQLITE_OMIT_ATTACH
1319 cmd ::= VACUUM.                {sqlite3Vacuum(pParse,0);}
1320 cmd ::= VACUUM nm(X).          {sqlite3Vacuum(pParse,&X);}
1321 %endif  SQLITE_OMIT_ATTACH
1322 %endif  SQLITE_OMIT_VACUUM
1323 
1324 ///////////////////////////// The PRAGMA command /////////////////////////////
1325 //
1326 %ifndef SQLITE_OMIT_PRAGMA
1327 cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
1328 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1329 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1330 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1331                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1332 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1333                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1334 
1335 nmnum(A) ::= plus_num(A).
1336 nmnum(A) ::= nm(A).
1337 nmnum(A) ::= ON(A).
1338 nmnum(A) ::= DELETE(A).
1339 nmnum(A) ::= DEFAULT(A).
1340 %endif SQLITE_OMIT_PRAGMA
1341 %token_class number INTEGER|FLOAT.
1342 plus_num(A) ::= PLUS number(X).       {A = X;}
1343 plus_num(A) ::= number(A).
1344 minus_num(A) ::= MINUS number(X).     {A = X;}
1345 //////////////////////////// The CREATE TRIGGER command /////////////////////
1346 
1347 %ifndef SQLITE_OMIT_TRIGGER
1348 
1349 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1350   Token all;
1351   all.z = A.z;
1352   all.n = (int)(Z.z - A.z) + Z.n;
1353   sqlite3FinishTrigger(pParse, S, &all);
1354 }
1355 
1356 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1357                     trigger_time(C) trigger_event(D)
1358                     ON fullname(E) foreach_clause when_clause(G). {
1359   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1360   A = (Z.n==0?B:Z); /*A-overwrites-T*/
1361 }
1362 
1363 %type trigger_time {int}
1364 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
1365 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
1366 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1367 trigger_time(A) ::= .            { A = TK_BEFORE; }
1368 
1369 %type trigger_event {struct TrigEvent}
1370 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1371 trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1372 trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1373 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1374 
1375 foreach_clause ::= .
1376 foreach_clause ::= FOR EACH ROW.
1377 
1378 %type when_clause {Expr*}
1379 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1380 when_clause(A) ::= .             { A = 0; }
1381 when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
1382 
1383 %type trigger_cmd_list {TriggerStep*}
1384 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1385 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1386   assert( A!=0 );
1387   A->pLast->pNext = X;
1388   A->pLast = X;
1389 }
1390 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1391   assert( A!=0 );
1392   A->pLast = A;
1393 }
1394 
1395 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1396 // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in
1397 // the same database as the table that the trigger fires on.
1398 //
1399 %type trnm {Token}
1400 trnm(A) ::= nm(A).
1401 trnm(A) ::= nm DOT nm(X). {
1402   A = X;
1403   sqlite3ErrorMsg(pParse,
1404         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1405         "statements within triggers");
1406 }
1407 
1408 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1409 // statements within triggers.  We make a specific error message for this
1410 // since it is an exception to the default grammar rules.
1411 //
1412 tridxby ::= .
1413 tridxby ::= INDEXED BY nm. {
1414   sqlite3ErrorMsg(pParse,
1415         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1416         "within triggers");
1417 }
1418 tridxby ::= NOT INDEXED. {
1419   sqlite3ErrorMsg(pParse,
1420         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1421         "within triggers");
1422 }
1423 
1424 
1425 
1426 %type trigger_cmd {TriggerStep*}
1427 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1428 // UPDATE
1429 trigger_cmd(A) ::=
1430    UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).
1431    {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R);}
1432 
1433 // INSERT
1434 trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S).
1435    {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);/*A-overwrites-R*/}
1436 
1437 // DELETE
1438 trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
1439    {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
1440 
1441 // SELECT
1442 trigger_cmd(A) ::= select(X).
1443    {A = sqlite3TriggerSelectStep(pParse->db, X); /*A-overwrites-X*/}
1444 
1445 // The special RAISE expression that may occur in trigger programs
1446 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
1447   spanSet(&A,&X,&Y);  /*A-overwrites-X*/
1448   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
1449   if( A.pExpr ){
1450     A.pExpr->affinity = OE_Ignore;
1451   }
1452 }
1453 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
1454   spanSet(&A,&X,&Y);  /*A-overwrites-X*/
1455   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z);
1456   if( A.pExpr ) {
1457     A.pExpr->affinity = (char)T;
1458   }
1459 }
1460 %endif  !SQLITE_OMIT_TRIGGER
1461 
1462 %type raisetype {int}
1463 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1464 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1465 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1466 
1467 
1468 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1469 %ifndef SQLITE_OMIT_TRIGGER
1470 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1471   sqlite3DropTrigger(pParse,X,NOERR);
1472 }
1473 %endif  !SQLITE_OMIT_TRIGGER
1474 
1475 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1476 %ifndef SQLITE_OMIT_ATTACH
1477 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1478   sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
1479 }
1480 cmd ::= DETACH database_kw_opt expr(D). {
1481   sqlite3Detach(pParse, D.pExpr);
1482 }
1483 
1484 %type key_opt {Expr*}
1485 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1486 key_opt(A) ::= .                     { A = 0; }
1487 key_opt(A) ::= KEY expr(X).          { A = X.pExpr; }
1488 
1489 database_kw_opt ::= DATABASE.
1490 database_kw_opt ::= .
1491 %endif SQLITE_OMIT_ATTACH
1492 
1493 ////////////////////////// REINDEX collation //////////////////////////////////
1494 %ifndef SQLITE_OMIT_REINDEX
1495 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1496 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1497 %endif  SQLITE_OMIT_REINDEX
1498 
1499 /////////////////////////////////// ANALYZE ///////////////////////////////////
1500 %ifndef SQLITE_OMIT_ANALYZE
1501 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1502 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1503 %endif
1504 
1505 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1506 %ifndef SQLITE_OMIT_ALTERTABLE
1507 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1508   sqlite3AlterRenameTable(pParse,X,&Z);
1509 }
1510 cmd ::= ALTER TABLE add_column_fullname
1511         ADD kwcolumn_opt columnname(Y) carglist. {
1512   Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1513   sqlite3AlterFinishAddColumn(pParse, &Y);
1514 }
1515 add_column_fullname ::= fullname(X). {
1516   disableLookaside(pParse);
1517   sqlite3AlterBeginAddColumn(pParse, X);
1518 }
1519 kwcolumn_opt ::= .
1520 kwcolumn_opt ::= COLUMNKW.
1521 %endif  SQLITE_OMIT_ALTERTABLE
1522 
1523 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1524 %ifndef SQLITE_OMIT_VIRTUALTABLE
1525 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1526 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1527 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1528                 nm(X) dbnm(Y) USING nm(Z). {
1529     sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1530 }
1531 vtabarglist ::= vtabarg.
1532 vtabarglist ::= vtabarglist COMMA vtabarg.
1533 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1534 vtabarg ::= vtabarg vtabargtoken.
1535 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1536 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1537 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1538 anylist ::= .
1539 anylist ::= anylist LP anylist RP.
1540 anylist ::= anylist ANY.
1541 %endif  SQLITE_OMIT_VIRTUALTABLE
1542 
1543 
1544 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1545 %type with {With*}
1546 %type wqlist {With*}
1547 %destructor with {sqlite3WithDelete(pParse->db, $$);}
1548 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1549 
1550 with(A) ::= . {A = 0;}
1551 %ifndef SQLITE_OMIT_CTE
1552 with(A) ::= WITH wqlist(W).              { A = W; }
1553 with(A) ::= WITH RECURSIVE wqlist(W).    { A = W; }
1554 
1555 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1556   A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1557 }
1558 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1559   A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1560 }
1561 %endif  SQLITE_OMIT_CTE
1562