blob: b7907467717bd7599aa7664410b8665ae8f20af4 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh348784e2000-05-29 20:41:49 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh348784e2000-05-29 20:41:49 +000010**
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**
danielk1977c3f9bad2002-05-15 08:30:12 +000017** @(#) $Id: parse.y,v 1.64 2002/05/15 08:30:14 danielk1977 Exp $
drh348784e2000-05-29 20:41:49 +000018*/
19%token_prefix TK_
20%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000021%default_type {Token}
drh348784e2000-05-29 20:41:49 +000022%extra_argument {Parse *pParse}
23%syntax_error {
drhc837e702000-06-08 16:26:24 +000024 sqliteSetString(&pParse->zErrMsg,"syntax error",0);
drh348784e2000-05-29 20:41:49 +000025 pParse->sErrToken = TOKEN;
26}
27%name sqliteParser
28%include {
29#include "sqliteInt.h"
30#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000031
32/*
33** A structure for holding two integers
34*/
35struct twoint { int a,b; };
danielk1977c3f9bad2002-05-15 08:30:12 +000036
37/*
38** A structure for holding an integer and an IdList
39*/
40struct int_idlist { int a; IdList * b; };
drh348784e2000-05-29 20:41:49 +000041}
42
drh348784e2000-05-29 20:41:49 +000043// These are extra tokens used by the lexer but never seen by the
44// parser. We put them in a rule so that the parser generator will
drh6206d502000-06-19 19:09:08 +000045// add them to the parse.h output file.
drh348784e2000-05-29 20:41:49 +000046//
drhc4a3c772001-04-04 11:48:57 +000047%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
48 COLUMN AGG_FUNCTION.
49
50// Input is zero or more commands.
51input ::= cmdlist.
drh348784e2000-05-29 20:41:49 +000052
53// A list of commands is zero or more commands
54//
55cmdlist ::= ecmd.
drh094b2bb2002-03-13 18:54:07 +000056cmdlist ::= cmdlist ecmd.
57ecmd ::= explain cmd SEMI. {sqliteExec(pParse);}
58ecmd ::= cmd SEMI. {sqliteExec(pParse);}
59ecmd ::= SEMI.
drh348784e2000-05-29 20:41:49 +000060explain ::= EXPLAIN. {pParse->explain = 1;}
61
drh382c0242001-10-06 16:33:02 +000062///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000063//
drhfa86c412002-02-02 15:01:15 +000064
drh0d65dc02002-02-03 00:56:09 +000065cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}
drhc4a3c772001-04-04 11:48:57 +000066trans_opt ::= .
67trans_opt ::= TRANSACTION.
68trans_opt ::= TRANSACTION ids.
69cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
70cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
71cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
72
drh382c0242001-10-06 16:33:02 +000073///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +000074//
75cmd ::= create_table create_table_args.
drh969fa7c2002-02-18 18:30:32 +000076create_table ::= CREATE(X) temp(T) TABLE ids(Y). {
77 sqliteStartTable(pParse,&X,&Y,T);
78}
drhf57b3392001-10-08 13:22:32 +000079%type temp {int}
80temp(A) ::= TEMP. {A = 1;}
81temp(A) ::= . {A = 0;}
drh969fa7c2002-02-18 18:30:32 +000082create_table_args ::= LP columnlist conslist_opt RP(X). {
83 sqliteEndTable(pParse,&X,0);
84}
85create_table_args ::= AS select(S). {
86 sqliteEndTable(pParse,0,S);
87 sqliteSelectDelete(S);
88}
drh348784e2000-05-29 20:41:49 +000089columnlist ::= columnlist COMMA column.
90columnlist ::= column.
91
92// About the only information used for a column is the name of the
93// column. The type is always just "text". But the code will accept
94// an elaborate typename. Perhaps someday we'll do something with it.
95//
96column ::= columnid type carglist.
drhc4a3c772001-04-04 11:48:57 +000097columnid ::= ids(X). {sqliteAddColumn(pParse,&X);}
98
99// An IDENTIFIER can be a generic identifier, or one of several
100// keywords. Any non-standard keyword can also be an identifier.
drh382c0242001-10-06 16:33:02 +0000101// We also make DESC and identifier since it comes up so often (as
102// an abbreviation of "description").
drhc4a3c772001-04-04 11:48:57 +0000103//
drh982cef72000-05-30 16:27:03 +0000104%type id {Token}
drh9cfcf5d2002-01-29 18:41:24 +0000105id(A) ::= ABORT(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000106id(A) ::= ASC(X). {A = X;}
107id(A) ::= BEGIN(X). {A = X;}
108id(A) ::= CLUSTER(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000109id(A) ::= CONFLICT(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000110id(A) ::= COPY(X). {A = X;}
111id(A) ::= DELIMITERS(X). {A = X;}
112id(A) ::= DESC(X). {A = X;}
113id(A) ::= END(X). {A = X;}
114id(A) ::= EXPLAIN(X). {A = X;}
115id(A) ::= FAIL(X). {A = X;}
116id(A) ::= ID(X). {A = X;}
117id(A) ::= IGNORE(X). {A = X;}
118id(A) ::= KEY(X). {A = X;}
119id(A) ::= OFFSET(X). {A = X;}
120id(A) ::= PRAGMA(X). {A = X;}
121id(A) ::= REPLACE(X). {A = X;}
122id(A) ::= TEMP(X). {A = X;}
123id(A) ::= VACUUM(X). {A = X;}
124id(A) ::= VIEW(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000125
126// And "ids" is an identifer-or-string.
127//
128%type ids {Token}
129ids(A) ::= id(X). {A = X;}
130ids(A) ::= STRING(X). {A = X;}
131
drh382c0242001-10-06 16:33:02 +0000132type ::= .
133type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
134type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
135type ::= typename(X) LP signed COMMA signed RP(Y).
136 {sqliteAddColumnType(pParse,&X,&Y);}
137%type typename {Token}
138typename(A) ::= ids(X). {A = X;}
139typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000140signed ::= INTEGER.
141signed ::= PLUS INTEGER.
142signed ::= MINUS INTEGER.
143carglist ::= carglist carg.
144carglist ::= .
drhc4a3c772001-04-04 11:48:57 +0000145carg ::= CONSTRAINT ids ccons.
drh348784e2000-05-29 20:41:49 +0000146carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000147carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
148carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
149carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
150carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
151carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
152carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
153carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
154carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
155carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000156
drh382c0242001-10-06 16:33:02 +0000157// In addition to the type name, we also care about the primary key and
158// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000159//
drh9cfcf5d2002-01-29 18:41:24 +0000160ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
161ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
162ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
163ccons ::= CHECK LP expr RP onconf.
drh348784e2000-05-29 20:41:49 +0000164
165// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000166// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000167//
168conslist_opt ::= .
169conslist_opt ::= COMMA conslist.
170conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000171conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000172conslist ::= tcons.
drhc4a3c772001-04-04 11:48:57 +0000173tcons ::= CONSTRAINT ids.
drh9cfcf5d2002-01-29 18:41:24 +0000174tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
175 {sqliteAddPrimaryKey(pParse,X,R);}
176tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
177 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
178tcons ::= CHECK expr onconf.
179
180// The following is a non-standard extension that allows us to declare the
181// default behavior when there is a constraint conflict.
182//
183%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000184%type orconf {int}
185%type resolvetype {int}
186onconf(A) ::= . { A = OE_Default; }
187onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
188orconf(A) ::= . { A = OE_Default; }
189orconf(A) ::= OR resolvetype(X). { A = X; }
190resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
191resolvetype(A) ::= ABORT. { A = OE_Abort; }
192resolvetype(A) ::= FAIL. { A = OE_Fail; }
193resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
194resolvetype(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000195
drh382c0242001-10-06 16:33:02 +0000196////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000197//
drh4ff6dfa2002-03-03 23:06:00 +0000198cmd ::= DROP TABLE ids(X). {sqliteDropTable(pParse,&X,0);}
drh348784e2000-05-29 20:41:49 +0000199
drha76b5df2002-02-23 02:32:10 +0000200///////////////////// The CREATE VIEW statement /////////////////////////////
201//
202cmd ::= CREATE(X) VIEW ids(Y) AS select(S). {
203 sqliteCreateView(pParse, &X, &Y, S);
204}
205cmd ::= DROP VIEW ids(X). {
drh4ff6dfa2002-03-03 23:06:00 +0000206 sqliteDropTable(pParse, &X, 1);
drha76b5df2002-02-23 02:32:10 +0000207}
208
drh382c0242001-10-06 16:33:02 +0000209//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000210//
drh9bb61fe2000-06-05 16:01:39 +0000211cmd ::= select(X). {
drh832508b2002-03-02 17:04:07 +0000212 sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
drh9bb61fe2000-06-05 16:01:39 +0000213 sqliteSelectDelete(X);
214}
drhefb72512000-05-31 20:00:52 +0000215
drh9bb61fe2000-06-05 16:01:39 +0000216%type select {Select*}
217%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000218%type oneselect {Select*}
219%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000220
drh82c3d632000-06-06 21:56:07 +0000221select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000222select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000223 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000224 Z->op = Y;
225 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000226 }
227 A = Z;
drh82c3d632000-06-06 21:56:07 +0000228}
drh0a36c572002-02-18 22:49:59 +0000229%type multiselect_op {int}
230multiselect_op(A) ::= UNION. {A = TK_UNION;}
231multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
232multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
233multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
drh82c3d632000-06-06 21:56:07 +0000234oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000235 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
236 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.a,L.b);
drh9bb61fe2000-06-05 16:01:39 +0000237}
238
239// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
240// present and false (0) if it is not.
241//
drhefb72512000-05-31 20:00:52 +0000242%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000243distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000244distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000245distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000246
drh9bb61fe2000-06-05 16:01:39 +0000247// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000248// values of the SELECT statement. The "*" in statements like
249// "SELECT * FROM ..." is encoded as a special expression with an
250// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000251//
drh348784e2000-05-29 20:41:49 +0000252%type selcollist {ExprList*}
253%destructor selcollist {sqliteExprListDelete($$);}
254%type sclp {ExprList*}
255%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000256sclp(A) ::= selcollist(X) COMMA. {A = X;}
257sclp(A) ::= . {A = 0;}
258selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drhc4a3c772001-04-04 11:48:57 +0000259selcollist(A) ::= sclp(P) expr(X) as ids(Y). {A = sqliteExprListAppend(P,X,&Y);}
drh7c917d12001-12-16 20:05:05 +0000260selcollist(A) ::= sclp(P) STAR. {
261 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
262}
drh54473222002-04-04 02:10:55 +0000263selcollist(A) ::= sclp(P) ids(X) DOT STAR. {
264 Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
265 Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
266 A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
267}
drh9bb61fe2000-06-05 16:01:39 +0000268as ::= .
269as ::= AS.
270
drh348784e2000-05-29 20:41:49 +0000271
272%type seltablist {IdList*}
273%destructor seltablist {sqliteIdListDelete($$);}
274%type stl_prefix {IdList*}
275%destructor stl_prefix {sqliteIdListDelete($$);}
276%type from {IdList*}
277%destructor from {sqliteIdListDelete($$);}
278
drhbf3a4fa2002-04-06 13:57:42 +0000279from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000280from(A) ::= FROM seltablist(X). {A = X;}
281stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
282stl_prefix(A) ::= . {A = 0;}
drhc4a3c772001-04-04 11:48:57 +0000283seltablist(A) ::= stl_prefix(X) ids(Y). {A = sqliteIdListAppend(X,&Y);}
284seltablist(A) ::= stl_prefix(X) ids(Y) as ids(Z). {
285 A = sqliteIdListAppend(X,&Y);
286 sqliteIdListAddAlias(A,&Z);
287}
drh22f70c32002-02-18 01:17:00 +0000288seltablist(A) ::= stl_prefix(X) LP select(S) RP. {
289 A = sqliteIdListAppend(X,0);
290 A->a[A->nId-1].pSelect = S;
drhd5feede2002-05-08 21:46:14 +0000291 if( S->pOrderBy ){
292 sqliteExprListDelete(S->pOrderBy);
293 S->pOrderBy = 0;
294 }
drh22f70c32002-02-18 01:17:00 +0000295}
296seltablist(A) ::= stl_prefix(X) LP select(S) RP as ids(Z). {
297 A = sqliteIdListAppend(X,0);
298 A->a[A->nId-1].pSelect = S;
drhd5feede2002-05-08 21:46:14 +0000299 if( S->pOrderBy ){
300 sqliteExprListDelete(S->pOrderBy);
301 S->pOrderBy = 0;
302 }
drh22f70c32002-02-18 01:17:00 +0000303 sqliteIdListAddAlias(A,&Z);
304}
drh348784e2000-05-29 20:41:49 +0000305
306%type orderby_opt {ExprList*}
307%destructor orderby_opt {sqliteExprListDelete($$);}
308%type sortlist {ExprList*}
309%destructor sortlist {sqliteExprListDelete($$);}
310%type sortitem {Expr*}
311%destructor sortitem {sqliteExprDelete($$);}
312
313orderby_opt(A) ::= . {A = 0;}
314orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000315sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
316 A = sqliteExprListAppend(X,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000317 if( A ) A->a[A->nExpr-1].sortOrder = Z; /* 0=ascending, 1=decending */
drh9bb61fe2000-06-05 16:01:39 +0000318}
319sortlist(A) ::= sortitem(Y) sortorder(Z). {
320 A = sqliteExprListAppend(0,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000321 if( A ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000322}
drhda9d6c42000-05-31 18:20:14 +0000323sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000324
325%type sortorder {int}
326
327sortorder(A) ::= ASC. {A = 0;}
328sortorder(A) ::= DESC. {A = 1;}
329sortorder(A) ::= . {A = 0;}
330
drh22827922000-06-06 17:27:05 +0000331%type groupby_opt {ExprList*}
332%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000333groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000334groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
335
336%type having_opt {Expr*}
337%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000338having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000339having_opt(A) ::= HAVING expr(X). {A = X;}
340
drh9bbca4c2001-11-06 04:00:18 +0000341%type limit_opt {struct twoint}
342limit_opt(A) ::= . {A.a = -1; A.b = 0;}
343limit_opt(A) ::= LIMIT INTEGER(X). {A.a = atoi(X.z); A.b = 0;}
344limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
345 {A.a = atoi(X.z); A.b = atoi(Y.z);}
346limit_sep ::= OFFSET.
347limit_sep ::= COMMA.
348
drh382c0242001-10-06 16:33:02 +0000349/////////////////////////// The DELETE statement /////////////////////////////
350//
drhc4a3c772001-04-04 11:48:57 +0000351cmd ::= DELETE FROM ids(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000352 {sqliteDeleteFrom(pParse, &X, Y);}
353
354%type where_opt {Expr*}
355%destructor where_opt {sqliteExprDelete($$);}
356
357where_opt(A) ::= . {A = 0;}
358where_opt(A) ::= WHERE expr(X). {A = X;}
359
360%type setlist {ExprList*}
361%destructor setlist {sqliteExprListDelete($$);}
362
drh382c0242001-10-06 16:33:02 +0000363////////////////////////// The UPDATE command ////////////////////////////////
364//
drh1c928532002-01-31 15:54:21 +0000365cmd ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z).
drh9cfcf5d2002-01-29 18:41:24 +0000366 {sqliteUpdate(pParse,&X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000367
drhc4a3c772001-04-04 11:48:57 +0000368setlist(A) ::= setlist(Z) COMMA ids(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000369 {A = sqliteExprListAppend(Z,Y,&X);}
drhc4a3c772001-04-04 11:48:57 +0000370setlist(A) ::= ids(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000371
drh382c0242001-10-06 16:33:02 +0000372////////////////////////// The INSERT command /////////////////////////////////
373//
drhfa86c412002-02-02 15:01:15 +0000374cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
drh9cfcf5d2002-01-29 18:41:24 +0000375 {sqliteInsert(pParse, &X, Y, 0, F, R);}
drhfa86c412002-02-02 15:01:15 +0000376cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) select(S).
drh9cfcf5d2002-01-29 18:41:24 +0000377 {sqliteInsert(pParse, &X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000378
drhfa86c412002-02-02 15:01:15 +0000379%type insert_cmd {int}
380insert_cmd(A) ::= INSERT orconf(R). {A = R;}
381insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
382
drh348784e2000-05-29 20:41:49 +0000383
384%type itemlist {ExprList*}
385%destructor itemlist {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000386
drhe64e7b22002-02-18 13:56:36 +0000387itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
388itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000389
drh967e8b72000-06-21 13:59:10 +0000390%type inscollist_opt {IdList*}
391%destructor inscollist_opt {sqliteIdListDelete($$);}
392%type inscollist {IdList*}
393%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000394
drhc4a3c772001-04-04 11:48:57 +0000395inscollist_opt(A) ::= . {A = 0;}
396inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
397inscollist(A) ::= inscollist(X) COMMA ids(Y). {A = sqliteIdListAppend(X,&Y);}
398inscollist(A) ::= ids(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000399
drh382c0242001-10-06 16:33:02 +0000400/////////////////////////// Expression Processing /////////////////////////////
401//
drh348784e2000-05-29 20:41:49 +0000402%left OR.
403%left AND.
drh8be51132000-06-03 19:19:41 +0000404%right NOT.
drhfef52082000-06-06 01:50:43 +0000405%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000406%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000407%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000408%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000409%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000410%left CONCAT.
drh81a20f22001-10-12 17:30:04 +0000411%right UMINUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000412
413%type expr {Expr*}
414%destructor expr {sqliteExprDelete($$);}
415
drhe1b6a5b2000-07-29 13:06:59 +0000416expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
drhe1b6a5b2000-07-29 13:06:59 +0000417expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drhc4a3c772001-04-04 11:48:57 +0000418expr(A) ::= id(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
419expr(A) ::= ids(X) DOT ids(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000420 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
421 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
422 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
423}
drh348784e2000-05-29 20:41:49 +0000424expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
425expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
426expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000427expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
428 A = sqliteExprFunction(Y, &X);
429 sqliteExprSpan(A,&X,&E);
430}
431expr(A) ::= ID(X) LP STAR RP(E). {
432 A = sqliteExprFunction(0, &X);
433 sqliteExprSpan(A,&X,&E);
434}
drh348784e2000-05-29 20:41:49 +0000435expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
436expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
437expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
438expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
439expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
440expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
441expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
442expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000443expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
444expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
445expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
446expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
drh0ac65892002-04-20 14:24:41 +0000447expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
448 ExprList *pList = sqliteExprListAppend(0, Y, 0);
449 pList = sqliteExprListAppend(pList, X, 0);
450 A = sqliteExprFunction(pList, &OP);
451 sqliteExprSpan(A, &X->span, &Y->span);
452}
453expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
454 ExprList *pList = sqliteExprListAppend(0, Y, 0);
455 pList = sqliteExprListAppend(pList, X, 0);
456 A = sqliteExprFunction(pList, &OP);
drh4794b982000-06-06 13:54:14 +0000457 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000458 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000459}
drh0ac65892002-04-20 14:24:41 +0000460likeop(A) ::= LIKE(X). {A = X;}
461likeop(A) ::= GLOB(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000462expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
463expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
464expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
465expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000466expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000467expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000468expr(A) ::= expr(X) ISNULL(E). {
469 A = sqliteExpr(TK_ISNULL, X, 0, 0);
470 sqliteExprSpan(A,&X->span,&E);
471}
drh33048c02001-10-01 14:29:22 +0000472expr(A) ::= expr(X) IS NULL(E). {
473 A = sqliteExpr(TK_ISNULL, X, 0, 0);
474 sqliteExprSpan(A,&X->span,&E);
475}
drhe1b6a5b2000-07-29 13:06:59 +0000476expr(A) ::= expr(X) NOTNULL(E). {
477 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
478 sqliteExprSpan(A,&X->span,&E);
479}
drh33048c02001-10-01 14:29:22 +0000480expr(A) ::= expr(X) NOT NULL(E). {
481 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
482 sqliteExprSpan(A,&X->span,&E);
483}
drh81a20f22001-10-12 17:30:04 +0000484expr(A) ::= expr(X) IS NOT NULL(E). {
485 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
486 sqliteExprSpan(A,&X->span,&E);
487}
drhe1b6a5b2000-07-29 13:06:59 +0000488expr(A) ::= NOT(B) expr(X). {
489 A = sqliteExpr(TK_NOT, X, 0, 0);
490 sqliteExprSpan(A,&B,&X->span);
491}
drh81a20f22001-10-12 17:30:04 +0000492expr(A) ::= BITNOT(B) expr(X). {
493 A = sqliteExpr(TK_BITNOT, X, 0, 0);
494 sqliteExprSpan(A,&B,&X->span);
495}
drhe1b6a5b2000-07-29 13:06:59 +0000496expr(A) ::= MINUS(B) expr(X). [UMINUS] {
497 A = sqliteExpr(TK_UMINUS, X, 0, 0);
498 sqliteExprSpan(A,&B,&X->span);
499}
500expr(A) ::= PLUS(B) expr(X). [UMINUS] {
501 A = X;
502 sqliteExprSpan(A,&B,&X->span);
503}
504expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000505 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000506 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000507 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000508}
drhfef52082000-06-06 01:50:43 +0000509expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
510 ExprList *pList = sqliteExprListAppend(0, X, 0);
511 pList = sqliteExprListAppend(pList, Y, 0);
512 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000513 if( A ) A->pList = pList;
drhe1b6a5b2000-07-29 13:06:59 +0000514 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000515}
drh4794b982000-06-06 13:54:14 +0000516expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
517 ExprList *pList = sqliteExprListAppend(0, X, 0);
518 pList = sqliteExprListAppend(pList, Y, 0);
519 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000520 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000521 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000522 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000523}
drhe1b6a5b2000-07-29 13:06:59 +0000524expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000525 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000526 if( A ) A->pList = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000527 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000528}
drhe1b6a5b2000-07-29 13:06:59 +0000529expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000530 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000531 if( A ) A->pSelect = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000532 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000533}
drhe1b6a5b2000-07-29 13:06:59 +0000534expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000535 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000536 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000537 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000538 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000539}
drhe1b6a5b2000-07-29 13:06:59 +0000540expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000541 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000542 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000543 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000544 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000545}
drhfef52082000-06-06 01:50:43 +0000546
drh17a7f8d2002-03-24 13:13:27 +0000547/* CASE expressions */
548expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
549 A = sqliteExpr(TK_CASE, X, Z, 0);
550 if( A ) A->pList = Y;
551 sqliteExprSpan(A, &C, &E);
552}
553%type case_exprlist {ExprList*}
554%destructor case_exprlist {sqliteExprListDelete($$);}
555case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
556 A = sqliteExprListAppend(X, Y, 0);
557 A = sqliteExprListAppend(A, Z, 0);
558}
559case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
560 A = sqliteExprListAppend(0, Y, 0);
561 A = sqliteExprListAppend(A, Z, 0);
562}
563%type case_else {Expr*}
564case_else(A) ::= ELSE expr(X). {A = X;}
565case_else(A) ::= . {A = 0;}
566%type case_operand {Expr*}
567case_operand(A) ::= expr(X). {A = X;}
568case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000569
570%type exprlist {ExprList*}
571%destructor exprlist {sqliteExprListDelete($$);}
572%type expritem {Expr*}
573%destructor expritem {sqliteExprDelete($$);}
574
drh348784e2000-05-29 20:41:49 +0000575exprlist(A) ::= exprlist(X) COMMA expritem(Y).
576 {A = sqliteExprListAppend(X,Y,0);}
577exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
578expritem(A) ::= expr(X). {A = X;}
579expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000580
drh382c0242001-10-06 16:33:02 +0000581///////////////////////////// The CREATE INDEX command ///////////////////////
582//
drh9cfcf5d2002-01-29 18:41:24 +0000583cmd ::= CREATE(S) uniqueflag(U) INDEX ids(X)
584 ON ids(Y) LP idxlist(Z) RP(E) onconf(R). {
585 if( U!=OE_None ) U = R;
586 if( U==OE_Default) U = OE_Abort;
587 sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);
588}
drh717e6402001-09-27 03:22:32 +0000589
590%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000591uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
592uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000593
594%type idxlist {IdList*}
595%destructor idxlist {sqliteIdListDelete($$);}
596%type idxitem {Token}
597
598idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
599 {A = sqliteIdListAppend(X,&Y);}
600idxlist(A) ::= idxitem(Y).
601 {A = sqliteIdListAppend(0,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000602idxitem(A) ::= ids(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000603
drh8aff1012001-12-22 14:49:24 +0000604///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000605//
606
drhc4a3c772001-04-04 11:48:57 +0000607cmd ::= DROP INDEX ids(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000608
drh382c0242001-10-06 16:33:02 +0000609
drh8aff1012001-12-22 14:49:24 +0000610///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000611//
drh1c928532002-01-31 15:54:21 +0000612cmd ::= COPY orconf(R) ids(X) FROM ids(Y) USING DELIMITERS STRING(Z).
drhb419a922002-01-30 16:17:23 +0000613 {sqliteCopy(pParse,&X,&Y,&Z,R);}
drh1c928532002-01-31 15:54:21 +0000614cmd ::= COPY orconf(R) ids(X) FROM ids(Y).
drhb419a922002-01-30 16:17:23 +0000615 {sqliteCopy(pParse,&X,&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000616
drh382c0242001-10-06 16:33:02 +0000617///////////////////////////// The VACUUM command /////////////////////////////
618//
drhdce2cbe2000-05-31 02:27:49 +0000619cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drhc4a3c772001-04-04 11:48:57 +0000620cmd ::= VACUUM ids(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000621
drh382c0242001-10-06 16:33:02 +0000622///////////////////////////// The PRAGMA command /////////////////////////////
623//
drhf57b14a2001-09-14 18:54:08 +0000624cmd ::= PRAGMA ids(X) EQ ids(Y). {sqlitePragma(pParse,&X,&Y,0);}
625cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
626cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
627cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh382c0242001-10-06 16:33:02 +0000628cmd ::= PRAGMA ids(X) LP ids(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drh603240c2002-03-05 01:11:12 +0000629cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
drhf57b14a2001-09-14 18:54:08 +0000630plus_num(A) ::= plus_opt number(X). {A = X;}
631minus_num(A) ::= MINUS number(X). {A = X;}
632number(A) ::= INTEGER(X). {A = X;}
633number(A) ::= FLOAT(X). {A = X;}
634plus_opt ::= PLUS.
635plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000636
637//////////////////////////// The CREATE TRIGGER command /////////////////////
638cmd ::= CREATE(A) TRIGGER ids(B) trigger_time(C) trigger_event(D) ON ids(E)
639 foreach_clause(F) when_clause(G)
640 BEGIN trigger_cmd_list(S) END(Z). {
641 sqliteCreateTrigger(pParse, &B, C, D.a, D.b, &E, F, G, S,
642 A.z, (int)(Z.z - A.z) + Z.n );
643}
644
645%type trigger_time {int}
646trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
647trigger_time(A) ::= AFTER. { A = TK_AFTER; }
648trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
649trigger_time(A) ::= . { A = TK_BEFORE; }
650
651%type trigger_event {struct int_idlist}
652trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; }
653trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; }
654trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;}
655trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; }
656
657%type foreach_clause {int}
658foreach_clause(A) ::= . { A = TK_ROW; }
659foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
660foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
661
662%type when_clause {Expr *}
663when_clause(A) ::= . { A = 0; }
664when_clause(A) ::= WHEN expr(X). { A = X; }
665
666%type trigger_cmd_list {TriggerStep *}
667trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
668 X->pNext = Y ; A = X; }
669trigger_cmd_list(A) ::= . { A = 0; }
670
671%type trigger_cmd {TriggerStep *}
672// UPDATE
673trigger_cmd(A) ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z).
674 { A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
675
676// INSERT
677trigger_cmd(A) ::= INSERT orconf(R) INTO ids(X) inscollist_opt(F)
678 VALUES LP itemlist(Y) RP.
679{A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
680
681trigger_cmd(A) ::= INSERT orconf(R) INTO ids(X) inscollist_opt(F) select(S).
682 {A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
683
684// DELETE
685trigger_cmd(A) ::= DELETE FROM ids(X) where_opt(Y).
686 {A = sqliteTriggerDeleteStep(&X, Y);}
687
688// SELECT
689trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); }
690
691//////////////////////// DROP TRIGGER statement //////////////////////////////
692cmd ::= DROP TRIGGER ids(X). {
693 sqliteDropTrigger(pParse,&X,0);
694}
695