CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
parse.y2161 linesDownload Raw Back to root
1%include {2/*3** 2001-09-154**5** The author disclaims copyright to this source code.  In place of6** a legal notice, here is a blessing:7**8**    May you do good and not evil.9**    May you find forgiveness for yourself and forgive others.10**    May you share freely, never taking more than you give.11**12*************************************************************************13** This file contains SQLite's SQL parser.14**15** The canonical source code to this file ("parse.y") is a Lemon grammar 16** file that specifies the input grammar and actions to take while parsing.17** That input file is processed by Lemon to generate a C-language 18** implementation of a parser for the given grammar.  You might be reading19** this comment as part of the translated C-code.  Edits should be made20** to the original parse.y sources.21*/22}23 24// Setup for the parser stack25%stack_size        50                        // Initial stack size26%stack_size_limit  parserStackSizeLimit      // Function returning max stack size27%realloc           parserStackRealloc        // realloc() for the stack28%free              parserStackFree           // free() for the stack29 30// All token codes are small integers with #defines that begin with "TK_"31%token_prefix TK_32 33// The type of the data attached to each token is Token.  This is also the34// default type for non-terminals.35//36%token_type {Token}37%default_type {Token}38 39// An extra argument to the constructor for the parser, which is available40// to all actions.41%extra_context {Parse *pParse}42 43// This code runs whenever there is a syntax error44//45%syntax_error {46  UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */47  if( TOKEN.z[0] ){48    parserSyntaxError(pParse, &TOKEN);49  }else{50    sqlite3ErrorMsg(pParse, "incomplete input");51  }52}53%stack_overflow {54  if( pParse->nErr==0 ) sqlite3ErrorMsg(pParse, "Recursion limit");55}56 57// The name of the generated procedure that implements the parser58// is as follows:59%name sqlite3Parser60 61// The following text is included near the beginning of the C source62// code file that implements the parser.63//64%include {65#include "sqliteInt.h"66 67/*68** Verify that the pParse->isCreate field is set69*/70#define ASSERT_IS_CREATE   assert(pParse->isCreate)71 72/*73** Disable all error recovery processing in the parser push-down74** automaton.75*/76#define YYNOERRORRECOVERY 177 78/*79** Make yytestcase() the same as testcase()80*/81#define yytestcase(X) testcase(X)82 83/*84** Indicate that sqlite3ParserFree() will never be called with a null85** pointer.86*/87#define YYPARSEFREENEVERNULL 188 89/*90** In the amalgamation, the parse.c file generated by lemon and the91** tokenize.c file are concatenated.  In that case, sqlite3RunParser()92** has access to the the size of the yyParser object and so the parser93** engine can be allocated from stack.  In that case, only the94** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked95** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be96** omitted.97*/98#ifdef SQLITE_AMALGAMATION99# define sqlite3Parser_ENGINEALWAYSONSTACK 1100#endif101 102/*103** Alternative datatype for the argument to the malloc() routine passed104** into sqlite3ParserAlloc().  The default is size_t.105*/106#define YYMALLOCARGTYPE  u64107 108/*109** An instance of the following structure describes the event of a110** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,111** TK_DELETE, or TK_INSTEAD.  If the event is of the form112**113**      UPDATE ON (a,b,c)114**115** Then the "b" IdList records the list "a,b,c".116*/117struct TrigEvent { int a; IdList * b; };118 119struct FrameBound     { int eType; Expr *pExpr; };120 121/*122** Generate a syntax error123*/124static void parserSyntaxError(Parse *pParse, Token *p){125  sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p);126}127 128/*129** Disable lookaside memory allocation for objects that might be130** shared across database connections.131*/132static void disableLookaside(Parse *pParse){133  sqlite3 *db = pParse->db;134  pParse->disableLookaside++;135#ifdef SQLITE_DEBUG136  pParse->isCreate = 1;137#endif138  memset(&pParse->u1.cr, 0, sizeof(pParse->u1.cr));139  DisableLookaside;140}141 142#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \143 && defined(SQLITE_UDL_CAPABLE_PARSER)144/*145** Issue an error message if an ORDER BY or LIMIT clause occurs on an146** UPDATE or DELETE statement.147*/148static void updateDeleteLimitError(149  Parse *pParse,150  ExprList *pOrderBy,151  Expr *pLimit152){153  if( pOrderBy ){154    sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");155  }else{156    sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");157  }158  sqlite3ExprListDelete(pParse->db, pOrderBy);159  sqlite3ExprDelete(pParse->db, pLimit);160}161#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */162 163} // end %include164 165// Input is a single SQL command166input ::= cmdlist.167cmdlist ::= cmdlist ecmd.168cmdlist ::= ecmd.169ecmd ::= SEMI.170ecmd ::= cmdx SEMI.171%ifndef SQLITE_OMIT_EXPLAIN172ecmd ::= explain cmdx SEMI.       {NEVER-REDUCE}173explain ::= EXPLAIN.              { if( pParse->pReprepare==0 ) pParse->explain = 1; }174explain ::= EXPLAIN QUERY PLAN.   { if( pParse->pReprepare==0 ) pParse->explain = 2; }175%endif  SQLITE_OMIT_EXPLAIN176cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }177 178///////////////////// Begin and end transactions. ////////////////////////////179//180 181cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}182trans_opt ::= .183trans_opt ::= TRANSACTION.184trans_opt ::= TRANSACTION nm.185%type transtype {int}186transtype(A) ::= .             {A = TK_DEFERRED;}187transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}188transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}189transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}190cmd ::= COMMIT|END(X) trans_opt.   {sqlite3EndTransaction(pParse,@X);}191cmd ::= ROLLBACK(X) trans_opt.     {sqlite3EndTransaction(pParse,@X);}192 193savepoint_opt ::= SAVEPOINT.194savepoint_opt ::= .195cmd ::= SAVEPOINT nm(X). {196  sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);197}198cmd ::= RELEASE savepoint_opt nm(X). {199  sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);200}201cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {202  sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);203}204 205///////////////////// The CREATE TABLE statement ////////////////////////////206//207cmd ::= create_table create_table_args.208create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {209   sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);210}211createkw(A) ::= CREATE(A).  {212  disableLookaside(pParse);213}214 215%type ifnotexists {int}216ifnotexists(A) ::= .              {A = 0;}217ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}218%type temp {int}219%ifndef SQLITE_OMIT_TEMPDB220temp(A) ::= TEMP.  {A = pParse->db->init.busy==0;}221%endif  SQLITE_OMIT_TEMPDB222temp(A) ::= .      {A = 0;}223create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). {224  sqlite3EndTable(pParse,&X,&E,F,0);225}226create_table_args ::= AS select(S). {227  sqlite3EndTable(pParse,0,0,0,S);228  sqlite3SelectDelete(pParse->db, S);229}230%type table_option_set {u32}231%type table_option {u32}232table_option_set(A) ::= .    {A = 0;}233table_option_set(A) ::= table_option(A).234table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;}235table_option(A) ::= WITHOUT nm(X). {236  if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){237    A = TF_WithoutRowid | TF_NoVisibleRowid;238  }else{239    A = 0;240    sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);241  }242}243table_option(A) ::= nm(X). {244  if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){245    A = TF_Strict;246  }else{247    A = 0;248    sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);249  }250}251columnlist ::= columnlist COMMA columnname carglist.252columnlist ::= columnname carglist.253columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}254 255// Declare some tokens early in order to influence their values, to 256// improve performance and reduce the executable size.  The goal here is257// to get the "jump" operations in ISNULL through ESCAPE to have numeric258// values that are early enough so that all jump operations are clustered259// at the beginning.  Also, operators like NE and EQ need to be adjacent,260// and all of the comparison operators need to be clustered together.261// Various assert() statements throughout the code enforce these restrictions.262//263%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.264%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.265%token OR AND NOT IS ISNOT MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.266%token GT LE LT GE ESCAPE.267 268// The following directive causes tokens ABORT, AFTER, ASC, etc. to269// fallback to ID if they will not parse as their original value.270// This obviates the need for the "id" nonterminal.271//272%fallback ID273  ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW274  CONFLICT DATABASE DEFERRED DESC DETACH DO275  EACH END EXCLUSIVE EXPLAIN FAIL FOR276  IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN277  QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS278  ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT279  NULLS FIRST LAST280%ifdef SQLITE_OMIT_COMPOUND_SELECT281  EXCEPT INTERSECT UNION282%endif SQLITE_OMIT_COMPOUND_SELECT283%ifndef SQLITE_OMIT_WINDOWFUNC284  CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED285  EXCLUDE GROUPS OTHERS TIES286%endif SQLITE_OMIT_WINDOWFUNC287%ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES288  WITHIN289%endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES290%ifndef SQLITE_OMIT_GENERATED_COLUMNS291  GENERATED ALWAYS292%endif293  MATERIALIZED294  REINDEX RENAME CTIME_KW IF295  .296%wildcard ANY.297 298// Define operator precedence early so that this is the first occurrence299// of the operator tokens in the grammar.  Keeping the operators together300// causes them to be assigned integer values that are close together,301// which keeps parser tables smaller.302//303// The token values assigned to these symbols is determined by the order304// in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,305// NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See306// the sqlite3ExprIfFalse() routine for additional information on this307// constraint.308//309%left OR.310%left AND.311%right NOT.312%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.313%left GT LE LT GE.314%right ESCAPE.315%left BITAND BITOR LSHIFT RSHIFT.316%left PLUS MINUS.317%left STAR SLASH REM.318%left CONCAT PTR.319%left COLLATE.320%right BITNOT.321%nonassoc ON.322 323// An IDENTIFIER can be a generic identifier, or one of several324// keywords.  Any non-standard keyword can also be an identifier.325//326%token_class id  ID|INDEXED.327 328// And "ids" is an identifier-or-string.329//330%token_class ids  ID|STRING.331 332// An identifier or a join-keyword333//334%token_class idj  ID|INDEXED|JOIN_KW.335 336// The name of a column or table can be any of the following:337//338%type nm {Token}339nm(A) ::= idj(A).340nm(A) ::= STRING(A).341 342// A typetoken is really zero or more tokens that form a type name such343// as can be found after the column name in a CREATE TABLE statement.344// Multiple tokens are concatenated to form the value of the typetoken.345//346%type typetoken {Token}347typetoken(A) ::= .   {A.n = 0; A.z = 0;}348typetoken(A) ::= typename(A).349typetoken(A) ::= typename(A) LP signed RP(Y). {350  A.n = (int)(&Y.z[Y.n] - A.z);351}352typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {353  A.n = (int)(&Y.z[Y.n] - A.z);354}355%type typename {Token}356typename(A) ::= ids(A).357typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}358signed ::= plus_num.359signed ::= minus_num.360 361// The scanpt non-terminal takes a value which is a pointer to the362// input text just past the last token that has been shifted into363// the parser.  By surrounding some phrase in the grammar with two364// scanpt non-terminals, we can capture the input text for that phrase.365// For example:366//367//      something ::= .... scanpt(A) phrase scanpt(Z).368//369// The text that is parsed as "phrase" is a string starting at A370// and containing (int)(Z-A) characters.  There might be some extra371// whitespace on either end of the text, but that can be removed in372// post-processing, if needed.373//374%type scanpt {const char*}375scanpt(A) ::= . {376  assert( yyLookahead!=YYNOCODE );377  A = yyLookaheadToken.z;378}379scantok(A) ::= . {380  assert( yyLookahead!=YYNOCODE );381  A = yyLookaheadToken;382}383 384// "carglist" is a list of additional constraints that come after the385// column name and column type in a CREATE TABLE statement.386//387carglist ::= carglist ccons.388carglist ::= .389ccons ::= CONSTRAINT nm(X). {ASSERT_IS_CREATE; pParse->u1.cr.constraintName = X;}390ccons ::= DEFAULT scantok(A) term(X).391                            {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}392ccons ::= DEFAULT LP(A) expr(X) RP(Z).393                            {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}394ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).395                            {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}396ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {397  Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);398  sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);399}400ccons ::= DEFAULT scantok id(X).       {401  Expr *p = tokenExpr(pParse, TK_STRING, X);402  if( p ){403    sqlite3ExprIdToTrueFalse(p);404    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );405  }406    sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);407}408 409// In addition to the type name, we also care about the primary key and410// UNIQUE constraints.411//412ccons ::= NULL onconf.413ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}414ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).415                                 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}416ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,417                                   SQLITE_IDXTYPE_UNIQUE);}418ccons ::= CHECK LP(A) expr(X) RP(B).  {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);}419ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).420                                 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}421ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}422ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}423ccons ::= GENERATED ALWAYS AS generated.424ccons ::= AS generated.425generated ::= LP expr(E) RP.          {sqlite3AddGenerated(pParse,E,0);}426generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}427 428// The optional AUTOINCREMENT keyword429%type autoinc {int}430autoinc(X) ::= .          {X = 0;}431autoinc(X) ::= AUTOINCR.  {X = 1;}432 433// The next group of rules parses the arguments to a REFERENCES clause434// that determine if the referential integrity checking is deferred or435// or immediate and which determine what action to take if a ref-integ436// check fails.437//438%type refargs {int}439refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}440refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }441%type refarg {struct {int value; int mask;}}442refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }443refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }444refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }445refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }446%type refact {int}447refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}448refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}449refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}450refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}451refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}452%type defer_subclause {int}453defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}454defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}455%type init_deferred_pred_opt {int}456init_deferred_pred_opt(A) ::= .                       {A = 0;}457init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}458init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}459 460conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}461conslist_opt(A) ::= COMMA(A) conslist.462conslist ::= conslist tconscomma tcons.463conslist ::= tcons.464tconscomma ::= COMMA.          {ASSERT_IS_CREATE; pParse->u1.cr.constraintName.n = 0;}465tconscomma ::= .466tcons ::= CONSTRAINT nm(X).    {ASSERT_IS_CREATE; pParse->u1.cr.constraintName = X;}467tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).468                                 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}469tcons ::= UNIQUE LP sortlist(X) RP onconf(R).470                                 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,471                                       SQLITE_IDXTYPE_UNIQUE);}472tcons ::= CHECK LP(A) expr(E) RP(B) onconf.473                                 {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);}474tcons ::= FOREIGN KEY LP eidlist(FA) RP475          REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {476    sqlite3CreateForeignKey(pParse, FA, &T, TA, R);477    sqlite3DeferForeignKey(pParse, D);478}479%type defer_subclause_opt {int}480defer_subclause_opt(A) ::= .                    {A = 0;}481defer_subclause_opt(A) ::= defer_subclause(A).482 483// The following is a non-standard extension that allows us to declare the484// default behavior when there is a constraint conflict.485//486%type onconf {int}487%type orconf {int}488%type resolvetype {int}489onconf(A) ::= .                              {A = OE_Default;}490onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}491orconf(A) ::= .                              {A = OE_Default;}492orconf(A) ::= OR resolvetype(X).             {A = X;}493resolvetype(A) ::= raisetype(A).494resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}495resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}496 497////////////////////////// The DROP TABLE /////////////////////////////////////498//499cmd ::= DROP TABLE ifexists(E) fullname(X). {500  sqlite3DropTable(pParse, X, 0, E);501}502%type ifexists {int}503ifexists(A) ::= IF EXISTS.   {A = 1;}504ifexists(A) ::= .            {A = 0;}505 506///////////////////// The CREATE VIEW statement /////////////////////////////507//508%ifndef SQLITE_OMIT_VIEW509cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)510          AS select(S). {511  sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);512}513cmd ::= DROP VIEW ifexists(E) fullname(X). {514  sqlite3DropTable(pParse, X, 1, E);515}516%endif  SQLITE_OMIT_VIEW517 518//////////////////////// The SELECT statement /////////////////////////////////519//520cmd ::= select(X).  {521  SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};522  if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed)!=0523   || sqlite3ReadSchema(pParse)==SQLITE_OK524  ){525    sqlite3Select(pParse, X, &dest);526  }527  sqlite3SelectDelete(pParse->db, X);528}529 530%type select {Select*}531%destructor select {sqlite3SelectDelete(pParse->db, $$);}532%type selectnowith {Select*}533%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}534%type oneselect {Select*}535%destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}536 537%include {538  /*539  ** For a compound SELECT statement, make sure p->pPrior->pNext==p for540  ** all elements in the list.  And make sure list length does not exceed541  ** SQLITE_LIMIT_COMPOUND_SELECT.542  */543  static void parserDoubleLinkSelect(Parse *pParse, Select *p){544    assert( p!=0 );545    if( p->pPrior ){546      Select *pNext = 0, *pLoop = p;547      int mxSelect, cnt = 1;548      while(1){549        pLoop->pNext = pNext;550        pLoop->selFlags |= SF_Compound;551        pNext = pLoop;552        pLoop = pLoop->pPrior;553        if( pLoop==0 ) break;554        cnt++;        555        if( pLoop->pOrderBy || pLoop->pLimit ){556          sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",557             pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",558             sqlite3SelectOpName(pNext->op));559          break;560        }561      }562      if( (p->selFlags & (SF_MultiValue|SF_Values))==0563       && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0564       && cnt>mxSelect565      ){566        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");567      }568    }569  }570 571  /* Attach a With object describing the WITH clause to a Select572  ** object describing the query for which the WITH clause is a prefix.573  */574  static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){575    if( pSelect ){576      pSelect->pWith = pWith;577      parserDoubleLinkSelect(pParse, pSelect);578    }else{579      sqlite3WithDelete(pParse->db, pWith);580    }581    return pSelect;582  }583 584  /* Memory allocator for parser stack resizing.  This is a thin wrapper around585  ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate586  ** testing.587  */588  static void *parserStackRealloc(589    void *pOld,               /* Prior allocation */590    sqlite3_uint64 newSize,   /* Requested new alloation size */591    Parse *pParse             /* Parsing context */592  ){593    void *p = sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize);594    if( p==0 ) sqlite3OomFault(pParse->db);595    return p;596  }597  static void parserStackFree(void *pOld, Parse *pParse){598    (void)pParse;599    sqlite3_free(pOld); 600  }601 602  /* Return an integer that is the maximum allowed stack size */603  static int parserStackSizeLimit(Parse *pParse){604    return pParse->db->aLimit[SQLITE_LIMIT_PARSER_DEPTH];605  }606}607 608%ifndef SQLITE_OMIT_CTE609select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}610select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).611                                              {A = attachWithToSelect(pParse,X,W);}612 613%endif /* SQLITE_OMIT_CTE */614select(A) ::= selectnowith(A). {615  Select *p = A;616  if( p ){617    parserDoubleLinkSelect(pParse, p);618  }619}620 621selectnowith(A) ::= oneselect(A).622%ifndef SQLITE_OMIT_COMPOUND_SELECT623selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {624  Select *pRhs = Z;625  Select *pLhs = A;626  if( pRhs && pRhs->pPrior ){627    SrcList *pFrom;628    Token x;629    x.n = 0;630    parserDoubleLinkSelect(pParse, pRhs);631    pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);632    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);633  }634  if( pRhs ){635    pRhs->op = (u8)Y;636    pRhs->pPrior = pLhs;637    if( ALWAYS(pLhs) ) pLhs->selFlags &= ~(u32)SF_MultiValue;638    pRhs->selFlags &= ~(u32)SF_MultiValue;639    if( Y!=TK_ALL ) pParse->hasCompound = 1;640  }else{641    sqlite3SelectDelete(pParse->db, pLhs);642  }643  A = pRhs;644}645%type multiselect_op {int}646multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}647multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}648multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}649%endif SQLITE_OMIT_COMPOUND_SELECT650 651oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)652                 groupby_opt(P) having_opt(Q) 653                 orderby_opt(Z) limit_opt(L). {654  A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);655}656%ifndef SQLITE_OMIT_WINDOWFUNC657oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)658                 groupby_opt(P) having_opt(Q) window_clause(R)659                 orderby_opt(Z) limit_opt(L). {660  A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);661  if( A ){662    A->pWinDefn = R;663  }else{664    sqlite3WindowListDelete(pParse->db, R);665  }666}667%endif668 669 670// Single row VALUES clause.671//672%type values {Select*}673oneselect(A) ::= values(A).674%destructor values {sqlite3SelectDelete(pParse->db, $$);}675values(A) ::= VALUES LP nexprlist(X) RP. {676  A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);677}678 679// Multiple row VALUES clause.680//681%type mvalues {Select*}682oneselect(A) ::= mvalues(A). {683  sqlite3MultiValuesEnd(pParse, A);684}685%destructor mvalues {sqlite3SelectDelete(pParse->db, $$);}686mvalues(A) ::= values(A) COMMA LP nexprlist(Y) RP. {687  A = sqlite3MultiValues(pParse, A, Y);688}689mvalues(A) ::= mvalues(A) COMMA LP nexprlist(Y) RP. {690  A = sqlite3MultiValues(pParse, A, Y);691}692 693// The "distinct" nonterminal is true (1) if the DISTINCT keyword is694// present and false (0) if it is not.695//696%type distinct {int}697distinct(A) ::= DISTINCT.   {A = SF_Distinct;}698distinct(A) ::= ALL.        {A = SF_All;}699distinct(A) ::= .           {A = 0;}700 701// selcollist is a list of expressions that are to become the return702// values of the SELECT statement.  The "*" in statements like703// "SELECT * FROM ..." is encoded as a special expression with an704// opcode of TK_ASTERISK.705//706%type selcollist {ExprList*}707%destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}708%type sclp {ExprList*}709%destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}710sclp(A) ::= selcollist(A) COMMA.711sclp(A) ::= .                                {A = 0;}712selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y).     {713   A = sqlite3ExprListAppend(pParse, A, X);714   if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);715   sqlite3ExprListSetSpan(pParse,A,B,Z);716}717selcollist(A) ::= sclp(A) scanpt STAR(X). {718  Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);719  sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail));720  A = sqlite3ExprListAppend(pParse, A, p);721}722selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). {723  Expr *pRight, *pLeft, *pDot;724  pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);725  sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail));726  pLeft = tokenExpr(pParse, TK_ID, X);727  pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);728  A = sqlite3ExprListAppend(pParse,A, pDot);729}730 731// An option "AS <id>" phrase that can follow one of the expressions that732// define the result set, or one of the tables in the FROM clause.733//734%type as {Token}735as(X) ::= AS nm(Y).    {X = Y;}736as(X) ::= ids(X).737as(X) ::= .            {X.n = 0; X.z = 0;}738 739 740%type seltablist {SrcList*}741%destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}742%type stl_prefix {SrcList*}743%destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}744%type from {SrcList*}745%destructor from {sqlite3SrcListDelete(pParse->db, $$);}746 747// A complete FROM clause.748//749from(A) ::= .                {A = 0;}750from(A) ::= FROM seltablist(X). {751  A = X;752  sqlite3SrcListShiftJoinType(pParse,A);753}754 755// "seltablist" is a "Select Table List" - the content of the FROM clause756// in a SELECT statement.  "stl_prefix" is a prefix of this list.757//758stl_prefix(A) ::= seltablist(A) joinop(Y).    {759   if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;760}761stl_prefix(A) ::= .                           {A = 0;}762seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {763  A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);764}765seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {766  A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);767  sqlite3SrcListIndexedBy(pParse, A, &I);768}769seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {770  A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);771  sqlite3SrcListFuncArgs(pParse, A, E);772}773%ifndef SQLITE_OMIT_SUBQUERY774  seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {775    A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);776  }777  seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {778    if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){779      A = F;780    }else if( ALWAYS(F!=0) && F->nSrc==1 ){781      A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);782      if( A ){783        SrcItem *pNew = &A->a[A->nSrc-1];784        SrcItem *pOld = F->a;785        assert( pOld->fg.fixedSchema==0 );786        pNew->zName = pOld->zName;787        assert( pOld->fg.fixedSchema==0 );788        if( pOld->fg.isSubquery ){789          pNew->fg.isSubquery = 1;790          pNew->u4.pSubq = pOld->u4.pSubq;791          pOld->u4.pSubq = 0;792          pOld->fg.isSubquery = 0;793          assert( pNew->u4.pSubq!=0 && pNew->u4.pSubq->pSelect!=0 );794          if( (pNew->u4.pSubq->pSelect->selFlags & SF_NestedFrom)!=0 ){795            pNew->fg.isNestedFrom = 1;796          }797        }else{798          pNew->u4.zDatabase = pOld->u4.zDatabase;799          pOld->u4.zDatabase = 0;800        }801        if( pOld->fg.isTabFunc ){802          pNew->u1.pFuncArg = pOld->u1.pFuncArg;803          pOld->u1.pFuncArg = 0;804          pOld->fg.isTabFunc = 0;805          pNew->fg.isTabFunc = 1;806        }807        pOld->zName = 0;808      }809      sqlite3SrcListDelete(pParse->db, F);810    }else{811      Select *pSubquery;812      sqlite3SrcListShiftJoinType(pParse,F);813      pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);814      A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);815    }816  }817%endif  SQLITE_OMIT_SUBQUERY818 819%type dbnm {Token}820dbnm(A) ::= .          {A.z=0; A.n=0;}821dbnm(A) ::= DOT nm(X). {A = X;}822 823%type fullname {SrcList*}824%destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}825fullname(A) ::= nm(X).  {826  A = sqlite3SrcListAppend(pParse,0,&X,0);827  if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);828}829fullname(A) ::= nm(X) DOT nm(Y). {830  A = sqlite3SrcListAppend(pParse,0,&X,&Y);831  if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);832}833 834%type xfullname {SrcList*}835%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}836xfullname(A) ::= nm(X).  {837  A = sqlite3SrcListAppend(pParse,0,&X,0);838  if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);839}840xfullname(A) ::= nm(X) DOT nm(Y). {841  A = sqlite3SrcListAppend(pParse,0,&X,&Y);842  if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);843}844xfullname(A) ::= nm(X) AS nm(Z).  {845  A = sqlite3SrcListAppend(pParse,0,&X,0);846  if( A ){847    if( IN_RENAME_OBJECT ){848      sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);849    }else{850      A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);851    }852  }853}854xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {855  A = sqlite3SrcListAppend(pParse,0,&X,&Y);856  if( A ){857    if( IN_RENAME_OBJECT ){858      sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);859    }else{860      A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);861    }862  }863}864 865 866%type joinop {int}867joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }868joinop(X) ::= JOIN_KW(A) JOIN.869                  {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}870joinop(X) ::= JOIN_KW(A) nm(B) JOIN.871                  {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}872joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.873                  {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}874 875// There is a parsing ambiguity in an upsert statement that uses a876// SELECT on the RHS of a the INSERT:877//878//      INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...879//                                        here ----^^880//881// When the ON token is encountered, the parser does not know if it is882// the beginning of an ON CONFLICT clause, or the beginning of an ON883// clause associated with the JOIN.  The conflict is resolved in favor884// of the JOIN.  If an ON CONFLICT clause is intended, insert a dummy885// WHERE clause in between, like this:886//887//      INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...888//889// The [AND] and [OR] precedence marks in the rules for on_using cause the890// ON in this context to always be interpreted as belonging to the JOIN.891//892%type on_using {OnOrUsing}893//%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}894on_using(N) ::= ON expr(E).            {N.pOn = E; N.pUsing = 0;}895on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}896on_using(N) ::= .                 [OR] {N.pOn = 0; N.pUsing = 0;}897 898// Note that this block abuses the Token type just a little. If there is899// no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If900// there is an INDEXED BY clause, then the token is populated as per normal,901// with z pointing to the token data and n containing the number of bytes902// in the token.903//904// If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is 905// normally illegal. The sqlite3SrcListIndexedBy() function 906// recognizes and interprets this as a special case.907//908%type indexed_opt {Token}909%type indexed_by  {Token}910indexed_opt(A) ::= .                 {A.z=0; A.n=0;}911indexed_opt(A) ::= indexed_by(A).912indexed_by(A)  ::= INDEXED BY nm(X). {A = X;}913indexed_by(A)  ::= NOT INDEXED.      {A.z=0; A.n=1;}914 915%type orderby_opt {ExprList*}916%destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}917 918// the sortlist non-terminal stores a list of expression where each919// expression is optionally followed by ASC or DESC to indicate the920// sort order.921//922%type sortlist {ExprList*}923%destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}924 925orderby_opt(A) ::= .                          {A = 0;}926orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}927sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {928  A = sqlite3ExprListAppend(pParse,A,Y);929  sqlite3ExprListSetSortOrder(A,Z,X);930}931sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {932  A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/933  sqlite3ExprListSetSortOrder(A,Z,X);934}935 936%type sortorder {int}937 938sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}939sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}940sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}941 942%type nulls {int}943nulls(A) ::= NULLS FIRST.       {A = SQLITE_SO_ASC;}944nulls(A) ::= NULLS LAST.        {A = SQLITE_SO_DESC;}945nulls(A) ::= .                  {A = SQLITE_SO_UNDEFINED;}946 947%type groupby_opt {ExprList*}948%destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}949groupby_opt(A) ::= .                      {A = 0;}950groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}951 952%type having_opt {Expr*}953%destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}954having_opt(A) ::= .                {A = 0;}955having_opt(A) ::= HAVING expr(X).  {A = X;}956 957%type limit_opt {Expr*}958 959// The destructor for limit_opt will never fire in the current grammar.960// The limit_opt non-terminal only occurs at the end of a single production961// rule for SELECT statements.  As soon as the rule that create the 962// limit_opt non-terminal reduces, the SELECT statement rule will also963// reduce.  So there is never a limit_opt non-terminal on the stack 964// except as a transient.  So there is never anything to destroy.965//966//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}967limit_opt(A) ::= .       {A = 0;}968limit_opt(A) ::= LIMIT expr(X).969                         {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}970limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 971                         {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}972limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 973                         {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}974 975/////////////////////////// The DELETE statement /////////////////////////////976//977%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER978cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)979        orderby_opt(O) limit_opt(L). {980  sqlite3SrcListIndexedBy(pParse, X, &I);981#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT982  if( O || L ){983    updateDeleteLimitError(pParse,O,L);984    O = 0;985    L = 0;986  }987#endif988  sqlite3DeleteFrom(pParse,X,W,O,L);989}990%else991cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {992  sqlite3SrcListIndexedBy(pParse, X, &I);993  sqlite3DeleteFrom(pParse,X,W,0,0);994}995%endif996 997%type where_opt {Expr*}998%destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}999%type where_opt_ret {Expr*}1000%destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}1001 1002where_opt(A) ::= .                    {A = 0;}1003where_opt(A) ::= WHERE expr(X).       {A = X;}1004where_opt_ret(A) ::= .                                      {A = 0;}1005where_opt_ret(A) ::= WHERE expr(X).                         {A = X;}1006where_opt_ret(A) ::= RETURNING selcollist(X).               1007       {sqlite3AddReturning(pParse,X); A = 0;}1008where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).1009       {sqlite3AddReturning(pParse,Y); A = X;}1010 1011////////////////////////// The UPDATE command ////////////////////////////////1012//1013%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER1014cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)1015        where_opt_ret(W) orderby_opt(O) limit_opt(L).  {1016  sqlite3SrcListIndexedBy(pParse, X, &I);1017  if( F ){1018    SrcList *pFromClause = F;1019    if( pFromClause->nSrc>1 ){1020      Select *pSubquery;1021      Token as;1022      pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);1023      as.n = 0;1024      as.z = 0;1025      pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);1026    }1027    X = sqlite3SrcListAppendList(pParse, X, pFromClause);1028  }1029  sqlite3ExprListCheckLength(pParse,Y,"set list"); 1030#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1031  if( O || L ){1032    updateDeleteLimitError(pParse,O,L);1033    O = 0;1034    L = 0;1035  }1036#endif1037  sqlite3Update(pParse,X,Y,W,R,O,L,0);1038}1039%else1040cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)1041        where_opt_ret(W). {1042  sqlite3SrcListIndexedBy(pParse, X, &I);1043  sqlite3ExprListCheckLength(pParse,Y,"set list"); 1044  if( F ){1045    SrcList *pFromClause = F;1046    if( pFromClause->nSrc>1 ){1047      Select *pSubquery;1048      Token as;1049      pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);1050      as.n = 0;1051      as.z = 0;1052      pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);1053    }1054    X = sqlite3SrcListAppendList(pParse, X, pFromClause);1055  }1056  sqlite3Update(pParse,X,Y,W,R,0,0,0);1057}1058%endif1059 1060 1061 1062%type setlist {ExprList*}1063%destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}1064 1065setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {1066  A = sqlite3ExprListAppend(pParse, A, Y);1067  sqlite3ExprListSetName(pParse, A, &X, 1);1068}1069setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {1070  A = sqlite3ExprListAppendVector(pParse, A, X, Y);1071}1072setlist(A) ::= nm(X) EQ expr(Y). {1073  A = sqlite3ExprListAppend(pParse, 0, Y);1074  sqlite3ExprListSetName(pParse, A, &X, 1);1075}1076setlist(A) ::= LP idlist(X) RP EQ expr(Y). {1077  A = sqlite3ExprListAppendVector(pParse, 0, X, Y);1078}1079 1080////////////////////////// The INSERT command /////////////////////////////////1081//1082cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)1083        upsert(U). {1084  sqlite3Insert(pParse, X, S, F, R, U);1085}1086cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.1087{1088  sqlite3Insert(pParse, X, 0, F, R, 0);1089}1090 1091%type upsert {Upsert*}1092 1093// Because upsert only occurs at the tip end of the INSERT rule for cmd,1094// there is never a case where the value of the upsert pointer will not1095// be destroyed by the cmd action.  So comment-out the destructor to1096// avoid unreachable code.1097//%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}1098upsert(A) ::= . { A = 0; }1099upsert(A) ::= RETURNING selcollist(X).  { A = 0; sqlite3AddReturning(pParse,X); }1100upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)1101              DO UPDATE SET setlist(Z) where_opt(W) upsert(N).1102              { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}1103upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).1104              { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }1105upsert(A) ::= ON CONFLICT DO NOTHING returning.1106              { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }1107upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.1108              { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}1109 1110returning ::= RETURNING selcollist(X).  {sqlite3AddReturning(pParse,X);}1111returning ::= .1112 1113%type insert_cmd {int}1114insert_cmd(A) ::= INSERT orconf(R).   {A = R;}1115insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}1116 1117%type idlist_opt {IdList*}1118%destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}1119%type idlist {IdList*}1120%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}1121 1122idlist_opt(A) ::= .                       {A = 0;}1123idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}1124idlist(A) ::= idlist(A) COMMA nm(Y).1125    {A = sqlite3IdListAppend(pParse,A,&Y);}1126idlist(A) ::= nm(Y).1127    {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}1128 1129/////////////////////////// Expression Processing /////////////////////////////1130//1131 1132%type expr {Expr*}1133%destructor expr {sqlite3ExprDelete(pParse->db, $$);}1134%type term {Expr*}1135%destructor term {sqlite3ExprDelete(pParse->db, $$);}1136 1137%include {1138 1139  /* Construct a new Expr object from a single token */1140  static Expr *tokenExpr(Parse *pParse, int op, Token t){1141    Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);1142    if( p ){1143      /* memset(p, 0, sizeof(Expr)); */1144      p->op = (u8)op;1145      p->affExpr = 0;1146      p->flags = EP_Leaf;1147      ExprClearVVAProperties(p);1148      /* p->iAgg = -1; // Not required */1149      p->pLeft = p->pRight = 0;1150      p->pAggInfo = 0;1151      memset(&p->x, 0, sizeof(p->x));1152      memset(&p->y, 0, sizeof(p->y));1153      p->op2 = 0;1154      p->iTable = 0;1155      p->iColumn = 0;1156      p->u.zToken = (char*)&p[1];1157      memcpy(p->u.zToken, t.z, t.n);1158      p->u.zToken[t.n] = 0;1159      p->w.iOfst = (int)(t.z - pParse->zTail);1160      if( sqlite3Isquote(p->u.zToken[0]) ){1161        sqlite3DequoteExpr(p);1162      }1163#if SQLITE_MAX_EXPR_DEPTH>01164      p->nHeight = 1;1165#endif  1166      if( IN_RENAME_OBJECT ){1167        return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);1168      }1169    }1170    return p;1171  }1172 1173}1174 1175expr(A) ::= term(A).1176expr(A) ::= LP expr(X) RP. {A = X;}1177expr(A) ::= idj(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}1178expr(A) ::= nm(X) DOT nm(Y). {1179  Expr *temp1 = tokenExpr(pParse,TK_ID,X);1180  Expr *temp2 = tokenExpr(pParse,TK_ID,Y);1181  A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);1182}1183expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {1184  Expr *temp1 = tokenExpr(pParse,TK_ID,X);1185  Expr *temp2 = tokenExpr(pParse,TK_ID,Y);1186  Expr *temp3 = tokenExpr(pParse,TK_ID,Z);1187  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);1188  if( IN_RENAME_OBJECT ){1189    sqlite3RenameTokenRemap(pParse, 0, temp1);1190  }1191  A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);1192}1193term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}1194term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}1195term(A) ::= INTEGER(X). {1196  int iValue;1197  if( sqlite3GetInt32(X.z, &iValue)==0 ){1198    A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 0);1199  }else{1200    A = sqlite3ExprInt32(pParse->db, iValue);

Showing the first 1,200 of 2161 lines. Download the file for the rest.