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