source: trunk/src/3rdparty/sqlite/parse.y@ 207

Last change on this file since 207 was 205, checked in by rudi, 14 years ago

Added SQLite 2.8.17 sources. This allows to build at least one of the sql drivers / plugins.

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