blob: ff24a585b1a16b83f9ea017672360718d460d2fe [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**
drh094b2bb2002-03-13 18:54:07 +000017** @(#) $Id: parse.y,v 1.57 2002/03/13 18:54:08 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}
drhc4a3c772001-04-04 11:48:57 +0000100id(A) ::= DESC(X). {A = X;}
101id(A) ::= ASC(X). {A = X;}
102id(A) ::= DELIMITERS(X). {A = X;}
103id(A) ::= EXPLAIN(X). {A = X;}
104id(A) ::= VACUUM(X). {A = X;}
105id(A) ::= BEGIN(X). {A = X;}
106id(A) ::= END(X). {A = X;}
drhf57b14a2001-09-14 18:54:08 +0000107id(A) ::= PRAGMA(X). {A = X;}
108id(A) ::= CLUSTER(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000109id(A) ::= ID(X). {A = X;}
drhf57b3392001-10-08 13:22:32 +0000110id(A) ::= TEMP(X). {A = X;}
drh9bbca4c2001-11-06 04:00:18 +0000111id(A) ::= OFFSET(X). {A = X;}
drhaacc5432002-01-06 17:07:40 +0000112id(A) ::= KEY(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000113id(A) ::= ABORT(X). {A = X;}
114id(A) ::= IGNORE(X). {A = X;}
115id(A) ::= REPLACE(X). {A = X;}
drh1c928532002-01-31 15:54:21 +0000116id(A) ::= FAIL(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000117id(A) ::= CONFLICT(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000118
119// And "ids" is an identifer-or-string.
120//
121%type ids {Token}
122ids(A) ::= id(X). {A = X;}
123ids(A) ::= STRING(X). {A = X;}
124
drh382c0242001-10-06 16:33:02 +0000125type ::= .
126type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
127type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
128type ::= typename(X) LP signed COMMA signed RP(Y).
129 {sqliteAddColumnType(pParse,&X,&Y);}
130%type typename {Token}
131typename(A) ::= ids(X). {A = X;}
132typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000133signed ::= INTEGER.
134signed ::= PLUS INTEGER.
135signed ::= MINUS INTEGER.
136carglist ::= carglist carg.
137carglist ::= .
drhc4a3c772001-04-04 11:48:57 +0000138carg ::= CONSTRAINT ids ccons.
drh348784e2000-05-29 20:41:49 +0000139carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000140carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
141carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
142carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
143carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
144carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
145carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
146carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
147carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
148carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000149
drh382c0242001-10-06 16:33:02 +0000150// In addition to the type name, we also care about the primary key and
151// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000152//
drh9cfcf5d2002-01-29 18:41:24 +0000153ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
154ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
155ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
156ccons ::= CHECK LP expr RP onconf.
drh348784e2000-05-29 20:41:49 +0000157
158// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000159// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000160//
161conslist_opt ::= .
162conslist_opt ::= COMMA conslist.
163conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000164conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000165conslist ::= tcons.
drhc4a3c772001-04-04 11:48:57 +0000166tcons ::= CONSTRAINT ids.
drh9cfcf5d2002-01-29 18:41:24 +0000167tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
168 {sqliteAddPrimaryKey(pParse,X,R);}
169tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
170 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
171tcons ::= CHECK expr onconf.
172
173// The following is a non-standard extension that allows us to declare the
174// default behavior when there is a constraint conflict.
175//
176%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000177%type orconf {int}
178%type resolvetype {int}
179onconf(A) ::= . { A = OE_Default; }
180onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
181orconf(A) ::= . { A = OE_Default; }
182orconf(A) ::= OR resolvetype(X). { A = X; }
183resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
184resolvetype(A) ::= ABORT. { A = OE_Abort; }
185resolvetype(A) ::= FAIL. { A = OE_Fail; }
186resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
187resolvetype(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000188
drh382c0242001-10-06 16:33:02 +0000189////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000190//
drh4ff6dfa2002-03-03 23:06:00 +0000191cmd ::= DROP TABLE ids(X). {sqliteDropTable(pParse,&X,0);}
drh348784e2000-05-29 20:41:49 +0000192
drha76b5df2002-02-23 02:32:10 +0000193///////////////////// The CREATE VIEW statement /////////////////////////////
194//
195cmd ::= CREATE(X) VIEW ids(Y) AS select(S). {
196 sqliteCreateView(pParse, &X, &Y, S);
197}
198cmd ::= DROP VIEW ids(X). {
drh4ff6dfa2002-03-03 23:06:00 +0000199 sqliteDropTable(pParse, &X, 1);
drha76b5df2002-02-23 02:32:10 +0000200}
201
drh382c0242001-10-06 16:33:02 +0000202//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000203//
drh9bb61fe2000-06-05 16:01:39 +0000204cmd ::= select(X). {
drh832508b2002-03-02 17:04:07 +0000205 sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
drh9bb61fe2000-06-05 16:01:39 +0000206 sqliteSelectDelete(X);
207}
drhefb72512000-05-31 20:00:52 +0000208
drh9bb61fe2000-06-05 16:01:39 +0000209%type select {Select*}
210%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000211%type oneselect {Select*}
212%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000213
drh82c3d632000-06-06 21:56:07 +0000214select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000215select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000216 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000217 Z->op = Y;
218 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000219 }
220 A = Z;
drh82c3d632000-06-06 21:56:07 +0000221}
drh0a36c572002-02-18 22:49:59 +0000222%type multiselect_op {int}
223multiselect_op(A) ::= UNION. {A = TK_UNION;}
224multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
225multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
226multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
drh82c3d632000-06-06 21:56:07 +0000227oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000228 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
229 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.a,L.b);
drh9bb61fe2000-06-05 16:01:39 +0000230}
231
232// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
233// present and false (0) if it is not.
234//
drhefb72512000-05-31 20:00:52 +0000235%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000236distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000237distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000238distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000239
drh9bb61fe2000-06-05 16:01:39 +0000240// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000241// values of the SELECT statement. The "*" in statements like
242// "SELECT * FROM ..." is encoded as a special expression with an
243// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000244//
drh348784e2000-05-29 20:41:49 +0000245%type selcollist {ExprList*}
246%destructor selcollist {sqliteExprListDelete($$);}
247%type sclp {ExprList*}
248%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000249sclp(A) ::= selcollist(X) COMMA. {A = X;}
250sclp(A) ::= . {A = 0;}
251selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drhc4a3c772001-04-04 11:48:57 +0000252selcollist(A) ::= sclp(P) expr(X) as ids(Y). {A = sqliteExprListAppend(P,X,&Y);}
drh7c917d12001-12-16 20:05:05 +0000253selcollist(A) ::= sclp(P) STAR. {
254 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
255}
drh9bb61fe2000-06-05 16:01:39 +0000256as ::= .
257as ::= AS.
258
drh348784e2000-05-29 20:41:49 +0000259
260%type seltablist {IdList*}
261%destructor seltablist {sqliteIdListDelete($$);}
262%type stl_prefix {IdList*}
263%destructor stl_prefix {sqliteIdListDelete($$);}
264%type from {IdList*}
265%destructor from {sqliteIdListDelete($$);}
266
267from(A) ::= FROM seltablist(X). {A = X;}
268stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
269stl_prefix(A) ::= . {A = 0;}
drhc4a3c772001-04-04 11:48:57 +0000270seltablist(A) ::= stl_prefix(X) ids(Y). {A = sqliteIdListAppend(X,&Y);}
271seltablist(A) ::= stl_prefix(X) ids(Y) as ids(Z). {
272 A = sqliteIdListAppend(X,&Y);
273 sqliteIdListAddAlias(A,&Z);
274}
drh22f70c32002-02-18 01:17:00 +0000275seltablist(A) ::= stl_prefix(X) LP select(S) RP. {
276 A = sqliteIdListAppend(X,0);
277 A->a[A->nId-1].pSelect = S;
278}
279seltablist(A) ::= stl_prefix(X) LP select(S) RP as ids(Z). {
280 A = sqliteIdListAppend(X,0);
281 A->a[A->nId-1].pSelect = S;
282 sqliteIdListAddAlias(A,&Z);
283}
drh348784e2000-05-29 20:41:49 +0000284
285%type orderby_opt {ExprList*}
286%destructor orderby_opt {sqliteExprListDelete($$);}
287%type sortlist {ExprList*}
288%destructor sortlist {sqliteExprListDelete($$);}
289%type sortitem {Expr*}
290%destructor sortitem {sqliteExprDelete($$);}
291
292orderby_opt(A) ::= . {A = 0;}
293orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000294sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
295 A = sqliteExprListAppend(X,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000296 if( A ) A->a[A->nExpr-1].sortOrder = Z; /* 0=ascending, 1=decending */
drh9bb61fe2000-06-05 16:01:39 +0000297}
298sortlist(A) ::= sortitem(Y) sortorder(Z). {
299 A = sqliteExprListAppend(0,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000300 if( A ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000301}
drhda9d6c42000-05-31 18:20:14 +0000302sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000303
304%type sortorder {int}
305
306sortorder(A) ::= ASC. {A = 0;}
307sortorder(A) ::= DESC. {A = 1;}
308sortorder(A) ::= . {A = 0;}
309
drh22827922000-06-06 17:27:05 +0000310%type groupby_opt {ExprList*}
311%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000312groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000313groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
314
315%type having_opt {Expr*}
316%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000317having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000318having_opt(A) ::= HAVING expr(X). {A = X;}
319
drh9bbca4c2001-11-06 04:00:18 +0000320%type limit_opt {struct twoint}
321limit_opt(A) ::= . {A.a = -1; A.b = 0;}
322limit_opt(A) ::= LIMIT INTEGER(X). {A.a = atoi(X.z); A.b = 0;}
323limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
324 {A.a = atoi(X.z); A.b = atoi(Y.z);}
325limit_sep ::= OFFSET.
326limit_sep ::= COMMA.
327
drh382c0242001-10-06 16:33:02 +0000328/////////////////////////// The DELETE statement /////////////////////////////
329//
drhc4a3c772001-04-04 11:48:57 +0000330cmd ::= DELETE FROM ids(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000331 {sqliteDeleteFrom(pParse, &X, Y);}
332
333%type where_opt {Expr*}
334%destructor where_opt {sqliteExprDelete($$);}
335
336where_opt(A) ::= . {A = 0;}
337where_opt(A) ::= WHERE expr(X). {A = X;}
338
339%type setlist {ExprList*}
340%destructor setlist {sqliteExprListDelete($$);}
341
drh382c0242001-10-06 16:33:02 +0000342////////////////////////// The UPDATE command ////////////////////////////////
343//
drh1c928532002-01-31 15:54:21 +0000344cmd ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z).
drh9cfcf5d2002-01-29 18:41:24 +0000345 {sqliteUpdate(pParse,&X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000346
drhc4a3c772001-04-04 11:48:57 +0000347setlist(A) ::= setlist(Z) COMMA ids(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000348 {A = sqliteExprListAppend(Z,Y,&X);}
drhc4a3c772001-04-04 11:48:57 +0000349setlist(A) ::= ids(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000350
drh382c0242001-10-06 16:33:02 +0000351////////////////////////// The INSERT command /////////////////////////////////
352//
drhfa86c412002-02-02 15:01:15 +0000353cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
drh9cfcf5d2002-01-29 18:41:24 +0000354 {sqliteInsert(pParse, &X, Y, 0, F, R);}
drhfa86c412002-02-02 15:01:15 +0000355cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) select(S).
drh9cfcf5d2002-01-29 18:41:24 +0000356 {sqliteInsert(pParse, &X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000357
drhfa86c412002-02-02 15:01:15 +0000358%type insert_cmd {int}
359insert_cmd(A) ::= INSERT orconf(R). {A = R;}
360insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
361
drh348784e2000-05-29 20:41:49 +0000362
363%type itemlist {ExprList*}
364%destructor itemlist {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000365
drhe64e7b22002-02-18 13:56:36 +0000366itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
367itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000368
drh967e8b72000-06-21 13:59:10 +0000369%type inscollist_opt {IdList*}
370%destructor inscollist_opt {sqliteIdListDelete($$);}
371%type inscollist {IdList*}
372%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000373
drhc4a3c772001-04-04 11:48:57 +0000374inscollist_opt(A) ::= . {A = 0;}
375inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
376inscollist(A) ::= inscollist(X) COMMA ids(Y). {A = sqliteIdListAppend(X,&Y);}
377inscollist(A) ::= ids(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000378
drh382c0242001-10-06 16:33:02 +0000379/////////////////////////// Expression Processing /////////////////////////////
380//
drh348784e2000-05-29 20:41:49 +0000381%left OR.
382%left AND.
drh8be51132000-06-03 19:19:41 +0000383%right NOT.
drhfef52082000-06-06 01:50:43 +0000384%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000385%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000386%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000387%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000388%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000389%left CONCAT.
drh81a20f22001-10-12 17:30:04 +0000390%right UMINUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000391
392%type expr {Expr*}
393%destructor expr {sqliteExprDelete($$);}
394
drhe1b6a5b2000-07-29 13:06:59 +0000395expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
drhe1b6a5b2000-07-29 13:06:59 +0000396expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drhc4a3c772001-04-04 11:48:57 +0000397expr(A) ::= id(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
398expr(A) ::= ids(X) DOT ids(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000399 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
400 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
401 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
402}
drh348784e2000-05-29 20:41:49 +0000403expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
404expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
405expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000406expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
407 A = sqliteExprFunction(Y, &X);
408 sqliteExprSpan(A,&X,&E);
409}
410expr(A) ::= ID(X) LP STAR RP(E). {
411 A = sqliteExprFunction(0, &X);
412 sqliteExprSpan(A,&X,&E);
413}
drh348784e2000-05-29 20:41:49 +0000414expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
415expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
416expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
417expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
418expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
419expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
420expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
421expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000422expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
423expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
424expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
425expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
426expr(A) ::= expr(X) LIKE expr(Y). {A = sqliteExpr(TK_LIKE, X, Y, 0);}
drh4794b982000-06-06 13:54:14 +0000427expr(A) ::= expr(X) NOT LIKE expr(Y). {
428 A = sqliteExpr(TK_LIKE, X, Y, 0);
429 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000430 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000431}
drhfef52082000-06-06 01:50:43 +0000432expr(A) ::= expr(X) GLOB expr(Y). {A = sqliteExpr(TK_GLOB,X,Y,0);}
drh4794b982000-06-06 13:54:14 +0000433expr(A) ::= expr(X) NOT GLOB expr(Y). {
434 A = sqliteExpr(TK_GLOB, X, Y, 0);
435 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000436 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000437}
drh348784e2000-05-29 20:41:49 +0000438expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
439expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
440expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
441expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000442expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000443expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000444expr(A) ::= expr(X) ISNULL(E). {
445 A = sqliteExpr(TK_ISNULL, X, 0, 0);
446 sqliteExprSpan(A,&X->span,&E);
447}
drh33048c02001-10-01 14:29:22 +0000448expr(A) ::= expr(X) IS NULL(E). {
449 A = sqliteExpr(TK_ISNULL, X, 0, 0);
450 sqliteExprSpan(A,&X->span,&E);
451}
drhe1b6a5b2000-07-29 13:06:59 +0000452expr(A) ::= expr(X) NOTNULL(E). {
453 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
454 sqliteExprSpan(A,&X->span,&E);
455}
drh33048c02001-10-01 14:29:22 +0000456expr(A) ::= expr(X) NOT NULL(E). {
457 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
458 sqliteExprSpan(A,&X->span,&E);
459}
drh81a20f22001-10-12 17:30:04 +0000460expr(A) ::= expr(X) IS NOT NULL(E). {
461 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
462 sqliteExprSpan(A,&X->span,&E);
463}
drhe1b6a5b2000-07-29 13:06:59 +0000464expr(A) ::= NOT(B) expr(X). {
465 A = sqliteExpr(TK_NOT, X, 0, 0);
466 sqliteExprSpan(A,&B,&X->span);
467}
drh81a20f22001-10-12 17:30:04 +0000468expr(A) ::= BITNOT(B) expr(X). {
469 A = sqliteExpr(TK_BITNOT, X, 0, 0);
470 sqliteExprSpan(A,&B,&X->span);
471}
drhe1b6a5b2000-07-29 13:06:59 +0000472expr(A) ::= MINUS(B) expr(X). [UMINUS] {
473 A = sqliteExpr(TK_UMINUS, X, 0, 0);
474 sqliteExprSpan(A,&B,&X->span);
475}
476expr(A) ::= PLUS(B) expr(X). [UMINUS] {
477 A = X;
478 sqliteExprSpan(A,&B,&X->span);
479}
480expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000481 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000482 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000483 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000484}
drhfef52082000-06-06 01:50:43 +0000485expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
486 ExprList *pList = sqliteExprListAppend(0, X, 0);
487 pList = sqliteExprListAppend(pList, Y, 0);
488 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000489 if( A ) A->pList = pList;
drhe1b6a5b2000-07-29 13:06:59 +0000490 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000491}
drh4794b982000-06-06 13:54:14 +0000492expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
493 ExprList *pList = sqliteExprListAppend(0, X, 0);
494 pList = sqliteExprListAppend(pList, Y, 0);
495 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000496 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000497 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000498 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000499}
drhe1b6a5b2000-07-29 13:06:59 +0000500expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000501 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000502 if( A ) A->pList = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000503 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000504}
drhe1b6a5b2000-07-29 13:06:59 +0000505expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000506 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000507 if( A ) A->pSelect = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000508 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000509}
drhe1b6a5b2000-07-29 13:06:59 +0000510expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000511 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000512 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000513 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000514 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000515}
drhe1b6a5b2000-07-29 13:06:59 +0000516expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000517 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000518 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000519 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000520 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000521}
drhfef52082000-06-06 01:50:43 +0000522
523
drh348784e2000-05-29 20:41:49 +0000524
525%type exprlist {ExprList*}
526%destructor exprlist {sqliteExprListDelete($$);}
527%type expritem {Expr*}
528%destructor expritem {sqliteExprDelete($$);}
529
drh348784e2000-05-29 20:41:49 +0000530exprlist(A) ::= exprlist(X) COMMA expritem(Y).
531 {A = sqliteExprListAppend(X,Y,0);}
532exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
533expritem(A) ::= expr(X). {A = X;}
534expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000535
drh382c0242001-10-06 16:33:02 +0000536///////////////////////////// The CREATE INDEX command ///////////////////////
537//
drh9cfcf5d2002-01-29 18:41:24 +0000538cmd ::= CREATE(S) uniqueflag(U) INDEX ids(X)
539 ON ids(Y) LP idxlist(Z) RP(E) onconf(R). {
540 if( U!=OE_None ) U = R;
541 if( U==OE_Default) U = OE_Abort;
542 sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);
543}
drh717e6402001-09-27 03:22:32 +0000544
545%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000546uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
547uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000548
549%type idxlist {IdList*}
550%destructor idxlist {sqliteIdListDelete($$);}
551%type idxitem {Token}
552
553idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
554 {A = sqliteIdListAppend(X,&Y);}
555idxlist(A) ::= idxitem(Y).
556 {A = sqliteIdListAppend(0,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000557idxitem(A) ::= ids(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000558
drh8aff1012001-12-22 14:49:24 +0000559///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000560//
561
drhc4a3c772001-04-04 11:48:57 +0000562cmd ::= DROP INDEX ids(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000563
drh382c0242001-10-06 16:33:02 +0000564
drh8aff1012001-12-22 14:49:24 +0000565///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000566//
drh1c928532002-01-31 15:54:21 +0000567cmd ::= COPY orconf(R) ids(X) FROM ids(Y) USING DELIMITERS STRING(Z).
drhb419a922002-01-30 16:17:23 +0000568 {sqliteCopy(pParse,&X,&Y,&Z,R);}
drh1c928532002-01-31 15:54:21 +0000569cmd ::= COPY orconf(R) ids(X) FROM ids(Y).
drhb419a922002-01-30 16:17:23 +0000570 {sqliteCopy(pParse,&X,&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000571
drh382c0242001-10-06 16:33:02 +0000572///////////////////////////// The VACUUM command /////////////////////////////
573//
drhdce2cbe2000-05-31 02:27:49 +0000574cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drhc4a3c772001-04-04 11:48:57 +0000575cmd ::= VACUUM ids(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000576
drh382c0242001-10-06 16:33:02 +0000577///////////////////////////// The PRAGMA command /////////////////////////////
578//
drhf57b14a2001-09-14 18:54:08 +0000579cmd ::= PRAGMA ids(X) EQ ids(Y). {sqlitePragma(pParse,&X,&Y,0);}
580cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
581cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
582cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh382c0242001-10-06 16:33:02 +0000583cmd ::= PRAGMA ids(X) LP ids(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drh603240c2002-03-05 01:11:12 +0000584cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
drhf57b14a2001-09-14 18:54:08 +0000585plus_num(A) ::= plus_opt number(X). {A = X;}
586minus_num(A) ::= MINUS number(X). {A = X;}
587number(A) ::= INTEGER(X). {A = X;}
588number(A) ::= FLOAT(X). {A = X;}
589plus_opt ::= PLUS.
590plus_opt ::= .