Commit bb1b8f69 authored by Tom Lane's avatar Tom Lane

De-reserve most statement-introducing keywords in plpgsql.

Add a bit of context sensitivity to plpgsql_yylex() so that it can
recognize when the word it is looking at is the first word of a new
statement, and if so whether it is the target of an assignment statement.
When we are at start of statement and it's not an assignment, we can
prefer recognizing unreserved keywords over recognizing variable names,
thereby allowing most statements' initial keywords to be demoted from
reserved to unreserved status.  This is rather useful already (there are
15 such words that get demoted here), and what's more to the point is
that future patches proposing to add new plpgsql statements can avoid
objections about having to add new reserved words.

The keywords BEGIN, DECLARE, FOR, FOREACH, LOOP, WHILE need to remain
reserved because they can be preceded by block labels, and the logic
added here doesn't understand about block labels.  In principle we
could probably fix that, but it would take more than one token of
lookback and the benefit doesn't seem worth extra complexity.

Also note I didn't de-reserve EXECUTE, because it is used in more places
than just statement start.  It's possible it could be de-reserved with
more work, but that would be an independent fix.

In passing, also de-reserve COLLATE and DEFAULT, which shouldn't have
been reserved in the first place since they only need to be recognized
within DECLARE sections.
parent bac27394
...@@ -2315,32 +2315,46 @@ unreserved_keyword : ...@@ -2315,32 +2315,46 @@ unreserved_keyword :
| K_ALIAS | K_ALIAS
| K_ARRAY | K_ARRAY
| K_BACKWARD | K_BACKWARD
| K_CLOSE
| K_COLLATE
| K_COLUMN | K_COLUMN
| K_COLUMN_NAME | K_COLUMN_NAME
| K_CONSTANT | K_CONSTANT
| K_CONSTRAINT | K_CONSTRAINT
| K_CONSTRAINT_NAME | K_CONSTRAINT_NAME
| K_CONTINUE
| K_CURRENT | K_CURRENT
| K_CURSOR | K_CURSOR
| K_DATATYPE | K_DATATYPE
| K_DEBUG | K_DEBUG
| K_DEFAULT
| K_DETAIL | K_DETAIL
| K_DIAGNOSTICS
| K_DUMP | K_DUMP
| K_ELSIF
| K_ERRCODE | K_ERRCODE
| K_ERROR | K_ERROR
| K_EXCEPTION
| K_EXIT
| K_FETCH
| K_FIRST | K_FIRST
| K_FORWARD | K_FORWARD
| K_GET
| K_HINT | K_HINT
| K_INFO | K_INFO
| K_INSERT
| K_IS | K_IS
| K_LAST | K_LAST
| K_LOG | K_LOG
| K_MESSAGE | K_MESSAGE
| K_MESSAGE_TEXT | K_MESSAGE_TEXT
| K_MOVE
| K_NEXT | K_NEXT
| K_NO | K_NO
| K_NOTICE | K_NOTICE
| K_OPEN
| K_OPTION | K_OPTION
| K_PERFORM
| K_PG_CONTEXT | K_PG_CONTEXT
| K_PG_DATATYPE_NAME | K_PG_DATATYPE_NAME
| K_PG_EXCEPTION_CONTEXT | K_PG_EXCEPTION_CONTEXT
...@@ -2349,8 +2363,10 @@ unreserved_keyword : ...@@ -2349,8 +2363,10 @@ unreserved_keyword :
| K_PRINT_STRICT_PARAMS | K_PRINT_STRICT_PARAMS
| K_PRIOR | K_PRIOR
| K_QUERY | K_QUERY
| K_RAISE
| K_RELATIVE | K_RELATIVE
| K_RESULT_OID | K_RESULT_OID
| K_RETURN
| K_RETURNED_SQLSTATE | K_RETURNED_SQLSTATE
| K_REVERSE | K_REVERSE
| K_ROW_COUNT | K_ROW_COUNT
......
This diff is collapsed.
...@@ -4906,6 +4906,20 @@ select unreserved_test(); ...@@ -4906,6 +4906,20 @@ select unreserved_test();
42 42
(1 row) (1 row)
create or replace function unreserved_test() returns int as $$
declare
return int := 42;
begin
return := return + 1;
return return;
end
$$ language plpgsql;
select unreserved_test();
unreserved_test
-----------------
43
(1 row)
drop function unreserved_test(); drop function unreserved_test();
-- --
-- Test FOREACH over arrays -- Test FOREACH over arrays
......
...@@ -3940,6 +3940,17 @@ $$ language plpgsql; ...@@ -3940,6 +3940,17 @@ $$ language plpgsql;
select unreserved_test(); select unreserved_test();
create or replace function unreserved_test() returns int as $$
declare
return int := 42;
begin
return := return + 1;
return return;
end
$$ language plpgsql;
select unreserved_test();
drop function unreserved_test(); drop function unreserved_test();
-- --
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment