blob: 65ee04244458f91c62df6dd8b97501bdc2cac3f3 [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**
29** @(#) $Id: parse.y,v 1.1 2000/05/29 20:42:06 drh Exp $
30*/
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.
68create_table ::= CREATE(X) TABLE ID(Y). {sqliteStartTable(pParse,&X,&Y);}
69create_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.
79columnid ::= ID(X). {sqliteAddColumn(pParse,&X);}
80type ::= typename.
81type ::= typename LP signed RP.
82type ::= typename LP signed COMMA signed RP.
83typename ::= ID.
84typename ::= typename ID.
85signed ::= INTEGER.
86signed ::= PLUS INTEGER.
87signed ::= MINUS INTEGER.
88carglist ::= carglist carg.
89carglist ::= .
90carg ::= CONSTRAINT ID ccons.
91carg ::= ccons.
92carg ::= DEFAULT expr.
93
94// In addition to the type name, we also care about the primary key.
95//
96ccons ::= NOT NULL.
97ccons ::= PRIMARY KEY sortorder.
98 {sqliteCreateIndex(pParse,0,0,0,0,0);}
99ccons ::= UNIQUE.
100ccons ::= CHECK expr.
101
102// For the time being, the only constraint we care about is the primary
103// key.
104//
105conslist_opt ::= .
106conslist_opt ::= COMMA conslist.
107conslist ::= conslist COMMA tcons.
108conslist ::= tcons.
109tcons ::= CONSTRAINT ID tcons2.
110tcons ::= tcons2.
111tcons2 ::= PRIMARY KEY LP idxlist(X) RP.
112 {sqliteCreateIndex(pParse,0,0,X,0,0);}
113tcons2 ::= UNIQUE LP idlist RP.
114tcons2 ::= CHECK expr.
115idlist ::= idlist COMMA ID.
116idlist ::= ID.
117
118// The next command format is dropping tables.
119//
120cmd ::= DROP TABLE ID(X). {sqliteDropTable(pParse,&X);}
121
122// The select statement
123//
124cmd ::= select.
125select ::= SELECT selcollist(W) from(X) where_opt(Y) orderby_opt(Z).
126 {sqliteSelect(pParse, W, X, Y, Z);}
127select ::= SELECT STAR from(X) where_opt(Y) orderby_opt(Z).
128 {sqliteSelect(pParse, 0, X, Y, Z);}
129
130%type selcollist {ExprList*}
131%destructor selcollist {sqliteExprListDelete($$);}
132%type sclp {ExprList*}
133%destructor sclp {sqliteExprListDelete($$);}
134
135sclp(A) ::= selcollist(X) COMMA. {A = X;}
136sclp(A) ::= . {A = 0;}
137selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
138selcollist(A) ::= sclp(P) expr(X) AS ID(Y). {A = sqliteExprListAppend(P,X,&Y);}
139selcollist(A) ::= sclp(P) expr(X) AS STRING(Y).
140 {A = sqliteExprListAppend(P,X,&Y);}
141
142%type seltablist {IdList*}
143%destructor seltablist {sqliteIdListDelete($$);}
144%type stl_prefix {IdList*}
145%destructor stl_prefix {sqliteIdListDelete($$);}
146%type from {IdList*}
147%destructor from {sqliteIdListDelete($$);}
148
149from(A) ::= FROM seltablist(X). {A = X;}
150stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
151stl_prefix(A) ::= . {A = 0;}
152seltablist(A) ::= stl_prefix(X) ID(Y). {A = sqliteIdListAppend(X,&Y);}
153seltablist(A) ::= stl_prefix(X) ID(Y) AS ID(Z).
154 {A = sqliteIdListAppend(X,&Y);
155 sqliteIdListAddAlias(A,&Z);}
156
157%type orderby_opt {ExprList*}
158%destructor orderby_opt {sqliteExprListDelete($$);}
159%type sortlist {ExprList*}
160%destructor sortlist {sqliteExprListDelete($$);}
161%type sortitem {Expr*}
162%destructor sortitem {sqliteExprDelete($$);}
163
164orderby_opt(A) ::= . {A = 0;}
165orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
166sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z).
167 {
168 A = sqliteExprListAppend(X,Y,0);
169 A->a[A->nExpr-1].idx = Z;
170 }
171sortlist(A) ::= sortitem(Y) sortorder(Z).
172 {
173 A = sqliteExprListAppend(0,Y,0);
174 A->a[0].idx = Z;
175 }
176sortitem(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
177sortitem(A) ::= ID(X) DOT ID(Y).
178 {
179 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
180 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
181 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
182 }
183
184%type sortorder {int}
185
186sortorder(A) ::= ASC. {A = 0;}
187sortorder(A) ::= DESC. {A = 1;}
188sortorder(A) ::= . {A = 0;}
189
190cmd ::= DELETE FROM ID(X) where_opt(Y).
191 {sqliteDeleteFrom(pParse, &X, Y);}
192
193%type where_opt {Expr*}
194%destructor where_opt {sqliteExprDelete($$);}
195
196where_opt(A) ::= . {A = 0;}
197where_opt(A) ::= WHERE expr(X). {A = X;}
198
199%type setlist {ExprList*}
200%destructor setlist {sqliteExprListDelete($$);}
201
202cmd ::= UPDATE ID(X) SET setlist(Y) where_opt(Z).
203 {sqliteUpdate(pParse,&X,Y,Z);}
204
205setlist(A) ::= ID(X) EQ expr(Y) COMMA setlist(Z).
206 {A = sqliteExprListAppend(Z,Y,&X);}
207setlist(A) ::= ID(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
208
209cmd ::= INSERT INTO ID(X) fieldlist_opt(F) VALUES LP itemlist(Y) RP.
210 {sqliteInsert(pParse, &X, Y, F);}
211
212
213%type itemlist {ExprList*}
214%destructor itemlist {sqliteExprListDelete($$);}
215%type item {Expr*}
216%destructor item {sqliteExprDelete($$);}
217
218itemlist(A) ::= itemlist(X) COMMA item(Y). {A = sqliteExprListAppend(X,Y,0);}
219itemlist(A) ::= item(X). {A = sqliteExprListAppend(0,X,0);}
220item(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
221item(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
222item(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
223
224%type fieldlist_opt {IdList*}
225%destructor fieldlist_opt {sqliteIdListDelete($$);}
226%type fieldlist {IdList*}
227%destructor fieldlist {sqliteIdListDelete($$);}
228
229fieldlist_opt(A) ::= . {A = 0;}
230fieldlist_opt(A) ::= LP fieldlist(X) RP. {A = X;}
231fieldlist(A) ::= fieldlist(X) COMMA ID(Y). {A = sqliteIdListAppend(X,&Y);}
232fieldlist(A) ::= ID(Y). {A = sqliteIdListAppend(0,&Y);}
233
234%left OR.
235%left AND.
236%left EQ NE ISNULL NOTNULL IS.
237%left GT GE LT LE.
238%left PLUS MINUS.
239%left STAR SLASH PERCENT.
240%right NOT.
241
242%type expr {Expr*}
243%destructor expr {sqliteExprDelete($$);}
244
245expr(A) ::= LP expr(X) RP. {A = X;}
246expr(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
247expr(A) ::= NULL. {A = sqliteExpr(TK_NULL, 0, 0, 0);}
248expr(A) ::= ID(X) DOT ID(Y). {Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
249 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
250 A = sqliteExpr(TK_DOT, temp1, temp2, 0);}
251expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
252expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
253expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
254// expr(A) ::= ID(X) LP exprlist(Y) RP. {A = sqliteExprFunction(Y, &X);}
255// expr(A) ::= ID(X) LP STAR RP. {A = sqliteExprFunction(0, &X);}
256expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
257expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
258expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
259expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
260expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
261expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
262expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
263expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
264expr(A) ::= expr(X) IS expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
265expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
266expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
267expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
268expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
269expr(A) ::= expr(X) ISNULL. {A = sqliteExpr(TK_ISNULL, X, 0, 0);}
270expr(A) ::= expr(X) NOTNULL. {A = sqliteExpr(TK_NOTNULL, X, 0, 0);}
271expr(A) ::= NOT expr(X). {A = sqliteExpr(TK_NOT, X, 0, 0);}
272expr(A) ::= MINUS expr(X). [NOT] {A = sqliteExpr(TK_UMINUS, X, 0, 0);}
273expr(A) ::= PLUS expr(X). [NOT] {A = X;}
274
275%type exprlist {ExprList*}
276%destructor exprlist {sqliteExprListDelete($$);}
277%type expritem {Expr*}
278%destructor expritem {sqliteExprDelete($$);}
279
280/*
281exprlist(A) ::= exprlist(X) COMMA expritem(Y).
282 {A = sqliteExprListAppend(X,Y,0);}
283exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
284expritem(A) ::= expr(X). {A = X;}
285expritem(A) ::= . {A = 0;}
286*/
287
288cmd ::= CREATE(S) uniqueflag INDEX ID(X) ON ID(Y) LP idxlist(Z) RP(E).
289 {sqliteCreateIndex(pParse, &X, &Y, Z, &S, &E);}
290uniqueflag ::= UNIQUE.
291uniqueflag ::= .
292
293%type idxlist {IdList*}
294%destructor idxlist {sqliteIdListDelete($$);}
295%type idxitem {Token}
296
297idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
298 {A = sqliteIdListAppend(X,&Y);}
299idxlist(A) ::= idxitem(Y).
300 {A = sqliteIdListAppend(0,&Y);}
301idxitem(A) ::= ID(X). {A = X;}
302
303cmd ::= DROP INDEX ID(X). {sqliteDropIndex(pParse, &X);}