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