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