PostgreSQL中Definitions作用是什么
这篇文章主要讲解了“PostgreSQL中Definitions作用是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PostgreSQL中Definitions作用是什么”吧!
Flex输入文件由四部分组成:
%{Declarations%}Definitions%%Rules%%UsersubroutinesDefinitions
在Declarations和Rules之间的部分是Definitions,这一部分可以定义进行正则表达式的”宏定义”,这些定义可在 规则(Rules)段被使用,如:
newline[\n\r]
这样在Rules中可以直接使用newline指代[\n\r]。
//各种选项设置%optionreentrant%optionbison-bridge%optionbison-locations%option8bit%optionnever-interactive%optionnodefault%optionnoinput%optionnounput%optionnoyywrap%optionnoyyalloc%optionnoyyrealloc%optionnoyyfree%optionwarn%optionprefix="core_yy"/**OK,hereisashortdescriptionoflex/flexrulesbehavior.*Thelongestpatternwhichmatchesaninputstringisalwayschosen.*Forequal-lengthpatterns,thefirstoccurringintheruleslistischosen.*INITIAListhestartingstate,towhichallnon-conditionalrulesapply.*Exclusivestateschangeparsingruleswhilethestateisactive.Whenin*anexclusivestate,onlythoserulesdefinedforthatstateapply.*下面是一些lex/flex规则动作的简单描述.*通常会选中可以最大匹配输入的字符串模式.*对于长度一致的模式,规则链表中的第一个规则会选中.*INITIAL是开始状态,适用于所有非条件规则.**Weuseexclusivestatesforquotedstrings,extendedcomments,*andtoeliminateparsingtroublesfornumericstrings.*Exclusivestates:*<xb>bitstringliteral*<xc>extendedC-stylecomments*<xd>delimitedidentifiers(double-quotedidentifiers)*<xh>hexadecimalnumericstring*<xq>standardquotedstrings*<xe>extendedquotedstrings(supportbackslashescapesequences)*<xdolq>$foo$quotedstrings*<xui>quotedidentifierwithUnicodeescapes*<xuiend>endofaquotedidentifierwithUnicodeescapes,UESCAPEcanfollow*<xus>quotedstringwithUnicodeescapes*<xusend>endofaquotedstringwithUnicodeescapes,UESCAPEcanfollow*<xeu>Unicodesurrogatepairinextendedquotedstring*对于引用字符串/扩展注释使用独占的状态,并消除数值字符串在解析中存在的麻烦.*独占的状态包括:*<xb>位字符串*<xc>扩展的C-style注释*<xd>分隔标识符(双引号标识符)*<xh>十六进制数字字符串*<xq>标准的带引号的字符串*<xe>扩展的带引号的字符串(支持反斜杠转义序列)*<xdolq>$foo$带引号的字符串*<xui>带Unicode转义的带引号的标识符*<xuiend>带Unicode转义的带引号的标识符的结尾,后跟UESCAPE*<xus>带Unicode转义的带引号的字符串*<xueend>带Unicode转义的带引号的字符串的结尾,后跟UESCAPE*<xeu>扩展带引号的字符串中的Unicode代理对**Remembertoaddan<<EOF>>casewheneveryouaddanewexclusivestate!*Thedefaultoneisprobablynottherightthing.*增加一个独占状态时,务请记住添加<<EOF>>.*默认情况下可能不是正确.*///INITIAL是开始状态,其他状态必须由%s或%x指定%xxb%xxc%xxd%xxh%xxe%xxq%xxdolq%xxui%xxuiend%xxus%xxusend%xxeu/**InordertomaketheworldsafeforWindowsandMacclientsaswellas*Unixones,weaccepteither\nor\rasanewline.ADOS-style\r\n*sequencewillbeseenastwosuccessivenewlines,butthatdoesn'tcause*anyproblems.Commentsthatstartwith--andextendtothenext*newlinearetreatedasequivalenttoasinglewhitespacecharacter.*对了适配Windows和Mac客户端,\n或者\r也视为新行.*DOS-style的\r\n序列被视为两个连续的新行,但这不会引起任何问题.*由--开始的注释,如果扩展到新行,视为单个空白字符**NOTEafinepoint:ifthereisnonewlinefollowing--,wewillabsorb*everythingtotheendoftheinputasacomment.Thisiscorrect.Older*versionsofPostgresfailedtorecognize--asacommentiftheinput*didnotendwithanewline.*注意:如果--后没有新行,将把输入末尾的所有内容作为注释.*PG的旧版本对这种情况无法识别,--如果没有以换行符结束,则作为注释**XXXperhaps\f(formfeed)shouldbetreatedasanewlineaswell?*XXX那么,\f也应该作为新行来处理**XXXifyouchangethesetofwhitespacecharacters,fixscanner_isspace()*toagree,andseealsotheplpgsqllexer.*XXX如果改变了空白字符集合,注意同步修改scanner_isspace()以适应修改后的情况,同时关注plpgsql的词法*///\t-->Tab键,\n-->换行,\t-->回车,\f-->换页space[\t\n\r\f]//tab键/换行/回车/换页horiz_space[\t\f]//空格/tab键/换页newline[\n\r]//换行/回车non_newline[^\n\r]//除了换行/回车外的其他字符//单行注释comment("--"{non_newline}*)//空白字符(1个或以上空格或者注释均视为whitespace)whitespace({space}+|{comment})/**SQLrequiresatleastonenewlineinthewhitespaceseparating*stringliteralsthataretobeconcatenated.Silly,butwhoarewe*toargue?Notethat{whitespace_with_newline}shouldnothave*after*it,whereas{whitespace}shouldgenerallyhavea*afterit...*SQL语句要求在分隔字符串字面值的空格中至少有一行换行符,*这些字符串字面值将被连接起来.*很傻:(但这又有什么好争论的呢?*注意{whitespace_with_newline}不应该在定义的后面存在*号,*这里{whitespace}通常至少在其后面跟一个**///特殊空白,1个+以上空格或注释后跟新行special_whitespace({space}+|{comment}{newline})//水平空白(一堆的空格或者注释)horiz_whitespace({horiz_space}|{comment})//0个或多个horiz_whitespace+新行+0个或多个特殊空白whitespace_with_newline({horiz_whitespace}*{newline}{special_whitespace}*)/**Toensurethat{quotecontinue}canbescannedwithouthavingtobackup*ifthefullpatternisn'tmatched,weincludetrailingwhitespacein*{quotestop}.Thismatchesallcaseswhere{quotecontinue}failstomatch,*exceptfor{quote}followedbywhitespaceandjustone"-"(nottwo,*whichwouldstarta{comment}).Tocoverthatwehave{quotefail}.*Theactionsfor{quotestop}and{quotefail}mustthrowbackcharacters*beyondthequoteproper.*如果全模式没有匹配,为了确保{quotecontinue}不需要备份就可以扫描,*我们在{quotestop}中包含了尾部空格.*这可以匹配{quotecontinue}无法匹配的所有情况,除了{quote}后跟空格而且只有一个'-'字符的情况*(注意,不是两个'-'字符,这被视为{comment}的开始)*为了覆盖含有{quotefail}的情况,{quotestop}和{quotefail}的动作必须返回超出引号的字符*/quote'quotestop{quote}{whitespace}*quotecontinue{quote}{whitespace_with_newline}{quote}quotefail{quote}{whitespace}*"-"//<xb>/*Bitstring*Itistemptingtoscanthestringforonlythosecharacters*whichareallowed.However,thisleadstosilentlyswallowed*charactersifillegalcharactersareincludedinthestring.*Forexample,ifxbinsideis[01]thenB'ABCD'isinterpreted*asazero-lengthstring,andtheABCD'islost!*Bettertopassthestringforwardandlettheinputroutines*validatethecontents.*位字符串*倾向于只扫描字符串中允许的字符.*但是这会导致如果非法字符包含在字符串中时默默的接受这些非法字符.*比如,如果xbinside是[01],则B'ABCD'被视为0长度的字符串,并且丢失了ABCD'*/xbstart[bB]{quote}//开始:b或B字符开头,后跟单引号'字符xbinside[^']*//字符串内容:除单引号外的其他字符,0个或多个/*Hexadecimalnumber*///<xh>十六进制数字xhstart[xX]{quote}//开始:以x或X打头,后跟单引号xhinside[^']*//内容:除单引号外的其他字符,0个或多个/*Nationalcharacter*///<xn>国家字符(Unicode)xnstart[nN]{quote}//开始:以n或N打头/*Quotedstringthatallowsbackslashescapes*///<xe>允许反斜杠转义字符的带引号的字符串xestart[eE]{quote}//开始:e或E打头,后跟单引号xeinside[^\\']+//内容:除反斜杠和单引号外的其他字符xeescape[\\][^0-7]//转义字符:以反斜杠打头后跟除0-7之外的其他字符xeoctesc[\\][0-7]{1,3}//八进制转义字符:以反斜杠打头后跟0-7,出现1次-3次xehexesc[\\]x[0-9A-Fa-f]{1,2}//十六进制转义字符:以反斜杠打头后跟0-F/f,出现1次-2次//Unicode字符:以反斜杠打头,后跟u和0-F/f(连续出现4次)或者是后跟U,0-F/f连续出现8次xeunicode[\\](u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})//不符合xeunicode的其他情况xeunicodefail[\\](u[0-9A-Fa-f]{0,3}|U[0-9A-Fa-f]{0,7})/*Extendedquote*扩展引号*xqdoubleimplementsembeddedquote,''''*xqdouble实现了内嵌引号,''''*/xqstart{quote}xqdouble{quote}{quote}xqinside[^']+/*$foo$stylequotes("dollarquoting")*Thequotedstringstartswith$foo$where"foo"isanoptionalstring*intheformofanidentifier,exceptthatitmaynotcontain"$",*andextendstothefirstoccurrenceofanidenticalstring.*Thereis*no*processingofthequotedtext.*$foo$类型的引号("美元引号")*带引号的字符串以$foo$开始,这里foo是一个可选的字符串,*但它不包含字符$,并且扩展到相同字符串的第一次出现.*扩展到标识符第一次出现的地方.*对于引用文本,不需要进行处理**{dolqfailed}isanerrorruletoavoidscannerbackupwhen{dolqdelim}*failstomatchitstrailing"$".*{dolqfailed}是一种错误规则,用以避免扫描器在{dolqdelim}不能匹配末尾的$时进行备份*///<xdolq>dolq_start[A-Za-z\200-\377_]//开始:大小写英文字母/80-FF字符(8进制是200-377)/下划线dolq_cont[A-Za-z\200-\377_0-9]//dolq_start+数字dolqdelim\$({dolq_start}{dolq_cont}*)?\$//分隔符$xx$,xx可选dolqfailed\${dolq_start}{dolq_cont}*//失败:以$开始,但没有$结束dolqinside[^$]+//内容:除$外的其他字符/*Doublequote*Allowsembeddedspacesandotherspecialcharactersintoidentifiers.*双引号*允许嵌入空格和其他特殊字符*///<xd>dquote\"//双引号xdstart{dquote}//开始:以双引号打头xdstop{dquote}//结束:以双引号结束xddouble{dquote}{dquote}//两个双引号xdinside[^"]+//内容:除双引号外的其他字符,1个或多个/*Unicodeescapes*///<xue>//转义字符:uescape[uU][eE][sS][cC][aA][pP][eE]{whitespace}*{quote}[^']{quote}/*errorruletoavoidbackup*///错误规则:避免备份uescapefail[uU][eE][sS][cC][aA][pP][eE]{whitespace}*"-"|[uU][eE][sS][cC][aA][pP][eE]{whitespace}*{quote}[^']|[uU][eE][sS][cC][aA][pP][eE]{whitespace}*{quote}|[uU][eE][sS][cC][aA][pP][eE]{whitespace}*|[uU][eE][sS][cC][aA][pP]|[uU][eE][sS][cC][aA]|[uU][eE][sS][cC]|[uU][eE][sS]|[uU][eE]|[uU]/*QuotedidentifierwithUnicodeescapes*///使用Unicode转义字符的引用标识符(双引号)//<xui>xuistart[uU]&{dquote}//开头:以u/U打头,后跟双引号/*QuotedstringwithUnicodeescapes*///使用Unicode转义字符的字符串(单引号)//<xus>xusstart[uU]&{quote}/*OptionalUESCAPEafteraquotedstringoridentifierwithUnicodeescapes.*///引用字符串或者标识符后可选的UESCAPE//<>xustop1{uescapefail}?xustop2{uescape}/*errorruletoavoidbackup*///错误规则:避免备份xufailed[uU]&/*C-stylecomments*C风格注释**The"extendedcomment"syntaxcloselyresemblesallowableoperatorsyntax.*Thetrickyparthereistogetlextorecognizeastringstartingwith*slash-starasacomment,wheninterpretingitasanoperatorwouldproduce*alongermatch---rememberlexwillpreferalongermatch!Also,ifwe*havesomethinglikeplus-slash-star,lexwillthinkthisisa3-character*operatorwhereaswewanttoseeitasa+operatorandacommentstart.*Thesolutionistwo-fold:*1.append{op_chars}*toxcstartsothatitmatchesasmuchtextas*{operator}would.Thenthetie-breaker(firstmatchingruleofsame*length)ensuresxcstartwins.Weputbacktheextrastuffwithyyless()*incaseitcontainsastar-slashthatshouldterminatethecomment.*2.Intheoperatorrule,checkforslash-starwithintheoperator,and*iffoundthrowitbackwithyyless().Thishandlestheplus-slash-star*problem.*Dash-dashcommentshavesimilarinteractionswiththeoperatorrule.*"扩展注释"语法与允许的操作符语法非常相似.*这里比较棘手的部分是让词法分析器可以识别以斜杠加星号开头的字符串作为注释,*因为在认为星号为操作符时可能会产生更长的匹配--记住:词法分析器倾向于更长的匹配.*同时,如果存在形如+/*这样的字符串,词法分析器会认为这是3元操作符,*但其实我们希望把它视作一个加号操作符和注释的开始.*解决方案如下:*1.追加{op_chars}*到xcstart中,以便它可以匹配尽可能多的文本(与{operator}一样).*然后,tie-breaker(相同长度首次匹配原则)确保xcstart会首先匹配.*我们用yyless()放进去了一些额外的东西,以防它包含一个星号和斜杠(即:*/),这会终止注释*2.在操作符规则中,检查操作符中的反斜杠+星号,如发现则返回给yyless().这可以处理+/*这个问题*"--"注释与操作符规则有类型的交互方式.*/xcstart\/\*{op_chars}*//开始:/*+操作符(0个或多个)xcstop\*+\///结束:1个或多个*号+字符/xcinside[^*/]+//内容:除了*和/外的其他字符,1个或多个digit[0-9]//数字:0-9ident_start[A-Za-z\200-\377_]//标识符开始:英文字母/80-FF字符/下划线ident_cont[A-Za-z\200-\377_0-9\$]//标识符:ident_start外加数字identifier{ident_start}{ident_cont}*//标识符/*Assortedspecial-caseoperatorsandoperator-liketokens*///组合的特殊情况操作符和类似操作符的tokenstypecast"::"//强制类型转换操作符dot_dot\.\.//点点操作符colon_equals":="//赋值操作符/**Theseoperator-liketokens(unliketheaboveones)alsomatchthe{operator}*rule,whichmeansthattheymightbeoverriddenbyalongermatchifthey*arefollowedbyacommentstartora+or-character.Accordingly,ifyou*addtothislist,youmustalsoaddcorrespondingcodetothe{operator}*blocktoreturnthecorrecttokeninsuchcases.(Thisisnotneededin*psqlscan.lsincethetokenvalueisignoredthere.)*这些类操作符tokens(不同于上面所列)同时会匹配{operator}规则,*这意味着如果后跟注释起始符或者+-字符的话,它们可能会被长匹配覆盖.*因此,如果加入到链表中,必须同时相应的代码到{operator}块中以便返回正确的token.*(在psqlscan.l中不需要这样做,因为token值会被忽略)*/equals_greater"=>"//等于大于less_equals"<="//小于等于greater_equals">="//大于等于less_greater"<>"//小于/大于not_equals"!="//不等于/**"self"isthesetofcharsthatshouldbereturnedassingle-character*tokens."op_chars"isthesetofcharsthatcanmakeup"Op"tokens,*whichcanbeoneormorecharacterslong(butifasingle-chartoken*appearsinthe"self"set,itisnottobereturnedasanOp).Note*thatthesetsoverlap,buteachhassomecharsthatarenotintheother.**Ifyouchangeeitherset,adjustthecharacterlistsappearinginthe*rulefor"operator"!*"self"是那些作为单字符tokens返回的字符集合.*"op_chars"是组成"Op"tokens(一个或多个字符)的字符集合*(如果单个字符token出现在"self"中,则不会作为Op返回).*注意这些集合是重复的,但是每个集合都有一些不在另外一个集合中的字符.*如果改变了其中一个集合,调整出现在"operator"所设定的规则中字符列表.*/self[,()\[\].;\:\+\-\*\/\%\^\<\>\=]op_chars[\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]operator{op_chars}+/*wenolongerallowunaryminusinnumbers.*insteadwepassitseparatelytoparser.thereitgets*coercedviadoNegate()--Leonaug201999*我们不再允许一进制负数,这些值会单独传递给解析器.*在那里,会通过doNegate()方法处理.**{decimalfail}isusedbecausewewouldlike"1..10"tolexas1,dot_dot,10.*{decimalfail}在处理形如1..10的情况**{realfail1}and{realfail2}areaddedtopreventtheneedforscanner*backupwhenthe{real}rulefailstomatchcompletely.*添加{realfail1}和{realfail2}的目的是防止在{real}规则匹配失败时的扫描器备份*/integer{digit}+//整数decimal(({digit}*\.{digit}+)|({digit}+\.{digit}*))//小数decimalfail{digit}+\.\.//匹配失败的小数real({integer}|{decimal})[Ee][-+]?{digit}+//实数realfail1({integer}|{decimal})[Ee]//匹配失败1realfail2({integer}|{decimal})[Ee][-+]//匹配失败2param\${integer}//参数other.//其他/**Dollarquotedstringsaretotallyopaque,andnoescapingisdoneonthem.*Otherquotedstringsmustallowsomespecialcharacterssuchassingle-quote*andnewline.*Embeddedsingle-quotesareimplementedbothintheSQLstandard*styleoftwoadjacentsinglequotes"''"andinthePostgres/Javastyle*ofescaped-quote"\'".*Otherembeddedescapedcharactersarematchedexplicitlyandtheleading*backslashisdroppedfromthestring.*Notethatxcstartmustappearbeforeoperator,asexplainedabove!*Alsowhitespace(comment)mustappearbeforeoperator.*使用$符号括起来的字符串是完全密封的,在其上无任何的转义可做.*其他引用字符串必须运行一些特殊字符比如单引号或者新行.*嵌入式的单引号在标准SQL风格中通过两个相邻的单引号"''"实现,*在Postgres/Java风格中使用转义字符"\'"实现.*其他嵌入式的转义字符显式匹配,打头的反斜杠会从字符串中去掉.*如前所解释过的,务必注意xcstart必须在操作符前出现.*同时空白字符(注释)必须在操作符前出现.*/
感谢各位的阅读,以上就是“PostgreSQL中Definitions作用是什么”的内容了,经过本文的学习后,相信大家对PostgreSQL中Definitions作用是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。