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