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 b2216a9eac..a62798cf98 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -537,6 +537,7 @@ other . { /* If NCHAR isn't a keyword, just return "n" */ yylval->str = pstrdup("n"); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return IDENT; } } @@ -605,9 +606,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: @@ -620,9 +623,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"); @@ -760,6 +765,7 @@ other . yyextra->dolqstart = NULL; BEGIN(INITIAL); yylval->str = litbufdup(yyscanner); + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return SCONST; } else @@ -805,6 +811,7 @@ other . if (yyextra->literallen >= NAMEDATALEN) truncate_identifier(ident, yyextra->literallen, true); yylval->str = ident; + yyextra->yyllocend = yytext - yyextra->scanbuf + yyleng; return IDENT; } {dquote} { @@ -813,6 +820,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} { @@ -832,6 +840,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; } @@ -1096,6 +1105,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 da013837cd..e2a73201c4 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; /*