apex-parser is a JavaScript parser specifically designed for the Salesforce Apex language, including support for Apex Triggers, and inline SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) queries. Built upon an ANTLR4 grammar, it provides a low-level parse tree representation of Apex code. The current stable version is 2.17.0, with regular updates addressing bug fixes, new Apex/SOQL/SOSL features, and dependency updates. Unlike higher-level tools, this library focuses solely on parsing, offering the raw parse tree for further analysis by downstream tools, rather than providing built-in semantic analysis. A key differentiator is its correct handling of Apex's case-insensitivity through a custom input stream. It is available as an NPM module for Node.js environments and a Maven package for JVMs.
npm install apex-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize the Apex Lexer and Parser, feed it Apex code, and obtain the root parse tree context for further traversal.
Ensure your code correctly interprets character positions based on Unicode code points if upgrading from older versions. Test any existing tools that consume character offsets.
Always wrap your input string with `new CaseInsensitiveInputStream(yourCodeString)` before passing it to the lexer.
Change `import { CommonTokenStream } from 'antlr4ts';` to `import { CommonTokenStream } from 'apex-parser';`.Consult the `SOSLParserTest` examples or the grammar for specific rules to use when parsing SOSL FIND queries in different contexts.
Wrap your input string in `new CaseInsensitiveInputStream(yourApexCode)` before creating the lexer.
Instead of `import { CommonTokenStream } from 'antlr4ts';`, use `import { CommonTokenStream } from 'apex-parser';`.Ensure your Node.js environment supports ES modules (Node.js >=12, with `.mjs` or `type: "module"` in `package.json`), or switch to CommonJS `require()` syntax: `const { ApexLexer } = require('apex-parser');`.No dependency data recorded yet.