blob: c225cfb90a7d0ca1588b8e7449bd50c9d946e78e [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**
drhb419a922002-01-30 16:17:23 +000017** @(#) $Id: parse.y,v 1.44 2002/01/30 16:17:24 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.
51cmdlist ::= cmdlist SEMI ecmd.
52ecmd ::= explain cmd. {sqliteExec(pParse);}
53ecmd ::= cmd. {sqliteExec(pParse);}
54ecmd ::= .
55explain ::= EXPLAIN. {pParse->explain = 1;}
56
drh382c0242001-10-06 16:33:02 +000057///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000058//
59cmd ::= BEGIN trans_opt. {sqliteBeginTransaction(pParse);}
60trans_opt ::= .
61trans_opt ::= TRANSACTION.
62trans_opt ::= TRANSACTION ids.
63cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
64cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
65cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
66
drh382c0242001-10-06 16:33:02 +000067///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +000068//
69cmd ::= create_table create_table_args.
drhf57b3392001-10-08 13:22:32 +000070create_table ::= CREATE(X) temp(T) TABLE ids(Y).
71 {sqliteStartTable(pParse,&X,&Y,T);}
72%type temp {int}
73temp(A) ::= TEMP. {A = 1;}
74temp(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +000075create_table_args ::= LP columnlist conslist_opt RP(X).
76 {sqliteEndTable(pParse,&X);}
77columnlist ::= columnlist COMMA column.
78columnlist ::= column.
79
80// About the only information used for a column is the name of the
81// column. The type is always just "text". But the code will accept
82// an elaborate typename. Perhaps someday we'll do something with it.
83//
84column ::= columnid type carglist.
drhc4a3c772001-04-04 11:48:57 +000085columnid ::= ids(X). {sqliteAddColumn(pParse,&X);}
86
87// An IDENTIFIER can be a generic identifier, or one of several
88// keywords. Any non-standard keyword can also be an identifier.
drh382c0242001-10-06 16:33:02 +000089// We also make DESC and identifier since it comes up so often (as
90// an abbreviation of "description").
drhc4a3c772001-04-04 11:48:57 +000091//
drh982cef72000-05-30 16:27:03 +000092%type id {Token}
drhc4a3c772001-04-04 11:48:57 +000093id(A) ::= DESC(X). {A = X;}
94id(A) ::= ASC(X). {A = X;}
95id(A) ::= DELIMITERS(X). {A = X;}
96id(A) ::= EXPLAIN(X). {A = X;}
97id(A) ::= VACUUM(X). {A = X;}
98id(A) ::= BEGIN(X). {A = X;}
99id(A) ::= END(X). {A = X;}
drhf57b14a2001-09-14 18:54:08 +0000100id(A) ::= PRAGMA(X). {A = X;}
101id(A) ::= CLUSTER(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000102id(A) ::= ID(X). {A = X;}
drhf57b3392001-10-08 13:22:32 +0000103id(A) ::= TEMP(X). {A = X;}
drh9bbca4c2001-11-06 04:00:18 +0000104id(A) ::= OFFSET(X). {A = X;}
drhaacc5432002-01-06 17:07:40 +0000105id(A) ::= KEY(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000106id(A) ::= ABORT(X). {A = X;}
107id(A) ::= IGNORE(X). {A = X;}
108id(A) ::= REPLACE(X). {A = X;}
109id(A) ::= CONFLICT(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000110
111// And "ids" is an identifer-or-string.
112//
113%type ids {Token}
114ids(A) ::= id(X). {A = X;}
115ids(A) ::= STRING(X). {A = X;}
116
drh382c0242001-10-06 16:33:02 +0000117type ::= .
118type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
119type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
120type ::= typename(X) LP signed COMMA signed RP(Y).
121 {sqliteAddColumnType(pParse,&X,&Y);}
122%type typename {Token}
123typename(A) ::= ids(X). {A = X;}
124typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000125signed ::= INTEGER.
126signed ::= PLUS INTEGER.
127signed ::= MINUS INTEGER.
128carglist ::= carglist carg.
129carglist ::= .
drhc4a3c772001-04-04 11:48:57 +0000130carg ::= CONSTRAINT ids ccons.
drh348784e2000-05-29 20:41:49 +0000131carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000132carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
133carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
134carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
135carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
136carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
137carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
138carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
139carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
140carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000141
drh382c0242001-10-06 16:33:02 +0000142// In addition to the type name, we also care about the primary key and
143// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000144//
drh9cfcf5d2002-01-29 18:41:24 +0000145ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
146ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
147ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
148ccons ::= CHECK LP expr RP onconf.
drh348784e2000-05-29 20:41:49 +0000149
150// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000151// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000152//
153conslist_opt ::= .
154conslist_opt ::= COMMA conslist.
155conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000156conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000157conslist ::= tcons.
drhc4a3c772001-04-04 11:48:57 +0000158tcons ::= CONSTRAINT ids.
drh9cfcf5d2002-01-29 18:41:24 +0000159tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
160 {sqliteAddPrimaryKey(pParse,X,R);}
161tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
162 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
163tcons ::= CHECK expr onconf.
164
165// The following is a non-standard extension that allows us to declare the
166// default behavior when there is a constraint conflict.
167//
168%type onconf {int}
169%type onconf_u {int}
170%type confresolve {int}
171onconf(A) ::= confresolve(X). { A = X; }
172onconf(A) ::= onconf_u(X). { A = X; }
173onconf_u(A) ::= ON CONFLICT confresolve(X). { A = X; }
174onconf_u(A) ::= . { A = OE_Default; }
175confresolve(A) ::= ABORT. { A = OE_Abort; }
176confresolve(A) ::= IGNORE. { A = OE_Ignore; }
177confresolve(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000178
drh382c0242001-10-06 16:33:02 +0000179////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000180//
drhc4a3c772001-04-04 11:48:57 +0000181cmd ::= DROP TABLE ids(X). {sqliteDropTable(pParse,&X);}
drh348784e2000-05-29 20:41:49 +0000182
drh382c0242001-10-06 16:33:02 +0000183//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000184//
drh9bb61fe2000-06-05 16:01:39 +0000185cmd ::= select(X). {
drhfef52082000-06-06 01:50:43 +0000186 sqliteSelect(pParse, X, SRT_Callback, 0);
drh9bb61fe2000-06-05 16:01:39 +0000187 sqliteSelectDelete(X);
188}
drhefb72512000-05-31 20:00:52 +0000189
drh9bb61fe2000-06-05 16:01:39 +0000190%type select {Select*}
191%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000192%type oneselect {Select*}
193%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000194
drh82c3d632000-06-06 21:56:07 +0000195select(A) ::= oneselect(X). {A = X;}
196select(A) ::= select(X) joinop(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000197 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000198 Z->op = Y;
199 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000200 }
201 A = Z;
drh82c3d632000-06-06 21:56:07 +0000202}
203%type joinop {int}
204joinop(A) ::= UNION. {A = TK_UNION;}
205joinop(A) ::= UNION ALL. {A = TK_ALL;}
206joinop(A) ::= INTERSECT. {A = TK_INTERSECT;}
207joinop(A) ::= EXCEPT. {A = TK_EXCEPT;}
208oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000209 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
210 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.a,L.b);
drh9bb61fe2000-06-05 16:01:39 +0000211}
212
213// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
214// present and false (0) if it is not.
215//
drhefb72512000-05-31 20:00:52 +0000216%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000217distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000218distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000219distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000220
drh9bb61fe2000-06-05 16:01:39 +0000221// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000222// values of the SELECT statement. The "*" in statements like
223// "SELECT * FROM ..." is encoded as a special expression with an
224// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000225//
drh348784e2000-05-29 20:41:49 +0000226%type selcollist {ExprList*}
227%destructor selcollist {sqliteExprListDelete($$);}
228%type sclp {ExprList*}
229%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000230sclp(A) ::= selcollist(X) COMMA. {A = X;}
231sclp(A) ::= . {A = 0;}
232selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drhc4a3c772001-04-04 11:48:57 +0000233selcollist(A) ::= sclp(P) expr(X) as ids(Y). {A = sqliteExprListAppend(P,X,&Y);}
drh7c917d12001-12-16 20:05:05 +0000234selcollist(A) ::= sclp(P) STAR. {
235 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
236}
drh9bb61fe2000-06-05 16:01:39 +0000237as ::= .
238as ::= AS.
239
drh348784e2000-05-29 20:41:49 +0000240
241%type seltablist {IdList*}
242%destructor seltablist {sqliteIdListDelete($$);}
243%type stl_prefix {IdList*}
244%destructor stl_prefix {sqliteIdListDelete($$);}
245%type from {IdList*}
246%destructor from {sqliteIdListDelete($$);}
247
248from(A) ::= FROM seltablist(X). {A = X;}
249stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
250stl_prefix(A) ::= . {A = 0;}
drhc4a3c772001-04-04 11:48:57 +0000251seltablist(A) ::= stl_prefix(X) ids(Y). {A = sqliteIdListAppend(X,&Y);}
252seltablist(A) ::= stl_prefix(X) ids(Y) as ids(Z). {
253 A = sqliteIdListAppend(X,&Y);
254 sqliteIdListAddAlias(A,&Z);
255}
drh348784e2000-05-29 20:41:49 +0000256
257%type orderby_opt {ExprList*}
258%destructor orderby_opt {sqliteExprListDelete($$);}
259%type sortlist {ExprList*}
260%destructor sortlist {sqliteExprListDelete($$);}
261%type sortitem {Expr*}
262%destructor sortitem {sqliteExprDelete($$);}
263
264orderby_opt(A) ::= . {A = 0;}
265orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000266sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
267 A = sqliteExprListAppend(X,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000268 if( A ) A->a[A->nExpr-1].sortOrder = Z; /* 0=ascending, 1=decending */
drh9bb61fe2000-06-05 16:01:39 +0000269}
270sortlist(A) ::= sortitem(Y) sortorder(Z). {
271 A = sqliteExprListAppend(0,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000272 if( A ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000273}
drhda9d6c42000-05-31 18:20:14 +0000274sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000275
276%type sortorder {int}
277
278sortorder(A) ::= ASC. {A = 0;}
279sortorder(A) ::= DESC. {A = 1;}
280sortorder(A) ::= . {A = 0;}
281
drh22827922000-06-06 17:27:05 +0000282%type groupby_opt {ExprList*}
283%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000284groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000285groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
286
287%type having_opt {Expr*}
288%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000289having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000290having_opt(A) ::= HAVING expr(X). {A = X;}
291
drh9bbca4c2001-11-06 04:00:18 +0000292%type limit_opt {struct twoint}
293limit_opt(A) ::= . {A.a = -1; A.b = 0;}
294limit_opt(A) ::= LIMIT INTEGER(X). {A.a = atoi(X.z); A.b = 0;}
295limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
296 {A.a = atoi(X.z); A.b = atoi(Y.z);}
297limit_sep ::= OFFSET.
298limit_sep ::= COMMA.
299
drh382c0242001-10-06 16:33:02 +0000300/////////////////////////// The DELETE statement /////////////////////////////
301//
drhc4a3c772001-04-04 11:48:57 +0000302cmd ::= DELETE FROM ids(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000303 {sqliteDeleteFrom(pParse, &X, Y);}
304
305%type where_opt {Expr*}
306%destructor where_opt {sqliteExprDelete($$);}
307
308where_opt(A) ::= . {A = 0;}
309where_opt(A) ::= WHERE expr(X). {A = X;}
310
311%type setlist {ExprList*}
312%destructor setlist {sqliteExprListDelete($$);}
313
drh382c0242001-10-06 16:33:02 +0000314////////////////////////// The UPDATE command ////////////////////////////////
315//
drh9cfcf5d2002-01-29 18:41:24 +0000316cmd ::= UPDATE onconf_u(R) ids(X) SET setlist(Y) where_opt(Z).
317 {sqliteUpdate(pParse,&X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000318
drhc4a3c772001-04-04 11:48:57 +0000319setlist(A) ::= setlist(Z) COMMA ids(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000320 {A = sqliteExprListAppend(Z,Y,&X);}
drhc4a3c772001-04-04 11:48:57 +0000321setlist(A) ::= ids(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000322
drh382c0242001-10-06 16:33:02 +0000323////////////////////////// The INSERT command /////////////////////////////////
324//
drh9cfcf5d2002-01-29 18:41:24 +0000325cmd ::= INSERT onconf(R) INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
326 {sqliteInsert(pParse, &X, Y, 0, F, R);}
327cmd ::= INSERT onconf(R) INTO ids(X) inscollist_opt(F) select(S).
328 {sqliteInsert(pParse, &X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000329
330
331%type itemlist {ExprList*}
332%destructor itemlist {sqliteExprListDelete($$);}
333%type item {Expr*}
334%destructor item {sqliteExprDelete($$);}
335
336itemlist(A) ::= itemlist(X) COMMA item(Y). {A = sqliteExprListAppend(X,Y,0);}
337itemlist(A) ::= item(X). {A = sqliteExprListAppend(0,X,0);}
338item(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000339item(A) ::= PLUS INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
340item(A) ::= MINUS INTEGER(X). {
drh6e142f52000-06-08 13:36:40 +0000341 A = sqliteExpr(TK_UMINUS, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000342 if( A ) A->pLeft = sqliteExpr(TK_INTEGER, 0, 0, &X);
drh7020f652000-06-03 18:06:52 +0000343}
drh348784e2000-05-29 20:41:49 +0000344item(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000345item(A) ::= PLUS FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
346item(A) ::= MINUS FLOAT(X). {
drh6e142f52000-06-08 13:36:40 +0000347 A = sqliteExpr(TK_UMINUS, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000348 if( A ) A->pLeft = sqliteExpr(TK_FLOAT, 0, 0, &X);
drh7020f652000-06-03 18:06:52 +0000349}
drh348784e2000-05-29 20:41:49 +0000350item(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drh8be51132000-06-03 19:19:41 +0000351item(A) ::= NULL. {A = sqliteExpr(TK_NULL, 0, 0, 0);}
drh348784e2000-05-29 20:41:49 +0000352
drh967e8b72000-06-21 13:59:10 +0000353%type inscollist_opt {IdList*}
354%destructor inscollist_opt {sqliteIdListDelete($$);}
355%type inscollist {IdList*}
356%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000357
drhc4a3c772001-04-04 11:48:57 +0000358inscollist_opt(A) ::= . {A = 0;}
359inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
360inscollist(A) ::= inscollist(X) COMMA ids(Y). {A = sqliteIdListAppend(X,&Y);}
361inscollist(A) ::= ids(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000362
drh382c0242001-10-06 16:33:02 +0000363/////////////////////////// Expression Processing /////////////////////////////
364//
drh348784e2000-05-29 20:41:49 +0000365%left OR.
366%left AND.
drh8be51132000-06-03 19:19:41 +0000367%right NOT.
drhfef52082000-06-06 01:50:43 +0000368%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000369%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000370%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000371%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000372%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000373%left CONCAT.
drh81a20f22001-10-12 17:30:04 +0000374%right UMINUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000375
376%type expr {Expr*}
377%destructor expr {sqliteExprDelete($$);}
378
drhe1b6a5b2000-07-29 13:06:59 +0000379expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
drhe1b6a5b2000-07-29 13:06:59 +0000380expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drhc4a3c772001-04-04 11:48:57 +0000381expr(A) ::= id(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
382expr(A) ::= ids(X) DOT ids(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000383 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
384 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
385 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
386}
drh348784e2000-05-29 20:41:49 +0000387expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
388expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
389expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000390expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
391 A = sqliteExprFunction(Y, &X);
392 sqliteExprSpan(A,&X,&E);
393}
394expr(A) ::= ID(X) LP STAR RP(E). {
395 A = sqliteExprFunction(0, &X);
396 sqliteExprSpan(A,&X,&E);
397}
drh348784e2000-05-29 20:41:49 +0000398expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
399expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
400expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
401expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
402expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
403expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
404expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
405expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000406expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
407expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
408expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
409expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
410expr(A) ::= expr(X) LIKE expr(Y). {A = sqliteExpr(TK_LIKE, X, Y, 0);}
drh4794b982000-06-06 13:54:14 +0000411expr(A) ::= expr(X) NOT LIKE expr(Y). {
412 A = sqliteExpr(TK_LIKE, X, Y, 0);
413 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000414 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000415}
drhfef52082000-06-06 01:50:43 +0000416expr(A) ::= expr(X) GLOB expr(Y). {A = sqliteExpr(TK_GLOB,X,Y,0);}
drh4794b982000-06-06 13:54:14 +0000417expr(A) ::= expr(X) NOT GLOB expr(Y). {
418 A = sqliteExpr(TK_GLOB, X, Y, 0);
419 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000420 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000421}
drh348784e2000-05-29 20:41:49 +0000422expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
423expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
424expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
425expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000426expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000427expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000428expr(A) ::= expr(X) ISNULL(E). {
429 A = sqliteExpr(TK_ISNULL, X, 0, 0);
430 sqliteExprSpan(A,&X->span,&E);
431}
drh33048c02001-10-01 14:29:22 +0000432expr(A) ::= expr(X) IS NULL(E). {
433 A = sqliteExpr(TK_ISNULL, X, 0, 0);
434 sqliteExprSpan(A,&X->span,&E);
435}
drhe1b6a5b2000-07-29 13:06:59 +0000436expr(A) ::= expr(X) NOTNULL(E). {
437 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
438 sqliteExprSpan(A,&X->span,&E);
439}
drh33048c02001-10-01 14:29:22 +0000440expr(A) ::= expr(X) NOT NULL(E). {
441 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
442 sqliteExprSpan(A,&X->span,&E);
443}
drh81a20f22001-10-12 17:30:04 +0000444expr(A) ::= expr(X) IS NOT NULL(E). {
445 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
446 sqliteExprSpan(A,&X->span,&E);
447}
drhe1b6a5b2000-07-29 13:06:59 +0000448expr(A) ::= NOT(B) expr(X). {
449 A = sqliteExpr(TK_NOT, X, 0, 0);
450 sqliteExprSpan(A,&B,&X->span);
451}
drh81a20f22001-10-12 17:30:04 +0000452expr(A) ::= BITNOT(B) expr(X). {
453 A = sqliteExpr(TK_BITNOT, X, 0, 0);
454 sqliteExprSpan(A,&B,&X->span);
455}
drhe1b6a5b2000-07-29 13:06:59 +0000456expr(A) ::= MINUS(B) expr(X). [UMINUS] {
457 A = sqliteExpr(TK_UMINUS, X, 0, 0);
458 sqliteExprSpan(A,&B,&X->span);
459}
460expr(A) ::= PLUS(B) expr(X). [UMINUS] {
461 A = X;
462 sqliteExprSpan(A,&B,&X->span);
463}
464expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000465 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000466 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000467 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000468}
drhfef52082000-06-06 01:50:43 +0000469expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
470 ExprList *pList = sqliteExprListAppend(0, X, 0);
471 pList = sqliteExprListAppend(pList, Y, 0);
472 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000473 if( A ) A->pList = pList;
drhe1b6a5b2000-07-29 13:06:59 +0000474 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000475}
drh4794b982000-06-06 13:54:14 +0000476expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
477 ExprList *pList = sqliteExprListAppend(0, X, 0);
478 pList = sqliteExprListAppend(pList, Y, 0);
479 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000480 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000481 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000482 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000483}
drhe1b6a5b2000-07-29 13:06:59 +0000484expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000485 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000486 if( A ) A->pList = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000487 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000488}
drhe1b6a5b2000-07-29 13:06:59 +0000489expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000490 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000491 if( A ) A->pSelect = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000492 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000493}
drhe1b6a5b2000-07-29 13:06:59 +0000494expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000495 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000496 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000497 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000498 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000499}
drhe1b6a5b2000-07-29 13:06:59 +0000500expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000501 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000502 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000503 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000504 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000505}
drhfef52082000-06-06 01:50:43 +0000506
507
drh348784e2000-05-29 20:41:49 +0000508
509%type exprlist {ExprList*}
510%destructor exprlist {sqliteExprListDelete($$);}
511%type expritem {Expr*}
512%destructor expritem {sqliteExprDelete($$);}
513
drh348784e2000-05-29 20:41:49 +0000514exprlist(A) ::= exprlist(X) COMMA expritem(Y).
515 {A = sqliteExprListAppend(X,Y,0);}
516exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
517expritem(A) ::= expr(X). {A = X;}
518expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000519
drh382c0242001-10-06 16:33:02 +0000520///////////////////////////// The CREATE INDEX command ///////////////////////
521//
drh9cfcf5d2002-01-29 18:41:24 +0000522cmd ::= CREATE(S) uniqueflag(U) INDEX ids(X)
523 ON ids(Y) LP idxlist(Z) RP(E) onconf(R). {
524 if( U!=OE_None ) U = R;
525 if( U==OE_Default) U = OE_Abort;
526 sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);
527}
drh717e6402001-09-27 03:22:32 +0000528
529%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000530uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
531uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000532
533%type idxlist {IdList*}
534%destructor idxlist {sqliteIdListDelete($$);}
535%type idxitem {Token}
536
537idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
538 {A = sqliteIdListAppend(X,&Y);}
539idxlist(A) ::= idxitem(Y).
540 {A = sqliteIdListAppend(0,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000541idxitem(A) ::= ids(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000542
drh8aff1012001-12-22 14:49:24 +0000543///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000544//
545
drhc4a3c772001-04-04 11:48:57 +0000546cmd ::= DROP INDEX ids(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000547
drh382c0242001-10-06 16:33:02 +0000548
drh8aff1012001-12-22 14:49:24 +0000549///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000550//
drhb419a922002-01-30 16:17:23 +0000551cmd ::= COPY onconf_u(R) ids(X) FROM ids(Y) USING DELIMITERS STRING(Z).
552 {sqliteCopy(pParse,&X,&Y,&Z,R);}
553cmd ::= COPY onconf_u(R) ids(X) FROM ids(Y).
554 {sqliteCopy(pParse,&X,&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000555
drh382c0242001-10-06 16:33:02 +0000556///////////////////////////// The VACUUM command /////////////////////////////
557//
drhdce2cbe2000-05-31 02:27:49 +0000558cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drhc4a3c772001-04-04 11:48:57 +0000559cmd ::= VACUUM ids(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000560
drh382c0242001-10-06 16:33:02 +0000561///////////////////////////// The PRAGMA command /////////////////////////////
562//
drhf57b14a2001-09-14 18:54:08 +0000563cmd ::= PRAGMA ids(X) EQ ids(Y). {sqlitePragma(pParse,&X,&Y,0);}
564cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
565cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
566cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh382c0242001-10-06 16:33:02 +0000567cmd ::= PRAGMA ids(X) LP ids(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drhf57b14a2001-09-14 18:54:08 +0000568plus_num(A) ::= plus_opt number(X). {A = X;}
569minus_num(A) ::= MINUS number(X). {A = X;}
570number(A) ::= INTEGER(X). {A = X;}
571number(A) ::= FLOAT(X). {A = X;}
572plus_opt ::= PLUS.
573plus_opt ::= .