📄 pgc.l
字号:
%{/*------------------------------------------------------------------------- * * pgc.l * lexical scanner for ecpg * * This is a modified version of src/backend/parser/scan.l * * * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.159.2.3 2008/02/17 18:42:23 meskes Exp $ * *------------------------------------------------------------------------- */#include "postgres_fe.h"#include <ctype.h>#include <sys/types.h>#include <limits.h>#include "extern.h"extern YYSTYPE yylval;static int xcdepth = 0; /* depth of nesting in slash-star comments */static char *dolqstart; /* current $foo$ quote start string */static YY_BUFFER_STATE scanbufhandle;static char *scanbuf;/* * literalbuf is used to accumulate literal values when multiple rules * are needed to parse a single literal. Call startlit to reset buffer * to empty, addlit to add text. Note that the buffer is permanently * malloc'd to the largest size needed so far in the current run. */static char *literalbuf = NULL; /* expandable buffer */static int literallen; /* actual current length */static int literalalloc; /* current allocated buffer size */#define startlit() (literalbuf[0] = '\0', literallen = 0)static void addlit(char *ytext, int yleng);static void addlitchar (unsigned char);static void parse_include (void);static bool ecpg_isspace(char ch);static bool isdefine(void);static bool isinformixdefine(void);char *token_start;int state_before;struct _yy_buffer { YY_BUFFER_STATE buffer; long lineno; char *filename; struct _yy_buffer *next;} *yy_buffer = NULL;static char *old;#define MAX_NESTED_IF 128static short preproc_tos;static short ifcond;static struct _if_value { short condition; short else_branch;} stacked_if_value[MAX_NESTED_IF];%}%option 8bit%option never-interactive%option nodefault%option noyywrap%option yylineno%x C SQL incl def def_ident undef /* * OK, here is a short description of lex/flex rules behavior. * The longest pattern which matches an input string is always chosen. * For equal-length patterns, the first occurring in the rules list is chosen. * INITIAL is the starting state, to which all non-conditional rules apply. * Exclusive states change parsing rules while the state is active. When in * an exclusive state, only those rules defined for that state apply. * * We use exclusive states for quoted strings, extended comments, * and to eliminate parsing troubles for numeric strings. * Exclusive states: * <xb> bit string literal * <xc> extended C-style comments - thomas 1997-07-12 * <xd> delimited identifiers (double-quoted identifiers) - thomas 1997-10-27 * <xh> hexadecimal numeric string - thomas 1997-11-16 * <xq> standard quoted strings - thomas 1997-07-30 * <xqc> standard quoted strings in C - michael * <xe> extended quoted strings (support backslash escape sequences) * <xn> national character quoted strings * <xdolq> $foo$ quoted strings */%x xb%x xc%x xd%x xdc%x xh%x xe%x xn%x xq%x xqc%x xdolq%x xcond%x xskip/* Bit string */xbstart [bB]{quote}xbinside [^']*/* Hexadecimal number */xhstart [xX]{quote}xhinside [^']*/* National character */xnstart [nN]{quote}/* Quoted string that allows backslash escapes */xestart [eE]{quote}xeinside [^\\']+xeescape [\\][^0-7]xeoctesc [\\][0-7]{1,3}xehexesc [\\]x[0-9A-Fa-f]{1,2}/* C version of hex number */xch 0[xX][0-9A-Fa-f]*/* Extended quote * xqdouble implements embedded quote, '''' */xqstart {quote}xqdouble {quote}{quote}xqcquote [\\]{quote}xqinside [^']+/* $foo$ style quotes ("dollar quoting") * The quoted string starts with $foo$ where "foo" is an optional string * in the form of an identifier, except that it may not contain "$", * and extends to the first occurrence of an identical string. * There is *no* processing of the quoted text. * * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim} * fails to match its trailing "$". */dolq_start [A-Za-z\200-\377_]dolq_cont [A-Za-z\200-\377_0-9]dolqdelim \$({dolq_start}{dolq_cont}*)?\$dolqfailed \${dolq_start}{dolq_cont}*dolqinside [^$]+/* Double quote * Allows embedded spaces and other special characters into identifiers. */dquote \"xdstart {dquote}xdstop {dquote}xddouble {dquote}{dquote}xdinside [^"]+/* special stuff for C strings */xdcqq \\\\xdcqdq \\\"xdcother [^"]xdcinside ({xdcqq}|{xdcqdq}|{xdcother})/* C-style comments * * The "extended comment" syntax closely resembles allowable operator syntax. * The tricky part here is to get lex to recognize a string starting with * slash-star as a comment, when interpreting it as an operator would produce * a longer match --- remember lex will prefer a longer match! Also, if we * have something like plus-slash-star, lex will think this is a 3-character * operator whereas we want to see it as a + operator and a comment start. * The solution is two-fold: * 1. append {op_chars}* to xcstart so that it matches as much text as * {operator} would. Then the tie-breaker (first matching rule of same * length) ensures xcstart wins. We put back the extra stuff with yyless() * in case it contains a star-slash that should terminate the comment. * 2. In the operator rule, check for slash-star within the operator, and * if found throw it back with yyless(). This handles the plus-slash-star * problem. * Dash-dash comments have similar interactions with the operator rule. */xcstart \/\*{op_chars}*xcstop \*+\/xcinside [^*/]+digit [0-9]ident_start [A-Za-z\200-\377_]ident_cont [A-Za-z\200-\377_0-9\$]identifier {ident_start}{ident_cont}*array ({ident_cont}|{whitespace}|[\[\]\+\-\*\%\/\(\)\>\.])*typecast "::"/* * "self" is the set of chars that should be returned as single-character * tokens. "op_chars" is the set of chars that can make up "Op" tokens, * which can be one or more characters long (but if a single-char token * appears in the "self" set, it is not to be returned as an Op). Note * that the sets overlap, but each has some chars that are not in the other. * * If you change either set, adjust the character lists appearing in the * rule for "operator"! */self [,()\[\].;\:\+\-\*\/\%\^\<\>\=]op_chars [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]operator {op_chars}+/* we no longer allow unary minus in numbers. * instead we pass it separately to parser. there it gets * coerced via doNegate() -- Leon aug 20 1999 * * {realfail1} and {realfail2} are added to prevent the need for scanner * backup when the {real} rule fails to match completely. */integer {digit}+decimal (({digit}*\.{digit}+)|({digit}+\.{digit}*))real ({integer}|{decimal})[Ee][-+]?{digit}+realfail1 ({integer}|{decimal})[Ee]realfail2 ({integer}|{decimal})[Ee][-+]param \${integer}/* * In order to make the world safe for Windows and Mac clients as well as * Unix ones, we accept either \n or \r as a newline. A DOS-style \r\n * sequence will be seen as two successive newlines, but that doesn't cause * any problems. SQL92-style comments, which start with -- and extend to the * next newline, are treated as equivalent to a single whitespace character. * * NOTE a fine point: if there is no newline following --, we will absorb * everything to the end of the input as a comment. This is correct. Older * versions of Postgres failed to recognize -- as a comment if the input * did not end with a newline. * * XXX perhaps \f (formfeed) should be treated as a newline as well? * * XXX if you change the set of whitespace characters, fix ecpg_isspace() * to agree. */ccomment "//".*\nspace [ \t\n\r\f]horiz_space [ \t\f]newline [\n\r]non_newline [^\n\r]comment ("--"{non_newline}*)whitespace ({space}+|{comment})/* * SQL92 requires at least one newline in the whitespace separating * string literals that are to be concatenated. Silly, but who are we * to argue? Note that {whitespace_with_newline} should not have * after * it, whereas {whitespace} should generally have a * after it... */horiz_whitespace ({horiz_space}|{comment})whitespace_with_newline ({horiz_whitespace}*{newline}{whitespace}*)quote 'quotestop {quote}{whitespace}*quotecontinue {quote}{whitespace_with_newline}{quote}quotefail {quote}{whitespace}*"-"/* special characters for other dbms *//* we have to react differently in compat mode */informix_special [\$]other ./* some stuff needed for ecpg */exec [eE][xX][eE][cC]sql [sS][qQ][lL]define [dD][eE][fF][iI][nN][eE]include [iI][nN][cC][lL][uU][dD][eE]undef [uU][nN][dD][eE][fF]ifdef [iI][fF][dD][eE][fF]ifndef [iI][fF][nN][dD][eE][fF]else [eE][lL][sS][eE]elif [eE][lL][iI][fF]endif [eE][nN][dD][iI][fF]struct [sS][tT][rR][uU][cC][tT]exec_sql {exec}{space}*{sql}{space}*ipdigit ({digit}|{digit}{digit}|{digit}{digit}{digit})ip {ipdigit}\.{ipdigit}\.{ipdigit}\.{ipdigit}/* we might want to parse all cpp include files */cppinclude {space}*#{include}{space}*/* Take care of cpp lines, they may also be continuated */cppline {space}*#(.*\\{space})*.*{newline}/* * Dollar quoted strings are totally opaque, and no escaping is done on them. * Other quoted strings must allow some special characters such as single-quote * and newline. * Embedded single-quotes are implemented both in the SQL standard * style of two adjacent single quotes "''" and in the Postgres/Java style * of escaped-quote "\'". * Other embedded escaped characters are matched explicitly and the leading * backslash is dropped from the string. - thomas 1997-09-24 * Note that xcstart must appear before operator, as explained above! * Also whitespace (comment) must appear before operator. */%%%{ /* code to execute during start of each call of yylex() */ token_start = NULL;%}<SQL>{whitespace} { /* ignore */ }<C,SQL>{xcstart} { token_start = yytext; state_before = YYSTATE; xcdepth = 0; BEGIN(xc); /* Put back any characters past slash-star; see above */ yyless(2); fputs("/*", yyout); }<xc>{xcstart} { xcdepth++; /* Put back any characters past slash-star; see above */ yyless(2); fputs("/*", yyout); }<xc>{xcstop} { ECHO; if (xcdepth <= 0) { BEGIN(state_before); token_start = NULL; } else xcdepth--; }<xc>{xcinside} { ECHO; }<xc>{op_chars} { ECHO; }<xc>\*+ { ECHO; }<xc><<EOF>> { mmerror(PARSE_ERROR, ET_FATAL, "Unterminated /* comment"); }<SQL>{xbstart} { token_start = yytext; BEGIN(xb); startlit(); addlitchar('b'); }<xb>{quotestop} |<xb>{quotefail} { yyless(1); BEGIN(SQL); if (literalbuf[strspn(literalbuf, "01") + 1] != '\0') mmerror(PARSE_ERROR, ET_ERROR, "invalid bit string input."); yylval.str = mm_strdup(literalbuf); return BCONST; }<xh>{xhinside} |<xb>{xbinside} { addlit(yytext, yyleng); }<xh>{quotecontinue} |<xb>{quotecontinue} { /* ignore */ }<xb><<EOF>> { mmerror(PARSE_ERROR, ET_FATAL, "Unterminated bit string"); }<SQL>{xhstart} { token_start = yytext; BEGIN(xh); startlit(); addlitchar('x'); }<xh>{quotestop} |<xh>{quotefail} { yyless(1); BEGIN(SQL); yylval.str = mm_strdup(literalbuf); return XCONST; }<xh><<EOF>> { mmerror(PARSE_ERROR, ET_FATAL, "Unterminated hexadecimal integer"); }<SQL>{xnstart} { /* National character. * Transfer it as-is to the backend. */ token_start = yytext; state_before = YYSTATE; BEGIN(xn); startlit(); }<C>{xqstart} { token_start = yytext; state_before = YYSTATE; BEGIN(xqc); startlit(); }<SQL>{xqstart} { token_start = yytext; state_before = YYSTATE; BEGIN(xq); startlit(); }<SQL>{xestart} { token_start = yytext; state_before = YYSTATE; BEGIN(xe); startlit(); }<xq,xqc>{quotestop} |<xq,xqc>{quotefail} { yyless(1); BEGIN(state_before); yylval.str = mm_strdup(literalbuf); return SCONST; }<xe>{quotestop} |<xe>{quotefail} { yyless(1); BEGIN(state_before); yylval.str = mm_strdup(literalbuf); return ECONST; }<xn>{quotestop} |<xn>{quotefail} { yyless(1); BEGIN(state_before); yylval.str = mm_strdup(literalbuf); return NCONST; }<xq,xe,xn>{xqdouble} { addlitchar('\''); }<xqc>{xqcquote} { addlitchar('\\'); addlitchar('\''); }<xq,xqc,xn>{xqinside} { addlit(yytext, yyleng); }<xe>{xeinside} { addlit(yytext, yyleng); }<xe>{xeescape} { addlit(yytext, yyleng); }<xe>{xeoctesc} { addlit(yytext, yyleng); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -