webengine/webkitutils/SqliteSymbian/parse.y
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 ** 2001 September 15
       
     3 **
       
     4 ** The author disclaims copyright to this source code.  In place of
       
     5 ** a legal notice, here is a blessing:
       
     6 **
       
     7 **    May you do good and not evil.
       
     8 **    May you find forgiveness for yourself and forgive others.
       
     9 **    May you share freely, never taking more than you give.
       
    10 **
       
    11 *************************************************************************
       
    12 ** This file contains SQLite's grammar for SQL.  Process this file
       
    13 ** using the lemon parser generator to generate C code that runs
       
    14 ** the parser.  Lemon will also generate a header file containing
       
    15 ** numeric codes for all of the tokens.
       
    16 **
       
    17 ** @(#) $Id: parse.y,v 1.204 2006/06/16 08:01:04 danielk1977 Exp $
       
    18 */
       
    19 
       
    20 // All token codes are small integers with #defines that begin with "TK_"
       
    21 %token_prefix TK_
       
    22 
       
    23 // The type of the data attached to each token is Token.  This is also the
       
    24 // default type for non-terminals.
       
    25 //
       
    26 %token_type {Token}
       
    27 %default_type {Token}
       
    28 
       
    29 // The generated parser function takes a 4th argument as follows:
       
    30 %extra_argument {Parse *pParse}
       
    31 
       
    32 // This code runs whenever there is a syntax error
       
    33 //
       
    34 %syntax_error {
       
    35   if( !pParse->parseError ){
       
    36     if( TOKEN.z[0] ){
       
    37       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
       
    38     }else{
       
    39       sqlite3ErrorMsg(pParse, "incomplete SQL statement");
       
    40     }
       
    41     pParse->parseError = 1;
       
    42   }
       
    43 }
       
    44 %stack_overflow {
       
    45   sqlite3ErrorMsg(pParse, "parser stack overflow");
       
    46   pParse->parseError = 1;
       
    47 }
       
    48 
       
    49 // The name of the generated procedure that implements the parser
       
    50 // is as follows:
       
    51 %name sqlite3Parser
       
    52 
       
    53 // The following text is included near the beginning of the C source
       
    54 // code file that implements the parser.
       
    55 //
       
    56 %include {
       
    57 #include "sqliteInt.h"
       
    58 #include "parse.h"
       
    59 
       
    60 /*
       
    61 ** An instance of this structure holds information about the
       
    62 ** LIMIT clause of a SELECT statement.
       
    63 */
       
    64 struct LimitVal {
       
    65   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
       
    66   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
       
    67 };
       
    68 
       
    69 /*
       
    70 ** An instance of this structure is used to store the LIKE,
       
    71 ** GLOB, NOT LIKE, and NOT GLOB operators.
       
    72 */
       
    73 struct LikeOp {
       
    74   Token eOperator;  /* "like" or "glob" or "regexp" */
       
    75   int not;         /* True if the NOT keyword is present */
       
    76 };
       
    77 
       
    78 /*
       
    79 ** An instance of the following structure describes the event of a
       
    80 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
       
    81 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
       
    82 **
       
    83 **      UPDATE ON (a,b,c)
       
    84 **
       
    85 ** Then the "b" IdList records the list "a,b,c".
       
    86 */
       
    87 struct TrigEvent { int a; IdList * b; };
       
    88 
       
    89 /*
       
    90 ** An instance of this structure holds the ATTACH key and the key type.
       
    91 */
       
    92 struct AttachKey { int type;  Token key; };
       
    93 
       
    94 } // end %include
       
    95 
       
    96 // Input is a single SQL command
       
    97 input ::= cmdlist.
       
    98 cmdlist ::= cmdlist ecmd.
       
    99 cmdlist ::= ecmd.
       
   100 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
       
   101 ecmd ::= SEMI.
       
   102 ecmd ::= explain cmdx SEMI.
       
   103 explain ::= .           { sqlite3BeginParse(pParse, 0); }
       
   104 %ifndef SQLITE_OMIT_EXPLAIN
       
   105 explain ::= EXPLAIN.              { sqlite3BeginParse(pParse, 1); }
       
   106 explain ::= EXPLAIN QUERY PLAN.   { sqlite3BeginParse(pParse, 2); }
       
   107 %endif
       
   108 
       
   109 ///////////////////// Begin and end transactions. ////////////////////////////
       
   110 //
       
   111 
       
   112 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
       
   113 trans_opt ::= .
       
   114 trans_opt ::= TRANSACTION.
       
   115 trans_opt ::= TRANSACTION nm.
       
   116 %type transtype {int}
       
   117 transtype(A) ::= .             {A = TK_DEFERRED;}
       
   118 transtype(A) ::= DEFERRED(X).  {A = @X;}
       
   119 transtype(A) ::= IMMEDIATE(X). {A = @X;}
       
   120 transtype(A) ::= EXCLUSIVE(X). {A = @X;}
       
   121 cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
       
   122 cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
       
   123 cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
       
   124 
       
   125 ///////////////////// The CREATE TABLE statement ////////////////////////////
       
   126 //
       
   127 cmd ::= create_table create_table_args.
       
   128 create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
       
   129    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
       
   130 }
       
   131 %type ifnotexists {int}
       
   132 ifnotexists(A) ::= .              {A = 0;}
       
   133 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
       
   134 %type temp {int}
       
   135 %ifndef SQLITE_OMIT_TEMPDB
       
   136 temp(A) ::= TEMP.  {A = 1;}
       
   137 %endif
       
   138 temp(A) ::= .      {A = 0;}
       
   139 create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
       
   140   sqlite3EndTable(pParse,&X,&Y,0);
       
   141 }
       
   142 create_table_args ::= AS select(S). {
       
   143   sqlite3EndTable(pParse,0,0,S);
       
   144   sqlite3SelectDelete(S);
       
   145 }
       
   146 columnlist ::= columnlist COMMA column.
       
   147 columnlist ::= column.
       
   148 
       
   149 // A "column" is a complete description of a single column in a
       
   150 // CREATE TABLE statement.  This includes the column name, its
       
   151 // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
       
   152 // NOT NULL and so forth.
       
   153 //
       
   154 column(A) ::= columnid(X) type carglist. {
       
   155   A.z = X.z;
       
   156   A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
       
   157 }
       
   158 columnid(A) ::= nm(X). {
       
   159   sqlite3AddColumn(pParse,&X);
       
   160   A = X;
       
   161 }
       
   162 
       
   163 
       
   164 // An IDENTIFIER can be a generic identifier, or one of several
       
   165 // keywords.  Any non-standard keyword can also be an identifier.
       
   166 //
       
   167 %type id {Token}
       
   168 id(A) ::= ID(X).         {A = X;}
       
   169 
       
   170 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
       
   171 // fallback to ID if they will not parse as their original value.
       
   172 // This obviates the need for the "id" nonterminal.
       
   173 //
       
   174 %fallback ID
       
   175   ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT
       
   176   DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
       
   177   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN QUERY KEY
       
   178   OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
       
   179   TEMP TRIGGER VACUUM VIEW VIRTUAL
       
   180 %ifdef SQLITE_OMIT_COMPOUND_SELECT
       
   181   EXCEPT INTERSECT UNION
       
   182 %endif
       
   183   REINDEX RENAME CTIME_KW IF
       
   184   .
       
   185 %wildcard ANY.
       
   186 
       
   187 // Define operator precedence early so that this is the first occurance
       
   188 // of the operator tokens in the grammer.  Keeping the operators together
       
   189 // causes them to be assigned integer values that are close together,
       
   190 // which keeps parser tables smaller.
       
   191 //
       
   192 // The token values assigned to these symbols is determined by the order
       
   193 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
       
   194 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
       
   195 // the sqlite3ExprIfFalse() routine for additional information on this
       
   196 // constraint.
       
   197 //
       
   198 %left OR.
       
   199 %left AND.
       
   200 %right NOT.
       
   201 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
       
   202 %left GT LE LT GE.
       
   203 %right ESCAPE.
       
   204 %left BITAND BITOR LSHIFT RSHIFT.
       
   205 %left PLUS MINUS.
       
   206 %left STAR SLASH REM.
       
   207 %left CONCAT.
       
   208 %right UMINUS UPLUS BITNOT.
       
   209 
       
   210 // And "ids" is an identifer-or-string.
       
   211 //
       
   212 %type ids {Token}
       
   213 ids(A) ::= ID|STRING(X).   {A = X;}
       
   214 
       
   215 // The name of a column or table can be any of the following:
       
   216 //
       
   217 %type nm {Token}
       
   218 nm(A) ::= ID(X).         {A = X;}
       
   219 nm(A) ::= STRING(X).     {A = X;}
       
   220 nm(A) ::= JOIN_KW(X).    {A = X;}
       
   221 
       
   222 // A typetoken is really one or more tokens that form a type name such
       
   223 // as can be found after the column name in a CREATE TABLE statement.
       
   224 // Multiple tokens are concatenated to form the value of the typetoken.
       
   225 //
       
   226 %type typetoken {Token}
       
   227 type ::= .
       
   228 type ::= typetoken(X).                   {sqlite3AddColumnType(pParse,&X);}
       
   229 typetoken(A) ::= typename(X).   {A = X;}
       
   230 typetoken(A) ::= typename(X) LP signed RP(Y). {
       
   231   A.z = X.z;
       
   232   A.n = &Y.z[Y.n] - X.z;
       
   233 }
       
   234 typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
       
   235   A.z = X.z;
       
   236   A.n = &Y.z[Y.n] - X.z;
       
   237 }
       
   238 %type typename {Token}
       
   239 typename(A) ::= ids(X).             {A = X;}
       
   240 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
       
   241 %type signed {int}
       
   242 signed(A) ::= plus_num(X).    { A = atoi((char*)X.z); }
       
   243 signed(A) ::= minus_num(X).   { A = -atoi((char*)X.z); }
       
   244 
       
   245 // "carglist" is a list of additional constraints that come after the
       
   246 // column name and column type in a CREATE TABLE statement.
       
   247 //
       
   248 carglist ::= carglist carg.
       
   249 carglist ::= .
       
   250 carg ::= CONSTRAINT nm ccons.
       
   251 carg ::= ccons.
       
   252 carg ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,X);}
       
   253 carg ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,X);}
       
   254 carg ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,X);}
       
   255 carg ::= DEFAULT MINUS term(X).      {
       
   256   Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0);
       
   257   sqlite3AddDefaultValue(pParse,p);
       
   258 }
       
   259 carg ::= DEFAULT id(X).              {
       
   260   Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X);
       
   261   sqlite3AddDefaultValue(pParse,p);
       
   262 }
       
   263 
       
   264 // In addition to the type name, we also care about the primary key and
       
   265 // UNIQUE constraints.
       
   266 //
       
   267 ccons ::= NULL onconf.
       
   268 ccons ::= NOT NULL onconf(R).               {sqlite3AddNotNull(pParse, R);}
       
   269 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
       
   270                                      {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
       
   271 ccons ::= UNIQUE onconf(R).    {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
       
   272 ccons ::= CHECK LP expr(X) RP.       {sqlite3AddCheckConstraint(pParse,X);}
       
   273 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
       
   274                                 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
       
   275 ccons ::= defer_subclause(D).   {sqlite3DeferForeignKey(pParse,D);}
       
   276 ccons ::= COLLATE id(C).  {sqlite3AddCollateType(pParse, (char*)C.z, C.n);}
       
   277 
       
   278 // The optional AUTOINCREMENT keyword
       
   279 %type autoinc {int}
       
   280 autoinc(X) ::= .          {X = 0;}
       
   281 autoinc(X) ::= AUTOINCR.  {X = 1;}
       
   282 
       
   283 // The next group of rules parses the arguments to a REFERENCES clause
       
   284 // that determine if the referential integrity checking is deferred or
       
   285 // or immediate and which determine what action to take if a ref-integ
       
   286 // check fails.
       
   287 //
       
   288 %type refargs {int}
       
   289 refargs(A) ::= .                     { A = OE_Restrict * 0x010101; }
       
   290 refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
       
   291 %type refarg {struct {int value; int mask;}}
       
   292 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
       
   293 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
       
   294 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
       
   295 refarg(A) ::= ON INSERT refact(X).   { A.value = X<<16; A.mask = 0xff0000; }
       
   296 %type refact {int}
       
   297 refact(A) ::= SET NULL.              { A = OE_SetNull; }
       
   298 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt; }
       
   299 refact(A) ::= CASCADE.               { A = OE_Cascade; }
       
   300 refact(A) ::= RESTRICT.              { A = OE_Restrict; }
       
   301 %type defer_subclause {int}
       
   302 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X).  {A = X;}
       
   303 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
       
   304 %type init_deferred_pred_opt {int}
       
   305 init_deferred_pred_opt(A) ::= .                       {A = 0;}
       
   306 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
       
   307 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
       
   308 
       
   309 // For the time being, the only constraint we care about is the primary
       
   310 // key and UNIQUE.  Both create indices.
       
   311 //
       
   312 conslist_opt(A) ::= .                   {A.n = 0; A.z = 0;}
       
   313 conslist_opt(A) ::= COMMA(X) conslist.  {A = X;}
       
   314 conslist ::= conslist COMMA tcons.
       
   315 conslist ::= conslist tcons.
       
   316 conslist ::= tcons.
       
   317 tcons ::= CONSTRAINT nm.
       
   318 tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
       
   319                                          {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
       
   320 tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
       
   321                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
       
   322 tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
       
   323 tcons ::= FOREIGN KEY LP idxlist(FA) RP
       
   324           REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
       
   325     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
       
   326     sqlite3DeferForeignKey(pParse, D);
       
   327 }
       
   328 %type defer_subclause_opt {int}
       
   329 defer_subclause_opt(A) ::= .                    {A = 0;}
       
   330 defer_subclause_opt(A) ::= defer_subclause(X).  {A = X;}
       
   331 
       
   332 // The following is a non-standard extension that allows us to declare the
       
   333 // default behavior when there is a constraint conflict.
       
   334 //
       
   335 %type onconf {int}
       
   336 %type orconf {int}
       
   337 %type resolvetype {int}
       
   338 onconf(A) ::= .                              {A = OE_Default;}
       
   339 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
       
   340 orconf(A) ::= .                              {A = OE_Default;}
       
   341 orconf(A) ::= OR resolvetype(X).             {A = X;}
       
   342 resolvetype(A) ::= raisetype(X).             {A = X;}
       
   343 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
       
   344 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
       
   345 
       
   346 ////////////////////////// The DROP TABLE /////////////////////////////////////
       
   347 //
       
   348 cmd ::= DROP TABLE ifexists(E) fullname(X). {
       
   349   sqlite3DropTable(pParse, X, 0, E);
       
   350 }
       
   351 %type ifexists {int}
       
   352 ifexists(A) ::= IF EXISTS.   {A = 1;}
       
   353 ifexists(A) ::= .            {A = 0;}
       
   354 
       
   355 ///////////////////// The CREATE VIEW statement /////////////////////////////
       
   356 //
       
   357 %ifndef SQLITE_OMIT_VIEW
       
   358 cmd ::= CREATE(X) temp(T) VIEW nm(Y) dbnm(Z) AS select(S). {
       
   359   sqlite3CreateView(pParse, &X, &Y, &Z, S, T);
       
   360 }
       
   361 cmd ::= DROP VIEW ifexists(E) fullname(X). {
       
   362   sqlite3DropTable(pParse, X, 1, E);
       
   363 }
       
   364 %endif // SQLITE_OMIT_VIEW
       
   365 
       
   366 //////////////////////// The SELECT statement /////////////////////////////////
       
   367 //
       
   368 cmd ::= select(X).  {
       
   369   sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0);
       
   370   sqlite3SelectDelete(X);
       
   371 }
       
   372 
       
   373 %type select {Select*}
       
   374 %destructor select {sqlite3SelectDelete($$);}
       
   375 %type oneselect {Select*}
       
   376 %destructor oneselect {sqlite3SelectDelete($$);}
       
   377 
       
   378 select(A) ::= oneselect(X).                      {A = X;}
       
   379 %ifndef SQLITE_OMIT_COMPOUND_SELECT
       
   380 select(A) ::= select(X) multiselect_op(Y) oneselect(Z).  {
       
   381   if( Z ){
       
   382     Z->op = Y;
       
   383     Z->pPrior = X;
       
   384   }
       
   385   A = Z;
       
   386 }
       
   387 %type multiselect_op {int}
       
   388 multiselect_op(A) ::= UNION(OP).             {A = @OP;}
       
   389 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
       
   390 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
       
   391 %endif // SQLITE_OMIT_COMPOUND_SELECT
       
   392 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
       
   393                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
       
   394   A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
       
   395 }
       
   396 
       
   397 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
       
   398 // present and false (0) if it is not.
       
   399 //
       
   400 %type distinct {int}
       
   401 distinct(A) ::= DISTINCT.   {A = 1;}
       
   402 distinct(A) ::= ALL.        {A = 0;}
       
   403 distinct(A) ::= .           {A = 0;}
       
   404 
       
   405 // selcollist is a list of expressions that are to become the return
       
   406 // values of the SELECT statement.  The "*" in statements like
       
   407 // "SELECT * FROM ..." is encoded as a special expression with an
       
   408 // opcode of TK_ALL.
       
   409 //
       
   410 %type selcollist {ExprList*}
       
   411 %destructor selcollist {sqlite3ExprListDelete($$);}
       
   412 %type sclp {ExprList*}
       
   413 %destructor sclp {sqlite3ExprListDelete($$);}
       
   414 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
       
   415 sclp(A) ::= .                                {A = 0;}
       
   416 selcollist(A) ::= sclp(P) expr(X) as(Y).     {
       
   417    A = sqlite3ExprListAppend(P,X,Y.n?&Y:0);
       
   418 }
       
   419 selcollist(A) ::= sclp(P) STAR. {
       
   420   A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
       
   421 }
       
   422 selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
       
   423   Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
       
   424   Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X);
       
   425   A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
       
   426 }
       
   427 
       
   428 // An option "AS <id>" phrase that can follow one of the expressions that
       
   429 // define the result set, or one of the tables in the FROM clause.
       
   430 //
       
   431 %type as {Token}
       
   432 as(X) ::= AS nm(Y).    {X = Y;}
       
   433 as(X) ::= ids(Y).      {X = Y;}
       
   434 as(X) ::= .            {X.n = 0;}
       
   435 
       
   436 
       
   437 %type seltablist {SrcList*}
       
   438 %destructor seltablist {sqlite3SrcListDelete($$);}
       
   439 %type stl_prefix {SrcList*}
       
   440 %destructor stl_prefix {sqlite3SrcListDelete($$);}
       
   441 %type from {SrcList*}
       
   442 %destructor from {sqlite3SrcListDelete($$);}
       
   443 
       
   444 // A complete FROM clause.
       
   445 //
       
   446 from(A) ::= .                                 {A = sqliteMalloc(sizeof(*A));}
       
   447 from(A) ::= FROM seltablist(X).               {A = X;}
       
   448 
       
   449 // "seltablist" is a "Select Table List" - the content of the FROM clause
       
   450 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
       
   451 //
       
   452 stl_prefix(A) ::= seltablist(X) joinop(Y).    {
       
   453    A = X;
       
   454    if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
       
   455 }
       
   456 stl_prefix(A) ::= .                           {A = 0;}
       
   457 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
       
   458   A = sqlite3SrcListAppend(X,&Y,&D);
       
   459   if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
       
   460   if( N ){
       
   461     if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
       
   462     else { sqlite3ExprDelete(N); }
       
   463   }
       
   464   if( U ){
       
   465     if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
       
   466     else { sqlite3IdListDelete(U); }
       
   467   }
       
   468 }
       
   469 %ifndef SQLITE_OMIT_SUBQUERY
       
   470   seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
       
   471                     as(Z) on_opt(N) using_opt(U). {
       
   472     A = sqlite3SrcListAppend(X,0,0);
       
   473     A->a[A->nSrc-1].pSelect = S;
       
   474     if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
       
   475     if( N ){
       
   476       if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
       
   477       else { sqlite3ExprDelete(N); }
       
   478     }
       
   479     if( U ){
       
   480       if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
       
   481       else { sqlite3IdListDelete(U); }
       
   482     }
       
   483   }
       
   484   
       
   485   // A seltablist_paren nonterminal represents anything in a FROM that
       
   486   // is contained inside parentheses.  This can be either a subquery or
       
   487   // a grouping of table and subqueries.
       
   488   //
       
   489   %type seltablist_paren {Select*}
       
   490   %destructor seltablist_paren {sqlite3SelectDelete($$);}
       
   491   seltablist_paren(A) ::= select(S).      {A = S;}
       
   492   seltablist_paren(A) ::= seltablist(F).  {
       
   493      A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0);
       
   494   }
       
   495 %endif // SQLITE_OMIT_SUBQUERY
       
   496 
       
   497 %type dbnm {Token}
       
   498 dbnm(A) ::= .          {A.z=0; A.n=0;}
       
   499 dbnm(A) ::= DOT nm(X). {A = X;}
       
   500 
       
   501 %type fullname {SrcList*}
       
   502 %destructor fullname {sqlite3SrcListDelete($$);}
       
   503 fullname(A) ::= nm(X) dbnm(Y).  {A = sqlite3SrcListAppend(0,&X,&Y);}
       
   504 
       
   505 %type joinop {int}
       
   506 %type joinop2 {int}
       
   507 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
       
   508 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
       
   509 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
       
   510 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
       
   511                                        { X = sqlite3JoinType(pParse,&A,&B,&C); }
       
   512 
       
   513 %type on_opt {Expr*}
       
   514 %destructor on_opt {sqlite3ExprDelete($$);}
       
   515 on_opt(N) ::= ON expr(E).   {N = E;}
       
   516 on_opt(N) ::= .             {N = 0;}
       
   517 
       
   518 %type using_opt {IdList*}
       
   519 %destructor using_opt {sqlite3IdListDelete($$);}
       
   520 using_opt(U) ::= USING LP inscollist(L) RP.  {U = L;}
       
   521 using_opt(U) ::= .                        {U = 0;}
       
   522 
       
   523 
       
   524 %type orderby_opt {ExprList*}
       
   525 %destructor orderby_opt {sqlite3ExprListDelete($$);}
       
   526 %type sortlist {ExprList*}
       
   527 %destructor sortlist {sqlite3ExprListDelete($$);}
       
   528 %type sortitem {Expr*}
       
   529 %destructor sortitem {sqlite3ExprDelete($$);}
       
   530 
       
   531 orderby_opt(A) ::= .                          {A = 0;}
       
   532 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
       
   533 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
       
   534   A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0);
       
   535   if( A ) A->a[A->nExpr-1].sortOrder = Z;
       
   536 }
       
   537 sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
       
   538   A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0);
       
   539   if( A && A->a ) A->a[0].sortOrder = Z;
       
   540 }
       
   541 sortitem(A) ::= expr(X).   {A = X;}
       
   542 
       
   543 %type sortorder {int}
       
   544 %type collate {Token}
       
   545 
       
   546 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
       
   547 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
       
   548 sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
       
   549 collate(C) ::= .                {C.z = 0; C.n = 0;}
       
   550 collate(C) ::= COLLATE id(X).   {C = X;}
       
   551 
       
   552 %type groupby_opt {ExprList*}
       
   553 %destructor groupby_opt {sqlite3ExprListDelete($$);}
       
   554 groupby_opt(A) ::= .                      {A = 0;}
       
   555 groupby_opt(A) ::= GROUP BY exprlist(X).  {A = X;}
       
   556 
       
   557 %type having_opt {Expr*}
       
   558 %destructor having_opt {sqlite3ExprDelete($$);}
       
   559 having_opt(A) ::= .                {A = 0;}
       
   560 having_opt(A) ::= HAVING expr(X).  {A = X;}
       
   561 
       
   562 %type limit_opt {struct LimitVal}
       
   563 %destructor limit_opt {
       
   564   sqlite3ExprDelete($$.pLimit);
       
   565   sqlite3ExprDelete($$.pOffset);
       
   566 }
       
   567 limit_opt(A) ::= .                     {A.pLimit = 0; A.pOffset = 0;}
       
   568 limit_opt(A) ::= LIMIT expr(X).        {A.pLimit = X; A.pOffset = 0;}
       
   569 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 
       
   570                                        {A.pLimit = X; A.pOffset = Y;}
       
   571 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 
       
   572                                        {A.pOffset = X; A.pLimit = Y;}
       
   573 
       
   574 /////////////////////////// The DELETE statement /////////////////////////////
       
   575 //
       
   576 cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
       
   577 
       
   578 %type where_opt {Expr*}
       
   579 %destructor where_opt {sqlite3ExprDelete($$);}
       
   580 
       
   581 where_opt(A) ::= .                    {A = 0;}
       
   582 where_opt(A) ::= WHERE expr(X).       {A = X;}
       
   583 
       
   584 ////////////////////////// The UPDATE command ////////////////////////////////
       
   585 //
       
   586 cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).
       
   587     {sqlite3Update(pParse,X,Y,Z,R);}
       
   588 
       
   589 %type setlist {ExprList*}
       
   590 %destructor setlist {sqlite3ExprListDelete($$);}
       
   591 
       
   592 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
       
   593     {A = sqlite3ExprListAppend(Z,Y,&X);}
       
   594 setlist(A) ::= nm(X) EQ expr(Y).   {A = sqlite3ExprListAppend(0,Y,&X);}
       
   595 
       
   596 ////////////////////////// The INSERT command /////////////////////////////////
       
   597 //
       
   598 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) 
       
   599         VALUES LP itemlist(Y) RP.
       
   600             {sqlite3Insert(pParse, X, Y, 0, F, R);}
       
   601 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
       
   602             {sqlite3Insert(pParse, X, 0, S, F, R);}
       
   603 
       
   604 %type insert_cmd {int}
       
   605 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
       
   606 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
       
   607 
       
   608 
       
   609 %type itemlist {ExprList*}
       
   610 %destructor itemlist {sqlite3ExprListDelete($$);}
       
   611 
       
   612 itemlist(A) ::= itemlist(X) COMMA expr(Y).  {A = sqlite3ExprListAppend(X,Y,0);}
       
   613 itemlist(A) ::= expr(X).                    {A = sqlite3ExprListAppend(0,X,0);}
       
   614 
       
   615 %type inscollist_opt {IdList*}
       
   616 %destructor inscollist_opt {sqlite3IdListDelete($$);}
       
   617 %type inscollist {IdList*}
       
   618 %destructor inscollist {sqlite3IdListDelete($$);}
       
   619 
       
   620 inscollist_opt(A) ::= .                       {A = 0;}
       
   621 inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
       
   622 inscollist(A) ::= inscollist(X) COMMA nm(Y).  {A = sqlite3IdListAppend(X,&Y);}
       
   623 inscollist(A) ::= nm(Y).                      {A = sqlite3IdListAppend(0,&Y);}
       
   624 
       
   625 /////////////////////////// Expression Processing /////////////////////////////
       
   626 //
       
   627 
       
   628 %type expr {Expr*}
       
   629 %destructor expr {sqlite3ExprDelete($$);}
       
   630 %type term {Expr*}
       
   631 %destructor term {sqlite3ExprDelete($$);}
       
   632 
       
   633 expr(A) ::= term(X).             {A = X;}
       
   634 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
       
   635 term(A) ::= NULL(X).             {A = sqlite3Expr(@X, 0, 0, &X);}
       
   636 expr(A) ::= ID(X).               {A = sqlite3Expr(TK_ID, 0, 0, &X);}
       
   637 expr(A) ::= JOIN_KW(X).          {A = sqlite3Expr(TK_ID, 0, 0, &X);}
       
   638 expr(A) ::= nm(X) DOT nm(Y). {
       
   639   Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
       
   640   Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
       
   641   A = sqlite3Expr(TK_DOT, temp1, temp2, 0);
       
   642 }
       
   643 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
       
   644   Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
       
   645   Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
       
   646   Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
       
   647   Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
       
   648   A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
       
   649 }
       
   650 term(A) ::= INTEGER|FLOAT|BLOB(X).      {A = sqlite3Expr(@X, 0, 0, &X);}
       
   651 term(A) ::= STRING(X).       {A = sqlite3Expr(@X, 0, 0, &X);}
       
   652 expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
       
   653 expr(A) ::= VARIABLE(X).     {
       
   654   Token *pToken = &X;
       
   655   Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
       
   656   sqlite3ExprAssignVarNumber(pParse, pExpr);
       
   657 }
       
   658 %ifndef SQLITE_OMIT_CAST
       
   659 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
       
   660   A = sqlite3Expr(TK_CAST, E, 0, &T);
       
   661   sqlite3ExprSpan(A,&X,&Y);
       
   662 }
       
   663 %endif // SQLITE_OMIT_CAST
       
   664 expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
       
   665   A = sqlite3ExprFunction(Y, &X);
       
   666   sqlite3ExprSpan(A,&X,&E);
       
   667   if( D && A ){
       
   668     A->flags |= EP_Distinct;
       
   669   }
       
   670 }
       
   671 expr(A) ::= ID(X) LP STAR RP(E). {
       
   672   A = sqlite3ExprFunction(0, &X);
       
   673   sqlite3ExprSpan(A,&X,&E);
       
   674 }
       
   675 term(A) ::= CTIME_KW(OP). {
       
   676   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
       
   677   ** treated as functions that return constants */
       
   678   A = sqlite3ExprFunction(0,&OP);
       
   679   if( A ) A->op = TK_CONST_FUNC;  
       
   680 }
       
   681 expr(A) ::= expr(X) AND(OP) expr(Y).            {A = sqlite3Expr(@OP, X, Y, 0);}
       
   682 expr(A) ::= expr(X) OR(OP) expr(Y).             {A = sqlite3Expr(@OP, X, Y, 0);}
       
   683 expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).    {A = sqlite3Expr(@OP, X, Y, 0);}
       
   684 expr(A) ::= expr(X) EQ|NE(OP) expr(Y).          {A = sqlite3Expr(@OP, X, Y, 0);}
       
   685 expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
       
   686                                                 {A = sqlite3Expr(@OP, X, Y, 0);}
       
   687 expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).     {A = sqlite3Expr(@OP, X, Y, 0);}
       
   688 expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
       
   689 expr(A) ::= expr(X) CONCAT(OP) expr(Y).         {A = sqlite3Expr(@OP, X, Y, 0);}
       
   690 %type likeop {struct LikeOp}
       
   691 likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.not = 0;}
       
   692 likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
       
   693 likeop(A) ::= MATCH(X).       {A.eOperator = X; A.not = 0;}
       
   694 likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.not = 1;}
       
   695 %type escape {Expr*}
       
   696 %destructor escape {sqlite3ExprDelete($$);}
       
   697 escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
       
   698 escape(X) ::= .               [ESCAPE] {X = 0;}
       
   699 expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E).  [LIKE_KW]  {
       
   700   ExprList *pList;
       
   701   pList = sqlite3ExprListAppend(0, Y, 0);
       
   702   pList = sqlite3ExprListAppend(pList, X, 0);
       
   703   if( E ){
       
   704     pList = sqlite3ExprListAppend(pList, E, 0);
       
   705   }
       
   706   A = sqlite3ExprFunction(pList, &OP.eOperator);
       
   707   if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0);
       
   708   sqlite3ExprSpan(A, &X->span, &Y->span);
       
   709 }
       
   710 
       
   711 expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
       
   712   A = sqlite3Expr(@E, X, 0, 0);
       
   713   sqlite3ExprSpan(A,&X->span,&E);
       
   714 }
       
   715 expr(A) ::= expr(X) IS NULL(E). {
       
   716   A = sqlite3Expr(TK_ISNULL, X, 0, 0);
       
   717   sqlite3ExprSpan(A,&X->span,&E);
       
   718 }
       
   719 expr(A) ::= expr(X) NOT NULL(E). {
       
   720   A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
       
   721   sqlite3ExprSpan(A,&X->span,&E);
       
   722 }
       
   723 expr(A) ::= expr(X) IS NOT NULL(E). {
       
   724   A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
       
   725   sqlite3ExprSpan(A,&X->span,&E);
       
   726 }
       
   727 expr(A) ::= NOT|BITNOT(B) expr(X). {
       
   728   A = sqlite3Expr(@B, X, 0, 0);
       
   729   sqlite3ExprSpan(A,&B,&X->span);
       
   730 }
       
   731 expr(A) ::= MINUS(B) expr(X). [UMINUS] {
       
   732   A = sqlite3Expr(TK_UMINUS, X, 0, 0);
       
   733   sqlite3ExprSpan(A,&B,&X->span);
       
   734 }
       
   735 expr(A) ::= PLUS(B) expr(X). [UPLUS] {
       
   736   A = sqlite3Expr(TK_UPLUS, X, 0, 0);
       
   737   sqlite3ExprSpan(A,&B,&X->span);
       
   738 }
       
   739 %type between_op {int}
       
   740 between_op(A) ::= BETWEEN.     {A = 0;}
       
   741 between_op(A) ::= NOT BETWEEN. {A = 1;}
       
   742 expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
       
   743   ExprList *pList = sqlite3ExprListAppend(0, X, 0);
       
   744   pList = sqlite3ExprListAppend(pList, Y, 0);
       
   745   A = sqlite3Expr(TK_BETWEEN, W, 0, 0);
       
   746   if( A ){
       
   747     A->pList = pList;
       
   748   }else{
       
   749     sqlite3ExprListDelete(pList);
       
   750   } 
       
   751   if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
       
   752   sqlite3ExprSpan(A,&W->span,&Y->span);
       
   753 }
       
   754 %ifndef SQLITE_OMIT_SUBQUERY
       
   755   %type in_op {int}
       
   756   in_op(A) ::= IN.      {A = 0;}
       
   757   in_op(A) ::= NOT IN.  {A = 1;}
       
   758   expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
       
   759     A = sqlite3Expr(TK_IN, X, 0, 0);
       
   760     if( A ){
       
   761       A->pList = Y;
       
   762     }else{
       
   763       sqlite3ExprListDelete(Y);
       
   764     }
       
   765     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
       
   766     sqlite3ExprSpan(A,&X->span,&E);
       
   767   }
       
   768   expr(A) ::= LP(B) select(X) RP(E). {
       
   769     A = sqlite3Expr(TK_SELECT, 0, 0, 0);
       
   770     if( A ){
       
   771       A->pSelect = X;
       
   772     }else{
       
   773       sqlite3SelectDelete(X);
       
   774     }
       
   775     sqlite3ExprSpan(A,&B,&E);
       
   776   }
       
   777   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
       
   778     A = sqlite3Expr(TK_IN, X, 0, 0);
       
   779     if( A ){
       
   780       A->pSelect = Y;
       
   781     }else{
       
   782       sqlite3SelectDelete(Y);
       
   783     }
       
   784     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
       
   785     sqlite3ExprSpan(A,&X->span,&E);
       
   786   }
       
   787   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
       
   788     SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z);
       
   789     A = sqlite3Expr(TK_IN, X, 0, 0);
       
   790     if( A ){
       
   791       A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0);
       
   792     }else{
       
   793       sqlite3SrcListDelete(pSrc);
       
   794     }
       
   795     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
       
   796     sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
       
   797   }
       
   798   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
       
   799     Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0);
       
   800     if( p ){
       
   801       p->pSelect = Y;
       
   802       sqlite3ExprSpan(p,&B,&E);
       
   803     }else{
       
   804       sqlite3SelectDelete(Y);
       
   805     }
       
   806   }
       
   807 %endif // SQLITE_OMIT_SUBQUERY
       
   808 
       
   809 /* CASE expressions */
       
   810 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
       
   811   A = sqlite3Expr(TK_CASE, X, Z, 0);
       
   812   if( A ){
       
   813     A->pList = Y;
       
   814   }else{
       
   815     sqlite3ExprListDelete(Y);
       
   816   }
       
   817   sqlite3ExprSpan(A, &C, &E);
       
   818 }
       
   819 %type case_exprlist {ExprList*}
       
   820 %destructor case_exprlist {sqlite3ExprListDelete($$);}
       
   821 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
       
   822   A = sqlite3ExprListAppend(X, Y, 0);
       
   823   A = sqlite3ExprListAppend(A, Z, 0);
       
   824 }
       
   825 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
       
   826   A = sqlite3ExprListAppend(0, Y, 0);
       
   827   A = sqlite3ExprListAppend(A, Z, 0);
       
   828 }
       
   829 %type case_else {Expr*}
       
   830 %destructor case_else {sqlite3ExprDelete($$);}
       
   831 case_else(A) ::=  ELSE expr(X).         {A = X;}
       
   832 case_else(A) ::=  .                     {A = 0;} 
       
   833 %type case_operand {Expr*}
       
   834 %destructor case_operand {sqlite3ExprDelete($$);}
       
   835 case_operand(A) ::= expr(X).            {A = X;} 
       
   836 case_operand(A) ::= .                   {A = 0;} 
       
   837 
       
   838 %type exprlist {ExprList*}
       
   839 %destructor exprlist {sqlite3ExprListDelete($$);}
       
   840 %type expritem {Expr*}
       
   841 %destructor expritem {sqlite3ExprDelete($$);}
       
   842 
       
   843 exprlist(A) ::= exprlist(X) COMMA expritem(Y). 
       
   844                                         {A = sqlite3ExprListAppend(X,Y,0);}
       
   845 exprlist(A) ::= expritem(X).            {A = sqlite3ExprListAppend(0,X,0);}
       
   846 expritem(A) ::= expr(X).                {A = X;}
       
   847 expritem(A) ::= .                       {A = 0;}
       
   848 
       
   849 ///////////////////////////// The CREATE INDEX command ///////////////////////
       
   850 //
       
   851 cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
       
   852         ON nm(Y) LP idxlist(Z) RP(E). {
       
   853   sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U,
       
   854                       &S, &E, SQLITE_SO_ASC, NE);
       
   855 }
       
   856 
       
   857 %type uniqueflag {int}
       
   858 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
       
   859 uniqueflag(A) ::= .        {A = OE_None;}
       
   860 
       
   861 %type idxlist {ExprList*}
       
   862 %destructor idxlist {sqlite3ExprListDelete($$);}
       
   863 %type idxlist_opt {ExprList*}
       
   864 %destructor idxlist_opt {sqlite3ExprListDelete($$);}
       
   865 %type idxitem {Token}
       
   866 
       
   867 idxlist_opt(A) ::= .                         {A = 0;}
       
   868 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
       
   869 idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z).  {
       
   870   Expr *p = 0;
       
   871   if( C.n>0 ){
       
   872     p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
       
   873     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
       
   874   }
       
   875   A = sqlite3ExprListAppend(X, p, &Y);
       
   876   if( A ) A->a[A->nExpr-1].sortOrder = Z;
       
   877 }
       
   878 idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). {
       
   879   Expr *p = 0;
       
   880   if( C.n>0 ){
       
   881     p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
       
   882     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
       
   883   }
       
   884   A = sqlite3ExprListAppend(0, p, &Y);
       
   885   if( A ) A->a[A->nExpr-1].sortOrder = Z;
       
   886 }
       
   887 idxitem(A) ::= nm(X).              {A = X;}
       
   888 
       
   889 
       
   890 ///////////////////////////// The DROP INDEX command /////////////////////////
       
   891 //
       
   892 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
       
   893 
       
   894 ///////////////////////////// The VACUUM command /////////////////////////////
       
   895 //
       
   896 cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
       
   897 cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
       
   898 
       
   899 ///////////////////////////// The PRAGMA command /////////////////////////////
       
   900 //
       
   901 %ifndef SQLITE_OMIT_PRAGMA
       
   902 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nm(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
       
   903 cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
       
   904 cmd ::= PRAGMA nm(X) dbnm(Z) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
       
   905 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
       
   906   sqlite3Pragma(pParse,&X,&Z,&Y,1);
       
   907 }
       
   908 cmd ::= PRAGMA nm(X) dbnm(Z) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
       
   909 cmd ::= PRAGMA nm(X) dbnm(Z).             {sqlite3Pragma(pParse,&X,&Z,0,0);}
       
   910 %endif // SQLITE_OMIT_PRAGMA
       
   911 plus_num(A) ::= plus_opt number(X).   {A = X;}
       
   912 minus_num(A) ::= MINUS number(X).     {A = X;}
       
   913 number(A) ::= INTEGER|FLOAT(X).       {A = X;}
       
   914 plus_opt ::= PLUS.
       
   915 plus_opt ::= .
       
   916 
       
   917 //////////////////////////// The CREATE TRIGGER command /////////////////////
       
   918 
       
   919 %ifndef SQLITE_OMIT_TRIGGER
       
   920 
       
   921 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
       
   922   Token all;
       
   923   all.z = A.z;
       
   924   all.n = (Z.z - A.z) + Z.n;
       
   925   sqlite3FinishTrigger(pParse, S, &all);
       
   926 }
       
   927 
       
   928 trigger_decl(A) ::= temp(T) TRIGGER nm(B) dbnm(Z) trigger_time(C)
       
   929                     trigger_event(D)
       
   930                     ON fullname(E) foreach_clause(F) when_clause(G). {
       
   931   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T);
       
   932   A = (Z.n==0?B:Z);
       
   933 }
       
   934 
       
   935 %type trigger_time  {int}
       
   936 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
       
   937 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
       
   938 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
       
   939 trigger_time(A) ::= .            { A = TK_BEFORE; }
       
   940 
       
   941 %type trigger_event {struct TrigEvent}
       
   942 %destructor trigger_event {sqlite3IdListDelete($$.b);}
       
   943 trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
       
   944 trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
       
   945 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
       
   946 
       
   947 %type foreach_clause {int}
       
   948 foreach_clause(A) ::= .                   { A = TK_ROW; }
       
   949 foreach_clause(A) ::= FOR EACH ROW.       { A = TK_ROW; }
       
   950 foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
       
   951 
       
   952 %type when_clause {Expr*}
       
   953 %destructor when_clause {sqlite3ExprDelete($$);}
       
   954 when_clause(A) ::= .             { A = 0; }
       
   955 when_clause(A) ::= WHEN expr(X). { A = X; }
       
   956 
       
   957 %type trigger_cmd_list {TriggerStep*}
       
   958 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
       
   959 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
       
   960   if( Y ){
       
   961     Y->pLast->pNext = X;
       
   962   }else{
       
   963     Y = X;
       
   964   }
       
   965   Y->pLast = X;
       
   966   A = Y;
       
   967 }
       
   968 trigger_cmd_list(A) ::= . { A = 0; }
       
   969 
       
   970 %type trigger_cmd {TriggerStep*}
       
   971 %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
       
   972 // UPDATE 
       
   973 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).  
       
   974                { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); }
       
   975 
       
   976 // INSERT
       
   977 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) 
       
   978                    VALUES LP itemlist(Y) RP.  
       
   979                {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);}
       
   980 
       
   981 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
       
   982                {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);}
       
   983 
       
   984 // DELETE
       
   985 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
       
   986                {A = sqlite3TriggerDeleteStep(&X, Y);}
       
   987 
       
   988 // SELECT
       
   989 trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(X); }
       
   990 
       
   991 // The special RAISE expression that may occur in trigger programs
       
   992 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
       
   993   A = sqlite3Expr(TK_RAISE, 0, 0, 0); 
       
   994   if( A ){
       
   995     A->iColumn = OE_Ignore;
       
   996     sqlite3ExprSpan(A, &X, &Y);
       
   997   }
       
   998 }
       
   999 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
       
  1000   A = sqlite3Expr(TK_RAISE, 0, 0, &Z); 
       
  1001   if( A ) {
       
  1002     A->iColumn = T;
       
  1003     sqlite3ExprSpan(A, &X, &Y);
       
  1004   }
       
  1005 }
       
  1006 %endif // !SQLITE_OMIT_TRIGGER
       
  1007 
       
  1008 %type raisetype {int}
       
  1009 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
       
  1010 raisetype(A) ::= ABORT.     {A = OE_Abort;}
       
  1011 raisetype(A) ::= FAIL.      {A = OE_Fail;}
       
  1012 
       
  1013 
       
  1014 ////////////////////////  DROP TRIGGER statement //////////////////////////////
       
  1015 %ifndef SQLITE_OMIT_TRIGGER
       
  1016 cmd ::= DROP TRIGGER fullname(X). {
       
  1017   sqlite3DropTrigger(pParse,X);
       
  1018 }
       
  1019 %endif // !SQLITE_OMIT_TRIGGER
       
  1020 
       
  1021 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
       
  1022 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
       
  1023   sqlite3Attach(pParse, F, D, K);
       
  1024 }
       
  1025 %type key_opt {Expr *}
       
  1026 %destructor key_opt {sqlite3ExprDelete($$);}
       
  1027 key_opt(A) ::= .                     { A = 0; }
       
  1028 key_opt(A) ::= KEY expr(X).          { A = X; }
       
  1029 
       
  1030 database_kw_opt ::= DATABASE.
       
  1031 database_kw_opt ::= .
       
  1032 
       
  1033 //////////////////////// DETACH DATABASE name /////////////////////////////////
       
  1034 cmd ::= DETACH database_kw_opt expr(D). {
       
  1035   sqlite3Detach(pParse, D);
       
  1036 }
       
  1037 
       
  1038 ////////////////////////// REINDEX collation //////////////////////////////////
       
  1039 %ifndef SQLITE_OMIT_REINDEX
       
  1040 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
       
  1041 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
       
  1042 %endif
       
  1043 
       
  1044 /////////////////////////////////// ANALYZE ///////////////////////////////////
       
  1045 %ifndef SQLITE_OMIT_ANALYZE
       
  1046 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
       
  1047 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
       
  1048 %endif
       
  1049 
       
  1050 //////////////////////// ALTER TABLE table ... ////////////////////////////////
       
  1051 %ifndef SQLITE_OMIT_ALTERTABLE
       
  1052 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
       
  1053   sqlite3AlterRenameTable(pParse,X,&Z);
       
  1054 }
       
  1055 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
       
  1056   sqlite3AlterFinishAddColumn(pParse, &Y);
       
  1057 }
       
  1058 add_column_fullname ::= fullname(X). {
       
  1059   sqlite3AlterBeginAddColumn(pParse, X);
       
  1060 }
       
  1061 kwcolumn_opt ::= .
       
  1062 kwcolumn_opt ::= COLUMNKW.
       
  1063 %endif
       
  1064 
       
  1065 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
       
  1066 %ifndef SQLITE_OMIT_VIRTUALTABLE
       
  1067 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
       
  1068 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
       
  1069 create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
       
  1070     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
       
  1071 }
       
  1072 vtabarglist ::= vtabarg.
       
  1073 vtabarglist ::= vtabarglist COMMA vtabarg.
       
  1074 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
       
  1075 vtabarg ::= vtabarg vtabargtoken.
       
  1076 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
       
  1077 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
       
  1078 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
       
  1079 anylist ::= .
       
  1080 anylist ::= anylist ANY(X).         {sqlite3VtabArgExtend(pParse,&X);}
       
  1081 %endif