commit 386cf95ca17d28ae4bc7db92a784ab8f6dde9f8b Author: Lukas Fittl Date: Sun Jan 3 15:59:40 2021 -0800 pg_query: Track yyllocend in lexer This is helpful for tracking the extent of tokens in the scan output, as this is made available by pg_query for uses such as syntax highlighting. diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index 3248fb5108..28fdd262b1 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -538,6 +538,7 @@ other . { /* If NCHAR isn't a keyword, just return "n" */ yylval->str = pstrdup("n"); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return IDENT; } } @@ -606,9 +607,11 @@ other . { case xb: yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return BCONST; case xh: yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return XCONST; case xq: case xe: @@ -621,9 +624,11 @@ other . yyextra->literallen, false); yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return SCONST; case xus: yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return USCONST; default: yyerror("unhandled previous state in xqs"); @@ -761,6 +766,7 @@ other . yyextra->dolqstart = NULL; BEGIN(INITIAL); yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return SCONST; } else @@ -806,6 +812,7 @@ other . if (yyextra->literallen >= NAMEDATALEN) truncate_identifier(ident, yyextra->literallen, true); yylval->str = ident; + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return IDENT; } {dquote} { @@ -814,6 +821,7 @@ other . yyerror("zero-length delimited identifier"); /* can't truncate till after we de-escape the ident */ yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return UIDENT; } {xddouble} { @@ -833,6 +841,7 @@ other . /* and treat it as {identifier} */ ident = downcase_truncate_identifier(yytext, yyleng, true); yylval->str = ident; + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return IDENT; } @@ -1097,6 +1106,7 @@ other . */ ident = downcase_truncate_identifier(yytext, yyleng, true); yylval->str = ident; + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return IDENT; } diff --git a/src/include/parser/scanner.h b/src/include/parser/scanner.h index d6293b1e87..59f492c6de 100644 --- a/src/include/parser/scanner.h +++ b/src/include/parser/scanner.h @@ -113,6 +113,8 @@ typedef struct core_yy_extra_type /* state variables for literal-lexing warnings */ bool warn_on_first_escape; bool saw_non_ascii; + + int yyllocend; } core_yy_extra_type; /*