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