xref: /sqlite-3.40.0/src/parse.y (revision c023e03e)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains SQLite's grammar for SQL.  Process this file
13 ** using the lemon parser generator to generate C code that runs
14 ** the parser.  Lemon will also generate a header file containing
15 ** numeric codes for all of the tokens.
16 **
17 ** @(#) $Id: parse.y,v 1.99 2003/07/16 02:19:38 drh Exp $
18 */
19 %token_prefix TK_
20 %token_type {Token}
21 %default_type {Token}
22 %extra_argument {Parse *pParse}
23 %syntax_error {
24   if( pParse->zErrMsg==0 ){
25     if( TOKEN.z[0] ){
26       sqliteSetNString(&pParse->zErrMsg,
27           "near \"", -1, TOKEN.z, TOKEN.n, "\": syntax error", -1, 0);
28     }else{
29       sqliteSetString(&pParse->zErrMsg, "incomplete SQL statement", 0);
30     }
31   }
32   pParse->nErr++;
33 }
34 %name sqliteParser
35 %include {
36 #include "sqliteInt.h"
37 #include "parse.h"
38 
39 /*
40 ** An instance of this structure holds information about the
41 ** LIMIT clause of a SELECT statement.
42 */
43 struct LimitVal {
44   int limit;    /* The LIMIT value.  -1 if there is no limit */
45   int offset;   /* The OFFSET.  0 if there is none */
46 };
47 
48 /*
49 ** An instance of the following structure describes the event of a
50 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
51 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
52 **
53 **      UPDATE ON (a,b,c)
54 **
55 ** Then the "b" IdList records the list "a,b,c".
56 */
57 struct TrigEvent { int a; IdList * b; };
58 
59 } // end %include
60 
61 // These are extra tokens used by the lexer but never seen by the
62 // parser.  We put them in a rule so that the parser generator will
63 // add them to the parse.h output file.
64 //
65 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
66           COLUMN AGG_FUNCTION.
67 
68 // Input is zero or more commands.
69 input ::= cmdlist.
70 
71 // A list of commands is zero or more commands
72 //
73 cmdlist ::= ecmd.
74 cmdlist ::= cmdlist ecmd.
75 ecmd ::= explain cmdx SEMI.
76 ecmd ::= SEMI.
77 cmdx ::= cmd.           { sqliteExec(pParse); }
78 explain ::= EXPLAIN.    { sqliteBeginParse(pParse, 1); }
79 explain ::= .           { sqliteBeginParse(pParse, 0); }
80 
81 ///////////////////// Begin and end transactions. ////////////////////////////
82 //
83 
84 cmd ::= BEGIN trans_opt onconf(R).  {sqliteBeginTransaction(pParse,R);}
85 trans_opt ::= .
86 trans_opt ::= TRANSACTION.
87 trans_opt ::= TRANSACTION nm.
88 cmd ::= COMMIT trans_opt.      {sqliteCommitTransaction(pParse);}
89 cmd ::= END trans_opt.         {sqliteCommitTransaction(pParse);}
90 cmd ::= ROLLBACK trans_opt.    {sqliteRollbackTransaction(pParse);}
91 
92 ///////////////////// The CREATE TABLE statement ////////////////////////////
93 //
94 cmd ::= create_table create_table_args.
95 create_table ::= CREATE(X) temp(T) TABLE nm(Y). {
96    sqliteStartTable(pParse,&X,&Y,T,0);
97 }
98 %type temp {int}
99 temp(A) ::= TEMP.  {A = 1;}
100 temp(A) ::= .      {A = 0;}
101 create_table_args ::= LP columnlist conslist_opt RP(X). {
102   sqliteEndTable(pParse,&X,0);
103 }
104 create_table_args ::= AS select(S). {
105   sqliteEndTable(pParse,0,S);
106   sqliteSelectDelete(S);
107 }
108 columnlist ::= columnlist COMMA column.
109 columnlist ::= column.
110 
111 // About the only information used for a column is the name of the
112 // column.  The type is always just "text".  But the code will accept
113 // an elaborate typename.  Perhaps someday we'll do something with it.
114 //
115 column ::= columnid type carglist.
116 columnid ::= nm(X).                {sqliteAddColumn(pParse,&X);}
117 
118 // An IDENTIFIER can be a generic identifier, or one of several
119 // keywords.  Any non-standard keyword can also be an identifier.
120 //
121 %type id {Token}
122 id(A) ::= ID(X).         {A = X;}
123 
124 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
125 // fallback to ID if they will not parse as their original value.
126 // This obviates the need for the "id" nonterminal.
127 //
128 %fallback ID
129   ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT
130   COPY DATABASE DEFERRED DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR
131   IGNORE IMMEDIATE INITIALLY INSTEAD MATCH KEY
132   OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
133   TEMP TRIGGER VACUUM VIEW.
134 
135 // And "ids" is an identifer-or-string.
136 //
137 %type ids {Token}
138 ids(A) ::= ID(X).        {A = X;}
139 ids(A) ::= STRING(X).    {A = X;}
140 
141 // The name of a column or table can be any of the following:
142 //
143 %type nm {Token}
144 nm(A) ::= ID(X).         {A = X;}
145 nm(A) ::= STRING(X).     {A = X;}
146 nm(A) ::= JOIN_KW(X).    {A = X;}
147 
148 type ::= .
149 type ::= typename(X).                    {sqliteAddColumnType(pParse,&X,&X);}
150 type ::= typename(X) LP signed RP(Y).    {sqliteAddColumnType(pParse,&X,&Y);}
151 type ::= typename(X) LP signed COMMA signed RP(Y).
152                                          {sqliteAddColumnType(pParse,&X,&Y);}
153 %type typename {Token}
154 typename(A) ::= ids(X).           {A = X;}
155 typename(A) ::= typename(X) ids.  {A = X;}
156 %type signed {int}
157 signed(A) ::= INTEGER(X).         { A = atoi(X.z); }
158 signed(A) ::= PLUS INTEGER(X).    { A = atoi(X.z); }
159 signed(A) ::= MINUS INTEGER(X).   { A = -atoi(X.z); }
160 carglist ::= carglist carg.
161 carglist ::= .
162 carg ::= CONSTRAINT nm ccons.
163 carg ::= ccons.
164 carg ::= DEFAULT STRING(X).          {sqliteAddDefaultValue(pParse,&X,0);}
165 carg ::= DEFAULT ID(X).              {sqliteAddDefaultValue(pParse,&X,0);}
166 carg ::= DEFAULT INTEGER(X).         {sqliteAddDefaultValue(pParse,&X,0);}
167 carg ::= DEFAULT PLUS INTEGER(X).    {sqliteAddDefaultValue(pParse,&X,0);}
168 carg ::= DEFAULT MINUS INTEGER(X).   {sqliteAddDefaultValue(pParse,&X,1);}
169 carg ::= DEFAULT FLOAT(X).           {sqliteAddDefaultValue(pParse,&X,0);}
170 carg ::= DEFAULT PLUS FLOAT(X).      {sqliteAddDefaultValue(pParse,&X,0);}
171 carg ::= DEFAULT MINUS FLOAT(X).     {sqliteAddDefaultValue(pParse,&X,1);}
172 carg ::= DEFAULT NULL.
173 
174 // In addition to the type name, we also care about the primary key and
175 // UNIQUE constraints.
176 //
177 ccons ::= NULL onconf.
178 ccons ::= NOT NULL onconf(R).               {sqliteAddNotNull(pParse, R);}
179 ccons ::= PRIMARY KEY sortorder onconf(R).  {sqliteAddPrimaryKey(pParse,0,R);}
180 ccons ::= UNIQUE onconf(R).           {sqliteCreateIndex(pParse,0,0,0,R,0,0,0);}
181 ccons ::= CHECK LP expr RP onconf.
182 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
183                                 {sqliteCreateForeignKey(pParse,0,&T,TA,R);}
184 ccons ::= defer_subclause(D).   {sqliteDeferForeignKey(pParse,D);}
185 ccons ::= COLLATE id(C).  {
186    sqliteAddCollateType(pParse, sqliteCollateType(C.z, C.n));
187 }
188 
189 // The next group of rules parses the arguments to a REFERENCES clause
190 // that determine if the referential integrity checking is deferred or
191 // or immediate and which determine what action to take if a ref-integ
192 // check fails.
193 //
194 %type refargs {int}
195 refargs(A) ::= .                     { A = OE_Restrict * 0x010101; }
196 refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
197 %type refarg {struct {int value; int mask;}}
198 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
199 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
200 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
201 refarg(A) ::= ON INSERT refact(X).   { A.value = X<<16; A.mask = 0xff0000; }
202 %type refact {int}
203 refact(A) ::= SET NULL.              { A = OE_SetNull; }
204 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt; }
205 refact(A) ::= CASCADE.               { A = OE_Cascade; }
206 refact(A) ::= RESTRICT.              { A = OE_Restrict; }
207 %type defer_subclause {int}
208 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X).  {A = X;}
209 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
210 %type init_deferred_pred_opt {int}
211 init_deferred_pred_opt(A) ::= .                       {A = 0;}
212 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
213 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
214 
215 // For the time being, the only constraint we care about is the primary
216 // key and UNIQUE.  Both create indices.
217 //
218 conslist_opt ::= .
219 conslist_opt ::= COMMA conslist.
220 conslist ::= conslist COMMA tcons.
221 conslist ::= conslist tcons.
222 conslist ::= tcons.
223 tcons ::= CONSTRAINT nm.
224 tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
225                                              {sqliteAddPrimaryKey(pParse,X,R);}
226 tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
227                                      {sqliteCreateIndex(pParse,0,0,X,R,0,0,0);}
228 tcons ::= CHECK expr onconf.
229 tcons ::= FOREIGN KEY LP idxlist(FA) RP
230           REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
231     sqliteCreateForeignKey(pParse, FA, &T, TA, R);
232     sqliteDeferForeignKey(pParse, D);
233 }
234 %type defer_subclause_opt {int}
235 defer_subclause_opt(A) ::= .                    {A = 0;}
236 defer_subclause_opt(A) ::= defer_subclause(X).  {A = X;}
237 
238 // The following is a non-standard extension that allows us to declare the
239 // default behavior when there is a constraint conflict.
240 //
241 %type onconf {int}
242 %type orconf {int}
243 %type resolvetype {int}
244 onconf(A) ::= .                              { A = OE_Default; }
245 onconf(A) ::= ON CONFLICT resolvetype(X).    { A = X; }
246 orconf(A) ::= .                              { A = OE_Default; }
247 orconf(A) ::= OR resolvetype(X).             { A = X; }
248 resolvetype(A) ::= ROLLBACK.                 { A = OE_Rollback; }
249 resolvetype(A) ::= ABORT.                    { A = OE_Abort; }
250 resolvetype(A) ::= FAIL.                     { A = OE_Fail; }
251 resolvetype(A) ::= IGNORE.                   { A = OE_Ignore; }
252 resolvetype(A) ::= REPLACE.                  { A = OE_Replace; }
253 
254 ////////////////////////// The DROP TABLE /////////////////////////////////////
255 //
256 cmd ::= DROP TABLE nm(X).          {sqliteDropTable(pParse,&X,0);}
257 
258 ///////////////////// The CREATE VIEW statement /////////////////////////////
259 //
260 cmd ::= CREATE(X) temp(T) VIEW nm(Y) AS select(S). {
261   sqliteCreateView(pParse, &X, &Y, S, T);
262 }
263 cmd ::= DROP VIEW nm(X). {
264   sqliteDropTable(pParse, &X, 1);
265 }
266 
267 //////////////////////// The SELECT statement /////////////////////////////////
268 //
269 cmd ::= select(X).  {
270   sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
271   sqliteSelectDelete(X);
272 }
273 
274 %type select {Select*}
275 %destructor select {sqliteSelectDelete($$);}
276 %type oneselect {Select*}
277 %destructor oneselect {sqliteSelectDelete($$);}
278 
279 select(A) ::= oneselect(X).                      {A = X;}
280 select(A) ::= select(X) multiselect_op(Y) oneselect(Z).  {
281   if( Z ){
282     Z->op = Y;
283     Z->pPrior = X;
284   }
285   A = Z;
286 }
287 %type multiselect_op {int}
288 multiselect_op(A) ::= UNION.      {A = TK_UNION;}
289 multiselect_op(A) ::= UNION ALL.  {A = TK_ALL;}
290 multiselect_op(A) ::= INTERSECT.  {A = TK_INTERSECT;}
291 multiselect_op(A) ::= EXCEPT.     {A = TK_EXCEPT;}
292 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
293                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
294   A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.limit,L.offset);
295 }
296 
297 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
298 // present and false (0) if it is not.
299 //
300 %type distinct {int}
301 distinct(A) ::= DISTINCT.   {A = 1;}
302 distinct(A) ::= ALL.        {A = 0;}
303 distinct(A) ::= .           {A = 0;}
304 
305 // selcollist is a list of expressions that are to become the return
306 // values of the SELECT statement.  The "*" in statements like
307 // "SELECT * FROM ..." is encoded as a special expression with an
308 // opcode of TK_ALL.
309 //
310 %type selcollist {ExprList*}
311 %destructor selcollist {sqliteExprListDelete($$);}
312 %type sclp {ExprList*}
313 %destructor sclp {sqliteExprListDelete($$);}
314 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
315 sclp(A) ::= .                                {A = 0;}
316 selcollist(A) ::= sclp(P) expr(X) as(Y).     {
317    A = sqliteExprListAppend(P,X,Y.n?&Y:0);
318 }
319 selcollist(A) ::= sclp(P) STAR. {
320   A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
321 }
322 selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
323   Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
324   Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
325   A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
326 }
327 
328 // An option "AS <id>" phrase that can follow one of the expressions that
329 // define the result set, or one of the tables in the FROM clause.
330 //
331 %type as {Token}
332 as(X) ::= AS nm(Y).    { X = Y; }
333 as(X) ::= ids(Y).      { X = Y; }
334 as(X) ::= .            { X.n = 0; }
335 
336 
337 %type seltablist {SrcList*}
338 %destructor seltablist {sqliteSrcListDelete($$);}
339 %type stl_prefix {SrcList*}
340 %destructor stl_prefix {sqliteSrcListDelete($$);}
341 %type from {SrcList*}
342 %destructor from {sqliteSrcListDelete($$);}
343 
344 // A complete FROM clause.
345 //
346 from(A) ::= .                                 {A = sqliteMalloc(sizeof(*A));}
347 from(A) ::= FROM seltablist(X).               {A = X;}
348 
349 // "seltablist" is a "Select Table List" - the content of the FROM clause
350 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
351 //
352 stl_prefix(A) ::= seltablist(X) joinop(Y).    {
353    A = X;
354    if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
355 }
356 stl_prefix(A) ::= .                           {A = 0;}
357 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
358   A = sqliteSrcListAppend(X,&Y,&D);
359   if( Z.n ) sqliteSrcListAddAlias(A,&Z);
360   if( N ){
361     if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
362     else { sqliteExprDelete(N); }
363   }
364   if( U ){
365     if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
366     else { sqliteIdListDelete(U); }
367   }
368 }
369 seltablist(A) ::= stl_prefix(X) LP select(S) RP as(Z) on_opt(N) using_opt(U). {
370   A = sqliteSrcListAppend(X,0,0);
371   A->a[A->nSrc-1].pSelect = S;
372   if( Z.n ) sqliteSrcListAddAlias(A,&Z);
373   if( N ){
374     if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
375     else { sqliteExprDelete(N); }
376   }
377   if( U ){
378     if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
379     else { sqliteIdListDelete(U); }
380   }
381 }
382 
383 %type dbnm {Token}
384 dbnm(A) ::= .          {A.z=0; A.n=0;}
385 dbnm(A) ::= DOT nm(X). {A = X;}
386 
387 %type joinop {int}
388 %type joinop2 {int}
389 joinop(X) ::= COMMA.                   { X = JT_INNER; }
390 joinop(X) ::= JOIN.                    { X = JT_INNER; }
391 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqliteJoinType(pParse,&A,0,0); }
392 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqliteJoinType(pParse,&A,&B,0); }
393 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
394                                        { X = sqliteJoinType(pParse,&A,&B,&C); }
395 
396 %type on_opt {Expr*}
397 %destructor on_opt {sqliteExprDelete($$);}
398 on_opt(N) ::= ON expr(E).   {N = E;}
399 on_opt(N) ::= .             {N = 0;}
400 
401 %type using_opt {IdList*}
402 %destructor using_opt {sqliteIdListDelete($$);}
403 using_opt(U) ::= USING LP idxlist(L) RP.  {U = L;}
404 using_opt(U) ::= .                        {U = 0;}
405 
406 
407 %type orderby_opt {ExprList*}
408 %destructor orderby_opt {sqliteExprListDelete($$);}
409 %type sortlist {ExprList*}
410 %destructor sortlist {sqliteExprListDelete($$);}
411 %type sortitem {Expr*}
412 %destructor sortitem {sqliteExprDelete($$);}
413 
414 orderby_opt(A) ::= .                          {A = 0;}
415 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
416 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
417   A = sqliteExprListAppend(X,Y,0);
418   if( A ) A->a[A->nExpr-1].sortOrder = C+Z;
419 }
420 sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
421   A = sqliteExprListAppend(0,Y,0);
422   if( A ) A->a[0].sortOrder = C+Z;
423 }
424 sortitem(A) ::= expr(X).   {A = X;}
425 
426 %type sortorder {int}
427 %type collate {int}
428 
429 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
430 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
431 sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
432 collate(C) ::= .                {C = SQLITE_SO_UNK;}
433 collate(C) ::= COLLATE id(X).   {C = sqliteCollateType(X.z, X.n);}
434 
435 %type groupby_opt {ExprList*}
436 %destructor groupby_opt {sqliteExprListDelete($$);}
437 groupby_opt(A) ::= .                      {A = 0;}
438 groupby_opt(A) ::= GROUP BY exprlist(X).  {A = X;}
439 
440 %type having_opt {Expr*}
441 %destructor having_opt {sqliteExprDelete($$);}
442 having_opt(A) ::= .                {A = 0;}
443 having_opt(A) ::= HAVING expr(X).  {A = X;}
444 
445 %type limit_opt {struct LimitVal}
446 limit_opt(A) ::= .                     {A.limit = -1; A.offset = 0;}
447 limit_opt(A) ::= LIMIT signed(X).      {A.limit = X; A.offset = 0;}
448 limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y).
449                                        {A.limit = X; A.offset = Y;}
450 limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y).
451                                        {A.limit = Y; A.offset = X;}
452 
453 /////////////////////////// The DELETE statement /////////////////////////////
454 //
455 cmd ::= DELETE FROM nm(X) dbnm(D) where_opt(Y). {
456    sqliteDeleteFrom(pParse, sqliteSrcListAppend(0,&X,&D), Y);
457 }
458 
459 %type where_opt {Expr*}
460 %destructor where_opt {sqliteExprDelete($$);}
461 
462 where_opt(A) ::= .                    {A = 0;}
463 where_opt(A) ::= WHERE expr(X).       {A = X;}
464 
465 %type setlist {ExprList*}
466 %destructor setlist {sqliteExprListDelete($$);}
467 
468 ////////////////////////// The UPDATE command ////////////////////////////////
469 //
470 cmd ::= UPDATE orconf(R) nm(X) dbnm(D) SET setlist(Y) where_opt(Z).
471     {sqliteUpdate(pParse,sqliteSrcListAppend(0,&X,&D),Y,Z,R);}
472 
473 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
474     {A = sqliteExprListAppend(Z,Y,&X);}
475 setlist(A) ::= nm(X) EQ expr(Y).   {A = sqliteExprListAppend(0,Y,&X);}
476 
477 ////////////////////////// The INSERT command /////////////////////////////////
478 //
479 cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F)
480         VALUES LP itemlist(Y) RP.
481             {sqliteInsert(pParse, sqliteSrcListAppend(0,&X,&D), Y, 0, F, R);}
482 cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F) select(S).
483             {sqliteInsert(pParse, sqliteSrcListAppend(0,&X,&D), 0, S, F, R);}
484 
485 %type insert_cmd {int}
486 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
487 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
488 
489 
490 %type itemlist {ExprList*}
491 %destructor itemlist {sqliteExprListDelete($$);}
492 
493 itemlist(A) ::= itemlist(X) COMMA expr(Y).  {A = sqliteExprListAppend(X,Y,0);}
494 itemlist(A) ::= expr(X).                    {A = sqliteExprListAppend(0,X,0);}
495 
496 %type inscollist_opt {IdList*}
497 %destructor inscollist_opt {sqliteIdListDelete($$);}
498 %type inscollist {IdList*}
499 %destructor inscollist {sqliteIdListDelete($$);}
500 
501 inscollist_opt(A) ::= .                       {A = 0;}
502 inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
503 inscollist(A) ::= inscollist(X) COMMA nm(Y).  {A = sqliteIdListAppend(X,&Y);}
504 inscollist(A) ::= nm(Y).                      {A = sqliteIdListAppend(0,&Y);}
505 
506 /////////////////////////// Expression Processing /////////////////////////////
507 //
508 %left OR.
509 %left AND.
510 %right NOT.
511 %left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
512 %left GT GE LT LE.
513 %left BITAND BITOR LSHIFT RSHIFT.
514 %left PLUS MINUS.
515 %left STAR SLASH REM.
516 %left CONCAT.
517 %right UMINUS UPLUS BITNOT.
518 %right ORACLE_OUTER_JOIN.
519 
520 %type expr {Expr*}
521 %destructor expr {sqliteExprDelete($$);}
522 
523 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E); }
524 expr(A) ::= NULL(X).             {A = sqliteExpr(TK_NULL, 0, 0, &X);}
525 expr(A) ::= ID(X).               {A = sqliteExpr(TK_ID, 0, 0, &X);}
526 expr(A) ::= JOIN_KW(X).          {A = sqliteExpr(TK_ID, 0, 0, &X);}
527 expr(A) ::= nm(X) DOT nm(Y). {
528   Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
529   Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
530   A = sqliteExpr(TK_DOT, temp1, temp2, 0);
531 }
532 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
533   Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
534   Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
535   Expr *temp3 = sqliteExpr(TK_ID, 0, 0, &Z);
536   Expr *temp4 = sqliteExpr(TK_DOT, temp2, temp3, 0);
537   A = sqliteExpr(TK_DOT, temp1, temp4, 0);
538 }
539 expr(A) ::= expr(B) ORACLE_OUTER_JOIN.
540                              {A = B; ExprSetProperty(A,EP_Oracle8Join);}
541 expr(A) ::= INTEGER(X).      {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
542 expr(A) ::= FLOAT(X).        {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
543 expr(A) ::= STRING(X).       {A = sqliteExpr(TK_STRING, 0, 0, &X);}
544 expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
545   A = sqliteExprFunction(Y, &X);
546   sqliteExprSpan(A,&X,&E);
547 }
548 expr(A) ::= ID(X) LP STAR RP(E). {
549   A = sqliteExprFunction(0, &X);
550   sqliteExprSpan(A,&X,&E);
551 }
552 expr(A) ::= expr(X) AND expr(Y).   {A = sqliteExpr(TK_AND, X, Y, 0);}
553 expr(A) ::= expr(X) OR expr(Y).    {A = sqliteExpr(TK_OR, X, Y, 0);}
554 expr(A) ::= expr(X) LT expr(Y).    {A = sqliteExpr(TK_LT, X, Y, 0);}
555 expr(A) ::= expr(X) GT expr(Y).    {A = sqliteExpr(TK_GT, X, Y, 0);}
556 expr(A) ::= expr(X) LE expr(Y).    {A = sqliteExpr(TK_LE, X, Y, 0);}
557 expr(A) ::= expr(X) GE expr(Y).    {A = sqliteExpr(TK_GE, X, Y, 0);}
558 expr(A) ::= expr(X) NE expr(Y).    {A = sqliteExpr(TK_NE, X, Y, 0);}
559 expr(A) ::= expr(X) EQ expr(Y).    {A = sqliteExpr(TK_EQ, X, Y, 0);}
560 expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
561 expr(A) ::= expr(X) BITOR expr(Y).  {A = sqliteExpr(TK_BITOR, X, Y, 0);}
562 expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
563 expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
564 expr(A) ::= expr(X) likeop(OP) expr(Y).  [LIKE]  {
565   ExprList *pList = sqliteExprListAppend(0, Y, 0);
566   pList = sqliteExprListAppend(pList, X, 0);
567   A = sqliteExprFunction(pList, 0);
568   if( A ) A->op = OP;
569   sqliteExprSpan(A, &X->span, &Y->span);
570 }
571 expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
572   ExprList *pList = sqliteExprListAppend(0, Y, 0);
573   pList = sqliteExprListAppend(pList, X, 0);
574   A = sqliteExprFunction(pList, 0);
575   if( A ) A->op = OP;
576   A = sqliteExpr(TK_NOT, A, 0, 0);
577   sqliteExprSpan(A,&X->span,&Y->span);
578 }
579 %type likeop {int}
580 likeop(A) ::= LIKE. {A = TK_LIKE;}
581 likeop(A) ::= GLOB. {A = TK_GLOB;}
582 expr(A) ::= expr(X) PLUS expr(Y).  {A = sqliteExpr(TK_PLUS, X, Y, 0);}
583 expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
584 expr(A) ::= expr(X) STAR expr(Y).  {A = sqliteExpr(TK_STAR, X, Y, 0);}
585 expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
586 expr(A) ::= expr(X) REM expr(Y).   {A = sqliteExpr(TK_REM, X, Y, 0);}
587 expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
588 expr(A) ::= expr(X) ISNULL(E). {
589   A = sqliteExpr(TK_ISNULL, X, 0, 0);
590   sqliteExprSpan(A,&X->span,&E);
591 }
592 expr(A) ::= expr(X) IS NULL(E). {
593   A = sqliteExpr(TK_ISNULL, X, 0, 0);
594   sqliteExprSpan(A,&X->span,&E);
595 }
596 expr(A) ::= expr(X) NOTNULL(E). {
597   A = sqliteExpr(TK_NOTNULL, X, 0, 0);
598   sqliteExprSpan(A,&X->span,&E);
599 }
600 expr(A) ::= expr(X) NOT NULL(E). {
601   A = sqliteExpr(TK_NOTNULL, X, 0, 0);
602   sqliteExprSpan(A,&X->span,&E);
603 }
604 expr(A) ::= expr(X) IS NOT NULL(E). {
605   A = sqliteExpr(TK_NOTNULL, X, 0, 0);
606   sqliteExprSpan(A,&X->span,&E);
607 }
608 expr(A) ::= NOT(B) expr(X). {
609   A = sqliteExpr(TK_NOT, X, 0, 0);
610   sqliteExprSpan(A,&B,&X->span);
611 }
612 expr(A) ::= BITNOT(B) expr(X). {
613   A = sqliteExpr(TK_BITNOT, X, 0, 0);
614   sqliteExprSpan(A,&B,&X->span);
615 }
616 expr(A) ::= MINUS(B) expr(X). [UMINUS] {
617   A = sqliteExpr(TK_UMINUS, X, 0, 0);
618   sqliteExprSpan(A,&B,&X->span);
619 }
620 expr(A) ::= PLUS(B) expr(X). [UPLUS] {
621   A = sqliteExpr(TK_UPLUS, X, 0, 0);
622   sqliteExprSpan(A,&B,&X->span);
623 }
624 expr(A) ::= LP(B) select(X) RP(E). {
625   A = sqliteExpr(TK_SELECT, 0, 0, 0);
626   if( A ) A->pSelect = X;
627   sqliteExprSpan(A,&B,&E);
628 }
629 expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
630   ExprList *pList = sqliteExprListAppend(0, X, 0);
631   pList = sqliteExprListAppend(pList, Y, 0);
632   A = sqliteExpr(TK_BETWEEN, W, 0, 0);
633   if( A ) A->pList = pList;
634   sqliteExprSpan(A,&W->span,&Y->span);
635 }
636 expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
637   ExprList *pList = sqliteExprListAppend(0, X, 0);
638   pList = sqliteExprListAppend(pList, Y, 0);
639   A = sqliteExpr(TK_BETWEEN, W, 0, 0);
640   if( A ) A->pList = pList;
641   A = sqliteExpr(TK_NOT, A, 0, 0);
642   sqliteExprSpan(A,&W->span,&Y->span);
643 }
644 expr(A) ::= expr(X) IN LP exprlist(Y) RP(E).  {
645   A = sqliteExpr(TK_IN, X, 0, 0);
646   if( A ) A->pList = Y;
647   sqliteExprSpan(A,&X->span,&E);
648 }
649 expr(A) ::= expr(X) IN LP select(Y) RP(E).  {
650   A = sqliteExpr(TK_IN, X, 0, 0);
651   if( A ) A->pSelect = Y;
652   sqliteExprSpan(A,&X->span,&E);
653 }
654 expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E).  {
655   A = sqliteExpr(TK_IN, X, 0, 0);
656   if( A ) A->pList = Y;
657   A = sqliteExpr(TK_NOT, A, 0, 0);
658   sqliteExprSpan(A,&X->span,&E);
659 }
660 expr(A) ::= expr(X) NOT IN LP select(Y) RP(E).  {
661   A = sqliteExpr(TK_IN, X, 0, 0);
662   if( A ) A->pSelect = Y;
663   A = sqliteExpr(TK_NOT, A, 0, 0);
664   sqliteExprSpan(A,&X->span,&E);
665 }
666 
667 /* CASE expressions */
668 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
669   A = sqliteExpr(TK_CASE, X, Z, 0);
670   if( A ) A->pList = Y;
671   sqliteExprSpan(A, &C, &E);
672 }
673 %type case_exprlist {ExprList*}
674 %destructor case_exprlist {sqliteExprListDelete($$);}
675 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
676   A = sqliteExprListAppend(X, Y, 0);
677   A = sqliteExprListAppend(A, Z, 0);
678 }
679 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
680   A = sqliteExprListAppend(0, Y, 0);
681   A = sqliteExprListAppend(A, Z, 0);
682 }
683 %type case_else {Expr*}
684 case_else(A) ::=  ELSE expr(X).         {A = X;}
685 case_else(A) ::=  .                     {A = 0;}
686 %type case_operand {Expr*}
687 case_operand(A) ::= expr(X).            {A = X;}
688 case_operand(A) ::= .                   {A = 0;}
689 
690 %type exprlist {ExprList*}
691 %destructor exprlist {sqliteExprListDelete($$);}
692 %type expritem {Expr*}
693 %destructor expritem {sqliteExprDelete($$);}
694 
695 exprlist(A) ::= exprlist(X) COMMA expritem(Y).
696    {A = sqliteExprListAppend(X,Y,0);}
697 exprlist(A) ::= expritem(X).            {A = sqliteExprListAppend(0,X,0);}
698 expritem(A) ::= expr(X).                {A = X;}
699 expritem(A) ::= .                       {A = 0;}
700 
701 ///////////////////////////// The CREATE INDEX command ///////////////////////
702 //
703 cmd ::= CREATE(S) temp(T) uniqueflag(U) INDEX nm(X)
704         ON nm(Y) dbnm(D) LP idxlist(Z) RP(E) onconf(R). {
705   SrcList *pSrc = sqliteSrcListAppend(0, &Y, &D);
706   if( U!=OE_None ) U = R;
707   if( U==OE_Default) U = OE_Abort;
708   sqliteCreateIndex(pParse, &X, pSrc, Z, U, T, &S, &E);
709 }
710 
711 %type uniqueflag {int}
712 uniqueflag(A) ::= UNIQUE.  { A = OE_Abort; }
713 uniqueflag(A) ::= .        { A = OE_None; }
714 
715 %type idxlist {IdList*}
716 %destructor idxlist {sqliteIdListDelete($$);}
717 %type idxlist_opt {IdList*}
718 %destructor idxlist_opt {sqliteIdListDelete($$);}
719 %type idxitem {Token}
720 
721 idxlist_opt(A) ::= .                         {A = 0;}
722 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
723 idxlist(A) ::= idxlist(X) COMMA idxitem(Y).  {A = sqliteIdListAppend(X,&Y);}
724 idxlist(A) ::= idxitem(Y).                   {A = sqliteIdListAppend(0,&Y);}
725 idxitem(A) ::= nm(X) sortorder.              {A = X;}
726 
727 ///////////////////////////// The DROP INDEX command /////////////////////////
728 //
729 
730 cmd ::= DROP INDEX nm(X) dbnm(Y).   {
731   sqliteDropIndex(pParse, sqliteSrcListAppend(0,&X,&Y));
732 }
733 
734 
735 ///////////////////////////// The COPY command ///////////////////////////////
736 //
737 cmd ::= COPY orconf(R) nm(X) dbnm(D) FROM nm(Y) USING DELIMITERS STRING(Z).
738     {sqliteCopy(pParse,sqliteSrcListAppend(0,&X,&D),&Y,&Z,R);}
739 cmd ::= COPY orconf(R) nm(X) dbnm(D) FROM nm(Y).
740     {sqliteCopy(pParse,sqliteSrcListAppend(0,&X,&D),&Y,0,R);}
741 
742 ///////////////////////////// The VACUUM command /////////////////////////////
743 //
744 cmd ::= VACUUM.                {sqliteVacuum(pParse,0);}
745 cmd ::= VACUUM nm(X).         {sqliteVacuum(pParse,&X);}
746 
747 ///////////////////////////// The PRAGMA command /////////////////////////////
748 //
749 cmd ::= PRAGMA ids(X) EQ nm(Y).         {sqlitePragma(pParse,&X,&Y,0);}
750 cmd ::= PRAGMA ids(X) EQ ON(Y).          {sqlitePragma(pParse,&X,&Y,0);}
751 cmd ::= PRAGMA ids(X) EQ plus_num(Y).    {sqlitePragma(pParse,&X,&Y,0);}
752 cmd ::= PRAGMA ids(X) EQ minus_num(Y).   {sqlitePragma(pParse,&X,&Y,1);}
753 cmd ::= PRAGMA ids(X) LP nm(Y) RP.      {sqlitePragma(pParse,&X,&Y,0);}
754 cmd ::= PRAGMA ids(X).                   {sqlitePragma(pParse,&X,&X,0);}
755 plus_num(A) ::= plus_opt number(X).   {A = X;}
756 minus_num(A) ::= MINUS number(X).     {A = X;}
757 number(A) ::= INTEGER(X).  {A = X;}
758 number(A) ::= FLOAT(X).    {A = X;}
759 plus_opt ::= PLUS.
760 plus_opt ::= .
761 
762 //////////////////////////// The CREATE TRIGGER command /////////////////////
763 
764 cmd ::= CREATE(A) trigger_decl BEGIN trigger_cmd_list(S) END(Z). {
765   Token all;
766   all.z = A.z;
767   all.n = (Z.z - A.z) + Z.n;
768   sqliteFinishTrigger(pParse, S, &all);
769 }
770 
771 trigger_decl ::= temp(T) TRIGGER nm(B) trigger_time(C) trigger_event(D)
772                  ON nm(E) dbnm(DB) foreach_clause(F) when_clause(G). {
773   SrcList *pTab = sqliteSrcListAppend(0, &E, &DB);
774   sqliteBeginTrigger(pParse, &B, C, D.a, D.b, pTab, F, G, T);
775 }
776 
777 %type trigger_time  {int}
778 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
779 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
780 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
781 trigger_time(A) ::= .            { A = TK_BEFORE; }
782 
783 %type trigger_event {struct TrigEvent}
784 %destructor trigger_event {sqliteIdListDelete($$.b);}
785 trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; }
786 trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; }
787 trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;}
788 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; }
789 
790 %type foreach_clause {int}
791 foreach_clause(A) ::= .                   { A = TK_ROW; }
792 foreach_clause(A) ::= FOR EACH ROW.       { A = TK_ROW; }
793 foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
794 
795 %type when_clause {Expr *}
796 when_clause(A) ::= .             { A = 0; }
797 when_clause(A) ::= WHEN expr(X). { A = X; }
798 
799 %type trigger_cmd_list {TriggerStep *}
800 %destructor trigger_cmd_list {sqliteDeleteTriggerStep($$);}
801 trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
802   X->pNext = Y;
803   A = X;
804 }
805 trigger_cmd_list(A) ::= . { A = 0; }
806 
807 %type trigger_cmd {TriggerStep *}
808 %destructor trigger_cmd {sqliteDeleteTriggerStep($$);}
809 // UPDATE
810 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
811                { A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
812 
813 // INSERT
814 trigger_cmd(A) ::= INSERT orconf(R) INTO nm(X) inscollist_opt(F)
815   VALUES LP itemlist(Y) RP.
816 {A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
817 
818 trigger_cmd(A) ::= INSERT orconf(R) INTO nm(X) inscollist_opt(F) select(S).
819                {A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
820 
821 // DELETE
822 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
823                {A = sqliteTriggerDeleteStep(&X, Y);}
824 
825 // SELECT
826 trigger_cmd(A) ::= select(X).  {A = sqliteTriggerSelectStep(X); }
827 
828 // The special RAISE expression that may occur in trigger programs
829 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
830   A = sqliteExpr(TK_RAISE, 0, 0, 0);
831   A->iColumn = OE_Ignore;
832   sqliteExprSpan(A, &X, &Y);
833 }
834 expr(A) ::= RAISE(X) LP ROLLBACK COMMA nm(Z) RP(Y).  {
835   A = sqliteExpr(TK_RAISE, 0, 0, &Z);
836   A->iColumn = OE_Rollback;
837   sqliteExprSpan(A, &X, &Y);
838 }
839 expr(A) ::= RAISE(X) LP ABORT COMMA nm(Z) RP(Y).  {
840   A = sqliteExpr(TK_RAISE, 0, 0, &Z);
841   A->iColumn = OE_Abort;
842   sqliteExprSpan(A, &X, &Y);
843 }
844 expr(A) ::= RAISE(X) LP FAIL COMMA nm(Z) RP(Y).  {
845   A = sqliteExpr(TK_RAISE, 0, 0, &Z);
846   A->iColumn = OE_Fail;
847   sqliteExprSpan(A, &X, &Y);
848 }
849 
850 ////////////////////////  DROP TRIGGER statement //////////////////////////////
851 cmd ::= DROP TRIGGER nm(X) dbnm(D). {
852   sqliteDropTrigger(pParse,sqliteSrcListAppend(0,&X,&D));
853 }
854 
855 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
856 cmd ::= ATTACH database_kw_opt ids(F) AS nm(D). {
857   sqliteAttach(pParse, &F, &D);
858 }
859 
860 database_kw_opt ::= DATABASE.
861 database_kw_opt ::= .
862 
863 //////////////////////// DETACH DATABASE name /////////////////////////////////
864 cmd ::= DETACH database_kw_opt nm(D). {
865   sqliteDetach(pParse, &D);
866 }
867