/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains SQLite's grammar for SQL. Process this file
** using the lemon parser generator to generate C code that runs
** the parser. Lemon will also generate a header file containing
** numeric codes for all of the tokens.
**
** @(#) $Id: parse.y,v 1.112 2004/02/22 18:40:57 drh Exp $
*/
%token_type {Token}
%syntax_error {
if( TOKEN.z[0] ){
}else{
}
}
}
%include {
#pragma ident "%Z%%M% %I% %E% SMI"
#include "sqliteInt.h"
#include "parse.h"
/*
** An instance of this structure holds information about the
** LIMIT clause of a SELECT statement.
*/
struct LimitVal {
int limit; /* The LIMIT value. -1 if there is no limit */
int offset; /* The OFFSET. 0 if there is none */
};
/*
** An instance of the following structure describes the event of a
** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
** TK_DELETE, or TK_INSTEAD. If the event is of the form
**
** UPDATE ON (a,b,c)
**
** Then the "b" IdList records the list "a,b,c".
*/
} // end %include
// These are extra tokens used by the lexer but never seen by the
// parser. We put them in a rule so that the parser generator will
// add them to the parse.h output file.
//
// Input is a single SQL command
///////////////////// Begin and end transactions. ////////////////////////////
//
trans_opt ::= .
///////////////////// The CREATE TABLE statement ////////////////////////////
//
sqliteStartTable(pParse,&X,&Y,T,0);
}
temp(A) ::= . {A = 0;}
sqliteEndTable(pParse,&X,0);
}
sqliteEndTable(pParse,0,S);
}
// About the only information used for a column is the name of the
// column. The type is always just "text". But the code will accept
// an elaborate typename. Perhaps someday we'll do something with it.
//
// An IDENTIFIER can be a generic identifier, or one of several
// keywords. Any non-standard keyword can also be an identifier.
//
// The following directive causes tokens ABORT, AFTER, ASC, etc. to
// fallback to ID if they will not parse as their original value.
// This obviates the need for the "id" nonterminal.
//
// Define operator precedence early so that this is the first occurance
// of the operator tokens in the grammer. Keeping the operators together
// causes them to be assigned integer values that are close together,
// which keeps parser tables smaller.
//
// And "ids" is an identifer-or-string.
//
// The name of a column or table can be any of the following:
//
type ::= .
{sqliteAddColumnType(pParse,&X,&Y);}
%type signed {int}
carglist ::= .
// In addition to the type name, we also care about the primary key and
// UNIQUE constraints.
//
}
// The next group of rules parses the arguments to a REFERENCES clause
// that determine if the referential integrity checking is deferred or
// or immediate and which determine what action to take if a ref-integ
// check fails.
//
%type defer_subclause {int}
%type init_deferred_pred_opt {int}
init_deferred_pred_opt(A) ::= . {A = 0;}
// For the time being, the only constraint we care about is the primary
// key and UNIQUE. Both create indices.
//
conslist_opt ::= .
{sqliteAddPrimaryKey(pParse,X,R);}
{sqliteCreateIndex(pParse,0,0,X,R,0,0);}
}
%type defer_subclause_opt {int}
defer_subclause_opt(A) ::= . {A = 0;}
defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
// The following is a non-standard extension that allows us to declare the
// default behavior when there is a constraint conflict.
//
%type resolvetype {int}
onconf(A) ::= . { A = OE_Default; }
orconf(A) ::= . { A = OE_Default; }
////////////////////////// The DROP TABLE /////////////////////////////////////
//
///////////////////// The CREATE VIEW statement /////////////////////////////
//
sqliteCreateView(pParse, &X, &Y, S, T);
}
}
//////////////////////// The SELECT statement /////////////////////////////////
//
}
if( Z ){
Z->op = Y;
Z->pPrior = X;
}
A = Z;
}
%type multiselect_op {int}
}
// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
// present and false (0) if it is not.
//
distinct(A) ::= . {A = 0;}
// selcollist is a list of expressions that are to become the return
// values of the SELECT statement. The "*" in statements like
// "SELECT * FROM ..." is encoded as a special expression with an
// opcode of TK_ALL.
//
sclp(A) ::= . {A = 0;}
A = sqliteExprListAppend(P,X,Y.n?&Y:0);
}
}
}
// An option "AS <id>" phrase that can follow one of the expressions that
// define the result set, or one of the tables in the FROM clause.
//
as(X) ::= . { X.n = 0; }
// A complete FROM clause.
//
from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
// "seltablist" is a "Select Table List" - the content of the FROM clause
// in a SELECT statement. "stl_prefix" is a prefix of this list.
//
A = X;
}
stl_prefix(A) ::= . {A = 0;}
A = sqliteSrcListAppend(X,&Y,&D);
if( Z.n ) sqliteSrcListAddAlias(A,&Z);
if( N ){
else { sqliteExprDelete(N); }
}
if( U ){
else { sqliteIdListDelete(U); }
}
}
A = sqliteSrcListAppend(X,0,0);
if( Z.n ) sqliteSrcListAddAlias(A,&Z);
if( N ){
else { sqliteExprDelete(N); }
}
if( U ){
else { sqliteIdListDelete(U); }
}
}
// A seltablist_paren nonterminal represents anything in a FROM that
// is contained inside parentheses. This can be either a subquery or
// a grouping of table and subqueries.
//
seltablist_paren(A) ::= select(S). {A = S;}
seltablist_paren(A) ::= seltablist(F). {
A = sqliteSelectNew(0,F,0,0,0,0,0,-1,0);
}
dbnm(A) ::= . {A.z=0; A.n=0;}
{ X = sqliteJoinType(pParse,&A,&B,&C); }
on_opt(N) ::= . {N = 0;}
using_opt(U) ::= . {U = 0;}
orderby_opt(A) ::= . {A = 0;}
A = sqliteExprListAppend(X,Y,0);
}
A = sqliteExprListAppend(0,Y,0);
if( A ) A->a[0].sortOrder = C+Z;
}
sortorder(A) ::= . {A = SQLITE_SO_ASC;}
collate(C) ::= . {C = SQLITE_SO_UNK;}
groupby_opt(A) ::= . {A = 0;}
having_opt(A) ::= . {A = 0;}
/////////////////////////// The DELETE statement /////////////////////////////
//
}
where_opt(A) ::= . {A = 0;}
////////////////////////// The UPDATE command ////////////////////////////////
//
{A = sqliteExprListAppend(Z,Y,&X);}
////////////////////////// The INSERT command /////////////////////////////////
//
%type insert_cmd {int}
inscollist_opt(A) ::= . {A = 0;}
/////////////////////////// Expression Processing /////////////////////////////
//
}
}
A = sqliteExpr(TK_VARIABLE, 0, 0, &X);
}
A = sqliteExprFunction(Y, &X);
sqliteExprSpan(A,&X,&E);
}
A = sqliteExprFunction(0, &X);
sqliteExprSpan(A,&X,&E);
}
A = sqliteExprFunction(pList, 0);
}
A = sqliteExprFunction(pList, 0);
A = sqliteExpr(TK_NOT, A, 0, 0);
}
A = sqliteExpr(TK_ISNULL, X, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_ISNULL, X, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_NOTNULL, X, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_NOTNULL, X, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_NOTNULL, X, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_NOT, X, 0, 0);
sqliteExprSpan(A,&B,&X->span);
}
A = sqliteExpr(TK_BITNOT, X, 0, 0);
sqliteExprSpan(A,&B,&X->span);
}
A = sqliteExpr(TK_UMINUS, X, 0, 0);
sqliteExprSpan(A,&B,&X->span);
}
A = sqliteExpr(TK_UPLUS, X, 0, 0);
sqliteExprSpan(A,&B,&X->span);
}
A = sqliteExpr(TK_SELECT, 0, 0, 0);
if( A ) A->pSelect = X;
sqliteExprSpan(A,&B,&E);
}
A = sqliteExpr(TK_BETWEEN, W, 0, 0);
}
A = sqliteExpr(TK_BETWEEN, W, 0, 0);
A = sqliteExpr(TK_NOT, A, 0, 0);
}
A = sqliteExpr(TK_IN, X, 0, 0);
if( A ) A->pList = Y;
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_IN, X, 0, 0);
if( A ) A->pSelect = Y;
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_IN, X, 0, 0);
if( A ) A->pList = Y;
A = sqliteExpr(TK_NOT, A, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_IN, X, 0, 0);
if( A ) A->pSelect = Y;
A = sqliteExpr(TK_NOT, A, 0, 0);
sqliteExprSpan(A,&X->span,&E);
}
A = sqliteExpr(TK_IN, X, 0, 0);
sqliteExprSpan(A,&X->span,D.z?&D:&Y);
}
A = sqliteExpr(TK_IN, X, 0, 0);
A = sqliteExpr(TK_NOT, A, 0, 0);
sqliteExprSpan(A,&X->span,D.z?&D:&Y);
}
/* CASE expressions */
A = sqliteExpr(TK_CASE, X, Z, 0);
if( A ) A->pList = Y;
sqliteExprSpan(A, &C, &E);
}
A = sqliteExprListAppend(X, Y, 0);
A = sqliteExprListAppend(A, Z, 0);
}
A = sqliteExprListAppend(0, Y, 0);
A = sqliteExprListAppend(A, Z, 0);
}
case_else(A) ::= . {A = 0;}
case_operand(A) ::= expr(X). {A = X;}
case_operand(A) ::= . {A = 0;}
{A = sqliteExprListAppend(X,Y,0);}
expritem(A) ::= . {A = 0;}
///////////////////////////// The CREATE INDEX command ///////////////////////
//
if( U!=OE_None ) U = R;
if( U==OE_Default) U = OE_Abort;
}
%type uniqueflag {int}
uniqueflag(A) ::= . { A = OE_None; }
idxlist_opt(A) ::= . {A = 0;}
///////////////////////////// The DROP INDEX command /////////////////////////
//
}
///////////////////////////// The COPY command ///////////////////////////////
//
///////////////////////////// The VACUUM command /////////////////////////////
//
///////////////////////////// The PRAGMA command /////////////////////////////
//
plus_opt ::= .
//////////////////////////// The CREATE TRIGGER command /////////////////////
all.z = A.z;
all.n = (Z.z - A.z) + Z.n;
}
}
%type trigger_time {int}
trigger_time(A) ::= . { A = TK_BEFORE; }
%type foreach_clause {int}
foreach_clause(A) ::= . { A = TK_ROW; }
when_clause(A) ::= . { A = 0; }
X->pNext = Y;
A = X;
}
trigger_cmd_list(A) ::= . { A = 0; }
// UPDATE
{ A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
// INSERT
{A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
{A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
// DELETE
{A = sqliteTriggerDeleteStep(&X, Y);}
// SELECT
// The special RAISE expression that may occur in trigger programs
A = sqliteExpr(TK_RAISE, 0, 0, 0);
sqliteExprSpan(A, &X, &Y);
}
A = sqliteExpr(TK_RAISE, 0, 0, &Z);
A->iColumn = OE_Rollback;
sqliteExprSpan(A, &X, &Y);
}
A = sqliteExpr(TK_RAISE, 0, 0, &Z);
sqliteExprSpan(A, &X, &Y);
}
A = sqliteExpr(TK_RAISE, 0, 0, &Z);
sqliteExprSpan(A, &X, &Y);
}
//////////////////////// DROP TRIGGER statement //////////////////////////////
}
//////////////////////// ATTACH DATABASE file AS name /////////////////////////
sqliteAttach(pParse, &F, &D, &K);
}
key_opt(A) ::= . { A.z = 0; A.n = 0; }
database_kw_opt ::= .
//////////////////////// DETACH DATABASE name /////////////////////////////////
sqliteDetach(pParse, &D);
}