xref: /sqlite-3.40.0/src/parse.y (revision 7c68d60b)
1 /*
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 **   [email protected]
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.25 2000/08/01 09:56:27 drh Exp $
30 */
31 %token_prefix TK_
32 %token_type {Token}
33 %extra_argument {Parse *pParse}
34 %syntax_error {
35   sqliteSetString(&pParse->zErrMsg,"syntax error",0);
36   pParse->sErrToken = TOKEN;
37 }
38 %name sqliteParser
39 %include {
40 #include "sqliteInt.h"
41 #include "parse.h"
42 }
43 
44 
45 // Input is zero or more commands.
46 input ::= cmdlist.
47 
48 // These are extra tokens used by the lexer but never seen by the
49 // parser.  We put them in a rule so that the parser generator will
50 // add them to the parse.h output file.
51 //
52 input ::= END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
53           UMINUS COLUMN AGG_FUNCTION.
54 
55 // A list of commands is zero or more commands
56 //
57 cmdlist ::= ecmd.
58 cmdlist ::= cmdlist SEMI ecmd.
59 ecmd ::= explain cmd.  {sqliteExec(pParse);}
60 ecmd ::= cmd.          {sqliteExec(pParse);}
61 ecmd ::= .
62 explain ::= EXPLAIN.    {pParse->explain = 1;}
63 
64 // The first form of a command is a CREATE TABLE statement.
65 //
66 cmd ::= create_table create_table_args.
67 create_table ::= CREATE(X) TABLE id(Y).    {sqliteStartTable(pParse,&X,&Y);}
68 create_table_args ::= LP columnlist conslist_opt RP(X).
69                                            {sqliteEndTable(pParse,&X);}
70 columnlist ::= columnlist COMMA column.
71 columnlist ::= column.
72 
73 // About the only information used for a column is the name of the
74 // column.  The type is always just "text".  But the code will accept
75 // an elaborate typename.  Perhaps someday we'll do something with it.
76 //
77 column ::= columnid type carglist.
78 columnid ::= id(X).                {sqliteAddColumn(pParse,&X);}
79 %type id {Token}
80 id(A) ::= ID(X).     {A = X;}
81 id(A) ::= STRING(X). {A = X;}
82 type ::= typename.
83 type ::= typename LP signed RP.
84 type ::= typename LP signed COMMA signed RP.
85 typename ::= id.
86 typename ::= typename id.
87 signed ::= INTEGER.
88 signed ::= PLUS INTEGER.
89 signed ::= MINUS INTEGER.
90 carglist ::= carglist carg.
91 carglist ::= .
92 carg ::= CONSTRAINT id ccons.
93 carg ::= ccons.
94 carg ::= DEFAULT STRING(X).          {sqliteAddDefaultValue(pParse,&X,0);}
95 carg ::= DEFAULT ID(X).              {sqliteAddDefaultValue(pParse,&X,0);}
96 carg ::= DEFAULT INTEGER(X).         {sqliteAddDefaultValue(pParse,&X,0);}
97 carg ::= DEFAULT PLUS INTEGER(X).    {sqliteAddDefaultValue(pParse,&X,0);}
98 carg ::= DEFAULT MINUS INTEGER(X).   {sqliteAddDefaultValue(pParse,&X,1);}
99 carg ::= DEFAULT FLOAT(X).           {sqliteAddDefaultValue(pParse,&X,0);}
100 carg ::= DEFAULT PLUS FLOAT(X).      {sqliteAddDefaultValue(pParse,&X,0);}
101 carg ::= DEFAULT MINUS FLOAT(X).     {sqliteAddDefaultValue(pParse,&X,1);}
102 carg ::= DEFAULT NULL.
103 
104 // In addition to the type name, we also care about the primary key.
105 //
106 ccons ::= NOT NULL.
107 ccons ::= PRIMARY KEY sortorder.     {sqliteCreateIndex(pParse,0,0,0,0,0);}
108 ccons ::= UNIQUE.
109 ccons ::= CHECK LP expr RP.
110 
111 // For the time being, the only constraint we care about is the primary
112 // key.
113 //
114 conslist_opt ::= .
115 conslist_opt ::= COMMA conslist.
116 conslist ::= conslist COMMA tcons.
117 conslist ::= tcons.
118 tcons ::= CONSTRAINT id tcons2.
119 tcons ::= tcons2.
120 tcons2 ::= PRIMARY KEY LP idxlist(X) RP. {sqliteCreateIndex(pParse,0,0,X,0,0);}
121 tcons2 ::= UNIQUE LP idlist RP.
122 tcons2 ::= CHECK expr.
123 idlist ::= idlist COMMA id.
124 idlist ::= id.
125 
126 // The next command format is dropping tables.
127 //
128 cmd ::= DROP TABLE id(X).          {sqliteDropTable(pParse,&X);}
129 
130 // The select statement
131 //
132 cmd ::= select(X).  {
133   sqliteSelect(pParse, X, SRT_Callback, 0);
134   sqliteSelectDelete(X);
135 }
136 
137 %type select {Select*}
138 %destructor select {sqliteSelectDelete($$);}
139 %type oneselect {Select*}
140 %destructor oneselect {sqliteSelectDelete($$);}
141 
142 select(A) ::= oneselect(X).                      {A = X;}
143 select(A) ::= select(X) joinop(Y) oneselect(Z).  {
144     Z->op = Y;
145     Z->pPrior = X;
146     A = Z;
147 }
148 %type joinop {int}
149 joinop(A) ::= UNION.      {A = TK_UNION;}
150 joinop(A) ::= UNION ALL.  {A = TK_ALL;}
151 joinop(A) ::= INTERSECT.  {A = TK_INTERSECT;}
152 joinop(A) ::= EXCEPT.     {A = TK_EXCEPT;}
153 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
154                  groupby_opt(P) having_opt(Q) orderby_opt(Z). {
155   A = sqliteSelectNew(W,X,Y,P,Q,Z,D);
156 }
157 
158 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
159 // present and false (0) if it is not.
160 //
161 %type distinct {int}
162 distinct(A) ::= DISTINCT.   {A = 1;}
163 distinct(A) ::= ALL.        {A = 0;}
164 distinct(A) ::= .           {A = 0;}
165 
166 // selcollist is a list of expressions that are to become the return
167 // values of the SELECT statement.  In the case of "SELECT * FROM ..."
168 // the selcollist value is NULL.
169 //
170 %type selcollist {ExprList*}
171 %destructor selcollist {sqliteExprListDelete($$);}
172 %type sclp {ExprList*}
173 %destructor sclp {sqliteExprListDelete($$);}
174 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
175 sclp(A) ::= .                                {A = 0;}
176 selcollist(A) ::= STAR.                      {A = 0;}
177 selcollist(A) ::= sclp(P) expr(X).           {A = sqliteExprListAppend(P,X,0);}
178 selcollist(A) ::= sclp(P) expr(X) as id(Y).  {A = sqliteExprListAppend(P,X,&Y);}
179 as ::= .
180 as ::= AS.
181 
182 
183 %type seltablist {IdList*}
184 %destructor seltablist {sqliteIdListDelete($$);}
185 %type stl_prefix {IdList*}
186 %destructor stl_prefix {sqliteIdListDelete($$);}
187 %type from {IdList*}
188 %destructor from {sqliteIdListDelete($$);}
189 
190 from(A) ::= FROM seltablist(X).               {A = X;}
191 stl_prefix(A) ::= seltablist(X) COMMA.        {A = X;}
192 stl_prefix(A) ::= .                           {A = 0;}
193 seltablist(A) ::= stl_prefix(X) id(Y).        {A = sqliteIdListAppend(X,&Y);}
194 seltablist(A) ::= stl_prefix(X) id(Y) as id(Z).
195    {A = sqliteIdListAppend(X,&Y);
196     sqliteIdListAddAlias(A,&Z);}
197 
198 %type orderby_opt {ExprList*}
199 %destructor orderby_opt {sqliteExprListDelete($$);}
200 %type sortlist {ExprList*}
201 %destructor sortlist {sqliteExprListDelete($$);}
202 %type sortitem {Expr*}
203 %destructor sortitem {sqliteExprDelete($$);}
204 
205 orderby_opt(A) ::= .                          {A = 0;}
206 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
207 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
208   A = sqliteExprListAppend(X,Y,0);
209   A->a[A->nExpr-1].sortOrder = Z;   /* 0 for ascending order, 1 for decending */
210 }
211 sortlist(A) ::= sortitem(Y) sortorder(Z). {
212   A = sqliteExprListAppend(0,Y,0);
213   A->a[0].sortOrder = Z;
214 }
215 sortitem(A) ::= expr(X).   {A = X;}
216 
217 %type sortorder {int}
218 
219 sortorder(A) ::= ASC.      {A = 0;}
220 sortorder(A) ::= DESC.     {A = 1;}
221 sortorder(A) ::= .         {A = 0;}
222 
223 %type groupby_opt {ExprList*}
224 %destructor groupby_opt {sqliteExprListDelete($$);}
225 groupby_opt(A) ::= .                      {A = 0;}
226 groupby_opt(A) ::= GROUP BY exprlist(X).  {A = X;}
227 
228 %type having_opt {Expr*}
229 %destructor having_opt {sqliteExprDelete($$);}
230 having_opt(A) ::= .                {A = 0;}
231 having_opt(A) ::= HAVING expr(X).  {A = X;}
232 
233 
234 cmd ::= DELETE FROM id(X) where_opt(Y).
235     {sqliteDeleteFrom(pParse, &X, Y);}
236 
237 %type where_opt {Expr*}
238 %destructor where_opt {sqliteExprDelete($$);}
239 
240 where_opt(A) ::= .                    {A = 0;}
241 where_opt(A) ::= WHERE expr(X).       {A = X;}
242 
243 %type setlist {ExprList*}
244 %destructor setlist {sqliteExprListDelete($$);}
245 
246 cmd ::= UPDATE id(X) SET setlist(Y) where_opt(Z).
247     {sqliteUpdate(pParse,&X,Y,Z);}
248 
249 setlist(A) ::= setlist(Z) COMMA id(X) EQ expr(Y).
250     {A = sqliteExprListAppend(Z,Y,&X);}
251 setlist(A) ::= id(X) EQ expr(Y).   {A = sqliteExprListAppend(0,Y,&X);}
252 
253 cmd ::= INSERT INTO id(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
254                {sqliteInsert(pParse, &X, Y, 0, F);}
255 cmd ::= INSERT INTO id(X) inscollist_opt(F) select(S).
256                {sqliteInsert(pParse, &X, 0, S, F);}
257 
258 
259 %type itemlist {ExprList*}
260 %destructor itemlist {sqliteExprListDelete($$);}
261 %type item {Expr*}
262 %destructor item {sqliteExprDelete($$);}
263 
264 itemlist(A) ::= itemlist(X) COMMA item(Y).  {A = sqliteExprListAppend(X,Y,0);}
265 itemlist(A) ::= item(X).     {A = sqliteExprListAppend(0,X,0);}
266 item(A) ::= INTEGER(X).      {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
267 item(A) ::= PLUS INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
268 item(A) ::= MINUS INTEGER(X). {
269   A = sqliteExpr(TK_UMINUS, 0, 0, 0);
270   A->pLeft = sqliteExpr(TK_INTEGER, 0, 0, &X);
271 }
272 item(A) ::= FLOAT(X).        {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
273 item(A) ::= PLUS FLOAT(X).   {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
274 item(A) ::= MINUS FLOAT(X).  {
275   A = sqliteExpr(TK_UMINUS, 0, 0, 0);
276   A->pLeft = sqliteExpr(TK_FLOAT, 0, 0, &X);
277 }
278 item(A) ::= STRING(X).       {A = sqliteExpr(TK_STRING, 0, 0, &X);}
279 item(A) ::= NULL.            {A = sqliteExpr(TK_NULL, 0, 0, 0);}
280 
281 %type inscollist_opt {IdList*}
282 %destructor inscollist_opt {sqliteIdListDelete($$);}
283 %type inscollist {IdList*}
284 %destructor inscollist {sqliteIdListDelete($$);}
285 
286 inscollist_opt(A) ::= .                      {A = 0;}
287 inscollist_opt(A) ::= LP inscollist(X) RP.   {A = X;}
288 inscollist(A) ::= inscollist(X) COMMA id(Y). {A = sqliteIdListAppend(X,&Y);}
289 inscollist(A) ::= id(Y).                     {A = sqliteIdListAppend(0,&Y);}
290 
291 %left OR.
292 %left AND.
293 %right NOT.
294 %left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
295 %left GT GE LT LE.
296 %left PLUS MINUS.
297 %left STAR SLASH.
298 %left CONCAT.
299 %right UMINUS.
300 
301 %type expr {Expr*}
302 %destructor expr {sqliteExprDelete($$);}
303 
304 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
305 expr(A) ::= ID(X).               {A = sqliteExpr(TK_ID, 0, 0, &X);}
306 expr(A) ::= NULL(X).             {A = sqliteExpr(TK_NULL, 0, 0, &X);}
307 expr(A) ::= id(X) DOT id(Y). {
308   Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
309   Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
310   A = sqliteExpr(TK_DOT, temp1, temp2, 0);
311 }
312 expr(A) ::= INTEGER(X).      {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
313 expr(A) ::= FLOAT(X).        {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
314 expr(A) ::= STRING(X).       {A = sqliteExpr(TK_STRING, 0, 0, &X);}
315 expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
316   A = sqliteExprFunction(Y, &X);
317   sqliteExprSpan(A,&X,&E);
318 }
319 expr(A) ::= ID(X) LP STAR RP(E). {
320   A = sqliteExprFunction(0, &X);
321   sqliteExprSpan(A,&X,&E);
322 }
323 expr(A) ::= expr(X) AND expr(Y).   {A = sqliteExpr(TK_AND, X, Y, 0);}
324 expr(A) ::= expr(X) OR expr(Y).    {A = sqliteExpr(TK_OR, X, Y, 0);}
325 expr(A) ::= expr(X) LT expr(Y).    {A = sqliteExpr(TK_LT, X, Y, 0);}
326 expr(A) ::= expr(X) GT expr(Y).    {A = sqliteExpr(TK_GT, X, Y, 0);}
327 expr(A) ::= expr(X) LE expr(Y).    {A = sqliteExpr(TK_LE, X, Y, 0);}
328 expr(A) ::= expr(X) GE expr(Y).    {A = sqliteExpr(TK_GE, X, Y, 0);}
329 expr(A) ::= expr(X) NE expr(Y).    {A = sqliteExpr(TK_NE, X, Y, 0);}
330 expr(A) ::= expr(X) EQ expr(Y).    {A = sqliteExpr(TK_EQ, X, Y, 0);}
331 expr(A) ::= expr(X) LIKE expr(Y).  {A = sqliteExpr(TK_LIKE, X, Y, 0);}
332 expr(A) ::= expr(X) NOT LIKE expr(Y).  {
333   A = sqliteExpr(TK_LIKE, X, Y, 0);
334   A = sqliteExpr(TK_NOT, A, 0, 0);
335   sqliteExprSpan(A,&X->span,&Y->span);
336 }
337 expr(A) ::= expr(X) GLOB expr(Y).  {A = sqliteExpr(TK_GLOB,X,Y,0);}
338 expr(A) ::= expr(X) NOT GLOB expr(Y).  {
339   A = sqliteExpr(TK_GLOB, X, Y, 0);
340   A = sqliteExpr(TK_NOT, A, 0, 0);
341   sqliteExprSpan(A,&X->span,&Y->span);
342 }
343 expr(A) ::= expr(X) PLUS expr(Y).  {A = sqliteExpr(TK_PLUS, X, Y, 0);}
344 expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
345 expr(A) ::= expr(X) STAR expr(Y).  {A = sqliteExpr(TK_STAR, X, Y, 0);}
346 expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
347 expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
348 expr(A) ::= expr(X) ISNULL(E). {
349   A = sqliteExpr(TK_ISNULL, X, 0, 0);
350   sqliteExprSpan(A,&X->span,&E);
351 }
352 expr(A) ::= expr(X) NOTNULL(E). {
353   A = sqliteExpr(TK_NOTNULL, X, 0, 0);
354   sqliteExprSpan(A,&X->span,&E);
355 }
356 expr(A) ::= NOT(B) expr(X). {
357   A = sqliteExpr(TK_NOT, X, 0, 0);
358   sqliteExprSpan(A,&B,&X->span);
359 }
360 expr(A) ::= MINUS(B) expr(X). [UMINUS] {
361   A = sqliteExpr(TK_UMINUS, X, 0, 0);
362   sqliteExprSpan(A,&B,&X->span);
363 }
364 expr(A) ::= PLUS(B) expr(X). [UMINUS] {
365   A = X;
366   sqliteExprSpan(A,&B,&X->span);
367 }
368 expr(A) ::= LP(B) select(X) RP(E). {
369   A = sqliteExpr(TK_SELECT, 0, 0, 0);
370   A->pSelect = X;
371   sqliteExprSpan(A,&B,&E);
372 }
373 expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
374   ExprList *pList = sqliteExprListAppend(0, X, 0);
375   pList = sqliteExprListAppend(pList, Y, 0);
376   A = sqliteExpr(TK_BETWEEN, W, 0, 0);
377   A->pList = pList;
378   sqliteExprSpan(A,&W->span,&Y->span);
379 }
380 expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
381   ExprList *pList = sqliteExprListAppend(0, X, 0);
382   pList = sqliteExprListAppend(pList, Y, 0);
383   A = sqliteExpr(TK_BETWEEN, W, 0, 0);
384   A->pList = pList;
385   A = sqliteExpr(TK_NOT, A, 0, 0);
386   sqliteExprSpan(A,&W->span,&Y->span);
387 }
388 expr(A) ::= expr(X) IN LP exprlist(Y) RP(E).  {
389   A = sqliteExpr(TK_IN, X, 0, 0);
390   A->pList = Y;
391   sqliteExprSpan(A,&X->span,&E);
392 }
393 expr(A) ::= expr(X) IN LP select(Y) RP(E).  {
394   A = sqliteExpr(TK_IN, X, 0, 0);
395   A->pSelect = Y;
396   sqliteExprSpan(A,&X->span,&E);
397 }
398 expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E).  {
399   A = sqliteExpr(TK_IN, X, 0, 0);
400   A->pList = Y;
401   A = sqliteExpr(TK_NOT, A, 0, 0);
402   sqliteExprSpan(A,&X->span,&E);
403 }
404 expr(A) ::= expr(X) NOT IN LP select(Y) RP(E).  {
405   A = sqliteExpr(TK_IN, X, 0, 0);
406   A->pSelect = Y;
407   A = sqliteExpr(TK_NOT, A, 0, 0);
408   sqliteExprSpan(A,&X->span,&E);
409 }
410 
411 
412 
413 %type exprlist {ExprList*}
414 %destructor exprlist {sqliteExprListDelete($$);}
415 %type expritem {Expr*}
416 %destructor expritem {sqliteExprDelete($$);}
417 
418 exprlist(A) ::= exprlist(X) COMMA expritem(Y).
419    {A = sqliteExprListAppend(X,Y,0);}
420 exprlist(A) ::= expritem(X).            {A = sqliteExprListAppend(0,X,0);}
421 expritem(A) ::= expr(X).                {A = X;}
422 expritem(A) ::= .                       {A = 0;}
423 
424 
425 cmd ::= CREATE(S) uniqueflag INDEX id(X) ON id(Y) LP idxlist(Z) RP(E).
426     {sqliteCreateIndex(pParse, &X, &Y, Z, &S, &E);}
427 uniqueflag ::= UNIQUE.
428 uniqueflag ::= .
429 
430 %type idxlist {IdList*}
431 %destructor idxlist {sqliteIdListDelete($$);}
432 %type idxitem {Token}
433 
434 idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
435      {A = sqliteIdListAppend(X,&Y);}
436 idxlist(A) ::= idxitem(Y).
437      {A = sqliteIdListAppend(0,&Y);}
438 idxitem(A) ::= id(X).           {A = X;}
439 
440 cmd ::= DROP INDEX id(X).       {sqliteDropIndex(pParse, &X);}
441 
442 cmd ::= COPY id(X) FROM id(Y) USING DELIMITERS STRING(Z).
443     {sqliteCopy(pParse,&X,&Y,&Z);}
444 cmd ::= COPY id(X) FROM id(Y).
445     {sqliteCopy(pParse,&X,&Y,0);}
446 
447 cmd ::= VACUUM.                {sqliteVacuum(pParse,0);}
448 cmd ::= VACUUM id(X).          {sqliteVacuum(pParse,&X);}
449