000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file contains SQLite's grammar for SQL.  Process this file
000013  ** using the lemon parser generator to generate C code that runs
000014  ** the parser.  Lemon will also generate a header file containing
000015  ** numeric codes for all of the tokens.
000016  */
000017  
000018  // All token codes are small integers with #defines that begin with "TK_"
000019  %token_prefix TK_
000020  
000021  // The type of the data attached to each token is Token.  This is also the
000022  // default type for non-terminals.
000023  //
000024  %token_type {Token}
000025  %default_type {Token}
000026  
000027  // The generated parser function takes a 4th argument as follows:
000028  %extra_argument {Parse *pParse}
000029  
000030  // This code runs whenever there is a syntax error
000031  //
000032  %syntax_error {
000033    UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
000034    assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
000035    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
000036  }
000037  %stack_overflow {
000038    sqlite3ErrorMsg(pParse, "parser stack overflow");
000039  }
000040  
000041  // The name of the generated procedure that implements the parser
000042  // is as follows:
000043  %name sqlite3Parser
000044  
000045  // The following text is included near the beginning of the C source
000046  // code file that implements the parser.
000047  //
000048  %include {
000049  #include "sqliteInt.h"
000050  
000051  /*
000052  ** Disable all error recovery processing in the parser push-down
000053  ** automaton.
000054  */
000055  #define YYNOERRORRECOVERY 1
000056  
000057  /*
000058  ** Make yytestcase() the same as testcase()
000059  */
000060  #define yytestcase(X) testcase(X)
000061  
000062  /*
000063  ** Indicate that sqlite3ParserFree() will never be called with a null
000064  ** pointer.
000065  */
000066  #define YYPARSEFREENEVERNULL 1
000067  
000068  /*
000069  ** Alternative datatype for the argument to the malloc() routine passed
000070  ** into sqlite3ParserAlloc().  The default is size_t.
000071  */
000072  #define YYMALLOCARGTYPE  u64
000073  
000074  /*
000075  ** An instance of this structure holds information about the
000076  ** LIMIT clause of a SELECT statement.
000077  */
000078  struct LimitVal {
000079    Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
000080    Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
000081  };
000082  
000083  /*
000084  ** An instance of the following structure describes the event of a
000085  ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
000086  ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
000087  **
000088  **      UPDATE ON (a,b,c)
000089  **
000090  ** Then the "b" IdList records the list "a,b,c".
000091  */
000092  struct TrigEvent { int a; IdList * b; };
000093  
000094  /*
000095  ** Disable lookaside memory allocation for objects that might be
000096  ** shared across database connections.
000097  */
000098  static void disableLookaside(Parse *pParse){
000099    pParse->disableLookaside++;
000100    pParse->db->lookaside.bDisable++;
000101  }
000102  
000103  } // end %include
000104  
000105  // Input is a single SQL command
000106  input ::= cmdlist.
000107  cmdlist ::= cmdlist ecmd.
000108  cmdlist ::= ecmd.
000109  ecmd ::= SEMI.
000110  ecmd ::= explain cmdx SEMI.
000111  explain ::= .
000112  %ifndef SQLITE_OMIT_EXPLAIN
000113  explain ::= EXPLAIN.              { pParse->explain = 1; }
000114  explain ::= EXPLAIN QUERY PLAN.   { pParse->explain = 2; }
000115  %endif  SQLITE_OMIT_EXPLAIN
000116  cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
000117  
000118  ///////////////////// Begin and end transactions. ////////////////////////////
000119  //
000120  
000121  cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
000122  trans_opt ::= .
000123  trans_opt ::= TRANSACTION.
000124  trans_opt ::= TRANSACTION nm.
000125  %type transtype {int}
000126  transtype(A) ::= .             {A = TK_DEFERRED;}
000127  transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}
000128  transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
000129  transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
000130  cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
000131  cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
000132  cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
000133  
000134  savepoint_opt ::= SAVEPOINT.
000135  savepoint_opt ::= .
000136  cmd ::= SAVEPOINT nm(X). {
000137    sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
000138  }
000139  cmd ::= RELEASE savepoint_opt nm(X). {
000140    sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
000141  }
000142  cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
000143    sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
000144  }
000145  
000146  ///////////////////// The CREATE TABLE statement ////////////////////////////
000147  //
000148  cmd ::= create_table create_table_args.
000149  create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
000150     sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
000151  }
000152  createkw(A) ::= CREATE(A).  {disableLookaside(pParse);}
000153  
000154  %type ifnotexists {int}
000155  ifnotexists(A) ::= .              {A = 0;}
000156  ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
000157  %type temp {int}
000158  %ifndef SQLITE_OMIT_TEMPDB
000159  temp(A) ::= TEMP.  {A = 1;}
000160  %endif  SQLITE_OMIT_TEMPDB
000161  temp(A) ::= .      {A = 0;}
000162  create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
000163    sqlite3EndTable(pParse,&X,&E,F,0);
000164  }
000165  create_table_args ::= AS select(S). {
000166    sqlite3EndTable(pParse,0,0,0,S);
000167    sqlite3SelectDelete(pParse->db, S);
000168  }
000169  %type table_options {int}
000170  table_options(A) ::= .    {A = 0;}
000171  table_options(A) ::= WITHOUT nm(X). {
000172    if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
000173      A = TF_WithoutRowid | TF_NoVisibleRowid;
000174    }else{
000175      A = 0;
000176      sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
000177    }
000178  }
000179  columnlist ::= columnlist COMMA columnname carglist.
000180  columnlist ::= columnname carglist.
000181  columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
000182  
000183  // Define operator precedence early so that this is the first occurrence
000184  // of the operator tokens in the grammer.  Keeping the operators together
000185  // causes them to be assigned integer values that are close together,
000186  // which keeps parser tables smaller.
000187  //
000188  // The token values assigned to these symbols is determined by the order
000189  // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
000190  // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
000191  // the sqlite3ExprIfFalse() routine for additional information on this
000192  // constraint.
000193  //
000194  %left OR.
000195  %left AND.
000196  %right NOT.
000197  %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
000198  %left GT LE LT GE.
000199  %right ESCAPE.
000200  %left BITAND BITOR LSHIFT RSHIFT.
000201  %left PLUS MINUS.
000202  %left STAR SLASH REM.
000203  %left CONCAT.
000204  %left COLLATE.
000205  %right BITNOT.
000206  
000207  // An IDENTIFIER can be a generic identifier, or one of several
000208  // keywords.  Any non-standard keyword can also be an identifier.
000209  //
000210  %token_class id  ID|INDEXED.
000211  
000212  // The following directive causes tokens ABORT, AFTER, ASC, etc. to
000213  // fallback to ID if they will not parse as their original value.
000214  // This obviates the need for the "id" nonterminal.
000215  //
000216  %fallback ID
000217    ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
000218    CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
000219    IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
000220    QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
000221    ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
000222  %ifdef SQLITE_OMIT_COMPOUND_SELECT
000223    EXCEPT INTERSECT UNION
000224  %endif SQLITE_OMIT_COMPOUND_SELECT
000225    REINDEX RENAME CTIME_KW IF
000226    .
000227  %wildcard ANY.
000228  
000229  
000230  // And "ids" is an identifer-or-string.
000231  //
000232  %token_class ids  ID|STRING.
000233  
000234  // The name of a column or table can be any of the following:
000235  //
000236  %type nm {Token}
000237  nm(A) ::= id(A).
000238  nm(A) ::= STRING(A).
000239  nm(A) ::= JOIN_KW(A).
000240  
000241  // A typetoken is really zero or more tokens that form a type name such
000242  // as can be found after the column name in a CREATE TABLE statement.
000243  // Multiple tokens are concatenated to form the value of the typetoken.
000244  //
000245  %type typetoken {Token}
000246  typetoken(A) ::= .   {A.n = 0; A.z = 0;}
000247  typetoken(A) ::= typename(A).
000248  typetoken(A) ::= typename(A) LP signed RP(Y). {
000249    A.n = (int)(&Y.z[Y.n] - A.z);
000250  }
000251  typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
000252    A.n = (int)(&Y.z[Y.n] - A.z);
000253  }
000254  %type typename {Token}
000255  typename(A) ::= ids(A).
000256  typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
000257  signed ::= plus_num.
000258  signed ::= minus_num.
000259  
000260  // "carglist" is a list of additional constraints that come after the
000261  // column name and column type in a CREATE TABLE statement.
000262  //
000263  carglist ::= carglist ccons.
000264  carglist ::= .
000265  ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
000266  ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
000267  ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
000268  ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
000269  ccons ::= DEFAULT MINUS(A) term(X).      {
000270    ExprSpan v;
000271    v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0);
000272    v.zStart = A.z;
000273    v.zEnd = X.zEnd;
000274    sqlite3AddDefaultValue(pParse,&v);
000275  }
000276  ccons ::= DEFAULT id(X).              {
000277    ExprSpan v;
000278    spanExpr(&v, pParse, TK_STRING, X);
000279    sqlite3AddDefaultValue(pParse,&v);
000280  }
000281  
000282  // In addition to the type name, we also care about the primary key and
000283  // UNIQUE constraints.
000284  //
000285  ccons ::= NULL onconf.
000286  ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
000287  ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
000288                                   {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
000289  ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
000290                                     SQLITE_IDXTYPE_UNIQUE);}
000291  ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
000292  ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
000293                                   {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
000294  ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
000295  ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
000296  
000297  // The optional AUTOINCREMENT keyword
000298  %type autoinc {int}
000299  autoinc(X) ::= .          {X = 0;}
000300  autoinc(X) ::= AUTOINCR.  {X = 1;}
000301  
000302  // The next group of rules parses the arguments to a REFERENCES clause
000303  // that determine if the referential integrity checking is deferred or
000304  // or immediate and which determine what action to take if a ref-integ
000305  // check fails.
000306  //
000307  %type refargs {int}
000308  refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
000309  refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
000310  %type refarg {struct {int value; int mask;}}
000311  refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
000312  refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
000313  refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
000314  refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
000315  %type refact {int}
000316  refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
000317  refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
000318  refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
000319  refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
000320  refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
000321  %type defer_subclause {int}
000322  defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
000323  defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
000324  %type init_deferred_pred_opt {int}
000325  init_deferred_pred_opt(A) ::= .                       {A = 0;}
000326  init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
000327  init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
000328  
000329  conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
000330  conslist_opt(A) ::= COMMA(A) conslist.
000331  conslist ::= conslist tconscomma tcons.
000332  conslist ::= tcons.
000333  tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
000334  tconscomma ::= .
000335  tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
000336  tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
000337                                   {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
000338  tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
000339                                   {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
000340                                         SQLITE_IDXTYPE_UNIQUE);}
000341  tcons ::= CHECK LP expr(E) RP onconf.
000342                                   {sqlite3AddCheckConstraint(pParse,E.pExpr);}
000343  tcons ::= FOREIGN KEY LP eidlist(FA) RP
000344            REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
000345      sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
000346      sqlite3DeferForeignKey(pParse, D);
000347  }
000348  %type defer_subclause_opt {int}
000349  defer_subclause_opt(A) ::= .                    {A = 0;}
000350  defer_subclause_opt(A) ::= defer_subclause(A).
000351  
000352  // The following is a non-standard extension that allows us to declare the
000353  // default behavior when there is a constraint conflict.
000354  //
000355  %type onconf {int}
000356  %type orconf {int}
000357  %type resolvetype {int}
000358  onconf(A) ::= .                              {A = OE_Default;}
000359  onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
000360  orconf(A) ::= .                              {A = OE_Default;}
000361  orconf(A) ::= OR resolvetype(X).             {A = X;}
000362  resolvetype(A) ::= raisetype(A).
000363  resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
000364  resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
000365  
000366  ////////////////////////// The DROP TABLE /////////////////////////////////////
000367  //
000368  cmd ::= DROP TABLE ifexists(E) fullname(X). {
000369    sqlite3DropTable(pParse, X, 0, E);
000370  }
000371  %type ifexists {int}
000372  ifexists(A) ::= IF EXISTS.   {A = 1;}
000373  ifexists(A) ::= .            {A = 0;}
000374  
000375  ///////////////////// The CREATE VIEW statement /////////////////////////////
000376  //
000377  %ifndef SQLITE_OMIT_VIEW
000378  cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
000379            AS select(S). {
000380    sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
000381  }
000382  cmd ::= DROP VIEW ifexists(E) fullname(X). {
000383    sqlite3DropTable(pParse, X, 1, E);
000384  }
000385  %endif  SQLITE_OMIT_VIEW
000386  
000387  //////////////////////// The SELECT statement /////////////////////////////////
000388  //
000389  cmd ::= select(X).  {
000390    SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
000391    sqlite3Select(pParse, X, &dest);
000392    sqlite3SelectDelete(pParse->db, X);
000393  }
000394  
000395  %type select {Select*}
000396  %destructor select {sqlite3SelectDelete(pParse->db, $$);}
000397  %type selectnowith {Select*}
000398  %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
000399  %type oneselect {Select*}
000400  %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
000401  
000402  %include {
000403    /*
000404    ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
000405    ** all elements in the list.  And make sure list length does not exceed
000406    ** SQLITE_LIMIT_COMPOUND_SELECT.
000407    */
000408    static void parserDoubleLinkSelect(Parse *pParse, Select *p){
000409      if( p->pPrior ){
000410        Select *pNext = 0, *pLoop;
000411        int mxSelect, cnt = 0;
000412        for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
000413          pLoop->pNext = pNext;
000414          pLoop->selFlags |= SF_Compound;
000415        }
000416        if( (p->selFlags & SF_MultiValue)==0 && 
000417          (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
000418          cnt>mxSelect
000419        ){
000420          sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
000421        }
000422      }
000423    }
000424  }
000425  
000426  select(A) ::= with(W) selectnowith(X). {
000427    Select *p = X;
000428    if( p ){
000429      p->pWith = W;
000430      parserDoubleLinkSelect(pParse, p);
000431    }else{
000432      sqlite3WithDelete(pParse->db, W);
000433    }
000434    A = p; /*A-overwrites-W*/
000435  }
000436  
000437  selectnowith(A) ::= oneselect(A).
000438  %ifndef SQLITE_OMIT_COMPOUND_SELECT
000439  selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {
000440    Select *pRhs = Z;
000441    Select *pLhs = A;
000442    if( pRhs && pRhs->pPrior ){
000443      SrcList *pFrom;
000444      Token x;
000445      x.n = 0;
000446      parserDoubleLinkSelect(pParse, pRhs);
000447      pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
000448      pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
000449    }
000450    if( pRhs ){
000451      pRhs->op = (u8)Y;
000452      pRhs->pPrior = pLhs;
000453      if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
000454      pRhs->selFlags &= ~SF_MultiValue;
000455      if( Y!=TK_ALL ) pParse->hasCompound = 1;
000456    }else{
000457      sqlite3SelectDelete(pParse->db, pLhs);
000458    }
000459    A = pRhs;
000460  }
000461  %type multiselect_op {int}
000462  multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}
000463  multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
000464  multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
000465  %endif SQLITE_OMIT_COMPOUND_SELECT
000466  oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
000467                   groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
000468  #if SELECTTRACE_ENABLED
000469    Token s = S; /*A-overwrites-S*/
000470  #endif
000471    A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
000472  #if SELECTTRACE_ENABLED
000473    /* Populate the Select.zSelName[] string that is used to help with
000474    ** query planner debugging, to differentiate between multiple Select
000475    ** objects in a complex query.
000476    **
000477    ** If the SELECT keyword is immediately followed by a C-style comment
000478    ** then extract the first few alphanumeric characters from within that
000479    ** comment to be the zSelName value.  Otherwise, the label is #N where
000480    ** is an integer that is incremented with each SELECT statement seen.
000481    */
000482    if( A!=0 ){
000483      const char *z = s.z+6;
000484      int i;
000485      sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
000486                       ++pParse->nSelect);
000487      while( z[0]==' ' ) z++;
000488      if( z[0]=='/' && z[1]=='*' ){
000489        z += 2;
000490        while( z[0]==' ' ) z++;
000491        for(i=0; sqlite3Isalnum(z[i]); i++){}
000492        sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
000493      }
000494    }
000495  #endif /* SELECTRACE_ENABLED */
000496  }
000497  oneselect(A) ::= values(A).
000498  
000499  %type values {Select*}
000500  %destructor values {sqlite3SelectDelete(pParse->db, $$);}
000501  values(A) ::= VALUES LP nexprlist(X) RP. {
000502    A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0);
000503  }
000504  values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
000505    Select *pRight, *pLeft = A;
000506    pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
000507    if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
000508    if( pRight ){
000509      pRight->op = TK_ALL;
000510      pRight->pPrior = pLeft;
000511      A = pRight;
000512    }else{
000513      A = pLeft;
000514    }
000515  }
000516  
000517  // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
000518  // present and false (0) if it is not.
000519  //
000520  %type distinct {int}
000521  distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
000522  distinct(A) ::= ALL.        {A = SF_All;}
000523  distinct(A) ::= .           {A = 0;}
000524  
000525  // selcollist is a list of expressions that are to become the return
000526  // values of the SELECT statement.  The "*" in statements like
000527  // "SELECT * FROM ..." is encoded as a special expression with an
000528  // opcode of TK_ASTERISK.
000529  //
000530  %type selcollist {ExprList*}
000531  %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
000532  %type sclp {ExprList*}
000533  %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
000534  sclp(A) ::= selcollist(A) COMMA.
000535  sclp(A) ::= .                                {A = 0;}
000536  selcollist(A) ::= sclp(A) expr(X) as(Y).     {
000537     A = sqlite3ExprListAppend(pParse, A, X.pExpr);
000538     if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
000539     sqlite3ExprListSetSpan(pParse,A,&X);
000540  }
000541  selcollist(A) ::= sclp(A) STAR. {
000542    Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
000543    A = sqlite3ExprListAppend(pParse, A, p);
000544  }
000545  selcollist(A) ::= sclp(A) nm(X) DOT STAR. {
000546    Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
000547    Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
000548    Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
000549    A = sqlite3ExprListAppend(pParse,A, pDot);
000550  }
000551  
000552  // An option "AS <id>" phrase that can follow one of the expressions that
000553  // define the result set, or one of the tables in the FROM clause.
000554  //
000555  %type as {Token}
000556  as(X) ::= AS nm(Y).    {X = Y;}
000557  as(X) ::= ids(X).
000558  as(X) ::= .            {X.n = 0; X.z = 0;}
000559  
000560  
000561  %type seltablist {SrcList*}
000562  %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
000563  %type stl_prefix {SrcList*}
000564  %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
000565  %type from {SrcList*}
000566  %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
000567  
000568  // A complete FROM clause.
000569  //
000570  from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
000571  from(A) ::= FROM seltablist(X). {
000572    A = X;
000573    sqlite3SrcListShiftJoinType(A);
000574  }
000575  
000576  // "seltablist" is a "Select Table List" - the content of the FROM clause
000577  // in a SELECT statement.  "stl_prefix" is a prefix of this list.
000578  //
000579  stl_prefix(A) ::= seltablist(A) joinop(Y).    {
000580     if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
000581  }
000582  stl_prefix(A) ::= .                           {A = 0;}
000583  seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
000584                    on_opt(N) using_opt(U). {
000585    A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
000586    sqlite3SrcListIndexedBy(pParse, A, &I);
000587  }
000588  seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
000589                    on_opt(N) using_opt(U). {
000590    A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
000591    sqlite3SrcListFuncArgs(pParse, A, E);
000592  }
000593  %ifndef SQLITE_OMIT_SUBQUERY
000594    seltablist(A) ::= stl_prefix(A) LP select(S) RP
000595                      as(Z) on_opt(N) using_opt(U). {
000596      A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
000597    }
000598    seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
000599                      as(Z) on_opt(N) using_opt(U). {
000600      if( A==0 && Z.n==0 && N==0 && U==0 ){
000601        A = F;
000602      }else if( F->nSrc==1 ){
000603        A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
000604        if( A ){
000605          struct SrcList_item *pNew = &A->a[A->nSrc-1];
000606          struct SrcList_item *pOld = F->a;
000607          pNew->zName = pOld->zName;
000608          pNew->zDatabase = pOld->zDatabase;
000609          pNew->pSelect = pOld->pSelect;
000610          pOld->zName = pOld->zDatabase = 0;
000611          pOld->pSelect = 0;
000612        }
000613        sqlite3SrcListDelete(pParse->db, F);
000614      }else{
000615        Select *pSubquery;
000616        sqlite3SrcListShiftJoinType(F);
000617        pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0);
000618        A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
000619      }
000620    }
000621  %endif  SQLITE_OMIT_SUBQUERY
000622  
000623  %type dbnm {Token}
000624  dbnm(A) ::= .          {A.z=0; A.n=0;}
000625  dbnm(A) ::= DOT nm(X). {A = X;}
000626  
000627  %type fullname {SrcList*}
000628  %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
000629  fullname(A) ::= nm(X) dbnm(Y).  
000630     {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
000631  
000632  %type joinop {int}
000633  joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
000634  joinop(X) ::= JOIN_KW(A) JOIN.
000635                    {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}
000636  joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
000637                    {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
000638  joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
000639                    {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
000640  
000641  %type on_opt {Expr*}
000642  %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
000643  on_opt(N) ::= ON expr(E).   {N = E.pExpr;}
000644  on_opt(N) ::= .             {N = 0;}
000645  
000646  // Note that this block abuses the Token type just a little. If there is
000647  // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
000648  // there is an INDEXED BY clause, then the token is populated as per normal,
000649  // with z pointing to the token data and n containing the number of bytes
000650  // in the token.
000651  //
000652  // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is 
000653  // normally illegal. The sqlite3SrcListIndexedBy() function 
000654  // recognizes and interprets this as a special case.
000655  //
000656  %type indexed_opt {Token}
000657  indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
000658  indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
000659  indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
000660  
000661  %type using_opt {IdList*}
000662  %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
000663  using_opt(U) ::= USING LP idlist(L) RP.  {U = L;}
000664  using_opt(U) ::= .                        {U = 0;}
000665  
000666  
000667  %type orderby_opt {ExprList*}
000668  %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
000669  
000670  // the sortlist non-terminal stores a list of expression where each
000671  // expression is optionally followed by ASC or DESC to indicate the
000672  // sort order.
000673  //
000674  %type sortlist {ExprList*}
000675  %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
000676  
000677  orderby_opt(A) ::= .                          {A = 0;}
000678  orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
000679  sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
000680    A = sqlite3ExprListAppend(pParse,A,Y.pExpr);
000681    sqlite3ExprListSetSortOrder(A,Z);
000682  }
000683  sortlist(A) ::= expr(Y) sortorder(Z). {
000684    A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/
000685    sqlite3ExprListSetSortOrder(A,Z);
000686  }
000687  
000688  %type sortorder {int}
000689  
000690  sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
000691  sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
000692  sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
000693  
000694  %type groupby_opt {ExprList*}
000695  %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
000696  groupby_opt(A) ::= .                      {A = 0;}
000697  groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
000698  
000699  %type having_opt {Expr*}
000700  %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
000701  having_opt(A) ::= .                {A = 0;}
000702  having_opt(A) ::= HAVING expr(X).  {A = X.pExpr;}
000703  
000704  %type limit_opt {struct LimitVal}
000705  
000706  // The destructor for limit_opt will never fire in the current grammar.
000707  // The limit_opt non-terminal only occurs at the end of a single production
000708  // rule for SELECT statements.  As soon as the rule that create the 
000709  // limit_opt non-terminal reduces, the SELECT statement rule will also
000710  // reduce.  So there is never a limit_opt non-terminal on the stack 
000711  // except as a transient.  So there is never anything to destroy.
000712  //
000713  //%destructor limit_opt {
000714  //  sqlite3ExprDelete(pParse->db, $$.pLimit);
000715  //  sqlite3ExprDelete(pParse->db, $$.pOffset);
000716  //}
000717  limit_opt(A) ::= .                    {A.pLimit = 0; A.pOffset = 0;}
000718  limit_opt(A) ::= LIMIT expr(X).       {A.pLimit = X.pExpr; A.pOffset = 0;}
000719  limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 
000720                                        {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
000721  limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 
000722                                        {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
000723  
000724  /////////////////////////// The DELETE statement /////////////////////////////
000725  //
000726  %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000727  cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W) 
000728          orderby_opt(O) limit_opt(L). {
000729    sqlite3WithPush(pParse, C, 1);
000730    sqlite3SrcListIndexedBy(pParse, X, &I);
000731    W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE");
000732    sqlite3DeleteFrom(pParse,X,W);
000733  }
000734  %endif
000735  %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000736  cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
000737    sqlite3WithPush(pParse, C, 1);
000738    sqlite3SrcListIndexedBy(pParse, X, &I);
000739    sqlite3DeleteFrom(pParse,X,W);
000740  }
000741  %endif
000742  
000743  %type where_opt {Expr*}
000744  %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
000745  
000746  where_opt(A) ::= .                    {A = 0;}
000747  where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
000748  
000749  ////////////////////////// The UPDATE command ////////////////////////////////
000750  //
000751  %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000752  cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
000753          where_opt(W) orderby_opt(O) limit_opt(L).  {
000754    sqlite3WithPush(pParse, C, 1);
000755    sqlite3SrcListIndexedBy(pParse, X, &I);
000756    sqlite3ExprListCheckLength(pParse,Y,"set list"); 
000757    W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
000758    sqlite3Update(pParse,X,Y,W,R);
000759  }
000760  %endif
000761  %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000762  cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
000763          where_opt(W).  {
000764    sqlite3WithPush(pParse, C, 1);
000765    sqlite3SrcListIndexedBy(pParse, X, &I);
000766    sqlite3ExprListCheckLength(pParse,Y,"set list"); 
000767    sqlite3Update(pParse,X,Y,W,R);
000768  }
000769  %endif
000770  
000771  %type setlist {ExprList*}
000772  %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
000773  
000774  setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
000775    A = sqlite3ExprListAppend(pParse, A, Y.pExpr);
000776    sqlite3ExprListSetName(pParse, A, &X, 1);
000777  }
000778  setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
000779    A = sqlite3ExprListAppendVector(pParse, A, X, Y.pExpr);
000780  }
000781  setlist(A) ::= nm(X) EQ expr(Y). {
000782    A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
000783    sqlite3ExprListSetName(pParse, A, &X, 1);
000784  }
000785  setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
000786    A = sqlite3ExprListAppendVector(pParse, 0, X, Y.pExpr);
000787  }
000788  
000789  ////////////////////////// The INSERT command /////////////////////////////////
000790  //
000791  cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). {
000792    sqlite3WithPush(pParse, W, 1);
000793    sqlite3Insert(pParse, X, S, F, R);
000794  }
000795  cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
000796  {
000797    sqlite3WithPush(pParse, W, 1);
000798    sqlite3Insert(pParse, X, 0, F, R);
000799  }
000800  
000801  %type insert_cmd {int}
000802  insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
000803  insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
000804  
000805  %type idlist_opt {IdList*}
000806  %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
000807  %type idlist {IdList*}
000808  %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
000809  
000810  idlist_opt(A) ::= .                       {A = 0;}
000811  idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
000812  idlist(A) ::= idlist(A) COMMA nm(Y).
000813      {A = sqlite3IdListAppend(pParse->db,A,&Y);}
000814  idlist(A) ::= nm(Y).
000815      {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
000816  
000817  /////////////////////////// Expression Processing /////////////////////////////
000818  //
000819  
000820  %type expr {ExprSpan}
000821  %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
000822  %type term {ExprSpan}
000823  %destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
000824  
000825  %include {
000826    /* This is a utility routine used to set the ExprSpan.zStart and
000827    ** ExprSpan.zEnd values of pOut so that the span covers the complete
000828    ** range of text beginning with pStart and going to the end of pEnd.
000829    */
000830    static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
000831      pOut->zStart = pStart->z;
000832      pOut->zEnd = &pEnd->z[pEnd->n];
000833    }
000834  
000835    /* Construct a new Expr object from a single identifier.  Use the
000836    ** new Expr to populate pOut.  Set the span of pOut to be the identifier
000837    ** that created the expression.
000838    */
000839    static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){
000840      Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
000841      if( p ){
000842        memset(p, 0, sizeof(Expr));
000843        p->op = (u8)op;
000844        p->flags = EP_Leaf;
000845        p->iAgg = -1;
000846        p->u.zToken = (char*)&p[1];
000847        memcpy(p->u.zToken, t.z, t.n);
000848        p->u.zToken[t.n] = 0;
000849        if( sqlite3Isquote(p->u.zToken[0]) ){
000850          if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
000851          sqlite3Dequote(p->u.zToken);
000852        }
000853  #if SQLITE_MAX_EXPR_DEPTH>0
000854        p->nHeight = 1;
000855  #endif  
000856      }
000857      pOut->pExpr = p;
000858      pOut->zStart = t.z;
000859      pOut->zEnd = &t.z[t.n];
000860    }
000861  }
000862  
000863  expr(A) ::= term(A).
000864  expr(A) ::= LP(B) expr(X) RP(E).
000865              {spanSet(&A,&B,&E); /*A-overwrites-B*/  A.pExpr = X.pExpr;}
000866  term(A) ::= NULL(X).        {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
000867  expr(A) ::= id(X).          {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
000868  expr(A) ::= JOIN_KW(X).     {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
000869  expr(A) ::= nm(X) DOT nm(Y). {
000870    Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
000871    Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
000872    spanSet(&A,&X,&Y); /*A-overwrites-X*/
000873    A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
000874  }
000875  expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
000876    Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
000877    Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
000878    Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
000879    Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
000880    spanSet(&A,&X,&Z); /*A-overwrites-X*/
000881    A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
000882  }
000883  term(A) ::= FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
000884  term(A) ::= STRING(X).     {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
000885  term(A) ::= INTEGER(X). {
000886    A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
000887    A.zStart = X.z;
000888    A.zEnd = X.z + X.n;
000889    if( A.pExpr ) A.pExpr->flags |= EP_Leaf;
000890  }
000891  expr(A) ::= VARIABLE(X).     {
000892    if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
000893      u32 n = X.n;
000894      spanExpr(&A, pParse, TK_VARIABLE, X);
000895      sqlite3ExprAssignVarNumber(pParse, A.pExpr, n);
000896    }else{
000897      /* When doing a nested parse, one can include terms in an expression
000898      ** that look like this:   #1 #2 ...  These terms refer to registers
000899      ** in the virtual machine.  #N is the N-th register. */
000900      Token t = X; /*A-overwrites-X*/
000901      assert( t.n>=2 );
000902      spanSet(&A, &t, &t);
000903      if( pParse->nested==0 ){
000904        sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
000905        A.pExpr = 0;
000906      }else{
000907        A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
000908        if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable);
000909      }
000910    }
000911  }
000912  expr(A) ::= expr(A) COLLATE ids(C). {
000913    A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1);
000914    A.zEnd = &C.z[C.n];
000915  }
000916  %ifndef SQLITE_OMIT_CAST
000917  expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
000918    spanSet(&A,&X,&Y); /*A-overwrites-X*/
000919    A.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
000920    sqlite3ExprAttachSubtrees(pParse->db, A.pExpr, E.pExpr, 0);
000921  }
000922  %endif  SQLITE_OMIT_CAST
000923  expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). {
000924    if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
000925      sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
000926    }
000927    A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
000928    spanSet(&A,&X,&E);
000929    if( D==SF_Distinct && A.pExpr ){
000930      A.pExpr->flags |= EP_Distinct;
000931    }
000932  }
000933  expr(A) ::= id(X) LP STAR RP(E). {
000934    A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
000935    spanSet(&A,&X,&E);
000936  }
000937  term(A) ::= CTIME_KW(OP). {
000938    A.pExpr = sqlite3ExprFunction(pParse, 0, &OP);
000939    spanSet(&A, &OP, &OP);
000940  }
000941  
000942  %include {
000943    /* This routine constructs a binary expression node out of two ExprSpan
000944    ** objects and uses the result to populate a new ExprSpan object.
000945    */
000946    static void spanBinaryExpr(
000947      Parse *pParse,      /* The parsing context.  Errors accumulate here */
000948      int op,             /* The binary operation */
000949      ExprSpan *pLeft,    /* The left operand, and output */
000950      ExprSpan *pRight    /* The right operand */
000951    ){
000952      pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr);
000953      pLeft->zEnd = pRight->zEnd;
000954    }
000955  
000956    /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
000957    ** outside of *ppExpr.
000958    */
000959    static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){
000960      if( doNot ){
000961        pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0);
000962      }
000963    }
000964  }
000965  
000966  expr(A) ::= LP(L) nexprlist(X) COMMA expr(Y) RP(R). {
000967    ExprList *pList = sqlite3ExprListAppend(pParse, X, Y.pExpr);
000968    A.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
000969    if( A.pExpr ){
000970      A.pExpr->x.pList = pList;
000971      spanSet(&A, &L, &R);
000972    }else{
000973      sqlite3ExprListDelete(pParse->db, pList);
000974    }
000975  }
000976  
000977  expr(A) ::= expr(A) AND(OP) expr(Y).    {spanBinaryExpr(pParse,@OP,&A,&Y);}
000978  expr(A) ::= expr(A) OR(OP) expr(Y).     {spanBinaryExpr(pParse,@OP,&A,&Y);}
000979  expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
000980                                          {spanBinaryExpr(pParse,@OP,&A,&Y);}
000981  expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {spanBinaryExpr(pParse,@OP,&A,&Y);}
000982  expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
000983                                          {spanBinaryExpr(pParse,@OP,&A,&Y);}
000984  expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
000985                                          {spanBinaryExpr(pParse,@OP,&A,&Y);}
000986  expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
000987                                          {spanBinaryExpr(pParse,@OP,&A,&Y);}
000988  expr(A) ::= expr(A) CONCAT(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);}
000989  %type likeop {Token}
000990  likeop(A) ::= LIKE_KW|MATCH(X).     {A=X;/*A-overwrites-X*/}
000991  likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
000992  expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
000993    ExprList *pList;
000994    int bNot = OP.n & 0x80000000;
000995    OP.n &= 0x7fffffff;
000996    pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
000997    pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);
000998    A.pExpr = sqlite3ExprFunction(pParse, pList, &OP);
000999    exprNot(pParse, bNot, &A);
001000    A.zEnd = Y.zEnd;
001001    if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
001002  }
001003  expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
001004    ExprList *pList;
001005    int bNot = OP.n & 0x80000000;
001006    OP.n &= 0x7fffffff;
001007    pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
001008    pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);
001009    pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
001010    A.pExpr = sqlite3ExprFunction(pParse, pList, &OP);
001011    exprNot(pParse, bNot, &A);
001012    A.zEnd = E.zEnd;
001013    if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
001014  }
001015  
001016  %include {
001017    /* Construct an expression node for a unary postfix operator
001018    */
001019    static void spanUnaryPostfix(
001020      Parse *pParse,         /* Parsing context to record errors */
001021      int op,                /* The operator */
001022      ExprSpan *pOperand,    /* The operand, and output */
001023      Token *pPostOp         /* The operand token for setting the span */
001024    ){
001025      pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
001026      pOperand->zEnd = &pPostOp->z[pPostOp->n];
001027    }                           
001028  }
001029  
001030  expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {spanUnaryPostfix(pParse,@E,&A,&E);}
001031  expr(A) ::= expr(A) NOT NULL(E). {spanUnaryPostfix(pParse,TK_NOTNULL,&A,&E);}
001032  
001033  %include {
001034    /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
001035    ** unary TK_ISNULL or TK_NOTNULL expression. */
001036    static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
001037      sqlite3 *db = pParse->db;
001038      if( pA && pY && pY->op==TK_NULL ){
001039        pA->op = (u8)op;
001040        sqlite3ExprDelete(db, pA->pRight);
001041        pA->pRight = 0;
001042      }
001043    }
001044  }
001045  
001046  //    expr1 IS expr2
001047  //    expr1 IS NOT expr2
001048  //
001049  // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
001050  // is any other expression, code as TK_IS or TK_ISNOT.
001051  // 
001052  expr(A) ::= expr(A) IS expr(Y).     {
001053    spanBinaryExpr(pParse,TK_IS,&A,&Y);
001054    binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL);
001055  }
001056  expr(A) ::= expr(A) IS NOT expr(Y). {
001057    spanBinaryExpr(pParse,TK_ISNOT,&A,&Y);
001058    binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL);
001059  }
001060  
001061  %include {
001062    /* Construct an expression node for a unary prefix operator
001063    */
001064    static void spanUnaryPrefix(
001065      ExprSpan *pOut,        /* Write the new expression node here */
001066      Parse *pParse,         /* Parsing context to record errors */
001067      int op,                /* The operator */
001068      ExprSpan *pOperand,    /* The operand */
001069      Token *pPreOp         /* The operand token for setting the span */
001070    ){
001071      pOut->zStart = pPreOp->z;
001072      pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
001073      pOut->zEnd = pOperand->zEnd;
001074    }
001075  }
001076  
001077  
001078  
001079  expr(A) ::= NOT(B) expr(X).  
001080                {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/}
001081  expr(A) ::= BITNOT(B) expr(X).
001082                {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/}
001083  expr(A) ::= MINUS(B) expr(X). [BITNOT]
001084                {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);/*A-overwrites-B*/}
001085  expr(A) ::= PLUS(B) expr(X). [BITNOT]
001086                {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);/*A-overwrites-B*/}
001087  
001088  %type between_op {int}
001089  between_op(A) ::= BETWEEN.     {A = 0;}
001090  between_op(A) ::= NOT BETWEEN. {A = 1;}
001091  expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
001092    ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
001093    pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
001094    A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0);
001095    if( A.pExpr ){
001096      A.pExpr->x.pList = pList;
001097    }else{
001098      sqlite3ExprListDelete(pParse->db, pList);
001099    } 
001100    exprNot(pParse, N, &A);
001101    A.zEnd = Y.zEnd;
001102  }
001103  %ifndef SQLITE_OMIT_SUBQUERY
001104    %type in_op {int}
001105    in_op(A) ::= IN.      {A = 0;}
001106    in_op(A) ::= NOT IN.  {A = 1;}
001107    expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP(E). [IN] {
001108      if( Y==0 ){
001109        /* Expressions of the form
001110        **
001111        **      expr1 IN ()
001112        **      expr1 NOT IN ()
001113        **
001114        ** simplify to constants 0 (false) and 1 (true), respectively,
001115        ** regardless of the value of expr1.
001116        */
001117        sqlite3ExprDelete(pParse->db, A.pExpr);
001118        A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
001119      }else if( Y->nExpr==1 ){
001120        /* Expressions of the form:
001121        **
001122        **      expr1 IN (?1)
001123        **      expr1 NOT IN (?2)
001124        **
001125        ** with exactly one value on the RHS can be simplified to something
001126        ** like this:
001127        **
001128        **      expr1 == ?1
001129        **      expr1 <> ?2
001130        **
001131        ** But, the RHS of the == or <> is marked with the EP_Generic flag
001132        ** so that it may not contribute to the computation of comparison
001133        ** affinity or the collating sequence to use for comparison.  Otherwise,
001134        ** the semantics would be subtly different from IN or NOT IN.
001135        */
001136        Expr *pRHS = Y->a[0].pExpr;
001137        Y->a[0].pExpr = 0;
001138        sqlite3ExprListDelete(pParse->db, Y);
001139        /* pRHS cannot be NULL because a malloc error would have been detected
001140        ** before now and control would have never reached this point */
001141        if( ALWAYS(pRHS) ){
001142          pRHS->flags &= ~EP_Collate;
001143          pRHS->flags |= EP_Generic;
001144        }
001145        A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS);
001146      }else{
001147        A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
001148        if( A.pExpr ){
001149          A.pExpr->x.pList = Y;
001150          sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
001151        }else{
001152          sqlite3ExprListDelete(pParse->db, Y);
001153        }
001154        exprNot(pParse, N, &A);
001155      }
001156      A.zEnd = &E.z[E.n];
001157    }
001158    expr(A) ::= LP(B) select(X) RP(E). {
001159      spanSet(&A,&B,&E); /*A-overwrites-B*/
001160      A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
001161      sqlite3PExprAddSelect(pParse, A.pExpr, X);
001162    }
001163    expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E).  [IN] {
001164      A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
001165      sqlite3PExprAddSelect(pParse, A.pExpr, Y);
001166      exprNot(pParse, N, &A);
001167      A.zEnd = &E.z[E.n];
001168    }
001169    expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
001170      SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
001171      Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
001172      if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
001173      A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
001174      sqlite3PExprAddSelect(pParse, A.pExpr, pSelect);
001175      exprNot(pParse, N, &A);
001176      A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
001177    }
001178    expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
001179      Expr *p;
001180      spanSet(&A,&B,&E); /*A-overwrites-B*/
001181      p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
001182      sqlite3PExprAddSelect(pParse, p, Y);
001183    }
001184  %endif SQLITE_OMIT_SUBQUERY
001185  
001186  /* CASE expressions */
001187  expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
001188    spanSet(&A,&C,&E);  /*A-overwrites-C*/
001189    A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0);
001190    if( A.pExpr ){
001191      A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
001192      sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
001193    }else{
001194      sqlite3ExprListDelete(pParse->db, Y);
001195      sqlite3ExprDelete(pParse->db, Z);
001196    }
001197  }
001198  %type case_exprlist {ExprList*}
001199  %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001200  case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
001201    A = sqlite3ExprListAppend(pParse,A, Y.pExpr);
001202    A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
001203  }
001204  case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
001205    A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
001206    A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
001207  }
001208  %type case_else {Expr*}
001209  %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
001210  case_else(A) ::=  ELSE expr(X).         {A = X.pExpr;}
001211  case_else(A) ::=  .                     {A = 0;} 
001212  %type case_operand {Expr*}
001213  %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
001214  case_operand(A) ::= expr(X).            {A = X.pExpr; /*A-overwrites-X*/} 
001215  case_operand(A) ::= .                   {A = 0;} 
001216  
001217  %type exprlist {ExprList*}
001218  %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001219  %type nexprlist {ExprList*}
001220  %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
001221  
001222  exprlist(A) ::= nexprlist(A).
001223  exprlist(A) ::= .                            {A = 0;}
001224  nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
001225      {A = sqlite3ExprListAppend(pParse,A,Y.pExpr);}
001226  nexprlist(A) ::= expr(Y).
001227      {A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/}
001228  
001229  %ifndef SQLITE_OMIT_SUBQUERY
001230  /* A paren_exprlist is an optional expression list contained inside
001231  ** of parenthesis */
001232  %type paren_exprlist {ExprList*}
001233  %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001234  paren_exprlist(A) ::= .   {A = 0;}
001235  paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
001236  %endif SQLITE_OMIT_SUBQUERY
001237  
001238  
001239  ///////////////////////////// The CREATE INDEX command ///////////////////////
001240  //
001241  cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
001242          ON nm(Y) LP sortlist(Z) RP where_opt(W). {
001243    sqlite3CreateIndex(pParse, &X, &D, 
001244                       sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
001245                        &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
001246  }
001247  
001248  %type uniqueflag {int}
001249  uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
001250  uniqueflag(A) ::= .        {A = OE_None;}
001251  
001252  
001253  // The eidlist non-terminal (Expression Id List) generates an ExprList
001254  // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
001255  // This list is stored in an ExprList rather than an IdList so that it
001256  // can be easily sent to sqlite3ColumnsExprList().
001257  //
001258  // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
001259  // used for the arguments to an index.  That is just an historical accident.
001260  //
001261  // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
001262  // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
001263  // places - places that might have been stored in the sqlite_master schema.
001264  // Those extra features were ignored.  But because they might be in some
001265  // (busted) old databases, we need to continue parsing them when loading
001266  // historical schemas.
001267  //
001268  %type eidlist {ExprList*}
001269  %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
001270  %type eidlist_opt {ExprList*}
001271  %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
001272  
001273  %include {
001274    /* Add a single new term to an ExprList that is used to store a
001275    ** list of identifiers.  Report an error if the ID list contains
001276    ** a COLLATE clause or an ASC or DESC keyword, except ignore the
001277    ** error while parsing a legacy schema.
001278    */
001279    static ExprList *parserAddExprIdListTerm(
001280      Parse *pParse,
001281      ExprList *pPrior,
001282      Token *pIdToken,
001283      int hasCollate,
001284      int sortOrder
001285    ){
001286      ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
001287      if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
001288          && pParse->db->init.busy==0
001289      ){
001290        sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
001291                           pIdToken->n, pIdToken->z);
001292      }
001293      sqlite3ExprListSetName(pParse, p, pIdToken, 1);
001294      return p;
001295    }
001296  } // end %include
001297  
001298  eidlist_opt(A) ::= .                         {A = 0;}
001299  eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
001300  eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
001301    A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
001302  }
001303  eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
001304    A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
001305  }
001306  
001307  %type collate {int}
001308  collate(C) ::= .              {C = 0;}
001309  collate(C) ::= COLLATE ids.   {C = 1;}
001310  
001311  
001312  ///////////////////////////// The DROP INDEX command /////////////////////////
001313  //
001314  cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
001315  
001316  ///////////////////////////// The VACUUM command /////////////////////////////
001317  //
001318  %ifndef SQLITE_OMIT_VACUUM
001319  %ifndef SQLITE_OMIT_ATTACH
001320  cmd ::= VACUUM.                {sqlite3Vacuum(pParse,0);}
001321  cmd ::= VACUUM nm(X).          {sqlite3Vacuum(pParse,&X);}
001322  %endif  SQLITE_OMIT_ATTACH
001323  %endif  SQLITE_OMIT_VACUUM
001324  
001325  ///////////////////////////// The PRAGMA command /////////////////////////////
001326  //
001327  %ifndef SQLITE_OMIT_PRAGMA
001328  cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
001329  cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
001330  cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
001331  cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 
001332                                               {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
001333  cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
001334                                               {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
001335  
001336  nmnum(A) ::= plus_num(A).
001337  nmnum(A) ::= nm(A).
001338  nmnum(A) ::= ON(A).
001339  nmnum(A) ::= DELETE(A).
001340  nmnum(A) ::= DEFAULT(A).
001341  %endif SQLITE_OMIT_PRAGMA
001342  %token_class number INTEGER|FLOAT.
001343  plus_num(A) ::= PLUS number(X).       {A = X;}
001344  plus_num(A) ::= number(A).
001345  minus_num(A) ::= MINUS number(X).     {A = X;}
001346  //////////////////////////// The CREATE TRIGGER command /////////////////////
001347  
001348  %ifndef SQLITE_OMIT_TRIGGER
001349  
001350  cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
001351    Token all;
001352    all.z = A.z;
001353    all.n = (int)(Z.z - A.z) + Z.n;
001354    sqlite3FinishTrigger(pParse, S, &all);
001355  }
001356  
001357  trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 
001358                      trigger_time(C) trigger_event(D)
001359                      ON fullname(E) foreach_clause when_clause(G). {
001360    sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
001361    A = (Z.n==0?B:Z); /*A-overwrites-T*/
001362  }
001363  
001364  %type trigger_time {int}
001365  trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
001366  trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
001367  trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
001368  trigger_time(A) ::= .            { A = TK_BEFORE; }
001369  
001370  %type trigger_event {struct TrigEvent}
001371  %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
001372  trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
001373  trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
001374  trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
001375  
001376  foreach_clause ::= .
001377  foreach_clause ::= FOR EACH ROW.
001378  
001379  %type when_clause {Expr*}
001380  %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
001381  when_clause(A) ::= .             { A = 0; }
001382  when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
001383  
001384  %type trigger_cmd_list {TriggerStep*}
001385  %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
001386  trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
001387    assert( A!=0 );
001388    A->pLast->pNext = X;
001389    A->pLast = X;
001390  }
001391  trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 
001392    assert( A!=0 );
001393    A->pLast = A;
001394  }
001395  
001396  // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
001397  // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in 
001398  // the same database as the table that the trigger fires on.
001399  //
001400  %type trnm {Token}
001401  trnm(A) ::= nm(A).
001402  trnm(A) ::= nm DOT nm(X). {
001403    A = X;
001404    sqlite3ErrorMsg(pParse, 
001405          "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
001406          "statements within triggers");
001407  }
001408  
001409  // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
001410  // statements within triggers.  We make a specific error message for this
001411  // since it is an exception to the default grammar rules.
001412  //
001413  tridxby ::= .
001414  tridxby ::= INDEXED BY nm. {
001415    sqlite3ErrorMsg(pParse,
001416          "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
001417          "within triggers");
001418  }
001419  tridxby ::= NOT INDEXED. {
001420    sqlite3ErrorMsg(pParse,
001421          "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
001422          "within triggers");
001423  }
001424  
001425  
001426  
001427  %type trigger_cmd {TriggerStep*}
001428  %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
001429  // UPDATE 
001430  trigger_cmd(A) ::=
001431     UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).  
001432     {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R);}
001433  
001434  // INSERT
001435  trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S).
001436     {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);/*A-overwrites-R*/}
001437  
001438  // DELETE
001439  trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
001440     {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
001441  
001442  // SELECT
001443  trigger_cmd(A) ::= select(X).
001444     {A = sqlite3TriggerSelectStep(pParse->db, X); /*A-overwrites-X*/}
001445  
001446  // The special RAISE expression that may occur in trigger programs
001447  expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
001448    spanSet(&A,&X,&Y);  /*A-overwrites-X*/
001449    A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
001450    if( A.pExpr ){
001451      A.pExpr->affinity = OE_Ignore;
001452    }
001453  }
001454  expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
001455    spanSet(&A,&X,&Y);  /*A-overwrites-X*/
001456    A.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
001457    if( A.pExpr ) {
001458      A.pExpr->affinity = (char)T;
001459    }
001460  }
001461  %endif  !SQLITE_OMIT_TRIGGER
001462  
001463  %type raisetype {int}
001464  raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
001465  raisetype(A) ::= ABORT.     {A = OE_Abort;}
001466  raisetype(A) ::= FAIL.      {A = OE_Fail;}
001467  
001468  
001469  ////////////////////////  DROP TRIGGER statement //////////////////////////////
001470  %ifndef SQLITE_OMIT_TRIGGER
001471  cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
001472    sqlite3DropTrigger(pParse,X,NOERR);
001473  }
001474  %endif  !SQLITE_OMIT_TRIGGER
001475  
001476  //////////////////////// ATTACH DATABASE file AS name /////////////////////////
001477  %ifndef SQLITE_OMIT_ATTACH
001478  cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
001479    sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
001480  }
001481  cmd ::= DETACH database_kw_opt expr(D). {
001482    sqlite3Detach(pParse, D.pExpr);
001483  }
001484  
001485  %type key_opt {Expr*}
001486  %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
001487  key_opt(A) ::= .                     { A = 0; }
001488  key_opt(A) ::= KEY expr(X).          { A = X.pExpr; }
001489  
001490  database_kw_opt ::= DATABASE.
001491  database_kw_opt ::= .
001492  %endif SQLITE_OMIT_ATTACH
001493  
001494  ////////////////////////// REINDEX collation //////////////////////////////////
001495  %ifndef SQLITE_OMIT_REINDEX
001496  cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
001497  cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
001498  %endif  SQLITE_OMIT_REINDEX
001499  
001500  /////////////////////////////////// ANALYZE ///////////////////////////////////
001501  %ifndef SQLITE_OMIT_ANALYZE
001502  cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
001503  cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
001504  %endif
001505  
001506  //////////////////////// ALTER TABLE table ... ////////////////////////////////
001507  %ifndef SQLITE_OMIT_ALTERTABLE
001508  cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
001509    sqlite3AlterRenameTable(pParse,X,&Z);
001510  }
001511  cmd ::= ALTER TABLE add_column_fullname
001512          ADD kwcolumn_opt columnname(Y) carglist. {
001513    Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
001514    sqlite3AlterFinishAddColumn(pParse, &Y);
001515  }
001516  add_column_fullname ::= fullname(X). {
001517    disableLookaside(pParse);
001518    sqlite3AlterBeginAddColumn(pParse, X);
001519  }
001520  kwcolumn_opt ::= .
001521  kwcolumn_opt ::= COLUMNKW.
001522  %endif  SQLITE_OMIT_ALTERTABLE
001523  
001524  //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
001525  %ifndef SQLITE_OMIT_VIRTUALTABLE
001526  cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
001527  cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
001528  create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
001529                  nm(X) dbnm(Y) USING nm(Z). {
001530      sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
001531  }
001532  vtabarglist ::= vtabarg.
001533  vtabarglist ::= vtabarglist COMMA vtabarg.
001534  vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
001535  vtabarg ::= vtabarg vtabargtoken.
001536  vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
001537  vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
001538  lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
001539  anylist ::= .
001540  anylist ::= anylist LP anylist RP.
001541  anylist ::= anylist ANY.
001542  %endif  SQLITE_OMIT_VIRTUALTABLE
001543  
001544  
001545  //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
001546  %type with {With*}
001547  %type wqlist {With*}
001548  %destructor with {sqlite3WithDelete(pParse->db, $$);}
001549  %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
001550  
001551  with(A) ::= . {A = 0;}
001552  %ifndef SQLITE_OMIT_CTE
001553  with(A) ::= WITH wqlist(W).              { A = W; }
001554  with(A) ::= WITH RECURSIVE wqlist(W).    { A = W; }
001555  
001556  wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
001557    A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
001558  }
001559  wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
001560    A = sqlite3WithAdd(pParse, A, &X, Y, Z);
001561  }
001562  %endif  SQLITE_OMIT_CTE