共計 5850 個字符,預計需要花費 15 分鐘才能閱讀完成。
這篇文章主要介紹“PostgreSQL 中 Declarations 的作用是什么”,在日常操作中,相信很多人在 PostgreSQL 中 Declarations 的作用是什么問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL 中 Declarations 的作用是什么”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
Flex 輸入文件由四部分組成:
%{
Declarations
Definitions
Rules
User subroutines
Declarations
由 %{和 %} 包含的部分為 Declarations 部分, 這一部分都是 C 代碼, 會原封不動的 copy 到 lex.yy.c 文件中.
比較重要的定義包括:
YYSTYPE-Bison 使用一個 union 聯合體來存儲所有可能類型的值, 全局變量 yyvalue 的類型是 YYSTYPE.
%top{
/*-------------------------------------------------------------------------
*
* scan.l
* lexical scanner for PostgreSQL
* PostgreSQL 的詞法掃描器
*
* NOTE NOTE NOTE:
* 特別特別特別注意:
* The rules in this file must be kept in sync with src/fe_utils/psqlscan.l!
* 這個文件中的規則必須與 src/fe_utils/psqlscan.l 文件中的規則保持一致!!!
*
* The rules are designed so that the scanner never has to backtrack,
* in the sense that there is always a rule that can match the input
* consumed so far (the rule action may internally throw back some input
* with yyless(), however). As explained in the flex manual, this makes
* for a useful speed increase --- about a third faster than a plain -CF
* lexer, in simple testing. The extra complexity is mostly in the rules
* for handling float numbers and continued string literals. If you change
* the lexical rules, verify that you haven t broken the no-backtrack
* property by running flex with the -b option and checking that the
* resulting lex.backup file says that no backing up is needed. (As of
* Postgres 9.2, this check is made automatically by the Makefile.)
* 之所以設計這一的規則是便于掃描器不需要回溯, 確保對于輸入一定有一條規則與其匹配
* (但是, 規則動作可能在內部用 yyless() throw back 一些輸入 ).
* 正如 Flex 手冊中所說明的, 這可以提升性能 --
* 在簡單測試的情況下, 相對于普通的 -CF 詞法分析器, 大概有 1 / 3 的性能提升.
* 額外的復雜性主要體現在處理浮點數和連續字符串文字的規則中.
* 如果修改了詞法規則, 通過以 - b 選項執行 Flex 以確保沒有打破無回溯的約定,
* 并且堅持結果文件 lex.backup 以確認無需備份.
* (在 PG 9.2, 該檢查通過 Makefile 自動執行)
*
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/parser/scan.l
*
*-------------------------------------------------------------------------
*/
#include postgres.h
#include ctype.h
#include unistd.h
#include common/string.h
#include parser/gramparse.h
#include parser/parser.h /* only needed for GUC variables */
#include parser/scansup.h
#include mb/pg_wchar.h
//------------------ 聲明部分
/* LCOV_EXCL_START */
/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
// 在掃描器出現致命錯誤時, 避免調用 exit() 直接退出
#undef fprintf
#define fprintf(file, fmt, msg) fprintf_to_ereport(fmt, msg)
static void
fprintf_to_ereport(const char *fmt, const char *msg)
ereport(ERROR, (errmsg_internal( %s , msg)));
* GUC variables. This is a DIRECT violation of the warning given at the
* head of gram.y, ie flex/bison code must not depend on any GUC variables;
* as such, changing their values can induce very unintuitive behavior.
* But we shall have to live with it until we can remove these variables.
* GUC 參數變量. 這直接違反了 gram.y 中提出的約定, 如 flex/bison 代碼不能依賴 GUC 變量;
* 因此, 改變他們的值會導致未知的后果.
* 但在去掉這些變量前, 不得不 活下去
*/
int backslash_quote = BACKSLASH_QUOTE_SAFE_ENCODING;
bool escape_string_warning = true;
bool standard_conforming_strings = true;
* Set the type of YYSTYPE.
* 設置 YYSTYPE.
* 在 Bison 中, 全局變量 yylval 的類型為 YYSTYPE, 默認為 int
* Internally, bison declares each value as a C union that includes all of the types.
* You list all of the types in %union declarations.
* Bison turns them into a typedef for a union type called YYSTYPE.
*/
#define YYSTYPE core_YYSTYPE
* Set the type of yyextra. All state variables used by the scanner should
* be in yyextra, *not* statically allocated.
* 設置 yyextra 的數據類型. 所有掃描器使用的狀態變量應在 yyextra 中, 不是靜態分配的.
*/
#define YY_EXTRA_TYPE core_yy_extra_type *
* Each call to yylex must set yylloc to the location of the found token
* (expressed as a byte offset from the start of the input text).
* When we parse a token that requires multiple lexer rules to process,
* this should be done in the first such rule, else yylloc will point
* into the middle of the token.
* 每一次調用 yylex 必須設置 yylloc 指向發現的 token 所在的位置.
* (從輸入文本開始計算的字節偏移量)
* 在分析一個需要多個詞法規則進行處理的 token 時,
* 在第一次應用規則時就應該完成這個動作, 否則的話 yylloc 會指向到 token 的中間位置.
*/
#define SET_YYLLOC() (*(yylloc) = yytext - yyextra- scanbuf)
* Advance yylloc by the given number of bytes.
* 通過給定的字節數調整 yylloc 的位置
*/
#define ADVANCE_YYLLOC(delta) ( *(yylloc) += (delta) )
#define startlit() ( yyextra- literallen = 0 )
static void addlit(char *ytext, int yleng, core_yyscan_t yyscanner);
static void addlitchar(unsigned char ychar, core_yyscan_t yyscanner);
static char *litbufdup(core_yyscan_t yyscanner);
static char *litbuf_udeescape(unsigned char escape, core_yyscan_t yyscanner);
static unsigned char unescape_single_char(unsigned char c, core_yyscan_t yyscanner);
static int process_integer_literal(const char *token, YYSTYPE *lval);
static bool is_utf16_surrogate_first(pg_wchar c);
static bool is_utf16_surrogate_second(pg_wchar c);
static pg_wchar surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second);
static void addunicode(pg_wchar c, yyscan_t yyscanner);
static bool check_uescapechar(unsigned char escape);
#define yyerror(msg) scanner_yyerror(msg, yyscanner)
#define lexer_errposition() scanner_errposition(*(yylloc), yyscanner)
static void check_string_escape_warning(unsigned char ychar, core_yyscan_t yyscanner);
static void check_escape_warning(core_yyscan_t yyscanner);
* Work around a bug in flex 2.5.35: it emits a couple of functions that
* it forgets to emit declarations for. Since we use -Wmissing-prototypes,
* this would cause warnings. Providing our own declarations should be
* harmless even when the bug gets fixed.
* Flex 2.5.35 存在一個 bug: 忽略了函數但沒有忽略函數聲明.
* 因為使用了 -Wmissing-prototypes 選項, 這會導致警告出現.
* 就算 bug 修復, 提供 PG 的聲明也可能會存在問題.
*/
extern int core_yyget_column(yyscan_t yyscanner);
extern void core_yyset_column(int column_no, yyscan_t yyscanner);
%}
到此,關于“PostgreSQL 中 Declarations 的作用是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!
正文完