From 8b0e1fd8c797917de0b445d2497fe3d12e2ba03c Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@2ndquadrant.com>
Date: Fri, 12 Jul 2019 14:05:17 +0700
Subject: [PATCH v6 2/2] Merge ECPG scanner states for C-style comments

This makes the ECPG scanner more similar to the backend scanner.
In passing, make some cosmetic adjustments to reduce the diffs
between the three core scanners.
---
 src/backend/parser/scan.l         |  2 +-
 src/fe_utils/psqlscan.l           |  2 +-
 src/interfaces/ecpg/preproc/pgc.l | 75 ++++++++++++++++---------------
 3 files changed, 40 insertions(+), 39 deletions(-)

diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l
index d2ccb438f6..4df96267cc 100644
--- a/src/backend/parser/scan.l
+++ b/src/backend/parser/scan.l
@@ -769,7 +769,7 @@ other			.
 					yylval->str = ident;
 					return IDENT;
 				}
-<xui>{dquote} {
+<xui>{dquote}	{
 					if (yyextra->literallen == 0)
 						yyerror("zero-length delimited identifier");
 
diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l
index a66c0f4c6e..85d179c421 100644
--- a/src/fe_utils/psqlscan.l
+++ b/src/fe_utils/psqlscan.l
@@ -610,7 +610,7 @@ other			.
 					BEGIN(INITIAL);
 					ECHO;
 				}
-<xui>{dquote} {
+<xui>{dquote}	{
 					/* xuend state looks for possible UESCAPE */
 					yyextra->state_before_lit_stop = YYSTATE;
 					BEGIN(xuend);
diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l
index 1eefbc05f6..a9a170df5b 100644
--- a/src/interfaces/ecpg/preproc/pgc.l
+++ b/src/interfaces/ecpg/preproc/pgc.l
@@ -111,8 +111,7 @@ static struct _if_value
  * and to eliminate parsing troubles for numeric strings.
  * Exclusive states:
  *  <xb> bit string literal
- *  <xcc> extended C-style comments in C
- *  <xcsql> extended C-style comments in SQL
+ *  <xc> extended C-style comments
  *  <xd> delimited identifiers (double-quoted identifiers)
  *  <xdc> double-quoted strings in C
  *  <xh> hexadecimal numeric string
@@ -138,8 +137,7 @@ static struct _if_value
  */
 
 %x xb
-%x xcc
-%x xcsql
+%x xc
 %x xd
 %x xdc
 %x xh
@@ -434,54 +432,58 @@ cppline			{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
 {whitespace}	{
 					/* ignore */
 				}
+} /* <SQL> */
 
+<C,SQL>{
 {xcstart}		{
 					token_start = yytext;
 					state_before_lit_start = YYSTATE;
 					xcdepth = 0;
-					BEGIN(xcsql);
+					BEGIN(xc);
 					/* Put back any characters past slash-star; see above */
 					yyless(2);
 					fputs("/*", yyout);
 				}
-} /* <SQL> */
+} /* <C,SQL> */
 
-<C>{xcstart}	{
-					token_start = yytext;
-					state_before_lit_start = YYSTATE;
-					xcdepth = 0;
-					BEGIN(xcc);
-					/* Put back any characters past slash-star; see above */
-					yyless(2);
-					fputs("/*", yyout);
-				}
-<xcc>{xcstart}	{ ECHO; }
-<xcsql>{xcstart}	{
-					xcdepth++;
-					/* Put back any characters past slash-star; see above */
-					yyless(2);
-					fputs("/_*", yyout);
-				}
-<xcsql>{xcstop}	{
-					if (xcdepth <= 0)
+<xc>{
+{xcstart}		{
+					if (state_before_lit_start == SQL)
 					{
-						ECHO;
-						BEGIN(state_before_lit_start);
-						token_start = NULL;
+						xcdepth++;
+						/* Put back any characters past slash-star; see above */
+						yyless(2);
+						fputs("/_*", yyout);
 					}
-					else
+					else if (state_before_lit_start == C)
 					{
-						xcdepth--;
-						fputs("*_/", yyout);
+						ECHO;
 					}
 				}
-<xcc>{xcstop}	{
-					ECHO;
-					BEGIN(state_before_lit_start);
-					token_start = NULL;
+
+{xcstop}		{
+					if (state_before_lit_start == SQL)
+					{
+						if (xcdepth <= 0)
+						{
+							ECHO;
+							BEGIN(SQL);
+							token_start = NULL;
+						}
+						else
+						{
+							xcdepth--;
+							fputs("*_/", yyout);
+						}
+					}
+					else if (state_before_lit_start == C)
+					{
+						ECHO;
+						BEGIN(C);
+						token_start = NULL;
+					}
 				}
 
-<xcc,xcsql>{
 {xcinside}		{
 					ECHO;
 				}
@@ -497,7 +499,7 @@ cppline			{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
 <<EOF>>			{
 					mmfatal(PARSE_ERROR, "unterminated /* comment");
 				}
-} /* <xcc,xcsql> */
+} /* <xc> */
 
 <SQL>{
 {xbstart}		{
@@ -732,7 +734,6 @@ cppline			{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
 					base_yylval.str = mm_strdup(literalbuf);
 					return CSTRING;
 				}
-
 <xui>{dquote}	{
 					if (literallen == 2) /* "U&" */
 						mmerror(PARSE_ERROR, ET_ERROR, "zero-length delimited identifier");
-- 
2.17.2 (Apple Git-113)

