Andrew Dunstan wrote:
Tom Lane wrote:
Andrew Dunstan <[EMAIL PROTECTED]> writes:
After staring at the code for a long time, I think I see how to do this. It's complicated a bit by the fact that $ is a valid identifier character. So my current thinking is to say that if we see $ not in a quote and not preceded by a valid identifier char then it is the start of a $foo$ sequence. Or have I missed something? Can we validly see $ in any other context?
I had missed one, though - numbered params in prepared statements. Fixed in attached patch.
Right, a $ should be considered to start a quote marker only if it's not part of an identifier. The backend lexer doesn't have a problem with this because it's written in flex, but I can imagine that getting it right in psql's ad-hoc parser might be tricky.
I think it's doable, though. I seem to have a working patch, which I will send out for review soon.
Proof of Concept patch (i.e. not for application) attached for review. The scanner changes are based on Tom's original, with some name/comment changes and a more liberal pattern. The psql changes are all my own work :-).
Comments welcome. Reviewers: I am not sure I got multi-byte stuff right in psql/mainloop.c - please pay close attention to that.
If I'm not wildly off course I will polish this up and start on docs.
cheers
andrew
Index: src/backend/parser/scan.l
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/parser/scan.l,v
retrieving revision 1.112
diff -c -w -r1.112 scan.l
*** src/backend/parser/scan.l 29 Nov 2003 19:51:52 -0000 1.112
--- src/backend/parser/scan.l 8 Feb 2004 19:59:24 -0000
***************
*** 39,44 ****
--- 39,46 ----
static int xcdepth = 0; /* depth of nesting in slash-star comments */
+ static char *dolqstart; /* current $foo$ quote start string */
+
/*
* literalbuf is used to accumulate literal values when multiple rules
* are needed to parse a single literal. Call startlit to reset buffer
***************
*** 95,100 ****
--- 97,103 ----
* <xd> delimited identifiers (double-quoted identifiers)
* <xh> hexadecimal numeric string
* <xq> quoted strings
+ * <dolq> $foo$-style quoted strings
*/
%x xb
***************
*** 102,107 ****
--- 105,111 ----
%x xd
%x xh
%x xq
+ %x dolq
/* Bit string
* It is tempting to scan the string for only those characters
***************
*** 141,146 ****
--- 145,159 ----
xqoctesc [\\][0-7]{1,3}
xqcat {quote}{whitespace_with_newline}{quote}
+ /* $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.
+ */
+ dolqdelim \$([A-Za-z\200-\377][A-Za-z\200-\377_0-9]*)?\$
+ dolqinside [^$]+
+
/* Double quote
* Allows embedded spaces and other special characters into identifiers.
*/
***************
*** 387,392 ****
--- 400,434 ----
}
<xq><<EOF>> { yyerror("unterminated quoted string"); }
+ {dolqdelim} {
+ token_start = yytext;
+ dolqstart = pstrdup(yytext);
+ BEGIN(dolq);
+ startlit();
+ }
+ <dolq>{dolqdelim} {
+ if (strcmp(yytext, dolqstart) == 0)
+ {
+ pfree(dolqstart);
+ BEGIN(INITIAL);
+ yylval.str = litbufdup();
+ return SCONST;
+ }
+ /*
+ * When we fail to match $...$ to dolqstart, transfer
+ * the $... part to the output, but put back the final
+ * $ for rescanning. Consider $delim$...$junk$delim$
+ */
+ addlit(yytext, yyleng-1);
+ yyless(yyleng-1);
+ }
+ <dolq>{dolqinside} {
+ addlit(yytext, yyleng);
+ }
+ <dolq>. {
+ addlitchar(yytext[0]);
+ }
+ <dolq><<EOF>> { yyerror("unterminated special-quoted string"); }
{xdstart} {
token_start = yytext;
Index: src/bin/psql/mainloop.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/mainloop.c,v
retrieving revision 1.61
diff -c -w -r1.61 mainloop.c
*** src/bin/psql/mainloop.c 25 Jan 2004 03:07:22 -0000 1.61
--- src/bin/psql/mainloop.c 8 Feb 2004 19:59:44 -0000
***************
*** 49,54 ****
--- 49,58 ----
unsigned int query_start;
volatile int count_eof = 0;
volatile unsigned int bslash_count = 0;
+ volatile bool free_dolquote = false;
+ char *dol_quote = NULL;
+ char *dol_end;
+
int i,
prevlen,
***************
*** 120,125 ****
--- 124,130 ----
in_quote = 0;
paren_level = 0;
count_eof = 0;
+ free_dolquote = true;
slashCmdStatus = CMD_UNKNOWN;
}
else
***************
*** 136,141 ****
--- 141,156 ----
pqsignal(SIGINT, handle_sigint); /* control-C => cancel
*/
#endif /* not WIN32 */
+ if (free_dolquote)
+ {
+ if(dol_quote)
+ {
+ free(dol_quote);
+ dol_quote = NULL;
+ }
+ free_dolquote = false;
+ }
+
fflush(stdout);
if (slashCmdStatus == CMD_NEWEDIT)
***************
*** 150,155 ****
--- 165,175 ----
in_xcomment = 0;
in_quote = 0;
paren_level = 0;
+ if(dol_quote)
+ {
+ free(dol_quote);
+ dol_quote = NULL;
+ }
slashCmdStatus = CMD_UNKNOWN;
}
***************
*** 161,167 ****
{
int prompt_status;
! if (in_quote && in_quote == '\'')
prompt_status = PROMPT_SINGLEQUOTE;
else if (in_quote && in_quote == '"')
prompt_status = PROMPT_DOUBLEQUOTE;
--- 181,189 ----
{
int prompt_status;
! if (dol_quote)
! prompt_status = PROMPT_DOLLARQUOTE;
! else if (in_quote && in_quote == '\'')
prompt_status = PROMPT_SINGLEQUOTE;
else if (in_quote && in_quote == '"')
prompt_status = PROMPT_DOUBLEQUOTE;
***************
*** 268,273 ****
--- 290,339 ----
in_quote = 0;
}
+ /*
+ * start of $foo$ type quote
+ * must be a $ not preceded by a valid identifier character,
+ * not inside a quote, not succeeded by a digit,
+ * and with a terminating $ somewhere on the line.
+ *
+ * if an invalid $foo$ meeting these requirements is seen, it
won't
+ * be trapped here but will instead generate an SQL syntax
error.
+ */
+
+ else if (!dol_quote && line[i] == '$' &&
+ !isdigit(line[i + thislen]) &&
+ (dol_end = strchr(line+i+1,'$')) != NULL &&
+ (i == 0 ||
+ ! ((line[i-1] & 0x80) != 0 ||
isalnum(line[i-1]) ||
+ line[i-1] == '_')))
+ {
+ char eos;
+
+ dol_end ++;
+ eos = *(dol_end);
+ *dol_end = '\0';
+ dol_quote = strdup(line+i);
+ *dol_end = eos;
+ ADVANCE_1;
+ while(line[i] != '$')
+ ADVANCE_1;
+
+ }
+
+ /* in or end of $foo$ type quote? */
+
+ else if (dol_quote)
+ {
+ if (strncmp(line+i,dol_quote,strlen(dol_quote)) == 0)
+ {
+ ADVANCE_1;
+ while(line[i] != '$')
+ ADVANCE_1;
+ free(dol_quote);
+ dol_quote = NULL;
+ }
+ }
+
/* start of extended comment? */
else if (line[i] == '/' && line[i + thislen] == '*')
{
***************
*** 291,297 ****
/* single-line comment? truncate line */
else if (line[i] == '-' && line[i + thislen] == '-')
{
! line[i] = '\0'; /* remove comment */
break;
}
--- 357,363 ----
/* single-line comment? truncate line */
else if (line[i] == '-' && line[i + thislen] == '-')
{
! line[i] = '\0'; /* removae comment */
break;
}
***************
*** 458,464 ****
/* Put the rest of the line in the query buffer. */
! if (in_quote || line[query_start + strspn(line + query_start, "
\t\n\r")] != '\0')
{
if (query_buf->len > 0)
appendPQExpBufferChar(query_buf, '\n');
--- 524,531 ----
/* Put the rest of the line in the query buffer. */
! if (in_quote || dol_quote ||
! line[query_start + strspn(line + query_start, " \t\n\r")] !=
'\0')
{
if (query_buf->len > 0)
appendPQExpBufferChar(query_buf, '\n');
Index: src/bin/psql/prompt.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/prompt.c,v
retrieving revision 1.34
diff -c -w -r1.34 prompt.c
*** src/bin/psql/prompt.c 25 Jan 2004 03:07:22 -0000 1.34
--- src/bin/psql/prompt.c 8 Feb 2004 19:59:44 -0000
***************
*** 85,90 ****
--- 85,91 ----
case PROMPT_CONTINUE:
case PROMPT_SINGLEQUOTE:
case PROMPT_DOUBLEQUOTE:
+ case PROMPT_DOLLARQUOTE:
case PROMPT_COMMENT:
case PROMPT_PAREN:
prompt_name = "PROMPT2";
***************
*** 198,203 ****
--- 199,207 ----
break;
case PROMPT_DOUBLEQUOTE:
buf[0] = '"';
+ break;
+ case PROMPT_DOLLARQUOTE:
+ buf[0] = '$';
break;
case PROMPT_COMMENT:
buf[0] = '*';
Index: src/bin/psql/prompt.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/prompt.h,v
retrieving revision 1.13
diff -c -w -r1.13 prompt.h
*** src/bin/psql/prompt.h 29 Nov 2003 19:52:07 -0000 1.13
--- src/bin/psql/prompt.h 8 Feb 2004 19:59:44 -0000
***************
*** 15,20 ****
--- 15,21 ----
PROMPT_COMMENT,
PROMPT_SINGLEQUOTE,
PROMPT_DOUBLEQUOTE,
+ PROMPT_DOLLARQUOTE,
PROMPT_PAREN,
PROMPT_COPY
} promptStatus_t;
---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend
