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