📄 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.112 2004/02/22 18:40:57 drh Exp $*/%token_prefix TK_%token_type {Token}%default_type {Token}%extra_argument {Parse *pParse}%syntax_error { if( pParse->zErrMsg==0 ){ if( TOKEN.z[0] ){ sqliteErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); }else{ sqliteErrorMsg(pParse, "incomplete SQL statement"); } }}%name sqliteParser%include {#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".*/struct TrigEvent { int a; IdList * b; };} // 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.//%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION COLUMN AGG_FUNCTION.// Input is a single SQL commandinput ::= cmdlist.cmdlist ::= cmdlist ecmd.cmdlist ::= ecmd.ecmd ::= explain cmdx SEMI.ecmd ::= SEMI.cmdx ::= cmd. { sqliteExec(pParse); }explain ::= EXPLAIN. { sqliteBeginParse(pParse, 1); }explain ::= . { sqliteBeginParse(pParse, 0); }///////////////////// Begin and end transactions. //////////////////////////////cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}trans_opt ::= .trans_opt ::= TRANSACTION.trans_opt ::= TRANSACTION nm.cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}///////////////////// The CREATE TABLE statement //////////////////////////////cmd ::= create_table create_table_args.create_table ::= CREATE(X) temp(T) TABLE nm(Y). { sqliteStartTable(pParse,&X,&Y,T,0);}%type temp {int}temp(A) ::= TEMP. {A = 1;}temp(A) ::= . {A = 0;}create_table_args ::= LP columnlist conslist_opt RP(X). { sqliteEndTable(pParse,&X,0);}create_table_args ::= AS select(S). { sqliteEndTable(pParse,0,S); sqliteSelectDelete(S);}columnlist ::= columnlist COMMA column.columnlist ::= column.// 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.//column ::= columnid type carglist. columnid ::= nm(X). {sqliteAddColumn(pParse,&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 ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT COPY DATABASE DEFERRED DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR GLOB IGNORE IMMEDIATE INITIALLY INSTEAD LIKE MATCH KEY OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT TEMP TRIGGER VACUUM VIEW.// 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.//%left OR.%left AND.%right NOT.%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.%left GT GE LT LE.%left BITAND BITOR LSHIFT RSHIFT.%left PLUS MINUS.%left STAR SLASH REM.%left CONCAT.%right UMINUS UPLUS BITNOT.// And "ids" is an identifer-or-string.//%type ids {Token}ids(A) ::= ID(X). {A = X;}ids(A) ::= 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;}type ::= .type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}type ::= typename(X) LP signed COMMA signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}%type typename {Token}typename(A) ::= ids(X). {A = X;}typename(A) ::= typename(X) ids. {A = X;}%type signed {int}signed(A) ::= INTEGER(X). { A = atoi(X.z); }signed(A) ::= PLUS INTEGER(X). { A = atoi(X.z); }signed(A) ::= MINUS INTEGER(X). { A = -atoi(X.z); }carglist ::= carglist carg.carglist ::= .carg ::= CONSTRAINT nm ccons.carg ::= ccons.carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}carg ::= DEFAULT NULL. // In addition to the type name, we also care about the primary key and// UNIQUE constraints.//ccons ::= NULL onconf.ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}ccons ::= CHECK LP expr RP onconf.ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R). {sqliteCreateForeignKey(pParse,0,&T,TA,R);}ccons ::= defer_subclause(D). {sqliteDeferForeignKey(pParse,D);}ccons ::= COLLATE id(C). { sqliteAddCollateType(pParse, sqliteCollateType(C.z, C.n));}// 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 ::= .conslist_opt ::= COMMA conslist.conslist ::= conslist COMMA tcons.conslist ::= conslist tcons.conslist ::= tcons.tcons ::= CONSTRAINT nm.tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R). {sqliteAddPrimaryKey(pParse,X,R);}tcons ::= UNIQUE LP idxlist(X) RP onconf(R). {sqliteCreateIndex(pParse,0,0,X,R,0,0);}tcons ::= CHECK expr onconf.tcons ::= FOREIGN KEY LP idxlist(FA) RP REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { sqliteCreateForeignKey(pParse, FA, &T, TA, R); sqliteDeferForeignKey(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) ::= ROLLBACK. { A = OE_Rollback; }resolvetype(A) ::= ABORT. { A = OE_Abort; }resolvetype(A) ::= FAIL. { A = OE_Fail; }resolvetype(A) ::= IGNORE. { A = OE_Ignore; }resolvetype(A) ::= REPLACE. { A = OE_Replace; }////////////////////////// The DROP TABLE ///////////////////////////////////////cmd ::= DROP TABLE nm(X). {sqliteDropTable(pParse,&X,0);}///////////////////// The CREATE VIEW statement ///////////////////////////////cmd ::= CREATE(X) temp(T) VIEW nm(Y) AS select(S). { sqliteCreateView(pParse, &X, &Y, S, T);}cmd ::= DROP VIEW nm(X). { sqliteDropTable(pParse, &X, 1);}//////////////////////// The SELECT statement ///////////////////////////////////cmd ::= select(X). { sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0); sqliteSelectDelete(X);}%type select {Select*}%destructor select {sqliteSelectDelete($$);}%type oneselect {Select*}%destructor oneselect {sqliteSelectDelete($$);}select(A) ::= oneselect(X). {A = X;}select(A) ::= select(X) multiselect_op(Y) oneselect(Z). { if( Z ){ Z->op = Y; Z->pPrior = X; } A = Z;}%type multiselect_op {int}multiselect_op(A) ::= UNION. {A = TK_UNION;}multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -