📄 parse.y
字号:
/*** 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.231 2007/06/20 12:18:31 drh Exp $*/// All token codes are small integers with #defines that begin with "TK_"%token_prefix TK_// The type of the data attached to each token is Token. This is also the// default type for non-terminals.//%token_type {Token}%default_type {Token}// The generated parser function takes a 4th argument as follows:%extra_argument {Parse *pParse}// This code runs whenever there is a syntax error//%syntax_error { if( !pParse->parseError ){ if( TOKEN.z[0] ){ sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); }else{ sqlite3ErrorMsg(pParse, "incomplete SQL statement"); } pParse->parseError = 1; }}%stack_overflow { sqlite3ErrorMsg(pParse, "parser stack overflow"); pParse->parseError = 1;}// The name of the generated procedure that implements the parser// is as follows:%name sqlite3Parser// The following text is included near the beginning of the C source// code file that implements the parser.//%include {#include "sqliteInt.h"#include "parse.h"/*** An instance of this structure holds information about the** LIMIT clause of a SELECT statement.*/struct LimitVal { Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ Expr *pOffset; /* The OFFSET expression. NULL if there is none */};/*** An instance of this structure is used to store the LIKE,** GLOB, NOT LIKE, and NOT GLOB operators.*/struct LikeOp { Token eOperator; /* "like" or "glob" or "regexp" */ int not; /* True if the NOT keyword is present */};/*** 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".*/struct TrigEvent { int a; IdList * b; };/*** An instance of this structure holds the ATTACH key and the key type.*/struct AttachKey { int type; Token key; };} // end %include// Input is a single SQL commandinput ::= cmdlist.cmdlist ::= cmdlist ecmd.cmdlist ::= ecmd.cmdx ::= cmd. { sqlite3FinishCoding(pParse); }ecmd ::= SEMI.ecmd ::= explain cmdx SEMI.explain ::= . { sqlite3BeginParse(pParse, 0); }%ifndef SQLITE_OMIT_EXPLAINexplain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); }explain ::= EXPLAIN QUERY PLAN. { sqlite3BeginParse(pParse, 2); }%endif SQLITE_OMIT_EXPLAIN///////////////////// Begin and end transactions. //////////////////////////////cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}trans_opt ::= .trans_opt ::= TRANSACTION.trans_opt ::= TRANSACTION nm.%type transtype {int}transtype(A) ::= . {A = TK_DEFERRED;}transtype(A) ::= DEFERRED(X). {A = @X;}transtype(A) ::= IMMEDIATE(X). {A = @X;}transtype(A) ::= EXCLUSIVE(X). {A = @X;}cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);}cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);}cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);}///////////////////// The CREATE TABLE statement //////////////////////////////cmd ::= create_table create_table_args.create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);}%type ifnotexists {int}ifnotexists(A) ::= . {A = 0;}ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}%type temp {int}%ifndef SQLITE_OMIT_TEMPDBtemp(A) ::= TEMP. {A = 1;}%endif SQLITE_OMIT_TEMPDBtemp(A) ::= . {A = 0;}create_table_args ::= LP columnlist conslist_opt(X) RP(Y). { sqlite3EndTable(pParse,&X,&Y,0);}create_table_args ::= AS select(S). { sqlite3EndTable(pParse,0,0,S); sqlite3SelectDelete(S);}columnlist ::= columnlist COMMA column.columnlist ::= column.// A "column" is a complete description of a single column in a// CREATE TABLE statement. This includes the column name, its// datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,// NOT NULL and so forth.//column(A) ::= columnid(X) type carglist. { A.z = X.z; A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n;}columnid(A) ::= nm(X). { sqlite3AddColumn(pParse,&X); A = X;}// An IDENTIFIER can be a generic identifier, or one of several// keywords. Any non-standard keyword can also be an identifier.//%type id {Token}id(A) ::= ID(X). {A = X;}// 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.//%fallback ID ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN QUERY KEY OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW TEMP TRIGGER VACUUM VIEW VIRTUAL%ifdef SQLITE_OMIT_COMPOUND_SELECT EXCEPT INTERSECT UNION%endif SQLITE_OMIT_COMPOUND_SELECT REINDEX RENAME CTIME_KW IF .%wildcard ANY.// 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.//// The token values assigned to these symbols is determined by the order// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See// the sqlite3ExprIfFalse() routine for additional information on this// constraint.//%left OR.%left AND.%right NOT.%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.%left GT LE LT GE.%right ESCAPE.%left BITAND BITOR LSHIFT RSHIFT.%left PLUS MINUS.%left STAR SLASH REM.%left CONCAT.%left COLLATE.%right UMINUS UPLUS BITNOT.// And "ids" is an identifer-or-string.//%type ids {Token}ids(A) ::= ID|STRING(X). {A = X;}// The name of a column or table can be any of the following://%type nm {Token}nm(A) ::= ID(X). {A = X;}nm(A) ::= STRING(X). {A = X;}nm(A) ::= JOIN_KW(X). {A = X;}// A typetoken is really one or more tokens that form a type name such// as can be found after the column name in a CREATE TABLE statement.// Multiple tokens are concatenated to form the value of the typetoken.//%type typetoken {Token}type ::= .type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);}typetoken(A) ::= typename(X). {A = X;}typetoken(A) ::= typename(X) LP signed RP(Y). { A.z = X.z; A.n = &Y.z[Y.n] - X.z;}typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). { A.z = X.z; A.n = &Y.z[Y.n] - X.z;}%type typename {Token}typename(A) ::= ids(X). {A = X;}typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}signed ::= plus_num.signed ::= minus_num.// "carglist" is a list of additional constraints that come after the// column name and column type in a CREATE TABLE statement.//carglist ::= carglist carg.carglist ::= .carg ::= CONSTRAINT nm ccons.carg ::= ccons.ccons ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,X);}ccons ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,X);}ccons ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,X);}ccons ::= DEFAULT MINUS term(X). { Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0); sqlite3AddDefaultValue(pParse,p);}ccons ::= DEFAULT id(X). { Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X); sqlite3AddDefaultValue(pParse,p);}// In addition to the type name, we also care about the primary key and// UNIQUE constraints.//ccons ::= NULL onconf.ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R). {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, (char*)C.z, C.n);}// The optional AUTOINCREMENT keyword%type autoinc {int}autoinc(X) ::= . {X = 0;}autoinc(X) ::= AUTOINCR. {X = 1;}// 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 refargs {int}refargs(A) ::= . { A = OE_Restrict * 0x010101; }refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }%type refarg {struct {int value; int mask;}}refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }%type refact {int}refact(A) ::= SET NULL. { A = OE_SetNull; }refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }refact(A) ::= CASCADE. { A = OE_Cascade; }refact(A) ::= RESTRICT. { A = OE_Restrict; }%type defer_subclause {int}defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}%type init_deferred_pred_opt {int}init_deferred_pred_opt(A) ::= . {A = 0;}init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}// For the time being, the only constraint we care about is the primary// key and UNIQUE. Both create indices.//conslist_opt(A) ::= . {A.n = 0; A.z = 0;}conslist_opt(A) ::= COMMA(X) conslist. {A = X;}conslist ::= conslist COMMA tcons.conslist ::= conslist tcons.conslist ::= tcons.tcons ::= CONSTRAINT nm.tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R). {sqlite3AddPrimaryKey(pParse,X,R,I,0);}tcons ::= UNIQUE LP idxlist(X) RP onconf(R). {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}tcons ::= FOREIGN KEY LP idxlist(FA) RP REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { sqlite3CreateForeignKey(pParse, FA, &T, TA, R); sqlite3DeferForeignKey(pParse, D);}%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 onconf {int}%type orconf {int}%type resolvetype {int}onconf(A) ::= . {A = OE_Default;}onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}orconf(A) ::= . {A = OE_Default;}orconf(A) ::= OR resolvetype(X). {A = X;}resolvetype(A) ::= raisetype(X). {A = X;}resolvetype(A) ::= IGNORE. {A = OE_Ignore;}resolvetype(A) ::= REPLACE. {A = OE_Replace;}////////////////////////// The DROP TABLE ///////////////////////////////////////cmd ::= DROP TABLE ifexists(E) fullname(X). { sqlite3DropTable(pParse, X, 0, E);}%type ifexists {int}ifexists(A) ::= IF EXISTS. {A = 1;}ifexists(A) ::= . {A = 0;}///////////////////// The CREATE VIEW statement ///////////////////////////////%ifndef SQLITE_OMIT_VIEWcmd ::= CREATE(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). { sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E);}cmd ::= DROP VIEW ifexists(E) fullname(X). { sqlite3DropTable(pParse, X, 1, E);}%endif SQLITE_OMIT_VIEW//////////////////////// The SELECT statement ///////////////////////////////////cmd ::= select(X). {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -