mirror of https://github.com/postgres/postgres
directly. This was a lot of trouble, but should be worth it in terms of not having to keep the plpgsql lexer in step with core anymore. In addition the handling of keywords is significantly better-structured, allowing us to de-reserve a number of words that plpgsql formerly treated as reserved.REL8_5_ALPHA3_BRANCH
parent
60cd1f1829
commit
2dee828cac
@ -0,0 +1,584 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* pl_scanner.c |
||||
* lexical scanning for PL/pgSQL |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_scanner.c,v 1.1 2009/11/12 00:13:00 tgl Exp $ |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "plpgsql.h" |
||||
|
||||
#include "mb/pg_wchar.h" |
||||
#include "parser/scanner.h" |
||||
|
||||
#include "pl_gram.h" /* must be after parser/scanner.h */ |
||||
|
||||
#define PG_KEYWORD(a,b,c) {a,b,c}, |
||||
|
||||
|
||||
/* Klugy flag to tell scanner whether to lookup identifiers */ |
||||
bool plpgsql_LookupIdentifiers = true; |
||||
|
||||
/*
|
||||
* A word about keywords: |
||||
* |
||||
* We keep reserved and unreserved keywords in separate arrays. The |
||||
* reserved keywords are passed to the core scanner, so they will be |
||||
* recognized before (and instead of) any variable name. Unreserved |
||||
* words are checked for separately, after determining that the identifier |
||||
* isn't a known variable name. If plpgsql_LookupIdentifiers is off then |
||||
* no variable names will be recognized, so the unreserved words always work. |
||||
* (Note in particular that this helps us avoid reserving keywords that are |
||||
* only needed in DECLARE sections, since we scan those sections with |
||||
* plpgsql_LookupIdentifiers off.) |
||||
* |
||||
* In certain contexts it is desirable to prefer recognizing an unreserved |
||||
* keyword over recognizing a variable name. Those cases are handled in |
||||
* gram.y using tok_is_keyword(). |
||||
* |
||||
* For the most part, the reserved keywords are those that start a PL/pgSQL |
||||
* statement (and so would conflict with an assignment to a variable of the |
||||
* same name). We also don't sweat it much about reserving keywords that |
||||
* are reserved in the core grammar. Try to avoid reserving other words. |
||||
*/ |
||||
|
||||
/*
|
||||
* Lists of keyword (name, token-value, category) entries. |
||||
* |
||||
* !!WARNING!!: These lists must be sorted by ASCII name, because binary |
||||
* search is used to locate entries. |
||||
* |
||||
* Be careful not to put the same word in both lists. Also be sure that |
||||
* gram.y's unreserved_keyword production agrees with the second list. |
||||
*/ |
||||
|
||||
static const ScanKeyword reserved_keywords[] = { |
||||
PG_KEYWORD("all", K_ALL, RESERVED_KEYWORD) |
||||
PG_KEYWORD("begin", K_BEGIN, RESERVED_KEYWORD) |
||||
PG_KEYWORD("by", K_BY, RESERVED_KEYWORD) |
||||
PG_KEYWORD("case", K_CASE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("close", K_CLOSE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("continue", K_CONTINUE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("declare", K_DECLARE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("default", K_DEFAULT, RESERVED_KEYWORD) |
||||
PG_KEYWORD("diagnostics", K_DIAGNOSTICS, RESERVED_KEYWORD) |
||||
PG_KEYWORD("else", K_ELSE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("elseif", K_ELSIF, RESERVED_KEYWORD) |
||||
PG_KEYWORD("elsif", K_ELSIF, RESERVED_KEYWORD) |
||||
PG_KEYWORD("end", K_END, RESERVED_KEYWORD) |
||||
PG_KEYWORD("exception", K_EXCEPTION, RESERVED_KEYWORD) |
||||
PG_KEYWORD("execute", K_EXECUTE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("exit", K_EXIT, RESERVED_KEYWORD) |
||||
PG_KEYWORD("fetch", K_FETCH, RESERVED_KEYWORD) |
||||
PG_KEYWORD("for", K_FOR, RESERVED_KEYWORD) |
||||
PG_KEYWORD("from", K_FROM, RESERVED_KEYWORD) |
||||
PG_KEYWORD("get", K_GET, RESERVED_KEYWORD) |
||||
PG_KEYWORD("if", K_IF, RESERVED_KEYWORD) |
||||
PG_KEYWORD("in", K_IN, RESERVED_KEYWORD) |
||||
PG_KEYWORD("insert", K_INSERT, RESERVED_KEYWORD) |
||||
PG_KEYWORD("into", K_INTO, RESERVED_KEYWORD) |
||||
PG_KEYWORD("loop", K_LOOP, RESERVED_KEYWORD) |
||||
PG_KEYWORD("move", K_MOVE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("not", K_NOT, RESERVED_KEYWORD) |
||||
PG_KEYWORD("null", K_NULL, RESERVED_KEYWORD) |
||||
PG_KEYWORD("open", K_OPEN, RESERVED_KEYWORD) |
||||
PG_KEYWORD("or", K_OR, RESERVED_KEYWORD) |
||||
PG_KEYWORD("perform", K_PERFORM, RESERVED_KEYWORD) |
||||
PG_KEYWORD("raise", K_RAISE, RESERVED_KEYWORD) |
||||
PG_KEYWORD("return", K_RETURN, RESERVED_KEYWORD) |
||||
PG_KEYWORD("strict", K_STRICT, RESERVED_KEYWORD) |
||||
PG_KEYWORD("then", K_THEN, RESERVED_KEYWORD) |
||||
PG_KEYWORD("to", K_TO, RESERVED_KEYWORD) |
||||
PG_KEYWORD("using", K_USING, RESERVED_KEYWORD) |
||||
PG_KEYWORD("when", K_WHEN, RESERVED_KEYWORD) |
||||
PG_KEYWORD("while", K_WHILE, RESERVED_KEYWORD) |
||||
}; |
||||
|
||||
static const int num_reserved_keywords = lengthof(reserved_keywords); |
||||
|
||||
static const ScanKeyword unreserved_keywords[] = { |
||||
PG_KEYWORD("absolute", K_ABSOLUTE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("alias", K_ALIAS, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("backward", K_BACKWARD, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("constant", K_CONSTANT, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("cursor", K_CURSOR, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("debug", K_DEBUG, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("detail", K_DETAIL, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("dump", K_DUMP, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("errcode", K_ERRCODE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("first", K_FIRST, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("forward", K_FORWARD, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("hint", K_HINT, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("info", K_INFO, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("is", K_IS, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("last", K_LAST, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("log", K_LOG, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("message", K_MESSAGE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("next", K_NEXT, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("no", K_NO, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("notice", K_NOTICE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("option", K_OPTION, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("prior", K_PRIOR, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("query", K_QUERY, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("relative", K_RELATIVE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("result_oid", K_RESULT_OID, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("reverse", K_REVERSE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("row_count", K_ROW_COUNT, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("rowtype", K_ROWTYPE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("scroll", K_SCROLL, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("sqlstate", K_SQLSTATE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("type", K_TYPE, UNRESERVED_KEYWORD) |
||||
PG_KEYWORD("warning", K_WARNING, UNRESERVED_KEYWORD) |
||||
}; |
||||
|
||||
static const int num_unreserved_keywords = lengthof(unreserved_keywords); |
||||
|
||||
|
||||
/* Auxiliary data about a token (other than the token type) */ |
||||
typedef struct |
||||
{ |
||||
YYSTYPE lval; /* semantic information */ |
||||
YYLTYPE lloc; /* offset in scanbuf */ |
||||
int leng; /* length in bytes */ |
||||
} TokenAuxData; |
||||
|
||||
/*
|
||||
* Scanner working state. At some point we might wish to fold all this |
||||
* into a YY_EXTRA struct. For the moment, there is no need for plpgsql's |
||||
* lexer to be re-entrant, and the notational burden of passing a yyscanner |
||||
* pointer around is great enough to not want to do it without need. |
||||
*/ |
||||
|
||||
/* The stuff the core lexer needs */ |
||||
static core_yyscan_t yyscanner = NULL; |
||||
static core_yy_extra_type core_yy; |
||||
|
||||
/* The original input string */ |
||||
static const char *scanorig; |
||||
|
||||
/* Current token's length (corresponds to plpgsql_yylval and plpgsql_yylloc) */ |
||||
static int plpgsql_yyleng; |
||||
|
||||
/* Token pushback stack */ |
||||
#define MAX_PUSHBACKS 4 |
||||
|
||||
static int num_pushbacks; |
||||
static int pushback_token[MAX_PUSHBACKS]; |
||||
static TokenAuxData pushback_auxdata[MAX_PUSHBACKS]; |
||||
|
||||
/* State for plpgsql_location_to_lineno() */ |
||||
static const char *cur_line_start; |
||||
static const char *cur_line_end; |
||||
static int cur_line_num; |
||||
|
||||
/* Internal functions */ |
||||
static int internal_yylex(TokenAuxData *auxdata); |
||||
static void push_back_token(int token, TokenAuxData *auxdata); |
||||
static void location_lineno_init(void); |
||||
|
||||
|
||||
/*
|
||||
* This is the yylex routine called from the PL/pgSQL grammar. |
||||
* It is a wrapper around the core lexer, with the ability to recognize |
||||
* PL/pgSQL variables and return them as special T_DATUM tokens. If a |
||||
* word or compound word does not match any variable name, or if matching |
||||
* is turned off by plpgsql_LookupIdentifiers, it is returned as |
||||
* T_WORD or T_CWORD respectively, or as an unreserved keyword if it |
||||
* matches one of those. |
||||
*/ |
||||
int |
||||
plpgsql_yylex(void) |
||||
{ |
||||
int tok1; |
||||
TokenAuxData aux1; |
||||
const ScanKeyword *kw; |
||||
|
||||
tok1 = internal_yylex(&aux1); |
||||
if (tok1 == IDENT || tok1 == PARAM) |
||||
{ |
||||
int tok2; |
||||
TokenAuxData aux2; |
||||
|
||||
tok2 = internal_yylex(&aux2); |
||||
if (tok2 == '.') |
||||
{ |
||||
int tok3; |
||||
TokenAuxData aux3; |
||||
|
||||
tok3 = internal_yylex(&aux3); |
||||
if (tok3 == IDENT) |
||||
{ |
||||
int tok4; |
||||
TokenAuxData aux4; |
||||
|
||||
tok4 = internal_yylex(&aux4); |
||||
if (tok4 == '.') |
||||
{ |
||||
int tok5; |
||||
TokenAuxData aux5; |
||||
|
||||
tok5 = internal_yylex(&aux5); |
||||
if (tok5 == IDENT) |
||||
{ |
||||
if (plpgsql_parse_tripword(aux1.lval.str, |
||||
aux3.lval.str, |
||||
aux5.lval.str, |
||||
&aux1.lval.wdatum, |
||||
&aux1.lval.cword)) |
||||
tok1 = T_DATUM; |
||||
else |
||||
tok1 = T_CWORD; |
||||
} |
||||
else |
||||
{ |
||||
/* not A.B.C, so just process A.B */ |
||||
push_back_token(tok5, &aux5); |
||||
push_back_token(tok4, &aux4); |
||||
if (plpgsql_parse_dblword(aux1.lval.str, |
||||
aux3.lval.str, |
||||
&aux1.lval.wdatum, |
||||
&aux1.lval.cword)) |
||||
tok1 = T_DATUM; |
||||
else |
||||
tok1 = T_CWORD; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
/* not A.B.C, so just process A.B */ |
||||
push_back_token(tok4, &aux4); |
||||
if (plpgsql_parse_dblword(aux1.lval.str, |
||||
aux3.lval.str, |
||||
&aux1.lval.wdatum, |
||||
&aux1.lval.cword)) |
||||
tok1 = T_DATUM; |
||||
else |
||||
tok1 = T_CWORD; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
/* not A.B, so just process A */ |
||||
push_back_token(tok3, &aux3); |
||||
push_back_token(tok2, &aux2); |
||||
if (plpgsql_parse_word(aux1.lval.str, |
||||
core_yy.scanbuf + aux1.lloc, |
||||
&aux1.lval.wdatum, |
||||
&aux1.lval.word)) |
||||
tok1 = T_DATUM; |
||||
else if (!aux1.lval.word.quoted && |
||||
(kw = ScanKeywordLookup(aux1.lval.word.ident, |
||||
unreserved_keywords, |
||||
num_unreserved_keywords))) |
||||
{ |
||||
aux1.lval.keyword = kw->name; |
||||
tok1 = kw->value; |
||||
} |
||||
else |
||||
tok1 = T_WORD; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
/* not A.B, so just process A */ |
||||
push_back_token(tok2, &aux2); |
||||
if (plpgsql_parse_word(aux1.lval.str, |
||||
core_yy.scanbuf + aux1.lloc, |
||||
&aux1.lval.wdatum, |
||||
&aux1.lval.word)) |
||||
tok1 = T_DATUM; |
||||
else if (!aux1.lval.word.quoted && |
||||
(kw = ScanKeywordLookup(aux1.lval.word.ident, |
||||
unreserved_keywords, |
||||
num_unreserved_keywords))) |
||||
{ |
||||
aux1.lval.keyword = kw->name; |
||||
tok1 = kw->value; |
||||
} |
||||
else |
||||
tok1 = T_WORD; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
/* Not a potential plpgsql variable name, just return the data */ |
||||
} |
||||
|
||||
plpgsql_yylval = aux1.lval; |
||||
plpgsql_yylloc = aux1.lloc; |
||||
plpgsql_yyleng = aux1.leng; |
||||
return tok1; |
||||
} |
||||
|
||||
/*
|
||||
* Internal yylex function. This wraps the core lexer and adds one feature: |
||||
* a token pushback stack. We also make a couple of trivial single-token |
||||
* translations from what the core lexer does to what we want, in particular |
||||
* interfacing from the core_YYSTYPE to YYSTYPE union. |
||||
*/ |
||||
static int |
||||
internal_yylex(TokenAuxData *auxdata) |
||||
{ |
||||
int token; |
||||
const char *yytext; |
||||
|
||||
if (num_pushbacks > 0) |
||||
{ |
||||
num_pushbacks--; |
||||
token = pushback_token[num_pushbacks]; |
||||
*auxdata = pushback_auxdata[num_pushbacks]; |
||||
} |
||||
else |
||||
{ |
||||
token = core_yylex(&auxdata->lval.core_yystype, |
||||
&auxdata->lloc, |
||||
yyscanner); |
||||
|
||||
/* remember the length of yytext before it gets changed */ |
||||
yytext = core_yy.scanbuf + auxdata->lloc; |
||||
auxdata->leng = strlen(yytext); |
||||
|
||||
/* Check for << >> and #, which the core considers operators */ |
||||
if (token == Op) |
||||
{ |
||||
if (strcmp(auxdata->lval.str, "<<") == 0) |
||||
token = LESS_LESS; |
||||
else if (strcmp(auxdata->lval.str, ">>") == 0) |
||||
token = GREATER_GREATER; |
||||
else if (strcmp(auxdata->lval.str, "#") == 0) |
||||
token = '#'; |
||||
} |
||||
|
||||
/* The core returns PARAM as ival, but we treat it like IDENT */ |
||||
else if (token == PARAM) |
||||
{ |
||||
auxdata->lval.str = pstrdup(yytext); |
||||
} |
||||
} |
||||
|
||||
return token; |
||||
} |
||||
|
||||
/*
|
||||
* Push back a token to be re-read by next internal_yylex() call. |
||||
*/ |
||||
static void |
||||
push_back_token(int token, TokenAuxData *auxdata) |
||||
{ |
||||
if (num_pushbacks >= MAX_PUSHBACKS) |
||||
elog(ERROR, "too many tokens pushed back"); |
||||
pushback_token[num_pushbacks] = token; |
||||
pushback_auxdata[num_pushbacks] = *auxdata; |
||||
num_pushbacks++; |
||||
} |
||||
|
||||
/*
|
||||
* Push back a single token to be re-read by next plpgsql_yylex() call. |
||||
* |
||||
* NOTE: this does not cause yylval or yylloc to "back up". Also, it |
||||
* is not a good idea to push back a token code other than what you read. |
||||
*/ |
||||
void |
||||
plpgsql_push_back_token(int token) |
||||
{ |
||||
TokenAuxData auxdata; |
||||
|
||||
auxdata.lval = plpgsql_yylval; |
||||
auxdata.lloc = plpgsql_yylloc; |
||||
auxdata.leng = plpgsql_yyleng; |
||||
push_back_token(token, &auxdata); |
||||
} |
||||
|
||||
/*
|
||||
* Append the function text starting at startlocation and extending to |
||||
* (not including) endlocation onto the existing contents of "buf". |
||||
*/ |
||||
void |
||||
plpgsql_append_source_text(StringInfo buf, |
||||
int startlocation, int endlocation) |
||||
{ |
||||
Assert(startlocation <= endlocation); |
||||
appendBinaryStringInfo(buf, scanorig + startlocation, |
||||
endlocation - startlocation); |
||||
} |
||||
|
||||
/*
|
||||
* plpgsql_scanner_errposition |
||||
* Report an error cursor position, if possible. |
||||
* |
||||
* This is expected to be used within an ereport() call. The return value |
||||
* is a dummy (always 0, in fact). |
||||
* |
||||
* Note that this can only be used for messages emitted during initial |
||||
* parsing of a plpgsql function, since it requires the scanorig string |
||||
* to still be available. |
||||
*/ |
||||
int |
||||
plpgsql_scanner_errposition(int location) |
||||
{ |
||||
int pos; |
||||
|
||||
if (location < 0 || scanorig == NULL) |
||||
return 0; /* no-op if location is unknown */ |
||||
|
||||
/* Convert byte offset to character number */ |
||||
pos = pg_mbstrlen_with_len(scanorig, location) + 1; |
||||
/* And pass it to the ereport mechanism */ |
||||
(void) internalerrposition(pos); |
||||
/* Also pass the function body string */ |
||||
return internalerrquery(scanorig); |
||||
} |
||||
|
||||
/*
|
||||
* plpgsql_yyerror |
||||
* Report a lexer or grammar error. |
||||
* |
||||
* The message's cursor position refers to the current token (the one |
||||
* last returned by plpgsql_yylex()). |
||||
* This is OK for syntax error messages from the Bison parser, because Bison |
||||
* parsers report error as soon as the first unparsable token is reached. |
||||
* Beware of using yyerror for other purposes, as the cursor position might |
||||
* be misleading! |
||||
*/ |
||||
void |
||||
plpgsql_yyerror(const char *message) |
||||
{ |
||||
char *yytext = core_yy.scanbuf + plpgsql_yylloc; |
||||
|
||||
if (*yytext == '\0') |
||||
{ |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_SYNTAX_ERROR), |
||||
/* translator: %s is typically the translation of "syntax error" */ |
||||
errmsg("%s at end of input", _(message)), |
||||
plpgsql_scanner_errposition(plpgsql_yylloc))); |
||||
} |
||||
else |
||||
{ |
||||
/*
|
||||
* If we have done any lookahead then flex will have restored the |
||||
* character after the end-of-token. Zap it again so that we |
||||
* report only the single token here. This modifies scanbuf but |
||||
* we no longer care about that. |
||||
*/ |
||||
yytext[plpgsql_yyleng] = '\0'; |
||||
|
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_SYNTAX_ERROR), |
||||
/* translator: first %s is typically the translation of "syntax error" */ |
||||
errmsg("%s at or near \"%s\"", _(message), yytext), |
||||
plpgsql_scanner_errposition(plpgsql_yylloc))); |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
* Given a location (a byte offset in the function source text), |
||||
* return a line number. |
||||
* |
||||
* We expect that this is typically called for a sequence of increasing |
||||
* location values, so optimize accordingly by tracking the endpoints |
||||
* of the "current" line. |
||||
*/ |
||||
int |
||||
plpgsql_location_to_lineno(int location) |
||||
{ |
||||
const char *loc; |
||||
|
||||
if (location < 0 || scanorig == NULL) |
||||
return 0; /* garbage in, garbage out */ |
||||
loc = scanorig + location; |
||||
|
||||
/* be correct, but not fast, if input location goes backwards */ |
||||
if (loc < cur_line_start) |
||||
location_lineno_init(); |
||||
|
||||
while (cur_line_end != NULL && loc > cur_line_end) |
||||
{ |
||||
cur_line_start = cur_line_end + 1; |
||||
cur_line_num++; |
||||
cur_line_end = strchr(cur_line_start, '\n'); |
||||
} |
||||
|
||||
return cur_line_num; |
||||
} |
||||
|
||||
/* initialize or reset the state for plpgsql_location_to_lineno */ |
||||
static void |
||||
location_lineno_init(void) |
||||
{ |
||||
cur_line_start = scanorig; |
||||
cur_line_num = 1; |
||||
|
||||
/*----------
|
||||
* Hack: skip any initial newline, so that in the common coding layout |
||||
* CREATE FUNCTION ... AS $$ |
||||
* code body |
||||
* $$ LANGUAGE plpgsql; |
||||
* we will think "line 1" is what the programmer thinks of as line 1. |
||||
*---------- |
||||
*/ |
||||
if (*cur_line_start == '\r') |
||||
cur_line_start++; |
||||
if (*cur_line_start == '\n') |
||||
cur_line_start++; |
||||
|
||||
cur_line_end = strchr(cur_line_start, '\n'); |
||||
} |
||||
|
||||
/* return the most recently computed lineno */ |
||||
int |
||||
plpgsql_latest_lineno(void) |
||||
{ |
||||
return cur_line_num; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Called before any actual parsing is done |
||||
* |
||||
* Note: the passed "str" must remain valid until plpgsql_scanner_finish(). |
||||
* Although it is not fed directly to flex, we need the original string |
||||
* to cite in error messages. |
||||
*/ |
||||
void |
||||
plpgsql_scanner_init(const char *str) |
||||
{ |
||||
/* Start up the core scanner */ |
||||
yyscanner = scanner_init(str, &core_yy, |
||||
reserved_keywords, num_reserved_keywords); |
||||
|
||||
/*
|
||||
* scanorig points to the original string, which unlike the scanner's |
||||
* scanbuf won't be modified on-the-fly by flex. Notice that although |
||||
* yytext points into scanbuf, we rely on being able to apply locations |
||||
* (offsets from string start) to scanorig as well. |
||||
*/ |
||||
scanorig = str; |
||||
|
||||
/* Other setup */ |
||||
plpgsql_LookupIdentifiers = true; |
||||
|
||||
num_pushbacks = 0; |
||||
|
||||
location_lineno_init(); |
||||
} |
||||
|
||||
/*
|
||||
* Called after parsing is done to clean up after plpgsql_scanner_init() |
||||
*/ |
||||
void |
||||
plpgsql_scanner_finish(void) |
||||
{ |
||||
/* release storage */ |
||||
scanner_finish(yyscanner); |
||||
/* avoid leaving any dangling pointers */ |
||||
yyscanner = NULL; |
||||
scanorig = NULL; |
||||
} |
||||
@ -1,605 +0,0 @@ |
||||
%{ |
||||
/*------------------------------------------------------------------------- |
||||
* |
||||
* scan.l - Scanner for the PL/pgSQL procedural language |
||||
* |
||||
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* $PostgreSQL: pgsql/src/pl/plpgsql/src/scan.l,v 1.76 2009/11/10 02:13:13 tgl Exp $ |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#include "plpgsql.h" |
||||
|
||||
#include "mb/pg_wchar.h" |
||||
|
||||
|
||||
/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */ |
||||
#undef fprintf |
||||
#define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg))) |
||||
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
#define SET_YYLLOC() (yylloc = yytext - scanbuf) |
||||
|
||||
/* Handles to the buffer that the lexer uses internally */ |
||||
static YY_BUFFER_STATE scanbufhandle; |
||||
static char *scanbuf; |
||||
|
||||
static const char *scanorig; /* original input string */ |
||||
|
||||
static int pushback_token; |
||||
static bool have_pushback_token; |
||||
static const char *cur_line_start; |
||||
static const char *cur_line_end; |
||||
static int cur_line_num; |
||||
static int xcdepth = 0; /* depth of nesting in slash-star comments */ |
||||
static char *dolqstart; /* current $foo$ quote start string */ |
||||
|
||||
bool plpgsql_LookupIdentifiers = true; |
||||
|
||||
static void location_lineno_init(void); |
||||
|
||||
%} |
||||
|
||||
%option 8bit |
||||
%option never-interactive |
||||
%option nodefault |
||||
%option noinput |
||||
%option nounput |
||||
%option noyywrap |
||||
%option noyyalloc |
||||
%option noyyrealloc |
||||
%option noyyfree |
||||
%option warn |
||||
%option prefix="plpgsql_base_yy" |
||||
|
||||
%option case-insensitive |
||||
|
||||
/* |
||||
* Exclusive states are a subset of the core lexer's: |
||||
* <xc> extended C-style comments |
||||
* <xq> standard quoted strings |
||||
* <xe> extended quoted strings (support backslash escape sequences) |
||||
* <xdolq> $foo$ quoted strings |
||||
*/ |
||||
|
||||
%x xc |
||||
%x xe |
||||
%x xq |
||||
%x xdolq |
||||
|
||||
/* |
||||
* Definitions --- these generally must match the core lexer, but in some |
||||
* cases we can simplify, since we only care about identifying the token |
||||
* boundaries and not about deriving the represented value. Also, we |
||||
* aren't trying to lex multicharacter operators so their interactions |
||||
* with comments go away. |
||||
*/ |
||||
|
||||
space [ \t\n\r\f] |
||||
horiz_space [ \t\f] |
||||
newline [\n\r] |
||||
non_newline [^\n\r] |
||||
|
||||
comment ("--"{non_newline}*) |
||||
|
||||
whitespace ({space}+|{comment}) |
||||
special_whitespace ({space}+|{comment}{newline}) |
||||
horiz_whitespace ({horiz_space}|{comment}) |
||||
whitespace_with_newline ({horiz_whitespace}*{newline}{special_whitespace}*) |
||||
|
||||
quote ' |
||||
quotestop {quote}{whitespace}* |
||||
quotecontinue {quote}{whitespace_with_newline}{quote} |
||||
quotefail {quote}{whitespace}*"-" |
||||
|
||||
xestart [eE]{quote} |
||||
xeinside [^\\']+ |
||||
xeescape [\\]. |
||||
|
||||
xqstart {quote} |
||||
xqdouble {quote}{quote} |
||||
xqinside [^']+ |
||||
|
||||
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 [^$]+ |
||||
|
||||
xcstart \/\* |
||||
xcstop \*+\/ |
||||
xcinside [^*/]+ |
||||
|
||||
digit [0-9] |
||||
ident_start [A-Za-z\200-\377_] |
||||
ident_cont [A-Za-z\200-\377_0-9\$] |
||||
|
||||
/* This is a simpler treatment of quoted identifiers than the core uses */ |
||||
quoted_ident (\"[^\"]*\")+ |
||||
|
||||
identifier ({ident_start}{ident_cont}*|{quoted_ident}) |
||||
|
||||
param \${digit}+ |
||||
|
||||
%% |
||||
/* ---------- |
||||
* Reset the state when entering yylex() |
||||
* ---------- |
||||
*/ |
||||
BEGIN(INITIAL); |
||||
|
||||
/* ---------- |
||||
* The keyword rules |
||||
* ---------- |
||||
*/ |
||||
:= { SET_YYLLOC(); return K_ASSIGN; } |
||||
= { SET_YYLLOC(); return K_ASSIGN; } |
||||
\.\. { SET_YYLLOC(); return K_DOTDOT; } |
||||
alias { SET_YYLLOC(); return K_ALIAS; } |
||||
all { SET_YYLLOC(); return K_ALL; } |
||||
begin { SET_YYLLOC(); return K_BEGIN; } |
||||
by { SET_YYLLOC(); return K_BY; } |
||||
case { SET_YYLLOC(); return K_CASE; } |
||||
close { SET_YYLLOC(); return K_CLOSE; } |
||||
constant { SET_YYLLOC(); return K_CONSTANT; } |
||||
continue { SET_YYLLOC(); return K_CONTINUE; } |
||||
cursor { SET_YYLLOC(); return K_CURSOR; } |
||||
declare { SET_YYLLOC(); return K_DECLARE; } |
||||
default { SET_YYLLOC(); return K_DEFAULT; } |
||||
diagnostics { SET_YYLLOC(); return K_DIAGNOSTICS; } |
||||
else { SET_YYLLOC(); return K_ELSE; } |
||||
elseif { SET_YYLLOC(); return K_ELSIF; } |
||||
elsif { SET_YYLLOC(); return K_ELSIF; } |
||||
end { SET_YYLLOC(); return K_END; } |
||||
exception { SET_YYLLOC(); return K_EXCEPTION; } |
||||
execute { SET_YYLLOC(); return K_EXECUTE; } |
||||
exit { SET_YYLLOC(); return K_EXIT; } |
||||
fetch { SET_YYLLOC(); return K_FETCH; } |
||||
for { SET_YYLLOC(); return K_FOR; } |
||||
from { SET_YYLLOC(); return K_FROM; } |
||||
get { SET_YYLLOC(); return K_GET; } |
||||
if { SET_YYLLOC(); return K_IF; } |
||||
in { SET_YYLLOC(); return K_IN; } |
||||
insert { SET_YYLLOC(); return K_INSERT; } |
||||
into { SET_YYLLOC(); return K_INTO; } |
||||
is { SET_YYLLOC(); return K_IS; } |
||||
loop { SET_YYLLOC(); return K_LOOP; } |
||||
move { SET_YYLLOC(); return K_MOVE; } |
||||
no{space}+scroll { SET_YYLLOC(); return K_NOSCROLL; } |
||||
not { SET_YYLLOC(); return K_NOT; } |
||||
null { SET_YYLLOC(); return K_NULL; } |
||||
open { SET_YYLLOC(); return K_OPEN; } |
||||
or { SET_YYLLOC(); return K_OR; } |
||||
perform { SET_YYLLOC(); return K_PERFORM; } |
||||
raise { SET_YYLLOC(); return K_RAISE; } |
||||
return { SET_YYLLOC(); return K_RETURN; } |
||||
scroll { SET_YYLLOC(); return K_SCROLL; } |
||||
strict { SET_YYLLOC(); return K_STRICT; } |
||||
then { SET_YYLLOC(); return K_THEN; } |
||||
to { SET_YYLLOC(); return K_TO; } |
||||
using { SET_YYLLOC(); return K_USING; } |
||||
when { SET_YYLLOC(); return K_WHEN; } |
||||
while { SET_YYLLOC(); return K_WHILE; } |
||||
|
||||
^#option { SET_YYLLOC(); return O_OPTION; } |
||||
dump { SET_YYLLOC(); return O_DUMP; } |
||||
|
||||
|
||||
/* ---------- |
||||
* Special word rules |
||||
* ---------- |
||||
*/ |
||||
{identifier} { |
||||
SET_YYLLOC(); |
||||
return plpgsql_parse_word(yytext); } |
||||
{identifier}{space}*\.{space}*{identifier} { |
||||
SET_YYLLOC(); |
||||
return plpgsql_parse_dblword(yytext); } |
||||
{identifier}{space}*\.{space}*{identifier}{space}*\.{space}*{identifier} { |
||||
SET_YYLLOC(); |
||||
return plpgsql_parse_tripword(yytext); } |
||||
{param} { |
||||
SET_YYLLOC(); |
||||
return plpgsql_parse_word(yytext); } |
||||
{param}{space}*\.{space}*{identifier} { |
||||
SET_YYLLOC(); |
||||
return plpgsql_parse_dblword(yytext); } |
||||
{param}{space}*\.{space}*{identifier}{space}*\.{space}*{identifier} { |
||||
SET_YYLLOC(); |
||||
return plpgsql_parse_tripword(yytext); } |
||||
|
||||
{digit}+ { SET_YYLLOC(); return T_NUMBER; } |
||||
|
||||
\". { SET_YYLLOC(); yyerror("unterminated quoted identifier"); } |
||||
|
||||
/* ---------- |
||||
* Comment and literal handling is mostly copied from the core lexer |
||||
* ---------- |
||||
*/ |
||||
{whitespace} { |
||||
/* ignore */ |
||||
} |
||||
|
||||
{xcstart} { |
||||
SET_YYLLOC(); |
||||
xcdepth = 0; |
||||
BEGIN(xc); |
||||
} |
||||
|
||||
<xc>{xcstart} { |
||||
xcdepth++; |
||||
} |
||||
|
||||
<xc>{xcstop} { |
||||
if (xcdepth <= 0) |
||||
BEGIN(INITIAL); |
||||
else |
||||
xcdepth--; |
||||
} |
||||
|
||||
<xc>{xcinside} { |
||||
/* ignore */ |
||||
} |
||||
|
||||
<xc>\/+ { |
||||
/* ignore */ |
||||
} |
||||
|
||||
<xc>\*+ { |
||||
/* ignore */ |
||||
} |
||||
|
||||
<xc><<EOF>> { yyerror("unterminated /* comment"); } |
||||
|
||||
{xqstart} { |
||||
SET_YYLLOC(); |
||||
if (standard_conforming_strings) |
||||
BEGIN(xq); |
||||
else |
||||
BEGIN(xe); |
||||
} |
||||
{xestart} { |
||||
SET_YYLLOC(); |
||||
BEGIN(xe); |
||||
} |
||||
<xq,xe>{quotestop} | |
||||
<xq,xe>{quotefail} { |
||||
yyless(1); |
||||
BEGIN(INITIAL); |
||||
/* adjust yytext/yyleng to describe whole string token */ |
||||
yyleng += (yytext - (scanbuf + yylloc)); |
||||
yytext = scanbuf + yylloc; |
||||
return T_STRING; |
||||
} |
||||
<xq,xe>{xqdouble} { |
||||
} |
||||
<xq>{xqinside} { |
||||
} |
||||
<xe>{xeinside} { |
||||
} |
||||
<xe>{xeescape} { |
||||
} |
||||
<xq,xe>{quotecontinue} { |
||||
/* ignore */ |
||||
} |
||||
<xe>. { |
||||
/* This is only needed for \ just before EOF */ |
||||
} |
||||
<xq,xe><<EOF>> { yyerror("unterminated quoted string"); } |
||||
|
||||
{dolqdelim} { |
||||
SET_YYLLOC(); |
||||
dolqstart = pstrdup(yytext); |
||||
BEGIN(xdolq); |
||||
} |
||||
{dolqfailed} { |
||||
/* throw back all but the initial "$" */ |
||||
yyless(1); |
||||
/* and treat it as {other} */ |
||||
SET_YYLLOC(); return yytext[0]; |
||||
} |
||||
<xdolq>{dolqdelim} { |
||||
if (strcmp(yytext, dolqstart) == 0) |
||||
{ |
||||
pfree(dolqstart); |
||||
BEGIN(INITIAL); |
||||
/* adjust yytext/yyleng to describe whole string */ |
||||
yyleng += (yytext - (scanbuf + yylloc)); |
||||
yytext = scanbuf + yylloc; |
||||
return T_STRING; |
||||
} |
||||
else |
||||
{ |
||||
/* |
||||
* When we fail to match $...$ to dolqstart, transfer |
||||
* the $... part to the output, but put back the final |
||||
* $ for rescanning. Consider $delim$...$junk$delim$ |
||||
*/ |
||||
yyless(yyleng-1); |
||||
} |
||||
} |
||||
<xdolq>{dolqinside} { |
||||
} |
||||
<xdolq>{dolqfailed} { |
||||
} |
||||
<xdolq>. { |
||||
/* This is only needed for $ inside the quoted text */ |
||||
} |
||||
<xdolq><<EOF>> { yyerror("unterminated dollar-quoted string"); } |
||||
|
||||
/* ---------- |
||||
* Any unmatched character is returned as is |
||||
* ---------- |
||||
*/ |
||||
. { |
||||
SET_YYLLOC(); return yytext[0]; |
||||
} |
||||
|
||||
%% |
||||
|
||||
|
||||
/* |
||||
* This is the yylex routine called from outside. It exists to provide |
||||
* a one-token pushback facility. Beware of trying to push back more; |
||||
* for the most part, plpgsql's gram.y assumes that yytext and yylloc |
||||
* are in step with the "current token". In particular it is assumed that |
||||
* those are in step with the result immediately after any yylex() call. |
||||
*/ |
||||
int |
||||
plpgsql_yylex(void) |
||||
{ |
||||
if (have_pushback_token) |
||||
{ |
||||
have_pushback_token = false; |
||||
return pushback_token; |
||||
} |
||||
return yylex(); |
||||
} |
||||
|
||||
/* |
||||
* Push back a single token to be re-read by next plpgsql_yylex() call. |
||||
* |
||||
* NOTE: this does not cause yytext or yylloc to "back up". Also, it |
||||
* is not a good idea to push back a token other than what you read. |
||||
*/ |
||||
void |
||||
plpgsql_push_back_token(int token) |
||||
{ |
||||
if (have_pushback_token) |
||||
elog(ERROR, "cannot push back multiple tokens"); |
||||
pushback_token = token; |
||||
have_pushback_token = true; |
||||
} |
||||
|
||||
/* |
||||
* Append the function text starting at startlocation and extending to |
||||
* (not including) endlocation onto the existing contents of "buf". |
||||
*/ |
||||
void |
||||
plpgsql_append_source_text(StringInfo buf, |
||||
int startlocation, int endlocation) |
||||
{ |
||||
Assert(startlocation <= endlocation); |
||||
appendBinaryStringInfo(buf, scanorig + startlocation, |
||||
endlocation - startlocation); |
||||
} |
||||
|
||||
/* |
||||
* plpgsql_scanner_errposition |
||||
* Report an error cursor position, if possible. |
||||
* |
||||
* This is expected to be used within an ereport() call. The return value |
||||
* is a dummy (always 0, in fact). |
||||
* |
||||
* Note that this can only be used for messages emitted during initial |
||||
* parsing of a plpgsql function, since it requires the scanorig string |
||||
* to still be available. |
||||
*/ |
||||
int |
||||
plpgsql_scanner_errposition(int location) |
||||
{ |
||||
int pos; |
||||
|
||||
if (location < 0 || scanorig == NULL) |
||||
return 0; /* no-op if location is unknown */ |
||||
|
||||
/* Convert byte offset to character number */ |
||||
pos = pg_mbstrlen_with_len(scanorig, location) + 1; |
||||
/* And pass it to the ereport mechanism */ |
||||
(void) internalerrposition(pos); |
||||
/* Also pass the function body string */ |
||||
return internalerrquery(scanorig); |
||||
} |
||||
|
||||
/* |
||||
* plpgsql_yyerror |
||||
* Report a lexer or grammar error. |
||||
* |
||||
* The message's cursor position is whatever YYLLOC was last set to, |
||||
* ie, the start of the current token if called within yylex(), or the |
||||
* most recently lexed token if called from the grammar. |
||||
* This is OK for syntax error messages from the Bison parser, because Bison |
||||
* parsers report error as soon as the first unparsable token is reached. |
||||
* Beware of using yyerror for other purposes, as the cursor position might |
||||
* be misleading! |
||||
*/ |
||||
void |
||||
plpgsql_yyerror(const char *message) |
||||
{ |
||||
const char *loc = scanbuf + yylloc; |
||||
|
||||
if (*loc == YY_END_OF_BUFFER_CHAR) |
||||
{ |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_SYNTAX_ERROR), |
||||
/* translator: %s is typically the translation of "syntax error" */ |
||||
errmsg("%s at end of input", _(message)), |
||||
plpgsql_scanner_errposition(yylloc))); |
||||
} |
||||
else |
||||
{ |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_SYNTAX_ERROR), |
||||
/* translator: first %s is typically the translation of "syntax error" */ |
||||
errmsg("%s at or near \"%s\"", _(message), loc), |
||||
plpgsql_scanner_errposition(yylloc))); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* Given a location (a byte offset in the function source text), |
||||
* return a line number. |
||||
* |
||||
* We expect that this is typically called for a sequence of increasing |
||||
* location values, so optimize accordingly by tracking the endpoints |
||||
* of the "current" line. |
||||
*/ |
||||
int |
||||
plpgsql_location_to_lineno(int location) |
||||
{ |
||||
const char *loc; |
||||
|
||||
if (location < 0 || scanorig == NULL) |
||||
return 0; /* garbage in, garbage out */ |
||||
loc = scanorig + location; |
||||
|
||||
/* be correct, but not fast, if input location goes backwards */ |
||||
if (loc < cur_line_start) |
||||
location_lineno_init(); |
||||
|
||||
while (cur_line_end != NULL && loc > cur_line_end) |
||||
{ |
||||
cur_line_start = cur_line_end + 1; |
||||
cur_line_num++; |
||||
cur_line_end = strchr(cur_line_start, '\n'); |
||||
} |
||||
|
||||
return cur_line_num; |
||||
} |
||||
|
||||
/* initialize or reset the state for plpgsql_location_to_lineno */ |
||||
static void |
||||
location_lineno_init(void) |
||||
{ |
||||
cur_line_start = scanorig; |
||||
cur_line_num = 1; |
||||
|
||||
/*---------- |
||||
* Hack: skip any initial newline, so that in the common coding layout |
||||
* CREATE FUNCTION ... AS $$ |
||||
* code body |
||||
* $$ LANGUAGE plpgsql; |
||||
* we will think "line 1" is what the programmer thinks of as line 1. |
||||
*---------- |
||||
*/ |
||||
if (*cur_line_start == '\r') |
||||
cur_line_start++; |
||||
if (*cur_line_start == '\n') |
||||
cur_line_start++; |
||||
|
||||
cur_line_end = strchr(cur_line_start, '\n'); |
||||
} |
||||
|
||||
/* return the most recently computed lineno */ |
||||
int |
||||
plpgsql_latest_lineno(void) |
||||
{ |
||||
return cur_line_num; |
||||
} |
||||
|
||||
|
||||
/* |
||||
* Called before any actual parsing is done |
||||
* |
||||
* Note: the passed "str" must remain valid until plpgsql_scanner_finish(). |
||||
* Although it is not fed directly to flex, we need the original string |
||||
* to cite in error messages. |
||||
*/ |
||||
void |
||||
plpgsql_scanner_init(const char *str) |
||||
{ |
||||
Size slen = strlen(str); |
||||
|
||||
/* |
||||
* Reset flex internal state. Whatever data it might think it has |
||||
* has long since been pfree'd. |
||||
*/ |
||||
yy_init_globals(); |
||||
|
||||
/* |
||||
* Make a scan buffer with special termination needed by flex. |
||||
*/ |
||||
scanbuf = (char *) palloc(slen + 2); |
||||
memcpy(scanbuf, str, slen); |
||||
scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR; |
||||
scanbufhandle = yy_scan_buffer(scanbuf, slen + 2); |
||||
|
||||
/* |
||||
* scanorig points to the original string, which unlike scanbuf won't |
||||
* be modified on-the-fly by flex. Notice that although yytext points |
||||
* into scanbuf, we rely on being able to apply locations (offsets from |
||||
* string start) to scanorig as well. |
||||
*/ |
||||
scanorig = str; |
||||
|
||||
/* Other setup */ |
||||
have_pushback_token = false; |
||||
|
||||
location_lineno_init(); |
||||
|
||||
BEGIN(INITIAL); |
||||
plpgsql_LookupIdentifiers = true; |
||||
} |
||||
|
||||
/* |
||||
* Called after parsing is done to clean up after plpgsql_scanner_init() |
||||
*/ |
||||
void |
||||
plpgsql_scanner_finish(void) |
||||
{ |
||||
/* release storage */ |
||||
yy_delete_buffer(scanbufhandle); |
||||
pfree(scanbuf); |
||||
/* avoid leaving any dangling pointers */ |
||||
scanbufhandle = NULL; |
||||
scanbuf = NULL; |
||||
scanorig = NULL; |
||||
} |
||||
|
||||
/* |
||||
* Interface functions to make flex use palloc() instead of malloc(). |
||||
* It'd be better to make these static, but flex insists otherwise. |
||||
*/ |
||||
|
||||
void * |
||||
plpgsql_base_yyalloc(yy_size_t bytes) |
||||
{ |
||||
return palloc(bytes); |
||||
} |
||||
|
||||
void * |
||||
plpgsql_base_yyrealloc(void *ptr, yy_size_t bytes) |
||||
{ |
||||
if (ptr) |
||||
return repalloc(ptr, bytes); |
||||
else |
||||
return palloc(bytes); |
||||
} |
||||
|
||||
void |
||||
plpgsql_base_yyfree(void *ptr) |
||||
{ |
||||
if (ptr) |
||||
pfree(ptr); |
||||
} |
||||
Loading…
Reference in new issue