IF Commons GitHub

extensions/plpgsql/parse

The parser of PL/pgSQL (commons lang-sql 0006-sql-routines FR-005 to FR-008): the body of a PostgreSQL routine, read as blocks, declarations and statements, with the SQL it holds read by the core parser at its place in the whole text. It works on the tokens of the body, finds where each construct ends, and hands the expressions and queries to parseFragment; a statement it cannot read is a node pl-unparsed and an error value, and the statements after it are read.

Every offset is an offset of the whole text, so a node of a body and a node of the script around it are in one coordinate system, and the comments of the body are attached by the rule of the core.

const p = parse("CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END $$ LANGUAGE plpgsql", postgres);
const r = parsePlpgsql(p, postgres, p.script.statements[0]!);
r.block?.statements[0]?.kind; // "pl-return"

Spec: 0006-sql-routines FR-005, FR-006, FR-007, FR-008

Source: commons/lang-sql/src/extensions/plpgsql/parse.ts

2 of 4 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

PlParseinterfaceno test imports it
interface PlParse

What parsePlpgsql gives.

MemberTypeWhat it is
blockPlBlock | undefinedThe body as a block, or none where it holds no block.
errorsreadonly ParseError[]What could not be read, with spans in the whole text.
commentsreadonly Comment[]The comments of the body.
attachmentsTableOf<PlTree>Each comment's node and placement.
parsePlpgsqlfunction
function parsePlpgsql(parsed: Parsed, dialect: Dialect, node: Statement): PlParse | undefined

Reads the body of a routine or of a DO command of a parse as PL/pgSQL.

ParameterWhat it is
parsedwhat parse gave
dialecthow it was written
nodea create-routine or a DO command of its script

Returns: the body, or none where the statement has no body that can be read as PL/pgSQL

Imported by 9 tests of the run (9 passed, 0 failed): the library's results

parsePlpgsqlRangefunction
function parsePlpgsqlRange(text: string, dialect: Dialect, from: number, to: number): PlParse

Reads a range of a text as the body of a PL/pgSQL routine.

ParameterWhat it is
textthe whole text
dialecthow it is written (PostgreSQL's)
fromwhere the body begins
towhere it ends

Returns: its block, errors, comments and their attachment; it does not throw

Imported by 9 tests of the run (9 passed, 0 failed): the library's results

plpgsqlBodyOffunctionno test imports it
function plpgsqlBodyOf(parsed: Parsed, dialect: Dialect, node: Statement): { readonly from: number; readonly to: number; } | undefined

The text of a body, from where it begins to where it ends in the whole text, of a routine or a DO command; or none.

Titles, headings, exports, tables, columns and requirements. Esc closes.