From 063560bb8e1e3658df858266d593a0ee2d053423 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 6 Jan 2007 19:14:17 +0000
Subject: [PATCH] Fix filtered_base_yylex() to save and restore base_yylval and
 base_yylloc properly when doing a lookahead.  The lack of this was causing
 various interesting misbehaviors when one tries to use "with" as a plain
 identifier.

---
 src/backend/parser/parser.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/backend/parser/parser.c b/src/backend/parser/parser.c
index 8490f8ab8b8..c007613cc4e 100644
--- a/src/backend/parser/parser.c
+++ b/src/backend/parser/parser.c
@@ -14,7 +14,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/parser/parser.c,v 1.69 2007/01/05 22:19:34 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/parser/parser.c,v 1.70 2007/01/06 19:14:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -28,8 +28,10 @@
 
 List	   *parsetree;			/* result of parsing is left here */
 
-static int	lookahead_token;	/* one-token lookahead */
-static bool have_lookahead;		/* lookahead_token set? */
+static bool have_lookahead;			/* is lookahead info valid? */
+static int	lookahead_token;		/* one-token lookahead */
+static YYSTYPE lookahead_yylval;	/* yylval for lookahead token */
+static YYLTYPE lookahead_yylloc;	/* yylloc for lookahead token */
 
 
 /*
@@ -77,11 +79,16 @@ int
 filtered_base_yylex(void)
 {
 	int			cur_token;
+	int			next_token;
+	YYSTYPE		cur_yylval;
+	YYLTYPE		cur_yylloc;
 
 	/* Get next token --- we might already have it */
 	if (have_lookahead)
 	{
 		cur_token = lookahead_token;
+		base_yylval = lookahead_yylval;
+		base_yylloc = lookahead_yylloc;
 		have_lookahead = false;
 	}
 	else
@@ -102,8 +109,10 @@ filtered_base_yylex(void)
 			 * (perhaps for SQL99 recursive queries), come back and simplify
 			 * this code.
 			 */
-			lookahead_token = base_yylex();
-			switch (lookahead_token)
+			cur_yylval = base_yylval;
+			cur_yylloc = base_yylloc;
+			next_token = base_yylex();
+			switch (next_token)
 			{
 				case CASCADED:
 					cur_token = WITH_CASCADED;
@@ -115,7 +124,14 @@ filtered_base_yylex(void)
 					cur_token = WITH_CHECK;
 					break;
 				default:
+					/* save the lookahead token for next time */
+					lookahead_token = next_token;
+					lookahead_yylval = base_yylval;
+					lookahead_yylloc = base_yylloc;
 					have_lookahead = true;
+					/* and back up the output info to cur_token */
+					base_yylval = cur_yylval;
+					base_yylloc = cur_yylloc;
 					break;
 			}
 			break;
-- 
GitLab