xref: /sqlite-3.40.0/src/parse.y (revision dce2cbe6)
1348784efSdrh /*
2348784efSdrh ** Copyright (c) 1999, 2000 D. Richard Hipp
3348784efSdrh **
4348784efSdrh ** This program is free software; you can redistribute it and/or
5348784efSdrh ** modify it under the terms of the GNU General Public
6348784efSdrh ** License as published by the Free Software Foundation; either
7348784efSdrh ** version 2 of the License, or (at your option) any later version.
8348784efSdrh **
9348784efSdrh ** This program is distributed in the hope that it will be useful,
10348784efSdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11348784efSdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12348784efSdrh ** General Public License for more details.
13348784efSdrh **
14348784efSdrh ** You should have received a copy of the GNU General Public
15348784efSdrh ** License along with this library; if not, write to the
16348784efSdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17348784efSdrh ** Boston, MA  02111-1307, USA.
18348784efSdrh **
19348784efSdrh ** Author contact information:
20348784efSdrh **   [email protected]
21348784efSdrh **   http://www.hwaci.com/drh/
22348784efSdrh **
23348784efSdrh *************************************************************************
24348784efSdrh ** This file contains SQLite's grammar for SQL.  Process this file
25348784efSdrh ** using the lemon parser generator to generate C code that runs
26348784efSdrh ** the parser.  Lemon will also generate a header file containing
27348784efSdrh ** numeric codes for all of the tokens.
28348784efSdrh **
29*dce2cbe6Sdrh ** @(#) $Id: parse.y,v 1.3 2000/05/31 02:27:49 drh Exp $
30348784efSdrh */
31348784efSdrh %token_prefix TK_
32348784efSdrh %token_type {Token}
33348784efSdrh %extra_argument {Parse *pParse}
34348784efSdrh %syntax_error {
35348784efSdrh   sqliteSetNString(&pParse->zErrMsg,"syntax error near \"",0,TOKEN.z,TOKEN.n,
36348784efSdrh                    "\"", 1, 0);
37348784efSdrh   pParse->sErrToken = TOKEN;
38348784efSdrh }
39348784efSdrh %name sqliteParser
40348784efSdrh %include {
41348784efSdrh #include "sqliteInt.h"
42348784efSdrh #include "parse.h"
43348784efSdrh }
44348784efSdrh 
45348784efSdrh 
46348784efSdrh // Input is zero or more commands.
47348784efSdrh input ::= cmdlist.
48348784efSdrh 
49348784efSdrh // These are extra tokens used by the lexer but never seen by the
50348784efSdrh // parser.  We put them in a rule so that the parser generator will
51348784efSdrh // add them to the sqliteTokens.h output file.
52348784efSdrh //
53348784efSdrh input ::= END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
54348784efSdrh           UMINUS FIELD.
55348784efSdrh 
56348784efSdrh // A list of commands is zero or more commands
57348784efSdrh //
58348784efSdrh cmdlist ::= ecmd.
59348784efSdrh cmdlist ::= cmdlist SEMI ecmd.
60348784efSdrh ecmd ::= explain cmd.  {sqliteExec(pParse);}
61348784efSdrh ecmd ::= cmd.          {sqliteExec(pParse);}
62348784efSdrh ecmd ::= .
63348784efSdrh explain ::= EXPLAIN.    {pParse->explain = 1;}
64348784efSdrh 
65348784efSdrh // The first form of a command is a CREATE TABLE statement.
66348784efSdrh //
67348784efSdrh cmd ::= create_table create_table_args.
68982cef7eSdrh create_table ::= CREATE(X) TABLE id(Y).    {sqliteStartTable(pParse,&X,&Y);}
69348784efSdrh create_table_args ::= LP columnlist conslist_opt RP(X).
70348784efSdrh                                            {sqliteEndTable(pParse,&X);}
71348784efSdrh columnlist ::= columnlist COMMA column.
72348784efSdrh columnlist ::= column.
73348784efSdrh 
74348784efSdrh // About the only information used for a column is the name of the
75348784efSdrh // column.  The type is always just "text".  But the code will accept
76348784efSdrh // an elaborate typename.  Perhaps someday we'll do something with it.
77348784efSdrh //
78348784efSdrh column ::= columnid type carglist.
79982cef7eSdrh columnid ::= id(X).                {sqliteAddColumn(pParse,&X);}
80982cef7eSdrh %type id {Token}
81982cef7eSdrh id(A) ::= ID(X).     {A = X;}
82982cef7eSdrh id(A) ::= STRING(X). {A = X;}
83348784efSdrh type ::= typename.
84348784efSdrh type ::= typename LP signed RP.
85348784efSdrh type ::= typename LP signed COMMA signed RP.
86348784efSdrh typename ::= ID.
87348784efSdrh typename ::= typename ID.
88348784efSdrh signed ::= INTEGER.
89348784efSdrh signed ::= PLUS INTEGER.
90348784efSdrh signed ::= MINUS INTEGER.
91348784efSdrh carglist ::= carglist carg.
92348784efSdrh carglist ::= .
93348784efSdrh carg ::= CONSTRAINT ID ccons.
94348784efSdrh carg ::= ccons.
95348784efSdrh carg ::= DEFAULT expr.
96348784efSdrh 
97348784efSdrh // In addition to the type name, we also care about the primary key.
98348784efSdrh //
99348784efSdrh ccons ::= NOT NULL.
100348784efSdrh ccons ::= PRIMARY KEY sortorder.
101348784efSdrh    {sqliteCreateIndex(pParse,0,0,0,0,0);}
102348784efSdrh ccons ::= UNIQUE.
103348784efSdrh ccons ::= CHECK expr.
104348784efSdrh 
105348784efSdrh // For the time being, the only constraint we care about is the primary
106348784efSdrh // key.
107348784efSdrh //
108348784efSdrh conslist_opt ::= .
109348784efSdrh conslist_opt ::= COMMA conslist.
110348784efSdrh conslist ::= conslist COMMA tcons.
111348784efSdrh conslist ::= tcons.
112348784efSdrh tcons ::= CONSTRAINT ID tcons2.
113348784efSdrh tcons ::= tcons2.
114348784efSdrh tcons2 ::= PRIMARY KEY LP idxlist(X) RP.
115348784efSdrh       {sqliteCreateIndex(pParse,0,0,X,0,0);}
116348784efSdrh tcons2 ::= UNIQUE LP idlist RP.
117348784efSdrh tcons2 ::= CHECK expr.
118982cef7eSdrh idlist ::= idlist COMMA id.
119982cef7eSdrh idlist ::= id.
120348784efSdrh 
121348784efSdrh // The next command format is dropping tables.
122348784efSdrh //
123982cef7eSdrh cmd ::= DROP TABLE id(X).          {sqliteDropTable(pParse,&X);}
124348784efSdrh 
125348784efSdrh // The select statement
126348784efSdrh //
127348784efSdrh cmd ::= select.
128348784efSdrh select ::= SELECT selcollist(W) from(X) where_opt(Y) orderby_opt(Z).
129348784efSdrh      {sqliteSelect(pParse, W, X, Y, Z);}
130348784efSdrh select ::= SELECT STAR from(X) where_opt(Y) orderby_opt(Z).
131348784efSdrh      {sqliteSelect(pParse, 0, X, Y, Z);}
132348784efSdrh 
133348784efSdrh %type selcollist {ExprList*}
134348784efSdrh %destructor selcollist {sqliteExprListDelete($$);}
135348784efSdrh %type sclp {ExprList*}
136348784efSdrh %destructor sclp {sqliteExprListDelete($$);}
137348784efSdrh 
138348784efSdrh sclp(A) ::= selcollist(X) COMMA.             {A = X;}
139348784efSdrh sclp(A) ::= .                                {A = 0;}
140348784efSdrh selcollist(A) ::= sclp(P) expr(X).           {A = sqliteExprListAppend(P,X,0);}
141348784efSdrh selcollist(A) ::= sclp(P) expr(X) AS ID(Y).  {A = sqliteExprListAppend(P,X,&Y);}
142348784efSdrh selcollist(A) ::= sclp(P) expr(X) AS STRING(Y).
143348784efSdrh   {A = sqliteExprListAppend(P,X,&Y);}
144348784efSdrh 
145348784efSdrh %type seltablist {IdList*}
146348784efSdrh %destructor seltablist {sqliteIdListDelete($$);}
147348784efSdrh %type stl_prefix {IdList*}
148348784efSdrh %destructor stl_prefix {sqliteIdListDelete($$);}
149348784efSdrh %type from {IdList*}
150348784efSdrh %destructor from {sqliteIdListDelete($$);}
151348784efSdrh 
152348784efSdrh from(A) ::= FROM seltablist(X).               {A = X;}
153348784efSdrh stl_prefix(A) ::= seltablist(X) COMMA.        {A = X;}
154348784efSdrh stl_prefix(A) ::= .                           {A = 0;}
155982cef7eSdrh seltablist(A) ::= stl_prefix(X) id(Y).        {A = sqliteIdListAppend(X,&Y);}
156982cef7eSdrh seltablist(A) ::= stl_prefix(X) id(Y) AS id(Z).
157348784efSdrh    {A = sqliteIdListAppend(X,&Y);
158348784efSdrh     sqliteIdListAddAlias(A,&Z);}
159348784efSdrh 
160348784efSdrh %type orderby_opt {ExprList*}
161348784efSdrh %destructor orderby_opt {sqliteExprListDelete($$);}
162348784efSdrh %type sortlist {ExprList*}
163348784efSdrh %destructor sortlist {sqliteExprListDelete($$);}
164348784efSdrh %type sortitem {Expr*}
165348784efSdrh %destructor sortitem {sqliteExprDelete($$);}
166348784efSdrh 
167348784efSdrh orderby_opt(A) ::= .                          {A = 0;}
168348784efSdrh orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
169348784efSdrh sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z).
170348784efSdrh   {
171348784efSdrh     A = sqliteExprListAppend(X,Y,0);
172348784efSdrh     A->a[A->nExpr-1].idx = Z;
173348784efSdrh   }
174348784efSdrh sortlist(A) ::= sortitem(Y) sortorder(Z).
175348784efSdrh   {
176348784efSdrh     A = sqliteExprListAppend(0,Y,0);
177348784efSdrh     A->a[0].idx = Z;
178348784efSdrh   }
179348784efSdrh sortitem(A) ::= ID(X).           {A = sqliteExpr(TK_ID, 0, 0, &X);}
180348784efSdrh sortitem(A) ::= ID(X) DOT ID(Y).
181348784efSdrh   {
182348784efSdrh      Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
183348784efSdrh      Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
184348784efSdrh      A = sqliteExpr(TK_DOT, temp1, temp2, 0);
185348784efSdrh   }
186348784efSdrh 
187348784efSdrh %type sortorder {int}
188348784efSdrh 
189348784efSdrh sortorder(A) ::= ASC.      {A = 0;}
190348784efSdrh sortorder(A) ::= DESC.     {A = 1;}
191348784efSdrh sortorder(A) ::= .         {A = 0;}
192348784efSdrh 
193348784efSdrh cmd ::= DELETE FROM ID(X) where_opt(Y).
194348784efSdrh     {sqliteDeleteFrom(pParse, &X, Y);}
195348784efSdrh 
196348784efSdrh %type where_opt {Expr*}
197348784efSdrh %destructor where_opt {sqliteExprDelete($$);}
198348784efSdrh 
199348784efSdrh where_opt(A) ::= .                    {A = 0;}
200348784efSdrh where_opt(A) ::= WHERE expr(X).       {A = X;}
201348784efSdrh 
202348784efSdrh %type setlist {ExprList*}
203348784efSdrh %destructor setlist {sqliteExprListDelete($$);}
204348784efSdrh 
205348784efSdrh cmd ::= UPDATE ID(X) SET setlist(Y) where_opt(Z).
206348784efSdrh     {sqliteUpdate(pParse,&X,Y,Z);}
207348784efSdrh 
208348784efSdrh setlist(A) ::= ID(X) EQ expr(Y) COMMA setlist(Z).
209348784efSdrh     {A = sqliteExprListAppend(Z,Y,&X);}
210348784efSdrh setlist(A) ::= ID(X) EQ expr(Y).   {A = sqliteExprListAppend(0,Y,&X);}
211348784efSdrh 
212348784efSdrh cmd ::= INSERT INTO ID(X) fieldlist_opt(F) VALUES LP itemlist(Y) RP.
213348784efSdrh                {sqliteInsert(pParse, &X, Y, F);}
214348784efSdrh 
215348784efSdrh 
216348784efSdrh %type itemlist {ExprList*}
217348784efSdrh %destructor itemlist {sqliteExprListDelete($$);}
218348784efSdrh %type item {Expr*}
219348784efSdrh %destructor item {sqliteExprDelete($$);}
220348784efSdrh 
221348784efSdrh itemlist(A) ::= itemlist(X) COMMA item(Y).  {A = sqliteExprListAppend(X,Y,0);}
222348784efSdrh itemlist(A) ::= item(X).     {A = sqliteExprListAppend(0,X,0);}
223348784efSdrh item(A) ::= INTEGER(X).      {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
224348784efSdrh item(A) ::= FLOAT(X).        {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
225348784efSdrh item(A) ::= STRING(X).       {A = sqliteExpr(TK_STRING, 0, 0, &X);}
226348784efSdrh 
227348784efSdrh %type fieldlist_opt {IdList*}
228348784efSdrh %destructor fieldlist_opt {sqliteIdListDelete($$);}
229348784efSdrh %type fieldlist {IdList*}
230348784efSdrh %destructor fieldlist {sqliteIdListDelete($$);}
231348784efSdrh 
232348784efSdrh fieldlist_opt(A) ::= .                    {A = 0;}
233348784efSdrh fieldlist_opt(A) ::= LP fieldlist(X) RP.  {A = X;}
234348784efSdrh fieldlist(A) ::= fieldlist(X) COMMA ID(Y). {A = sqliteIdListAppend(X,&Y);}
235348784efSdrh fieldlist(A) ::= ID(Y).                    {A = sqliteIdListAppend(0,&Y);}
236348784efSdrh 
237348784efSdrh %left OR.
238348784efSdrh %left AND.
239*dce2cbe6Sdrh %left EQ NE ISNULL NOTNULL IS LIKE GLOB.
240348784efSdrh %left GT GE LT LE.
241348784efSdrh %left PLUS MINUS.
242348784efSdrh %left STAR SLASH PERCENT.
243348784efSdrh %right NOT.
244348784efSdrh 
245348784efSdrh %type expr {Expr*}
246348784efSdrh %destructor expr {sqliteExprDelete($$);}
247348784efSdrh 
248348784efSdrh expr(A) ::= LP expr(X) RP.   {A = X;}
249348784efSdrh expr(A) ::= ID(X).           {A = sqliteExpr(TK_ID, 0, 0, &X);}
250348784efSdrh expr(A) ::= NULL.            {A = sqliteExpr(TK_NULL, 0, 0, 0);}
251348784efSdrh expr(A) ::= ID(X) DOT ID(Y). {Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
252348784efSdrh                               Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
253348784efSdrh                               A = sqliteExpr(TK_DOT, temp1, temp2, 0);}
254348784efSdrh expr(A) ::= INTEGER(X).      {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
255348784efSdrh expr(A) ::= FLOAT(X).        {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
256348784efSdrh expr(A) ::= STRING(X).       {A = sqliteExpr(TK_STRING, 0, 0, &X);}
257348784efSdrh // expr(A) ::= ID(X) LP exprlist(Y) RP.  {A = sqliteExprFunction(Y, &X);}
258348784efSdrh // expr(A) ::= ID(X) LP STAR RP.         {A = sqliteExprFunction(0, &X);}
259348784efSdrh expr(A) ::= expr(X) AND expr(Y).   {A = sqliteExpr(TK_AND, X, Y, 0);}
260348784efSdrh expr(A) ::= expr(X) OR expr(Y).    {A = sqliteExpr(TK_OR, X, Y, 0);}
261348784efSdrh expr(A) ::= expr(X) LT expr(Y).    {A = sqliteExpr(TK_LT, X, Y, 0);}
262348784efSdrh expr(A) ::= expr(X) GT expr(Y).    {A = sqliteExpr(TK_GT, X, Y, 0);}
263348784efSdrh expr(A) ::= expr(X) LE expr(Y).    {A = sqliteExpr(TK_LE, X, Y, 0);}
264348784efSdrh expr(A) ::= expr(X) GE expr(Y).    {A = sqliteExpr(TK_GE, X, Y, 0);}
265348784efSdrh expr(A) ::= expr(X) NE expr(Y).    {A = sqliteExpr(TK_NE, X, Y, 0);}
266348784efSdrh expr(A) ::= expr(X) EQ expr(Y).    {A = sqliteExpr(TK_EQ, X, Y, 0);}
267*dce2cbe6Sdrh expr(A) ::= expr(X) LIKE expr(Y).  {A = sqliteExpr(TK_LIKE, X, Y, 0);}
268*dce2cbe6Sdrh expr(A) ::= expr(X) GLOB expr(Y).   {A = sqliteExpr(TK_GLOB,X,Y,0);}
269*dce2cbe6Sdrh // expr(A) ::= expr(X) IS expr(Y).    {A = sqliteExpr(TK_EQ, X, Y, 0);}
270348784efSdrh expr(A) ::= expr(X) PLUS expr(Y).  {A = sqliteExpr(TK_PLUS, X, Y, 0);}
271348784efSdrh expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
272348784efSdrh expr(A) ::= expr(X) STAR expr(Y).  {A = sqliteExpr(TK_STAR, X, Y, 0);}
273348784efSdrh expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
274348784efSdrh expr(A) ::= expr(X) ISNULL.        {A = sqliteExpr(TK_ISNULL, X, 0, 0);}
275348784efSdrh expr(A) ::= expr(X) NOTNULL.       {A = sqliteExpr(TK_NOTNULL, X, 0, 0);}
276348784efSdrh expr(A) ::= NOT expr(X).           {A = sqliteExpr(TK_NOT, X, 0, 0);}
277348784efSdrh expr(A) ::= MINUS expr(X). [NOT]   {A = sqliteExpr(TK_UMINUS, X, 0, 0);}
278348784efSdrh expr(A) ::= PLUS expr(X). [NOT]    {A = X;}
279348784efSdrh 
280348784efSdrh %type exprlist {ExprList*}
281348784efSdrh %destructor exprlist {sqliteExprListDelete($$);}
282348784efSdrh %type expritem {Expr*}
283348784efSdrh %destructor expritem {sqliteExprDelete($$);}
284348784efSdrh 
285348784efSdrh /*
286348784efSdrh exprlist(A) ::= exprlist(X) COMMA expritem(Y).
287348784efSdrh    {A = sqliteExprListAppend(X,Y,0);}
288348784efSdrh exprlist(A) ::= expritem(X).            {A = sqliteExprListAppend(0,X,0);}
289348784efSdrh expritem(A) ::= expr(X).                {A = X;}
290348784efSdrh expritem(A) ::= .                       {A = 0;}
291348784efSdrh */
292348784efSdrh 
293348784efSdrh cmd ::= CREATE(S) uniqueflag INDEX ID(X) ON ID(Y) LP idxlist(Z) RP(E).
294348784efSdrh     {sqliteCreateIndex(pParse, &X, &Y, Z, &S, &E);}
295348784efSdrh uniqueflag ::= UNIQUE.
296348784efSdrh uniqueflag ::= .
297348784efSdrh 
298348784efSdrh %type idxlist {IdList*}
299348784efSdrh %destructor idxlist {sqliteIdListDelete($$);}
300348784efSdrh %type idxitem {Token}
301348784efSdrh 
302348784efSdrh idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
303348784efSdrh      {A = sqliteIdListAppend(X,&Y);}
304348784efSdrh idxlist(A) ::= idxitem(Y).
305348784efSdrh      {A = sqliteIdListAppend(0,&Y);}
306348784efSdrh idxitem(A) ::= ID(X).           {A = X;}
307348784efSdrh 
308982cef7eSdrh cmd ::= DROP INDEX id(X).       {sqliteDropIndex(pParse, &X);}
309982cef7eSdrh 
310982cef7eSdrh cmd ::= COPY id(X) FROM id(Y) USING DELIMITERS STRING(Z).
311982cef7eSdrh     {sqliteCopy(pParse,&X,&Y,&Z);}
312982cef7eSdrh cmd ::= COPY id(X) FROM id(Y).
313982cef7eSdrh     {sqliteCopy(pParse,&X,&Y,0);}
314*dce2cbe6Sdrh 
315*dce2cbe6Sdrh cmd ::= VACUUM.                {sqliteVacuum(pParse,0);}
316*dce2cbe6Sdrh cmd ::= VACUUM id(X).          {sqliteVacuum(pParse,&X);}
317