From 92c1a5195149da907ec5aab9b9e1f0518e440bc6 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@2ndquadrant.com>
Date: Mon, 13 Jan 2020 17:57:25 +0800
Subject: [PATCH v11 2/2] Merge ECPG scanner states regarding C comments.

Previously, there were different start conditions for C-style comments
used in SQL and in C, since those have different rules regarding nested
comments. Since we already have the ability to keep track of the previous
start condition, use this to handle the different cases within a single
start condition. This matches the core scanner more closely.
---
 src/interfaces/ecpg/preproc/pgc.l | 74 ++++++++++++++++---------------
 1 file changed, 38 insertions(+), 36 deletions(-)

diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l
index daca8d9dd9..208ccd6c94 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
@@ -135,8 +134,7 @@ static struct _if_value
  */
 
 %x xb
-%x xcc
-%x xcsql
+%x xc
 %x xd
 %x xdc
 %x xh
@@ -419,54 +417,58 @@ cppline			{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
 {whitespace}	{
 					/* ignore */
 				}
+} /* <SQL> */
 
+<C,SQL>{
 {xcstart}		{
 					token_start = yytext;
 					state_before_str_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_str_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_str_start == SQL)
 					{
-						ECHO;
-						BEGIN(state_before_str_start);
-						token_start = NULL;
+						xcdepth++;
+						/* Put back any characters past slash-star; see above */
+						yyless(2);
+						fputs("/_*", yyout);
 					}
-					else
+					else if (state_before_str_start == C)
 					{
-						xcdepth--;
-						fputs("*_/", yyout);
+						ECHO;
 					}
 				}
-<xcc>{xcstop}	{
-					ECHO;
-					BEGIN(state_before_str_start);
-					token_start = NULL;
+
+{xcstop}		{
+					if (state_before_str_start == SQL)
+					{
+						if (xcdepth <= 0)
+						{
+							ECHO;
+							BEGIN(SQL);
+							token_start = NULL;
+						}
+						else
+						{
+							xcdepth--;
+							fputs("*_/", yyout);
+						}
+					}
+					else if (state_before_str_start == C)
+					{
+						ECHO;
+						BEGIN(C);
+						token_start = NULL;
+					}
 				}
 
-<xcc,xcsql>{
 {xcinside}		{
 					ECHO;
 				}
@@ -482,7 +484,7 @@ cppline			{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
 <<EOF>>			{
 					mmfatal(PARSE_ERROR, "unterminated /* comment");
 				}
-} /* <xcc,xcsql> */
+} /* <xc> */
 
 <SQL>{
 {xbstart}		{
-- 
2.22.0

