ANTLR4 graps (Grammar Parsing Service) is a TypeScript node module using the TypeScript ANTLR runtime (antlr4ts) created by Sam Harwell. At the time being this runtime is still considered alpha, while it works already quite well.
At its core this module manages source contexts for each ANTLR4 grammar source an application provides (usually files, but can also be strings in memory). It manages grammar dependencies and automatically loads them as required (either from the same folder as the requesting grammar or from a common lib folder).
A context is identfied by a unique string, which is usually the absolute file name, but can be any unique string (e.g. a UUID). Using a file name has the advantage that the module can load file content transparently, if no grammar code is given as string (especially important for dependencies).
The module comes with jars for parser generation (the main ANTLR jar and a derived one only for typescript) and can so generate all parser and lexer files for you (targets: Java, C++, C#, JS, Python, Typescript, Swift, Go). It will then parse all error messages and provide diagnostic informations, if problems were reported. Additionally the module does its own syntactic and semantic checks and can so provide a subset of the diagnostics available from ANTLR. These are provided on each reparse call, while ANTLR diagnostics are only available after a generation call (in this case the interal diagnostics are replaced by the external ones).
Here's a feature summary of the module:
- semi-automatic grammar management (manual loading/unloading of a main grammar, automatic loading of dependencies)
- (re)parsing of a grammar (and its dependencies), which collects internal diagnostics
- symbol lookup at a given position, rule name from position, enclosing range (the outer rule or the block containing a symbol)
- listing of all found symbols
- syntactic and semantic diagnostics (e.g. duplicate symbols, unknown symbols)
- symbol reference counts (usage counts), unreferenced rules
- a reference graph for all symbols
- code completion candidates (fairly stable now, might be a bit slow for large grammars)
- railroad diagrams for rules
- parser generation, including interpreter data generation, with flexible configuration (listeners, visitors, search/lib folder etc.)
- full semantic checks and reports by code generation (e.g. left-recursive rule detection etc.)
- built-in interpreters for parse runs without the need to compile the generated parser (not yet finished)
- ATN graph (nodes and links describing the ATN structure for a given rule)
- Sentence generation (random or all-path generation of sentences that are valid for a given grammar), mostly used for testing and grammar review.
You can find details about use and all available public functions and structures in the api reference file.
The GIT repository contains a test setup, which allows to run unit tests (via
mocha). Simply execute:
Additionally, there is a
mocha setup for Visual Studio Code, which even allows to debug the tests. Tests are however not shipped in the published node module.
see bug tracker
Also, the code completion feature is not fully ready yet and needs some improvements. Use it only for testing small grammars for now.
- Improve code completion
- Automatic code formatting
- Refactoring: extract rule, rename items, comment selected lines, remove left recursion
- Templates (for frequently used rules like whitespace, ID etc.)
- Support for debugging
I'd love to see code contributions, to make the module evolve quicker.
- Return also the rule index from
- Added support for sentence generation
- Travis build fixes
- Code completion improvements which now make this feature actually usable.
- Improvements for RRD scripts.
- Improvements for ATN graph scripts.
- antlr4-graps now ships with custom builds of antlr4 and antlr4-typescript jars, to be able to generate code.
- Added code generation support for grammars.
- Typscript code is no longer published.
- Added special ATN deserializer class for the typescript runtime that allows to load standard ATN data (antlr4ts uses an extended version).
- Added parser for antlr4 error message, which allows to integrate them into the module's diagnostics.
- Added ATN graph generation support.
- Doc updated to describe JS code, not TS code.
- Added support for symbol references (e.g. for references graphs).
- Updated doc and added unit tests for that.
- Now using antlr4-c3 for code completion + symbol tables.
- Added support for railroad diagrams.
- Updates for new antlr4ts version.
- Improved documenation
- Added token reference count support.
- New unit tests added for that.
- Enabled strict null checks for TS code.
- Radical change: switched from C++ to TypeScript backend (antlr4ts).
- Added more unit tests
- Better organization of exported types.
- Improved readme.md.
npm test works now.
- Improved symbol information, introduced SymbolKind enum.
- Visual Studio build fixes (C++).
1.0.6 Initial Release
This was the first public release (yet with a C++ backend). Implements the base project structure (source contexts).