thrift-parser-typescript
raw JSON → 0.0.5 verified Fri May 01 auth: no javascript
A TypeScript library for parsing Apache Thrift IDL files into an AST. Current stable version is 0.0.5, with irregular releases. It provides a complete ANTLR-based parser for .thrift files and exports the generated visitor, parser, lexer, and listener. This package is one of the few TypeScript-first Thrift parsers, ships its own type definitions, and is suitable for tooling like code generation or linting.
Common errors
error Cannot find module 'antlr4ts' ↓
cause Missing peer dependency antlr4ts.
fix
Run 'npm install antlr4ts' in your project.
error TypeError: Class extends value undefined is not a constructor or null ↓
cause Importing ThriftVisitor incorrectly or missing import.
fix
Use 'import { ThriftVisitor } from 'thrift-parser-typescript'.
error Module not found: Can't resolve 'thrift-parser-typescript' ↓
cause Package not installed or import path incorrect.
fix
Run 'npm install thrift-parser-typescript' and check import path.
error Cannot read property 'document' of undefined ↓
cause Parser not instantiated correctly (missing tokens or input).
fix
Ensure you create CommonTokenStream and pass to ThriftParser constructor.
Warnings
gotcha The package requires antlr4ts as a peer dependency; not installing it leads to runtime errors. ↓
fix Install antlr4ts: npm install antlr4ts
breaking In v0.0.4, exports were reorganized; ThriftLexer and ThriftParser moved from subpaths to root. ↓
fix Update imports to use root package path.
gotcha The package does not export a 'parse' convenience function; users must manually wire ANTLRInputStream, lexer, tokens, and parser. ↓
fix Write boilerplate to create the parser pipeline as shown in quickstart.
deprecated Importing from 'thrift-parser-typescript/dist/*' is deprecated but still works; prefer root imports. ↓
fix Use import { ... } from 'thrift-parser-typescript'.
Install
npm install thrift-parser-typescript yarn add thrift-parser-typescript pnpm add thrift-parser-typescript Imports
- ThriftLexer wrong
const ThriftLexer = require('thrift-parser-typescript').ThriftLexercorrectimport { ThriftLexer } from 'thrift-parser-typescript' - ThriftParser wrong
import ThriftParser from 'thrift-parser-typescript'correctimport { ThriftParser } from 'thrift-parser-typescript' - ThriftVisitor wrong
import { ThriftVisitor } from 'thrift-parser-typescript/dist/ThriftVisitor'correctimport { ThriftVisitor } from 'thrift-parser-typescript' - ThriftListener wrong
const ThriftListener = require('thrift-parser-typescript').ThriftListenercorrectimport { ThriftListener } from 'thrift-parser-typescript'
Quickstart
import { ThriftLexer, ThriftParser, ThriftVisitor } from 'thrift-parser-typescript';
const input = `struct User { 1: required string name; 2: optional i32 age; }`;
const chars = new (require('antlr4ts').ANTLRInputStream)(input);
const lexer = new ThriftLexer(chars);
const tokens = new (require('antlr4ts').CommonTokenStream)(lexer);
const parser = new ThriftParser(tokens);
const tree = parser.document();
class MyVisitor extends ThriftVisitor<string> {
visitStruct(ctx: any): string {
return `struct ${ctx.name}`;
}
}
const result = new MyVisitor().visit(tree);
console.log(result);