blob: ab16c8b4cf4b9f18e1f6e843ca2a0e27ac6426a6 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This file contains SQLite's grammar for SQL. Process this file
25** using the lemon parser generator to generate C code that runs
26** the parser. Lemon will also generate a header file containing
27** numeric codes for all of the tokens.
28**
drh4794b982000-06-06 13:54:14 +000029** @(#) $Id: parse.y,v 1.12 2000/06/06 13:54:15 drh Exp $
drh348784e2000-05-29 20:41:49 +000030*/
31%token_prefix TK_
32%token_type {Token}
33%extra_argument {Parse *pParse}
34%syntax_error {
35 sqliteSetNString(&pParse->zErrMsg,"syntax error near \"",0,TOKEN.z,TOKEN.n,
36 "\"", 1, 0);
37 pParse->sErrToken = TOKEN;
38}
39%name sqliteParser
40%include {
41#include "sqliteInt.h"
42#include "parse.h"
43}
44
45
46// Input is zero or more commands.
47input ::= cmdlist.
48
49// These are extra tokens used by the lexer but never seen by the
50// parser. We put them in a rule so that the parser generator will
51// add them to the sqliteTokens.h output file.
52//
53input ::= END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
54 UMINUS FIELD.
55
56// A list of commands is zero or more commands
57//
58cmdlist ::= ecmd.
59cmdlist ::= cmdlist SEMI ecmd.
60ecmd ::= explain cmd. {sqliteExec(pParse);}
61ecmd ::= cmd. {sqliteExec(pParse);}
62ecmd ::= .
63explain ::= EXPLAIN. {pParse->explain = 1;}
64
65// The first form of a command is a CREATE TABLE statement.
66//
67cmd ::= create_table create_table_args.
drh982cef72000-05-30 16:27:03 +000068create_table ::= CREATE(X) TABLE id(Y). {sqliteStartTable(pParse,&X,&Y);}
drh348784e2000-05-29 20:41:49 +000069create_table_args ::= LP columnlist conslist_opt RP(X).
70 {sqliteEndTable(pParse,&X);}
71columnlist ::= columnlist COMMA column.
72columnlist ::= column.
73
74// About the only information used for a column is the name of the
75// column. The type is always just "text". But the code will accept
76// an elaborate typename. Perhaps someday we'll do something with it.
77//
78column ::= columnid type carglist.
drh982cef72000-05-30 16:27:03 +000079columnid ::= id(X). {sqliteAddColumn(pParse,&X);}
80%type id {Token}
81id(A) ::= ID(X). {A = X;}
82id(A) ::= STRING(X). {A = X;}
drh348784e2000-05-29 20:41:49 +000083type ::= typename.
84type ::= typename LP signed RP.
85type ::= typename LP signed COMMA signed RP.
86typename ::= ID.
87typename ::= typename ID.
88signed ::= INTEGER.
89signed ::= PLUS INTEGER.
90signed ::= MINUS INTEGER.
91carglist ::= carglist carg.
92carglist ::= .
93carg ::= CONSTRAINT ID ccons.
94carg ::= ccons.
drh7020f652000-06-03 18:06:52 +000095carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
96carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
97carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
98carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
99carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
100carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
101carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
102carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
103carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000104
105// In addition to the type name, we also care about the primary key.
106//
107ccons ::= NOT NULL.
108ccons ::= PRIMARY KEY sortorder.
109 {sqliteCreateIndex(pParse,0,0,0,0,0);}
110ccons ::= UNIQUE.
drh4794b982000-06-06 13:54:14 +0000111ccons ::= CHECK LP expr RP.
drh348784e2000-05-29 20:41:49 +0000112
113// For the time being, the only constraint we care about is the primary
114// key.
115//
116conslist_opt ::= .
117conslist_opt ::= COMMA conslist.
118conslist ::= conslist COMMA tcons.
119conslist ::= tcons.
120tcons ::= CONSTRAINT ID tcons2.
121tcons ::= tcons2.
122tcons2 ::= PRIMARY KEY LP idxlist(X) RP.
123 {sqliteCreateIndex(pParse,0,0,X,0,0);}
124tcons2 ::= UNIQUE LP idlist RP.
125tcons2 ::= CHECK expr.
drh982cef72000-05-30 16:27:03 +0000126idlist ::= idlist COMMA id.
127idlist ::= id.
drh348784e2000-05-29 20:41:49 +0000128
129// The next command format is dropping tables.
130//
drh982cef72000-05-30 16:27:03 +0000131cmd ::= DROP TABLE id(X). {sqliteDropTable(pParse,&X);}
drh348784e2000-05-29 20:41:49 +0000132
133// The select statement
134//
drh9bb61fe2000-06-05 16:01:39 +0000135cmd ::= select(X). {
drhfef52082000-06-06 01:50:43 +0000136 sqliteSelect(pParse, X, SRT_Callback, 0);
drh9bb61fe2000-06-05 16:01:39 +0000137 sqliteSelectDelete(X);
138}
drhefb72512000-05-31 20:00:52 +0000139
drh9bb61fe2000-06-05 16:01:39 +0000140%type select {Select*}
141%destructor select {sqliteSelectDelete($$);}
142
143select(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
144 orderby_opt(Z). {
145 A = sqliteSelectNew(W,X,Y,0,0,Z,D);
146}
147
148// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
149// present and false (0) if it is not.
150//
drhefb72512000-05-31 20:00:52 +0000151%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000152distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000153distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000154distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000155
drh9bb61fe2000-06-05 16:01:39 +0000156// selcollist is a list of expressions that are to become the return
157// values of the SELECT statement. In the case of "SELECT * FROM ..."
158// the selcollist value is NULL.
159//
drh348784e2000-05-29 20:41:49 +0000160%type selcollist {ExprList*}
161%destructor selcollist {sqliteExprListDelete($$);}
162%type sclp {ExprList*}
163%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000164sclp(A) ::= selcollist(X) COMMA. {A = X;}
165sclp(A) ::= . {A = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000166selcollist(A) ::= STAR. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000167selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drh9bb61fe2000-06-05 16:01:39 +0000168selcollist(A) ::= sclp(P) expr(X) as ID(Y). {A = sqliteExprListAppend(P,X,&Y);}
169selcollist(A) ::= sclp(P) expr(X) as STRING(Y).
drh348784e2000-05-29 20:41:49 +0000170 {A = sqliteExprListAppend(P,X,&Y);}
drh9bb61fe2000-06-05 16:01:39 +0000171as ::= .
172as ::= AS.
173
drh348784e2000-05-29 20:41:49 +0000174
175%type seltablist {IdList*}
176%destructor seltablist {sqliteIdListDelete($$);}
177%type stl_prefix {IdList*}
178%destructor stl_prefix {sqliteIdListDelete($$);}
179%type from {IdList*}
180%destructor from {sqliteIdListDelete($$);}
181
182from(A) ::= FROM seltablist(X). {A = X;}
183stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
184stl_prefix(A) ::= . {A = 0;}
drh982cef72000-05-30 16:27:03 +0000185seltablist(A) ::= stl_prefix(X) id(Y). {A = sqliteIdListAppend(X,&Y);}
186seltablist(A) ::= stl_prefix(X) id(Y) AS id(Z).
drh348784e2000-05-29 20:41:49 +0000187 {A = sqliteIdListAppend(X,&Y);
188 sqliteIdListAddAlias(A,&Z);}
189
190%type orderby_opt {ExprList*}
191%destructor orderby_opt {sqliteExprListDelete($$);}
192%type sortlist {ExprList*}
193%destructor sortlist {sqliteExprListDelete($$);}
194%type sortitem {Expr*}
195%destructor sortitem {sqliteExprDelete($$);}
196
197orderby_opt(A) ::= . {A = 0;}
198orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000199sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
200 A = sqliteExprListAppend(X,Y,0);
201 A->a[A->nExpr-1].idx = Z; /* 0 for ascending order, 1 for decending */
202}
203sortlist(A) ::= sortitem(Y) sortorder(Z). {
204 A = sqliteExprListAppend(0,Y,0);
205 A->a[0].idx = Z;
206}
drhda9d6c42000-05-31 18:20:14 +0000207sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000208
209%type sortorder {int}
210
211sortorder(A) ::= ASC. {A = 0;}
212sortorder(A) ::= DESC. {A = 1;}
213sortorder(A) ::= . {A = 0;}
214
215cmd ::= DELETE FROM ID(X) where_opt(Y).
216 {sqliteDeleteFrom(pParse, &X, Y);}
217
218%type where_opt {Expr*}
219%destructor where_opt {sqliteExprDelete($$);}
220
221where_opt(A) ::= . {A = 0;}
222where_opt(A) ::= WHERE expr(X). {A = X;}
223
224%type setlist {ExprList*}
225%destructor setlist {sqliteExprListDelete($$);}
226
227cmd ::= UPDATE ID(X) SET setlist(Y) where_opt(Z).
228 {sqliteUpdate(pParse,&X,Y,Z);}
229
230setlist(A) ::= ID(X) EQ expr(Y) COMMA setlist(Z).
231 {A = sqliteExprListAppend(Z,Y,&X);}
232setlist(A) ::= ID(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
233
234cmd ::= INSERT INTO ID(X) fieldlist_opt(F) VALUES LP itemlist(Y) RP.
235 {sqliteInsert(pParse, &X, Y, F);}
236
237
238%type itemlist {ExprList*}
239%destructor itemlist {sqliteExprListDelete($$);}
240%type item {Expr*}
241%destructor item {sqliteExprDelete($$);}
242
243itemlist(A) ::= itemlist(X) COMMA item(Y). {A = sqliteExprListAppend(X,Y,0);}
244itemlist(A) ::= item(X). {A = sqliteExprListAppend(0,X,0);}
245item(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000246item(A) ::= PLUS INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
247item(A) ::= MINUS INTEGER(X). {
248 A = sqliteExpr(TK_INTEGER, 0, 0, 0);
249 A->token.z = 0;
250 sqliteSetNString(&A->token.z, "-", 1, X.z, X.n, 0);
251}
drh348784e2000-05-29 20:41:49 +0000252item(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000253item(A) ::= PLUS FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
254item(A) ::= MINUS FLOAT(X). {
255 A = sqliteExpr(TK_FLOAT, 0, 0, 0);
256 A->token.z = 0;
257 sqliteSetNString(&A->token.z, "-", 1, X.z, X.n, 0);
258}
drh348784e2000-05-29 20:41:49 +0000259item(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drh8be51132000-06-03 19:19:41 +0000260item(A) ::= NULL. {A = sqliteExpr(TK_NULL, 0, 0, 0);}
drh348784e2000-05-29 20:41:49 +0000261
262%type fieldlist_opt {IdList*}
263%destructor fieldlist_opt {sqliteIdListDelete($$);}
264%type fieldlist {IdList*}
265%destructor fieldlist {sqliteIdListDelete($$);}
266
267fieldlist_opt(A) ::= . {A = 0;}
268fieldlist_opt(A) ::= LP fieldlist(X) RP. {A = X;}
269fieldlist(A) ::= fieldlist(X) COMMA ID(Y). {A = sqliteIdListAppend(X,&Y);}
270fieldlist(A) ::= ID(Y). {A = sqliteIdListAppend(0,&Y);}
271
272%left OR.
273%left AND.
drh8be51132000-06-03 19:19:41 +0000274%right NOT.
drhfef52082000-06-06 01:50:43 +0000275%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000276%left GT GE LT LE.
277%left PLUS MINUS.
278%left STAR SLASH PERCENT.
drh8be51132000-06-03 19:19:41 +0000279%right UMINUS.
drh348784e2000-05-29 20:41:49 +0000280
281%type expr {Expr*}
282%destructor expr {sqliteExprDelete($$);}
283
284expr(A) ::= LP expr(X) RP. {A = X;}
285expr(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
286expr(A) ::= NULL. {A = sqliteExpr(TK_NULL, 0, 0, 0);}
287expr(A) ::= ID(X) DOT ID(Y). {Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
288 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
289 A = sqliteExpr(TK_DOT, temp1, temp2, 0);}
290expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
291expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
292expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhcce7d172000-05-31 15:34:51 +0000293expr(A) ::= ID(X) LP exprlist(Y) RP. {A = sqliteExprFunction(Y, &X);}
294expr(A) ::= ID(X) LP STAR RP. {A = sqliteExprFunction(0, &X);}
drh348784e2000-05-29 20:41:49 +0000295expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
296expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
297expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
298expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
299expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
300expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
301expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
302expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drhdce2cbe2000-05-31 02:27:49 +0000303expr(A) ::= expr(X) LIKE expr(Y). {A = sqliteExpr(TK_LIKE, X, Y, 0);}
drh4794b982000-06-06 13:54:14 +0000304expr(A) ::= expr(X) NOT LIKE expr(Y). {
305 A = sqliteExpr(TK_LIKE, X, Y, 0);
306 A = sqliteExpr(TK_NOT, A, 0, 0);
307}
drhfef52082000-06-06 01:50:43 +0000308expr(A) ::= expr(X) GLOB expr(Y). {A = sqliteExpr(TK_GLOB,X,Y,0);}
drh4794b982000-06-06 13:54:14 +0000309expr(A) ::= expr(X) NOT GLOB expr(Y). {
310 A = sqliteExpr(TK_GLOB, X, Y, 0);
311 A = sqliteExpr(TK_NOT, A, 0, 0);
312}
drh348784e2000-05-29 20:41:49 +0000313expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
314expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
315expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
316expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
317expr(A) ::= expr(X) ISNULL. {A = sqliteExpr(TK_ISNULL, X, 0, 0);}
318expr(A) ::= expr(X) NOTNULL. {A = sqliteExpr(TK_NOTNULL, X, 0, 0);}
319expr(A) ::= NOT expr(X). {A = sqliteExpr(TK_NOT, X, 0, 0);}
drh8be51132000-06-03 19:19:41 +0000320expr(A) ::= MINUS expr(X). [UMINUS] {A = sqliteExpr(TK_UMINUS, X, 0, 0);}
321expr(A) ::= PLUS expr(X). [UMINUS] {A = X;}
drh19a775c2000-06-05 18:54:46 +0000322expr(A) ::= LP select(X) RP. {
323 A = sqliteExpr(TK_SELECT, 0, 0, 0);
324 A->pSelect = X;
325}
drhfef52082000-06-06 01:50:43 +0000326expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
327 ExprList *pList = sqliteExprListAppend(0, X, 0);
328 pList = sqliteExprListAppend(pList, Y, 0);
329 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
330 A->pList = pList;
331}
drh4794b982000-06-06 13:54:14 +0000332expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
333 ExprList *pList = sqliteExprListAppend(0, X, 0);
334 pList = sqliteExprListAppend(pList, Y, 0);
335 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
336 A->pList = pList;
337 A = sqliteExpr(TK_NOT, A, 0, 0);
338}
drhfef52082000-06-06 01:50:43 +0000339expr(A) ::= expr(X) IN LP exprlist(Y) RP. {
340 A = sqliteExpr(TK_IN, X, 0, 0);
341 A->pList = Y;
342}
343expr(A) ::= expr(X) IN LP select(Y) RP. {
344 A = sqliteExpr(TK_IN, X, 0, 0);
345 A->pSelect = Y;
346}
drh4794b982000-06-06 13:54:14 +0000347expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP. {
348 A = sqliteExpr(TK_IN, X, 0, 0);
349 A->pList = Y;
350 A = sqliteExpr(TK_NOT, A, 0, 0);
351}
352expr(A) ::= expr(X) NOT IN LP select(Y) RP. {
353 A = sqliteExpr(TK_IN, X, 0, 0);
354 A->pSelect = Y;
355 A = sqliteExpr(TK_NOT, A, 0, 0);
356}
drhfef52082000-06-06 01:50:43 +0000357
358
drh348784e2000-05-29 20:41:49 +0000359
360%type exprlist {ExprList*}
361%destructor exprlist {sqliteExprListDelete($$);}
362%type expritem {Expr*}
363%destructor expritem {sqliteExprDelete($$);}
364
drh348784e2000-05-29 20:41:49 +0000365exprlist(A) ::= exprlist(X) COMMA expritem(Y).
366 {A = sqliteExprListAppend(X,Y,0);}
367exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
368expritem(A) ::= expr(X). {A = X;}
369expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000370
drh348784e2000-05-29 20:41:49 +0000371
372cmd ::= CREATE(S) uniqueflag INDEX ID(X) ON ID(Y) LP idxlist(Z) RP(E).
373 {sqliteCreateIndex(pParse, &X, &Y, Z, &S, &E);}
374uniqueflag ::= UNIQUE.
375uniqueflag ::= .
376
377%type idxlist {IdList*}
378%destructor idxlist {sqliteIdListDelete($$);}
379%type idxitem {Token}
380
381idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
382 {A = sqliteIdListAppend(X,&Y);}
383idxlist(A) ::= idxitem(Y).
384 {A = sqliteIdListAppend(0,&Y);}
385idxitem(A) ::= ID(X). {A = X;}
386
drh982cef72000-05-30 16:27:03 +0000387cmd ::= DROP INDEX id(X). {sqliteDropIndex(pParse, &X);}
388
389cmd ::= COPY id(X) FROM id(Y) USING DELIMITERS STRING(Z).
390 {sqliteCopy(pParse,&X,&Y,&Z);}
391cmd ::= COPY id(X) FROM id(Y).
392 {sqliteCopy(pParse,&X,&Y,0);}
drhdce2cbe2000-05-31 02:27:49 +0000393
394cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
395cmd ::= VACUUM id(X). {sqliteVacuum(pParse,&X);}