@griffithswaite/ts-plsql-parser
v1.0.5
Published
PL/SQL parser in TypeScript. Generated using Antlr4.
Downloads
9
Readme
What is Antlr?
From the docs: "ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build parse trees and also generates a listener interface (or visitor) that makes it easy to respond to the recognition of phrases of interest."
Find out more about Antlr on its website.
Why would I want to parse PL/SQL?
Parsing code such as PL/SQL can be useful for a number of reasons, especially in Enterprise environments (or other environments where you have a lot of legacy code). For example:
- Generate documentation from your codebase.
- Create a list of all the functions and procedures in your codebase.
- Generate type-safe consumer code for your API.
- Find unused columns on your tables.
- You can use it... to deduce just about anything you want from your code.
Being able to easily parse PL/SQL code can be an incredibly powerful tool in your arsenal for interrogating what your code is doing. Combined with our PL/SQL Viewer, you can quickly and easily take advantage of legacy code by interrogating it.
Check out the examples within this repository to see how you can use this package to parse PL/SQL code.
To try out an example, you can pull the repo and after installing dependencies do the following:
node --loader=ts-node/esm examples/interface-generator/index.ts
Installation
npm install @griffithswaite/ts-plsql-parser
Usage
Antlr produces two methods for parsing code: a visitor and a listener. We also provide a method for parsing code into a single JSON representation of the tree (getParsedNodes
), this can be useful for quick parsing and debugging. You can read more about Antlr's visitor and listener patterns here.
The listener method is documented below in ESM format. If you're using CommonJS you may have to modify this code (such as removing the node:url import and __dirname definition).
Listener
import * as url from "node:url";
import { ParseTreeWalker, ParseTreeListener } from "antlr4";
import {
ParseTreeListener,
PlSqlParserListener,
getParserFromFile,
} from "@griffithswaite/ts-plsql-parser";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const parser = getParserFromFile(__dirname + "/your-code.pbp");
class YourListener
extends ParseTreeListener
implements PlSqlParserListener
{
enterUnit_statement(ctx: PlSqlParser.Unit_statementContext) {
// Do something when entering a unit_statement
}
}
const listener = new YourListener();
// Use the entry point for listeners
ParseTreeWalker.DEFAULT.walk(listener, parser.sql_script());