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