xref: /sqlite-3.40.0/src/parse.y (revision 902e2602)
1 %include {
2 /*
3 ** 2001-09-15
4 **
5 ** The author disclaims copyright to this source code.  In place of
6 ** 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 grammer.  You might be reading
19 ** this comment as part of the translated C-code.  Edits should be made
20 ** to the original parse.y sources.
21 */
22 }
23 
24 // All token codes are small integers with #defines that begin with "TK_"
25 %token_prefix TK_
26 
27 // The type of the data attached to each token is Token.  This is also the
28 // default type for non-terminals.
29 //
30 %token_type {Token}
31 %default_type {Token}
32 
33 // An extra argument to the constructor for the parser, which is available
34 // to all actions.
35 %extra_context {Parse *pParse}
36 
37 // This code runs whenever there is a syntax error
38 //
39 %syntax_error {
40   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
41   if( TOKEN.z[0] ){
42     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
43   }else{
44     sqlite3ErrorMsg(pParse, "incomplete input");
45   }
46 }
47 %stack_overflow {
48   sqlite3ErrorMsg(pParse, "parser stack overflow");
49 }
50 
51 // The name of the generated procedure that implements the parser
52 // is as follows:
53 %name sqlite3Parser
54 
55 // The following text is included near the beginning of the C source
56 // code file that implements the parser.
57 //
58 %include {
59 #include "sqliteInt.h"
60 
61 /*
62 ** Disable all error recovery processing in the parser push-down
63 ** automaton.
64 */
65 #define YYNOERRORRECOVERY 1
66 
67 /*
68 ** Make yytestcase() the same as testcase()
69 */
70 #define yytestcase(X) testcase(X)
71 
72 /*
73 ** Indicate that sqlite3ParserFree() will never be called with a null
74 ** pointer.
75 */
76 #define YYPARSEFREENEVERNULL 1
77 
78 /*
79 ** In the amalgamation, the parse.c file generated by lemon and the
80 ** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
81 ** has access to the the size of the yyParser object and so the parser
82 ** engine can be allocated from stack.  In that case, only the
83 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
84 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
85 ** omitted.
86 */
87 #ifdef SQLITE_AMALGAMATION
88 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
89 #endif
90 
91 /*
92 ** Alternative datatype for the argument to the malloc() routine passed
93 ** into sqlite3ParserAlloc().  The default is size_t.
94 */
95 #define YYMALLOCARGTYPE  u64
96 
97 /*
98 ** An instance of the following structure describes the event of a
99 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
100 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
101 **
102 **      UPDATE ON (a,b,c)
103 **
104 ** Then the "b" IdList records the list "a,b,c".
105 */
106 struct TrigEvent { int a; IdList * b; };
107 
108 struct FrameBound     { int eType; Expr *pExpr; };
109 
110 /*
111 ** Disable lookaside memory allocation for objects that might be
112 ** shared across database connections.
113 */
114 static void disableLookaside(Parse *pParse){
115   sqlite3 *db = pParse->db;
116   pParse->disableLookaside++;
117   DisableLookaside;
118 }
119 
120 #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
121  && defined(SQLITE_UDL_CAPABLE_PARSER)
122 /*
123 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
124 ** UPDATE or DELETE statement.
125 */
126 static void updateDeleteLimitError(
127   Parse *pParse,
128   ExprList *pOrderBy,
129   Expr *pLimit
130 ){
131   if( pOrderBy ){
132     sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
133   }else{
134     sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
135   }
136   sqlite3ExprListDelete(pParse->db, pOrderBy);
137   sqlite3ExprDelete(pParse->db, pLimit);
138 }
139 #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
140 
141 } // end %include
142 
143 // Input is a single SQL command
144 input ::= cmdlist.
145 cmdlist ::= cmdlist ecmd.
146 cmdlist ::= ecmd.
147 ecmd ::= SEMI.
148 ecmd ::= cmdx SEMI.
149 %ifndef SQLITE_OMIT_EXPLAIN
150 ecmd ::= explain cmdx SEMI.       {NEVER-REDUCE}
151 explain ::= EXPLAIN.              { pParse->explain = 1; }
152 explain ::= EXPLAIN QUERY PLAN.   { pParse->explain = 2; }
153 %endif  SQLITE_OMIT_EXPLAIN
154 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
155 
156 ///////////////////// Begin and end transactions. ////////////////////////////
157 //
158 
159 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
160 trans_opt ::= .
161 trans_opt ::= TRANSACTION.
162 trans_opt ::= TRANSACTION nm.
163 %type transtype {int}
164 transtype(A) ::= .             {A = TK_DEFERRED;}
165 transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}
166 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
167 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
168 cmd ::= COMMIT|END(X) trans_opt.   {sqlite3EndTransaction(pParse,@X);}
169 cmd ::= ROLLBACK(X) trans_opt.     {sqlite3EndTransaction(pParse,@X);}
170 
171 savepoint_opt ::= SAVEPOINT.
172 savepoint_opt ::= .
173 cmd ::= SAVEPOINT nm(X). {
174   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
175 }
176 cmd ::= RELEASE savepoint_opt nm(X). {
177   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
178 }
179 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
180   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
181 }
182 
183 ///////////////////// The CREATE TABLE statement ////////////////////////////
184 //
185 cmd ::= create_table create_table_args.
186 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
187    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
188 }
189 createkw(A) ::= CREATE(A).  {disableLookaside(pParse);}
190 
191 %type ifnotexists {int}
192 ifnotexists(A) ::= .              {A = 0;}
193 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
194 %type temp {int}
195 %ifndef SQLITE_OMIT_TEMPDB
196 temp(A) ::= TEMP.  {A = pParse->db->init.busy==0;}
197 %endif  SQLITE_OMIT_TEMPDB
198 temp(A) ::= .      {A = 0;}
199 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). {
200   sqlite3EndTable(pParse,&X,&E,F,0);
201 }
202 create_table_args ::= AS select(S). {
203   sqlite3EndTable(pParse,0,0,0,S);
204   sqlite3SelectDelete(pParse->db, S);
205 }
206 %type table_option_set {u32}
207 %type table_option {u32}
208 table_option_set(A) ::= .    {A = 0;}
209 table_option_set(A) ::= table_option(A).
210 table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;}
211 table_option(A) ::= WITHOUT nm(X). {
212   if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
213     A = TF_WithoutRowid | TF_NoVisibleRowid;
214   }else{
215     A = 0;
216     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
217   }
218 }
219 table_option(A) ::= nm(X). {
220   if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
221     A = TF_Strict;
222   }else{
223     A = 0;
224     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
225   }
226 }
227 columnlist ::= columnlist COMMA columnname carglist.
228 columnlist ::= columnname carglist.
229 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
230 
231 // Declare some tokens early in order to influence their values, to
232 // improve performance and reduce the executable size.  The goal here is
233 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
234 // values that are early enough so that all jump operations are clustered
235 // at the beginning.
236 //
237 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
238 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
239 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
240 %token GT LE LT GE ESCAPE.
241 
242 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
243 // fallback to ID if they will not parse as their original value.
244 // This obviates the need for the "id" nonterminal.
245 //
246 %fallback ID
247   ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
248   CONFLICT DATABASE DEFERRED DESC DETACH DO
249   EACH END EXCLUSIVE EXPLAIN FAIL FOR
250   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
251   QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
252   ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
253   NULLS FIRST LAST
254 %ifdef SQLITE_OMIT_COMPOUND_SELECT
255   EXCEPT INTERSECT UNION
256 %endif SQLITE_OMIT_COMPOUND_SELECT
257 %ifndef SQLITE_OMIT_WINDOWFUNC
258   CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
259   EXCLUDE GROUPS OTHERS TIES
260 %endif SQLITE_OMIT_WINDOWFUNC
261 %ifndef SQLITE_OMIT_GENERATED_COLUMNS
262   GENERATED ALWAYS
263 %endif
264   MATERIALIZED
265   REINDEX RENAME CTIME_KW IF
266   .
267 %wildcard ANY.
268 
269 // Define operator precedence early so that this is the first occurrence
270 // of the operator tokens in the grammer.  Keeping the operators together
271 // causes them to be assigned integer values that are close together,
272 // which keeps parser tables smaller.
273 //
274 // The token values assigned to these symbols is determined by the order
275 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
276 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
277 // the sqlite3ExprIfFalse() routine for additional information on this
278 // constraint.
279 //
280 %left OR.
281 %left AND.
282 %right NOT.
283 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
284 %left GT LE LT GE.
285 %right ESCAPE.
286 %left BITAND BITOR LSHIFT RSHIFT.
287 %left PLUS MINUS.
288 %left STAR SLASH REM.
289 %left CONCAT PTR.
290 %left COLLATE.
291 %right BITNOT.
292 %nonassoc ON.
293 
294 // An IDENTIFIER can be a generic identifier, or one of several
295 // keywords.  Any non-standard keyword can also be an identifier.
296 //
297 %token_class id  ID|INDEXED.
298 
299 
300 // And "ids" is an identifer-or-string.
301 //
302 %token_class ids  ID|STRING.
303 
304 // The name of a column or table can be any of the following:
305 //
306 %type nm {Token}
307 nm(A) ::= id(A).
308 nm(A) ::= STRING(A).
309 nm(A) ::= JOIN_KW(A).
310 
311 // A typetoken is really zero or more tokens that form a type name such
312 // as can be found after the column name in a CREATE TABLE statement.
313 // Multiple tokens are concatenated to form the value of the typetoken.
314 //
315 %type typetoken {Token}
316 typetoken(A) ::= .   {A.n = 0; A.z = 0;}
317 typetoken(A) ::= typename(A).
318 typetoken(A) ::= typename(A) LP signed RP(Y). {
319   A.n = (int)(&Y.z[Y.n] - A.z);
320 }
321 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
322   A.n = (int)(&Y.z[Y.n] - A.z);
323 }
324 %type typename {Token}
325 typename(A) ::= ids(A).
326 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
327 signed ::= plus_num.
328 signed ::= minus_num.
329 
330 // The scanpt non-terminal takes a value which is a pointer to the
331 // input text just past the last token that has been shifted into
332 // the parser.  By surrounding some phrase in the grammar with two
333 // scanpt non-terminals, we can capture the input text for that phrase.
334 // For example:
335 //
336 //      something ::= .... scanpt(A) phrase scanpt(Z).
337 //
338 // The text that is parsed as "phrase" is a string starting at A
339 // and containing (int)(Z-A) characters.  There might be some extra
340 // whitespace on either end of the text, but that can be removed in
341 // post-processing, if needed.
342 //
343 %type scanpt {const char*}
344 scanpt(A) ::= . {
345   assert( yyLookahead!=YYNOCODE );
346   A = yyLookaheadToken.z;
347 }
348 scantok(A) ::= . {
349   assert( yyLookahead!=YYNOCODE );
350   A = yyLookaheadToken;
351 }
352 
353 // "carglist" is a list of additional constraints that come after the
354 // column name and column type in a CREATE TABLE statement.
355 //
356 carglist ::= carglist ccons.
357 carglist ::= .
358 ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
359 ccons ::= DEFAULT scantok(A) term(X).
360                             {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
361 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
362                             {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
363 ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
364                             {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
365 ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
366   Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
367   sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
368 }
369 ccons ::= DEFAULT scantok id(X).       {
370   Expr *p = tokenExpr(pParse, TK_STRING, X);
371   if( p ){
372     sqlite3ExprIdToTrueFalse(p);
373     testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
374   }
375     sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
376 }
377 
378 // In addition to the type name, we also care about the primary key and
379 // UNIQUE constraints.
380 //
381 ccons ::= NULL onconf.
382 ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
383 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
384                                  {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
385 ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
386                                    SQLITE_IDXTYPE_UNIQUE);}
387 ccons ::= CHECK LP(A) expr(X) RP(B).  {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);}
388 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
389                                  {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
390 ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
391 ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
392 ccons ::= GENERATED ALWAYS AS generated.
393 ccons ::= AS generated.
394 generated ::= LP expr(E) RP.          {sqlite3AddGenerated(pParse,E,0);}
395 generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
396 
397 // The optional AUTOINCREMENT keyword
398 %type autoinc {int}
399 autoinc(X) ::= .          {X = 0;}
400 autoinc(X) ::= AUTOINCR.  {X = 1;}
401 
402 // The next group of rules parses the arguments to a REFERENCES clause
403 // that determine if the referential integrity checking is deferred or
404 // or immediate and which determine what action to take if a ref-integ
405 // check fails.
406 //
407 %type refargs {int}
408 refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
409 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
410 %type refarg {struct {int value; int mask;}}
411 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
412 refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
413 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
414 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
415 %type refact {int}
416 refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
417 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
418 refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
419 refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
420 refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
421 %type defer_subclause {int}
422 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
423 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
424 %type init_deferred_pred_opt {int}
425 init_deferred_pred_opt(A) ::= .                       {A = 0;}
426 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
427 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
428 
429 conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
430 conslist_opt(A) ::= COMMA(A) conslist.
431 conslist ::= conslist tconscomma tcons.
432 conslist ::= tcons.
433 tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
434 tconscomma ::= .
435 tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
436 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
437                                  {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
438 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
439                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
440                                        SQLITE_IDXTYPE_UNIQUE);}
441 tcons ::= CHECK LP(A) expr(E) RP(B) onconf.
442                                  {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);}
443 tcons ::= FOREIGN KEY LP eidlist(FA) RP
444           REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
445     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
446     sqlite3DeferForeignKey(pParse, D);
447 }
448 %type defer_subclause_opt {int}
449 defer_subclause_opt(A) ::= .                    {A = 0;}
450 defer_subclause_opt(A) ::= defer_subclause(A).
451 
452 // The following is a non-standard extension that allows us to declare the
453 // default behavior when there is a constraint conflict.
454 //
455 %type onconf {int}
456 %type orconf {int}
457 %type resolvetype {int}
458 onconf(A) ::= .                              {A = OE_Default;}
459 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
460 orconf(A) ::= .                              {A = OE_Default;}
461 orconf(A) ::= OR resolvetype(X).             {A = X;}
462 resolvetype(A) ::= raisetype(A).
463 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
464 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
465 
466 ////////////////////////// The DROP TABLE /////////////////////////////////////
467 //
468 cmd ::= DROP TABLE ifexists(E) fullname(X). {
469   sqlite3DropTable(pParse, X, 0, E);
470 }
471 %type ifexists {int}
472 ifexists(A) ::= IF EXISTS.   {A = 1;}
473 ifexists(A) ::= .            {A = 0;}
474 
475 ///////////////////// The CREATE VIEW statement /////////////////////////////
476 //
477 %ifndef SQLITE_OMIT_VIEW
478 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
479           AS select(S). {
480   sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
481 }
482 cmd ::= DROP VIEW ifexists(E) fullname(X). {
483   sqlite3DropTable(pParse, X, 1, E);
484 }
485 %endif  SQLITE_OMIT_VIEW
486 
487 //////////////////////// The SELECT statement /////////////////////////////////
488 //
489 cmd ::= select(X).  {
490   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
491   sqlite3Select(pParse, X, &dest);
492   sqlite3SelectDelete(pParse->db, X);
493 }
494 
495 %type select {Select*}
496 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
497 %type selectnowith {Select*}
498 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
499 %type oneselect {Select*}
500 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
501 
502 %include {
503   /*
504   ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
505   ** all elements in the list.  And make sure list length does not exceed
506   ** SQLITE_LIMIT_COMPOUND_SELECT.
507   */
508   static void parserDoubleLinkSelect(Parse *pParse, Select *p){
509     assert( p!=0 );
510     if( p->pPrior ){
511       Select *pNext = 0, *pLoop = p;
512       int mxSelect, cnt = 1;
513       while(1){
514         pLoop->pNext = pNext;
515         pLoop->selFlags |= SF_Compound;
516         pNext = pLoop;
517         pLoop = pLoop->pPrior;
518         if( pLoop==0 ) break;
519         cnt++;
520         if( pLoop->pOrderBy || pLoop->pLimit ){
521           sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
522              pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
523              sqlite3SelectOpName(pNext->op));
524           break;
525         }
526       }
527       if( (p->selFlags & SF_MultiValue)==0 &&
528         (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
529         cnt>mxSelect
530       ){
531         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
532       }
533     }
534   }
535 
536   /* Attach a With object describing the WITH clause to a Select
537   ** object describing the query for which the WITH clause is a prefix.
538   */
539   static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
540     if( pSelect ){
541       pSelect->pWith = pWith;
542       parserDoubleLinkSelect(pParse, pSelect);
543     }else{
544       sqlite3WithDelete(pParse->db, pWith);
545     }
546     return pSelect;
547   }
548 }
549 
550 %ifndef SQLITE_OMIT_CTE
551 select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}
552 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).
553                                               {A = attachWithToSelect(pParse,X,W);}
554 %endif /* SQLITE_OMIT_CTE */
555 select(A) ::= selectnowith(X). {
556   Select *p = X;
557   if( p ){
558     parserDoubleLinkSelect(pParse, p);
559   }
560   A = p; /*A-overwrites-X*/
561 }
562 
563 selectnowith(A) ::= oneselect(A).
564 %ifndef SQLITE_OMIT_COMPOUND_SELECT
565 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {
566   Select *pRhs = Z;
567   Select *pLhs = A;
568   if( pRhs && pRhs->pPrior ){
569     SrcList *pFrom;
570     Token x;
571     x.n = 0;
572     parserDoubleLinkSelect(pParse, pRhs);
573     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
574     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
575   }
576   if( pRhs ){
577     pRhs->op = (u8)Y;
578     pRhs->pPrior = pLhs;
579     if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
580     pRhs->selFlags &= ~SF_MultiValue;
581     if( Y!=TK_ALL ) pParse->hasCompound = 1;
582   }else{
583     sqlite3SelectDelete(pParse->db, pLhs);
584   }
585   A = pRhs;
586 }
587 %type multiselect_op {int}
588 multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}
589 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
590 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
591 %endif SQLITE_OMIT_COMPOUND_SELECT
592 
593 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
594                  groupby_opt(P) having_opt(Q)
595                  orderby_opt(Z) limit_opt(L). {
596   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
597 }
598 %ifndef SQLITE_OMIT_WINDOWFUNC
599 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
600                  groupby_opt(P) having_opt(Q) window_clause(R)
601                  orderby_opt(Z) limit_opt(L). {
602   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
603   if( A ){
604     A->pWinDefn = R;
605   }else{
606     sqlite3WindowListDelete(pParse->db, R);
607   }
608 }
609 %endif
610 
611 
612 oneselect(A) ::= values(A).
613 
614 %type values {Select*}
615 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
616 values(A) ::= VALUES LP nexprlist(X) RP. {
617   A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
618 }
619 values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
620   Select *pRight, *pLeft = A;
621   pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
622   if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
623   if( pRight ){
624     pRight->op = TK_ALL;
625     pRight->pPrior = pLeft;
626     A = pRight;
627   }else{
628     A = pLeft;
629   }
630 }
631 
632 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
633 // present and false (0) if it is not.
634 //
635 %type distinct {int}
636 distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
637 distinct(A) ::= ALL.        {A = SF_All;}
638 distinct(A) ::= .           {A = 0;}
639 
640 // selcollist is a list of expressions that are to become the return
641 // values of the SELECT statement.  The "*" in statements like
642 // "SELECT * FROM ..." is encoded as a special expression with an
643 // opcode of TK_ASTERISK.
644 //
645 %type selcollist {ExprList*}
646 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
647 %type sclp {ExprList*}
648 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
649 sclp(A) ::= selcollist(A) COMMA.
650 sclp(A) ::= .                                {A = 0;}
651 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y).     {
652    A = sqlite3ExprListAppend(pParse, A, X);
653    if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
654    sqlite3ExprListSetSpan(pParse,A,B,Z);
655 }
656 selcollist(A) ::= sclp(A) scanpt STAR. {
657   Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
658   A = sqlite3ExprListAppend(pParse, A, p);
659 }
660 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
661   Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
662   Expr *pLeft = tokenExpr(pParse, TK_ID, X);
663   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
664   A = sqlite3ExprListAppend(pParse,A, pDot);
665 }
666 
667 // An option "AS <id>" phrase that can follow one of the expressions that
668 // define the result set, or one of the tables in the FROM clause.
669 //
670 %type as {Token}
671 as(X) ::= AS nm(Y).    {X = Y;}
672 as(X) ::= ids(X).
673 as(X) ::= .            {X.n = 0; X.z = 0;}
674 
675 
676 %type seltablist {SrcList*}
677 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
678 %type stl_prefix {SrcList*}
679 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
680 %type from {SrcList*}
681 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
682 
683 // A complete FROM clause.
684 //
685 from(A) ::= .                {A = 0;}
686 from(A) ::= FROM seltablist(X). {
687   A = X;
688   sqlite3SrcListShiftJoinType(pParse,A);
689 }
690 
691 // "seltablist" is a "Select Table List" - the content of the FROM clause
692 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
693 //
694 stl_prefix(A) ::= seltablist(A) joinop(Y).    {
695    if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
696 }
697 stl_prefix(A) ::= .                           {A = 0;}
698 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {
699   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
700 }
701 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {
702   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
703   sqlite3SrcListIndexedBy(pParse, A, &I);
704 }
705 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {
706   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
707   sqlite3SrcListFuncArgs(pParse, A, E);
708 }
709 %ifndef SQLITE_OMIT_SUBQUERY
710   seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {
711     A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);
712   }
713   seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {
714     if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){
715       A = F;
716     }else if( F->nSrc==1 ){
717       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);
718       if( A ){
719         SrcItem *pNew = &A->a[A->nSrc-1];
720         SrcItem *pOld = F->a;
721         pNew->zName = pOld->zName;
722         pNew->zDatabase = pOld->zDatabase;
723         pNew->pSelect = pOld->pSelect;
724         if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
725           pNew->fg.isNestedFrom = 1;
726         }
727         if( pOld->fg.isTabFunc ){
728           pNew->u1.pFuncArg = pOld->u1.pFuncArg;
729           pOld->u1.pFuncArg = 0;
730           pOld->fg.isTabFunc = 0;
731           pNew->fg.isTabFunc = 1;
732         }
733         pOld->zName = pOld->zDatabase = 0;
734         pOld->pSelect = 0;
735       }
736       sqlite3SrcListDelete(pParse->db, F);
737     }else{
738       Select *pSubquery;
739       sqlite3SrcListShiftJoinType(pParse,F);
740       pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
741       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);
742     }
743   }
744 %endif  SQLITE_OMIT_SUBQUERY
745 
746 %type dbnm {Token}
747 dbnm(A) ::= .          {A.z=0; A.n=0;}
748 dbnm(A) ::= DOT nm(X). {A = X;}
749 
750 %type fullname {SrcList*}
751 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
752 fullname(A) ::= nm(X).  {
753   A = sqlite3SrcListAppend(pParse,0,&X,0);
754   if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
755 }
756 fullname(A) ::= nm(X) DOT nm(Y). {
757   A = sqlite3SrcListAppend(pParse,0,&X,&Y);
758   if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
759 }
760 
761 %type xfullname {SrcList*}
762 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
763 xfullname(A) ::= nm(X).
764    {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
765 xfullname(A) ::= nm(X) DOT nm(Y).
766    {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
767 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z).  {
768    A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
769    if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
770 }
771 xfullname(A) ::= nm(X) AS nm(Z). {
772    A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
773    if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
774 }
775 
776 %type joinop {int}
777 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
778 joinop(X) ::= JOIN_KW(A) JOIN.
779                   {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}
780 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
781                   {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
782 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
783                   {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
784 
785 // There is a parsing abiguity in an upsert statement that uses a
786 // SELECT on the RHS of a the INSERT:
787 //
788 //      INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
789 //                                        here ----^^
790 //
791 // When the ON token is encountered, the parser does not know if it is
792 // the beginning of an ON CONFLICT clause, or the beginning of an ON
793 // clause associated with the JOIN.  The conflict is resolved in favor
794 // of the JOIN.  If an ON CONFLICT clause is intended, insert a dummy
795 // WHERE clause in between, like this:
796 //
797 //      INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
798 //
799 // The [AND] and [OR] precedence marks in the rules for on_using cause the
800 // ON in this context to always be interpreted as belonging to the JOIN.
801 //
802 %type on_using {OnOrUsing}
803 //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
804 on_using(N) ::= ON expr(E).            {N.pOn = E; N.pUsing = 0;}
805 on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}
806 on_using(N) ::= .                 [OR] {N.pOn = 0; N.pUsing = 0;}
807 
808 // Note that this block abuses the Token type just a little. If there is
809 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
810 // there is an INDEXED BY clause, then the token is populated as per normal,
811 // with z pointing to the token data and n containing the number of bytes
812 // in the token.
813 //
814 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
815 // normally illegal. The sqlite3SrcListIndexedBy() function
816 // recognizes and interprets this as a special case.
817 //
818 %type indexed_opt {Token}
819 %type indexed_by  {Token}
820 indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
821 indexed_opt(A) ::= indexed_by(A).
822 indexed_by(A)  ::= INDEXED BY nm(X). {A = X;}
823 indexed_by(A)  ::= NOT INDEXED.      {A.z=0; A.n=1;}
824 
825 %type orderby_opt {ExprList*}
826 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
827 
828 // the sortlist non-terminal stores a list of expression where each
829 // expression is optionally followed by ASC or DESC to indicate the
830 // sort order.
831 //
832 %type sortlist {ExprList*}
833 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
834 
835 orderby_opt(A) ::= .                          {A = 0;}
836 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
837 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
838   A = sqlite3ExprListAppend(pParse,A,Y);
839   sqlite3ExprListSetSortOrder(A,Z,X);
840 }
841 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
842   A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
843   sqlite3ExprListSetSortOrder(A,Z,X);
844 }
845 
846 %type sortorder {int}
847 
848 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
849 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
850 sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
851 
852 %type nulls {int}
853 nulls(A) ::= NULLS FIRST.       {A = SQLITE_SO_ASC;}
854 nulls(A) ::= NULLS LAST.        {A = SQLITE_SO_DESC;}
855 nulls(A) ::= .                  {A = SQLITE_SO_UNDEFINED;}
856 
857 %type groupby_opt {ExprList*}
858 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
859 groupby_opt(A) ::= .                      {A = 0;}
860 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
861 
862 %type having_opt {Expr*}
863 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
864 having_opt(A) ::= .                {A = 0;}
865 having_opt(A) ::= HAVING expr(X).  {A = X;}
866 
867 %type limit_opt {Expr*}
868 
869 // The destructor for limit_opt will never fire in the current grammar.
870 // The limit_opt non-terminal only occurs at the end of a single production
871 // rule for SELECT statements.  As soon as the rule that create the
872 // limit_opt non-terminal reduces, the SELECT statement rule will also
873 // reduce.  So there is never a limit_opt non-terminal on the stack
874 // except as a transient.  So there is never anything to destroy.
875 //
876 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
877 limit_opt(A) ::= .       {A = 0;}
878 limit_opt(A) ::= LIMIT expr(X).
879                          {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
880 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
881                          {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
882 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
883                          {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
884 
885 /////////////////////////// The DELETE statement /////////////////////////////
886 //
887 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
888 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
889         orderby_opt(O) limit_opt(L). {
890   sqlite3SrcListIndexedBy(pParse, X, &I);
891 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
892   if( O || L ){
893     updateDeleteLimitError(pParse,O,L);
894     O = 0;
895     L = 0;
896   }
897 #endif
898   sqlite3DeleteFrom(pParse,X,W,O,L);
899 }
900 %else
901 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
902   sqlite3SrcListIndexedBy(pParse, X, &I);
903   sqlite3DeleteFrom(pParse,X,W,0,0);
904 }
905 %endif
906 
907 %type where_opt {Expr*}
908 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
909 %type where_opt_ret {Expr*}
910 %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
911 
912 where_opt(A) ::= .                    {A = 0;}
913 where_opt(A) ::= WHERE expr(X).       {A = X;}
914 where_opt_ret(A) ::= .                                      {A = 0;}
915 where_opt_ret(A) ::= WHERE expr(X).                         {A = X;}
916 where_opt_ret(A) ::= RETURNING selcollist(X).
917        {sqlite3AddReturning(pParse,X); A = 0;}
918 where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
919        {sqlite3AddReturning(pParse,Y); A = X;}
920 
921 ////////////////////////// The UPDATE command ////////////////////////////////
922 //
923 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
924 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
925         where_opt_ret(W) orderby_opt(O) limit_opt(L).  {
926   sqlite3SrcListIndexedBy(pParse, X, &I);
927   X = sqlite3SrcListAppendList(pParse, X, F);
928   sqlite3ExprListCheckLength(pParse,Y,"set list");
929 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
930   if( O || L ){
931     updateDeleteLimitError(pParse,O,L);
932     O = 0;
933     L = 0;
934   }
935 #endif
936   sqlite3Update(pParse,X,Y,W,R,O,L,0);
937 }
938 %else
939 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
940         where_opt_ret(W). {
941   sqlite3SrcListIndexedBy(pParse, X, &I);
942   sqlite3ExprListCheckLength(pParse,Y,"set list");
943   X = sqlite3SrcListAppendList(pParse, X, F);
944   sqlite3Update(pParse,X,Y,W,R,0,0,0);
945 }
946 %endif
947 
948 
949 
950 %type setlist {ExprList*}
951 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
952 
953 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
954   A = sqlite3ExprListAppend(pParse, A, Y);
955   sqlite3ExprListSetName(pParse, A, &X, 1);
956 }
957 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
958   A = sqlite3ExprListAppendVector(pParse, A, X, Y);
959 }
960 setlist(A) ::= nm(X) EQ expr(Y). {
961   A = sqlite3ExprListAppend(pParse, 0, Y);
962   sqlite3ExprListSetName(pParse, A, &X, 1);
963 }
964 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
965   A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
966 }
967 
968 ////////////////////////// The INSERT command /////////////////////////////////
969 //
970 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
971         upsert(U). {
972   sqlite3Insert(pParse, X, S, F, R, U);
973 }
974 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
975 {
976   sqlite3Insert(pParse, X, 0, F, R, 0);
977 }
978 
979 %type upsert {Upsert*}
980 
981 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
982 // there is never a case where the value of the upsert pointer will not
983 // be destroyed by the cmd action.  So comment-out the destructor to
984 // avoid unreachable code.
985 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
986 upsert(A) ::= . { A = 0; }
987 upsert(A) ::= RETURNING selcollist(X).  { A = 0; sqlite3AddReturning(pParse,X); }
988 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
989               DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
990               { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
991 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
992               { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
993 upsert(A) ::= ON CONFLICT DO NOTHING returning.
994               { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
995 upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
996               { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
997 
998 returning ::= RETURNING selcollist(X).  {sqlite3AddReturning(pParse,X);}
999 returning ::= .
1000 
1001 %type insert_cmd {int}
1002 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
1003 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
1004 
1005 %type idlist_opt {IdList*}
1006 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
1007 %type idlist {IdList*}
1008 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
1009 
1010 idlist_opt(A) ::= .                       {A = 0;}
1011 idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
1012 idlist(A) ::= idlist(A) COMMA nm(Y).
1013     {A = sqlite3IdListAppend(pParse,A,&Y);}
1014 idlist(A) ::= nm(Y).
1015     {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
1016 
1017 /////////////////////////// Expression Processing /////////////////////////////
1018 //
1019 
1020 %type expr {Expr*}
1021 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
1022 %type term {Expr*}
1023 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
1024 
1025 %include {
1026 
1027   /* Construct a new Expr object from a single token */
1028   static Expr *tokenExpr(Parse *pParse, int op, Token t){
1029     Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
1030     if( p ){
1031       /* memset(p, 0, sizeof(Expr)); */
1032       p->op = (u8)op;
1033       p->affExpr = 0;
1034       p->flags = EP_Leaf;
1035       ExprClearVVAProperties(p);
1036       /* p->iAgg = -1; // Not required */
1037       p->pLeft = p->pRight = 0;
1038       p->pAggInfo = 0;
1039       memset(&p->x, 0, sizeof(p->x));
1040       memset(&p->y, 0, sizeof(p->y));
1041       p->op2 = 0;
1042       p->iTable = 0;
1043       p->iColumn = 0;
1044       p->u.zToken = (char*)&p[1];
1045       memcpy(p->u.zToken, t.z, t.n);
1046       p->u.zToken[t.n] = 0;
1047       p->w.iOfst = (int)(t.z - pParse->zTail);
1048       if( sqlite3Isquote(p->u.zToken[0]) ){
1049         sqlite3DequoteExpr(p);
1050       }
1051 #if SQLITE_MAX_EXPR_DEPTH>0
1052       p->nHeight = 1;
1053 #endif
1054       if( IN_RENAME_OBJECT ){
1055         return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
1056       }
1057     }
1058     return p;
1059   }
1060 
1061 }
1062 
1063 expr(A) ::= term(A).
1064 expr(A) ::= LP expr(X) RP. {A = X;}
1065 expr(A) ::= id(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1066 expr(A) ::= JOIN_KW(X).     {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1067 expr(A) ::= nm(X) DOT nm(Y). {
1068   Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1069   Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1070   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
1071 }
1072 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
1073   Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1074   Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1075   Expr *temp3 = tokenExpr(pParse,TK_ID,Z);
1076   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
1077   if( IN_RENAME_OBJECT ){
1078     sqlite3RenameTokenRemap(pParse, 0, temp1);
1079   }
1080   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
1081 }
1082 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1083 term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1084 term(A) ::= INTEGER(X). {
1085   A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
1086   if( A ) A->w.iOfst = (int)(X.z - pParse->zTail);
1087 }
1088 expr(A) ::= VARIABLE(X).     {
1089   if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
1090     u32 n = X.n;
1091     A = tokenExpr(pParse, TK_VARIABLE, X);
1092     sqlite3ExprAssignVarNumber(pParse, A, n);
1093   }else{
1094     /* When doing a nested parse, one can include terms in an expression
1095     ** that look like this:   #1 #2 ...  These terms refer to registers
1096     ** in the virtual machine.  #N is the N-th register. */
1097     Token t = X; /*A-overwrites-X*/
1098     assert( t.n>=2 );
1099     if( pParse->nested==0 ){
1100       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1101       A = 0;
1102     }else{
1103       A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1104       if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1105     }
1106   }
1107 }
1108 expr(A) ::= expr(A) COLLATE ids(C). {
1109   A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1110 }
1111 %ifndef SQLITE_OMIT_CAST
1112 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1113   A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1114   sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1115 }
1116 %endif  SQLITE_OMIT_CAST
1117 
1118 
1119 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
1120   A = sqlite3ExprFunction(pParse, Y, &X, D);
1121 }
1122 expr(A) ::= id(X) LP STAR RP. {
1123   A = sqlite3ExprFunction(pParse, 0, &X, 0);
1124 }
1125 
1126 %ifndef SQLITE_OMIT_WINDOWFUNC
1127 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
1128   A = sqlite3ExprFunction(pParse, Y, &X, D);
1129   sqlite3WindowAttach(pParse, A, Z);
1130 }
1131 expr(A) ::= id(X) LP STAR RP filter_over(Z). {
1132   A = sqlite3ExprFunction(pParse, 0, &X, 0);
1133   sqlite3WindowAttach(pParse, A, Z);
1134 }
1135 %endif
1136 
1137 term(A) ::= CTIME_KW(OP). {
1138   A = sqlite3ExprFunction(pParse, 0, &OP, 0);
1139 }
1140 
1141 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1142   ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1143   A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1144   if( A ){
1145     A->x.pList = pList;
1146     if( ALWAYS(pList->nExpr) ){
1147       A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1148     }
1149   }else{
1150     sqlite3ExprListDelete(pParse->db, pList);
1151   }
1152 }
1153 
1154 expr(A) ::= expr(A) AND expr(Y).        {A=sqlite3ExprAnd(pParse,A,Y);}
1155 expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
1156 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1157                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1158 expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
1159 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1160                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1161 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1162                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1163 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1164                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1165 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1166 %type likeop {Token}
1167 likeop(A) ::= LIKE_KW|MATCH(A).
1168 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1169 expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
1170   ExprList *pList;
1171   int bNot = OP.n & 0x80000000;
1172   OP.n &= 0x7fffffff;
1173   pList = sqlite3ExprListAppend(pParse,0, Y);
1174   pList = sqlite3ExprListAppend(pParse,pList, A);
1175   A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1176   if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1177   if( A ) A->flags |= EP_InfixFunc;
1178 }
1179 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
1180   ExprList *pList;
1181   int bNot = OP.n & 0x80000000;
1182   OP.n &= 0x7fffffff;
1183   pList = sqlite3ExprListAppend(pParse,0, Y);
1184   pList = sqlite3ExprListAppend(pParse,pList, A);
1185   pList = sqlite3ExprListAppend(pParse,pList, E);
1186   A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1187   if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1188   if( A ) A->flags |= EP_InfixFunc;
1189 }
1190 
1191 expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {A = sqlite3PExpr(pParse,@E,A,0);}
1192 expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1193 
1194 %include {
1195   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1196   ** unary TK_ISNULL or TK_NOTNULL expression. */
1197   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1198     sqlite3 *db = pParse->db;
1199     if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
1200       pA->op = (u8)op;
1201       sqlite3ExprDelete(db, pA->pRight);
1202       pA->pRight = 0;
1203     }
1204   }
1205 }
1206 
1207 //    expr1 IS expr2
1208 //    expr1 IS NOT expr2
1209 //
1210 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
1211 // is any other expression, code as TK_IS or TK_ISNOT.
1212 //
1213 expr(A) ::= expr(A) IS expr(Y).     {
1214   A = sqlite3PExpr(pParse,TK_IS,A,Y);
1215   binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1216 }
1217 expr(A) ::= expr(A) IS NOT expr(Y). {
1218   A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1219   binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1220 }
1221 expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y).     {
1222   A = sqlite3PExpr(pParse,TK_IS,A,Y);
1223   binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1224 }
1225 expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
1226   A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1227   binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1228 }
1229 
1230 expr(A) ::= NOT(B) expr(X).
1231               {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1232 expr(A) ::= BITNOT(B) expr(X).
1233               {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1234 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1235   A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1236   /*A-overwrites-B*/
1237 }
1238 
1239 expr(A) ::= expr(B) PTR(C) expr(D). {
1240   ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
1241   pList = sqlite3ExprListAppend(pParse, pList, D);
1242   A = sqlite3ExprFunction(pParse, pList, &C, 0);
1243 }
1244 
1245 %type between_op {int}
1246 between_op(A) ::= BETWEEN.     {A = 0;}
1247 between_op(A) ::= NOT BETWEEN. {A = 1;}
1248 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1249   ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1250   pList = sqlite3ExprListAppend(pParse,pList, Y);
1251   A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1252   if( A ){
1253     A->x.pList = pList;
1254   }else{
1255     sqlite3ExprListDelete(pParse->db, pList);
1256   }
1257   if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1258 }
1259 %ifndef SQLITE_OMIT_SUBQUERY
1260   %type in_op {int}
1261   in_op(A) ::= IN.      {A = 0;}
1262   in_op(A) ::= NOT IN.  {A = 1;}
1263   expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1264     if( Y==0 ){
1265       /* Expressions of the form
1266       **
1267       **      expr1 IN ()
1268       **      expr1 NOT IN ()
1269       **
1270       ** simplify to constants 0 (false) and 1 (true), respectively,
1271       ** regardless of the value of expr1.
1272       */
1273       sqlite3ExprUnmapAndDelete(pParse, A);
1274       A = sqlite3Expr(pParse->db, TK_INTEGER, N ? "1" : "0");
1275     }else{
1276       Expr *pRHS = Y->a[0].pExpr;
1277       if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){
1278         Y->a[0].pExpr = 0;
1279         sqlite3ExprListDelete(pParse->db, Y);
1280         pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
1281         A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1282       }else{
1283         A = sqlite3PExpr(pParse, TK_IN, A, 0);
1284         if( A==0 ){
1285           sqlite3ExprListDelete(pParse->db, Y);
1286         }else if( A->pLeft->op==TK_VECTOR ){
1287           int nExpr = A->pLeft->x.pList->nExpr;
1288           Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
1289           if( pSelectRHS ){
1290             parserDoubleLinkSelect(pParse, pSelectRHS);
1291             sqlite3PExprAddSelect(pParse, A, pSelectRHS);
1292           }
1293         }else{
1294           A->x.pList = Y;
1295           sqlite3ExprSetHeightAndFlags(pParse, A);
1296         }
1297       }
1298       if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1299     }
1300   }
1301   expr(A) ::= LP select(X) RP. {
1302     A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1303     sqlite3PExprAddSelect(pParse, A, X);
1304   }
1305   expr(A) ::= expr(A) in_op(N) LP select(Y) RP.  [IN] {
1306     A = sqlite3PExpr(pParse, TK_IN, A, 0);
1307     sqlite3PExprAddSelect(pParse, A, Y);
1308     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1309   }
1310   expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1311     SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
1312     Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1313     if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1314     A = sqlite3PExpr(pParse, TK_IN, A, 0);
1315     sqlite3PExprAddSelect(pParse, A, pSelect);
1316     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1317   }
1318   expr(A) ::= EXISTS LP select(Y) RP. {
1319     Expr *p;
1320     p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1321     sqlite3PExprAddSelect(pParse, p, Y);
1322   }
1323 %endif SQLITE_OMIT_SUBQUERY
1324 
1325 /* CASE expressions */
1326 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1327   A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1328   if( A ){
1329     A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1330     sqlite3ExprSetHeightAndFlags(pParse, A);
1331   }else{
1332     sqlite3ExprListDelete(pParse->db, Y);
1333     sqlite3ExprDelete(pParse->db, Z);
1334   }
1335 }
1336 %type case_exprlist {ExprList*}
1337 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1338 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1339   A = sqlite3ExprListAppend(pParse,A, Y);
1340   A = sqlite3ExprListAppend(pParse,A, Z);
1341 }
1342 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1343   A = sqlite3ExprListAppend(pParse,0, Y);
1344   A = sqlite3ExprListAppend(pParse,A, Z);
1345 }
1346 %type case_else {Expr*}
1347 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1348 case_else(A) ::=  ELSE expr(X).         {A = X;}
1349 case_else(A) ::=  .                     {A = 0;}
1350 %type case_operand {Expr*}
1351 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1352 case_operand(A) ::= expr(X).            {A = X; /*A-overwrites-X*/}
1353 case_operand(A) ::= .                   {A = 0;}
1354 
1355 %type exprlist {ExprList*}
1356 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1357 %type nexprlist {ExprList*}
1358 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1359 
1360 exprlist(A) ::= nexprlist(A).
1361 exprlist(A) ::= .                            {A = 0;}
1362 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1363     {A = sqlite3ExprListAppend(pParse,A,Y);}
1364 nexprlist(A) ::= expr(Y).
1365     {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1366 
1367 %ifndef SQLITE_OMIT_SUBQUERY
1368 /* A paren_exprlist is an optional expression list contained inside
1369 ** of parenthesis */
1370 %type paren_exprlist {ExprList*}
1371 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1372 paren_exprlist(A) ::= .   {A = 0;}
1373 paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
1374 %endif SQLITE_OMIT_SUBQUERY
1375 
1376 
1377 ///////////////////////////// The CREATE INDEX command ///////////////////////
1378 //
1379 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1380         ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1381   sqlite3CreateIndex(pParse, &X, &D,
1382                      sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
1383                       &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1384   if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1385     sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1386   }
1387 }
1388 
1389 %type uniqueflag {int}
1390 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
1391 uniqueflag(A) ::= .        {A = OE_None;}
1392 
1393 
1394 // The eidlist non-terminal (Expression Id List) generates an ExprList
1395 // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
1396 // This list is stored in an ExprList rather than an IdList so that it
1397 // can be easily sent to sqlite3ColumnsExprList().
1398 //
1399 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1400 // used for the arguments to an index.  That is just an historical accident.
1401 //
1402 // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
1403 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1404 // places - places that might have been stored in the sqlite_schema table.
1405 // Those extra features were ignored.  But because they might be in some
1406 // (busted) old databases, we need to continue parsing them when loading
1407 // historical schemas.
1408 //
1409 %type eidlist {ExprList*}
1410 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1411 %type eidlist_opt {ExprList*}
1412 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1413 
1414 %include {
1415   /* Add a single new term to an ExprList that is used to store a
1416   ** list of identifiers.  Report an error if the ID list contains
1417   ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1418   ** error while parsing a legacy schema.
1419   */
1420   static ExprList *parserAddExprIdListTerm(
1421     Parse *pParse,
1422     ExprList *pPrior,
1423     Token *pIdToken,
1424     int hasCollate,
1425     int sortOrder
1426   ){
1427     ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1428     if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1429         && pParse->db->init.busy==0
1430     ){
1431       sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1432                          pIdToken->n, pIdToken->z);
1433     }
1434     sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1435     return p;
1436   }
1437 } // end %include
1438 
1439 eidlist_opt(A) ::= .                         {A = 0;}
1440 eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
1441 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
1442   A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1443 }
1444 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1445   A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1446 }
1447 
1448 %type collate {int}
1449 collate(C) ::= .              {C = 0;}
1450 collate(C) ::= COLLATE ids.   {C = 1;}
1451 
1452 
1453 ///////////////////////////// The DROP INDEX command /////////////////////////
1454 //
1455 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
1456 
1457 ///////////////////////////// The VACUUM command /////////////////////////////
1458 //
1459 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
1460 %type vinto {Expr*}
1461 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1462 cmd ::= VACUUM vinto(Y).                {sqlite3Vacuum(pParse,0,Y);}
1463 cmd ::= VACUUM nm(X) vinto(Y).          {sqlite3Vacuum(pParse,&X,Y);}
1464 vinto(A) ::= INTO expr(X).              {A = X;}
1465 vinto(A) ::= .                          {A = 0;}
1466 %endif
1467 
1468 ///////////////////////////// The PRAGMA command /////////////////////////////
1469 //
1470 %ifndef SQLITE_OMIT_PRAGMA
1471 cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
1472 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1473 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1474 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1475                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1476 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1477                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1478 
1479 nmnum(A) ::= plus_num(A).
1480 nmnum(A) ::= nm(A).
1481 nmnum(A) ::= ON(A).
1482 nmnum(A) ::= DELETE(A).
1483 nmnum(A) ::= DEFAULT(A).
1484 %endif SQLITE_OMIT_PRAGMA
1485 %token_class number INTEGER|FLOAT.
1486 plus_num(A) ::= PLUS number(X).       {A = X;}
1487 plus_num(A) ::= number(A).
1488 minus_num(A) ::= MINUS number(X).     {A = X;}
1489 //////////////////////////// The CREATE TRIGGER command /////////////////////
1490 
1491 %ifndef SQLITE_OMIT_TRIGGER
1492 
1493 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1494   Token all;
1495   all.z = A.z;
1496   all.n = (int)(Z.z - A.z) + Z.n;
1497   sqlite3FinishTrigger(pParse, S, &all);
1498 }
1499 
1500 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1501                     trigger_time(C) trigger_event(D)
1502                     ON fullname(E) foreach_clause when_clause(G). {
1503   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1504   A = (Z.n==0?B:Z); /*A-overwrites-T*/
1505 }
1506 
1507 %type trigger_time {int}
1508 trigger_time(A) ::= BEFORE|AFTER(X).  { A = @X; /*A-overwrites-X*/ }
1509 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1510 trigger_time(A) ::= .            { A = TK_BEFORE; }
1511 
1512 %type trigger_event {struct TrigEvent}
1513 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1514 trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1515 trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1516 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1517 
1518 foreach_clause ::= .
1519 foreach_clause ::= FOR EACH ROW.
1520 
1521 %type when_clause {Expr*}
1522 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1523 when_clause(A) ::= .             { A = 0; }
1524 when_clause(A) ::= WHEN expr(X). { A = X; }
1525 
1526 %type trigger_cmd_list {TriggerStep*}
1527 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1528 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1529   assert( A!=0 );
1530   A->pLast->pNext = X;
1531   A->pLast = X;
1532 }
1533 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1534   assert( A!=0 );
1535   A->pLast = A;
1536 }
1537 
1538 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1539 // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in
1540 // the same database as the table that the trigger fires on.
1541 //
1542 %type trnm {Token}
1543 trnm(A) ::= nm(A).
1544 trnm(A) ::= nm DOT nm(X). {
1545   A = X;
1546   sqlite3ErrorMsg(pParse,
1547         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1548         "statements within triggers");
1549 }
1550 
1551 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1552 // statements within triggers.  We make a specific error message for this
1553 // since it is an exception to the default grammar rules.
1554 //
1555 tridxby ::= .
1556 tridxby ::= INDEXED BY nm. {
1557   sqlite3ErrorMsg(pParse,
1558         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1559         "within triggers");
1560 }
1561 tridxby ::= NOT INDEXED. {
1562   sqlite3ErrorMsg(pParse,
1563         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1564         "within triggers");
1565 }
1566 
1567 
1568 
1569 %type trigger_cmd {TriggerStep*}
1570 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1571 // UPDATE
1572 trigger_cmd(A) ::=
1573    UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1574    {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
1575 
1576 // INSERT
1577 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1578                       trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1579    A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1580 }
1581 // DELETE
1582 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1583    {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
1584 
1585 // SELECT
1586 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1587    {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1588 
1589 // The special RAISE expression that may occur in trigger programs
1590 expr(A) ::= RAISE LP IGNORE RP.  {
1591   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1592   if( A ){
1593     A->affExpr = OE_Ignore;
1594   }
1595 }
1596 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
1597   A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1598   if( A ) {
1599     A->affExpr = (char)T;
1600   }
1601 }
1602 %endif  !SQLITE_OMIT_TRIGGER
1603 
1604 %type raisetype {int}
1605 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1606 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1607 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1608 
1609 
1610 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1611 %ifndef SQLITE_OMIT_TRIGGER
1612 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1613   sqlite3DropTrigger(pParse,X,NOERR);
1614 }
1615 %endif  !SQLITE_OMIT_TRIGGER
1616 
1617 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1618 %ifndef SQLITE_OMIT_ATTACH
1619 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1620   sqlite3Attach(pParse, F, D, K);
1621 }
1622 cmd ::= DETACH database_kw_opt expr(D). {
1623   sqlite3Detach(pParse, D);
1624 }
1625 
1626 %type key_opt {Expr*}
1627 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1628 key_opt(A) ::= .                     { A = 0; }
1629 key_opt(A) ::= KEY expr(X).          { A = X; }
1630 
1631 database_kw_opt ::= DATABASE.
1632 database_kw_opt ::= .
1633 %endif SQLITE_OMIT_ATTACH
1634 
1635 ////////////////////////// REINDEX collation //////////////////////////////////
1636 %ifndef SQLITE_OMIT_REINDEX
1637 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1638 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1639 %endif  SQLITE_OMIT_REINDEX
1640 
1641 /////////////////////////////////// ANALYZE ///////////////////////////////////
1642 %ifndef SQLITE_OMIT_ANALYZE
1643 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1644 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1645 %endif
1646 
1647 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1648 %ifndef SQLITE_OMIT_ALTERTABLE
1649 %ifndef SQLITE_OMIT_VIRTUALTABLE
1650 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1651   sqlite3AlterRenameTable(pParse,X,&Z);
1652 }
1653 cmd ::= ALTER TABLE add_column_fullname
1654         ADD kwcolumn_opt columnname(Y) carglist. {
1655   Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1656   sqlite3AlterFinishAddColumn(pParse, &Y);
1657 }
1658 cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
1659   sqlite3AlterDropColumn(pParse, X, &Y);
1660 }
1661 
1662 add_column_fullname ::= fullname(X). {
1663   disableLookaside(pParse);
1664   sqlite3AlterBeginAddColumn(pParse, X);
1665 }
1666 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1667   sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1668 }
1669 
1670 kwcolumn_opt ::= .
1671 kwcolumn_opt ::= COLUMNKW.
1672 
1673 %endif SQLITE_OMIT_VIRTUALTABLE
1674 %endif SQLITE_OMIT_ALTERTABLE
1675 
1676 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1677 %ifndef SQLITE_OMIT_VIRTUALTABLE
1678 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1679 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1680 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1681                 nm(X) dbnm(Y) USING nm(Z). {
1682     sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1683 }
1684 vtabarglist ::= vtabarg.
1685 vtabarglist ::= vtabarglist COMMA vtabarg.
1686 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1687 vtabarg ::= vtabarg vtabargtoken.
1688 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1689 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1690 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1691 anylist ::= .
1692 anylist ::= anylist LP anylist RP.
1693 anylist ::= anylist ANY.
1694 %endif  SQLITE_OMIT_VIRTUALTABLE
1695 
1696 
1697 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1698 %type wqlist {With*}
1699 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1700 %type wqitem {Cte*}
1701 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
1702 
1703 with ::= .
1704 %ifndef SQLITE_OMIT_CTE
1705 with ::= WITH wqlist(W).              { sqlite3WithPush(pParse, W, 1); }
1706 with ::= WITH RECURSIVE wqlist(W).    { sqlite3WithPush(pParse, W, 1); }
1707 
1708 %type wqas {u8}
1709 wqas(A)   ::= AS.                  {A = M10d_Any;}
1710 wqas(A)   ::= AS MATERIALIZED.     {A = M10d_Yes;}
1711 wqas(A)   ::= AS NOT MATERIALIZED. {A = M10d_No;}
1712 wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
1713   A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
1714 }
1715 wqlist(A) ::= wqitem(X). {
1716   A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
1717 }
1718 wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
1719   A = sqlite3WithAdd(pParse, A, X);
1720 }
1721 %endif  SQLITE_OMIT_CTE
1722 
1723 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1724 // These must be at the end of this file. Specifically, the rules that
1725 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1726 // the integer values assigned to these tokens to be larger than all other
1727 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
1728 //
1729 %ifndef SQLITE_OMIT_WINDOWFUNC
1730 %type windowdefn_list {Window*}
1731 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
1732 windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1733 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1734   assert( Z!=0 );
1735   sqlite3WindowChain(pParse, Z, Y);
1736   Z->pNextWin = Y;
1737   A = Z;
1738 }
1739 
1740 %type windowdefn {Window*}
1741 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1742 windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
1743   if( ALWAYS(Y) ){
1744     Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1745   }
1746   A = Y;
1747 }
1748 
1749 %type window {Window*}
1750 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1751 
1752 %type frame_opt {Window*}
1753 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1754 
1755 %type part_opt {ExprList*}
1756 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1757 
1758 %type filter_clause {Expr*}
1759 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1760 
1761 %type over_clause {Window*}
1762 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
1763 
1764 %type filter_over {Window*}
1765 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1766 
1767 %type range_or_rows {int}
1768 
1769 %type frame_bound {struct FrameBound}
1770 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1771 %type frame_bound_s {struct FrameBound}
1772 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1773 %type frame_bound_e {struct FrameBound}
1774 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1775 
1776 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1777   A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1778 }
1779 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1780   A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1781 }
1782 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1783   A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1784 }
1785 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1786   A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1787 }
1788 window(A) ::= frame_opt(Z). {
1789   A = Z;
1790 }
1791 window(A) ::= nm(W) frame_opt(Z). {
1792   A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
1793 }
1794 
1795 frame_opt(A) ::= .                             {
1796   A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
1797 }
1798 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1799   A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
1800 }
1801 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1802                           frame_bound_e(Z) frame_exclude_opt(W). {
1803   A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
1804 }
1805 
1806 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X).   {A = @X; /*A-overwrites-X*/}
1807 
1808 frame_bound_s(A) ::= frame_bound(X).         {A = X;}
1809 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1810 frame_bound_e(A) ::= frame_bound(X).         {A = X;}
1811 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
1812 
1813 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1814                                              {A.eType = @Y; A.pExpr = X;}
1815 frame_bound(A) ::= CURRENT(X) ROW.           {A.eType = @X; A.pExpr = 0;}
1816 
1817 %type frame_exclude_opt {u8}
1818 frame_exclude_opt(A) ::= . {A = 0;}
1819 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
1820 
1821 %type frame_exclude {u8}
1822 frame_exclude(A) ::= NO(X) OTHERS.   {A = @X; /*A-overwrites-X*/}
1823 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1824 frame_exclude(A) ::= GROUP|TIES(X).  {A = @X; /*A-overwrites-X*/}
1825 
1826 
1827 %type window_clause {Window*}
1828 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1829 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
1830 
1831 filter_over(A) ::= filter_clause(F) over_clause(O). {
1832   if( O ){
1833     O->pFilter = F;
1834   }else{
1835     sqlite3ExprDelete(pParse->db, F);
1836   }
1837   A = O;
1838 }
1839 filter_over(A) ::= over_clause(O). {
1840   A = O;
1841 }
1842 filter_over(A) ::= filter_clause(F). {
1843   A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1844   if( A ){
1845     A->eFrmType = TK_FILTER;
1846     A->pFilter = F;
1847   }else{
1848     sqlite3ExprDelete(pParse->db, F);
1849   }
1850 }
1851 
1852 over_clause(A) ::= OVER LP window(Z) RP. {
1853   A = Z;
1854   assert( A!=0 );
1855 }
1856 over_clause(A) ::= OVER nm(Z). {
1857   A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1858   if( A ){
1859     A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1860   }
1861 }
1862 
1863 filter_clause(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
1864 %endif /* SQLITE_OMIT_WINDOWFUNC */
1865 
1866 /*
1867 ** The code generator needs some extra TK_ token values for tokens that
1868 ** are synthesized and do not actually appear in the grammar:
1869 */
1870 %token
1871   COLUMN          /* Reference to a table column */
1872   AGG_FUNCTION    /* An aggregate function */
1873   AGG_COLUMN      /* An aggregated column */
1874   TRUEFALSE       /* True or false keyword */
1875   ISNOT           /* Combination of IS and NOT */
1876   FUNCTION        /* A function invocation */
1877   UMINUS          /* Unary minus */
1878   UPLUS           /* Unary plus */
1879   TRUTH           /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1880   REGISTER        /* Reference to a VDBE register */
1881   VECTOR          /* Vector */
1882   SELECT_COLUMN   /* Choose a single column from a multi-column SELECT */
1883   IF_NULL_ROW     /* the if-null-row operator */
1884   ASTERISK        /* The "*" in count(*) and similar */
1885   SPAN            /* The span operator */
1886   ERROR           /* An expression containing an error */
1887 .
1888 /* There must be no more than 255 tokens defined above.  If this grammar
1889 ** is extended with new rules and tokens, they must either be so few in
1890 ** number that TK_SPAN is no more than 255, or else the new tokens must
1891 ** appear after this line.
1892 */
1893 %include {
1894 #if TK_SPAN>255
1895 # error too many tokens in the grammar
1896 #endif
1897 }
1898 
1899 /*
1900 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens.  The
1901 ** parser depends on this.  Those tokens are not used in any grammar rule.
1902 ** They are only used by the tokenizer.  Declare them last so that they
1903 ** are guaranteed to be the last two tokens
1904 */
1905 %token SPACE ILLEGAL.
1906