blob: f2a1897fd3d413ee7df0bbeaaaabdc0a7cd3b98f [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**
drh7c917d12001-12-16 20:05:05 +000017** @(#) $Id: parse.y,v 1.39 2001/12/16 20:05:06 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;}
drhc4a3c772001-04-04 11:48:57 +0000105
106// And "ids" is an identifer-or-string.
107//
108%type ids {Token}
109ids(A) ::= id(X). {A = X;}
110ids(A) ::= STRING(X). {A = X;}
111
drh382c0242001-10-06 16:33:02 +0000112type ::= .
113type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
114type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
115type ::= typename(X) LP signed COMMA signed RP(Y).
116 {sqliteAddColumnType(pParse,&X,&Y);}
117%type typename {Token}
118typename(A) ::= ids(X). {A = X;}
119typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000120signed ::= INTEGER.
121signed ::= PLUS INTEGER.
122signed ::= MINUS INTEGER.
123carglist ::= carglist carg.
124carglist ::= .
drhc4a3c772001-04-04 11:48:57 +0000125carg ::= CONSTRAINT ids ccons.
drh348784e2000-05-29 20:41:49 +0000126carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000127carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
128carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
129carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
130carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
131carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
132carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
133carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
134carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
135carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000136
drh382c0242001-10-06 16:33:02 +0000137// In addition to the type name, we also care about the primary key and
138// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000139//
drh382c0242001-10-06 16:33:02 +0000140ccons ::= NOT NULL. {sqliteAddNotNull(pParse);}
drh717e6402001-09-27 03:22:32 +0000141ccons ::= PRIMARY KEY sortorder. {sqliteCreateIndex(pParse,0,0,0,1,0,0);}
drhadbca9c2001-09-27 15:11:53 +0000142ccons ::= UNIQUE. {sqliteCreateIndex(pParse,0,0,0,1,0,0);}
drh4794b982000-06-06 13:54:14 +0000143ccons ::= CHECK LP expr RP.
drh348784e2000-05-29 20:41:49 +0000144
145// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000146// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000147//
148conslist_opt ::= .
149conslist_opt ::= COMMA conslist.
150conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000151conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000152conslist ::= tcons.
drhc4a3c772001-04-04 11:48:57 +0000153tcons ::= CONSTRAINT ids.
drh717e6402001-09-27 03:22:32 +0000154tcons ::= PRIMARY KEY LP idxlist(X) RP. {sqliteCreateIndex(pParse,0,0,X,1,0,0);}
drhadbca9c2001-09-27 15:11:53 +0000155tcons ::= UNIQUE LP idxlist(X) RP. {sqliteCreateIndex(pParse,0,0,X,1,0,0);}
drha2e1bb52001-01-04 14:20:18 +0000156tcons ::= CHECK expr.
drh348784e2000-05-29 20:41:49 +0000157
drh382c0242001-10-06 16:33:02 +0000158////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000159//
drhc4a3c772001-04-04 11:48:57 +0000160cmd ::= DROP TABLE ids(X). {sqliteDropTable(pParse,&X);}
drh348784e2000-05-29 20:41:49 +0000161
drh382c0242001-10-06 16:33:02 +0000162//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000163//
drh9bb61fe2000-06-05 16:01:39 +0000164cmd ::= select(X). {
drhfef52082000-06-06 01:50:43 +0000165 sqliteSelect(pParse, X, SRT_Callback, 0);
drh9bb61fe2000-06-05 16:01:39 +0000166 sqliteSelectDelete(X);
167}
drhefb72512000-05-31 20:00:52 +0000168
drh9bb61fe2000-06-05 16:01:39 +0000169%type select {Select*}
170%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000171%type oneselect {Select*}
172%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000173
drh82c3d632000-06-06 21:56:07 +0000174select(A) ::= oneselect(X). {A = X;}
175select(A) ::= select(X) joinop(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000176 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000177 Z->op = Y;
178 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000179 }
180 A = Z;
drh82c3d632000-06-06 21:56:07 +0000181}
182%type joinop {int}
183joinop(A) ::= UNION. {A = TK_UNION;}
184joinop(A) ::= UNION ALL. {A = TK_ALL;}
185joinop(A) ::= INTERSECT. {A = TK_INTERSECT;}
186joinop(A) ::= EXCEPT. {A = TK_EXCEPT;}
187oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000188 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
189 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.a,L.b);
drh9bb61fe2000-06-05 16:01:39 +0000190}
191
192// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
193// present and false (0) if it is not.
194//
drhefb72512000-05-31 20:00:52 +0000195%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000196distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000197distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000198distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000199
drh9bb61fe2000-06-05 16:01:39 +0000200// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000201// values of the SELECT statement. The "*" in statements like
202// "SELECT * FROM ..." is encoded as a special expression with an
203// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000204//
drh348784e2000-05-29 20:41:49 +0000205%type selcollist {ExprList*}
206%destructor selcollist {sqliteExprListDelete($$);}
207%type sclp {ExprList*}
208%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000209sclp(A) ::= selcollist(X) COMMA. {A = X;}
210sclp(A) ::= . {A = 0;}
211selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drhc4a3c772001-04-04 11:48:57 +0000212selcollist(A) ::= sclp(P) expr(X) as ids(Y). {A = sqliteExprListAppend(P,X,&Y);}
drh7c917d12001-12-16 20:05:05 +0000213selcollist(A) ::= sclp(P) STAR. {
214 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
215}
drh9bb61fe2000-06-05 16:01:39 +0000216as ::= .
217as ::= AS.
218
drh348784e2000-05-29 20:41:49 +0000219
220%type seltablist {IdList*}
221%destructor seltablist {sqliteIdListDelete($$);}
222%type stl_prefix {IdList*}
223%destructor stl_prefix {sqliteIdListDelete($$);}
224%type from {IdList*}
225%destructor from {sqliteIdListDelete($$);}
226
227from(A) ::= FROM seltablist(X). {A = X;}
228stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
229stl_prefix(A) ::= . {A = 0;}
drhc4a3c772001-04-04 11:48:57 +0000230seltablist(A) ::= stl_prefix(X) ids(Y). {A = sqliteIdListAppend(X,&Y);}
231seltablist(A) ::= stl_prefix(X) ids(Y) as ids(Z). {
232 A = sqliteIdListAppend(X,&Y);
233 sqliteIdListAddAlias(A,&Z);
234}
drh348784e2000-05-29 20:41:49 +0000235
236%type orderby_opt {ExprList*}
237%destructor orderby_opt {sqliteExprListDelete($$);}
238%type sortlist {ExprList*}
239%destructor sortlist {sqliteExprListDelete($$);}
240%type sortitem {Expr*}
241%destructor sortitem {sqliteExprDelete($$);}
242
243orderby_opt(A) ::= . {A = 0;}
244orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000245sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
246 A = sqliteExprListAppend(X,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000247 if( A ) A->a[A->nExpr-1].sortOrder = Z; /* 0=ascending, 1=decending */
drh9bb61fe2000-06-05 16:01:39 +0000248}
249sortlist(A) ::= sortitem(Y) sortorder(Z). {
250 A = sqliteExprListAppend(0,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000251 if( A ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000252}
drhda9d6c42000-05-31 18:20:14 +0000253sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000254
255%type sortorder {int}
256
257sortorder(A) ::= ASC. {A = 0;}
258sortorder(A) ::= DESC. {A = 1;}
259sortorder(A) ::= . {A = 0;}
260
drh22827922000-06-06 17:27:05 +0000261%type groupby_opt {ExprList*}
262%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000263groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000264groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
265
266%type having_opt {Expr*}
267%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000268having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000269having_opt(A) ::= HAVING expr(X). {A = X;}
270
drh9bbca4c2001-11-06 04:00:18 +0000271%type limit_opt {struct twoint}
272limit_opt(A) ::= . {A.a = -1; A.b = 0;}
273limit_opt(A) ::= LIMIT INTEGER(X). {A.a = atoi(X.z); A.b = 0;}
274limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
275 {A.a = atoi(X.z); A.b = atoi(Y.z);}
276limit_sep ::= OFFSET.
277limit_sep ::= COMMA.
278
drh382c0242001-10-06 16:33:02 +0000279/////////////////////////// The DELETE statement /////////////////////////////
280//
drhc4a3c772001-04-04 11:48:57 +0000281cmd ::= DELETE FROM ids(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000282 {sqliteDeleteFrom(pParse, &X, Y);}
283
284%type where_opt {Expr*}
285%destructor where_opt {sqliteExprDelete($$);}
286
287where_opt(A) ::= . {A = 0;}
288where_opt(A) ::= WHERE expr(X). {A = X;}
289
290%type setlist {ExprList*}
291%destructor setlist {sqliteExprListDelete($$);}
292
drh382c0242001-10-06 16:33:02 +0000293////////////////////////// The UPDATE command ////////////////////////////////
294//
drhc4a3c772001-04-04 11:48:57 +0000295cmd ::= UPDATE ids(X) SET setlist(Y) where_opt(Z).
drh348784e2000-05-29 20:41:49 +0000296 {sqliteUpdate(pParse,&X,Y,Z);}
297
drhc4a3c772001-04-04 11:48:57 +0000298setlist(A) ::= setlist(Z) COMMA ids(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000299 {A = sqliteExprListAppend(Z,Y,&X);}
drhc4a3c772001-04-04 11:48:57 +0000300setlist(A) ::= ids(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000301
drh382c0242001-10-06 16:33:02 +0000302////////////////////////// The INSERT command /////////////////////////////////
303//
drhc4a3c772001-04-04 11:48:57 +0000304cmd ::= INSERT INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
drh5974a302000-06-07 14:42:26 +0000305 {sqliteInsert(pParse, &X, Y, 0, F);}
drhc4a3c772001-04-04 11:48:57 +0000306cmd ::= INSERT INTO ids(X) inscollist_opt(F) select(S).
drh5974a302000-06-07 14:42:26 +0000307 {sqliteInsert(pParse, &X, 0, S, F);}
drh348784e2000-05-29 20:41:49 +0000308
309
310%type itemlist {ExprList*}
311%destructor itemlist {sqliteExprListDelete($$);}
312%type item {Expr*}
313%destructor item {sqliteExprDelete($$);}
314
315itemlist(A) ::= itemlist(X) COMMA item(Y). {A = sqliteExprListAppend(X,Y,0);}
316itemlist(A) ::= item(X). {A = sqliteExprListAppend(0,X,0);}
317item(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000318item(A) ::= PLUS INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
319item(A) ::= MINUS INTEGER(X). {
drh6e142f52000-06-08 13:36:40 +0000320 A = sqliteExpr(TK_UMINUS, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000321 if( A ) A->pLeft = sqliteExpr(TK_INTEGER, 0, 0, &X);
drh7020f652000-06-03 18:06:52 +0000322}
drh348784e2000-05-29 20:41:49 +0000323item(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000324item(A) ::= PLUS FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
325item(A) ::= MINUS FLOAT(X). {
drh6e142f52000-06-08 13:36:40 +0000326 A = sqliteExpr(TK_UMINUS, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000327 if( A ) A->pLeft = sqliteExpr(TK_FLOAT, 0, 0, &X);
drh7020f652000-06-03 18:06:52 +0000328}
drh348784e2000-05-29 20:41:49 +0000329item(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drh8be51132000-06-03 19:19:41 +0000330item(A) ::= NULL. {A = sqliteExpr(TK_NULL, 0, 0, 0);}
drh348784e2000-05-29 20:41:49 +0000331
drh967e8b72000-06-21 13:59:10 +0000332%type inscollist_opt {IdList*}
333%destructor inscollist_opt {sqliteIdListDelete($$);}
334%type inscollist {IdList*}
335%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000336
drhc4a3c772001-04-04 11:48:57 +0000337inscollist_opt(A) ::= . {A = 0;}
338inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
339inscollist(A) ::= inscollist(X) COMMA ids(Y). {A = sqliteIdListAppend(X,&Y);}
340inscollist(A) ::= ids(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000341
drh382c0242001-10-06 16:33:02 +0000342/////////////////////////// Expression Processing /////////////////////////////
343//
drh348784e2000-05-29 20:41:49 +0000344%left OR.
345%left AND.
drh8be51132000-06-03 19:19:41 +0000346%right NOT.
drhfef52082000-06-06 01:50:43 +0000347%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000348%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000349%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000350%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000351%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000352%left CONCAT.
drh81a20f22001-10-12 17:30:04 +0000353%right UMINUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000354
355%type expr {Expr*}
356%destructor expr {sqliteExprDelete($$);}
357
drhe1b6a5b2000-07-29 13:06:59 +0000358expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
drhe1b6a5b2000-07-29 13:06:59 +0000359expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drhc4a3c772001-04-04 11:48:57 +0000360expr(A) ::= id(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
361expr(A) ::= ids(X) DOT ids(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000362 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
363 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
364 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
365}
drh348784e2000-05-29 20:41:49 +0000366expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
367expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
368expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000369expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
370 A = sqliteExprFunction(Y, &X);
371 sqliteExprSpan(A,&X,&E);
372}
373expr(A) ::= ID(X) LP STAR RP(E). {
374 A = sqliteExprFunction(0, &X);
375 sqliteExprSpan(A,&X,&E);
376}
drh348784e2000-05-29 20:41:49 +0000377expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
378expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
379expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
380expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
381expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
382expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
383expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
384expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000385expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
386expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
387expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
388expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
389expr(A) ::= expr(X) LIKE expr(Y). {A = sqliteExpr(TK_LIKE, X, Y, 0);}
drh4794b982000-06-06 13:54:14 +0000390expr(A) ::= expr(X) NOT LIKE expr(Y). {
391 A = sqliteExpr(TK_LIKE, X, Y, 0);
392 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000393 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000394}
drhfef52082000-06-06 01:50:43 +0000395expr(A) ::= expr(X) GLOB expr(Y). {A = sqliteExpr(TK_GLOB,X,Y,0);}
drh4794b982000-06-06 13:54:14 +0000396expr(A) ::= expr(X) NOT GLOB expr(Y). {
397 A = sqliteExpr(TK_GLOB, X, Y, 0);
398 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000399 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000400}
drh348784e2000-05-29 20:41:49 +0000401expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
402expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
403expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
404expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000405expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000406expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000407expr(A) ::= expr(X) ISNULL(E). {
408 A = sqliteExpr(TK_ISNULL, X, 0, 0);
409 sqliteExprSpan(A,&X->span,&E);
410}
drh33048c02001-10-01 14:29:22 +0000411expr(A) ::= expr(X) IS NULL(E). {
412 A = sqliteExpr(TK_ISNULL, X, 0, 0);
413 sqliteExprSpan(A,&X->span,&E);
414}
drhe1b6a5b2000-07-29 13:06:59 +0000415expr(A) ::= expr(X) NOTNULL(E). {
416 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
417 sqliteExprSpan(A,&X->span,&E);
418}
drh33048c02001-10-01 14:29:22 +0000419expr(A) ::= expr(X) NOT NULL(E). {
420 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
421 sqliteExprSpan(A,&X->span,&E);
422}
drh81a20f22001-10-12 17:30:04 +0000423expr(A) ::= expr(X) IS NOT NULL(E). {
424 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
425 sqliteExprSpan(A,&X->span,&E);
426}
drhe1b6a5b2000-07-29 13:06:59 +0000427expr(A) ::= NOT(B) expr(X). {
428 A = sqliteExpr(TK_NOT, X, 0, 0);
429 sqliteExprSpan(A,&B,&X->span);
430}
drh81a20f22001-10-12 17:30:04 +0000431expr(A) ::= BITNOT(B) expr(X). {
432 A = sqliteExpr(TK_BITNOT, X, 0, 0);
433 sqliteExprSpan(A,&B,&X->span);
434}
drhe1b6a5b2000-07-29 13:06:59 +0000435expr(A) ::= MINUS(B) expr(X). [UMINUS] {
436 A = sqliteExpr(TK_UMINUS, X, 0, 0);
437 sqliteExprSpan(A,&B,&X->span);
438}
439expr(A) ::= PLUS(B) expr(X). [UMINUS] {
440 A = X;
441 sqliteExprSpan(A,&B,&X->span);
442}
443expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000444 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000445 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000446 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000447}
drhfef52082000-06-06 01:50:43 +0000448expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
449 ExprList *pList = sqliteExprListAppend(0, X, 0);
450 pList = sqliteExprListAppend(pList, Y, 0);
451 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000452 if( A ) A->pList = pList;
drhe1b6a5b2000-07-29 13:06:59 +0000453 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000454}
drh4794b982000-06-06 13:54:14 +0000455expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
456 ExprList *pList = sqliteExprListAppend(0, X, 0);
457 pList = sqliteExprListAppend(pList, Y, 0);
458 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000459 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000460 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000461 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000462}
drhe1b6a5b2000-07-29 13:06:59 +0000463expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000464 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000465 if( A ) A->pList = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000466 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000467}
drhe1b6a5b2000-07-29 13:06:59 +0000468expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000469 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000470 if( A ) A->pSelect = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000471 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000472}
drhe1b6a5b2000-07-29 13:06:59 +0000473expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000474 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000475 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000476 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000477 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000478}
drhe1b6a5b2000-07-29 13:06:59 +0000479expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000480 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000481 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000482 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000483 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000484}
drhfef52082000-06-06 01:50:43 +0000485
486
drh348784e2000-05-29 20:41:49 +0000487
488%type exprlist {ExprList*}
489%destructor exprlist {sqliteExprListDelete($$);}
490%type expritem {Expr*}
491%destructor expritem {sqliteExprDelete($$);}
492
drh348784e2000-05-29 20:41:49 +0000493exprlist(A) ::= exprlist(X) COMMA expritem(Y).
494 {A = sqliteExprListAppend(X,Y,0);}
495exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
496expritem(A) ::= expr(X). {A = X;}
497expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000498
drh382c0242001-10-06 16:33:02 +0000499///////////////////////////// The CREATE INDEX command ///////////////////////
500//
drh717e6402001-09-27 03:22:32 +0000501cmd ::= CREATE(S) uniqueflag(U) INDEX ids(X) ON ids(Y) LP idxlist(Z) RP(E).
502 {sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);}
503
504%type uniqueflag {int}
505uniqueflag(A) ::= UNIQUE. { A = 1; }
506uniqueflag(A) ::= . { A = 0; }
drh348784e2000-05-29 20:41:49 +0000507
508%type idxlist {IdList*}
509%destructor idxlist {sqliteIdListDelete($$);}
510%type idxitem {Token}
511
512idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
513 {A = sqliteIdListAppend(X,&Y);}
514idxlist(A) ::= idxitem(Y).
515 {A = sqliteIdListAppend(0,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000516idxitem(A) ::= ids(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000517
drh382c0242001-10-06 16:33:02 +0000518///////////////////////////// The CREATE INDEX command ///////////////////////
519//
520
drhc4a3c772001-04-04 11:48:57 +0000521cmd ::= DROP INDEX ids(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000522
drh382c0242001-10-06 16:33:02 +0000523
524///////////////////////////// The DROP INDEX command /////////////////////////
525//
drhc4a3c772001-04-04 11:48:57 +0000526cmd ::= COPY ids(X) FROM ids(Y) USING DELIMITERS STRING(Z).
drh982cef72000-05-30 16:27:03 +0000527 {sqliteCopy(pParse,&X,&Y,&Z);}
drhc4a3c772001-04-04 11:48:57 +0000528cmd ::= COPY ids(X) FROM ids(Y).
drh982cef72000-05-30 16:27:03 +0000529 {sqliteCopy(pParse,&X,&Y,0);}
drhdce2cbe2000-05-31 02:27:49 +0000530
drh382c0242001-10-06 16:33:02 +0000531///////////////////////////// The VACUUM command /////////////////////////////
532//
drhdce2cbe2000-05-31 02:27:49 +0000533cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drhc4a3c772001-04-04 11:48:57 +0000534cmd ::= VACUUM ids(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000535
drh382c0242001-10-06 16:33:02 +0000536///////////////////////////// The PRAGMA command /////////////////////////////
537//
drhf57b14a2001-09-14 18:54:08 +0000538cmd ::= PRAGMA ids(X) EQ ids(Y). {sqlitePragma(pParse,&X,&Y,0);}
539cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
540cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
541cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh382c0242001-10-06 16:33:02 +0000542cmd ::= PRAGMA ids(X) LP ids(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drhf57b14a2001-09-14 18:54:08 +0000543plus_num(A) ::= plus_opt number(X). {A = X;}
544minus_num(A) ::= MINUS number(X). {A = X;}
545number(A) ::= INTEGER(X). {A = X;}
546number(A) ::= FLOAT(X). {A = X;}
547plus_opt ::= PLUS.
548plus_opt ::= .