blob: 41b276bc9ad09d14d31e3be56cf02b75f0b351d6 [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**
drh7020f652000-06-03 18:06:52 +000029** @(#) $Id: parse.y,v 1.7 2000/06/03 18:06:53 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.
111ccons ::= CHECK expr.
112
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//
135cmd ::= select.
drhefb72512000-05-31 20:00:52 +0000136select ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) orderby_opt(Z).
137 {sqliteSelect(pParse, W, X, Y, Z, D);}
138select ::= SELECT distinct(D) STAR from(X) where_opt(Y) orderby_opt(Z).
139 {sqliteSelect(pParse, 0, X, Y, Z, D);}
140
141%type distinct {int}
142
143distinct(A) ::= DISTINCT. {A = 1;}
144distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000145
146%type selcollist {ExprList*}
147%destructor selcollist {sqliteExprListDelete($$);}
148%type sclp {ExprList*}
149%destructor sclp {sqliteExprListDelete($$);}
150
151sclp(A) ::= selcollist(X) COMMA. {A = X;}
152sclp(A) ::= . {A = 0;}
153selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
154selcollist(A) ::= sclp(P) expr(X) AS ID(Y). {A = sqliteExprListAppend(P,X,&Y);}
155selcollist(A) ::= sclp(P) expr(X) AS STRING(Y).
156 {A = sqliteExprListAppend(P,X,&Y);}
157
158%type seltablist {IdList*}
159%destructor seltablist {sqliteIdListDelete($$);}
160%type stl_prefix {IdList*}
161%destructor stl_prefix {sqliteIdListDelete($$);}
162%type from {IdList*}
163%destructor from {sqliteIdListDelete($$);}
164
165from(A) ::= FROM seltablist(X). {A = X;}
166stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
167stl_prefix(A) ::= . {A = 0;}
drh982cef72000-05-30 16:27:03 +0000168seltablist(A) ::= stl_prefix(X) id(Y). {A = sqliteIdListAppend(X,&Y);}
169seltablist(A) ::= stl_prefix(X) id(Y) AS id(Z).
drh348784e2000-05-29 20:41:49 +0000170 {A = sqliteIdListAppend(X,&Y);
171 sqliteIdListAddAlias(A,&Z);}
172
173%type orderby_opt {ExprList*}
174%destructor orderby_opt {sqliteExprListDelete($$);}
175%type sortlist {ExprList*}
176%destructor sortlist {sqliteExprListDelete($$);}
177%type sortitem {Expr*}
178%destructor sortitem {sqliteExprDelete($$);}
179
180orderby_opt(A) ::= . {A = 0;}
181orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
182sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z).
183 {
184 A = sqliteExprListAppend(X,Y,0);
185 A->a[A->nExpr-1].idx = Z;
186 }
187sortlist(A) ::= sortitem(Y) sortorder(Z).
188 {
189 A = sqliteExprListAppend(0,Y,0);
190 A->a[0].idx = Z;
191 }
drhda9d6c42000-05-31 18:20:14 +0000192sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000193
194%type sortorder {int}
195
196sortorder(A) ::= ASC. {A = 0;}
197sortorder(A) ::= DESC. {A = 1;}
198sortorder(A) ::= . {A = 0;}
199
200cmd ::= DELETE FROM ID(X) where_opt(Y).
201 {sqliteDeleteFrom(pParse, &X, Y);}
202
203%type where_opt {Expr*}
204%destructor where_opt {sqliteExprDelete($$);}
205
206where_opt(A) ::= . {A = 0;}
207where_opt(A) ::= WHERE expr(X). {A = X;}
208
209%type setlist {ExprList*}
210%destructor setlist {sqliteExprListDelete($$);}
211
212cmd ::= UPDATE ID(X) SET setlist(Y) where_opt(Z).
213 {sqliteUpdate(pParse,&X,Y,Z);}
214
215setlist(A) ::= ID(X) EQ expr(Y) COMMA setlist(Z).
216 {A = sqliteExprListAppend(Z,Y,&X);}
217setlist(A) ::= ID(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
218
219cmd ::= INSERT INTO ID(X) fieldlist_opt(F) VALUES LP itemlist(Y) RP.
220 {sqliteInsert(pParse, &X, Y, F);}
221
222
223%type itemlist {ExprList*}
224%destructor itemlist {sqliteExprListDelete($$);}
225%type item {Expr*}
226%destructor item {sqliteExprDelete($$);}
227
228itemlist(A) ::= itemlist(X) COMMA item(Y). {A = sqliteExprListAppend(X,Y,0);}
229itemlist(A) ::= item(X). {A = sqliteExprListAppend(0,X,0);}
230item(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000231item(A) ::= PLUS INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
232item(A) ::= MINUS INTEGER(X). {
233 A = sqliteExpr(TK_INTEGER, 0, 0, 0);
234 A->token.z = 0;
235 sqliteSetNString(&A->token.z, "-", 1, X.z, X.n, 0);
236}
drh348784e2000-05-29 20:41:49 +0000237item(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
drh7020f652000-06-03 18:06:52 +0000238item(A) ::= PLUS FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
239item(A) ::= MINUS FLOAT(X). {
240 A = sqliteExpr(TK_FLOAT, 0, 0, 0);
241 A->token.z = 0;
242 sqliteSetNString(&A->token.z, "-", 1, X.z, X.n, 0);
243}
drh348784e2000-05-29 20:41:49 +0000244item(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
245
246%type fieldlist_opt {IdList*}
247%destructor fieldlist_opt {sqliteIdListDelete($$);}
248%type fieldlist {IdList*}
249%destructor fieldlist {sqliteIdListDelete($$);}
250
251fieldlist_opt(A) ::= . {A = 0;}
252fieldlist_opt(A) ::= LP fieldlist(X) RP. {A = X;}
253fieldlist(A) ::= fieldlist(X) COMMA ID(Y). {A = sqliteIdListAppend(X,&Y);}
254fieldlist(A) ::= ID(Y). {A = sqliteIdListAppend(0,&Y);}
255
256%left OR.
257%left AND.
drhdce2cbe2000-05-31 02:27:49 +0000258%left EQ NE ISNULL NOTNULL IS LIKE GLOB.
drh348784e2000-05-29 20:41:49 +0000259%left GT GE LT LE.
260%left PLUS MINUS.
261%left STAR SLASH PERCENT.
262%right NOT.
263
264%type expr {Expr*}
265%destructor expr {sqliteExprDelete($$);}
266
267expr(A) ::= LP expr(X) RP. {A = X;}
268expr(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
269expr(A) ::= NULL. {A = sqliteExpr(TK_NULL, 0, 0, 0);}
270expr(A) ::= ID(X) DOT ID(Y). {Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
271 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
272 A = sqliteExpr(TK_DOT, temp1, temp2, 0);}
273expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
274expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
275expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhcce7d172000-05-31 15:34:51 +0000276expr(A) ::= ID(X) LP exprlist(Y) RP. {A = sqliteExprFunction(Y, &X);}
277expr(A) ::= ID(X) LP STAR RP. {A = sqliteExprFunction(0, &X);}
drh348784e2000-05-29 20:41:49 +0000278expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
279expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
280expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
281expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
282expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
283expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
284expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
285expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drhdce2cbe2000-05-31 02:27:49 +0000286expr(A) ::= expr(X) LIKE expr(Y). {A = sqliteExpr(TK_LIKE, X, Y, 0);}
287expr(A) ::= expr(X) GLOB expr(Y). {A = sqliteExpr(TK_GLOB,X,Y,0);}
288// expr(A) ::= expr(X) IS expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh348784e2000-05-29 20:41:49 +0000289expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
290expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
291expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
292expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
293expr(A) ::= expr(X) ISNULL. {A = sqliteExpr(TK_ISNULL, X, 0, 0);}
294expr(A) ::= expr(X) NOTNULL. {A = sqliteExpr(TK_NOTNULL, X, 0, 0);}
295expr(A) ::= NOT expr(X). {A = sqliteExpr(TK_NOT, X, 0, 0);}
296expr(A) ::= MINUS expr(X). [NOT] {A = sqliteExpr(TK_UMINUS, X, 0, 0);}
297expr(A) ::= PLUS expr(X). [NOT] {A = X;}
298
299%type exprlist {ExprList*}
300%destructor exprlist {sqliteExprListDelete($$);}
301%type expritem {Expr*}
302%destructor expritem {sqliteExprDelete($$);}
303
drh348784e2000-05-29 20:41:49 +0000304exprlist(A) ::= exprlist(X) COMMA expritem(Y).
305 {A = sqliteExprListAppend(X,Y,0);}
306exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
307expritem(A) ::= expr(X). {A = X;}
308expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000309
drh348784e2000-05-29 20:41:49 +0000310
311cmd ::= CREATE(S) uniqueflag INDEX ID(X) ON ID(Y) LP idxlist(Z) RP(E).
312 {sqliteCreateIndex(pParse, &X, &Y, Z, &S, &E);}
313uniqueflag ::= UNIQUE.
314uniqueflag ::= .
315
316%type idxlist {IdList*}
317%destructor idxlist {sqliteIdListDelete($$);}
318%type idxitem {Token}
319
320idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
321 {A = sqliteIdListAppend(X,&Y);}
322idxlist(A) ::= idxitem(Y).
323 {A = sqliteIdListAppend(0,&Y);}
324idxitem(A) ::= ID(X). {A = X;}
325
drh982cef72000-05-30 16:27:03 +0000326cmd ::= DROP INDEX id(X). {sqliteDropIndex(pParse, &X);}
327
328cmd ::= COPY id(X) FROM id(Y) USING DELIMITERS STRING(Z).
329 {sqliteCopy(pParse,&X,&Y,&Z);}
330cmd ::= COPY id(X) FROM id(Y).
331 {sqliteCopy(pParse,&X,&Y,0);}
drhdce2cbe2000-05-31 02:27:49 +0000332
333cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
334cmd ::= VACUUM id(X). {sqliteVacuum(pParse,&X);}