aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-09-20 14:22:58 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2019-09-20 14:22:58 -0400
commite56cad84d542a8cc2056390a9c651118cfa6c89c (patch)
treec4d6b3ea69ae574e3880507f77037c474309755b /src/backend/utils
parent96b6c82c9dd4a6a91c7e54bf42d36da111959ec6 (diff)
downloadpostgresql-e56cad84d542a8cc2056390a9c651118cfa6c89c.tar.gz
postgresql-e56cad84d542a8cc2056390a9c651118cfa6c89c.zip
Fix some minor spec-compliance issues in jsonpath lexer.
Although the SQL/JSON tech report makes reference to ECMAScript which allows both single- and double-quoted strings, all the rest of the report speaks only of double-quoted string literals in jsonpaths. That's more compatible with JSON itself; moreover single-quoted strings are hard to use inside a jsonpath that is itself a single-quoted SQL literal. So guess that the intent is to allow only double-quoted literals, and remove lexer support for single-quoted literals. It'll be less painful to add this again later if we're wrong, than to remove a shipped feature. Also, adjust the lexer so that unrecognized backslash sequences are treated as just meaning the escaped character, not as errors. This change has much better support in the standards, as JSON, JavaScript and ECMAScript all make it plain that that's what's supposed to happen. Back-patch to v12. Discussion: https://postgr.es/m/CAPpHfdvDci4iqNF9fhRkTqhe-5_8HmzeLt56drH%2B_Rv2rNRqfg@mail.gmail.com
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/jsonpath_scan.l79
1 files changed, 31 insertions, 48 deletions
diff --git a/src/backend/utils/adt/jsonpath_scan.l b/src/backend/utils/adt/jsonpath_scan.l
index 2165ffcc254..e35636883a1 100644
--- a/src/backend/utils/adt/jsonpath_scan.l
+++ b/src/backend/utils/adt/jsonpath_scan.l
@@ -59,25 +59,24 @@ fprintf_to_ereport(const char *fmt, const char *msg)
%option noyyfree
/*
- * We use exclusive states for quoted, signle-quoted and non-quoted strings,
- * quoted variable names and C-tyle comments.
+ * We use exclusive states for quoted and non-quoted strings,
+ * quoted variable names and C-style comments.
* Exclusive states:
* <xq> - quoted strings
* <xnq> - non-quoted strings
* <xvq> - quoted variable names
- * <xsq> - single-quoted strings
* <xc> - C-style comment
*/
%x xq
%x xnq
%x xvq
-%x xsq
%x xc
-special [\?\%\$\.\[\]\{\}\(\)\|\&\!\=\<\>\@\#\,\*:\-\+\/]
-any [^\?\%\$\.\[\]\{\}\(\)\|\&\!\=\<\>\@\#\,\*:\-\+\/\\\"\' \t\n\r\f]
+special [\?\%\$\.\[\]\{\}\(\)\|\&\!\=\<\>\@\#\,\*:\-\+\/]
blank [ \t\n\r\f]
+/* "other" means anything that's not special, blank, or '\' or '"' */
+other [^\?\%\$\.\[\]\{\}\(\)\|\&\!\=\<\>\@\#\,\*:\-\+\/\\\" \t\n\r\f]
digit [0-9]
integer (0|[1-9]{digit}*)
@@ -95,7 +94,7 @@ hex_fail \\x{hex_dig}{0,1}
%%
-<xnq>{any}+ {
+<xnq>{other}+ {
addstring(false, yytext, yyleng);
}
@@ -105,13 +104,12 @@ hex_fail \\x{hex_dig}{0,1}
return checkKeyword();
}
-
<xnq>\/\* {
yylval->str = scanstring;
BEGIN xc;
}
-<xnq>({special}|\"|\') {
+<xnq>({special}|\") {
yylval->str = scanstring;
yyless(0);
BEGIN INITIAL;
@@ -124,39 +122,37 @@ hex_fail \\x{hex_dig}{0,1}
return checkKeyword();
}
-<xnq,xq,xvq,xsq>\\[\"\'\\] { addchar(false, yytext[1]); }
-
-<xnq,xq,xvq,xsq>\\b { addchar(false, '\b'); }
+<xnq,xq,xvq>\\b { addchar(false, '\b'); }
-<xnq,xq,xvq,xsq>\\f { addchar(false, '\f'); }
+<xnq,xq,xvq>\\f { addchar(false, '\f'); }
-<xnq,xq,xvq,xsq>\\n { addchar(false, '\n'); }
+<xnq,xq,xvq>\\n { addchar(false, '\n'); }
-<xnq,xq,xvq,xsq>\\r { addchar(false, '\r'); }
+<xnq,xq,xvq>\\r { addchar(false, '\r'); }
-<xnq,xq,xvq,xsq>\\t { addchar(false, '\t'); }
+<xnq,xq,xvq>\\t { addchar(false, '\t'); }
-<xnq,xq,xvq,xsq>\\v { addchar(false, '\v'); }
+<xnq,xq,xvq>\\v { addchar(false, '\v'); }
-<xnq,xq,xvq,xsq>{unicode}+ { parseUnicode(yytext, yyleng); }
+<xnq,xq,xvq>{unicode}+ { parseUnicode(yytext, yyleng); }
-<xnq,xq,xvq,xsq>{hex_char} { parseHexChar(yytext); }
+<xnq,xq,xvq>{hex_char} { parseHexChar(yytext); }
-<xnq,xq,xvq,xsq>{unicode}*{unicodefail} { yyerror(NULL, "invalid unicode sequence"); }
+<xnq,xq,xvq>{unicode}*{unicodefail} { yyerror(NULL, "invalid unicode sequence"); }
-<xnq,xq,xvq,xsq>{hex_fail} { yyerror(NULL, "invalid hex character sequence"); }
+<xnq,xq,xvq>{hex_fail} { yyerror(NULL, "invalid hex character sequence"); }
-<xnq,xq,xvq,xsq>{unicode}+\\ {
- /* throw back the \\, and treat as unicode */
- yyless(yyleng - 1);
- parseUnicode(yytext, yyleng);
- }
+<xnq,xq,xvq>{unicode}+\\ {
+ /* throw back the \\, and treat as unicode */
+ yyless(yyleng - 1);
+ parseUnicode(yytext, yyleng);
+ }
-<xnq,xq,xvq,xsq>\\. { yyerror(NULL, "escape sequence is invalid"); }
+<xnq,xq,xvq>\\. { addchar(false, yytext[1]); }
-<xnq,xq,xvq,xsq>\\ { yyerror(NULL, "unexpected end after backslash"); }
+<xnq,xq,xvq>\\ { yyerror(NULL, "unexpected end after backslash"); }
-<xq,xvq,xsq><<EOF>> { yyerror(NULL, "unexpected end of quoted string"); }
+<xq,xvq><<EOF>> { yyerror(NULL, "unexpected end of quoted string"); }
<xq>\" {
yylval->str = scanstring;
@@ -170,16 +166,8 @@ hex_fail \\x{hex_dig}{0,1}
return VARIABLE_P;
}
-<xsq>\' {
- yylval->str = scanstring;
- BEGIN INITIAL;
- return STRING_P;
- }
-
<xq,xvq>[^\\\"]+ { addstring(false, yytext, yyleng); }
-<xsq>[^\\\']+ { addstring(false, yytext, yyleng); }
-
<xc>\*\/ { BEGIN INITIAL; }
<xc>[^\*]+ { }
@@ -210,7 +198,7 @@ hex_fail \\x{hex_dig}{0,1}
\> { return GREATER_P; }
-\${any}+ {
+\${other}+ {
addstring(true, yytext + 1, yyleng - 1);
addchar(false, '\0');
yylval->str = scanstring;
@@ -263,27 +251,22 @@ hex_fail \\x{hex_dig}{0,1}
({realfail1}|{realfail2}) { yyerror(NULL, "invalid floating point number"); }
-{any}+ {
- addstring(true, yytext, yyleng);
- BEGIN xnq;
- }
-
\" {
addchar(true, '\0');
BEGIN xq;
}
-\' {
- addchar(true, '\0');
- BEGIN xsq;
- }
-
\\ {
yyless(0);
addchar(true, '\0');
BEGIN xnq;
}
+{other}+ {
+ addstring(true, yytext, yyleng);
+ BEGIN xnq;
+ }
+
<<EOF>> { yyterminate(); }
%%