blob: cf89bb1155559b8fd0faecb790a7ed0f030a2585 [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**
drh0ac65892002-04-20 14:24:41 +000017** @(#) $Id: parse.y,v 1.62 2002/04/20 14:24:42 drh 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; };
drh348784e2000-05-29 20:41:49 +000036}
37
drh348784e2000-05-29 20:41:49 +000038// These are extra tokens used by the lexer but never seen by the
39// parser. We put them in a rule so that the parser generator will
drh6206d502000-06-19 19:09:08 +000040// add them to the parse.h output file.
drh348784e2000-05-29 20:41:49 +000041//
drhc4a3c772001-04-04 11:48:57 +000042%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
43 COLUMN AGG_FUNCTION.
44
45// Input is zero or more commands.
46input ::= cmdlist.
drh348784e2000-05-29 20:41:49 +000047
48// A list of commands is zero or more commands
49//
50cmdlist ::= ecmd.
drh094b2bb2002-03-13 18:54:07 +000051cmdlist ::= cmdlist ecmd.
52ecmd ::= explain cmd SEMI. {sqliteExec(pParse);}
53ecmd ::= cmd SEMI. {sqliteExec(pParse);}
54ecmd ::= SEMI.
drh348784e2000-05-29 20:41:49 +000055explain ::= EXPLAIN. {pParse->explain = 1;}
56
drh382c0242001-10-06 16:33:02 +000057///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000058//
drhfa86c412002-02-02 15:01:15 +000059
drh0d65dc02002-02-03 00:56:09 +000060cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}
drhc4a3c772001-04-04 11:48:57 +000061trans_opt ::= .
62trans_opt ::= TRANSACTION.
63trans_opt ::= TRANSACTION ids.
64cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
65cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
66cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
67
drh382c0242001-10-06 16:33:02 +000068///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +000069//
70cmd ::= create_table create_table_args.
drh969fa7c2002-02-18 18:30:32 +000071create_table ::= CREATE(X) temp(T) TABLE ids(Y). {
72 sqliteStartTable(pParse,&X,&Y,T);
73}
drhf57b3392001-10-08 13:22:32 +000074%type temp {int}
75temp(A) ::= TEMP. {A = 1;}
76temp(A) ::= . {A = 0;}
drh969fa7c2002-02-18 18:30:32 +000077create_table_args ::= LP columnlist conslist_opt RP(X). {
78 sqliteEndTable(pParse,&X,0);
79}
80create_table_args ::= AS select(S). {
81 sqliteEndTable(pParse,0,S);
82 sqliteSelectDelete(S);
83}
drh348784e2000-05-29 20:41:49 +000084columnlist ::= columnlist COMMA column.
85columnlist ::= column.
86
87// About the only information used for a column is the name of the
88// column. The type is always just "text". But the code will accept
89// an elaborate typename. Perhaps someday we'll do something with it.
90//
91column ::= columnid type carglist.
drhc4a3c772001-04-04 11:48:57 +000092columnid ::= ids(X). {sqliteAddColumn(pParse,&X);}
93
94// An IDENTIFIER can be a generic identifier, or one of several
95// keywords. Any non-standard keyword can also be an identifier.
drh382c0242001-10-06 16:33:02 +000096// We also make DESC and identifier since it comes up so often (as
97// an abbreviation of "description").
drhc4a3c772001-04-04 11:48:57 +000098//
drh982cef72000-05-30 16:27:03 +000099%type id {Token}
drh9cfcf5d2002-01-29 18:41:24 +0000100id(A) ::= ABORT(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000101id(A) ::= ASC(X). {A = X;}
102id(A) ::= BEGIN(X). {A = X;}
103id(A) ::= CLUSTER(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000104id(A) ::= CONFLICT(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000105id(A) ::= COPY(X). {A = X;}
106id(A) ::= DELIMITERS(X). {A = X;}
107id(A) ::= DESC(X). {A = X;}
108id(A) ::= END(X). {A = X;}
109id(A) ::= EXPLAIN(X). {A = X;}
110id(A) ::= FAIL(X). {A = X;}
111id(A) ::= ID(X). {A = X;}
112id(A) ::= IGNORE(X). {A = X;}
113id(A) ::= KEY(X). {A = X;}
114id(A) ::= OFFSET(X). {A = X;}
115id(A) ::= PRAGMA(X). {A = X;}
116id(A) ::= REPLACE(X). {A = X;}
117id(A) ::= TEMP(X). {A = X;}
118id(A) ::= VACUUM(X). {A = X;}
119id(A) ::= VIEW(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000120
121// And "ids" is an identifer-or-string.
122//
123%type ids {Token}
124ids(A) ::= id(X). {A = X;}
125ids(A) ::= STRING(X). {A = X;}
126
drh382c0242001-10-06 16:33:02 +0000127type ::= .
128type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
129type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
130type ::= typename(X) LP signed COMMA signed RP(Y).
131 {sqliteAddColumnType(pParse,&X,&Y);}
132%type typename {Token}
133typename(A) ::= ids(X). {A = X;}
134typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000135signed ::= INTEGER.
136signed ::= PLUS INTEGER.
137signed ::= MINUS INTEGER.
138carglist ::= carglist carg.
139carglist ::= .
drhc4a3c772001-04-04 11:48:57 +0000140carg ::= CONSTRAINT ids ccons.
drh348784e2000-05-29 20:41:49 +0000141carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000142carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
143carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
144carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
145carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
146carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
147carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
148carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
149carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
150carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000151
drh382c0242001-10-06 16:33:02 +0000152// In addition to the type name, we also care about the primary key and
153// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000154//
drh9cfcf5d2002-01-29 18:41:24 +0000155ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
156ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
157ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
158ccons ::= CHECK LP expr RP onconf.
drh348784e2000-05-29 20:41:49 +0000159
160// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000161// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000162//
163conslist_opt ::= .
164conslist_opt ::= COMMA conslist.
165conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000166conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000167conslist ::= tcons.
drhc4a3c772001-04-04 11:48:57 +0000168tcons ::= CONSTRAINT ids.
drh9cfcf5d2002-01-29 18:41:24 +0000169tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
170 {sqliteAddPrimaryKey(pParse,X,R);}
171tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
172 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
173tcons ::= CHECK expr onconf.
174
175// The following is a non-standard extension that allows us to declare the
176// default behavior when there is a constraint conflict.
177//
178%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000179%type orconf {int}
180%type resolvetype {int}
181onconf(A) ::= . { A = OE_Default; }
182onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
183orconf(A) ::= . { A = OE_Default; }
184orconf(A) ::= OR resolvetype(X). { A = X; }
185resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
186resolvetype(A) ::= ABORT. { A = OE_Abort; }
187resolvetype(A) ::= FAIL. { A = OE_Fail; }
188resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
189resolvetype(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000190
drh382c0242001-10-06 16:33:02 +0000191////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000192//
drh4ff6dfa2002-03-03 23:06:00 +0000193cmd ::= DROP TABLE ids(X). {sqliteDropTable(pParse,&X,0);}
drh348784e2000-05-29 20:41:49 +0000194
drha76b5df2002-02-23 02:32:10 +0000195///////////////////// The CREATE VIEW statement /////////////////////////////
196//
197cmd ::= CREATE(X) VIEW ids(Y) AS select(S). {
198 sqliteCreateView(pParse, &X, &Y, S);
199}
200cmd ::= DROP VIEW ids(X). {
drh4ff6dfa2002-03-03 23:06:00 +0000201 sqliteDropTable(pParse, &X, 1);
drha76b5df2002-02-23 02:32:10 +0000202}
203
drh382c0242001-10-06 16:33:02 +0000204//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000205//
drh9bb61fe2000-06-05 16:01:39 +0000206cmd ::= select(X). {
drh832508b2002-03-02 17:04:07 +0000207 sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
drh9bb61fe2000-06-05 16:01:39 +0000208 sqliteSelectDelete(X);
209}
drhefb72512000-05-31 20:00:52 +0000210
drh9bb61fe2000-06-05 16:01:39 +0000211%type select {Select*}
212%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000213%type oneselect {Select*}
214%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000215
drh82c3d632000-06-06 21:56:07 +0000216select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000217select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000218 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000219 Z->op = Y;
220 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000221 }
222 A = Z;
drh82c3d632000-06-06 21:56:07 +0000223}
drh0a36c572002-02-18 22:49:59 +0000224%type multiselect_op {int}
225multiselect_op(A) ::= UNION. {A = TK_UNION;}
226multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
227multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
228multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
drh82c3d632000-06-06 21:56:07 +0000229oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000230 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
231 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.a,L.b);
drh9bb61fe2000-06-05 16:01:39 +0000232}
233
234// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
235// present and false (0) if it is not.
236//
drhefb72512000-05-31 20:00:52 +0000237%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000238distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000239distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000240distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000241
drh9bb61fe2000-06-05 16:01:39 +0000242// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000243// values of the SELECT statement. The "*" in statements like
244// "SELECT * FROM ..." is encoded as a special expression with an
245// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000246//
drh348784e2000-05-29 20:41:49 +0000247%type selcollist {ExprList*}
248%destructor selcollist {sqliteExprListDelete($$);}
249%type sclp {ExprList*}
250%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000251sclp(A) ::= selcollist(X) COMMA. {A = X;}
252sclp(A) ::= . {A = 0;}
253selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drhc4a3c772001-04-04 11:48:57 +0000254selcollist(A) ::= sclp(P) expr(X) as ids(Y). {A = sqliteExprListAppend(P,X,&Y);}
drh7c917d12001-12-16 20:05:05 +0000255selcollist(A) ::= sclp(P) STAR. {
256 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
257}
drh54473222002-04-04 02:10:55 +0000258selcollist(A) ::= sclp(P) ids(X) DOT STAR. {
259 Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
260 Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
261 A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
262}
drh9bb61fe2000-06-05 16:01:39 +0000263as ::= .
264as ::= AS.
265
drh348784e2000-05-29 20:41:49 +0000266
267%type seltablist {IdList*}
268%destructor seltablist {sqliteIdListDelete($$);}
269%type stl_prefix {IdList*}
270%destructor stl_prefix {sqliteIdListDelete($$);}
271%type from {IdList*}
272%destructor from {sqliteIdListDelete($$);}
273
drhbf3a4fa2002-04-06 13:57:42 +0000274from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000275from(A) ::= FROM seltablist(X). {A = X;}
276stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
277stl_prefix(A) ::= . {A = 0;}
drhc4a3c772001-04-04 11:48:57 +0000278seltablist(A) ::= stl_prefix(X) ids(Y). {A = sqliteIdListAppend(X,&Y);}
279seltablist(A) ::= stl_prefix(X) ids(Y) as ids(Z). {
280 A = sqliteIdListAppend(X,&Y);
281 sqliteIdListAddAlias(A,&Z);
282}
drh22f70c32002-02-18 01:17:00 +0000283seltablist(A) ::= stl_prefix(X) LP select(S) RP. {
284 A = sqliteIdListAppend(X,0);
285 A->a[A->nId-1].pSelect = S;
286}
287seltablist(A) ::= stl_prefix(X) LP select(S) RP as ids(Z). {
288 A = sqliteIdListAppend(X,0);
289 A->a[A->nId-1].pSelect = S;
290 sqliteIdListAddAlias(A,&Z);
291}
drh348784e2000-05-29 20:41:49 +0000292
293%type orderby_opt {ExprList*}
294%destructor orderby_opt {sqliteExprListDelete($$);}
295%type sortlist {ExprList*}
296%destructor sortlist {sqliteExprListDelete($$);}
297%type sortitem {Expr*}
298%destructor sortitem {sqliteExprDelete($$);}
299
300orderby_opt(A) ::= . {A = 0;}
301orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000302sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
303 A = sqliteExprListAppend(X,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000304 if( A ) A->a[A->nExpr-1].sortOrder = Z; /* 0=ascending, 1=decending */
drh9bb61fe2000-06-05 16:01:39 +0000305}
306sortlist(A) ::= sortitem(Y) sortorder(Z). {
307 A = sqliteExprListAppend(0,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000308 if( A ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000309}
drhda9d6c42000-05-31 18:20:14 +0000310sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000311
312%type sortorder {int}
313
314sortorder(A) ::= ASC. {A = 0;}
315sortorder(A) ::= DESC. {A = 1;}
316sortorder(A) ::= . {A = 0;}
317
drh22827922000-06-06 17:27:05 +0000318%type groupby_opt {ExprList*}
319%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000320groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000321groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
322
323%type having_opt {Expr*}
324%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000325having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000326having_opt(A) ::= HAVING expr(X). {A = X;}
327
drh9bbca4c2001-11-06 04:00:18 +0000328%type limit_opt {struct twoint}
329limit_opt(A) ::= . {A.a = -1; A.b = 0;}
330limit_opt(A) ::= LIMIT INTEGER(X). {A.a = atoi(X.z); A.b = 0;}
331limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
332 {A.a = atoi(X.z); A.b = atoi(Y.z);}
333limit_sep ::= OFFSET.
334limit_sep ::= COMMA.
335
drh382c0242001-10-06 16:33:02 +0000336/////////////////////////// The DELETE statement /////////////////////////////
337//
drhc4a3c772001-04-04 11:48:57 +0000338cmd ::= DELETE FROM ids(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000339 {sqliteDeleteFrom(pParse, &X, Y);}
340
341%type where_opt {Expr*}
342%destructor where_opt {sqliteExprDelete($$);}
343
344where_opt(A) ::= . {A = 0;}
345where_opt(A) ::= WHERE expr(X). {A = X;}
346
347%type setlist {ExprList*}
348%destructor setlist {sqliteExprListDelete($$);}
349
drh382c0242001-10-06 16:33:02 +0000350////////////////////////// The UPDATE command ////////////////////////////////
351//
drh1c928532002-01-31 15:54:21 +0000352cmd ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z).
drh9cfcf5d2002-01-29 18:41:24 +0000353 {sqliteUpdate(pParse,&X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000354
drhc4a3c772001-04-04 11:48:57 +0000355setlist(A) ::= setlist(Z) COMMA ids(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000356 {A = sqliteExprListAppend(Z,Y,&X);}
drhc4a3c772001-04-04 11:48:57 +0000357setlist(A) ::= ids(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000358
drh382c0242001-10-06 16:33:02 +0000359////////////////////////// The INSERT command /////////////////////////////////
360//
drhfa86c412002-02-02 15:01:15 +0000361cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
drh9cfcf5d2002-01-29 18:41:24 +0000362 {sqliteInsert(pParse, &X, Y, 0, F, R);}
drhfa86c412002-02-02 15:01:15 +0000363cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) select(S).
drh9cfcf5d2002-01-29 18:41:24 +0000364 {sqliteInsert(pParse, &X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000365
drhfa86c412002-02-02 15:01:15 +0000366%type insert_cmd {int}
367insert_cmd(A) ::= INSERT orconf(R). {A = R;}
368insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
369
drh348784e2000-05-29 20:41:49 +0000370
371%type itemlist {ExprList*}
372%destructor itemlist {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000373
drhe64e7b22002-02-18 13:56:36 +0000374itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
375itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000376
drh967e8b72000-06-21 13:59:10 +0000377%type inscollist_opt {IdList*}
378%destructor inscollist_opt {sqliteIdListDelete($$);}
379%type inscollist {IdList*}
380%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000381
drhc4a3c772001-04-04 11:48:57 +0000382inscollist_opt(A) ::= . {A = 0;}
383inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
384inscollist(A) ::= inscollist(X) COMMA ids(Y). {A = sqliteIdListAppend(X,&Y);}
385inscollist(A) ::= ids(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000386
drh382c0242001-10-06 16:33:02 +0000387/////////////////////////// Expression Processing /////////////////////////////
388//
drh348784e2000-05-29 20:41:49 +0000389%left OR.
390%left AND.
drh8be51132000-06-03 19:19:41 +0000391%right NOT.
drhfef52082000-06-06 01:50:43 +0000392%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000393%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000394%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000395%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000396%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000397%left CONCAT.
drh81a20f22001-10-12 17:30:04 +0000398%right UMINUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000399
400%type expr {Expr*}
401%destructor expr {sqliteExprDelete($$);}
402
drhe1b6a5b2000-07-29 13:06:59 +0000403expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
drhe1b6a5b2000-07-29 13:06:59 +0000404expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drhc4a3c772001-04-04 11:48:57 +0000405expr(A) ::= id(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
406expr(A) ::= ids(X) DOT ids(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000407 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
408 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
409 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
410}
drh348784e2000-05-29 20:41:49 +0000411expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
412expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
413expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000414expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
415 A = sqliteExprFunction(Y, &X);
416 sqliteExprSpan(A,&X,&E);
417}
418expr(A) ::= ID(X) LP STAR RP(E). {
419 A = sqliteExprFunction(0, &X);
420 sqliteExprSpan(A,&X,&E);
421}
drh348784e2000-05-29 20:41:49 +0000422expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
423expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
424expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
425expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
426expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
427expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
428expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
429expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000430expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
431expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
432expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
433expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
drh0ac65892002-04-20 14:24:41 +0000434expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
435 ExprList *pList = sqliteExprListAppend(0, Y, 0);
436 pList = sqliteExprListAppend(pList, X, 0);
437 A = sqliteExprFunction(pList, &OP);
438 sqliteExprSpan(A, &X->span, &Y->span);
439}
440expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
441 ExprList *pList = sqliteExprListAppend(0, Y, 0);
442 pList = sqliteExprListAppend(pList, X, 0);
443 A = sqliteExprFunction(pList, &OP);
drh4794b982000-06-06 13:54:14 +0000444 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000445 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000446}
drh0ac65892002-04-20 14:24:41 +0000447likeop(A) ::= LIKE(X). {A = X;}
448likeop(A) ::= GLOB(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000449expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
450expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
451expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
452expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000453expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000454expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000455expr(A) ::= expr(X) ISNULL(E). {
456 A = sqliteExpr(TK_ISNULL, X, 0, 0);
457 sqliteExprSpan(A,&X->span,&E);
458}
drh33048c02001-10-01 14:29:22 +0000459expr(A) ::= expr(X) IS NULL(E). {
460 A = sqliteExpr(TK_ISNULL, X, 0, 0);
461 sqliteExprSpan(A,&X->span,&E);
462}
drhe1b6a5b2000-07-29 13:06:59 +0000463expr(A) ::= expr(X) NOTNULL(E). {
464 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
465 sqliteExprSpan(A,&X->span,&E);
466}
drh33048c02001-10-01 14:29:22 +0000467expr(A) ::= expr(X) NOT NULL(E). {
468 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
469 sqliteExprSpan(A,&X->span,&E);
470}
drh81a20f22001-10-12 17:30:04 +0000471expr(A) ::= expr(X) IS NOT NULL(E). {
472 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
473 sqliteExprSpan(A,&X->span,&E);
474}
drhe1b6a5b2000-07-29 13:06:59 +0000475expr(A) ::= NOT(B) expr(X). {
476 A = sqliteExpr(TK_NOT, X, 0, 0);
477 sqliteExprSpan(A,&B,&X->span);
478}
drh81a20f22001-10-12 17:30:04 +0000479expr(A) ::= BITNOT(B) expr(X). {
480 A = sqliteExpr(TK_BITNOT, X, 0, 0);
481 sqliteExprSpan(A,&B,&X->span);
482}
drhe1b6a5b2000-07-29 13:06:59 +0000483expr(A) ::= MINUS(B) expr(X). [UMINUS] {
484 A = sqliteExpr(TK_UMINUS, X, 0, 0);
485 sqliteExprSpan(A,&B,&X->span);
486}
487expr(A) ::= PLUS(B) expr(X). [UMINUS] {
488 A = X;
489 sqliteExprSpan(A,&B,&X->span);
490}
491expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000492 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000493 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000494 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000495}
drhfef52082000-06-06 01:50:43 +0000496expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
497 ExprList *pList = sqliteExprListAppend(0, X, 0);
498 pList = sqliteExprListAppend(pList, Y, 0);
499 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000500 if( A ) A->pList = pList;
drhe1b6a5b2000-07-29 13:06:59 +0000501 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000502}
drh4794b982000-06-06 13:54:14 +0000503expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
504 ExprList *pList = sqliteExprListAppend(0, X, 0);
505 pList = sqliteExprListAppend(pList, Y, 0);
506 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000507 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000508 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000509 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000510}
drhe1b6a5b2000-07-29 13:06:59 +0000511expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000512 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000513 if( A ) A->pList = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000514 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000515}
drhe1b6a5b2000-07-29 13:06:59 +0000516expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000517 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000518 if( A ) A->pSelect = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000519 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000520}
drhe1b6a5b2000-07-29 13:06:59 +0000521expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000522 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000523 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000524 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000525 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000526}
drhe1b6a5b2000-07-29 13:06:59 +0000527expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000528 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000529 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000530 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000531 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000532}
drhfef52082000-06-06 01:50:43 +0000533
drh17a7f8d2002-03-24 13:13:27 +0000534/* CASE expressions */
535expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
536 A = sqliteExpr(TK_CASE, X, Z, 0);
537 if( A ) A->pList = Y;
538 sqliteExprSpan(A, &C, &E);
539}
540%type case_exprlist {ExprList*}
541%destructor case_exprlist {sqliteExprListDelete($$);}
542case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
543 A = sqliteExprListAppend(X, Y, 0);
544 A = sqliteExprListAppend(A, Z, 0);
545}
546case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
547 A = sqliteExprListAppend(0, Y, 0);
548 A = sqliteExprListAppend(A, Z, 0);
549}
550%type case_else {Expr*}
551case_else(A) ::= ELSE expr(X). {A = X;}
552case_else(A) ::= . {A = 0;}
553%type case_operand {Expr*}
554case_operand(A) ::= expr(X). {A = X;}
555case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000556
557%type exprlist {ExprList*}
558%destructor exprlist {sqliteExprListDelete($$);}
559%type expritem {Expr*}
560%destructor expritem {sqliteExprDelete($$);}
561
drh348784e2000-05-29 20:41:49 +0000562exprlist(A) ::= exprlist(X) COMMA expritem(Y).
563 {A = sqliteExprListAppend(X,Y,0);}
564exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
565expritem(A) ::= expr(X). {A = X;}
566expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000567
drh382c0242001-10-06 16:33:02 +0000568///////////////////////////// The CREATE INDEX command ///////////////////////
569//
drh9cfcf5d2002-01-29 18:41:24 +0000570cmd ::= CREATE(S) uniqueflag(U) INDEX ids(X)
571 ON ids(Y) LP idxlist(Z) RP(E) onconf(R). {
572 if( U!=OE_None ) U = R;
573 if( U==OE_Default) U = OE_Abort;
574 sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);
575}
drh717e6402001-09-27 03:22:32 +0000576
577%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000578uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
579uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000580
581%type idxlist {IdList*}
582%destructor idxlist {sqliteIdListDelete($$);}
583%type idxitem {Token}
584
585idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
586 {A = sqliteIdListAppend(X,&Y);}
587idxlist(A) ::= idxitem(Y).
588 {A = sqliteIdListAppend(0,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000589idxitem(A) ::= ids(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000590
drh8aff1012001-12-22 14:49:24 +0000591///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000592//
593
drhc4a3c772001-04-04 11:48:57 +0000594cmd ::= DROP INDEX ids(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000595
drh382c0242001-10-06 16:33:02 +0000596
drh8aff1012001-12-22 14:49:24 +0000597///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000598//
drh1c928532002-01-31 15:54:21 +0000599cmd ::= COPY orconf(R) ids(X) FROM ids(Y) USING DELIMITERS STRING(Z).
drhb419a922002-01-30 16:17:23 +0000600 {sqliteCopy(pParse,&X,&Y,&Z,R);}
drh1c928532002-01-31 15:54:21 +0000601cmd ::= COPY orconf(R) ids(X) FROM ids(Y).
drhb419a922002-01-30 16:17:23 +0000602 {sqliteCopy(pParse,&X,&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000603
drh382c0242001-10-06 16:33:02 +0000604///////////////////////////// The VACUUM command /////////////////////////////
605//
drhdce2cbe2000-05-31 02:27:49 +0000606cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drhc4a3c772001-04-04 11:48:57 +0000607cmd ::= VACUUM ids(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000608
drh382c0242001-10-06 16:33:02 +0000609///////////////////////////// The PRAGMA command /////////////////////////////
610//
drhf57b14a2001-09-14 18:54:08 +0000611cmd ::= PRAGMA ids(X) EQ ids(Y). {sqlitePragma(pParse,&X,&Y,0);}
612cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
613cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
614cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh382c0242001-10-06 16:33:02 +0000615cmd ::= PRAGMA ids(X) LP ids(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drh603240c2002-03-05 01:11:12 +0000616cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
drhf57b14a2001-09-14 18:54:08 +0000617plus_num(A) ::= plus_opt number(X). {A = X;}
618minus_num(A) ::= MINUS number(X). {A = X;}
619number(A) ::= INTEGER(X). {A = X;}
620number(A) ::= FLOAT(X). {A = X;}
621plus_opt ::= PLUS.
622plus_opt ::= .